1 ;;;; socket.test --- test socket functions -*- scheme -*-
3 ;;;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 ;;;; 2011 Free Software Foundation, Inc.
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.
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.
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
20 (define-module (test-suite test-socket)
21 #:use-module (rnrs bytevectors)
22 #:use-module (srfi srfi-26)
23 #:use-module (test-suite lib))
32 (with-test-prefix "htonl"
34 (pass-if "0" (eqv? 0 (htonl 0)))
36 (pass-if-exception "-1" exception:out-of-range
39 ;; prior to guile 1.6.9 and 1.8.1, systems with 64-bit longs didn't detect
40 ;; an overflow for values 2^32 <= x < 2^63
41 (pass-if-exception "2^32" exception:out-of-range
44 (pass-if-exception "2^1024" exception:out-of-range
45 (htonl (ash 1 1024)))))
52 (if (defined? 'inet-ntop)
53 (with-test-prefix "inet-ntop"
55 (with-test-prefix "ipv6"
57 (string? (inet-ntop AF_INET6 0)))
60 (string? (inet-ntop AF_INET6 (1- (ash 1 128)))))
62 (pass-if-exception "-1" exception:out-of-range
63 (inet-ntop AF_INET6 -1))
65 (pass-if-exception "2^128" exception:out-of-range
66 (inet-ntop AF_INET6 (ash 1 128)))
68 (pass-if-exception "2^1024" exception:out-of-range
69 (inet-ntop AF_INET6 (ash 1 1024))))))
75 (if (defined? 'inet-pton)
76 (with-test-prefix "inet-pton"
78 (with-test-prefix "ipv6"
79 (pass-if "00:00:00:00:00:00:00:00"
80 (eqv? 0 (inet-pton AF_INET6 "00:00:00:00:00:00:00:00")))
82 (pass-if "0:0:0:0:0:0:0:1"
83 (eqv? 1 (inet-pton AF_INET6 "0:0:0:0:0:0:0:1")))
86 (eqv? 1 (inet-pton AF_INET6 "::1")))
88 (pass-if "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
89 (eqv? #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
91 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF")))
93 (pass-if "F000:0000:0000:0000:0000:0000:0000:0000"
94 (eqv? #xF0000000000000000000000000000000
96 "F000:0000:0000:0000:0000:0000:0000:0000")))
98 (pass-if "0F00:0000:0000:0000:0000:0000:0000:0000"
99 (eqv? #x0F000000000000000000000000000000
101 "0F00:0000:0000:0000:0000:0000:0000:0000")))
103 (pass-if "0000:0000:0000:0000:0000:0000:0000:00F0"
106 "0000:0000:0000:0000:0000:0000:0000:00F0"))))))
108 (if (defined? 'inet-ntop)
109 (with-test-prefix "inet-ntop"
111 (with-test-prefix "ipv4"
113 (equal? "127.0.0.1" (inet-ntop AF_INET INADDR_LOOPBACK))))
115 (if (defined? 'AF_INET6)
116 (with-test-prefix "ipv6"
117 (pass-if "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
118 (string-ci=? "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
119 (inet-ntop AF_INET6 (- (expt 2 128) 1))))
122 (equal? "::1" (inet-ntop AF_INET6 1)))))))
126 ;;; make-socket-address
129 (with-test-prefix "make-socket-address"
130 (if (defined? 'AF_INET)
132 (let ((sa (make-socket-address AF_INET 123456 80)))
133 (and (= (sockaddr:fam sa) AF_INET)
134 (= (sockaddr:addr sa) 123456)
135 (= (sockaddr:port sa) 80)))))
137 (if (defined? 'AF_INET6)
139 ;; Since the platform doesn't necessarily support `scopeid', we won't
141 (let ((sa* (make-socket-address AF_INET6 123456 80 1))
142 (sa+ (make-socket-address AF_INET6 123456 80)))
143 (and (= (sockaddr:fam sa*) (sockaddr:fam sa+) AF_INET6)
144 (= (sockaddr:addr sa*) (sockaddr:addr sa+) 123456)
145 (= (sockaddr:port sa*) (sockaddr:port sa+) 80)
146 (= (sockaddr:flowinfo sa*) 1)))))
148 (if (defined? 'AF_UNIX)
150 (let ((sa (make-socket-address AF_UNIX "/tmp/unix-socket")))
151 (and (= (sockaddr:fam sa) AF_UNIX)
152 (string=? (sockaddr:path sa) "/tmp/unix-socket"))))))
158 (if (defined? 'ntohl)
159 (with-test-prefix "ntohl"
161 (pass-if "0" (eqv? 0 (ntohl 0)))
163 (pass-if-exception "-1" exception:out-of-range
166 ;; prior to guile 1.6.9 and 1.8.1, systems with 64-bit longs didn't detect
167 ;; an overflow for values 2^32 <= x < 2^63
168 (pass-if-exception "2^32" exception:out-of-range
171 (pass-if-exception "2^1024" exception:out-of-range
172 (ntohl (ash 1 1024)))))
177 ;;; AF_UNIX sockets and `make-socket-address'
181 ;; Honor `$TMPDIR', which tmpnam(3) doesn't do.
182 (or (getenv "TMPDIR") "/tmp"))
185 ;; Remember the current working directory.
188 ;; Temporarily cd to %TMPDIR. The goal is to work around path name
189 ;; limitations, which can lead to exceptions like:
191 ;; (misc-error "scm_to_sockaddr"
192 ;; "unix address path too long: ~A"
193 ;; ("/tmp/nix-build-fb7bph4ifh0vr3ihigm702dzffdnapfj-guile-coverage-1.9.5.drv-0/guile-test-socket-1258553296-77619")
197 (define (temp-file-path)
198 ;; Return a temporary file name, assuming the current directory is %TMPDIR.
199 (string-append "guile-test-socket-"
200 (number->string (current-time)) "-"
201 (number->string (random 100000))))
204 (if (defined? 'AF_UNIX)
205 (with-test-prefix "AF_UNIX/SOCK_DGRAM"
207 ;; testing `bind' and `sendto' and datagram sockets
209 (let ((server-socket (socket AF_UNIX SOCK_DGRAM 0))
211 (path (temp-file-path)))
216 (bind server-socket AF_UNIX path)
217 (set! server-bound? #t)
220 (let ((errno (system-error-errno args)))
221 (cond ((= errno EADDRINUSE) (throw 'unresolved))
222 (else (apply throw args)))))))
224 (pass-if "bind/sockaddr"
225 (let* ((sock (socket AF_UNIX SOCK_STREAM 0))
226 (path (temp-file-path))
227 (sockaddr (make-socket-address AF_UNIX path)))
231 (false-if-exception (delete-file path))
234 (let ((errno (system-error-errno args)))
235 (cond ((= errno EADDRINUSE) (throw 'unresolved))
236 (else (apply throw args))))))))
239 (if (not server-bound?)
241 (let ((client (socket AF_UNIX SOCK_DGRAM 0))
242 (message (string->utf8 "hello")))
243 (> (sendto client message AF_UNIX path) 0))))
245 (pass-if "sendto/sockaddr"
246 (if (not server-bound?)
248 (let ((client (socket AF_UNIX SOCK_DGRAM 0))
249 (message (string->utf8 "hello"))
250 (sockaddr (make-socket-address AF_UNIX path)))
251 (> (sendto client message sockaddr) 0))))
253 (false-if-exception (delete-file path)))))
256 (if (defined? 'AF_UNIX)
257 (with-test-prefix "AF_UNIX/SOCK_STREAM"
259 ;; testing `bind', `listen' and `connect' on stream-oriented sockets
261 (let ((server-socket (socket AF_UNIX SOCK_STREAM 0))
263 (server-listening? #f)
265 (path (temp-file-path)))
270 (bind server-socket AF_UNIX path)
271 (set! server-bound? #t)
274 (let ((errno (system-error-errno args)))
275 (cond ((= errno EADDRINUSE) (throw 'unresolved))
276 (else (apply throw args)))))))
278 (pass-if "bind/sockaddr"
279 (let* ((sock (socket AF_UNIX SOCK_STREAM 0))
280 (path (temp-file-path))
281 (sockaddr (make-socket-address AF_UNIX path)))
285 (false-if-exception (delete-file path))
288 (let ((errno (system-error-errno args)))
289 (cond ((= errno EADDRINUSE) (throw 'unresolved))
290 (else (apply throw args))))))))
293 (if (not server-bound?)
296 (listen server-socket 123)
297 (set! server-listening? #t)
300 (force-output (current-output-port))
301 (force-output (current-error-port))
302 (if server-listening?
303 (let ((pid (primitive-fork)))
304 ;; Spawn a server process.
306 ((-1) (throw 'unresolved))
307 ((0) ;; the kid: serve two connections and exit
309 (false-if-exception (accept server-socket)))
314 (serve (false-if-exception (accept server-socket))
318 (set! server-pid pid)
324 (let ((s (socket AF_UNIX SOCK_STREAM 0)))
325 (connect s AF_UNIX path)
328 (pass-if "connect/sockaddr"
331 (let ((s (socket AF_UNIX SOCK_STREAM 0)))
332 (connect s (make-socket-address AF_UNIX path))
338 (let ((status (cdr (waitpid server-pid))))
339 (eq? 0 (status:exit-val status)))))
341 (false-if-exception (delete-file path))
346 ;; Testing `send', `recv!' & co. on stream-oriented sockets (with
347 ;; a bit of duplication with the above.)
349 (let ((server-socket (socket AF_UNIX SOCK_STREAM 0))
351 (server-listening? #f)
353 (message "hello, world!")
354 (path (temp-file-path)))
356 (define (sub-bytevector bv len)
357 (let ((c (make-bytevector len)))
358 (bytevector-copy! bv 0 c 0 len)
361 (pass-if "bind (bis)"
364 (bind server-socket AF_UNIX path)
365 (set! server-bound? #t)
368 (let ((errno (system-error-errno args)))
369 (cond ((= errno EADDRINUSE) (throw 'unresolved))
370 (else (apply throw args)))))))
372 (pass-if "listen (bis)"
373 (if (not server-bound?)
376 (listen server-socket 123)
377 (set! server-listening? #t)
380 (force-output (current-output-port))
381 (force-output (current-error-port))
382 (if server-listening?
383 (let ((pid (primitive-fork)))
384 ;; Spawn a server process.
386 ((-1) (throw 'unresolved))
387 ((0) ;; the kid: send MESSAGE and exit
390 (let ((conn (car (accept server-socket)))
391 (bv (string->utf8 message)))
392 (= (bytevector-length bv)
395 (set! server-pid pid)
401 (let ((s (socket AF_UNIX SOCK_STREAM 0)))
402 (connect s AF_UNIX path)
403 (let* ((buf (make-bytevector 123))
404 (received (recv! s buf)))
405 (string=? (utf8->string (sub-bytevector buf received))
408 (pass-if "accept (bis)"
411 (let ((status (cdr (waitpid server-pid))))
412 (eq? 0 (status:exit-val status)))))
414 (false-if-exception (delete-file path))
419 (if (defined? 'AF_INET6)
420 (with-test-prefix "AF_INET6/SOCK_STREAM"
422 ;; testing `bind', `listen' and `connect' on stream-oriented sockets
425 ;; Some platforms don't support this protocol/family combination.
426 (false-if-exception (socket AF_INET6 SOCK_STREAM 0)))
428 (server-listening? #f)
435 (if (not server-socket)
439 (bind server-socket AF_INET6 ipv6-addr server-port)
440 (set! server-bound? #t)
443 (let ((errno (system-error-errno args)))
444 (cond ((= errno EADDRINUSE) (throw 'unresolved))
445 (else (apply throw args)))))))
447 (pass-if "bind/sockaddr"
448 (let* ((sock (false-if-exception (socket AF_INET6 SOCK_STREAM 0)))
449 (sockaddr (make-socket-address AF_INET6 ipv6-addr client-port)))
457 (let ((errno (system-error-errno args)))
458 (cond ((= errno EADDRINUSE) (throw 'unresolved))
459 (else (apply throw args))))))))
462 (if (not server-bound?)
465 (listen server-socket 123)
466 (set! server-listening? #t)
469 (force-output (current-output-port))
470 (force-output (current-error-port))
471 (if server-listening?
472 (let ((pid (primitive-fork)))
473 ;; Spawn a server process.
475 ((-1) (throw 'unresolved))
476 ((0) ;; the kid: serve two connections and exit
478 (false-if-exception (accept server-socket)))
483 (serve (false-if-exception (accept server-socket))
487 (set! server-pid pid)
493 (let ((s (socket AF_INET6 SOCK_STREAM 0)))
494 (connect s AF_INET6 ipv6-addr server-port)
497 (pass-if "connect/sockaddr"
500 (let ((s (socket AF_INET6 SOCK_STREAM 0)))
501 (connect s (make-socket-address AF_INET6 ipv6-addr server-port))
507 (let ((status (cdr (waitpid server-pid))))
508 (eq? 0 (status:exit-val status)))))
512 ;; Switch back to the previous directory.
513 (false-if-exception (chdir %curdir))