Fixed crash/interface. Added tests.

This commit is contained in:
Yuriy Glukhov
2018-03-15 15:44:12 +02:00
parent 29bb10b185
commit 1bd0efb067
4 changed files with 42 additions and 8 deletions

View File

@@ -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")