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