tnet_ll.nim 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. discard """
  2. action: run
  3. output: '''
  4. [Suite] inet_ntop tests
  5. '''
  6. """
  7. when defined(windows):
  8. import winlean
  9. elif defined(posix):
  10. import posix
  11. else:
  12. {.error: "Unsupported OS".}
  13. import unittest, strutils
  14. suite "inet_ntop tests":
  15. setup:
  16. when defined(windows):
  17. var wsa: WSAData
  18. discard wsaStartup(0x101'i16, wsa.addr)
  19. test "IP V4":
  20. var ip4 = 0x10111213
  21. var buff: array[0..255, char]
  22. let r = inet_ntop(AF_INET, ip4.addr, buff[0].addr, buff.sizeof.int32)
  23. let res = if r == nil: "" else: $r
  24. check: res == "19.18.17.16"
  25. test "IP V6":
  26. when defined(windows):
  27. let ipv6Support = (getVersion() and 0xff) > 0x5
  28. else:
  29. let ipv6Support = true
  30. var ip6 = [0x1000'u16, 0x1001, 0x2000, 0x2001, 0x3000, 0x3001, 0x4000, 0x4001]
  31. var buff: array[0..255, char]
  32. let r = inet_ntop(AF_INET6, ip6[0].addr, buff[0].addr, buff.sizeof.int32)
  33. let res = if r == nil: "" else: $r
  34. check: not ipv6Support or res == "10:110:20:120:30:130:40:140"