7ce9d39ae5e717e4ac355864ab4fc84a2a202cac
[sbcl.git] / contrib / sb-bsd-sockets / tests.lisp
1 (defpackage "SB-BSD-SOCKETS-TEST"
2   (:use "CL" "SB-BSD-SOCKETS" "SB-RT"))
3
4 (in-package :sb-bsd-sockets-test)
5
6 (defmacro deftest* ((name &key fails-on) form &rest results)
7   `(progn
8      (when (sb-impl::featurep ',fails-on)
9        (pushnew ',name sb-rt::*expected-failures*))
10      (deftest ,name ,form ,@results)))
11
12 ;;; a real address
13 (deftest make-inet-address
14   (equalp (make-inet-address "127.0.0.1")  #(127 0 0 1))
15   t)
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))
19   t)
20
21 (deftest get-protocol-by-name/tcp
22     (integerp (get-protocol-by-name "tcp"))
23   t)
24
25 (deftest get-protocol-by-name/udp
26   (integerp (get-protocol-by-name "udp"))
27   t)
28
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")
35     (unknown-protocol ()
36       t)
37     (:no-error ()
38       nil))
39   t)
40
41 (deftest make-inet-socket
42   ;; make a socket
43   (let ((s (make-instance 'inet-socket :type :stream :protocol (get-protocol-by-name "tcp"))))
44     (and (> (socket-file-descriptor s) 1) t))
45   t)
46
47 (deftest make-inet-socket-keyword
48     ;; make a socket
49     (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
50       (and (> (socket-file-descriptor s) 1) t))
51   t)
52
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
56     (handler-case
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)
64           (c)
65         (declare (ignorable c)) t)
66       (:no-error nil))
67   t)
68
69 (deftest* (make-inet-socket-keyword-wrong)
70     ;; same again with keywords
71     (handler-case
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.
76       ((or
77         #+darwin socket-error
78         protocol-not-supported-error
79         socket-type-not-supported-error)
80           (c)
81         (declare (ignorable c)) t)
82       (:no-error nil))
83   t)
84
85
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))
90   t)
91
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)))
97     (unwind-protect
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
101          (progn
102            (socket-bind s1 address 0)
103            (handler-case
104                (let ((port (nth-value 1 (socket-name s1))))
105                  (socket-bind s2 address port)
106                  nil)
107              (address-in-use-error () t)))
108       (socket-close s1)
109       (socket-close s2)))
110   t)
111
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))
118   t)
119
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)))
123     (do ((i 0 (1+ i))
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))))
128
129 #+internet-available
130 (deftest name-service-return-type
131   (vectorp (host-ent-address (get-host-by-address #(127 0 0 1))))
132   t)
133
134 ;;; these require that the echo services are turned on in inetd
135 #+internet-available
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))))
145   t)
146
147 #+internet-available
148 (deftest sockaddr-return-type
149   (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
150     (unwind-protect
151          (progn
152            (socket-connect s #(127 0 0 1) 7)
153            (multiple-value-bind (host port) (socket-peername s)
154              (and (vectorp host)
155                   (numberp port))))
156       (socket-close s)))
157   t)
158
159 #+internet-available
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))))
170   t)
171
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
176
177 #-win32
178 (deftest simple-local-client
179     (progn
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
184       ;; device.
185       (when (block nil
186               (handler-bind ((sb-posix:syscall-error
187                               (lambda (e)
188                                 (declare (ignore e))
189                                 (return nil))))
190                 (sb-posix:s-issock
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)
194           (finish-output)
195           (handler-case
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)
200               (finish-output)
201               (socket-connect s "/dev/log")))
202           (format t "ok.~%")
203           (let ((stream (socket-make-stream s :input t :output t :buffering :none)))
204             (format stream
205                     "<7>bsd-sockets: Don't panic.  We're testing local-domain client code; this message can safely be ignored"))))
206       t)
207   t)
208
209
210 ;;; these require that the internet (or bits of it, at least) is available
211
212 #+internet-available
213 (deftest get-host-by-name
214   (equalp (car (host-ent-addresses (get-host-by-name "a.root-servers.net")))
215           #(198 41 0 4))
216   t)
217
218 #+internet-available
219 (deftest get-host-by-address
220     (host-ent-name (get-host-by-address #(198 41 0 4)))
221   "a.root-servers.net")
222
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.
226 #+nil
227 (deftest get-host-by-name-wrong
228   (handler-case
229    (get-host-by-name "foo.tninkpad.telent.net.")
230    (NAME-SERVICE-ERROR () t)
231    (:no-error nil))
232   t)
233
234 (defun http-stream (host port request)
235   (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
236     (socket-connect
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))
240     s))
241
242 #+internet-available
243 (deftest simple-http-client-1
244     (handler-case
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))))
250             (princ data)
251             (> (length data) 0)))
252       (network-unreachable-error () 'network-unreachable))
253   t)
254
255
256 #+internet-available
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 =
261     (handler-case
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))
271   t)
272
273 (deftest socket-open-p-true.1
274     (socket-open-p (make-instance 'inet-socket :type :stream :protocol :tcp))
275   t)
276 #+internet-available
277 (deftest socket-open-p-true.2
278     (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
279       (unwind-protect
280            (progn
281              (socket-connect s #(127 0 0 1) 7)
282              (socket-open-p s))
283         (socket-close s)))
284   t)
285 (deftest socket-open-p-false
286     (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
287       (socket-close s)
288       (socket-open-p s))
289   nil)
290
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
296
297
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)
302
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)
306     (loop
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)))))))
310
311 #+sb-thread
312 (deftest interrupt-io
313     (let (result)
314       (labels
315           ((client (port)
316              (setf result
317                    (let ((s (make-instance 'inet-socket
318                                            :type :stream
319                                            :protocol :tcp)))
320                      (socket-connect s #(127 0 0 1) port)
321                      (let ((stream (socket-make-stream s
322                                                        :input t
323                                                        :output t
324                                                        :buffering :none)))
325                        (handler-case
326                            (prog1
327                                (catch 'stop
328                                  (progn
329                                    (read-char stream)
330                                    (sleep 0.1)
331                                    (sleep 0.1)
332                                    (sleep 0.1)))
333                              (close stream))
334                          (error (c)
335                            c))))))
336            (server ()
337              (let ((s (make-instance 'inet-socket
338                                      :type :stream
339                                      :protocol :tcp)))
340                (setf (sockopt-reuse-address s) t)
341                (socket-bind s (make-inet-address "127.0.0.1") 0)
342                (socket-listen s 5)
343                (multiple-value-bind (* port)
344                    (socket-name s)
345                  (let* ((client (sb-thread:make-thread
346                                  (lambda () (client port))))
347                         (r (socket-accept s))
348                         (stream (socket-make-stream r
349                                                     :input t
350                                                     :output t
351                                                     :buffering :none))
352                         (ok :ok))
353                    (socket-close s)
354                    (sleep 5)
355                    (sb-thread:interrupt-thread client
356                                                (lambda () (throw 'stop ok)))
357                    (sleep 5)
358                    (setf ok :not-ok)
359                    (write-char #\x stream)
360                    (close stream)
361                    (socket-close r))))))
362         (server))
363       result)
364   :ok)