OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
Søren Gjesse
2014/11/10 11:38:00
2014
kustermann
2014/11/10 12:30:21
Done.
| |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // | |
5 // This test tests TLS session resume, by making multiple client connections | |
Søren Gjesse
2014/11/10 11:38:00
This description does not fit with what is being t
kustermann
2014/11/10 12:30:21
Done.
| |
6 // on the same port to the same server, with a delay of 200 ms between them. | |
7 // The unmodified secure_server_test creates all sessions simultaneously, | |
8 // which means that no handshake completes and caches its keys in the session | |
9 // cache in time for other connections to use it. | |
10 | |
11 import 'dart:io'; | |
12 import 'dart:convert'; | |
13 | |
14 import 'package:expect/expect.dart'; | |
15 import 'package:async_helper/async_helper.dart'; | |
16 | |
17 const String MAX_LEN_ERROR = | |
18 'Length of protocol must be between 1 and 255'; | |
19 | |
20 const String MAX_MSG_LEN_ERROR = | |
21 'The maximum message length supported is 2^15-1'; | |
22 | |
23 void InitializeSSL() { | |
24 var testPkcertDatabase = Platform.script.resolve('pkcert').toFilePath(); | |
25 SecureSocket.initialize(database: testPkcertDatabase, | |
26 password: 'dartdart'); | |
27 } | |
28 | |
29 | |
30 // Tests that client/server with same protocol can securly establish a | |
31 // connection, negogiate the protocol and can send data to each other. | |
32 void testSuccessfulAlpnNegogiationConnection(List<String> clientProtocols, | |
33 List<String> serverProtocols, | |
34 String selectedProtocol) { | |
35 asyncStart(); | |
36 SecureServerSocket.bind('localhost', 0, 'localhost_cert', | |
37 supportedProtocols: serverProtocols).then((SecureServerSocket server) { | |
38 | |
39 asyncStart(); | |
40 server.first.then((SecureSocket socket) { | |
41 Expect.equals(selectedProtocol, socket.selectedProtocol); | |
42 socket..write('server message')..close(); | |
43 socket.transform(ASCII.decoder).join('').then((String s) { | |
44 Expect.equals('client message', s); | |
45 asyncEnd(); | |
46 }); | |
47 }); | |
48 | |
49 asyncStart(); | |
50 SecureSocket.connect('localhost', server.port, | |
51 supportedProtocols: clientProtocols).then((socket) { | |
52 Expect.equals(selectedProtocol, socket.selectedProtocol); | |
53 socket..write('client message')..close(); | |
54 socket.transform(ASCII.decoder).join('').then((String s) { | |
55 Expect.equals('server message', s); | |
56 server.close(); | |
57 asyncEnd(); | |
58 }); | |
59 }); | |
60 | |
61 asyncEnd(); | |
62 }); | |
63 } | |
64 | |
65 void testFailedAlpnNegogiationConnection(List<String> clientProtocols, | |
66 List<String> serverProtocols) { | |
67 asyncStart(); | |
68 SecureServerSocket.bind('localhost', 0, 'localhost_cert', | |
69 supportedProtocols: serverProtocols).then((SecureServerSocket server) { | |
70 | |
71 asyncStart(); | |
72 server.first.catchError((error, stack) { | |
Søren Gjesse
2014/11/10 11:38:00
Add a test of the error type?
kustermann
2014/11/10 12:30:21
Added
Expect.isTrue(error is HandshakeException);
| |
73 asyncEnd(); | |
74 }); | |
75 | |
76 asyncStart(); | |
77 SecureSocket.connect('localhost', | |
78 server.port, | |
79 supportedProtocols: clientProtocols) | |
80 .catchError((error, stack) { | |
Søren Gjesse
2014/11/10 11:38:00
Ditto.
kustermann
2014/11/10 12:30:21
ditto
| |
81 asyncEnd(); | |
82 }); | |
83 | |
84 asyncEnd(); | |
85 }); | |
86 } | |
87 | |
88 | |
89 void testInvalidArgumentsLongName(List<String> protocols, | |
90 bool isLenError, | |
91 bool isMsgLenError) { | |
92 asyncStart(); | |
93 SecureServerSocket.bind('localhost', 0, 'localhost_cert', | |
94 supportedProtocols: protocols).then((SecureServerSocket server) { | |
95 | |
96 asyncStart(); | |
97 server.first.catchError((error, stack) { | |
98 String errorString = '${(error as ArgumentError)}'; | |
99 if (isLenError) { | |
100 Expect.isTrue(errorString.contains(MAX_LEN_ERROR)); | |
101 } else if (isMsgLenError) { | |
102 Expect.isTrue(errorString.contains(MAX_MSG_LEN_ERROR)); | |
103 } else { | |
104 throw 'unreachable'; | |
105 } | |
106 asyncEnd(); | |
107 }); | |
108 | |
109 asyncStart(); | |
110 SecureSocket.connect('localhost', | |
111 server.port, | |
112 supportedProtocols: protocols) | |
113 .catchError((error, stack) { | |
114 String errorString = '${(error as ArgumentError)}'; | |
115 if (isLenError) { | |
116 Expect.isTrue(errorString.contains(MAX_LEN_ERROR)); | |
117 } else if (isMsgLenError) { | |
118 Expect.isTrue(errorString.contains(MAX_MSG_LEN_ERROR)); | |
119 } else { | |
120 throw 'unreachable'; | |
121 } | |
122 asyncEnd(); | |
123 }); | |
124 | |
125 asyncEnd(); | |
126 }); | |
127 } | |
128 | |
129 | |
130 main() { | |
131 InitializeSSL(); | |
132 final longname256 = 'p' * 256; | |
133 final String longname255 = 'p' * 255; | |
134 final String strangelongname255 = 'ø' + 'p' * 253; | |
135 final String strangelongname256 = 'ø' + 'p' * 254; | |
136 | |
137 // This produces a message of (1 << 15)-2 bytes (1 length and 1 ascii byte). | |
138 final List<String> allProtocols = new Iterable.generate( | |
139 (1 << 14) - 1, (i) => '0').toList(); | |
140 | |
141 // This produces a message of (1 << 15) bytes (1 length and 1 ascii byte). | |
142 final List<String> allProtocolsPlusOne = new Iterable.generate( | |
143 (1 << 14), (i) => '0').toList(); | |
144 | |
145 // Protocols are in order of decreasing priority. First matching protocol | |
146 // will be taken. | |
147 | |
148 // Test successfull negotiation, including priority. | |
149 testSuccessfulAlpnNegogiationConnection(['a'], | |
150 ['a'], | |
151 'a'); | |
152 | |
153 testSuccessfulAlpnNegogiationConnection([longname255], | |
154 [longname255], | |
155 longname255); | |
156 | |
157 testSuccessfulAlpnNegogiationConnection([strangelongname255], | |
158 [strangelongname255], | |
159 strangelongname255); | |
160 | |
161 testSuccessfulAlpnNegogiationConnection(allProtocols, | |
162 allProtocols, | |
163 '0'); | |
164 | |
165 testSuccessfulAlpnNegogiationConnection(['a', 'b', 'c'], | |
166 ['a', 'b', 'c'], | |
167 'a'); | |
168 | |
169 testSuccessfulAlpnNegogiationConnection(['a', 'b', 'c'], | |
170 ['c'], | |
171 'c'); | |
172 | |
173 // Server precedence. | |
174 testSuccessfulAlpnNegogiationConnection(['a', 'b', 'c'], | |
175 ['c', 'b', 'a'], | |
176 'a'); | |
177 | |
178 testSuccessfulAlpnNegogiationConnection(['c'], | |
179 ['a', 'b', 'c'], | |
180 'c'); | |
181 | |
182 testSuccessfulAlpnNegogiationConnection(['s1', 'b', 'e1'], | |
183 ['s2', 'b', 'e2'], | |
184 'b'); | |
185 | |
186 // Test no protocol negotiation support | |
187 testSuccessfulAlpnNegogiationConnection(null, | |
188 null, | |
189 null); | |
190 | |
191 testSuccessfulAlpnNegogiationConnection(['a', 'b', 'c'], | |
192 null, | |
193 null); | |
194 | |
195 testSuccessfulAlpnNegogiationConnection(null, | |
196 ['a', 'b', 'c'], | |
197 null); | |
198 | |
199 testSuccessfulAlpnNegogiationConnection([], | |
200 [], | |
201 null); | |
202 | |
203 testSuccessfulAlpnNegogiationConnection(['a', 'b', 'c'], | |
204 [], | |
205 null); | |
206 | |
207 testSuccessfulAlpnNegogiationConnection([], | |
208 ['a', 'b', 'c'], | |
209 null); | |
210 | |
211 // Test non-overlapping protocols. | |
212 testFailedAlpnNegogiationConnection(['a'], ['b']); | |
213 | |
214 | |
215 // Test too short / too long protocol names. | |
216 testInvalidArgumentsLongName([longname256], true, false); | |
217 testInvalidArgumentsLongName([strangelongname256], true, false); | |
218 testInvalidArgumentsLongName([''], true, false); | |
219 testInvalidArgumentsLongName(allProtocolsPlusOne, false, true); | |
220 testInvalidArgumentsLongName(allProtocolsPlusOne, false, true); | |
221 } | |
OLD | NEW |