summary refs log tree commit diff stats
path: root/tests/stdlib/tnet.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/stdlib/tnet.nim')
-rw-r--r--tests/stdlib/tnet.nim32
1 files changed, 31 insertions, 1 deletions
diff --git a/tests/stdlib/tnet.nim b/tests/stdlib/tnet.nim
index e8ada05e7..64d690fc9 100644
--- a/tests/stdlib/tnet.nim
+++ b/tests/stdlib/tnet.nim
@@ -1,4 +1,4 @@
-import net
+import net, nativesockets
 import unittest
 
 suite "isIpAddress tests":
@@ -45,3 +45,33 @@ suite "parseIpAddress tests":
   test "invalid ipv6":
     expect(ValueError):
       discard parseIpAddress("gggg:cdba:0000:0000:0000:0000:3257:9652")
+
+block: # "IpAddress/Sockaddr conversion"
+  proc test(ipaddrstr: string) =
+    var ipaddr_1 = parseIpAddress(ipaddrstr)
+    # echo ipaddrstr, " ", $ipaddr_1
+
+    doAssert($ipaddrstr == $ipaddr_1)
+
+    var sockaddr: Sockaddr_storage
+    var socklen: Socklen
+    var ipaddr_2: IpAddress
+    var port_2: Port
+
+    toSockAddr(ipaddr_1, Port(0), sockaddr, socklen)
+    fromSockAddr(sockaddr, socklen, ipaddr_2, port_2)
+
+    doAssert(ipaddrstr == $ipaddr_1)
+
+    doAssert(ipaddr_1 == ipaddr_2)
+    doAssert($ipaddr_1 == $ipaddr_2)
+
+
+  # ipv6 address of example.com
+  test("2606:2800:220:1:248:1893:25c8:1946")
+  # ipv6 address of localhost
+  test("::1")
+  # ipv4 address of example.com
+  test("93.184.216.34")
+  # ipv4 address of localhost
+  test("127.0.0.1")