6f2201736b848271f272779889452bfe3e68ff95
[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 ;;; a real address
7 (deftest make-inet-address
8   (equalp (make-inet-address "127.0.0.1")  #(127 0 0 1))
9   t)
10 ;;; and an address with bit 8 set on some octets
11 (deftest make-inet-address2
12   (equalp (make-inet-address "242.1.211.3")  #(242 1 211 3))
13   t)
14
15 (deftest get-protocol-by-name/tcp
16     (integerp (get-protocol-by-name "tcp"))
17   t)
18
19 (deftest get-protocol-by-name/udp
20   (integerp (get-protocol-by-name "udp"))
21   t)
22
23 ;;; See https://bugs.launchpad.net/sbcl/+bug/659857
24 ;;; Apparently getprotobyname_r on FreeBSD says -1 and EINTR
25 ;;; for unknown protocols...
26 #-(and freebsd sb-thread)
27 (deftest get-protocol-by-name/error
28   (handler-case (get-protocol-by-name "nonexistent-protocol")
29     (unknown-protocol ()
30       t)
31     (:no-error ()
32       nil))
33   t)
34
35 (deftest make-inet-socket
36   ;; make a socket
37   (let ((s (make-instance 'inet-socket :type :stream :protocol (get-protocol-by-name "tcp"))))
38     (and (> (socket-file-descriptor s) 1) t))
39   t)
40
41 (deftest make-inet-socket-keyword
42     ;; make a socket
43     (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
44       (and (> (socket-file-descriptor s) 1) t))
45   t)
46
47 (deftest make-inet-socket-wrong
48     ;; fail to make a socket: check correct error return.  There's no nice
49     ;; way to check the condition stuff on its own, which is a shame
50     (handler-case
51         (make-instance 'inet-socket :type :stream :protocol (get-protocol-by-name "udp"))
52       ;; CLH FIXME! some versions of darwin just return a socket error
53       ;; here, not socket-type-not-supported-error or
54       ;; protocol-not-supported-error.
55       ((or #+darwin socket-error
56         socket-type-not-supported-error
57         protocol-not-supported-error)
58           (c)
59         (declare (ignorable c)) t)
60       (:no-error nil))
61   t)
62
63 (deftest make-inet-socket-keyword-wrong
64     ;; same again with keywords
65     (handler-case
66         (make-instance 'inet-socket :type :stream :protocol :udp)
67       ;; CLH FIXME! some versions of darwin just return a socket error
68       ;; here, not socket-type-not-supported-error or
69       ;; protocol-not-supported-error.
70       ((or
71         #+darwin socket-error
72         protocol-not-supported-error
73         socket-type-not-supported-error)
74           (c)
75         (declare (ignorable c)) t)
76       (:no-error nil))
77   t)
78
79
80 (deftest non-block-socket
81   (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
82     (setf (non-blocking-mode s) t)
83     (non-blocking-mode s))
84   t)
85
86 (defun do-gc-portably ()
87   ;; cmucl on linux has generational gc with a keyword argument,
88   ;; sbcl GC function takes same arguments no matter what collector is in
89   ;; use
90   #+(or sbcl gencgc) (SB-EXT:gc :full t)
91   ;; other platforms have full gc or nothing
92   #-(or sbcl gencgc) (sb-ext:gc))
93
94 (deftest inet-socket-bind
95   (let ((s (make-instance 'inet-socket :type :stream :protocol (get-protocol-by-name "tcp"))))
96     ;; Given the functions we've got so far, if you can think of a
97     ;; better way to make sure the bind succeeded than trying it
98     ;; twice, let me know
99     ;; 1974 has no special significance, unless you're the same age as me
100     (do-gc-portably) ;gc should clear out any old sockets bound to this port
101     (socket-bind s (make-inet-address "127.0.0.1") 1974)
102     (handler-case
103         (let ((s2 (make-instance 'inet-socket :type :stream :protocol (get-protocol-by-name "tcp"))))
104           (socket-bind s2 (make-inet-address "127.0.0.1") 1974)
105           nil)
106       (address-in-use-error () t)))
107   t)
108
109 (deftest simple-sockopt-test
110   ;; test we can set SO_REUSEADDR on a socket and retrieve it, and in
111   ;; the process that all the weird macros in sockopt happened right.
112   (let ((s (make-instance 'inet-socket :type :stream :protocol (get-protocol-by-name "tcp"))))
113     (setf (sockopt-reuse-address s) t)
114     (sockopt-reuse-address s))
115   t)
116
117 (defun read-buf-nonblock (buffer stream)
118   "Like READ-SEQUENCE, but returns early if the full quantity of data isn't there to be read.  Blocks if no input at all"
119   (let ((eof (gensym)))
120     (do ((i 0 (1+ i))
121          (c (read-char stream nil eof)
122             (read-char-no-hang stream nil eof)))
123         ((or (>= i (length buffer)) (not c) (eq c eof)) i)
124       (setf (elt buffer i) c))))
125
126 #+internet-available
127 (deftest name-service-return-type
128   (vectorp (host-ent-address (get-host-by-address #(127 0 0 1))))
129   t)
130
131 ;;; these require that the echo services are turned on in inetd
132 #+internet-available
133 (deftest simple-tcp-client
134     (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp))
135           (data (make-string 200)))
136       (socket-connect s #(127 0 0 1) 7)
137       (let ((stream (socket-make-stream s :input t :output t :buffering :none)))
138         (format stream "here is some text")
139         (let ((data (subseq data 0 (read-buf-nonblock data stream))))
140           (format t "~&Got ~S back from TCP echo server~%" data)
141           (> (length data) 0))))
142   t)
143
144 #+internet-available
145 (deftest sockaddr-return-type
146   (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
147     (unwind-protect
148          (progn
149            (socket-connect s #(127 0 0 1) 7)
150            (multiple-value-bind (host port) (socket-peername s)
151              (and (vectorp host)
152                   (numberp port))))
153       (socket-close s)))
154   t)
155
156 #+internet-available
157 (deftest simple-udp-client
158   (let ((s (make-instance 'inet-socket :type :datagram :protocol (get-protocol-by-name "udp")))
159         (data (make-string 200)))
160     (format t "Socket type is ~A~%" (sockopt-type s))
161     (socket-connect s #(127 0 0 1) 7)
162     (let ((stream (socket-make-stream s :input t :output t :buffering :none)))
163       (format stream "here is some text")
164       (let ((data (subseq data 0 (read-buf-nonblock data stream))))
165         (format t "~&Got ~S back from UDP echo server~%" data)
166         (> (length data) 0))))
167   t)
168
169 ;;; A fairly rudimentary test that connects to the syslog socket and
170 ;;; sends a message.  Priority 7 is kern.debug; you'll probably want
171 ;;; to look at /etc/syslog.conf or local equivalent to find out where
172 ;;; the message ended up
173
174 (deftest simple-local-client
175     #-win32
176     (progn
177       ;; SunOS (Solaris) and Darwin systems don't have a socket at
178       ;; /dev/log.  We might also be building in a chroot or
179       ;; something, so don't fail this test just because the file is
180       ;; unavailable, or if it's a symlink to some weird character
181       ;; device.
182       (when (block nil
183               (handler-bind ((sb-posix:syscall-error
184                               (lambda (e)
185                                 (declare (ignore e))
186                                 (return nil))))
187                 (sb-posix:s-issock
188                  (sb-posix::stat-mode (sb-posix:stat "/dev/log")))))
189         (let ((s (make-instance 'local-socket :type :datagram)))
190           (format t "Connecting ~A... " s)
191           (finish-output)
192           (handler-case
193               (socket-connect s "/dev/log")
194             (sb-bsd-sockets::socket-error ()
195               (setq s (make-instance 'local-socket :type :stream))
196               (format t "failed~%Retrying with ~A... " s)
197               (finish-output)
198               (socket-connect s "/dev/log")))
199           (format t "ok.~%")
200           (let ((stream (socket-make-stream s :input t :output t :buffering :none)))
201             (format stream
202                     "<7>bsd-sockets: Don't panic.  We're testing local-domain client code; this message can safely be ignored"))))
203       t)
204   t)
205
206
207 ;;; these require that the internet (or bits of it, at least) is available
208
209 #+internet-available
210 (deftest get-host-by-name
211   (equalp (car (host-ent-addresses (get-host-by-name "a.root-servers.net")))
212           #(198 41 0 4))
213   t)
214
215 #+internet-available
216 (deftest get-host-by-address
217     (host-ent-name (get-host-by-address #(198 41 0 4)))
218   "a.root-servers.net")
219
220 ;;; These days lots of people seem to be using DNS servers that don't
221 ;;; report resolving failures for non-existing domains. This test
222 ;;; will fail there, so we've disabled it.
223 #+nil
224 (deftest get-host-by-name-wrong
225   (handler-case
226    (get-host-by-name "foo.tninkpad.telent.net.")
227    (NAME-SERVICE-ERROR () t)
228    (:no-error nil))
229   t)
230
231 (defun http-stream (host port request)
232   (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
233     (socket-connect
234      s (car (host-ent-addresses (get-host-by-name host))) port)
235     (let ((stream (socket-make-stream s :input t :output t :buffering :none)))
236       (format stream "~A HTTP/1.0~%~%" request))
237     s))
238
239 #+internet-available
240 (deftest simple-http-client-1
241     (handler-case
242         (let ((s (http-stream "ww.telent.net" 80 "HEAD /")))
243           (let ((data (make-string 200)))
244             (setf data (subseq data 0
245                                (read-buf-nonblock data
246                                                   (socket-make-stream s))))
247             (princ data)
248             (> (length data) 0)))
249       (network-unreachable-error () 'network-unreachable))
250   t)
251
252
253 #+internet-available
254 (deftest sockopt-receive-buffer
255     ;; on Linux x86, the receive buffer size appears to be doubled in the
256     ;; kernel: we set a size of x and then getsockopt() returns 2x.
257     ;; This is why we compare with >= instead of =
258     (handler-case
259         (let ((s (http-stream "ww.telent.net" 80 "HEAD /")))
260           (setf (sockopt-receive-buffer s) 1975)
261           (let ((data (make-string 200)))
262             (setf data (subseq data 0
263                                (read-buf-nonblock data
264                                                   (socket-make-stream s))))
265             (and (> (length data) 0)
266                  (>= (sockopt-receive-buffer s) 1975))))
267       (network-unreachable-error () 'network-unreachable))
268   t)
269
270 (deftest socket-open-p-true.1
271     (socket-open-p (make-instance 'inet-socket :type :stream :protocol :tcp))
272   t)
273 #+internet-available
274 (deftest socket-open-p-true.2
275     (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
276       (unwind-protect
277            (progn
278              (socket-connect s #(127 0 0 1) 7)
279              (socket-open-p s))
280         (socket-close s)))
281   t)
282 (deftest socket-open-p-false
283     (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
284       (socket-close s)
285       (socket-open-p s))
286   nil)
287
288 ;;; we don't have an automatic test for some of this yet.  There's no
289 ;;; simple way to run servers and have something automatically connect
290 ;;; to them as client, unless we spawn external programs.  Then we
291 ;;; have to start telling people what external programs they should
292 ;;; have installed.  Which, eventually, we will, but not just yet
293
294
295 ;;; to check with this: can display packets from multiple peers
296 ;;; peer address is shown correctly for each packet
297 ;;; packet length is correct
298 ;;; long (>500 byte) packets have the full length shown (doesn't work)
299
300 (defun udp-server (port)
301   (let ((s (make-instance 'inet-socket :type :datagram :protocol :udp)))
302     (socket-bind s #(0 0 0 0) port)
303     (loop
304      (multiple-value-bind (buf len address port) (socket-receive s nil 500)
305        (format t "Received ~A bytes from ~A:~A - ~A ~%"
306                len address port (subseq buf 0 (min 10 len)))))))
307
308