summaryrefslogtreecommitdiff
path: root/test.lisp
blob: 46411c9b45c35d7d30d864b0b97ec1bda2cceb78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
;;; Copyright (C) 2008  David Lichteblau
;;; See LICENSE for details.

#|
(load "test.lisp")
|#

(defpackage :ssl-test
  (:use :cl))
(in-package :ssl-test)

(defvar *port* 8080)
(defvar *cert* "/home/david/newcert.pem")
(defvar *key* "/home/david/newkey.pem")

(eval-when (:compile-toplevel :load-toplevel :execute)
  (asdf:operate 'asdf:load-op :trivial-sockets)
  (asdf:operate 'asdf:load-op :bordeaux-threads))

(defparameter *tests* '())

(defvar *sockets* '())
(defvar *sockets-lock* (bordeaux-threads:make-lock))

(defun record-socket (socket)
  (unless (integerp socket)
    (bordeaux-threads:with-lock-held (*sockets-lock*)
      (push socket *sockets*)))
  socket)

(defun close-socket (socket &key abort)
  (if (streamp socket)
      (close socket :abort abort)
      (trivial-sockets:close-server socket)))

(defun check-sockets ()
  (let ((failures nil))
    (bordeaux-threads:with-lock-held (*sockets-lock*)
      (dolist (socket *sockets*)
  (when (close-socket socket :abort t)
    (push socket failures)))
      (setf *sockets* nil))
    #-sbcl        ;fixme
    (when failures
      (error "failed to close sockets properly:~{  ~A~%~}" failures))))

(defmacro deftest (name &body body)
  `(progn
     (defun ,name ()
       (format t "~%----- ~A ----------------------------~%" ',name)
       (handler-case
     (progn
       ,@body
       (check-sockets)
       (format t "===== [OK] ~A ====================~%" ',name)
       t)
   (error (c)
     (when (typep c 'trivial-sockets:socket-error)
       (setf c (trivial-sockets:socket-nested-error c)))
     (format t "~%===== [FAIL] ~A: ~A~%" ',name c)
     (handler-case
         (check-sockets)
       (error (c)
         (format t "muffling follow-up error ~A~%" c)))
     nil)))
     (push ',name *tests*)))

(defun run-all-tests ()
  (unless (probe-file *cert*) (error "~A not found" *cert*))
  (unless (probe-file *key*) (error "~A not found" *key*))
  (let ((n 0)
  (nok 0))
    (dolist (test (reverse *tests*))
      (when (funcall test)
  (incf nok))
      (incf n))
    (format t "~&passed ~D/~D tests~%" nok n)))

(define-condition quit (condition)
  ())

(defparameter *please-quit* t)

(defun make-test-thread (name init main &rest args)
  "Start a thread named NAME, wait until it has funcalled INIT with ARGS
   as arguments, then continue while the thread concurrently funcalls MAIN
   with INIT's return values as arguments."
  (let ((cv (bordeaux-threads:make-condition-variable))
  (lock (bordeaux-threads:make-lock name))
  ;; redirect io manually, because swan's global redirection isn't as
  ;; global as one might hope
  (out *terminal-io*)
  (init-ok nil))
    (bordeaux-threads:with-lock-held (lock)
      (setf *please-quit* nil)
      (prog1
    (bordeaux-threads:make-thread
     (lambda ()
       (flet ((notify ()
          (bordeaux-threads:with-lock-held (lock)
      (bordeaux-threads:condition-notify cv))))
         (let ((*terminal-io* out)
         (*standard-output* out)
         (*trace-output* out)
         (*error-output* out))
     (handler-case
         (let ((values (multiple-value-list (apply init args))))
           (setf init-ok t)
           (notify)
           (apply main values))
       (quit ()
         (notify)
         t)
       (error (c)
         (when (typep c 'trivial-sockets:socket-error)
           (setf c (trivial-sockets:socket-nested-error c)))
         (format t "aborting test thread ~A: ~A" name c)
         (notify)
         nil)))))
     :name name)
  (bordeaux-threads:condition-wait cv lock)
  (unless init-ok
    (error "failed to start background thread"))))))

(defmacro with-thread ((name init main &rest args) &body body)
  `(invoke-with-thread (lambda () ,@body)
           ,name
           ,init
           ,main
           ,@args))

(defun invoke-with-thread (body name init main &rest args)
  (let ((thread (apply #'make-test-thread name init main args)))
    (unwind-protect
   (funcall body)
      (setf *please-quit* t)
      (loop
   for delay = 0.0001 then (* delay 2)
   while (and (< delay 0.5) (bordeaux-threads:thread-alive-p thread))
   do
     (sleep delay))
      (when (bordeaux-threads:thread-alive-p thread)
  (format t "~&thread doesn't want to quit, killing it~%")
  (force-output)
  (bordeaux-threads:interrupt-thread thread (lambda () (error 'quit)))
  (loop
     for delay = 0.0001 then (* delay 2)
     while (bordeaux-threads:thread-alive-p thread)
     do
     (sleep delay))))))

(defun init-server (&key (unwrap-stream-p t))
  (format t "~&SSL server listening on port ~d~%" *port*)
  (values (record-socket (trivial-sockets:open-server :port *port*))
    unwrap-stream-p))

(defun test-server (listening-socket unwrap-stream-p)
  (format t "~&SSL server accepting...~%")
  (unwind-protect
       (let* ((socket (record-socket
           (trivial-sockets:accept-connection
      listening-socket
      :element-type '(unsigned-byte 8))))
        (callback nil))
   (when (eq unwrap-stream-p :caller)
     (setf callback (let ((s socket)) (lambda () (close-socket s))))
     (setf socket (cl+ssl:stream-fd socket))
     (setf unwrap-stream-p nil))
   (let ((client (record-socket
      (cl+ssl:make-ssl-server-stream
       socket
       :unwrap-stream-p unwrap-stream-p
       :close-callback callback
       :external-format :iso-8859-1
       :certificate *cert*
       :key *key*))))
     (unwind-protect
    (loop
       for line = (prog2
          (when *please-quit* (return))
          (read-line client nil)
        (when *please-quit* (return)))
       while line
       do
         (cond
           ((equal line "freeze")
      (format t "~&Freezing on client request~%")
      (loop
         (sleep 1)
         (when *please-quit* (return))))
           (t
      (format t "~&Responding to query ~A...~%" line)
      (format client "(echo ~A)~%" line)
      (force-output client))))
       (close-socket client))))
    (close-socket listening-socket)))

(defun init-client (&key (unwrap-stream-p t))
  (let ((socket (record-socket
     (trivial-sockets:open-stream
      "127.0.0.1"
      *port*
      :element-type '(unsigned-byte 8))))
  (callback nil))
    (when (eq unwrap-stream-p :caller)
      (setf callback (let ((s socket)) (lambda () (close-socket s))))
      (setf socket (cl+ssl:stream-fd socket))
      (setf unwrap-stream-p nil))
    (cl+ssl:make-ssl-client-stream
     socket
     :unwrap-stream-p unwrap-stream-p
     :close-callback callback
     :external-format :iso-8859-1)))

;; CCL requires specifying the
;; deadline at the socket cration (
;; in constrast to SBCL which has
;; the WITH-TIMEOUT macro).
;;
;; Therefore a separate INIT-CLIENT
;; function is needed for CCL when
;; we need read/write deadlines on
;; the SSL client stream.
#+clozure-common-lisp
(defun ccl-init-client-with-deadline (&key (unwrap-stream-p t)
              seconds)
  (let* ((deadline
    (+ (get-internal-real-time)
       (* seconds internal-time-units-per-second)))
   (low
    (record-socket
     (ccl:make-socket
      :address-family :internet
      :connect :active
      :type :stream
      :remote-host "127.0.0.1"
      :remote-port *port*
      :deadline deadline))))
    (cl+ssl:make-ssl-client-stream
     low
     :unwrap-stream-p unwrap-stream-p
     :external-format :iso-8859-1)))

;;; Simple echo-server test.  Write a line and check that the result
;;; watches, three times in a row.
(deftest echo
  (with-thread ("simple server" #'init-server #'test-server)
    (with-open-stream (socket (init-client))
      (write-line "test" socket)
      (force-output socket)
      (assert (equal (read-line socket) "(echo test)"))
      (write-line "test2" socket)
      (force-output socket)
      (assert (equal (read-line socket) "(echo test2)"))
      (write-line "test3" socket)
      (force-output socket)
      (assert (equal (read-line socket) "(echo test3)")))))

;;; Run tests with different BIO setup strategies:
;;;   - :UNWRAP-STREAMS T
;;;     In this case, CL+SSL will convert the socket to a file descriptor.
;;;   - :UNWRAP-STREAMS :CLIENT
;;;     Convert the socket to a file descriptor manually, and give that
;;;     to CL+SSL.
;;;   - :UNWRAP-STREAMS NIL
;;;     Let CL+SSL write to the stream directly, using the Lisp BIO.
(macrolet ((deftests (name (var &rest values) &body body)
       `(progn
    ,@(loop
         for value in values
         collect
         `(deftest ,(intern (format nil "~A-~A" name value))
      (let ((,var ',value))
        ,@body))))))

  (deftests unwrap-strategy (usp nil t :caller)
    (with-thread ("echo server for strategy test"
      (lambda () (init-server :unwrap-stream-p usp))
      #'test-server)
      (with-open-stream (socket (init-client :unwrap-stream-p usp))
  (write-line "test" socket)
  (force-output socket)
  (assert (equal (read-line socket) "(echo test)")))))

  #+clozure-common-lisp
  (deftests read-deadline (usp nil t :caller)
    (with-thread ("echo server for deadline test"
      (lambda () (init-server :unwrap-stream-p usp))
      #'test-server)
      (with-open-stream
    (socket
     (ccl-init-client-with-deadline
      :unwrap-stream-p usp
      :seconds 3))
  (write-line "test" socket)
  (force-output socket)
  (assert (equal (read-line socket) "(echo test)"))
  (handler-case
      (progn
        (read-char socket)
        (error "unexpected data"))
    (ccl::communication-deadline-expired ())))))

  #+sbcl
  (deftests read-deadline (usp nil t :caller)
    (with-thread ("echo server for deadline test"
      (lambda () (init-server :unwrap-stream-p usp))
      #'test-server)
      (sb-sys:with-deadline (:seconds 3)
  (with-open-stream (socket (init-client :unwrap-stream-p usp))
    (write-line "test" socket)
    (force-output socket)
    (assert (equal (read-line socket) "(echo test)"))
    (handler-case
        (progn
    (read-char socket)
    (error "unexpected data"))
      (sb-sys:deadline-timeout ()))))))

  #+clozure-common-lisp
  (deftests write-deadline (usp nil t)
    (with-thread ("echo server for deadline test"
      (lambda () (init-server :unwrap-stream-p usp))
      #'test-server)
      (with-open-stream
    (socket
     (ccl-init-client-with-deadline
      :unwrap-stream-p usp
      :seconds 3))
      (unwind-protect
     (progn
       (write-line "test" socket)
       (force-output socket)
       (assert (equal (read-line socket) "(echo test)"))
       (write-line "freeze" socket)
       (force-output socket)
       (let ((n 0))
         (handler-case
       (loop
          (write-line "deadbeef" socket)
          (incf n))
     (ccl::communication-deadline-expired ()))
         ;; should have written a couple of lines before the deadline:
         (assert (> n 100))))
  (handler-case
      (close-socket socket :abort t)
    (ccl::communication-deadline-expired ()))))))

  #+sbcl
  (deftests write-deadline (usp nil t)
    (with-thread ("echo server for deadline test"
      (lambda () (init-server :unwrap-stream-p usp))
      #'test-server)
      (with-open-stream (socket (init-client :unwrap-stream-p usp))
  (unwind-protect
       (sb-sys:with-deadline (:seconds 3)
         (write-line "test" socket)
         (force-output socket)
         (assert (equal (read-line socket) "(echo test)"))
         (write-line "freeze" socket)
         (force-output socket)
         (let ((n 0))
     (handler-case
         (loop
      (write-line "deadbeef" socket)
      (incf n))
       (sb-sys:deadline-timeout ()))
     ;; should have written a couple of lines before the deadline:
     (assert (> n 100))))
    (handler-case
        (close-socket socket :abort t)
      (sb-sys:deadline-timeout ()))))))

  #+clozure-common-lisp
  (deftests read-char-no-hang/test (usp nil t :caller)
    (with-thread ("echo server for read-char-no-hang test"
      (lambda () (init-server :unwrap-stream-p usp))
      #'test-server)
      (with-open-stream
    (socket (ccl-init-client-with-deadline
       :unwrap-stream-p usp
       :seconds 3))
  (write-line "test" socket)
  (force-output socket)
  (assert (equal (read-line socket) "(echo test)"))
  (handler-case
      (when (read-char-no-hang socket)
        (error "unexpected data"))
    (ccl::communication-deadline-expired ()
      (error "read-char-no-hang hangs"))))))

  #+sbcl
  (deftests read-char-no-hang/test (usp nil t :caller)
    (with-thread ("echo server for read-char-no-hang test"
      (lambda () (init-server :unwrap-stream-p usp))
      #'test-server)
      (sb-sys:with-deadline (:seconds 3)
  (with-open-stream (socket (init-client :unwrap-stream-p usp))
    (write-line "test" socket)
    (force-output socket)
    (assert (equal (read-line socket) "(echo test)"))
    (handler-case
        (when (read-char-no-hang socket)
    (error "unexpected data"))
      (sb-sys:deadline-timeout ()
        (error "read-char-no-hang hangs"))))))))

#+(or)
(run-all-tests)