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.backend.unix classes words destructors threads tools.test
5 concurrency.promises byte-arrays locals calendar io.timeouts
6 io.sockets.secure.debug ;
8 { 1 0 } [ [ ] with-secure-context ] must-infer-as
10 { } [ <promise> "port" set ] unit-test
12 :: server-test ( quot -- )
15 "127.0.0.1" 0 <inet4> <secure> ascii <server> [
16 dup addr>> addrspec>> port>> "port" get fulfill
22 ] "SSL server test" spawn drop ;
24 : client-test ( -- string )
26 "127.0.0.1" "port" get ?promise <inet4> <secure> ascii <client> drop stream-contents
27 ] with-secure-context ;
29 { } [ [ class-of name>> write ] server-test ] unit-test
31 { "secure" } [ client-test ] unit-test
33 ! Now, see what happens if the server closes the connection prematurely
34 { } [ <promise> "port" set ] unit-test
40 input-stream get stream>> handle>> f >>connected drop
44 ! Actually, this should not be an error since many HTTPS servers
45 ! (eg, google.com) do this.
47 ! [ client-test ] [ premature-close? ] must-fail-with
48 { "hello" } [ client-test ] unit-test
50 ! Now, try validating the certificate. This should fail because its
51 ! actually an invalid certificate
52 { } [ <promise> "port" set ] unit-test
54 { } [ [ drop "hi" write ] server-test ] unit-test
58 "localhost" "port" get ?promise <inet> <secure> ascii
61 ] [ certificate-verify-error? ] must-fail-with
63 ! Client-side handshake timeout
64 { } [ <promise> "port" set ] unit-test
69 "127.0.0.1" 0 <inet4> ascii <server> &dispose
70 dup addr>> port>> "port" get fulfill
71 accept drop &dispose 1 minutes sleep
73 ] "Silly server" spawn drop
77 1 seconds secure-socket-timeout [
80 ] [ io-timeout? ] must-fail-with
82 ! Server-side handshake timeout
83 { } [ <promise> "port" set ] unit-test
88 "127.0.0.1" "port" get ?promise
89 <inet4> ascii <client> drop &dispose 1 minutes sleep
91 ] "Silly client" spawn drop
95 1 seconds secure-socket-timeout [
98 "127.0.0.1" 0 <inet4> <secure> ascii <server> [
99 dup addr>> addrspec>> port>> "port" get fulfill
100 accept drop &dispose dup stream-read1 drop
105 ] [ io-timeout? ] must-fail-with
107 ! Client socket shutdown timeout
109 ! Until I sort out two-stage handshaking, I can't do much here
111 [ ] [ <promise> "port" set ] unit-test
117 "127.0.0.1" 0 <inet4> <secure> ascii <server> [
118 dup addr>> addrspec>> port>> "port" get fulfill
119 accept drop &dispose 1 minutes sleep
123 ] "Silly server" spawn drop
127 1 seconds secure-socket-timeout [
129 "127.0.0.1" "port" get ?promise <inet4> <secure>
130 ascii <client> drop dispose
131 ] with-secure-context
133 ] [ io-timeout? ] must-fail-with
135 ! Server socket shutdown timeout
136 [ ] [ <promise> "port" set ] unit-test
142 "127.0.0.1" "port" get ?promise
143 <inet4> <secure> ascii <client> drop &dispose 1 minutes sleep
146 ] "Silly client" spawn drop
151 1 seconds secure-socket-timeout [
153 "127.0.0.1" 0 <inet4> <secure> ascii <server> [
154 dup addr>> addrspec>> port>> "port" get fulfill
160 ] [ io-timeout? ] must-fail-with