Example #1
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);
        }
Example #2
0
        public void buildMessage()
        {
            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 }));

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

            byte[] transaction = new byte[12];

            Array.Copy(message, 8, transaction, 0, 12);

            ByteBuffer transactionInt = new ByteBuffer(transaction);

            uint magicCookie = new ByteBuffer(message).GetUIntAt(4);

            // check length
            Assert.AreEqual(0, (message.Length - 20) % 4);
            // check length without header
            Assert.AreEqual(message.Length - 20, new ByteBuffer(message).GetUShortAt(2));
            // check group
            Assert.AreEqual(STUNClass.Error, STUNHeader.GetClass(new ByteBuffer(message).GetUShortAt(0)));
            // check method
            Assert.AreEqual(STUNMethod.Binding, STUNHeader.GetMethod(new ByteBuffer(message).GetUShortAt(0)));
            // check magic cookie
            Assert.AreEqual(STUNHeader.MAGIC_COOKIE, magicCookie);
            // check transaction
            Assert.AreEqual(1, transactionInt[0]);
            for (int i = 1; i < transactionInt.Length; i++)
            {
                Assert.AreEqual(0, transactionInt[i]);
            }
            // check first byte of tlv
            Assert.AreEqual(0, message[20]);
            // check second byte of tlv
            Assert.AreEqual(0b111, message[20 + 1]);
            // check first byte of tlv length
            Assert.AreEqual(0, message[20 + 2]);
            // check second byte of tlv length
            Assert.AreEqual(1, message[20 + 2 + 1]);
            // check first byte of tlv value
            Assert.AreEqual(255, message[20 + 2 + 2]);
            // check second byte of tlv value
            Assert.AreEqual(0, message[20 + 2 + 2 + 1]);
            // check third byte of tlv value
            Assert.AreEqual(0, message[20 + 2 + 2 + 2]);
            // check fourth byte of tlv value
            Assert.AreEqual(0, message[20 + 2 + 2 + 3]);
        }
Example #3
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");
        }
Example #4
0
        public void Test()
        {
            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
            };

            // Test using an offseted ByteBuffer
            var msg = new STUNMessageBuilder(new ByteBuffer(new byte[1024], 30, 700),
                                             STUNClass.Request, STUNMethod.Binding,
                                             new Transaction(120, 110, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10));

            msg.WriteAttribute(new STUNAttr_Username("a:b"));
            msg.WriteAttribute(new STUNAttr_Priority(0x6e7f1eff));
            msg.WriteAttribute(new STUNAttr_UseCandidate());
            msg.WriteAttribute(new STUNAttr_MessageIntegrity("pass"));
            var stunReq = msg.Build();

            CollectionAssert.AreEqual(reference, stunReq.ToArray());
        }