summary refs log tree commit diff stats
path: root/tests/stdlib/tnet.nim
blob: 27a6ac49c9776ef4728a01d51336acbb91e9e3ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
discard """
matrix: "--mm:refc; --mm:orc"
outputsub: ""
"""

import net, nativesockets
import unittest
import std/assertions

block: # isIpAddress tests
  block: # 127.0.0.1 is valid
    check isIpAddress("127.0.0.1") == true

  block: # ipv6 localhost is valid
    check isIpAddress("::1") == true

  block: # fqdn is not an ip address
    check isIpAddress("example.com") == false

  block: # random string is not an ipaddress
    check isIpAddress("foo bar") == false

  block: # 5127.0.0.1 is invalid
    check isIpAddress("5127.0.0.1") == false

  block: # ipv6 is valid
    check isIpAddress("2001:cdba:0000:0000:0000:0000:3257:9652") == true

  block: # invalid ipv6
    check isIpAddress("gggg:cdba:0000:0000:0000:0000:3257:9652") == false


block: # parseIpAddress tests
  block: # 127.0.0.1 is valid
    discard parseIpAddress("127.0.0.1")

  block: # ipv6 localhost is valid
    discard parseIpAddress("::1")

  block: # fqdn is not an ip address
    expect(ValueError):
      discard parseIpAddress("example.com")

  block: # random string is not an ipaddress
    expect(ValueError):
      discard parseIpAddress("foo bar")

  block: # ipv6 is valid
    discard parseIpAddress("2001:cdba:0000:0000:0000:0000:3257:9652")

  block: # invalid ipv6
    expect(ValueError):
      discard parseIpAddress("gggg:cdba:0000:0000:0000:0000:3257:9652")

  block: # ipv4-compatible ipv6 address (embedded ipv4 address)
    check parseIpAddress("::ffff:10.0.0.23") == parseIpAddress("::ffff:0a00:0017")

  block: # octal number in ipv4 address
    expect(ValueError):
      discard parseIpAddress("010.8.8.8")
    expect(ValueError):
      discard parseIpAddress("8.010.8.8")

  block: # hexadecimal number in ipv4 address
    expect(ValueError):
      discard parseIpAddress("0xc0.168.0.1")
    expect(ValueError):
      discard parseIpAddress("192.0xa8.0.1")

  block: # less than 4 numbers in ipv4 address
    expect(ValueError):
      discard parseIpAddress("127.0.1")

  block: # octal number in embedded ipv4 address
    expect(ValueError):
      discard parseIpAddress("::ffff:010.8.8.8")
    expect(ValueError):
      discard parseIpAddress("::ffff:8.010.8.8")

  block: # hexadecimal number in embedded ipv4 address
    expect(ValueError):
      discard parseIpAddress("::ffff:0xc0.168.0.1")
    expect(ValueError):
      discard parseIpAddress("::ffff:192.0xa8.0.1")

  block: # less than 4 numbers in embedded ipv4 address
    expect(ValueError):
      discard parseIpAddress("::ffff:127.0.1")

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)

    if sockaddr.ss_family.cint == AF_INET.toInt:
      var sockaddr4: Sockaddr_in
      copyMem(addr sockaddr4, addr sockaddr, sizeof(sockaddr4))
      fromSockAddr(sockaddr4, socklen, ipaddr_2, port_2)
    elif sockaddr.ss_family.cint == AF_INET6.toInt:
      var sockaddr6: Sockaddr_in6
      copyMem(addr sockaddr6, addr sockaddr, sizeof(sockaddr6))
      fromSockAddr(sockaddr6, socklen, ipaddr_2, port_2)

    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")
.height = SIZE + "px"; span.style.background = SCROLL_ICON; span.style.left = e.x - (SIZE / 2) + "px"; span.style.top = e.y - (SIZE / 2) + "px"; span.style.position = "fixed"; span.style.fontSize = SIZE + "px"; span.style.opacity = 0.6; cursorStyle = doc.defaultView.getComputedStyle(doc.body, null).cursor; doc.body.style.cursor = "move"; doc.body.appendChild(span); doc.addEventListener('mousemove', mouseMove, false); span = span; } function clear (e) { doc.body.style.cursor = cursorStyle; span.parentNode.removeChild(span); doc.removeEventListener('mousemove', mouseMove, false); stopTimer(); if (span) span = null; if (ev) ev = null; } function mouseUp (e) { /* Simulate click, click event does not work during scrolling */ if (Math.abs(e.x - x) < 5 && Math.abs(e.y - y) < 5) { init(e); window.removeEventListener('mouseup', mouseUp, false); } } function mouseDown (e) { var t = e.target; if (e.button == 0) { if (span) { clear(); } } else if (e.button == 1) { if (span) { clear(); } else if (!t.hasAttribute("href") && !t.hasAttribute("onmousedown") && !(t.hasAttribute("onclick"))) { x = e.x; y = e.y; window.addEventListener('mouseup', mouseUp, false); } } } window.addEventListener('mousedown', mouseDown, false); })();