UnitTest.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using STUN.Message.Attributes;
  3. using STUN.Message.Enums;
  4. using System.Linq;
  5. using System.Net;
  6. namespace UnitTest
  7. {
  8. [TestClass]
  9. public class UnitTest
  10. {
  11. private readonly byte[] _magicCookie = { 0x21, 0x12, 0xa4, 0x42 };
  12. private readonly byte[] _transactionId =
  13. {
  14. 0xb7, 0xe7, 0xa7, 0x01,
  15. 0xbc, 0x34, 0xd6, 0x86,
  16. 0xfa, 0x87, 0xdf, 0xae
  17. };
  18. private static readonly byte[] XorPort = { 0xa1, 0x47 };
  19. private static readonly byte[] XorIPv4 = { 0xe1, 0x12, 0xa6, 0x43 };
  20. private static readonly byte[] XorIPv6 =
  21. {
  22. 0x01, 0x13, 0xa9, 0xfa,
  23. 0xa5, 0xd3, 0xf1, 0x79,
  24. 0xbc, 0x25, 0xf4, 0xb5,
  25. 0xbe, 0xd2, 0xb9, 0xd9
  26. };
  27. private const ushort Port = 32853;
  28. private readonly IPAddress IPv4 = IPAddress.Parse(@"192.0.2.1");
  29. private readonly IPAddress IPv6 = IPAddress.Parse(@"2001:db8:1234:5678:11:2233:4455:6677");
  30. private readonly byte[] _ipv4Response = new byte[] { 0x00, (byte)IpFamily.IPv4 }.Concat(XorPort).Concat(XorIPv4).ToArray();
  31. private readonly byte[] _ipv6Response = new byte[] { 0x00, (byte)IpFamily.IPv6 }.Concat(XorPort).Concat(XorIPv6).ToArray();
  32. /// <summary>
  33. /// https://tools.ietf.org/html/rfc5769.html
  34. /// </summary>
  35. [TestMethod]
  36. public void TestXorMapped()
  37. {
  38. var t = new XorMappedAddressAttribute(_magicCookie, _transactionId)
  39. {
  40. Port = Port,
  41. Family = IpFamily.IPv4,
  42. Address = IPv4
  43. };
  44. Assert.IsTrue(_ipv4Response.SequenceEqual(t.Bytes));
  45. t = new XorMappedAddressAttribute(_magicCookie, _transactionId);
  46. Assert.IsTrue(t.TryParse(_ipv4Response));
  47. Assert.AreEqual(t.Port, Port);
  48. Assert.AreEqual(t.Family, IpFamily.IPv4);
  49. Assert.AreEqual(t.Address, IPv4);
  50. t = new XorMappedAddressAttribute(_magicCookie, _transactionId);
  51. Assert.IsTrue(t.TryParse(_ipv6Response));
  52. Assert.AreEqual(t.Port, Port);
  53. Assert.AreEqual(t.Family, IpFamily.IPv6);
  54. Assert.AreEqual(t.Address, IPv6);
  55. Assert.IsTrue(_ipv6Response.SequenceEqual(t.Bytes));
  56. }
  57. }
  58. }