1 (defpackage "SB-BSD-SOCKETS-TEST"
2 (:use "CL" "SB-BSD-SOCKETS" "SB-RT"))
4 (in-package :sb-bsd-sockets-test)
6 (defmacro deftest* ((name &key fails-on) form &rest results)
8 (when (sb-impl::featurep ',fails-on)
9 (pushnew ',name sb-rt::*expected-failures*))
10 (deftest ,name ,form ,@results)))
13 (deftest make-inet-address
14 (equalp (make-inet-address "127.0.0.1") #(127 0 0 1))
16 ;;; and an address with bit 8 set on some octets
17 (deftest make-inet-address2
18 (equalp (make-inet-address "242.1.211.3") #(242 1 211 3))
21 (deftest get-protocol-by-name/tcp
22 (integerp (get-protocol-by-name "tcp"))
25 (deftest get-protocol-by-name/udp
26 (integerp (get-protocol-by-name "udp"))
29 ;;; See https://bugs.launchpad.net/sbcl/+bug/659857
30 ;;; Apparently getprotobyname_r on FreeBSD says -1 and EINTR
31 ;;; for unknown protocols...
32 #-(and freebsd sb-thread)
33 (deftest get-protocol-by-name/error
34 (handler-case (get-protocol-by-name "nonexistent-protocol")
41 (deftest make-inet-socket
43 (let ((s (make-instance 'inet-socket :type :stream :protocol (get-protocol-by-name "tcp"))))
44 (and (> (socket-file-descriptor s) 1) t))
47 (deftest make-inet-socket-keyword
49 (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
50 (and (> (socket-file-descriptor s) 1) t))
53 (deftest* (make-inet-socket-wrong)
54 ;; fail to make a socket: check correct error return. There's no nice
55 ;; way to check the condition stuff on its own, which is a shame
57 (make-instance 'inet-socket :type :stream :protocol (get-protocol-by-name "udp"))
58 ;; CLH FIXME! some versions of darwin just return a socket error
59 ;; here, not socket-type-not-supported-error or
60 ;; protocol-not-supported-error.
61 ((or #+darwin socket-error
62 socket-type-not-supported-error
63 protocol-not-supported-error)
65 (declare (ignorable c)) t)
69 (deftest* (make-inet-socket-keyword-wrong)
70 ;; same again with keywords
72 (make-instance 'inet-socket :type :stream :protocol :udp)
73 ;; CLH FIXME! some versions of darwin just return a socket error
74 ;; here, not socket-type-not-supported-error or
75 ;; protocol-not-supported-error.
78 protocol-not-supported-error
79 socket-type-not-supported-error)
81 (declare (ignorable c)) t)
86 (deftest* (non-block-socket)
87 (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
88 (setf (non-blocking-mode s) t)
89 (non-blocking-mode s))
92 (deftest inet-socket-bind
93 (let* ((tcp (get-protocol-by-name "tcp"))
94 (address (make-inet-address "127.0.0.1"))
95 (s1 (make-instance 'inet-socket :type :stream :protocol tcp))
96 (s2 (make-instance 'inet-socket :type :stream :protocol tcp)))
98 ;; Given the functions we've got so far, if you can think of a
99 ;; better way to make sure the bind succeeded than trying it
100 ;; twice, let me know
102 (socket-bind s1 address 0)
104 (let ((port (nth-value 1 (socket-name s1))))
105 (socket-bind s2 address port)
107 (address-in-use-error () t)))
112 (deftest* (simple-sockopt-test)
113 ;; test we can set SO_REUSEADDR on a socket and retrieve it, and in
114 ;; the process that all the weird macros in sockopt happened right.
115 (let ((s (make-instance 'inet-socket :type :stream :protocol (get-protocol-by-name "tcp"))))
116 (setf (sockopt-reuse-address s) t)
117 (sockopt-reuse-address s))
120 (defun read-buf-nonblock (buffer stream)
121 "Like READ-SEQUENCE, but returns early if the full quantity of data isn't there to be read. Blocks if no input at all"
122 (let ((eof (gensym)))
124 (c (read-char stream nil eof)
125 (read-char-no-hang stream nil eof)))
126 ((or (>= i (length buffer)) (not c) (eq c eof)) i)
127 (setf (elt buffer i) c))))
130 (deftest name-service-return-type
131 (vectorp (host-ent-address (get-host-by-address #(127 0 0 1))))
134 ;;; these require that the echo services are turned on in inetd
136 (deftest simple-tcp-client
137 (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp))
138 (data (make-string 200)))
139 (socket-connect s #(127 0 0 1) 7)
140 (let ((stream (socket-make-stream s :input t :output t :buffering :none)))
141 (format stream "here is some text")
142 (let ((data (subseq data 0 (read-buf-nonblock data stream))))
143 (format t "~&Got ~S back from TCP echo server~%" data)
144 (> (length data) 0))))
148 (deftest sockaddr-return-type
149 (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
152 (socket-connect s #(127 0 0 1) 7)
153 (multiple-value-bind (host port) (socket-peername s)
160 (deftest simple-udp-client
161 (let ((s (make-instance 'inet-socket :type :datagram :protocol (get-protocol-by-name "udp")))
162 (data (make-string 200)))
163 (format t "Socket type is ~A~%" (sockopt-type s))
164 (socket-connect s #(127 0 0 1) 7)
165 (let ((stream (socket-make-stream s :input t :output t :buffering :none)))
166 (format stream "here is some text")
167 (let ((data (subseq data 0 (read-buf-nonblock data stream))))
168 (format t "~&Got ~S back from UDP echo server~%" data)
169 (> (length data) 0))))
172 ;;; A fairly rudimentary test that connects to the syslog socket and
173 ;;; sends a message. Priority 7 is kern.debug; you'll probably want
174 ;;; to look at /etc/syslog.conf or local equivalent to find out where
175 ;;; the message ended up
178 (deftest simple-local-client
180 ;; SunOS (Solaris) and Darwin systems don't have a socket at
181 ;; /dev/log. We might also be building in a chroot or
182 ;; something, so don't fail this test just because the file is
183 ;; unavailable, or if it's a symlink to some weird character
186 (handler-bind ((sb-posix:syscall-error
191 (sb-posix::stat-mode (sb-posix:stat "/dev/log")))))
192 (let ((s (make-instance 'local-socket :type :datagram)))
193 (format t "Connecting ~A... " s)
196 (socket-connect s "/dev/log")
197 (sb-bsd-sockets::socket-error ()
198 (setq s (make-instance 'local-socket :type :stream))
199 (format t "failed~%Retrying with ~A... " s)
201 (socket-connect s "/dev/log")))
203 (let ((stream (socket-make-stream s :input t :output t :buffering :none)))
205 "<7>bsd-sockets: Don't panic. We're testing local-domain client code; this message can safely be ignored"))))
210 ;;; these require that the internet (or bits of it, at least) is available
213 (deftest get-host-by-name
214 (equalp (car (host-ent-addresses (get-host-by-name "a.root-servers.net")))
219 (deftest get-host-by-address
220 (host-ent-name (get-host-by-address #(198 41 0 4)))
221 "a.root-servers.net")
223 ;;; These days lots of people seem to be using DNS servers that don't
224 ;;; report resolving failures for non-existing domains. This test
225 ;;; will fail there, so we've disabled it.
227 (deftest get-host-by-name-wrong
229 (get-host-by-name "foo.tninkpad.telent.net.")
230 (NAME-SERVICE-ERROR () t)
234 (defun http-stream (host port request)
235 (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
237 s (car (host-ent-addresses (get-host-by-name host))) port)
238 (let ((stream (socket-make-stream s :input t :output t :buffering :none)))
239 (format stream "~A HTTP/1.0~%~%" request))
243 (deftest simple-http-client-1
245 (let ((s (http-stream "ww.telent.net" 80 "HEAD /")))
246 (let ((data (make-string 200)))
247 (setf data (subseq data 0
248 (read-buf-nonblock data
249 (socket-make-stream s))))
251 (> (length data) 0)))
252 (network-unreachable-error () 'network-unreachable))
257 (deftest sockopt-receive-buffer
258 ;; on Linux x86, the receive buffer size appears to be doubled in the
259 ;; kernel: we set a size of x and then getsockopt() returns 2x.
260 ;; This is why we compare with >= instead of =
262 (let ((s (http-stream "ww.telent.net" 80 "HEAD /")))
263 (setf (sockopt-receive-buffer s) 1975)
264 (let ((data (make-string 200)))
265 (setf data (subseq data 0
266 (read-buf-nonblock data
267 (socket-make-stream s))))
268 (and (> (length data) 0)
269 (>= (sockopt-receive-buffer s) 1975))))
270 (network-unreachable-error () 'network-unreachable))
273 (deftest socket-open-p-true.1
274 (socket-open-p (make-instance 'inet-socket :type :stream :protocol :tcp))
277 (deftest socket-open-p-true.2
278 (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
281 (socket-connect s #(127 0 0 1) 7)
285 (deftest socket-open-p-false
286 (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
291 ;;; we don't have an automatic test for some of this yet. There's no
292 ;;; simple way to run servers and have something automatically connect
293 ;;; to them as client, unless we spawn external programs. Then we
294 ;;; have to start telling people what external programs they should
295 ;;; have installed. Which, eventually, we will, but not just yet
298 ;;; to check with this: can display packets from multiple peers
299 ;;; peer address is shown correctly for each packet
300 ;;; packet length is correct
301 ;;; long (>500 byte) packets have the full length shown (doesn't work)
303 (defun udp-server (port)
304 (let ((s (make-instance 'inet-socket :type :datagram :protocol :udp)))
305 (socket-bind s #(0 0 0 0) port)
307 (multiple-value-bind (buf len address port) (socket-receive s nil 500)
308 (format t "Received ~A bytes from ~A:~A - ~A ~%"
309 len address port (subseq buf 0 (min 10 len)))))))
312 (deftest interrupt-io
317 (let ((s (make-instance 'inet-socket
320 (socket-connect s #(127 0 0 1) port)
321 (let ((stream (socket-make-stream s
337 (let ((s (make-instance 'inet-socket
340 (setf (sockopt-reuse-address s) t)
341 (socket-bind s (make-inet-address "127.0.0.1") 0)
343 (multiple-value-bind (* port)
345 (let* ((client (sb-thread:make-thread
346 (lambda () (client port))))
347 (r (socket-accept s))
348 (stream (socket-make-stream r
355 (sb-thread:interrupt-thread client
356 (lambda () (throw 'stop ok)))
359 (write-char #\x stream)
361 (socket-close r))))))