]> gitweb.factorcode.org Git - factor.git/blob - basis/io/sockets/sockets-tests.factor
Put brackets around ipv6 addresses in `inet6 present`
[factor.git] / basis / io / sockets / sockets-tests.factor
1 USING: continuations io.sockets io.sockets.private sequences math
2 tools.test namespaces accessors kernel destructors calendar io.timeouts
3 io.encodings.utf8 io concurrency.promises threads
4 io.streams.string present system ;
5 IN: io.sockets.tests
6
7 os unix? [
8     [ T{ local f "/tmp/foo" } ] [ "/tmp/foo" <local> ] unit-test
9 ] when
10
11 { T{ inet4 f f 0 } } [ f 0 <inet4> ] unit-test
12 { T{ inet6 f f 0 1 } } [ f 1 <inet6> ] unit-test
13
14 { T{ inet f "google.com" f } } [ "google.com" f <inet> ] unit-test
15
16 { T{ inet f "google.com" 0 } } [ "google.com" 0 <inet> ] unit-test
17 { T{ inet f "google.com" 80 } } [ "google.com" 0 <inet> 80 with-port ] unit-test
18 { T{ inet4 f "8.8.8.8" 0 } } [ "8.8.8.8" 0 <inet4> ] unit-test
19 { T{ inet4 f "8.8.8.8" 53 } } [ "8.8.8.8" 0 <inet4> 53 with-port ] unit-test
20 { T{ inet6 f "5:5:5:5:6:6:6:6" 0 12 } } [ "5:5:5:5:6:6:6:6" 0 <inet6> 12 with-port ] unit-test
21 { T{ inet6 f "fe80::1" 1 80 } } [ T{ ipv6 f "fe80::1" 1 } 80 with-port ] unit-test
22
23 : test-sockaddr ( addrspec -- )
24     [ dup make-sockaddr ] keep parse-sockaddr assert= ;
25
26 { } [ T{ inet4 f "8.8.8.8" 53 } test-sockaddr ] unit-test
27 { } [ T{ inet6 f "5:5:5:5:6:6:6:6" 0 12 } test-sockaddr ] unit-test
28 { } [ T{ inet6 f "fe80:0:0:0:0:0:0:1" 1 80 } test-sockaddr ] unit-test
29
30 { T{ inet f "google.com" 80 } } [ "google.com" 80 with-port ] unit-test
31
32 ! Test bad hostnames
33 [ "google.com" f <inet4> ] must-fail
34 [ "a.b.c.d" f <inet4> ] must-fail
35 [ "google.com" f <inet6> ] must-fail
36 [ "a.b.c.d" f <inet6> ] must-fail
37
38 ! Test present on addrspecs
39 { "4.4.4.4:12" } [ "4.4.4.4" 12 <inet4> present ] unit-test
40 { "[::1]:12" } [ "::1" 12 <inet6> present ] unit-test
41 { "[fe80::1%1]:12" } [ "fe80::1" 1 12 inet6 boa present ] unit-test
42
43 { B{ 1 2 3 4 } }
44 [ "1.2.3.4" T{ inet4 } inet-pton ] unit-test
45
46 { "1.2.3.4" }
47 [ B{ 1 2 3 4 } T{ inet4 } inet-ntop ] unit-test
48
49 { "255.255.255.255" }
50 [ B{ 255 255 255 255 } T{ inet4 } inet-ntop ] unit-test
51
52 { B{ 255 255 255 255 } }
53 [ "255.255.255.255" T{ inet4 } inet-pton ] unit-test
54
55 { B{ 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 } }
56 [ "1:2:3:4:5:6:7:8" T{ inet6 } inet-pton ] unit-test
57
58 { "1:2:3:4:5:6:7:8" }
59 [ B{ 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 } T{ inet6 } inet-ntop ] unit-test
60
61 { B{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } }
62 [ "::" T{ inet6 } inet-pton ] unit-test
63
64 { "0:0:0:0:0:0:0:0" }
65 [ B{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } T{ inet6 } inet-ntop ] unit-test
66
67 { B{ 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } }
68 [ "1::" T{ inet6 } inet-pton ] unit-test
69
70 { B{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 } }
71 [ "::1" T{ inet6 } inet-pton ] unit-test
72
73 { B{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 } }
74 [ "::100" T{ inet6 } inet-pton ] unit-test
75
76 { B{ 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 } }
77 [ "1::2" T{ inet6 } inet-pton ] unit-test
78
79 { B{ 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 3 } }
80 [ "1::2:3" T{ inet6 } inet-pton ] unit-test
81
82 { B{ 0 1 0 2 0 0 0 0 0 0 0 0 0 3 0 4 } }
83 [ "1:2::3:4" T{ inet6 } inet-pton ] unit-test
84
85 { "1:2:0:0:0:0:3:4" }
86 [ B{ 0 1 0 2 0 0 0 0 0 0 0 0 0 3 0 4 } T{ inet6 } inet-ntop ] unit-test
87
88 { B{ 0 0 0 0 0 0 0 0 0 127 0 0 0 0 0 1 } }
89 [ "::127.0.0.1" T{ inet6 } inet-pton ] unit-test
90
91 { B{ 0 2 0 0 0 0 0 9 0 127 0 0 0 0 0 1 } }
92 [ "2::9:127.0.0.1" T{ inet6 } inet-pton ] unit-test
93
94 { "2001:6f8:37a:5:0:0:0:1" }
95 [ "2001:6f8:37a:5::1" T{ inet6 } [ inet-pton ] [ inet-ntop ] bi ] unit-test
96
97 { t t } [
98     "localhost" 80 <inet> resolve-host
99     [ length 1 >= ]
100     [ [ [ inet4? ] [ inet6? ] bi or ] all? ] bi
101 ] unit-test
102
103 { t t } [
104     "localhost" resolve-host
105     [ length 1 >= ]
106     [ [ [ ipv4? ] [ ipv6? ] bi or ] all? ] bi
107 ] unit-test
108
109 { t t } [
110     f resolve-host
111     [ length 1 >= ]
112     [ [ [ ipv4? ] [ ipv6? ] bi or ] all? ] bi
113 ] unit-test
114
115 { t t } [
116     f 0 <inet> resolve-host
117     [ length 1 >= ]
118     [ [ [ ipv4? ] [ ipv6? ] bi or ] all? ] bi
119 ] unit-test
120
121 ! Smoke-test UDP
122 { } [ "127.0.0.1" 0 <inet4> <datagram> "datagram1" set ] unit-test
123 { } [ "datagram1" get addr>> "addr1" set ] unit-test
124 { f } [ "addr1" get port>> 0 = ] unit-test
125
126 { } [ "127.0.0.1" 0 <inet4> <datagram> "datagram2" set ] unit-test
127 { } [ "datagram2" get addr>> "addr2" set ] unit-test
128 { f } [ "addr2" get port>> 0 = ] unit-test
129
130 { } [ B{ 1 2 3 4 } "addr2" get "datagram1" get send ] unit-test
131 { B{ 1 2 3 4 } } [ "datagram2" get receive "from" set ] unit-test
132 { } [ B{ 4 3 2 1 } "from" get "datagram2" get send ] unit-test
133 { B{ 4 3 2 1 } t } [ "datagram1" get receive "addr2" get = ] unit-test
134
135 { } [ "datagram1" get dispose ] unit-test
136 { } [ "datagram2" get dispose ] unit-test
137
138 ! Test timeouts
139 { } [ "127.0.0.1" 0 <inet4> <datagram> "datagram3" set ] unit-test
140
141 { } [ 1 seconds "datagram3" get set-timeout ] unit-test
142 [ "datagram3" get receive ] must-fail
143
144 ! See what happens if other end is closed
145 { } [ <promise> "port" set ] unit-test
146
147 { } [ "datagram3" get dispose ] unit-test
148
149 { } [
150     [
151         "127.0.0.1" 0 <inet4> utf8 <server>
152         dup addr>> "port" get fulfill
153         [
154             accept drop
155             dup stream-readln drop
156             "hello" <string-reader> swap stream-copy
157         ] with-disposal
158     ] "Socket close test" spawn drop
159 ] unit-test
160
161 { "hello" f } [
162     "port" get ?promise utf8 [
163         1 seconds input-stream get set-timeout
164         1 seconds output-stream get set-timeout
165         "hi\n" write flush readln readln
166     ] with-client
167 ] unit-test
168
169 ! Binding to all interfaces should work
170 { } [ f 0 <inet4> <datagram> dispose ] unit-test
171 { } [ f 0 <inet6> <datagram> dispose ] unit-test
172
173 { 80 } [ "http" protocol-port ] unit-test
174 { f } [ f protocol-port ] unit-test
175
176 { "http" } [ 80 port-protocol ] unit-test
177 { f } [ f port-protocol ] unit-test
178
179 [ "you-cant-resolve-me!" resolve-host ] [ addrinfo-error? ] must-fail-with
180
181 [ ] [ B{ 1 2 3 } f 9000 <inet4> send-once ] unit-test
182 [ ] [ B{ 1 2 3 } f 9000 <inet4> broadcast-once ] unit-test
183 [ ] [ B{ 1 2 3 } "0.0.0.0" 9000 <inet4> send-once ] unit-test
184 [ ] [ B{ 1 2 3 } "0.0.0.0" 9000 <inet4> broadcast-once ] unit-test
185
186 ipv6-supported? [
187     [ ] [ B{ 1 2 3 } f 9000 <inet6> send-once ] unit-test
188     [ ] [ B{ 1 2 3 } f 9000 <inet6> broadcast-once ] unit-test
189     [ ] [ B{ 1 2 3 } "::" 9000 <inet6> send-once ] unit-test
190     [ ] [ B{ 1 2 3 } "::" 9000 <inet6> broadcast-once ] unit-test
191 ] when