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