Exemplo n.º 1
0
        public void header()
        {
            STUNMessageBuilder builder = new STUNMessageBuilder(null,
                                                                STUNClass.Error, STUNMethod.Binding,
                                                                new Transaction(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10));

            builder.WriteAttribute(0xBABA, new ByteBuffer(new byte[20]));

            byte[] header = builder.GetHeader().ToArray();

            byte[] transaction = new byte[12];

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

            ByteBuffer transactionInt = new ByteBuffer(transaction);

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

            Assert.IsNotNull(header);
            Assert.AreEqual(20, header.Length);

            // check group
            Assert.AreEqual(STUNClass.Error, STUNHeader.GetClass(new ByteBuffer(header).GetUShortAt(0)));
            // check method
            Assert.AreEqual(STUNMethod.Binding, STUNHeader.GetMethod(new ByteBuffer(header).GetUShortAt(0)));
            // check tlv length
            Assert.AreEqual(20 + 4, new ByteBuffer(header).GetUShortAt(2));
            // check magic cookie
            Assert.AreEqual(STUNHeader.MAGIC_COOKIE, magicCookie);
            // check transaction
            Assert.AreEqual(10, transactionInt[0]);
            for (int i = 1; i < transactionInt.Length; i++)
            {
                Assert.AreEqual(0, transactionInt[i]);
            }
        }
Exemplo n.º 2
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
            };

            HMAC_SHA1 hmacGenerator = null;

            // 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());
            var stunReq = msg.Build("pass", false, ref hmacGenerator);


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