Chromium Code Reviews| 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"; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 clientEnd.shutdown(SocketDirection.SEND); | 126 clientEnd.shutdown(SocketDirection.SEND); |
| 127 serverEnd.shutdown(SocketDirection.SEND); | 127 serverEnd.shutdown(SocketDirection.SEND); |
| 128 server.close(); | 128 server.close(); |
| 129 port.close(); | 129 port.close(); |
| 130 }); | 130 }); |
| 131 }); | 131 }); |
| 132 }); | 132 }); |
| 133 }); | 133 }); |
| 134 } | 134 } |
| 135 | 135 |
| 136 void testSimpleReadWrite() { | 136 // This test creates a server and a client connects. The client then |
| 137 // This test creates a server and a client connects. The client then | 137 // writes and the server echos. When the server has finished its echo |
| 138 // writes and the server echos. When the server has finished its | 138 // it half-closes. When the client gets the close event is closes |
| 139 // echo it half-closes. When the client gets the close event is | 139 // |
|
Mads Ager (google)
2013/04/03 15:26:55
Move 'fully.' up here and have the empty line afte
Søren Gjesse
2013/04/19 05:17:09
Done.
| |
| 140 // closes fully. | 140 // fully. |
| 141 // The test can be run in different configurations based on | |
| 142 // the boolean arguments. | |
| 143 // | |
| 144 // listenSecure | |
| 145 // When this argument is true a secure server is used. When this is false | |
| 146 // a non-secure server is used and the connections are secured after beeing | |
| 147 // connected. | |
| 148 // | |
| 149 // connectSecure | |
| 150 // When this argument is true a secure client connection is used. When this | |
| 151 // is false a non-secure client connection is used and the connection is | |
| 152 // secured after being connected. | |
| 153 // | |
| 154 // handshakeBeforeSecure | |
| 155 // When this argument is true some initial clear text handshake is done | |
| 156 // between client and server before the connection is secured. This argument | |
| 157 // only makes sense when both listenSecure and connectSecure are false. | |
| 158 void testSimpleReadWrite(bool listenSecure, | |
| 159 bool connectSecure, | |
| 160 bool handshakeBeforeSecure) { | |
| 161 if (handshakeBeforeSecure == true && | |
| 162 (listenSecure == true || connectSecure == true)) { | |
| 163 Expect.fails("Invalid arguments to testSimpleReadWrite"); | |
| 164 } | |
| 165 | |
| 141 ReceivePort port = new ReceivePort(); | 166 ReceivePort port = new ReceivePort(); |
| 142 | 167 |
| 143 const messageSize = 1000; | 168 const messageSize = 1000; |
| 169 const handshakeMessageSize = 100; | |
| 144 | 170 |
| 145 List<int> createTestData() { | 171 List<int> createTestData() { |
| 146 List<int> data = new List<int>(messageSize); | 172 List<int> data = new List<int>(messageSize); |
| 147 for (int i = 0; i < messageSize; i++) { | 173 for (int i = 0; i < messageSize; i++) { |
| 148 data[i] = i & 0xff; | 174 data[i] = i & 0xff; |
| 149 } | 175 } |
| 150 return data; | 176 return data; |
| 151 } | 177 } |
| 152 | 178 |
| 179 List<int> createHandshakeTestData() { | |
| 180 List<int> data = new List<int>(handshakeMessageSize); | |
| 181 for (int i = 0; i < handshakeMessageSize; i++) { | |
| 182 data[i] = i & 0xff; | |
| 183 } | |
| 184 return data; | |
| 185 } | |
| 186 | |
| 153 void verifyTestData(List<int> data) { | 187 void verifyTestData(List<int> data) { |
| 154 Expect.equals(messageSize, data.length); | 188 Expect.equals(messageSize, data.length); |
| 155 List<int> expected = createTestData(); | 189 List<int> expected = createTestData(); |
| 156 for (int i = 0; i < messageSize; i++) { | 190 for (int i = 0; i < messageSize; i++) { |
| 157 Expect.equals(expected[i], data[i]); | 191 Expect.equals(expected[i], data[i]); |
| 158 } | 192 } |
| 159 } | 193 } |
| 160 | 194 |
| 161 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { | 195 void verifyHandshakeTestData(List<int> data) { |
| 196 Expect.equals(handshakeMessageSize, data.length); | |
| 197 List<int> expected = createHandshakeTestData(); | |
| 198 for (int i = 0; i < handshakeMessageSize; i++) { | |
| 199 Expect.equals(expected[i], data[i]); | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 Future runServer(RawSocket client) { | |
| 204 var completer = new Completer(); | |
| 205 int bytesRead = 0; | |
| 206 int bytesWritten = 0; | |
| 207 List<int> data = new List<int>(messageSize); | |
| 208 client.writeEventsEnabled = false; | |
| 209 var subscription; | |
| 210 subscription = client.listen((event) { | |
| 211 switch (event) { | |
| 212 case RawSocketEvent.READ: | |
| 213 Expect.isTrue(bytesWritten == 0); | |
| 214 Expect.isTrue(client.available() > 0); | |
| 215 var buffer = client.read(); | |
| 216 if (buffer != null) { | |
| 217 data.setRange(bytesRead, buffer.length, buffer); | |
| 218 bytesRead += buffer.length; | |
| 219 for (var value in buffer) { | |
| 220 Expect.isTrue(value is int); | |
| 221 Expect.isTrue(value < 256 && value >= 0); | |
| 222 } | |
| 223 } | |
| 224 if (bytesRead == data.length) { | |
| 225 verifyTestData(data); | |
| 226 client.writeEventsEnabled = true; | |
| 227 } | |
| 228 break; | |
| 229 case RawSocketEvent.WRITE: | |
| 230 Expect.isFalse(client.writeEventsEnabled); | |
| 231 Expect.equals(bytesRead, data.length); | |
| 232 for (int i = bytesWritten; i < data.length; ++i) { | |
| 233 Expect.isTrue(data[i] is int); | |
| 234 Expect.isTrue(data[i] < 256 && data[i] >= 0); | |
| 235 } | |
| 236 bytesWritten += client.write( | |
| 237 data, bytesWritten, data.length - bytesWritten); | |
| 238 if (bytesWritten < data.length) { | |
| 239 client.writeEventsEnabled = true; | |
| 240 } | |
| 241 if (bytesWritten == data.length) { | |
| 242 client.shutdown(SocketDirection.SEND); | |
| 243 } | |
| 244 break; | |
| 245 case RawSocketEvent.READ_CLOSED: | |
| 246 completer.complete(null); | |
| 247 break; | |
| 248 default: throw "Unexpected event $event"; | |
| 249 } | |
| 250 }); | |
| 251 return completer.future; | |
| 252 } | |
| 253 | |
| 254 Future<RawSocket> runClient(RawSocket socket) { | |
| 255 var completer = new Completer(); | |
| 256 int bytesRead = 0; | |
| 257 int bytesWritten = 0; | |
| 258 List<int> dataSent = createTestData(); | |
| 259 List<int> dataReceived = new List<int>(dataSent.length); | |
| 260 socket.listen((event) { | |
| 261 switch (event) { | |
| 262 case RawSocketEvent.READ: | |
| 263 Expect.isTrue(socket.available() > 0); | |
| 264 var buffer = socket.read(); | |
| 265 if (buffer != null) { | |
| 266 dataReceived.setRange(bytesRead, buffer.length, buffer); | |
| 267 bytesRead += buffer.length; | |
| 268 } | |
| 269 break; | |
| 270 case RawSocketEvent.WRITE: | |
| 271 Expect.isTrue(bytesRead == 0); | |
| 272 Expect.isFalse(socket.writeEventsEnabled); | |
| 273 bytesWritten += socket.write( | |
| 274 dataSent, bytesWritten, dataSent.length - bytesWritten); | |
| 275 if (bytesWritten < dataSent.length) { | |
| 276 socket.writeEventsEnabled = true; | |
| 277 } | |
| 278 break; | |
| 279 case RawSocketEvent.READ_CLOSED: | |
| 280 verifyTestData(dataReceived); | |
| 281 completer.complete(socket); | |
| 282 break; | |
| 283 default: throw "Unexpected event $event"; | |
| 284 } | |
| 285 }); | |
| 286 return completer.future; | |
| 287 } | |
| 288 | |
| 289 Future runServerHandshake(RawSocket client) { | |
| 290 var completer = new Completer(); | |
| 291 int bytesRead = 0; | |
| 292 int bytesWritten = 0; | |
| 293 List<int> data = new List<int>(handshakeMessageSize); | |
| 294 client.writeEventsEnabled = false; | |
| 295 var subscription; | |
| 296 subscription = client.listen((event) { | |
| 297 switch (event) { | |
| 298 case RawSocketEvent.READ: | |
| 299 Expect.isTrue(bytesWritten == 0); | |
| 300 Expect.isTrue(client.available() > 0); | |
| 301 var buffer = client.read(); | |
| 302 if (buffer != null) { | |
| 303 data.setRange(bytesRead, buffer.length, buffer); | |
| 304 bytesRead += buffer.length; | |
| 305 for (var value in buffer) { | |
| 306 Expect.isTrue(value is int); | |
| 307 Expect.isTrue(value < 256 && value >= 0); | |
| 308 } | |
| 309 } | |
| 310 if (bytesRead == data.length) { | |
| 311 verifyHandshakeTestData(data); | |
| 312 client.writeEventsEnabled = true; | |
| 313 } | |
| 314 break; | |
| 315 case RawSocketEvent.WRITE: | |
| 316 Expect.isFalse(client.writeEventsEnabled); | |
| 317 Expect.equals(bytesRead, data.length); | |
| 318 for (int i = bytesWritten; i < data.length; ++i) { | |
| 319 Expect.isTrue(data[i] is int); | |
| 320 Expect.isTrue(data[i] < 256 && data[i] >= 0); | |
| 321 } | |
| 322 bytesWritten += client.write( | |
| 323 data, bytesWritten, data.length - bytesWritten); | |
| 324 if (bytesWritten < data.length) { | |
| 325 client.writeEventsEnabled = true; | |
| 326 } | |
| 327 if (bytesWritten == data.length) { | |
| 328 subscription.cancel(); | |
| 329 completer.complete(null); | |
| 330 } | |
| 331 break; | |
| 332 case RawSocketEvent.READ_CLOSED: | |
| 333 Expect.fail("Unexpected close"); | |
| 334 break; | |
| 335 default: throw "Unexpected event $event"; | |
| 336 } | |
| 337 }); | |
| 338 return completer.future; | |
| 339 } | |
| 340 | |
| 341 Future<RawSocket> runClientHandshake(RawSocket socket) { | |
| 342 var completer = new Completer(); | |
| 343 int bytesRead = 0; | |
| 344 int bytesWritten = 0; | |
| 345 List<int> dataSent = createHandshakeTestData(); | |
| 346 List<int> dataReceived = new List<int>(dataSent.length); | |
| 347 var subscription; | |
| 348 subscription = socket.listen((event) { | |
| 349 switch (event) { | |
| 350 case RawSocketEvent.READ: | |
| 351 Expect.isTrue(socket.available() > 0); | |
| 352 var buffer = socket.read(); | |
| 353 if (buffer != null) { | |
| 354 dataReceived.setRange(bytesRead, buffer.length, buffer); | |
| 355 bytesRead += buffer.length; | |
| 356 if (bytesRead == dataSent.length) { | |
| 357 verifyHandshakeTestData(dataReceived); | |
| 358 subscription.cancel(); | |
| 359 completer.complete(socket); | |
| 360 } | |
| 361 } | |
| 362 break; | |
| 363 case RawSocketEvent.WRITE: | |
| 364 Expect.isTrue(bytesRead == 0); | |
| 365 Expect.isFalse(socket.writeEventsEnabled); | |
| 366 bytesWritten += socket.write( | |
| 367 dataSent, bytesWritten, dataSent.length - bytesWritten); | |
| 368 if (bytesWritten < dataSent.length) { | |
| 369 socket.writeEventsEnabled = true; | |
| 370 } | |
| 371 break; | |
| 372 case RawSocketEvent.READ_CLOSED: | |
| 373 Expect.fail("Unexpected close"); | |
| 374 break; | |
| 375 default: throw "Unexpected event $event"; | |
| 376 } | |
| 377 }); | |
| 378 return completer.future; | |
| 379 } | |
| 380 | |
| 381 Future<RawSecureSocket> connectClient(int port) { | |
| 382 if (connectSecure) { | |
| 383 return RawSecureSocket.connect(HOST_NAME, port); | |
| 384 } else if (!handshakeBeforeSecure) { | |
| 385 return RawSocket.connect(HOST_NAME, port).then((socket) { | |
| 386 return RawSecureSocket.secure(socket); | |
| 387 }); | |
| 388 } else { | |
| 389 return RawSocket.connect(HOST_NAME, port).then((socket) { | |
| 390 return runClientHandshake(socket).then((_) { | |
| 391 return RawSecureSocket.secure(socket); | |
| 392 }); | |
| 393 }); | |
| 394 } | |
| 395 } | |
| 396 | |
| 397 serverReady(server) { | |
| 162 server.listen((client) { | 398 server.listen((client) { |
| 163 int bytesRead = 0; | 399 if (listenSecure) { |
| 164 int bytesWritten = 0; | 400 runServer(client).then((_) => server.close()); |
| 165 List<int> data = new List<int>(messageSize); | 401 } else if (!handshakeBeforeSecure) { |
| 166 | 402 RawSecureSocket.secureServer(client, CERTIFICATE).then((client) { |
| 167 client.writeEventsEnabled = false; | 403 runServer(client).then((_) => server.close()); |
| 168 client.listen((event) { | 404 }); |
| 169 switch (event) { | 405 } else { |
| 170 case RawSocketEvent.READ: | 406 runServerHandshake(client).then((_) { |
| 171 Expect.isTrue(bytesWritten == 0); | 407 RawSecureSocket.secureServer(client, CERTIFICATE).then((client) { |
| 172 Expect.isTrue(client.available() > 0); | 408 runServer(client).then((_) => server.close()); |
| 173 var buffer = client.read(); | 409 }); |
| 174 if (buffer != null) { | 410 }); |
| 175 data.setRange(bytesRead, buffer.length, buffer); | 411 } |
| 176 bytesRead += buffer.length; | 412 }); |
| 177 for (var value in buffer) { | 413 |
| 178 Expect.isTrue(value is int); | 414 connectClient(server.port).then(runClient).then((socket) { |
| 179 Expect.isTrue(value < 256 && value >= 0); | 415 socket.close(); |
| 180 } | 416 port.close(); |
| 181 } | 417 }); |
| 182 if (bytesRead == data.length) { | 418 } |
| 183 verifyTestData(data); | 419 |
| 184 client.writeEventsEnabled = true; | 420 if (listenSecure) { |
| 185 } | 421 RawSecureServerSocket.bind( |
| 186 break; | 422 SERVER_ADDRESS, 0, 5, CERTIFICATE).then(serverReady); |
| 187 case RawSocketEvent.WRITE: | 423 } else { |
| 188 Expect.isFalse(client.writeEventsEnabled); | 424 RawServerSocket.bind(SERVER_ADDRESS, 0, 5).then(serverReady); |
| 189 Expect.equals(bytesRead, data.length); | 425 } |
| 190 for (int i = bytesWritten; i < data.length; ++i) { | |
| 191 Expect.isTrue(data[i] is int); | |
| 192 Expect.isTrue(data[i] < 256 && data[i] >= 0); | |
| 193 } | |
| 194 bytesWritten += client.write( | |
| 195 data, bytesWritten, data.length - bytesWritten); | |
| 196 if (bytesWritten < data.length) { | |
| 197 client.writeEventsEnabled = true; | |
| 198 } | |
| 199 if (bytesWritten == data.length) { | |
| 200 client.shutdown(SocketDirection.SEND); | |
| 201 } | |
| 202 break; | |
| 203 case RawSocketEvent.READ_CLOSED: | |
| 204 server.close(); | |
| 205 break; | |
| 206 default: throw "Unexpected event $event"; | |
| 207 } | |
| 208 }); | |
| 209 }); | |
| 210 | |
| 211 RawSecureSocket.connect(HOST_NAME, server.port).then((socket) { | |
| 212 int bytesRead = 0; | |
| 213 int bytesWritten = 0; | |
| 214 List<int> dataSent = createTestData(); | |
| 215 List<int> dataReceived = new List<int>(dataSent.length); | |
| 216 socket.listen((event) { | |
| 217 switch (event) { | |
| 218 case RawSocketEvent.READ: | |
| 219 Expect.isTrue(socket.available() > 0); | |
| 220 var buffer = socket.read(); | |
| 221 if (buffer != null) { | |
| 222 dataReceived.setRange(bytesRead, buffer.length, buffer); | |
| 223 bytesRead += buffer.length; | |
| 224 } | |
| 225 break; | |
| 226 case RawSocketEvent.WRITE: | |
| 227 Expect.isTrue(bytesRead == 0); | |
| 228 Expect.isFalse(socket.writeEventsEnabled); | |
| 229 bytesWritten += socket.write( | |
| 230 dataSent, bytesWritten, dataSent.length - bytesWritten); | |
| 231 if (bytesWritten < dataSent.length) { | |
| 232 socket.writeEventsEnabled = true; | |
| 233 } | |
| 234 break; | |
| 235 case RawSocketEvent.READ_CLOSED: | |
| 236 verifyTestData(dataReceived); | |
| 237 socket.close(); | |
| 238 port.close(); | |
| 239 break; | |
| 240 default: throw "Unexpected event $event"; | |
| 241 } | |
| 242 }); | |
| 243 }); | |
| 244 }); | |
| 245 } | 426 } |
| 246 | 427 |
| 247 main() { | 428 main() { |
| 248 Path scriptDir = new Path(new Options().script).directoryPath; | 429 Path scriptDir = new Path(new Options().script).directoryPath; |
| 249 Path certificateDatabase = scriptDir.append('pkcert'); | 430 Path certificateDatabase = scriptDir.append('pkcert'); |
| 250 SecureSocket.initialize(database: certificateDatabase.toNativePath(), | 431 SecureSocket.initialize(database: certificateDatabase.toNativePath(), |
| 251 password: 'dartdart', | 432 password: 'dartdart', |
| 252 useBuiltinRoots: false); | 433 useBuiltinRoots: false); |
| 253 testArguments(); | 434 testArguments(); |
| 254 testSimpleBind(); | 435 testSimpleBind(); |
| 255 testInvalidBind(); | 436 testInvalidBind(); |
| 256 testSimpleConnect(CERTIFICATE); | 437 testSimpleConnect(CERTIFICATE); |
| 257 testSimpleConnect("CN=localhost"); | 438 testSimpleConnect("CN=localhost"); |
| 258 testSimpleConnectFail("not_a_nickname"); | 439 testSimpleConnectFail("not_a_nickname"); |
| 259 testSimpleConnectFail("CN=notARealDistinguishedName"); | 440 testSimpleConnectFail("CN=notARealDistinguishedName"); |
| 260 testServerListenAfterConnect(); | 441 testServerListenAfterConnect(); |
| 261 testSimpleReadWrite(); | 442 testSimpleReadWrite(true, true, false); |
| 443 testSimpleReadWrite(true, false, false); | |
| 444 testSimpleReadWrite(false, true, false); | |
| 445 testSimpleReadWrite(false, false, false); | |
| 446 testSimpleReadWrite(false, false, true); | |
| 262 } | 447 } |
| OLD | NEW |