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