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
|
;;;; $Id$
;;;; $URL$
;;;; See LICENSE for licensing information.
;;;; Usage: (usoct:run-usocket-tests) or (usoct:do-tests)
(in-package :usocket-test)
(defparameter +non-existing-host+ "1.2.3.4")
(defparameter +unused-local-port+ 15213)
(defparameter *fake-usocket*
(usocket::make-stream-socket :socket :my-socket
:stream :my-stream))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *common-lisp-net*
#.(first (usocket::get-hosts-by-name "common-lisp.net"))))
(defvar *local-ip*)
(defmacro with-caught-conditions ((expect throw) &body body)
`(catch 'caught-error
(handler-case
(handler-bind ((usocket:unsupported
#'(lambda (c)
(declare (ignore c)) (continue))))
(progn ,@body))
(usocket:unknown-error (c) (if (typep c ',expect)
(throw 'caught-error ,throw)
(progn
(describe c)
(describe
(usocket::usocket-real-error c))
c)))
(error (c) (if (typep c ',expect)
(throw 'caught-error ,throw)
(progn
(describe c)
c)))
(usocket:unknown-condition (c) (if (typep c ',expect)
(throw 'caught-error ,throw)
(progn
(describe c)
(describe
(usocket::usocket-real-condition c))
c)))
(condition (c) (if (typep c ',expect)
(throw 'caught-error ,throw)
(progn
(describe c)
c))))))
(deftest make-socket.1 (usocket:socket *fake-usocket*) :my-socket)
(deftest make-socket.2 (usocket:socket-stream *fake-usocket*) :my-stream)
(deftest socket-no-connect.1
(with-caught-conditions (usocket:socket-error nil)
(usocket:socket-connect "127.0.0.0" +unused-local-port+ :timeout 1)
t)
nil)
(deftest socket-no-connect.2
(with-caught-conditions (usocket:socket-error nil)
(usocket:socket-connect #(127 0 0 0) +unused-local-port+ :timeout 1)
t)
nil)
(deftest socket-no-connect.3
(with-caught-conditions (usocket:socket-error nil)
(usocket:socket-connect 2130706432 +unused-local-port+ :timeout 1) ;; == #(127 0 0 0)
t)
nil)
(deftest socket-failure.1
(with-caught-conditions (usocket:timeout-error nil)
(usocket:socket-connect 2130706432 +unused-local-port+ :timeout 1) ;; == #(127 0 0 0)
:unreach)
nil)
(deftest socket-failure.2
(with-caught-conditions (usocket:timeout-error nil)
(usocket:socket-connect +non-existing-host+ 80 :timeout 1) ;; 80 = just a port
:unreach)
nil)
;; let's hope c-l.net doesn't move soon, or that people start to
;; test usocket like crazy..
(deftest socket-connect.1
(with-caught-conditions (nil nil)
(let ((sock (usocket:socket-connect "common-lisp.net" 80)))
(unwind-protect
(when (typep sock 'usocket:usocket) t)
(usocket:socket-close sock))))
t)
(deftest socket-connect.2
(with-caught-conditions (nil nil)
(let ((sock (usocket:socket-connect *common-lisp-net* 80)))
(unwind-protect
(when (typep sock 'usocket:usocket) t)
(usocket:socket-close sock))))
t)
(deftest socket-connect.3
(with-caught-conditions (nil nil)
(let ((sock (usocket:socket-connect (usocket::host-byte-order *common-lisp-net*) 80)))
(unwind-protect
(when (typep sock 'usocket:usocket) t)
(usocket:socket-close sock))))
t)
;; let's hope c-l.net doesn't change its software any time soon
(deftest socket-stream.1
(with-caught-conditions (nil nil)
(let ((sock (usocket:socket-connect "common-lisp.net" 80)))
(unwind-protect
(progn
(format (usocket:socket-stream sock)
"GET / HTTP/1.0~2%")
(force-output (usocket:socket-stream sock))
(subseq (read-line (usocket:socket-stream sock)) 0 15))
(usocket:socket-close sock))))
"HTTP/1.1 200 OK")
(deftest socket-name.1
(with-caught-conditions (nil nil)
(let ((sock (usocket:socket-connect *common-lisp-net* 80)))
(unwind-protect
(usocket::get-peer-address sock)
(usocket:socket-close sock))))
#.*common-lisp-net*)
(deftest socket-name.2
(with-caught-conditions (nil nil)
(let ((sock (usocket:socket-connect *common-lisp-net* 80)))
(unwind-protect
(usocket::get-peer-port sock)
(usocket:socket-close sock))))
80)
(deftest socket-name.3
(with-caught-conditions (nil nil)
(let ((sock (usocket:socket-connect *common-lisp-net* 80)))
(unwind-protect
(usocket::get-peer-name sock)
(usocket:socket-close sock))))
#.*common-lisp-net* 80)
#+ignore
(deftest socket-name.4
(with-caught-conditions (nil nil)
(let ((sock (usocket:socket-connect *common-lisp-net* 80)))
(unwind-protect
(equal (usocket::get-local-address sock) *local-ip*)
(usocket:socket-close sock))))
t)
(deftest socket-shutdown.1
(with-caught-conditions (nil nil)
(let ((sock (usocket:socket-connect *common-lisp-net* 80)))
(unwind-protect
(usocket::ignore-unsupported-warnings
(usocket:socket-shutdown sock :input))
(usocket:socket-close sock))
t))
t)
(deftest socket-shutdown.2
(with-caught-conditions (nil nil)
(let ((sock (usocket:socket-connect *common-lisp-net* 80)))
(unwind-protect
(usocket::ignore-unsupported-warnings
(usocket:socket-shutdown sock :output))
(usocket:socket-close sock))
t))
t)
(defun run-usocket-tests ()
(do-tests))
|