1 IN: io.sockets.secure.tests
2 USING: accessors kernel namespaces io io.sockets
3 io.sockets.secure io.encodings.ascii io.streams.duplex
4 io.unix.backend classes words destructors threads tools.test
5 concurrency.promises byte-arrays locals calendar io.timeouts ;
7 \ <secure-config> must-infer
8 { 1 0 } [ [ ] with-secure-context ] must-infer-as
10 [ ] [ <promise> "port" set ] unit-test
12 : with-test-context ( quot -- )
14 "resource:extra/openssl/test/server.pem" >>key-file
15 "resource:extra/openssl/test/dh1024.pem" >>dh-file
17 swap with-secure-context ; inline
19 :: server-test ( quot -- )
22 "127.0.0.1" 0 <inet4> <secure> ascii <server> [
23 dup addr>> addrspec>> port>> "port" get fulfill
29 ] "SSL server test" spawn drop ;
31 : client-test ( -- string )
33 "127.0.0.1" "port" get ?promise <inet4> <secure> ascii <client> drop contents
34 ] with-secure-context ;
36 [ ] [ [ class name>> write ] server-test ] unit-test
38 [ "secure" ] [ client-test ] unit-test
40 ! Now, see what happens if the server closes the connection prematurely
41 [ ] [ <promise> "port" set ] unit-test
47 input-stream get stream>> handle>> f >>connected drop
51 [ client-test ] [ premature-close? ] must-fail-with
53 ! Now, try validating the certificate. This should fail because its
54 ! actually an invalid certificate
55 [ ] [ <promise> "port" set ] unit-test
57 [ ] [ [ drop "hi" write ] server-test ] unit-test
61 "localhost" "port" get ?promise <inet> <secure> ascii
64 ] [ certificate-verify-error? ] must-fail-with
66 ! Client-side handshake timeout
67 [ ] [ <promise> "port" set ] unit-test
71 "127.0.0.1" 0 <inet4> ascii <server> [
72 dup addr>> port>> "port" get fulfill
73 accept drop 1 minutes sleep dispose
75 ] "Silly server" spawn drop
79 1 seconds secure-socket-timeout [
82 ] [ io-timeout? ] must-fail-with
84 ! Server-side handshake timeout
85 [ ] [ <promise> "port" set ] unit-test
89 "127.0.0.1" "port" get ?promise
90 <inet4> ascii <client> drop 1 minutes sleep dispose
91 ] "Silly client" spawn drop
95 1 seconds secure-socket-timeout [
97 "127.0.0.1" 0 <inet4> <secure> ascii <server> [
98 dup addr>> addrspec>> port>> "port" get fulfill
99 accept drop dup stream-read1 drop dispose
103 ] [ io-timeout? ] must-fail-with
105 ! Client socket shutdown timeout
107 ! Until I sort out two-stage handshaking, I can't do much here
109 [ ] [ <promise> "port" set ] unit-test
114 "127.0.0.1" 0 <inet4> <secure> ascii <server> [
115 dup addr>> addrspec>> port>> "port" get fulfill
116 accept drop 1 minutes sleep dispose
119 ] "Silly server" spawn drop
123 1 seconds secure-socket-timeout [
125 "127.0.0.1" "port" get ?promise <inet4> <secure>
126 ascii <client> drop dispose
127 ] with-secure-context
129 ] [ io-timeout? ] must-fail-with
131 ! Server socket shutdown timeout
132 [ ] [ <promise> "port" set ] unit-test
137 "127.0.0.1" "port" get ?promise
138 <inet4> <secure> ascii <client> drop 1 minutes sleep dispose
140 ] "Silly client" spawn drop
144 1 seconds secure-socket-timeout [
146 "127.0.0.1" 0 <inet4> <secure> ascii <server> [
147 dup addr>> addrspec>> port>> "port" get fulfill
152 ] [ io-timeout? ] must-fail-with