tnet.nim 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. outputsub: ""
  4. """
  5. import net, nativesockets
  6. import unittest
  7. import std/assertions
  8. block: # isIpAddress tests
  9. block: # 127.0.0.1 is valid
  10. check isIpAddress("127.0.0.1") == true
  11. block: # ipv6 localhost is valid
  12. check isIpAddress("::1") == true
  13. block: # fqdn is not an ip address
  14. check isIpAddress("example.com") == false
  15. block: # random string is not an ipaddress
  16. check isIpAddress("foo bar") == false
  17. block: # 5127.0.0.1 is invalid
  18. check isIpAddress("5127.0.0.1") == false
  19. block: # ipv6 is valid
  20. check isIpAddress("2001:cdba:0000:0000:0000:0000:3257:9652") == true
  21. block: # invalid ipv6
  22. check isIpAddress("gggg:cdba:0000:0000:0000:0000:3257:9652") == false
  23. block: # parseIpAddress tests
  24. block: # 127.0.0.1 is valid
  25. discard parseIpAddress("127.0.0.1")
  26. block: # ipv6 localhost is valid
  27. discard parseIpAddress("::1")
  28. block: # fqdn is not an ip address
  29. expect(ValueError):
  30. discard parseIpAddress("example.com")
  31. block: # random string is not an ipaddress
  32. expect(ValueError):
  33. discard parseIpAddress("foo bar")
  34. block: # ipv6 is valid
  35. discard parseIpAddress("2001:cdba:0000:0000:0000:0000:3257:9652")
  36. block: # invalid ipv6
  37. expect(ValueError):
  38. discard parseIpAddress("gggg:cdba:0000:0000:0000:0000:3257:9652")
  39. block: # ipv4-compatible ipv6 address (embedded ipv4 address)
  40. check parseIpAddress("::ffff:10.0.0.23") == parseIpAddress("::ffff:0a00:0017")
  41. block: # octal number in ipv4 address
  42. expect(ValueError):
  43. discard parseIpAddress("010.8.8.8")
  44. expect(ValueError):
  45. discard parseIpAddress("8.010.8.8")
  46. block: # hexadecimal number in ipv4 address
  47. expect(ValueError):
  48. discard parseIpAddress("0xc0.168.0.1")
  49. expect(ValueError):
  50. discard parseIpAddress("192.0xa8.0.1")
  51. block: # less than 4 numbers in ipv4 address
  52. expect(ValueError):
  53. discard parseIpAddress("127.0.1")
  54. block: # octal number in embedded ipv4 address
  55. expect(ValueError):
  56. discard parseIpAddress("::ffff:010.8.8.8")
  57. expect(ValueError):
  58. discard parseIpAddress("::ffff:8.010.8.8")
  59. block: # hexadecimal number in embedded ipv4 address
  60. expect(ValueError):
  61. discard parseIpAddress("::ffff:0xc0.168.0.1")
  62. expect(ValueError):
  63. discard parseIpAddress("::ffff:192.0xa8.0.1")
  64. block: # less than 4 numbers in embedded ipv4 address
  65. expect(ValueError):
  66. discard parseIpAddress("::ffff:127.0.1")
  67. block: # "IpAddress/Sockaddr conversion"
  68. proc test(ipaddrstr: string) =
  69. var ipaddr_1 = parseIpAddress(ipaddrstr)
  70. # echo ipaddrstr, " ", $ipaddr_1
  71. doAssert($ipaddrstr == $ipaddr_1)
  72. var sockaddr: Sockaddr_storage = default(Sockaddr_storage)
  73. var socklen: SockLen = default(SockLen)
  74. var ipaddr_2: IpAddress = default(IpAddress)
  75. var port_2: Port = default(Port)
  76. toSockAddr(ipaddr_1, Port(0), sockaddr, socklen)
  77. fromSockAddr(sockaddr, socklen, ipaddr_2, port_2)
  78. doAssert(ipaddrstr == $ipaddr_1)
  79. doAssert(ipaddr_1 == ipaddr_2)
  80. doAssert($ipaddr_1 == $ipaddr_2)
  81. if sockaddr.ss_family.cint == AF_INET.toInt:
  82. var sockaddr4: Sockaddr_in = default(Sockaddr_in)
  83. copyMem(addr sockaddr4, addr sockaddr, sizeof(sockaddr4))
  84. fromSockAddr(sockaddr4, socklen, ipaddr_2, port_2)
  85. elif sockaddr.ss_family.cint == AF_INET6.toInt:
  86. var sockaddr6: Sockaddr_in6 = default(Sockaddr_in6)
  87. copyMem(addr sockaddr6, addr sockaddr, sizeof(sockaddr6))
  88. fromSockAddr(sockaddr6, socklen, ipaddr_2, port_2)
  89. doAssert(ipaddr_1 == ipaddr_2)
  90. doAssert($ipaddr_1 == $ipaddr_2)
  91. # ipv6 address of example.com
  92. test("2606:2800:220:1:248:1893:25c8:1946")
  93. # ipv6 address of localhost
  94. test("::1")
  95. # ipv4 address of example.com
  96. test("93.184.216.34")
  97. # ipv4 address of localhost
  98. test("127.0.0.1")