1 ;;;; socket.test --- test socket functions -*- scheme -*-
3 ;;;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 2.1 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (define-module (test-suite test-numbers)
20 #:use-module (test-suite lib))
27 (if (defined? 'inet-ntop)
28 (with-test-prefix "inet-ntop"
30 (with-test-prefix "ipv6"
32 (string? (inet-ntop AF_INET6 0)))
35 (string? (inet-ntop AF_INET6 (1- (ash 1 128)))))
37 (pass-if-exception "-1" exception:out-of-range
38 (inet-ntop AF_INET6 -1))
40 (pass-if-exception "2^128" exception:out-of-range
41 (inet-ntop AF_INET6 (ash 1 128)))
43 (pass-if-exception "2^1024" exception:out-of-range
44 (inet-ntop AF_INET6 (ash 1 1024))))))
50 (if (defined? 'inet-pton)
51 (with-test-prefix "inet-pton"
53 (with-test-prefix "ipv6"
54 (pass-if "00:00:00:00:00:00:00:00"
55 (eqv? 0 (inet-pton AF_INET6 "00:00:00:00:00:00:00:00")))
57 (pass-if "0:0:0:0:0:0:0:1"
58 (eqv? 1 (inet-pton AF_INET6 "0:0:0:0:0:0:0:1")))
61 (eqv? 1 (inet-pton AF_INET6 "::1")))
63 (pass-if "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
64 (eqv? #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
66 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF")))
68 (pass-if "F000:0000:0000:0000:0000:0000:0000:0000"
69 (eqv? #xF0000000000000000000000000000000
71 "F000:0000:0000:0000:0000:0000:0000:0000")))
73 (pass-if "0F00:0000:0000:0000:0000:0000:0000:0000"
74 (eqv? #x0F000000000000000000000000000000
76 "0F00:0000:0000:0000:0000:0000:0000:0000")))
78 (pass-if "0000:0000:0000:0000:0000:0000:0000:00F0"
81 "0000:0000:0000:0000:0000:0000:0000:00F0"))))))
85 ;;; make-socket-address
88 (with-test-prefix "make-socket-address"
89 (if (defined? 'AF_INET)
91 (let ((sa (make-socket-address AF_INET 123456 80)))
92 (and (= (sockaddr:fam sa) AF_INET)
93 (= (sockaddr:addr sa) 123456)
94 (= (sockaddr:port sa) 80)))))
96 (if (defined? 'AF_INET6)
98 ;; Since the platform doesn't necessarily support `scopeid', we won't
100 (let ((sa* (make-socket-address AF_INET6 123456 80 1))
101 (sa+ (make-socket-address AF_INET6 123456 80)))
102 (and (= (sockaddr:fam sa*) (sockaddr:fam sa+) AF_INET6)
103 (= (sockaddr:addr sa*) (sockaddr:addr sa+) 123456)
104 (= (sockaddr:port sa*) (sockaddr:port sa+) 80)
105 (= (sockaddr:flowinfo sa*) 1)))))
107 (if (defined? 'AF_UNIX)
109 (let ((sa (make-socket-address AF_UNIX "/tmp/unix-socket")))
110 (and (= (sockaddr:fam sa) AF_UNIX)
111 (string=? (sockaddr:path sa) "/tmp/unix-socket"))))))
116 ;;; AF_UNIX sockets and `make-socket-address'
119 (if (defined? 'AF_UNIX)
120 (with-test-prefix "AF_UNIX/SOCK_DGRAM"
122 ;; testing `bind' and `sendto' and datagram sockets
124 (let ((server-socket (socket AF_UNIX SOCK_DGRAM 0))
131 (bind server-socket AF_UNIX path)
132 (set! server-bound? #t)
135 (let ((errno (system-error-errno args)))
136 (cond ((= errno EADDRINUSE) (throw 'unresolved))
137 (else (apply throw args)))))))
139 (pass-if "bind/sockaddr"
140 (let* ((sock (socket AF_UNIX SOCK_STREAM 0))
142 (sockaddr (make-socket-address AF_UNIX path)))
146 (false-if-exception (delete-file path))
149 (let ((errno (system-error-errno args)))
150 (cond ((= errno EADDRINUSE) (throw 'unresolved))
151 (else (apply throw args))))))))
154 (if (not server-bound?)
156 (let ((client (socket AF_UNIX SOCK_DGRAM 0)))
157 (> (sendto client "hello" AF_UNIX path) 0))))
159 (pass-if "sendto/sockaddr"
160 (if (not server-bound?)
162 (let ((client (socket AF_UNIX SOCK_DGRAM 0))
163 (sockaddr (make-socket-address AF_UNIX path)))
164 (> (sendto client "hello" sockaddr) 0))))
166 (false-if-exception (delete-file path)))))
169 (if (defined? 'AF_UNIX)
170 (with-test-prefix "AF_UNIX/SOCK_STREAM"
172 ;; testing `bind', `listen' and `connect' on stream-oriented sockets
174 (let ((server-socket (socket AF_UNIX SOCK_STREAM 0))
176 (server-listening? #f)
183 (bind server-socket AF_UNIX path)
184 (set! server-bound? #t)
187 (let ((errno (system-error-errno args)))
188 (cond ((= errno EADDRINUSE) (throw 'unresolved))
189 (else (apply throw args)))))))
191 (pass-if "bind/sockaddr"
192 (let* ((sock (socket AF_UNIX SOCK_STREAM 0))
194 (sockaddr (make-socket-address AF_UNIX path)))
198 (false-if-exception (delete-file path))
201 (let ((errno (system-error-errno args)))
202 (cond ((= errno EADDRINUSE) (throw 'unresolved))
203 (else (apply throw args))))))))
206 (if (not server-bound?)
209 (listen server-socket 123)
210 (set! server-listening? #t)
213 (if server-listening?
214 (let ((pid (primitive-fork)))
215 ;; Spawn a server process.
217 ((-1) (throw 'unresolved))
218 ((0) ;; the kid: serve two connections and exit
220 (false-if-exception (accept server-socket)))
225 (serve (false-if-exception (accept server-socket))
229 (set! server-pid pid)
235 (let ((s (socket AF_UNIX SOCK_STREAM 0)))
236 (connect s AF_UNIX path)
239 (pass-if "connect/sockaddr"
242 (let ((s (socket AF_UNIX SOCK_STREAM 0)))
243 (connect s (make-socket-address AF_UNIX path))
249 (let ((status (cdr (waitpid server-pid))))
250 (eq? 0 (status:exit-val status)))))
252 (false-if-exception (delete-file path))