tnet_ll.nim 1.3 KB

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