| 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 "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
| 11 import "dart:async"; | 11 import "dart:async"; |
| 12 import "dart:io"; | 12 import "dart:io"; |
| 13 import "dart:isolate"; | 13 import "dart:isolate"; |
| 14 | 14 |
| 15 const HOST_NAME = "localhost"; | 15 const HOST_NAME = "localhost"; |
| 16 const CERTIFICATE = "localhost_cert"; | 16 const CERTIFICATE = "localhost_cert"; |
| 17 | 17 |
| 18 void testSimpleBind() { | 18 void testSimpleBind() { |
| 19 ReceivePort port = new ReceivePort(); | 19 ReceivePort port = new ReceivePort(); |
| 20 RawSecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((s) { | 20 RawSecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((s) { |
| 21 Expect.isTrue(s.port > 0); | 21 Expect.isTrue(s.port > 0); |
| 22 s.close(); | 22 s.close(); |
| 23 port.close(); | 23 port.close(); |
| 24 }); | 24 }); |
| 25 } | 25 } |
| 26 | 26 |
| 27 void testInvalidBind() { | 27 void testInvalidBind() { |
| 28 int count = 0; | 28 int count = 0; |
| 29 ReceivePort port = new ReceivePort(); | 29 ReceivePort port = new ReceivePort(); |
| 30 port.receive((_, __) { count++; if (count == 3) port.close(); }); | 30 port.receive((_, __) { count++; if (count == 3) port.close(); }); |
| 31 | 31 |
| 32 // Bind to a unknown DNS name. | 32 // Bind to a unknown DNS name. |
| 33 RawSecureServerSocket.bind("ko.faar.__hest__", 0, 5, CERTIFICATE).then((_) { | 33 RawSecureServerSocket.bind("ko.faar.__hest__", 0, CERTIFICATE).then((_) { |
| 34 Expect.fail("Failure expected"); | 34 Expect.fail("Failure expected"); |
| 35 }).catchError((error) { | 35 }).catchError((error) { |
| 36 Expect.isTrue(error is SocketIOException); | 36 Expect.isTrue(error is SocketIOException); |
| 37 port.toSendPort().send(1); | 37 port.toSendPort().send(1); |
| 38 }); | 38 }); |
| 39 | 39 |
| 40 // Bind to an unavaliable IP-address. | 40 // Bind to an unavaliable IP-address. |
| 41 RawSecureServerSocket.bind("8.8.8.8", 0, 5, CERTIFICATE).then((_) { | 41 RawSecureServerSocket.bind("8.8.8.8", 0, CERTIFICATE).then((_) { |
| 42 Expect.fail("Failure expected"); | 42 Expect.fail("Failure expected"); |
| 43 }).catchError((error) { | 43 }).catchError((error) { |
| 44 Expect.isTrue(error is SocketIOException); | 44 Expect.isTrue(error is SocketIOException); |
| 45 port.toSendPort().send(1); | 45 port.toSendPort().send(1); |
| 46 }); | 46 }); |
| 47 | 47 |
| 48 // Bind to a port already in use. | 48 // Bind to a port already in use. |
| 49 // Either an error or a successful bind is allowed. | 49 // Either an error or a successful bind is allowed. |
| 50 // Windows platforms allow multiple binding to the same socket, with | 50 // Windows platforms allow multiple binding to the same socket, with |
| 51 // unpredictable results. | 51 // unpredictable results. |
| 52 RawSecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((s) { | 52 RawSecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((s) { |
| 53 RawSecureServerSocket.bind(HOST_NAME, | 53 RawSecureServerSocket.bind(HOST_NAME, |
| 54 s.port, | 54 s.port, |
| 55 5, | |
| 56 CERTIFICATE).then((t) { | 55 CERTIFICATE).then((t) { |
| 57 Expect.equals('windows', Platform.operatingSystem); | 56 Expect.equals('windows', Platform.operatingSystem); |
| 58 Expect.equals(s.port, t.port); | 57 Expect.equals(s.port, t.port); |
| 59 s.close(); | 58 s.close(); |
| 60 t.close(); | 59 t.close(); |
| 61 port.toSendPort().send(1); | 60 port.toSendPort().send(1); |
| 62 }) | 61 }) |
| 63 .catchError((error) { | 62 .catchError((error) { |
| 64 Expect.notEquals('windows', Platform.operatingSystem); | 63 Expect.notEquals('windows', Platform.operatingSystem); |
| 65 Expect.isTrue(error is SocketIOException); | 64 Expect.isTrue(error is SocketIOException); |
| 66 s.close(); | 65 s.close(); |
| 67 port.toSendPort().send(1); | 66 port.toSendPort().send(1); |
| 68 }); | 67 }); |
| 69 }); | 68 }); |
| 70 } | 69 } |
| 71 | 70 |
| 72 void testSimpleConnect(String certificate) { | 71 void testSimpleConnect(String certificate) { |
| 73 ReceivePort port = new ReceivePort(); | 72 ReceivePort port = new ReceivePort(); |
| 74 RawSecureServerSocket.bind(HOST_NAME, 0, 5, certificate).then((server) { | 73 RawSecureServerSocket.bind(HOST_NAME, 0, certificate).then((server) { |
| 75 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); | 74 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); |
| 76 server.listen((serverEnd) { | 75 server.listen((serverEnd) { |
| 77 clientEndFuture.then((clientEnd) { | 76 clientEndFuture.then((clientEnd) { |
| 78 clientEnd.shutdown(SocketDirection.SEND); | 77 clientEnd.shutdown(SocketDirection.SEND); |
| 79 serverEnd.shutdown(SocketDirection.SEND); | 78 serverEnd.shutdown(SocketDirection.SEND); |
| 80 server.close(); | 79 server.close(); |
| 81 port.close(); | 80 port.close(); |
| 82 }); | 81 }); |
| 83 }); | 82 }); |
| 84 }); | 83 }); |
| 85 } | 84 } |
| 86 | 85 |
| 87 void testSimpleConnectFail(String certificate) { | 86 void testSimpleConnectFail(String certificate) { |
| 88 ReceivePort port = new ReceivePort(); | 87 ReceivePort port = new ReceivePort(); |
| 89 RawSecureServerSocket.bind(HOST_NAME, 0, 5, certificate).then((server) { | 88 RawSecureServerSocket.bind(HOST_NAME, 0, certificate).then((server) { |
| 90 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port) | 89 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port) |
| 91 .then((clientEnd) { | 90 .then((clientEnd) { |
| 92 Expect.fail("No client connection expected."); | 91 Expect.fail("No client connection expected."); |
| 93 }) | 92 }) |
| 94 .catchError((error) { | 93 .catchError((error) { |
| 95 Expect.isTrue(error is SocketIOException); | 94 Expect.isTrue(error is SocketIOException); |
| 96 }); | 95 }); |
| 97 server.listen((serverEnd) { | 96 server.listen((serverEnd) { |
| 98 Expect.fail("No server connection expected."); | 97 Expect.fail("No server connection expected."); |
| 99 }, | 98 }, |
| 100 onError: (error) { | 99 onError: (error) { |
| 101 Expect.isTrue(error is SocketIOException); | 100 Expect.isTrue(error is SocketIOException); |
| 102 clientEndFuture.then((_) => port.close()); | 101 clientEndFuture.then((_) => port.close()); |
| 103 }); | 102 }); |
| 104 }); | 103 }); |
| 105 } | 104 } |
| 106 | 105 |
| 107 void testServerListenAfterConnect() { | 106 void testServerListenAfterConnect() { |
| 108 ReceivePort port = new ReceivePort(); | 107 ReceivePort port = new ReceivePort(); |
| 109 RawSecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((server) { | 108 RawSecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((server) { |
| 110 Expect.isTrue(server.port > 0); | 109 Expect.isTrue(server.port > 0); |
| 111 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); | 110 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); |
| 112 new Timer(const Duration(milliseconds: 500), () { | 111 new Timer(const Duration(milliseconds: 500), () { |
| 113 server.listen((serverEnd) { | 112 server.listen((serverEnd) { |
| 114 clientEndFuture.then((clientEnd) { | 113 clientEndFuture.then((clientEnd) { |
| 115 clientEnd.shutdown(SocketDirection.SEND); | 114 clientEnd.shutdown(SocketDirection.SEND); |
| 116 serverEnd.shutdown(SocketDirection.SEND); | 115 serverEnd.shutdown(SocketDirection.SEND); |
| 117 server.close(); | 116 server.close(); |
| 118 port.close(); | 117 port.close(); |
| 119 }); | 118 }); |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 }); | 422 }); |
| 424 | 423 |
| 425 connectClient(server.port).then(runClient).then((socket) { | 424 connectClient(server.port).then(runClient).then((socket) { |
| 426 socket.close(); | 425 socket.close(); |
| 427 port.close(); | 426 port.close(); |
| 428 }); | 427 }); |
| 429 } | 428 } |
| 430 | 429 |
| 431 if (listenSecure) { | 430 if (listenSecure) { |
| 432 RawSecureServerSocket.bind( | 431 RawSecureServerSocket.bind( |
| 433 HOST_NAME, 0, 5, CERTIFICATE).then(serverReady); | 432 HOST_NAME, 0, CERTIFICATE).then(serverReady); |
| 434 } else { | 433 } else { |
| 435 RawServerSocket.bind(HOST_NAME, 0, 5).then(serverReady); | 434 RawServerSocket.bind(HOST_NAME, 0).then(serverReady); |
| 436 } | 435 } |
| 437 } | 436 } |
| 438 | 437 |
| 439 main() { | 438 main() { |
| 440 Path scriptDir = new Path(new Options().script).directoryPath; | 439 Path scriptDir = new Path(new Options().script).directoryPath; |
| 441 Path certificateDatabase = scriptDir.append('pkcert'); | 440 Path certificateDatabase = scriptDir.append('pkcert'); |
| 442 SecureSocket.initialize(database: certificateDatabase.toNativePath(), | 441 SecureSocket.initialize(database: certificateDatabase.toNativePath(), |
| 443 password: 'dartdart', | 442 password: 'dartdart', |
| 444 useBuiltinRoots: false); | 443 useBuiltinRoots: false); |
| 445 testSimpleBind(); | 444 testSimpleBind(); |
| 446 testInvalidBind(); | 445 testInvalidBind(); |
| 447 testSimpleConnect(CERTIFICATE); | 446 testSimpleConnect(CERTIFICATE); |
| 448 testSimpleConnect("CN=localhost"); | 447 testSimpleConnect("CN=localhost"); |
| 449 testSimpleConnectFail("not_a_nickname"); | 448 testSimpleConnectFail("not_a_nickname"); |
| 450 testSimpleConnectFail("CN=notARealDistinguishedName"); | 449 testSimpleConnectFail("CN=notARealDistinguishedName"); |
| 451 testServerListenAfterConnect(); | 450 testServerListenAfterConnect(); |
| 452 testSimpleReadWrite(true, true, false); | 451 testSimpleReadWrite(true, true, false); |
| 453 testSimpleReadWrite(true, false, false); | 452 testSimpleReadWrite(true, false, false); |
| 454 testSimpleReadWrite(false, true, false); | 453 testSimpleReadWrite(false, true, false); |
| 455 testSimpleReadWrite(false, false, false); | 454 testSimpleReadWrite(false, false, false); |
| 456 testSimpleReadWrite(false, false, true, true); | 455 testSimpleReadWrite(false, false, true, true); |
| 457 testSimpleReadWrite(false, false, true, false); | 456 testSimpleReadWrite(false, false, true, false); |
| 458 } | 457 } |
| OLD | NEW |