public void SendJpegFormat()
        {
            var serverSession = new Mock <IVncServerSession>();

            serverSession
            .Setup(s => s.ClientEncodings)
            .Returns(new List <VncEncoding>()
            {
                VncEncoding.TightQualityLevel4,
            });

            TightEncoder encoder = new TightEncoder(serverSession.Object)
            {
                Compression = TightCompression.Jpeg
            };

            byte[] raw = null;

            using (MemoryStream output = new MemoryStream())
            {
                var contents = new byte[512];
                encoder.Send(output, VncPixelFormat.RGB32, new VncRectangle()
                {
                    Width = 1, Height = 1
                }, contents);
                raw = output.ToArray();
            }

            Assert.Equal((byte)TightCompressionControl.JpegCompression, raw[0]);

            // Make sure the compressed image is a valid JPEG image,
            // by checking for the JPEG magic.
            Assert.Equal(0xFF, raw[3]);
            Assert.Equal(0xD8, raw[4]);
        }
示例#2
0
        public void SendTestStandardPixelFormat()
        {
            TightEncoder encoder = new TightEncoder(Mock.Of <IVncServerSession>())
            {
                Compression = TightCompression.Basic,
            };

            byte[] raw = null;

            using (MemoryStream output = new MemoryStream())
            {
                var contents = Encoding.ASCII.GetBytes("hello, world\n");
                encoder.Send(output, new VncPixelFormat(32, 24, 8, 0, 8, 0, 8, 0, false, true), default(VncRectangle), contents);
                raw = output.ToArray();
            }

            // First byte:
            // - Basic compression
            // - Use stream 0
            // - Reset stream 0
            Assert.Equal(0b0000_0001, raw[0]);

            byte[] expectedZlibData = new byte[] { 0x78, 0x9c, 0xcb, 0x48, 0xcd, 0xc9, 0xc9, 0xd7, 0x51, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00, 0x21, 0xe7, 0x04, 0x93 };
            byte[] actualZlibData   = new byte[raw.Length - 2];
            Array.Copy(raw, 2, actualZlibData, 0, raw.Length - 2);

            Assert.Equal((byte)expectedZlibData.Length, raw[1]);
            Assert.Equal(expectedZlibData, actualZlibData);
        }
        public void SendSmallRectangleFormat()
        {
            TightEncoder encoder = new TightEncoder(Mock.Of <IVncServerSession>())
            {
                Compression = TightCompression.Basic,
            };

            byte[] raw = null;

            using (MemoryStream output = new MemoryStream())
            {
                var contents = new byte[] { 0x01, 0x02, 0x03, 0x04 };
                encoder.Send(output, new VncPixelFormat(), new VncRectangle(), contents);
                raw = output.ToArray();
            }

            // First byte:
            // - Basic compression
            // - Use stream 0
            // - Reset stream 0
            Assert.Equal(0b0000_0000, raw[0]);

            byte[] expectedZlibData = new byte[] { 0x01, 0x02, 0x03, 0x04 };

            byte[] actualZlibData = new byte[raw.Length - 2];
            Array.Copy(raw, 2, actualZlibData, 0, raw.Length - 2);

            Assert.Equal((byte)expectedZlibData.Length, raw[1]);
            Assert.Equal(expectedZlibData, actualZlibData);
        }
示例#4
0
        public void SendTightPixelFormat()
        {
            TightEncoder encoder = new TightEncoder(Mock.Of <IVncServerSession>())
            {
                Compression = TightCompression.Basic,
            };

            byte[] raw = null;

            using (MemoryStream output = new MemoryStream())
            {
                var contents = new byte[]
                {
                    // A 2x2 framebuffer in
                    // BGRX format
                    0x01, 0x02, 0x03, 0x04,
                    0x01, 0x02, 0x03, 0x04,
                    0x01, 0x02, 0x03, 0x04,
                    0x01, 0x02, 0x03, 0x04,
                };

                encoder.Send(output, VncPixelFormat.RGB32, new VncRectangle(0, 0, 2, 2), contents);
                raw = output.ToArray();
            }

            // First byte:
            // - Basic compression
            // - Use stream 0
            // - Reset stream 0
            Assert.Equal(0b0000_0001, raw[0]);

            byte[] expectedZlibData = new byte[] { 0x78, 0x9c, 0x62, 0x66, 0x62, 0x64, 0x86, 0x20, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x62, 0x02, 0x00, 0x00, 0x00, 0xff, 0xff, 0x62, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff };

            byte[] actualZlibData = new byte[raw.Length - 2];
            Array.Copy(raw, 2, actualZlibData, 0, raw.Length - 2);

            Assert.Equal((byte)expectedZlibData.Length, raw[1]);
            Assert.Equal(expectedZlibData, actualZlibData);

            // Decompress the data and make sure the pixel format is correct (RGB, the 'VncPixelFormat.RGB32' actually represents BGRX)
            using (var compressedStream = new MemoryStream(actualZlibData))
                using (var stream = new ZlibStream(compressedStream, CompressionMode.Decompress))
                {
                    byte[] decompressed = new byte[0x09];
                    Assert.Equal(9, stream.Read(decompressed, 0, 9));

                    // Make sure all data was read
                    Assert.Equal(0, stream.Read(decompressed, 0, 0));

                    var expectedDecompressedData = new byte[]
                    {
                        0x03, 0x02, 0x01,
                        0x03, 0x02, 0x01,
                        0x03, 0x02, 0x01,
                    };

                    Assert.Equal(expectedDecompressedData, decompressed);
                }
        }
示例#5
0
        public void SendSmallRectangleFormat()
        {
            TightEncoder encoder = new TightEncoder(Mock.Of <IVncServerSession>())
            {
                Compression = TightCompression.Basic,
            };

            byte[] raw = null;

            using (MemoryStream output = new MemoryStream())
            {
                var contents = new byte[] { 0x01, 0x02, 0x03, 0x04 };
                encoder.Send(output, new VncPixelFormat(), default, contents);
示例#6
0
        public void WriteEncodedValueTest()
        {
            byte[] buffer = new byte[4];

            for (int i = 0; i < 64; i++)
            {
                Assert.Equal(1, TightEncoder.WriteEncodedValue(buffer, 0, i));
                Assert.Equal((byte)i, buffer[0]);
            }

            Assert.Equal(2, TightEncoder.WriteEncodedValue(buffer, 0, 10_000));
            Assert.Equal(0x90, buffer[0]);
            Assert.Equal(0x4E, buffer[1]);
        }
        public void GetTightQualityLevelTest(int expectedQualityLevel, VncEncoding encoding)
        {
            Collection <VncEncoding> encodings = new Collection <VncEncoding>();

            encodings.Add(VncEncoding.Raw);
            encodings.Add(VncEncoding.Tight);
            encodings.Add(encoding);

            var mock = new Mock <IVncServerSession>();

            mock
            .Setup(m => m.ClientEncodings)
            .Returns(encodings);

            var compressionLevel = TightEncoder.GetQualityLevel(mock.Object);

            Assert.Equal(expectedQualityLevel, compressionLevel);
        }