示例#1
0
        public void wrongLengthOfAttributes()
        {
            byte[] reference = new byte[] {
                0x00, 0x01, 0x00, 0x2C, 0x21, 0x12, 0xA4, 0x42, 0x0A, 0x14, 0x1E, 0x28, 0x32, 0x3C, 0x46, 0x50,
                0x5A, 0x64, 0x6E, 0x78, 0x00, 0x06, 0x00, 0x03, 0x61, 0x3A, 0x62, 0x00, 0x00, 0x24, 0x00, 0x04,
                0x6E, 0x7F, 0x1E, 0xFF, 0x00, 0x25, 0x00, 0x00, 0x00, 0x08, 0x00, 0x14, 0xF5, 0xC6, 0x0F, 0x17,
                0xF5, 0xBB, 0xC0, 0x2D, 0xA6, 0xDE, 0x64, 0x4B, 0x36, 0xF8, 0xB6, 0xBE, 0x79, 0xA0, 0xA6, 0x16
            };

            STUNMessageParser parsed;

            Assert.IsTrue(STUNMessageParser.TryParse(new ByteBuffer(reference), out parsed));

            // Length too long
            reference[3] = 0x3c;
            Assert.IsFalse(STUNMessageParser.TryParse(new ByteBuffer(reference), out parsed));

            // Length too short
            reference[3] = 0x04;
            Assert.IsFalse(STUNMessageParser.TryParse(new ByteBuffer(reference), out parsed));

            // Length not % 4
            reference[3] = 0x2d;
            Assert.IsFalse(STUNMessageParser.TryParse(new ByteBuffer(reference), out parsed));
        }
示例#2
0
        public void ShortHeader()
        {
            List <STUNAttr>   attrs  = new List <STUNAttr>();
            STUNMessageParser parser = new STUNMessageParser(new ByteBuffer(new byte[19]), false, attrs);

            Assert.IsFalse(parser.isValid);
        }
示例#3
0
        public void NoTLV()
        {
            byte[] header = new byte[20];

            List <STUNAttr>   attrs  = new List <STUNAttr>();
            STUNMessageParser parser = new STUNMessageParser(new ByteBuffer(header), false, attrs);

            Assert.IsFalse(parser.isValid);
        }
示例#4
0
 public static bool TryParse(ByteBuffer buffer, out STUNMessageParser parsed)
 {
     if (0 == (0xFE & buffer[0]))
     {
         parsed = new STUNMessageParser(buffer);
         return(parsed.isValid);
     }
     parsed = default(STUNMessageParser);
     return(false);
 }
示例#5
0
        public void HeaderDoesNotStartWith00()
        {
            byte[] header = new byte[20];
            header[0] = 255;

            List <STUNAttr>   attrs  = new List <STUNAttr>();
            STUNMessageParser parser = new STUNMessageParser(new ByteBuffer(header), false, attrs);

            Assert.IsFalse(parser.isValid);
        }
示例#6
0
        public void ParserNotStunBenchmark()
        {
            ByteBuffer b = new ByteBuffer(new byte[] {
                0x05, 0x01, 0x00, 0x2C, 0x21, 0x12, 0xA4, 0x42, 0x0A, 0x14, 0x1E, 0x28, 0x32, 0x3C, 0x46, 0x50,
                0x5A, 0x64, 0x6E, 0x78, 0x00, 0x06, 0x00, 0x03, 0x61, 0x3A, 0x62, 0x00, 0x00, 0x24, 0x00, 0x04,
                0x6E, 0x7F, 0x1E, 0xFF, 0x00, 0x25, 0x00, 0x00, 0x00, 0x08, 0x00, 0x14, 0xF5, 0xC6, 0x0F, 0x17,
                0xF5, 0xBB, 0xC0, 0x2D, 0xA6, 0xDE, 0x64, 0x4B, 0x36, 0xF8, 0xB6, 0xBE, 0x79, 0xA0, 0xA6, 0x16
            });

            Bench("Fail 1", 100000, 20, (iterations) => {
                for (int i = 0; i < iterations; i++)
                {
                    Assert.IsFalse(STUNMessageParser.IsValidSTUN(b));
                }
            });


            b = new ByteBuffer(new byte[] {
                0x00, 0x01, 0x00, 0x2C, 0x21, 0x12, 0xA5, 0x42, 0x0A, 0x15, 0x1E, 0x28, 0x32, 0x3C, 0x46, 0x50,
                0x5A, 0x64, 0x6E, 0x78, 0x00, 0x06, 0x00, 0x03, 0x61, 0x3A, 0x62, 0x00, 0x00, 0x24, 0x00, 0x04,
                0x6E, 0x7F, 0x1E, 0xFF, 0x00, 0x25, 0x00, 0x00, 0x00, 0x08, 0x00, 0x14, 0xF5, 0xC6, 0x0F, 0x17,
                0xF5, 0xBB, 0xC0, 0x2D, 0xA6, 0xDE, 0x64, 0x4B, 0x36, 0xF8, 0xB6, 0xBE, 0x79, 0xA0, 0xA6, 0x16
            });
            Bench("Fail 2", 100000, 20, (iterations) => {
                for (int i = 0; i < iterations; i++)
                {
                    Assert.IsFalse(STUNMessageParser.IsValidSTUN(b));
                }
            });


            b = new ByteBuffer(new byte[] {
                0x00, 0x01, 0x00, 0x2C, 0x21, 0x12, 0xA4, 0x42, 0x0A, 0x14, 0x1E, 0x28, 0x32, 0x3C, 0x46, 0x50,
                0x5A, 0x64, 0x6E, 0x78, 0x00, 0x06, 0x00, 0x03, 0x61, 0x3A, 0x62, 0x00, 0x00, 0x24, 0x00, 0x04,
                0x6E, 0x7F, 0x1E, 0xFF, 0x00, 0x25, 0x00, 0x00, 0x00, 0x08, 0x00, 0x14, 0xF5, 0xC6, 0x0F, 0x17,
                0xF5, 0xBB, 0xC0, 0x2D, 0xA6, 0xDE, 0x64, 0x4B, 0x36, 0xF8, 0xB6, 0xBE, 0x79, 0xA0, 0xA6
            });
            Bench("Fail 3", 100000, 20, (iterations) => {
                for (int i = 0; i < iterations; i++)
                {
                    Assert.IsFalse(STUNMessageParser.IsValidSTUN(b));
                }
            });


            b = new ByteBuffer(new byte[] {
                0x00, 0x01, 0x00, 0x2C, 0x21, 0x12
            });
            Bench("Fail 4", 100000, 20, (iterations) => {
                for (int i = 0; i < iterations; i++)
                {
                    Assert.IsFalse(STUNMessageParser.IsValidSTUN(b));
                }
            });
        }
示例#7
0
        public void Parsing()
        {
            STUNMessageBuilder builder = new STUNMessageBuilder(null,
                                                                STUNClass.Error, STUNMethod.Binding,
                                                                new Transaction(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1));

            builder.WriteAttribute(0b111, new ByteBuffer(new byte[] { 255 }));
            builder.WriteAttribute(0b010, new ByteBuffer(new byte[] { 0, 255, 0, 255 }));

            byte[] message = builder.Build().ToArray();
            // Add some offset to test it too
            List <byte> b = new List <byte>();

            b.AddRange(new byte[] { 1, 2, 3 });
            b.AddRange(message);

            List <STUNAttr>   attrs  = new List <STUNAttr>();
            STUNMessageParser parser = new STUNMessageParser(new ByteBuffer(b.ToArray(), 3), false, attrs);

            Assert.IsTrue(parser.isValid);

            byte[] copy = new byte[20];
            Buffer.BlockCopy(message, 0, copy, 0, 20);
            CollectionAssert.AreEqual(copy, parser.GetHeader().ToArray());

            Assert.AreEqual(STUNClass.Error, parser.stunClass);
            Assert.AreEqual(STUNMethod.Binding, parser.stunMethod);
            var transaction = parser.transaction;

            Assert.AreEqual(1, transaction[0]);
            for (int i = 1; i < Transaction.Length; i++)
            {
                Assert.AreEqual(0, transaction[i]);
            }
            Assert.IsTrue(0 == parser.length % 4);
            Assert.IsTrue(parser.isValid);

            Assert.AreEqual(2, attrs.Count, "Wrong number of attributes");

            var tlv1 = attrs[0];

            Assert.IsNotNull(tlv1);
            Assert.AreEqual((STUNAttribute)0b111, tlv1.type);
            CollectionAssert.AreEqual(new byte[] { 255 }, tlv1.data.ToArray());
            Assert.AreEqual(1, tlv1.data.Length);

            var tlv2 = attrs[1];

            Assert.IsNotNull(tlv2);
            Assert.AreEqual((STUNAttribute)0b010, tlv2.type);
            CollectionAssert.AreEqual(new byte[] { 0, 255, 0, 255 }, tlv2.data.ToArray());
            Assert.AreEqual(4, tlv2.data.Length);
        }
示例#8
0
        public void LengthNotAMultipleOf4()
        {
            byte[] header = new byte[20];

            header[2] = 0;
            header[3] = 3;

            List <STUNAttr>   attrs  = new List <STUNAttr>();
            STUNMessageParser parser = new STUNMessageParser(new ByteBuffer(header), false, attrs);

            Assert.IsFalse(parser.isValid);
        }
示例#9
0
        public void EosAtReadingFirstTLVPadding()
        {
            STUNMessageBuilder builder = new STUNMessageBuilder(null,
                                                                STUNClass.Request, STUNMethod.Binding,
                                                                new Transaction(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10));

            builder.WriteAttribute(0b11, new ByteBuffer(new byte[] { 255, 255 }));

            byte[] message = builder.Build().ToArray();

            List <STUNAttr>   attrs  = new List <STUNAttr>();
            STUNMessageParser parser = new STUNMessageParser(new ByteBuffer(message, 0, 20 + 4 + 2 + 1), false, attrs);

            Assert.IsFalse(parser.isValid);
            Assert.AreEqual(0, attrs.Count, "Wrong number of attributes");
        }