public void FullTestIPv4() { byte[] expected = new byte[] { 0x01, 0x01, 0x00, 0x0C, 0x21, 0x12, 0xA4, 0x42, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x00, 0x01, 0x00, 0x08, 0x00, 0x01, 0x12, 0x34, 0x01, 0x02, 0x03, 0x04 }; IPAddress usedAddress = IPAddress.Parse("1.2.3.4"); ushort usedPort = 4660; var msg = new STUNMessageBuilder(new byte[128], STUNClass.Success, STUNMethod.Binding, new Transaction(new byte[12] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 })); msg.WriteAttribute(new STUNAttr_MappedAddress(usedAddress, usedPort)); var stunReq = msg.Build(); CollectionAssert.AreEqual(expected, stunReq.ToArray()); var attrs = new List <STUNAttr>(); var parser = new STUNMessageParser(stunReq, attrs); Assert.IsTrue(parser.isValid); STUNAttr_MappedAddress parsedAddr = new STUNAttr_MappedAddress(); parsedAddr.ReadFromBuffer(attrs[0]); Assert.AreEqual(usedPort, parsedAddr.port, "Parser can't read the address correctly"); Assert.IsTrue(parsedAddr.isIPv4()); Assert.AreEqual(usedAddress, parsedAddr.ipv4.ToIPAddress(), "Parser can't read the port correctly"); }
public void NoErrors() { byte[] expected = new byte[] { 0x01, 0x01, 0x00, 0x10, 0x21, 0x12, 0xA4, 0x42, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x00, 0x09, 0x00, 0x0A, 0x00, 0x00, 0x04, 0x38, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x00, 0x00 }; ushort errorCode = 456; string errorMessage = "123456"; var msg = new STUNMessageBuilder(new byte[128], STUNClass.Success, STUNMethod.Binding, new Transaction(new byte[12] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 })); msg.WriteAttribute(new STUNAttr_ErrorCode(errorCode, errorMessage)); var stunReq = msg.Build(); CollectionAssert.AreEqual(expected, stunReq.ToArray()); var attrs = new List <STUNAttr>(); var parser = new STUNMessageParser(stunReq, attrs); Assert.IsTrue(parser.isValid); STUNAttr_ErrorCode parsedAttr = new STUNAttr_ErrorCode(); parsedAttr.ReadFromBuffer(attrs[0]); Assert.AreEqual(errorCode, parsedAttr.code, "Parser can't read the errorCode correctly"); Assert.AreEqual(errorMessage, parsedAttr.reason, "Parser can't read the errorMessage correctly"); }
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; string username = "******"; uint priority = 0x6e7f1eff; var msg = new STUNMessageBuilder(new byte[1024], STUNClass.Request, STUNMethod.Binding, new Transaction(120, 110, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10)); msg.WriteAttribute(new STUNAttr_Username(username)); msg.WriteAttribute(new STUNAttr_Priority(priority)); msg.WriteAttribute(new STUNAttr_UseCandidate()); var stunReq = msg.Build("pass", false, ref hmacGenerator); CollectionAssert.AreEqual(reference, stunReq.ToArray()); var attrs = new List <STUNAttr>(); var parser = new STUNMessageParser(stunReq, attrs); Assert.IsTrue(parser.isValid); STUNAttr_Username parsedAttr0 = new STUNAttr_Username(); parsedAttr0.ReadFromBuffer(attrs[0]); Assert.AreEqual(username, parsedAttr0.usernameInString, "Parser can't read the username correctly"); STUNAttr_Priority parsedAttr1 = new STUNAttr_Priority(); parsedAttr1.ReadFromBuffer(attrs[1]); Assert.AreEqual(priority, parsedAttr1.priority, "Parser can't read the priority correctly"); }
public void FullTestIPv6() { // WRONG: regenerate byte[] expected = new byte[] { 0x01, 0x01, 0x00, 0x18, 0x21, 0x12, 0xA4, 0x42, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x00, 0x20, 0x00, 0x14, 0x00, 0x02, 0xFC, 0xC7, 0x01, 0x13, 0xA9, 0xFA, 0x85, 0xA2, 0x02, 0x03, 0x04, 0x05, 0x8C, 0x29, 0x0B, 0x79, 0x79, 0x3F }; IPAddress usedAddress = IPAddress.Parse("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"); ushort usedPort = 56789; var msg = new STUNMessageBuilder(new byte[128], STUNClass.Success, STUNMethod.Binding, new Transaction(new byte[12] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 })); msg.WriteAttribute(new STUNAttr_XORMappedAddress(usedAddress, 56789)); var stunReq = msg.Build(); CollectionAssert.AreEqual(expected, stunReq.ToArray()); var attrs = new List <STUNAttr>(); var parser = new STUNMessageParser(stunReq, attrs); Assert.IsTrue(parser.isValid); STUNAttr_XORMappedAddress parsedAddr = new STUNAttr_XORMappedAddress(); parsedAddr.ReadFromBuffer(attrs[0]); Assert.AreEqual(usedPort, parsedAddr.port, "Parser can't read the port correctly"); Assert.IsFalse(parsedAddr.isIPv4()); Assert.AreEqual(usedAddress, parsedAddr.ip.ToIPAddress(), "Parser can't read the address correctly"); }
public void Test() { byte[] reference = new byte[] { 0x00, 0x01, 0x00, 0x34, 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, 0x80, 0x28, 0x00, 0x04, 0x1E, 0xC5, 0xAA, 0x8B }; var msg = new STUNMessageBuilder(new byte[1024], 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")); msg.WriteAttribute(new STUNAttr_Fingerprint()); var stunReq = msg.Build(); CollectionAssert.AreEqual(reference, stunReq.ToArray()); var attrs = new List <STUNAttr>(); var parsed = new STUNMessageParser(stunReq, false, attrs); int fingerprintIndex = STUNMessageParser.AttributeLastIndexOf(attrs, STUNAttribute.FINGERPRINT); Assert.AreEqual(4, fingerprintIndex); fingerprintIndex = STUNMessageParser.AttributeIndexOf(attrs, STUNAttribute.FINGERPRINT); Assert.AreEqual(4, fingerprintIndex); var fingerprint = new STUNAttr_Fingerprint(attrs[fingerprintIndex]); Assert.IsTrue(fingerprint.Check()); stunReq[5]++; Assert.IsFalse(fingerprint.Check()); }