Debian Codegraph @

contrib/sockets/test.lisp

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