tnet.nim 3.6 KB

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