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