tnet.nim 3.6 KB

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