tnet_ll.nim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. # regular
  21. var ip4 = InAddr()
  22. ip4.s_addr = 0x10111213'u32
  23. check: ip4.s_addr == 0x10111213'u32
  24. var buff: array[0..255, char]
  25. let r = inet_ntop(AF_INET, cast[pointer](ip4.s_addr.addr), cast[cstring](buff[0].addr), buff.len.int32)
  26. let res = if r == nil: "" else: $r
  27. check: res == "19.18.17.16"
  28. test "IP V6":
  29. when defined(windows):
  30. let ipv6Support = (getVersion() and 0xff) > 0x5
  31. else:
  32. let ipv6Support = true
  33. var ip6 = [0x1000'u16, 0x1001, 0x2000, 0x2001, 0x3000, 0x3001, 0x4000, 0x4001]
  34. var buff: array[0..255, char]
  35. let r = inet_ntop(AF_INET6, cast[pointer](ip6[0].addr), cast[cstring](buff[0].addr), buff.len.int32)
  36. let res = if r == nil: "" else: $r
  37. check: not ipv6Support or res == "10:110:20:120:30:130:40:140"
  38. test "InAddr":
  39. # issue 19244
  40. var ip4 = InAddr(s_addr: 0x10111213'u32)
  41. check: ip4.s_addr == 0x10111213'u32