]> gitweb.factorcode.org Git - factor.git/blob - basis/io/sockets/secure/unix/unix-tests.factor
a3bfacc8a88cf08d9ba5a2f2c6ba98d7f11817e4
[factor.git] / basis / io / sockets / secure / unix / unix-tests.factor
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.unix.debug ;
7
8 \ <secure-config> must-infer
9 { 1 0 } [ [ ] with-secure-context ] must-infer-as
10
11 [ ] [ <promise> "port" set ] unit-test
12
13 :: server-test ( quot -- )
14     [
15         [
16             "127.0.0.1" 0 <inet4> <secure> ascii <server> [
17                 dup addr>> addrspec>> port>> "port" get fulfill
18                 accept [
19                     quot call
20                 ] curry with-stream
21             ] with-disposal
22         ] with-test-context
23     ] "SSL server test" spawn drop ;
24
25 : client-test ( -- string )
26     <secure-config> [
27         "127.0.0.1" "port" get ?promise <inet4> <secure> ascii <client> drop contents
28     ] with-secure-context ;
29
30 [ ] [ [ class name>> write ] server-test ] unit-test
31
32 [ "secure" ] [ client-test ] unit-test
33
34 ! Now, see what happens if the server closes the connection prematurely
35 [ ] [ <promise> "port" set ] unit-test
36
37 [ ] [
38     [
39         drop
40         "hello" write flush
41         input-stream get stream>> handle>> f >>connected drop
42     ] server-test
43 ] unit-test
44
45 [ client-test ] [ premature-close? ] must-fail-with
46
47 ! Now, try validating the certificate. This should fail because its
48 ! actually an invalid certificate
49 [ ] [ <promise> "port" set ] unit-test
50
51 [ ] [ [ drop "hi" write ] server-test ] unit-test
52
53 [
54     <secure-config> [
55         "localhost" "port" get ?promise <inet> <secure> ascii
56         <client> drop dispose
57     ] with-secure-context
58 ] [ certificate-verify-error? ] must-fail-with
59
60 ! Client-side handshake timeout
61 [ ] [ <promise> "port" set ] unit-test
62
63 [ ] [
64     [
65         "127.0.0.1" 0 <inet4> ascii <server> [
66             dup addr>> port>> "port" get fulfill
67             accept drop 1 minutes sleep dispose
68         ] with-disposal
69     ] "Silly server" spawn drop
70 ] unit-test
71
72 [
73     1 seconds secure-socket-timeout [
74         client-test
75     ] with-variable
76 ] [ io-timeout? ] must-fail-with
77
78 ! Server-side handshake timeout
79 [ ] [ <promise> "port" set ] unit-test
80
81 [ ] [
82     [
83         "127.0.0.1" "port" get ?promise
84         <inet4> ascii <client> drop 1 minutes sleep dispose
85     ] "Silly client" spawn drop
86 ] unit-test
87
88 [
89     1 seconds secure-socket-timeout [
90         [
91             "127.0.0.1" 0 <inet4> <secure> ascii <server> [
92                 dup addr>> addrspec>> port>> "port" get fulfill
93                 accept drop dup stream-read1 drop dispose
94             ] with-disposal
95         ] with-test-context
96     ] with-variable
97 ] [ io-timeout? ] must-fail-with
98
99 ! Client socket shutdown timeout
100
101 ! Until I sort out two-stage handshaking, I can't do much here
102 [
103     [ ] [ <promise> "port" set ] unit-test
104     
105     [ ] [
106         [
107             [
108                 "127.0.0.1" 0 <inet4> <secure> ascii <server> [
109                     dup addr>> addrspec>> port>> "port" get fulfill
110                     accept drop 1 minutes sleep dispose
111                 ] with-disposal
112             ] with-test-context
113         ] "Silly server" spawn drop
114     ] unit-test
115     
116     [
117         1 seconds secure-socket-timeout [
118             <secure-config> [
119                 "127.0.0.1" "port" get ?promise <inet4> <secure>
120                 ascii <client> drop dispose
121             ] with-secure-context
122         ] with-variable
123     ] [ io-timeout? ] must-fail-with
124     
125     ! Server socket shutdown timeout
126     [ ] [ <promise> "port" set ] unit-test
127     
128     [ ] [
129         [
130             [
131                 "127.0.0.1" "port" get ?promise
132                 <inet4> <secure> ascii <client> drop 1 minutes sleep dispose
133             ] with-test-context
134         ] "Silly client" spawn drop
135     ] unit-test
136     
137     [
138         1 seconds secure-socket-timeout [
139             [
140                 "127.0.0.1" 0 <inet4> <secure> ascii <server> [
141                     dup addr>> addrspec>> port>> "port" get fulfill
142                     accept drop dispose
143                 ] with-disposal
144             ] with-test-context
145         ] with-variable
146     ] [ io-timeout? ] must-fail-with
147 ] drop