test(aio,socket,tty): match musl error messages
[jimtcl.git] / tests / socket.test
blobf2d940fa8495e5fb4c15449d6e97c2231f047310
1 source [file dirname [info script]]/testing.tcl
3 needs constraint jim
4 needs cmd socket
5 needs cmd os.fork
7 catch {[socket -ipv6 stream ::1:5000]} res
8 set ipv6 1
9 if {[string match "*not supported" $res]} {
10     set ipv6 0
11 } else {
12     # Also, if we can't bind an IPv6 socket, don't run IPv6 tests
13     if {[catch {
14         [socket -ipv6 stream.server ::1:5000] close
15     } msg opts]} {
16         set ipv6 0
17     }
19 testConstraint ipv6 $ipv6
21 test socket-1.1 {stream} -body {
22         # Let the system choose a port
23         set s [socket stream.server 127.0.0.1:0]
24         stdout flush
25         if {[os.fork] == 0} {
26                 # child
27                 set c [socket stream [$s sockname]]
28                 $s close
29                 $c puts hello
30                 $c close
31                 exit 99
32         }
33         set cs [$s accept]
34         $cs gets buf
35         $cs close
36         $s close
37         set buf
38 } -result {hello}
40 test socket-1.2 {dgram - connected} -body {
41         # Let the system choose a port
42         set s [socket dgram.server 127.0.0.1:0]
43         set c [socket dgram [$s sockname]]
44         $s buffering none
45         $c buffering none
46         $c puts -nonewline hello
47         set buf [$s recv 1000]
48         $c close
49         $s close
50         set buf
51 } -result {hello}
53 test socket-1.3 {dgram - unconnected} -body {
54         # Let the system choose a port
55         set s [socket dgram.server 127.0.0.1:0]
56         set c [socket dgram]
57         $s buffering none
58         $c buffering none
59         $c sendto hello [$s sockname]
60         set buf [$s recv 1000]
61         $c close
62         $s close
63         set buf
64 } -result {hello}
66 test socket-1.4 {unix} -body {
67         set path [file tempfile]
68         file delete $path
69         set s [socket unix.server $path]
70         stdout flush
71         if {[os.fork] == 0} {
72                 # child
73                 set c [socket unix [$s sockname]]
74                 $s close
75                 $c puts hello
76                 $c close
77                 exit 99
78         }
79         set cs [$s accept]
80         $cs gets buf
81         $cs close
82         $s close
83         set buf
84 } -result {hello}
86 test socket-1.5 {unix.dgram} -body {
87         set path [file tempfile]
88         file delete $path
89         set s [socket unix.dgram.server $path]
90         set c [socket unix.dgram [$s sockname]]
91         $s buffering none
92         $c buffering none
93         $c puts -nonewline hello
94         set buf [$s recv 1000]
95         $s close
96         $c close
97         set buf
98 } -result {hello}
100 test socket-1.6 {pipe} -body {
101         lassign [socket pipe] r w
102         stdout flush
103         if {[os.fork] == 0} {
104                 $r close
105                 $w puts hello
106                 $w close
107                 exit 99
108         }
109         $w close
110         $r gets buf
111         $r close
112         set buf
113 } -result {hello}
115 test socket-1.7 {socketpair} -body {
116         lassign [socket pair] s1 s2
117         stdout flush
118         if {[os.fork] == 0} {
119                 $s1 close
120                 # Read data and send it back
121                 $s2 gets buf
122                 $s2 puts $buf
123                 $s2 close
124                 exit 99
125         }
126         $s2 close
127         $s1 puts hello
128         $s1 gets buf
129         $s1 close
130         set buf
131 } -result {hello}
133 test socket-1.8 {stream - ipv6} -constraints ipv6 -body {
134         # Let the system choose a port
135         set s [socket -ipv6 stream.server localhost:0]
136         stdout flush
137         if {[os.fork] == 0} {
138                 # child
139                 set c [socket -ipv6 stream [$s sockname]]
140                 $s close
141                 $c puts hello
142                 $c close
143                 exit 99
144         }
145         set cs [$s accept]
146         $cs gets buf
147         $cs close
148         $s close
149         set buf
150 } -result {hello}
152 test socket-1.9 {dgram - ipv6 - unconnected} -constraints ipv6 -body {
153         # Let the system choose a port
154         set s [socket -ipv6 dgram.server localhost:0]
155         set c [socket -ipv6 dgram]
156         $s buffering none
157         $c buffering none
158         $c sendto hello [$s sockname]
159         set buf [$s recv 1000]
160         $c close
161         $s close
162         set buf
163 } -result {hello}
165 test socket-1.10 {stream - port only} -body {
166         set s [socket stream.server 0]
167         stdout flush
168         if {[os.fork] == 0} {
169                 # child
170                 set c [socket stream [$s sockname]]
171                 $s close
172                 $c puts hello
173                 $c close
174                 exit 99
175         }
176         set cs [$s accept]
177         $cs gets buf
178         $cs close
179         $s close
180         set buf
181 } -result {hello}
183 test socket-1.11 {stream - ipv6 - port only} -constraints ipv6 -body {
184         # Let the system choose a port
185         set s [socket -ipv6 stream.server 0]
186         stdout flush
187         if {[os.fork] == 0} {
188                 # child
189                 set c [socket -ipv6 stream [$s sockname]]
190                 $s close
191                 $c puts hello
192                 $c close
193                 exit 99
194         }
195         set cs [$s accept]
196         $cs gets buf
197         $cs close
198         $s close
199         set buf
200 } -result {hello}
202 test socket-2.1 {read 1} -body {
203         lassign [socket pipe] r w
204         $w puts -nonewline hello
205         $w close
206         set chars {}
207         while {1} {
208                 set c [$r read 1]
209                 if {$c eq ""} {
210                         break
211                 }
212                 lappend chars $c
213         }
214         $r close
215         set chars
216 } -result {h e l l o}
218 test socket-2.2 {read to EOF} -body {
219         lassign [socket pipe] r w
220         $w puts -nonewline hello
221         $w close
222         set buf [$r read]
223         $r close
224         set buf
225 } -result {hello}
227 test socket-2.3 {read -nonewline} -body {
228         lassign [socket pipe] r w
229         $w puts hello
230         $w close
231         set buf [$r read -nonewline]
232         $r close
233         set buf
234 } -result {hello}
236 test socket-2.4 {isatty} -body {
237         lassign [socket pipe] r w
238         set result [list [$r isatty] [$w isatty]]
239         $r close
240         $w close
241         set result
242 } -result {0 0}
244 test socket-2.5 {peername} -body {
245         set s [socket stream.server 0]
246         stdout flush
247         if {[os.fork] == 0} {
248                 try {
249                         set c [socket stream [$s sockname]]
250                         $s close
251                         $c puts [list [$c sockname] [$c peername]]
252                         $c close
253                 } on error msg {
254                         stderr puts $msg
255                 }
256                 exit 99
257         }
258         set cs [$s accept]
259         lassign [$cs gets] c_sockname c_peername
260         if {$c_sockname ne [$cs peername]} {
261                 error "client sockname=$c_sockname not equal to server peername=[$cs peername]"
262         }
263         if {$c_peername ne [$cs sockname]} {
264                 error "client peername=$c_peername not equal to server sockname=[$cs sockname]"
265         }
266         $cs close
267         $s close
268 } -result {}
270 test socket-3.1 {listen} {
271         set s [socket stream.server 0]
272         $s listen 10
273         $s close
274 } {}
276 test socket-3.2 {listen usage} -body {
277         set s [socket stream.server 0]
278         $s listen
279 } -returnCodes error -match glob -result {wrong # args: should be "* listen backlog"} -cleanup {
280         $s close
283 test socket-3.3 {listen usage} -body {
284         set s [socket stream.server 0]
285         $s listen blah
286 } -returnCodes error -match glob -result {expected integer but got "blah"} -cleanup {
287         $s close
290 test socket-3.4 {listen not a socket} -body {
291         set f [open [info script]]
292         $f listen 10
293 } -returnCodes error -match regexp -result {^(Socket operation on non-socket|Not a socket)$} -cleanup {
294         $f close
297 test socket-4.1 {invalid ipv6 address} -constraints ipv6 -body {
298         socket -ipv6 stream "- invalid - address -"
299 } -returnCodes error -result {Not a valid address: :::- invalid - address -}
301 test socket-4.2 {invalid ipv4 address} -body {
302         socket stream {9.9.9.9.9:0}
303 } -returnCodes error -result {Not a valid address: 9.9.9.9.9:0}
305 test socket-4.3 {sockname on non-socket} -body {
306         set f [open [info script]]
307         $f sockname
308 } -returnCodes error -match regexp -result {^(Socket operation on non-socket|Not a socket)$} -cleanup {
309         $f close
312 test socket-4.4 {peername on non-socket} -body {
313         set f [open [info script]]
314         $f peername
315 } -returnCodes error -match regexp -result {^(Socket operation on non-socket|Not a socket)$} -cleanup {
316         $f close
319 # For the eventloop tests, let's set up a client and a server where the client
320 # simply echos everything back to the server
322 set s [socket stream.server 0]
323 if {[os.fork] == 0} {
324         # child
325         set c [socket stream [$s sockname]]
326         # Note: We have to disable buffering here, otherwise
327         # when we read data in $c readable {} we many leave buffered
328         # data and readable won't retrigger.
329         $c buffering none
330         $s close
331         $c readable {
332                 # when we read we need to also read any pending data,
333                 # otherwise readable won't retrigger
334                 set buf [$c read 1]
335                 if {[string length $buf] == 0} {
336                         incr readdone
337                         $c close
338                 } else {
339                         $c puts -nonewline $buf
340                 }
341         }
342         vwait readdone
343         exit 99
346 # Now set up the server
347 set cs [$s accept addr]
348 defer {
349         $cs close
351 $s close
353 # At this point, $cs is the server connection to the client in the child process
355 test eventloop-1.1 {puts/gets} {
356         $cs puts hello
357         $cs gets
358 } hello
360 test eventloop-1.2 {puts/gets} {
361         $cs puts -nonewline again
362         lmap p [range 5] {
363                 set c [$cs read 1]
364                 set c
365         }
366 } {a g a i n}
368 test sockopt-1.1 {sockopt} -body {
369         lsort [dict keys [$cs sockopt]]
370 } -match glob -result {*tcp_nodelay*}
372 test sockopt-1.2 {sockopt set} {
373         $cs sockopt tcp_nodelay 1
374         dict get [$cs sockopt] tcp_nodelay
375 } 1
377 test sockopt-1.3 {sockopt set invalid} -body {
378         $cs sockopt tcp_nodelay badbool
379 } -returnCodes error -result {expected boolean but got "badbool"}
381 testreport