]> gitweb.factorcode.org Git - factor.git/blob - basis/io/sockets/sockets-tests.factor
Merge branches 'master' and 'cleanup' into cleanup
[factor.git] / basis / io / sockets / sockets-tests.factor
1 USING: io.sockets io.sockets.private sequences math tools.test
2 namespaces accessors kernel destructors calendar io.timeouts
3 io.encodings.utf8 io concurrency.promises threads
4 io.streams.string ;
5 IN: io.sockets.tests
6
7 [ T{ inet4 f f 0 } ] [ f 0 <inet4> ] unit-test
8 [ T{ inet6 f f 0 } ] [ f 0 <inet6> ] unit-test
9
10 [ T{ inet f "google.com" f } ] [ "google.com" f <inet> ] unit-test
11
12 [ T{ inet f "google.com" 0 } ] [ "google.com" 0 <inet> ] unit-test
13 [ T{ inet f "google.com" 80 } ] [ "google.com" 0 <inet> 80 with-port ] unit-test
14 [ T{ inet4 f "8.8.8.8" 0 } ] [ "8.8.8.8" 0 <inet4> ] unit-test
15 [ T{ inet4 f "8.8.8.8" 53 } ] [ "8.8.8.8" 0 <inet4> 53 with-port ] unit-test
16 [ T{ inet6 f "5:5:5:5:6:6:6:6" 12 } ] [ "5:5:5:5:6:6:6:6" 0 <inet6> 12 with-port ] unit-test
17
18 [ T{ inet f "google.com" 80 } ] [ "google.com" 80 with-port ] unit-test
19
20 [ B{ 1 2 3 4 } ]
21 [ "1.2.3.4" T{ inet4 } inet-pton ] unit-test
22
23 [ "1.2.3.4" ]
24 [ B{ 1 2 3 4 } T{ inet4 } inet-ntop ] unit-test
25
26 [ "255.255.255.255" ]
27 [ B{ 255 255 255 255 } T{ inet4 } inet-ntop ] unit-test
28
29 [ B{ 255 255 255 255 } ]
30 [ "255.255.255.255" T{ inet4 } inet-pton ] unit-test
31
32 [ B{ 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 } ]
33 [ "1:2:3:4:5:6:7:8" T{ inet6 } inet-pton ] unit-test
34
35 [ "1:2:3:4:5:6:7:8" ]
36 [ B{ 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 } T{ inet6 } inet-ntop ] unit-test
37
38 [ B{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } ]
39 [ "::" T{ inet6 } inet-pton ] unit-test
40
41 [ "0:0:0:0:0:0:0:0" ]
42 [ B{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } T{ inet6 } inet-ntop ] unit-test
43
44 [ B{ 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } ]
45 [ "1::" T{ inet6 } inet-pton ] unit-test
46
47 [ B{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 } ]
48 [ "::1" T{ inet6 } inet-pton ] unit-test
49
50 [ B{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 } ]
51 [ "::100" T{ inet6 } inet-pton ] unit-test
52
53 [ B{ 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 } ]
54 [ "1::2" T{ inet6 } inet-pton ] unit-test
55
56 [ B{ 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 3 } ]
57 [ "1::2:3" T{ inet6 } inet-pton ] unit-test
58
59 [ B{ 0 1 0 2 0 0 0 0 0 0 0 0 0 3 0 4 } ]
60 [ "1:2::3:4" T{ inet6 } inet-pton ] unit-test
61
62 [ "1:2:0:0:0:0:3:4" ]
63 [ B{ 0 1 0 2 0 0 0 0 0 0 0 0 0 3 0 4 } T{ inet6 } inet-ntop ] unit-test
64
65 [ B{ 0 0 0 0 0 0 0 0 0 127 0 0 0 0 0 1 } ]
66 [ "::127.0.0.1" T{ inet6 } inet-pton ] unit-test
67
68 [ B{ 0 2 0 0 0 0 0 9 0 127 0 0 0 0 0 1 } ]
69 [ "2::9:127.0.0.1" T{ inet6 } inet-pton ] unit-test
70
71 [ "2001:6f8:37a:5:0:0:0:1" ]
72 [ "2001:6f8:37a:5::1" T{ inet6 } [ inet-pton ] [ inet-ntop ] bi ] unit-test
73
74 [ t t ] [
75     "localhost" 80 <inet> resolve-host
76     [ length 1 >= ]
77     [ [ [ inet4? ] [ inet6? ] bi or ] all? ] bi
78 ] unit-test
79
80 [ t t ] [
81     "localhost" resolve-host
82     [ length 1 >= ]
83     [ [ [ ipv4? ] [ ipv6? ] bi or ] all? ] bi
84 ] unit-test
85
86 [ t t ] [
87     f resolve-host
88     [ length 1 >= ]
89     [ [ [ ipv4? ] [ ipv6? ] bi or ] all? ] bi
90 ] unit-test
91
92 [ t t ] [
93     f 0 <inet> resolve-host
94     [ length 1 >= ]
95     [ [ [ ipv4? ] [ ipv6? ] bi or ] all? ] bi
96 ] unit-test
97
98 ! Smoke-test UDP
99 [ ] [ "127.0.0.1" 0 <inet4> <datagram> "datagram1" set ] unit-test
100 [ ] [ "datagram1" get addr>> "addr1" set ] unit-test
101 [ f ] [ "addr1" get port>> 0 = ] unit-test
102
103 [ ] [ "127.0.0.1" 0 <inet4> <datagram> "datagram2" set ] unit-test
104 [ ] [ "datagram2" get addr>> "addr2" set ] unit-test
105 [ f ] [ "addr2" get port>> 0 = ] unit-test
106
107 [ ] [ B{ 1 2 3 4 } "addr2" get "datagram1" get send ] unit-test
108 [ B{ 1 2 3 4 } ] [ "datagram2" get receive "from" set ] unit-test
109 [ ] [ B{ 4 3 2 1 } "from" get "datagram2" get send ] unit-test
110 [ B{ 4 3 2 1 } t ] [ "datagram1" get receive "addr2" get = ] unit-test
111
112 [ ] [ "datagram1" get dispose ] unit-test
113 [ ] [ "datagram2" get dispose ] unit-test
114
115 ! Test timeouts
116 [ ] [ "127.0.0.1" 0 <inet4> <datagram> "datagram3" set ] unit-test
117
118 [ ] [ 1 seconds "datagram3" get set-timeout ] unit-test
119 [ "datagram3" get receive ] must-fail
120
121 ! See what happens if other end is closed
122 [ ] [ <promise> "port" set ] unit-test
123
124 [ ] [ "datagram3" get dispose ] unit-test
125
126 [ ] [
127     [
128         "127.0.0.1" 0 <inet4> utf8 <server>
129         dup addr>> "port" get fulfill
130         [
131             accept drop
132             dup stream-readln drop
133             "hello" <string-reader> swap stream-copy
134         ] with-disposal
135     ] "Socket close test" spawn drop
136 ] unit-test
137
138 [ "hello" f ] [
139     "port" get ?promise utf8 [
140         1 seconds input-stream get set-timeout
141         1 seconds output-stream get set-timeout
142         "hi\n" write flush readln readln
143     ] with-client
144 ] unit-test
145
146 ! Binding to all interfaces should work
147 [ ] [ f 0 <inet4> <datagram> dispose ] unit-test
148 [ ] [ f 0 <inet6> <datagram> dispose ] unit-test