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