Debian Codegraph @

test-suite/tests/00-socket.test

raw
1 ;;;; 00-socket.test --- test socket functions -*- scheme -*-
2 ;;;;
3 ;;;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 ;;;; 2011, 2012, 2013, 2014, 2017, 2021 Free Software Foundation, Inc.
5 ;;;;
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;;
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 ;; This test runs early, so that we can fork before any threads are
21 ;; created in other tests.
22
23 (define-module (test-suite test-socket)
24 #:use-module (rnrs bytevectors)
25 #:use-module (srfi srfi-26)
26 #:use-module (test-suite lib))
27
28 (define (skip-on-darwin)
29 (when (string-ci=? "darwin" (utsname:sysname (uname)))
30 (throw 'untested)))
31
32 (define (skip-on-freebsd)
33 (when (string-ci=? "FreeBSD" (utsname:sysname (uname)))
34 (throw 'untested)))
35
36 (define (skip-on-cygwin-msys)
37 (when (or (string-contains %host-type "cygwin")
38 (string-contains %host-type "msys"))
39 ;; See https://cygwin.com/pipermail/cygwin/2023-June/253785.html
40 (throw 'unsupported)))
41
42
43
44 ;;;
45 ;;; inet-ntop
46 ;;;
47
48 (if (defined? 'inet-ntop)
49 (with-test-prefix "inet-ntop"
50
51 (with-test-prefix "ipv6"
52 (pass-if "0"
53 (string? (inet-ntop AF_INET6 0)))
54
55 (pass-if "2^128-1"
56 (string? (inet-ntop AF_INET6 (1- (ash 1 128)))))
57
58 (pass-if-exception "-1" exception:out-of-range
59 (inet-ntop AF_INET6 -1))
60
61 (pass-if-exception "2^128" exception:out-of-range
62 (inet-ntop AF_INET6 (ash 1 128)))
63
64 (pass-if-exception "2^1024" exception:out-of-range
65 (inet-ntop AF_INET6 (ash 1 1024))))))
66
67 ;;;
68 ;;; inet-pton
69 ;;;
70
71 (if (defined? 'inet-pton)
72 (with-test-prefix "inet-pton"
73
74 (with-test-prefix "ipv6"
75 (pass-if "00:00:00:00:00:00:00:00"
76 (eqv? 0 (inet-pton AF_INET6 "00:00:00:00:00:00:00:00")))
77
78 (pass-if "0:0:0:0:0:0:0:1"
79 (eqv? 1 (inet-pton AF_INET6 "0:0:0:0:0:0:0:1")))
80
81 (pass-if "::1"
82 (eqv? 1 (inet-pton AF_INET6 "::1")))
83
84 (pass-if "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
85 (eqv? #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
86 (inet-pton AF_INET6
87 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF")))
88
89 (pass-if "F000:0000:0000:0000:0000:0000:0000:0000"
90 (eqv? #xF0000000000000000000000000000000
91 (inet-pton AF_INET6
92 "F000:0000:0000:0000:0000:0000:0000:0000")))
93
94 (pass-if "0F00:0000:0000:0000:0000:0000:0000:0000"
95 (eqv? #x0F000000000000000000000000000000
96 (inet-pton AF_INET6
97 "0F00:0000:0000:0000:0000:0000:0000:0000")))
98
99 (pass-if "0000:0000:0000:0000:0000:0000:0000:00F0"
100 (eqv? #xF0
101 (inet-pton AF_INET6
102 "0000:0000:0000:0000:0000:0000:0000:00F0"))))))
103
104 (if (defined? 'inet-ntop)
105 (with-test-prefix "inet-ntop"
106
107 (with-test-prefix "ipv4"
108 (pass-if "127.0.0.1"
109 (equal? "127.0.0.1" (inet-ntop AF_INET INADDR_LOOPBACK))))
110
111 (if (defined? 'AF_INET6)
112 (with-test-prefix "ipv6"
113 (pass-if "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
114 (string-ci=? "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
115 (inet-ntop AF_INET6 (- (expt 2 128) 1))))
116
117 (pass-if "::1"
118 (equal? "::1" (inet-ntop AF_INET6 1)))))))
119
120
121 ;;;
122 ;;; make-socket-address
123 ;;;
124
125 (with-test-prefix "make-socket-address"
126 (if (defined? 'AF_INET)
127 (pass-if "AF_INET"
128 (let ((sa (make-socket-address AF_INET 123456 80)))
129 (and (= (sockaddr:fam sa) AF_INET)
130 (= (sockaddr:addr sa) 123456)
131 (= (sockaddr:port sa) 80)))))
132
133 (if (defined? 'AF_INET6)
134 (pass-if "AF_INET6"
135 ;; Since the platform doesn't necessarily support `scopeid', we won't
136 ;; test it.
137 (let ((sa* (make-socket-address AF_INET6 123456 80 1))
138 (sa+ (make-socket-address AF_INET6 123456 80)))
139 (and (= (sockaddr:fam sa*) (sockaddr:fam sa+) AF_INET6)
140 (= (sockaddr:addr sa*) (sockaddr:addr sa+) 123456)
141 (= (sockaddr:port sa*) (sockaddr:port sa+) 80)
142 (= (sockaddr:flowinfo sa*) 1)))))
143
144 (if (defined? 'AF_UNIX)
145 (begin
146 (pass-if "AF_UNIX"
147 (let ((sa (make-socket-address AF_UNIX "/tmp/unix-socket")))
148 (and (= (sockaddr:fam sa) AF_UNIX)
149 (string=? (sockaddr:path sa) "/tmp/unix-socket"))))
150 (pass-if "AF_UNIX abstract"
151 (let ((sa (make-socket-address AF_UNIX "\x00/tmp/abstract-socket")))
152 (and (= (sockaddr:fam sa) AF_UNIX)
153 (string=? (sockaddr:path sa) "\x00/tmp/abstract-socket")))))))
154
155 ;;;
156 ;;; setsockopt
157 ;;;
158
159 (with-test-prefix "setsockopt AF_INET"
160 (if (and (defined? 'AF_INET) (defined? 'TCP_NODELAY))
161 (pass-if "IPPROTO_TCP TCP_NODELAY"
162 (let ((sock (socket AF_INET SOCK_STREAM 0)))
163 (setsockopt sock IPPROTO_TCP TCP_NODELAY 1)
164 (not (eqv? 0 (getsockopt sock IPPROTO_TCP TCP_NODELAY)))))))
165
166
167 ;;;
168 ;;; AF_UNIX sockets and `make-socket-address'
169 ;;;
170
171 (define %tmpdir
172 ;; Honor `$TMPDIR', which tmpnam(3) doesn't do.
173 (or (getenv "TMPDIR") "/tmp"))
174
175 (define %curdir
176 ;; Remember the current working directory.
177 (getcwd))
178
179 ;; Temporarily cd to %TMPDIR. The goal is to work around path name
180 ;; limitations, which can lead to exceptions like:
181 ;;
182 ;; (misc-error "scm_to_sockaddr"
183 ;; "unix address path too long: ~A"
184 ;; ("/tmp/nix-build-fb7bph4ifh0vr3ihigm702dzffdnapfj-guile-coverage-1.9.5.drv-0/guile-test-socket-1258553296-77619")
185 ;; #f)
186 (false-if-exception (chdir %tmpdir))
187
188 (define (temp-file-path)
189 ;; Return a temporary file name, assuming the current directory is %TMPDIR.
190 (string-append "guile-test-socket-"
191 (number->string (current-time)) "-"
192 (number->string (random 100000))))
193
194 (define (primitive-fork-if-available)
195 (if (not (provided? 'fork))
196 -1
197 (begin
198 (flush-all-ports)
199 (primitive-fork))))
200
201 (if (defined? 'AF_UNIX)
202 (with-test-prefix "AF_UNIX/SOCK_DGRAM"
203
204 ;; testing `bind' and `sendto' and datagram sockets
205
206 (let ((server-socket (socket AF_UNIX SOCK_DGRAM 0))
207 (server-bound? #f)
208 (path (temp-file-path)))
209
210 (pass-if "bind"
211 (catch 'system-error
212 (lambda ()
213 (bind server-socket AF_UNIX path)
214 (set! server-bound? #t)
215 #t)
216 (lambda args
217 (let ((errno (system-error-errno args)))
218 (cond ((= errno EADDRINUSE) (throw 'unresolved))
219 (else (apply throw args)))))))
220
221 (pass-if "bind/sockaddr"
222 (let* ((sock (socket AF_UNIX SOCK_STREAM 0))
223 (path (temp-file-path))
224 (sockaddr (make-socket-address AF_UNIX path)))
225 (catch 'system-error
226 (lambda ()
227 (bind sock sockaddr)
228 (false-if-exception (delete-file path))
229 #t)
230 (lambda args
231 (let ((errno (system-error-errno args)))
232 (cond ((= errno EADDRINUSE) (throw 'unresolved))
233 (else (apply throw args))))))))
234
235 (pass-if "sendto"
236 (if (not server-bound?)
237 (throw 'unresolved)
238 (let ((client (socket AF_UNIX SOCK_DGRAM 0))
239 (message (string->utf8 "hello")))
240 (> (sendto client message AF_UNIX path) 0))))
241
242 (pass-if "sendto/sockaddr"
243 (if (not server-bound?)
244 (throw 'unresolved)
245 (let ((client (socket AF_UNIX SOCK_DGRAM 0))
246 (message (string->utf8 "hello"))
247 (sockaddr (make-socket-address AF_UNIX path)))
248 (> (sendto client message sockaddr) 0))))
249
250 (false-if-exception (delete-file path)))))
251
252
253 (if (defined? 'AF_UNIX)
254 (with-test-prefix "AF_UNIX/SOCK_STREAM"
255
256 ;; testing `bind', `listen' and `connect' on stream-oriented sockets
257
258 (let ((server-socket (socket AF_UNIX SOCK_STREAM 0))
259 (server-bound? #f)
260 (server-listening? #f)
261 (server-pid #f)
262 (path (temp-file-path)))
263
264 (pass-if "bind"
265 (catch 'system-error
266 (lambda ()
267 (bind server-socket AF_UNIX path)
268 (set! server-bound? #t)
269 #t)
270 (lambda args
271 (let ((errno (system-error-errno args)))
272 (cond ((= errno EADDRINUSE) (throw 'unresolved))
273 (else (apply throw args)))))))
274
275 (pass-if "bind/sockaddr"
276 (let* ((sock (socket AF_UNIX SOCK_STREAM 0))
277 (path (temp-file-path))
278 (sockaddr (make-socket-address AF_UNIX path)))
279 (catch 'system-error
280 (lambda ()
281 (bind sock sockaddr)
282 (false-if-exception (delete-file path))
283 #t)
284 (lambda args
285 (let ((errno (system-error-errno args)))
286 (cond ((= errno EADDRINUSE) (throw 'unresolved))
287 (else (apply throw args))))))))
288
289 (pass-if "listen"
290 (if (not server-bound?)
291 (throw 'unresolved)
292 (begin
293 (listen server-socket 123)
294 (set! server-listening? #t)
295 #t)))
296
297 (when server-listening?
298 (let ((pid (primitive-fork-if-available)))
299 ;; Spawn a server process.
300 (case pid
301 ((-1) ;; fork not available
302 #f)
303 ((0) ;; the kid: serve two connections and exit
304 (let serve ((conn
305 (false-if-exception (accept server-socket)))
306 (count 1))
307 (if (not conn)
308 (exit 1)
309 (if (> count 0)
310 (serve (false-if-exception (accept server-socket))
311 (- count 1)))))
312 (exit 0))
313 (else ;; the parent
314 (set! server-pid pid)
315 #t))))
316
317 (pass-if "connect"
318 (if (not server-pid)
319 (throw 'unresolved)
320 (let ((s (socket AF_UNIX SOCK_STREAM 0)))
321 (connect s AF_UNIX path)
322 #t)))
323
324 (pass-if "connect/sockaddr"
325 (if (not server-pid)
326 (throw 'unresolved)
327 (let ((s (socket AF_UNIX SOCK_STREAM 0)))
328 (connect s (make-socket-address AF_UNIX path))
329 #t)))
330
331 (pass-if "accept"
332 (if (not server-pid)
333 (throw 'unresolved)
334 (let ((status (cdr (waitpid server-pid))))
335 (eqv? 0 (status:exit-val status)))))
336
337 (false-if-exception (delete-file path))
338
339 #t)
340
341 ;; testing `bind', `listen' and `connect' on abstract stream-oriented sockets
342
343 (let ((server-socket (socket AF_UNIX SOCK_STREAM 0))
344 (server-bound? #f)
345 (server-listening? #f)
346 (server-pid #f)
347 (path (temp-file-path)))
348
349 (false-if-exception (delete-file path))
350 (set! path (string-append "\x00" path))
351
352 (pass-if "bind abstract"
353 (skip-on-darwin)
354 (skip-on-cygwin-msys)
355 (skip-on-freebsd)
356 (catch 'system-error
357 (lambda ()
358 (bind server-socket AF_UNIX path)
359 (set! server-bound? #t)
360 #t)
361 (lambda args
362 (let ((errno (system-error-errno args)))
363 (if (= errno EADDRINUSE)
364 (throw 'unresolved)
365 (apply throw args))))))
366
367 (pass-if "listen abstract"
368 (skip-on-darwin)
369 (if (not server-bound?)
370 (throw 'unresolved)
371 (begin
372 (listen server-socket 123)
373 (set! server-listening? #t)
374 #t)))
375
376 (when server-listening?
377 (let ((pid (primitive-fork-if-available)))
378 ;; Spawn a server process.
379 (case pid
380 ((-1) ;; fork not available
381 #f)
382 ((0) ;; the kid: serve one connection and exit
383 (let serve ((conn
384 (false-if-exception (accept server-socket)))
385 (count 0))
386 (if (not conn)
387 (exit 1)
388 (exit 0))))
389 (else ;; the parent
390 (set! server-pid pid)
391 #t))))
392
393 (pass-if "connect abstract"
394 (skip-on-darwin)
395 (if (not server-pid)
396 (throw 'unresolved)
397 (let ((s (socket AF_UNIX SOCK_STREAM 0)))
398 (display "connect abstract\n")
399 (connect s AF_UNIX path)
400 #t)))
401
402 (pass-if "accept abstract"
403 (skip-on-darwin)
404 (if (not server-pid)
405 (throw 'unresolved)
406 (begin
407 (let ((status (cdr (waitpid server-pid))))
408 (eqv? 0 (status:exit-val status))))))
409
410 #t)
411
412 ;; Testing `send', `recv!' & co. on stream-oriented sockets (with
413 ;; a bit of duplication with the above.)
414
415 (let ((server-socket (socket AF_UNIX SOCK_STREAM 0))
416 (server-bound? #f)
417 (server-listening? #f)
418 (server-pid #f)
419 (message "hello, world!")
420 (path (temp-file-path)))
421
422 (define (sub-bytevector bv len)
423 (let ((c (make-bytevector len)))
424 (bytevector-copy! bv 0 c 0 len)
425 c))
426
427 (pass-if "bind (bis)"
428 (catch 'system-error
429 (lambda ()
430 (bind server-socket AF_UNIX path)
431 (set! server-bound? #t)
432 #t)
433 (lambda args
434 (let ((errno (system-error-errno args)))
435 (cond ((= errno EADDRINUSE) (throw 'unresolved))
436 (else (apply throw args)))))))
437
438 (pass-if "listen (bis)"
439 (if (not server-bound?)
440 (throw 'unresolved)
441 (begin
442 (listen server-socket 123)
443 (set! server-listening? #t)
444 #t)))
445
446 (if server-listening?
447 (let ((pid (primitive-fork-if-available)))
448 ;; Spawn a server process.
449 (case pid
450 ((-1)
451 #f)
452 ((0) ;; the kid: send MESSAGE and exit
453 (exit
454 (false-if-exception
455 (let ((conn (car (accept server-socket)))
456 (bv (string->utf8 message)))
457 (= (bytevector-length bv)
458 (send conn bv))))))
459 (else ;; the parent
460 (set! server-pid pid)
461 #t))))
462
463 (pass-if "recv!"
464 (if (not server-pid)
465 (throw 'unresolved)
466 (let ((s (socket AF_UNIX SOCK_STREAM 0)))
467 (connect s AF_UNIX path)
468 (let* ((buf (make-bytevector 123))
469 (received (recv! s buf)))
470 (string=? (utf8->string (sub-bytevector buf received))
471 message)))))
472
473 (pass-if "accept (bis)"
474 (if (not server-pid)
475 (throw 'unresolved)
476 (let ((status (cdr (waitpid server-pid))))
477 (eqv? 0 (status:exit-val status)))))
478
479 (false-if-exception (delete-file path))
480
481 #t)))
482
483
484 (if (defined? 'AF_INET6)
485 (with-test-prefix "AF_INET6/SOCK_STREAM"
486
487 ;; testing `bind', `listen' and `connect' on stream-oriented sockets
488
489 (let ((server-socket
490 ;; Some platforms don't support this protocol/family combination.
491 (false-if-exception (socket AF_INET6 SOCK_STREAM 0)))
492 (server-bound? #f)
493 (server-listening? #f)
494 (server-pid #f)
495 (ipv6-addr 1) ; ::1
496 (server-port 8889)
497 (client-port 9998))
498
499 (pass-if "bind"
500 (if (not server-socket)
501 (throw 'unresolved))
502 (catch 'system-error
503 (lambda ()
504 (bind server-socket AF_INET6 ipv6-addr server-port)
505 (set! server-bound? #t)
506 #t)
507 (lambda args
508 (let ((errno (system-error-errno args)))
509 (cond ((= errno EADDRINUSE) (throw 'unresolved))
510
511 ;; On Linux-based systems, when `ipv6' support is
512 ;; missing (for instance, `ipv6' is loaded and
513 ;; /proc/sys/net/ipv6/conf/all/disable_ipv6 is set
514 ;; to 1), the socket call above succeeds but
515 ;; bind(2) fails like this.
516 ((= errno EADDRNOTAVAIL) (throw 'unresolved))
517
518 (else (apply throw args)))))))
519
520 (pass-if "bind/sockaddr"
521 (let* ((sock (false-if-exception (socket AF_INET6 SOCK_STREAM 0)))
522 (sockaddr (make-socket-address AF_INET6 ipv6-addr client-port)))
523 (if (not sock)
524 (throw 'unresolved))
525 (catch 'system-error
526 (lambda ()
527 (bind sock sockaddr)
528 #t)
529 (lambda args
530 (let ((errno (system-error-errno args)))
531 (cond ((= errno EADDRINUSE) (throw 'unresolved))
532 ((= errno EADDRNOTAVAIL) (throw 'unresolved))
533 (else (apply throw args))))))))
534
535 (pass-if "listen"
536 (if (not server-bound?)
537 (throw 'unresolved)
538 (begin
539 (listen server-socket 123)
540 (set! server-listening? #t)
541 #t)))
542
543 (if server-listening?
544 (let ((pid (primitive-fork-if-available)))
545 ;; Spawn a server process.
546 (case pid
547 ((-1)
548 #f)
549 ((0) ;; the kid: serve two connections and exit
550 (let serve ((conn
551 (false-if-exception (accept server-socket)))
552 (count 1))
553 (if (not conn)
554 (exit 1)
555 (if (> count 0)
556 (serve (false-if-exception (accept server-socket))
557 (- count 1)))))
558 (exit 0))
559 (else ;; the parent
560 (set! server-pid pid)
561 #t))))
562
563 (pass-if "connect"
564 (if (not server-pid)
565 (throw 'unresolved)
566 (let ((s (socket AF_INET6 SOCK_STREAM 0)))
567 (connect s AF_INET6 ipv6-addr server-port)
568 #t)))
569
570 (pass-if "connect/sockaddr"
571 (if (not server-pid)
572 (throw 'unresolved)
573 (let ((s (socket AF_INET6 SOCK_STREAM 0)))
574 (connect s (make-socket-address AF_INET6 ipv6-addr server-port))
575 #t)))
576
577 (pass-if "accept"
578 (if (not server-pid)
579 (throw 'unresolved)
580 (let ((status (cdr (waitpid server-pid))))
581 (eqv? 0 (status:exit-val status)))))
582
583 #t)))
584
585 ;; Switch back to the previous directory.
586 (false-if-exception (chdir %curdir))