Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
0
        public static void Reply(TinyPacket response)
        {
            var buffer = response.GetBytes();

            Pipe.Write(buffer);
            Pipe.Flush();
            Pipe.Disconnect();
        }
Ejemplo n.º 3
0
        public void TinyPacket_Encode_Empty()
        {
            var target = new TinyPacket("Example", "Test");

            var actual = UTF8Encoding.UTF8.GetString(target.GetBytes(null, omitIdentifiers: true));

            Assert.AreEqual(@"Tiny Example Test {}", actual);
        }
Ejemplo n.º 4
0
        public void TinyPacket_Encode_01()
        {
            var target = new TinyPacket("Example", "Test");

            target["Key1Text"] = "Value1Text";
            target["Key2Text"] = "Value2Text";

            var actual = UTF8Encoding.UTF8.GetString(target.GetBytes(null, omitIdentifiers: true));

            Assert.AreEqual(@"Tiny Example Test {""Key1Text"":""Value1Text"",""Key2Text"":""Value2Text""}", actual);
        }
Ejemplo n.º 5
0
        public void TinyPacket_Encode_Indexer()
        {
            var target = new TinyPacket("Example", "Test");

            target["A"] = "0";
            target["A"] = "1";

            var actual = UTF8Encoding.UTF8.GetString(target.GetBytes(null, omitIdentifiers: true));

            Assert.AreEqual(@"Tiny Example Test {""A"":""1""}", actual);
        }
Ejemplo n.º 6
0
        public void TinyPacket_Encode_NullItem_01()
        {
            var target = new TinyPacket("Example", "Test");

            target["A"] = "1";
            target["B"] = null;
            target["C"] = "2";

            var actual = UTF8Encoding.UTF8.GetString(target.GetBytes(null, omitIdentifiers: true));

            Assert.AreEqual(@"Tiny Example Test {""A"":""1"",""B"":null,""C"":""2""}", actual);
        }
Ejemplo n.º 7
0
        public void TinyPacket_Encode_02()
        {
            var target = new TinyPacket("Example", "Test");

            target["Key1"] = "A\n\rB";
            target["Key2"] = "\0";
            target["Key3"] = "\\";

            var actual = UTF8Encoding.UTF8.GetString(target.GetBytes(null, omitIdentifiers: true));

            Assert.AreEqual(@"Tiny Example Test {""Key1"":""A\n\rB"",""Key2"":""\u0000"",""Key3"":""\\""}", actual);
        }
Ejemplo n.º 8
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));
        }
Ejemplo n.º 9
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));
        }
Ejemplo n.º 10
0
 public static void Reply(TinyPacket response)
 {
     var buffer = response.GetBytes();
     Pipe.Write(buffer);
     Pipe.Flush();
     Pipe.Disconnect();
 }