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