示例#1
0
        public void TinyPacket_Decode_Null()
        {
            TinyPacket actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test  null "));

            Assert.AreEqual("Example", actual.Product);
            Assert.AreEqual("Test", actual.Operation);
        }
示例#2
0
        public void TinyPacket_EncryptedWithoutIdentifiers()
        {
            var packet = new TinyPacket("Example", "Test");

            var key     = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            var bytesE1 = packet.GetBytes(key, omitIdentifiers: true);
            var bytesE2 = packet.GetBytes(key, omitIdentifiers: true);

            Assert.AreEqual("Tiny128 ", Encoding.UTF8.GetString(bytesE1, 0, 8));
            Assert.AreEqual("Tiny128 ", Encoding.UTF8.GetString(bytesE2, 0, 8));
            Assert.AreNotEqual(BitConverter.ToString(bytesE1), BitConverter.ToString(bytesE2));

            var packet1a = TinyPacket.Parse(bytesE1, key);
            var packet2a = TinyPacket.Parse(bytesE2, key);

            Assert.IsNull(packet1a[".Id"]);
            Assert.IsNull(packet1a[".Host"]);
            Assert.IsNull(packet2a[".Id"]);
            Assert.IsNull(packet2a[".Host"]);

            var packet1b = packet1a.Clone();
            var packet2b = packet2a.Clone();

            Assert.IsNull(packet1b[".Id"]);
            Assert.IsNull(packet1b[".Host"]);
            Assert.IsNull(packet2b[".Id"]);
            Assert.IsNull(packet2b[".Host"]);

            var bytesP1 = packet1b.GetBytes(null, omitIdentifiers: true);
            var bytesP2 = packet2b.GetBytes(null, omitIdentifiers: true);

            Assert.AreEqual("Tiny ", Encoding.UTF8.GetString(bytesP1, 0, 5));
            Assert.AreEqual("Tiny ", Encoding.UTF8.GetString(bytesP2, 0, 5));
            Assert.AreEqual(Encoding.UTF8.GetString(bytesP1), Encoding.UTF8.GetString(bytesP2));
        }
示例#3
0
        public void TinyPacket_Decode_MissingData_02()   //it is an error state, but we shall recognize it.
        {
            TinyPacket actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test"));

            Assert.AreEqual("Example", actual.Product);
            Assert.AreEqual("Test", actual.Operation);
        }
示例#4
0
        public void TinyPacket_Decode_01()
        {
            var actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test {""Key1Text"":""Value1Text"",""Key2Text"":""Value2Text""}  "));

            Assert.AreEqual("Example", actual.Product);
            Assert.AreEqual("Test", actual.Operation);
            Assert.AreEqual("Value1Text", actual["Key1Text"]);
            Assert.AreEqual("Value2Text", actual["Key2Text"]);
        }
示例#5
0
        public void TinyPacket_Decode_NullItem_01A()
        {
            TinyPacket actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test {""A"":""1"",""B"":null,""C"":""2""}"));

            Assert.AreEqual("Example", actual.Product);
            Assert.AreEqual("Test", actual.Operation);
            Assert.AreEqual("1", actual["A"]);
            Assert.AreEqual(null, actual["B"]);
            Assert.AreEqual("2", actual["C"]);
        }
示例#6
0
        public void TinyPacket_Decode_02()
        {
            TinyPacket actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test {""Key1"":""A\n\rB"",""Key2"":""\u0000"",""Key3"":""\\""}"));

            Assert.AreEqual("Example", actual.Product);
            Assert.AreEqual("Test", actual.Operation);
            Assert.AreEqual("A\n\rB", actual["Key1"]);
            Assert.AreEqual("\0", actual["Key2"]);
            Assert.AreEqual("\\", actual["Key3"]);
        }
示例#7
0
        public void TinyPacket_Decode_Indexer()
        {
            var actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test {""A"":""0"",""A"":""1"",""B"":""null"",""B"":null}"));

            Assert.AreEqual("Example", actual.Product);
            Assert.AreEqual("Test", actual.Operation);
            Assert.AreEqual("1", actual["A"]);
            Assert.AreEqual(null, actual["B"]);
            Assert.AreEqual(null, actual["C"]);
        }
示例#8
0
        public void TinyPacket_Decode_01()
        {
            var data = new Dictionary <string, string>();

            data.Add("Key1Text", "Value1Text");
            data.Add("Key2Text", "Value2Text");

            TinyPacket actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny Example Test {""Key1Text"":""Value1Text"",""Key2Text"":""Value2Text""}  "));

            Assert.AreEqual("Example", actual.Product);
            Assert.AreEqual("Test", actual.Operation);
            Assert.AreEqual("Value1Text", actual["Key1Text"]);
            Assert.AreEqual("Value2Text", actual["Key2Text"]);
        }
示例#9
0
        public void TinyPacket_Cloning()
        {
            var packet = new TinyPacket("Example", "Test")
            {
                { "A", "1" }, { "B", "2" }
            };

            var key    = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            var bytesE = packet.GetBytes(key, omitIdentifiers: false);

            var packetA = TinyPacket.Parse(bytesE, key);

            Assert.IsNull(packetA[".Id"]);
            Assert.IsNotNull(packetA[".Host"]);
            Assert.IsNotNull(packetA["A"]);
            Assert.IsNotNull(packetA["B"]);

            var packetB = packetA.Clone();

            Assert.IsNull(packetB[".Id"]);
            Assert.IsNull(packetB[".Host"]);
            Assert.IsNotNull(packetA["A"]);
            Assert.IsNotNull(packetA["B"]);

            var packetC = TinyPacket.Parse(packetB.GetBytes());

            Assert.IsNotNull(packetC[".Id"]);
            Assert.IsNotNull(packetC[".Host"]);
            Assert.IsNotNull(packetA["A"]);
            Assert.IsNotNull(packetA["B"]);

            var packetD = packetC.Clone();

            var bytesB = packetB.GetBytes(null, omitIdentifiers: true);
            var bytesD = packetD.GetBytes(null, omitIdentifiers: true);

            Assert.AreEqual(Encoding.UTF8.GetString(bytesB), Encoding.UTF8.GetString(bytesD));
        }
示例#10
0
        public void TinyPacket_EncodeDecode_SpeedTest()
        {
            TinyPacket target = null;

            byte[] bytes = null;

            var nEncodePlain  = 0;
            var swEncodePlain = Stopwatch.StartNew();

            while (swEncodePlain.ElapsedMilliseconds < 500)
            {
                target = new TinyPacket("Example", "Test")
                {
                    { "Key1Text", "Value1Text" },
                    { "Key2Text", "Value2Text" }
                };
                bytes         = target.GetBytes();
                nEncodePlain += 1;
            }
            swEncodePlain.Stop();
            TestContext.WriteLine(string.Format("TinyPacket.Encode.Plain: {0:#,##0}K packets/second", (double)nEncodePlain / swEncodePlain.ElapsedMilliseconds));

            var nDecodePlain  = 0;
            var swDecodePlain = Stopwatch.StartNew();

            while (swDecodePlain.ElapsedMilliseconds < 500)
            {
                var target2 = TinyPacket.Parse(bytes);
                nDecodePlain += 1;
            }
            swDecodePlain.Stop();
            TestContext.WriteLine(string.Format("TinyPacket.Decode.Plain: {0:#,##0}K packets/second", (double)nDecodePlain / swDecodePlain.ElapsedMilliseconds));

            var key = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

            var nEncodeAes  = 0;
            var swEncodeAes = Stopwatch.StartNew();

            while (swEncodeAes.ElapsedMilliseconds < 500)
            {
                target = new TinyPacket("Example", "Test")
                {
                    { "Key1Text", "Value1Text" },
                    { "Key2Text", "Value2Text" }
                };
                bytes       = target.GetBytes(key);
                nEncodeAes += 1;
            }
            swEncodeAes.Stop();
            TestContext.WriteLine(string.Format("TinyPacket.Encode.AES: {0:#,##0}K packets/second", (double)nEncodeAes / swEncodeAes.ElapsedMilliseconds));

            Assert.AreEqual("Tiny128 ", Encoding.UTF8.GetString(bytes, 0, 8));

            var nDecodeAes  = 0;
            var swDecodeAes = Stopwatch.StartNew();

            while (swDecodeAes.ElapsedMilliseconds < 500)
            {
                var target2 = TinyPacket.Parse(bytes, key);
                nDecodeAes += 1;
            }
            swDecodeAes.Stop();
            TestContext.WriteLine(string.Format("TinyPacket.Decode.AES: {0:#,##0}K packets/second", (double)nDecodeAes / swDecodeAes.ElapsedMilliseconds));
        }
示例#11
0
 public void TinyPacket_Decode_Error_03()
 {
     TinyPacket actual = TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@""));
 }
示例#12
0
 public void TinyPacket_Decode_Error_02()
 {
     TinyPacket.Parse(UTF8Encoding.UTF8.GetBytes(@"Tiny "));
 }
示例#13
0
        public static TinyPacket Receive()
        {
            Pipe.Connect();

            while (Pipe.HasBytesToRead == false)
            {
                Thread.Sleep(100);
            }
            var buffer = Pipe.ReadAvailable();

            var packet = TinyPacket.Parse(buffer);

            if (packet != null)
            {
                if (packet.Product != "VhdAttach")
                {
                    return(null);
                }
                try {
                    switch (packet.Operation)
                    {
                    case "Attach":
                        ReceivedAttach(packet);
                        return(GetResponse(packet));

                    case "Detach":
                        ReceivedDetach(packet);
                        return(GetResponse(packet));

                    case "DetachDrive":
                        ReceivedDetachDrive(packet);
                        return(GetResponse(packet));

                    case "WriteContextMenuVhdSettings":
                        ReceivedWriteContextMenuVhdSettings(packet);
                        return(GetResponse(packet));

                    case "WriteContextMenuIsoSettings":
                        ReceivedWriteContextMenuIsoSettings(packet);
                        return(GetResponse(packet));

                    case "WriteAutoAttachSettings":
                        ReceivedWriteAutoAttachSettings(packet);
                        return(GetResponse(packet));

                    case "RegisterExtensionVhd":
                        ReceivedRegisterExtensionVhd();
                        return(GetResponse(packet));

                    case "RegisterExtensionIso":
                        ReceivedRegisterExtensionIso();
                        return(GetResponse(packet));

                    case "ChangeDriveLetter":
                        ReceivedChangeDriveLetter(packet);
                        return(GetResponse(packet));

                    default: throw new InvalidOperationException("Unknown command.");
                    }
                } catch (InvalidOperationException ex) {
                    return(GetResponse(packet, ex));
                }
            }
            else
            {
                return(null);
            }
        }