OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // | 4 // |
5 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 | 9 |
10 import "dart:async"; | 10 import "dart:async"; |
11 import "dart:io"; | 11 import "dart:io"; |
12 | 12 |
13 import "package:async_helper/async_helper.dart"; | 13 import "package:async_helper/async_helper.dart"; |
14 import "package:expect/expect.dart"; | 14 import "package:expect/expect.dart"; |
15 | 15 |
16 InternetAddress HOST; | 16 InternetAddress HOST; |
17 const CERTIFICATE = "localhost_cert"; | 17 |
| 18 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
| 19 |
| 20 SecurityContext serverContext = new SecurityContext() |
| 21 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
| 22 ..usePrivateKey(localFile('certificates/server_key.pem'), |
| 23 password: 'dartdart'); |
| 24 |
| 25 SecurityContext clientContext = new SecurityContext() |
| 26 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); |
18 | 27 |
19 void testCloseOneEnd(String toClose) { | 28 void testCloseOneEnd(String toClose) { |
20 asyncStart(); | 29 asyncStart(); |
21 Completer serverDone = new Completer(); | 30 Completer serverDone = new Completer(); |
22 Completer serverEndDone = new Completer(); | 31 Completer serverEndDone = new Completer(); |
23 Completer clientEndDone = new Completer(); | 32 Completer clientEndDone = new Completer(); |
24 Future.wait([serverDone.future, serverEndDone.future, clientEndDone.future]) | 33 Future.wait([serverDone.future, serverEndDone.future, clientEndDone.future]) |
25 .then((_) { | 34 .then((_) { |
26 asyncEnd(); | 35 asyncEnd(); |
27 }); | 36 }); |
28 SecureServerSocket.bind(HOST, 0, CERTIFICATE).then((server) { | 37 SecureServerSocket.bind(HOST, 0, serverContext).then((server) { |
29 server.listen((serverConnection) { | 38 server.listen((serverConnection) { |
30 serverConnection.listen( | 39 serverConnection.listen( |
31 (data) { | 40 (data) { |
32 Expect.fail("No data should be received by server"); | 41 Expect.fail("No data should be received by server"); |
33 }, | 42 }, |
34 onDone: () { | 43 onDone: () { |
35 serverConnection.close(); | 44 serverConnection.close(); |
36 serverEndDone.complete(null); | 45 serverEndDone.complete(null); |
37 server.close(); | 46 server.close(); |
38 }); | 47 }); |
39 if (toClose == "server") { | 48 if (toClose == "server") { |
40 serverConnection.close(); | 49 serverConnection.close(); |
41 } | 50 } |
42 }, | 51 }, |
43 onDone: () { | 52 onDone: () { |
44 serverDone.complete(null); | 53 serverDone.complete(null); |
45 }); | 54 }); |
46 SecureSocket.connect(HOST, server.port).then((clientConnection) { | 55 SecureSocket.connect(HOST, server.port, context: clientContext) |
| 56 .then((clientConnection) { |
47 clientConnection.listen( | 57 clientConnection.listen( |
48 (data) { | 58 (data) { |
49 Expect.fail("No data should be received by client"); | 59 Expect.fail("No data should be received by client"); |
50 }, | 60 }, |
51 onDone: () { | 61 onDone: () { |
52 clientConnection.close(); | 62 clientConnection.close(); |
53 clientEndDone.complete(null); | 63 clientEndDone.complete(null); |
54 }); | 64 }); |
55 if (toClose == "client") { | 65 if (toClose == "client") { |
56 clientConnection.close(); | 66 clientConnection.close(); |
57 } | 67 } |
58 }); | 68 }); |
59 }); | 69 }); |
60 } | 70 } |
61 | 71 |
62 void testCloseBothEnds() { | 72 void testCloseBothEnds() { |
63 asyncStart(); | 73 asyncStart(); |
64 SecureServerSocket.bind(HOST, 0, CERTIFICATE).then((server) { | 74 SecureServerSocket.bind(HOST, 0, serverContext).then((server) { |
65 var clientEndFuture = SecureSocket.connect(HOST, server.port); | 75 var clientEndFuture = |
| 76 SecureSocket.connect(HOST, server.port, context: clientContext); |
66 server.listen((serverEnd) { | 77 server.listen((serverEnd) { |
67 clientEndFuture.then((clientEnd) { | 78 clientEndFuture.then((clientEnd) { |
68 clientEnd.destroy(); | 79 clientEnd.destroy(); |
69 serverEnd.destroy(); | 80 serverEnd.destroy(); |
70 server.close(); | 81 server.close(); |
71 asyncEnd(); | 82 asyncEnd(); |
72 }); | 83 }); |
73 }); | 84 }); |
74 }); | 85 }); |
75 } | 86 } |
76 | 87 |
77 testPauseServerSocket() { | 88 testPauseServerSocket() { |
78 const int socketCount = 10; | 89 const int socketCount = 10; |
79 var acceptCount = 0; | 90 var acceptCount = 0; |
80 var resumed = false; | 91 var resumed = false; |
81 | 92 |
82 asyncStart(); | 93 asyncStart(); |
83 | 94 |
84 SecureServerSocket.bind(HOST, | 95 SecureServerSocket.bind(HOST, |
85 0, | 96 0, |
86 CERTIFICATE, | 97 serverContext, |
87 backlog: 2 * socketCount).then((server) { | 98 backlog: 2 * socketCount).then((server) { |
88 Expect.isTrue(server.port > 0); | 99 Expect.isTrue(server.port > 0); |
89 var subscription; | 100 var subscription; |
90 subscription = server.listen((connection) { | 101 subscription = server.listen((connection) { |
91 Expect.isTrue(resumed); | 102 Expect.isTrue(resumed); |
92 connection.close(); | 103 connection.close(); |
93 if (++acceptCount == 2 * socketCount) { | 104 if (++acceptCount == 2 * socketCount) { |
94 server.close(); | 105 server.close(); |
95 asyncEnd(); | 106 asyncEnd(); |
96 } | 107 } |
97 }); | 108 }); |
98 | 109 |
99 // Pause the server socket subscription and resume it after having | 110 // Pause the server socket subscription and resume it after having |
100 // connected a number client sockets. Then connect more client | 111 // connected a number client sockets. Then connect more client sockets. |
101 // sockets. | |
102 subscription.pause(); | 112 subscription.pause(); |
103 var connectCount = 0; | 113 var connectCount = 0; |
104 for (int i = 0; i < socketCount; i++) { | 114 for (int i = 0; i < socketCount; i++) { |
105 SecureSocket.connect(HOST, server.port).then((connection) { | 115 SecureSocket.connect(HOST, server.port, context: clientContext) |
| 116 .then((connection) { |
106 connection.close(); | 117 connection.close(); |
107 }); | 118 }); |
108 } | 119 } |
109 new Timer(const Duration(milliseconds: 500), () { | 120 new Timer(const Duration(milliseconds: 500), () { |
110 subscription.resume(); | 121 subscription.resume(); |
111 resumed = true; | 122 resumed = true; |
112 for (int i = 0; i < socketCount; i++) { | 123 for (int i = 0; i < socketCount; i++) { |
113 SecureSocket.connect(HOST, server.port).then((connection) { | 124 SecureSocket.connect(HOST, server.port, context: clientContext) |
| 125 .then((connection) { |
114 connection.close(); | 126 connection.close(); |
115 }); | 127 }); |
116 } | 128 } |
117 }); | 129 }); |
118 }); | 130 }); |
119 } | 131 } |
120 | 132 |
121 | 133 |
122 testCloseServer() { | 134 testCloseServer() { |
123 const int socketCount = 3; | 135 const int socketCount = 3; |
124 var endCount = 0; | 136 var endCount = 0; |
125 asyncStart(); | 137 asyncStart(); |
126 List ends = []; | 138 List ends = []; |
127 | 139 |
128 SecureServerSocket.bind(HOST, 0, CERTIFICATE).then((server) { | 140 SecureServerSocket.bind(HOST, 0, serverContext).then((server) { |
129 Expect.isTrue(server.port > 0); | 141 Expect.isTrue(server.port > 0); |
130 void checkDone() { | 142 void checkDone() { |
131 if (ends.length < 2 * socketCount) return; | 143 if (ends.length < 2 * socketCount) return; |
132 for (var end in ends) { | 144 for (var end in ends) { |
133 end.destroy(); | 145 end.destroy(); |
134 } | 146 } |
135 server.close(); | 147 server.close(); |
136 asyncEnd(); | 148 asyncEnd(); |
137 } | 149 } |
138 | 150 |
139 server.listen((connection) { | 151 server.listen((connection) { |
140 ends.add(connection); | 152 ends.add(connection); |
141 checkDone(); | 153 checkDone(); |
142 }); | 154 }); |
143 | 155 |
144 for (int i = 0; i < socketCount; i++) { | 156 for (int i = 0; i < socketCount; i++) { |
145 SecureSocket.connect(HOST, server.port).then((connection) { | 157 SecureSocket.connect(HOST, server.port, context: clientContext) |
| 158 .then((connection) { |
146 ends.add(connection); | 159 ends.add(connection); |
147 checkDone(); | 160 checkDone(); |
148 }); | 161 }); |
149 } | 162 } |
150 }); | 163 }); |
151 } | 164 } |
152 | 165 |
153 | 166 |
154 main() { | 167 main() { |
155 asyncStart(); | 168 asyncStart(); |
156 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); | |
157 SecureSocket.initialize(database: certificateDatabase, | |
158 password: 'dartdart', | |
159 useBuiltinRoots: false); | |
160 InternetAddress.lookup("localhost").then((hosts) { | 169 InternetAddress.lookup("localhost").then((hosts) { |
161 HOST = hosts.first; | 170 HOST = hosts.first; |
162 runTests(); | 171 runTests(); |
163 asyncEnd(); | 172 asyncEnd(); |
164 }); | 173 }); |
165 } | 174 } |
166 | 175 |
167 runTests() { | 176 runTests() { |
168 testCloseOneEnd("client"); | 177 testCloseOneEnd("client"); |
169 testCloseOneEnd("server"); | 178 testCloseOneEnd("server"); |
170 testCloseBothEnds(); | 179 testCloseBothEnds(); |
171 testPauseServerSocket(); | 180 testPauseServerSocket(); |
172 testCloseServer(); | 181 testCloseServer(); |
173 // TODO(whesse): Add testPauseSocket from raw_socket_test.dart. | 182 // TODO(whesse): Add testPauseSocket from raw_socket_test.dart. |
174 // TODO(whesse): Add testCancelResubscribeSocket from raw_socket_test.dart. | 183 // TODO(whesse): Add testCancelResubscribeSocket from raw_socket_test.dart. |
175 } | 184 } |
OLD | NEW |