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