| 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 import "package:path/path.dart"; | |
| 16 | 15 |
| 17 const HOST_NAME = "localhost"; | 16 InternetAddress HOST; |
| 18 const CERTIFICATE = "localhost_cert"; | 17 const CERTIFICATE = "localhost_cert"; |
| 19 | 18 |
| 20 void testSimpleBind() { | 19 void testSimpleBind() { |
| 21 asyncStart(); | 20 asyncStart(); |
| 22 RawSecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((s) { | 21 RawSecureServerSocket.bind(HOST, 0, CERTIFICATE).then((s) { |
| 23 Expect.isTrue(s.port > 0); | 22 Expect.isTrue(s.port > 0); |
| 24 s.close(); | 23 s.close(); |
| 25 asyncEnd(); | 24 asyncEnd(); |
| 26 }); | 25 }); |
| 27 } | 26 } |
| 28 | 27 |
| 29 void testInvalidBind() { | 28 void testInvalidBind() { |
| 30 int count = 0; | 29 int count = 0; |
| 31 | 30 |
| 32 // Bind to a unknown DNS name. | 31 // Bind to a unknown DNS name. |
| 33 asyncStart(); | 32 asyncStart(); |
| 34 RawSecureServerSocket.bind("ko.faar.__hest__", 0, CERTIFICATE).then((_) { | 33 RawSecureServerSocket.bind("ko.faar.__hest__", 0, CERTIFICATE).then((_) { |
| 35 Expect.fail("Failure expected"); | 34 Expect.fail("Failure expected"); |
| 36 }).catchError((error) { | 35 }).catchError((error) { |
| 37 Expect.isTrue(error is SocketException); | 36 Expect.isTrue(error is SocketException); |
| 38 asyncEnd(); | 37 asyncEnd(); |
| 39 }); | 38 }); |
| 40 | 39 |
| 41 // Bind to an unavaliable IP-address. | 40 // Bind to an unavaliable IP-address. |
| 42 asyncStart(); | 41 asyncStart(); |
| 43 RawSecureServerSocket.bind("8.8.8.8", 0, CERTIFICATE).then((_) { | 42 RawSecureServerSocket.bind("8.8.8.8", 0, CERTIFICATE).then((_) { |
| 44 Expect.fail("Failure expected"); | 43 Expect.fail("Failure expected"); |
| 45 }).catchError((error) { | 44 }).catchError((error) { |
| 46 Expect.isTrue(error is SocketException); | 45 Expect.isTrue(error is SocketException); |
| 47 asyncEnd(); | 46 asyncEnd(); |
| 48 }); | 47 }); |
| 49 | 48 |
| 50 // Bind to a port already in use. | 49 // Bind to a port already in use. |
| 51 asyncStart(); | 50 asyncStart(); |
| 52 RawSecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((s) { | 51 RawSecureServerSocket.bind(HOST, 0, CERTIFICATE).then((s) { |
| 53 RawSecureServerSocket.bind(HOST_NAME, | 52 RawSecureServerSocket.bind(HOST, |
| 54 s.port, | 53 s.port, |
| 55 CERTIFICATE).then((t) { | 54 CERTIFICATE).then((t) { |
| 56 s.close(); | 55 s.close(); |
| 57 t.close(); | 56 t.close(); |
| 58 Expect.fail("Multiple listens on same port"); | 57 Expect.fail("Multiple listens on same port"); |
| 59 }) | 58 }) |
| 60 .catchError((error) { | 59 .catchError((error) { |
| 61 Expect.isTrue(error is SocketException); | 60 Expect.isTrue(error is SocketException); |
| 62 s.close(); | 61 s.close(); |
| 63 asyncEnd(); | 62 asyncEnd(); |
| 64 }); | 63 }); |
| 65 }); | 64 }); |
| 66 } | 65 } |
| 67 | 66 |
| 68 void testSimpleConnect(String certificate) { | 67 void testSimpleConnect(String certificate) { |
| 69 asyncStart(); | 68 asyncStart(); |
| 70 RawSecureServerSocket.bind(HOST_NAME, 0, certificate).then((server) { | 69 RawSecureServerSocket.bind(HOST, 0, certificate).then((server) { |
| 71 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); | 70 var clientEndFuture = RawSecureSocket.connect(HOST, server.port); |
| 72 server.listen((serverEnd) { | 71 server.listen((serverEnd) { |
| 73 clientEndFuture.then((clientEnd) { | 72 clientEndFuture.then((clientEnd) { |
| 74 clientEnd.shutdown(SocketDirection.SEND); | 73 clientEnd.shutdown(SocketDirection.SEND); |
| 75 serverEnd.shutdown(SocketDirection.SEND); | 74 serverEnd.shutdown(SocketDirection.SEND); |
| 76 server.close(); | 75 server.close(); |
| 77 asyncEnd(); | 76 asyncEnd(); |
| 78 }); | 77 }); |
| 79 }); | 78 }); |
| 80 }); | 79 }); |
| 81 } | 80 } |
| 82 | 81 |
| 83 void testSimpleConnectFail(String certificate, bool cancelOnError) { | 82 void testSimpleConnectFail(String certificate, bool cancelOnError) { |
| 84 asyncStart(); | 83 asyncStart(); |
| 85 RawSecureServerSocket.bind(HOST_NAME, 0, certificate).then((server) { | 84 RawSecureServerSocket.bind(HOST, 0, certificate).then((server) { |
| 86 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port) | 85 var clientEndFuture = RawSecureSocket.connect(HOST, server.port) |
| 87 .then((clientEnd) { | 86 .then((clientEnd) { |
| 88 Expect.fail("No client connection expected."); | 87 Expect.fail("No client connection expected."); |
| 89 }) | 88 }) |
| 90 .catchError((error) { | 89 .catchError((error) { |
| 91 Expect.isTrue(error is SocketException || | 90 Expect.isTrue(error is SocketException || |
| 92 error is HandshakeException); | 91 error is HandshakeException); |
| 93 }); | 92 }); |
| 94 server.listen((serverEnd) { | 93 server.listen((serverEnd) { |
| 95 Expect.fail("No server connection expected."); | 94 Expect.fail("No server connection expected."); |
| 96 }, | 95 }, |
| 97 onError: (error) { | 96 onError: (error) { |
| 98 Expect.isTrue(error is CertificateException); | 97 Expect.isTrue(error is CertificateException); |
| 99 clientEndFuture.then((_) { | 98 clientEndFuture.then((_) { |
| 100 if (!cancelOnError) server.close(); | 99 if (!cancelOnError) server.close(); |
| 101 asyncEnd(); | 100 asyncEnd(); |
| 102 }); | 101 }); |
| 103 }, | 102 }, |
| 104 cancelOnError: cancelOnError); | 103 cancelOnError: cancelOnError); |
| 105 }); | 104 }); |
| 106 } | 105 } |
| 107 | 106 |
| 108 void testServerListenAfterConnect() { | 107 void testServerListenAfterConnect() { |
| 109 asyncStart(); | 108 asyncStart(); |
| 110 RawSecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((server) { | 109 RawSecureServerSocket.bind(HOST, 0, CERTIFICATE).then((server) { |
| 111 Expect.isTrue(server.port > 0); | 110 Expect.isTrue(server.port > 0); |
| 112 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); | 111 var clientEndFuture = RawSecureSocket.connect(HOST, server.port); |
| 113 new Timer(const Duration(milliseconds: 500), () { | 112 new Timer(const Duration(milliseconds: 500), () { |
| 114 server.listen((serverEnd) { | 113 server.listen((serverEnd) { |
| 115 clientEndFuture.then((clientEnd) { | 114 clientEndFuture.then((clientEnd) { |
| 116 clientEnd.shutdown(SocketDirection.SEND); | 115 clientEnd.shutdown(SocketDirection.SEND); |
| 117 serverEnd.shutdown(SocketDirection.SEND); | 116 serverEnd.shutdown(SocketDirection.SEND); |
| 118 server.close(); | 117 server.close(); |
| 119 asyncEnd(); | 118 asyncEnd(); |
| 120 }); | 119 }); |
| 121 }); | 120 }); |
| 122 }); | 121 }); |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 Expect.fail("Unexpected close"); | 415 Expect.fail("Unexpected close"); |
| 417 break; | 416 break; |
| 418 default: throw "Unexpected event $event"; | 417 default: throw "Unexpected event $event"; |
| 419 } | 418 } |
| 420 }); | 419 }); |
| 421 return completer.future; | 420 return completer.future; |
| 422 } | 421 } |
| 423 | 422 |
| 424 Future<RawSecureSocket> connectClient(int port) { | 423 Future<RawSecureSocket> connectClient(int port) { |
| 425 if (connectSecure) { | 424 if (connectSecure) { |
| 426 return RawSecureSocket.connect(HOST_NAME, port); | 425 return RawSecureSocket.connect(HOST, port); |
| 427 } else if (!handshakeBeforeSecure) { | 426 } else if (!handshakeBeforeSecure) { |
| 428 return RawSocket.connect(HOST_NAME, port).then((socket) { | 427 return RawSocket.connect(HOST, port).then((socket) { |
| 429 return RawSecureSocket.secure(socket); | 428 return RawSecureSocket.secure(socket); |
| 430 }); | 429 }); |
| 431 } else { | 430 } else { |
| 432 return RawSocket.connect(HOST_NAME, port).then((socket) { | 431 return RawSocket.connect(HOST, port).then((socket) { |
| 433 return runClientHandshake(socket).then((subscription) { | 432 return runClientHandshake(socket).then((subscription) { |
| 434 return RawSecureSocket.secure(socket, subscription: subscription); | 433 return RawSecureSocket.secure(socket, subscription: subscription); |
| 435 }); | 434 }); |
| 436 }); | 435 }); |
| 437 } | 436 } |
| 438 } | 437 } |
| 439 | 438 |
| 440 serverReady(server) { | 439 serverReady(server) { |
| 441 server.listen((client) { | 440 server.listen((client) { |
| 442 if (listenSecure) { | 441 if (listenSecure) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 459 }); | 458 }); |
| 460 | 459 |
| 461 connectClient(server.port).then(runClient).then((socket) { | 460 connectClient(server.port).then(runClient).then((socket) { |
| 462 socket.close(); | 461 socket.close(); |
| 463 asyncEnd(); | 462 asyncEnd(); |
| 464 }); | 463 }); |
| 465 } | 464 } |
| 466 | 465 |
| 467 if (listenSecure) { | 466 if (listenSecure) { |
| 468 RawSecureServerSocket.bind( | 467 RawSecureServerSocket.bind( |
| 469 HOST_NAME, 0, CERTIFICATE).then(serverReady); | 468 HOST, 0, CERTIFICATE).then(serverReady); |
| 470 } else { | 469 } else { |
| 471 RawServerSocket.bind(HOST_NAME, 0).then(serverReady); | 470 RawServerSocket.bind(HOST, 0).then(serverReady); |
| 472 } | 471 } |
| 473 } | 472 } |
| 474 | 473 |
| 475 testPausedSecuringSubscription(bool pausedServer, bool pausedClient) { | 474 testPausedSecuringSubscription(bool pausedServer, bool pausedClient) { |
| 476 bool expectFail = pausedServer || pausedClient; | 475 bool expectFail = pausedServer || pausedClient; |
| 477 | 476 |
| 478 asyncStart(); | 477 asyncStart(); |
| 479 var clientComplete = new Completer(); | 478 var clientComplete = new Completer(); |
| 480 RawServerSocket.bind(HOST_NAME, 0).then((server) { | 479 RawServerSocket.bind(HOST, 0).then((server) { |
| 481 server.listen((client) { | 480 server.listen((client) { |
| 482 var subscription; | 481 var subscription; |
| 483 subscription = client.listen((_) { | 482 subscription = client.listen((_) { |
| 484 if (pausedServer) { | 483 if (pausedServer) { |
| 485 subscription.pause(); | 484 subscription.pause(); |
| 486 } | 485 } |
| 487 RawSecureSocket.secureServer( | 486 RawSecureSocket.secureServer( |
| 488 client, CERTIFICATE, subscription: subscription).then((client) { | 487 client, CERTIFICATE, subscription: subscription).then((client) { |
| 489 if (expectFail) { | 488 if (expectFail) { |
| 490 Expect.fail("secureServer succeeded with paused subscription"); | 489 Expect.fail("secureServer succeeded with paused subscription"); |
| 491 } | 490 } |
| 492 }).catchError((e) { | 491 }).catchError((e) { |
| 493 if (!expectFail) { | 492 if (!expectFail) { |
| 494 Expect.fail("secureServer failed with non-paused subscriptions"); | 493 Expect.fail("secureServer failed with non-paused subscriptions"); |
| 495 } | 494 } |
| 496 if (pausedServer) { | 495 if (pausedServer) { |
| 497 Expect.isTrue(e is StateError); | 496 Expect.isTrue(e is StateError); |
| 498 } | 497 } |
| 499 }).whenComplete(() { | 498 }).whenComplete(() { |
| 500 server.close(); | 499 server.close(); |
| 501 clientComplete.future.then((_) { | 500 clientComplete.future.then((_) { |
| 502 client.close(); | 501 client.close(); |
| 503 asyncEnd(); | 502 asyncEnd(); |
| 504 }); | 503 }); |
| 505 }); | 504 }); |
| 506 }); | 505 }); |
| 507 }); | 506 }); |
| 508 | 507 |
| 509 RawSocket.connect(HOST_NAME, server.port).then((socket) { | 508 RawSocket.connect(HOST, server.port).then((socket) { |
| 510 var subscription; | 509 var subscription; |
| 511 subscription = socket.listen((_) { | 510 subscription = socket.listen((_) { |
| 512 if (pausedClient) { | 511 if (pausedClient) { |
| 513 subscription.pause(); | 512 subscription.pause(); |
| 514 } | 513 } |
| 515 RawSecureSocket.secure( | 514 RawSecureSocket.secure( |
| 516 socket, subscription: subscription).then((socket) { | 515 socket, subscription: subscription).then((socket) { |
| 517 if (expectFail) { | 516 if (expectFail) { |
| 518 Expect.fail("secure succeeded with paused subscription"); | 517 Expect.fail("secure succeeded with paused subscription"); |
| 519 } | 518 } |
| 520 socket.close(); | 519 socket.close(); |
| 521 }).catchError((e) { | 520 }).catchError((e) { |
| 522 if (!expectFail) { | 521 if (!expectFail) { |
| 523 Expect.fail("secure failed with non-paused subscriptions ($e)"); | 522 Expect.fail("secure failed with non-paused subscriptions ($e)"); |
| 524 } | 523 } |
| 525 if (pausedClient) { | 524 if (pausedClient) { |
| 526 Expect.isTrue(e is StateError); | 525 Expect.isTrue(e is StateError); |
| 527 } | 526 } |
| 528 }).whenComplete(() { | 527 }).whenComplete(() { |
| 529 clientComplete.complete(null); | 528 clientComplete.complete(null); |
| 530 }); | 529 }); |
| 531 }); | 530 }); |
| 532 }); | 531 }); |
| 533 }); | 532 }); |
| 534 } | 533 } |
| 535 | 534 |
| 536 main() { | 535 main() { |
| 536 asyncStart(); |
| 537 var certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); | 537 var certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); |
| 538 SecureSocket.initialize(database: certificateDatabase, | 538 SecureSocket.initialize(database: certificateDatabase, |
| 539 password: 'dartdart', | 539 password: 'dartdart', |
| 540 useBuiltinRoots: false); | 540 useBuiltinRoots: false); |
| 541 InternetAddress.lookup("localhost").then((hosts) { |
| 542 HOST = hosts.first; |
| 543 runTests(); |
| 544 asyncEnd(); |
| 545 }); |
| 546 } |
| 547 |
| 548 runTests() { |
| 541 testSimpleBind(); | 549 testSimpleBind(); |
| 542 testInvalidBind(); | 550 testInvalidBind(); |
| 543 testSimpleConnect(CERTIFICATE); | 551 testSimpleConnect(CERTIFICATE); |
| 544 testSimpleConnect("CN=localhost"); | 552 testSimpleConnect("CN=localhost"); |
| 545 testSimpleConnectFail("not_a_nickname", false); | 553 testSimpleConnectFail("not_a_nickname", false); |
| 546 testSimpleConnectFail("CN=notARealDistinguishedName", false); | 554 testSimpleConnectFail("CN=notARealDistinguishedName", false); |
| 547 testSimpleConnectFail("not_a_nickname", true); | 555 testSimpleConnectFail("not_a_nickname", true); |
| 548 testSimpleConnectFail("CN=notARealDistinguishedName", true); | 556 testSimpleConnectFail("CN=notARealDistinguishedName", true); |
| 549 testServerListenAfterConnect(); | 557 testServerListenAfterConnect(); |
| 550 testSimpleReadWrite(listenSecure: true, | 558 testSimpleReadWrite(listenSecure: true, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 testSimpleReadWrite(listenSecure: false, | 593 testSimpleReadWrite(listenSecure: false, |
| 586 connectSecure: false, | 594 connectSecure: false, |
| 587 handshakeBeforeSecure: true, | 595 handshakeBeforeSecure: true, |
| 588 postponeSecure: true, | 596 postponeSecure: true, |
| 589 dropReads: true); | 597 dropReads: true); |
| 590 testPausedSecuringSubscription(false, false); | 598 testPausedSecuringSubscription(false, false); |
| 591 testPausedSecuringSubscription(true, false); | 599 testPausedSecuringSubscription(true, false); |
| 592 testPausedSecuringSubscription(false, true); | 600 testPausedSecuringSubscription(false, true); |
| 593 testPausedSecuringSubscription(true, true); | 601 testPausedSecuringSubscription(true, true); |
| 594 } | 602 } |
| OLD | NEW |