| 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 SERVER_ADDRESS = "127.0.0.1"; |
| 15 const HOST_NAME = "localhost"; | 16 const HOST_NAME = "localhost"; |
| 16 const CERTIFICATE = "localhost_cert"; | 17 const CERTIFICATE = "localhost_cert"; |
| 17 | 18 |
| 18 void testArguments() { | 19 void testArguments() { |
| 19 Expect.throws(() => | 20 Expect.throws(() => |
| 20 RawSecureServerSocket.bind(HOST_NAME, 65536, 5, CERTIFICATE)); | 21 RawSecureServerSocket.bind(SERVER_ADDRESS, 65536, 5, CERTIFICATE)); |
| 21 Expect.throws(() => | 22 Expect.throws(() => |
| 22 RawSecureServerSocket.bind(HOST_NAME, -1, CERTIFICATE)); | 23 RawSecureServerSocket.bind(SERVER_ADDRESS, -1, CERTIFICATE)); |
| 23 Expect.throws(() => | 24 Expect.throws(() => |
| 24 RawSecureServerSocket.bind(HOST_NAME, 0, -1, CERTIFICATE)); | 25 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, -1, CERTIFICATE)); |
| 25 } | 26 } |
| 26 | 27 |
| 27 void testSimpleBind() { | 28 void testSimpleBind() { |
| 28 ReceivePort port = new ReceivePort(); | 29 ReceivePort port = new ReceivePort(); |
| 29 RawSecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((s) { | 30 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((s) { |
| 30 Expect.isTrue(s.port > 0); | 31 Expect.isTrue(s.port > 0); |
| 31 s.close(); | 32 s.close(); |
| 32 port.close(); | 33 port.close(); |
| 33 }); | 34 }); |
| 34 } | 35 } |
| 35 | 36 |
| 36 void testInvalidBind() { | 37 void testInvalidBind() { |
| 37 int count = 0; | 38 int count = 0; |
| 38 ReceivePort port = new ReceivePort(); | 39 ReceivePort port = new ReceivePort(); |
| 39 port.receive((_, __) { count++; if (count == 3) port.close(); }); | 40 port.receive((_, __) { count++; if (count == 3) port.close(); }); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 51 Expect.fail("Failure expected"); | 52 Expect.fail("Failure expected"); |
| 52 }).catchError((error) { | 53 }).catchError((error) { |
| 53 Expect.isTrue(error is SocketIOException); | 54 Expect.isTrue(error is SocketIOException); |
| 54 port.toSendPort().send(1); | 55 port.toSendPort().send(1); |
| 55 }); | 56 }); |
| 56 | 57 |
| 57 // Bind to a port already in use. | 58 // Bind to a port already in use. |
| 58 // Either an error or a successful bind is allowed. | 59 // Either an error or a successful bind is allowed. |
| 59 // Windows platforms allow multiple binding to the same socket, with | 60 // Windows platforms allow multiple binding to the same socket, with |
| 60 // unpredictable results. | 61 // unpredictable results. |
| 61 RawSecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((s) { | 62 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((s) { |
| 62 RawSecureServerSocket.bind(HOST_NAME, | 63 RawSecureServerSocket.bind(SERVER_ADDRESS, |
| 63 s.port, | 64 s.port, |
| 64 5, | 65 5, |
| 65 CERTIFICATE).then((t) { | 66 CERTIFICATE).then((t) { |
| 66 Expect.equals('windows', Platform.operatingSystem); | 67 Expect.equals('windows', Platform.operatingSystem); |
| 67 Expect.equals(s.port, t.port); | 68 Expect.equals(s.port, t.port); |
| 68 s.close(); | 69 s.close(); |
| 69 t.close(); | 70 t.close(); |
| 70 port.toSendPort().send(1); | 71 port.toSendPort().send(1); |
| 71 }) | 72 }) |
| 72 .catchError((error) { | 73 .catchError((error) { |
| 73 Expect.notEquals('windows', Platform.operatingSystem); | 74 Expect.notEquals('windows', Platform.operatingSystem); |
| 74 Expect.isTrue(error is SocketIOException); | 75 Expect.isTrue(error is SocketIOException); |
| 75 s.close(); | 76 s.close(); |
| 76 port.toSendPort().send(1); | 77 port.toSendPort().send(1); |
| 77 }); | 78 }); |
| 78 }); | 79 }); |
| 79 } | 80 } |
| 80 | 81 |
| 81 void testSimpleConnect(String certificate) { | 82 void testSimpleConnect(String certificate) { |
| 82 ReceivePort port = new ReceivePort(); | 83 ReceivePort port = new ReceivePort(); |
| 83 RawSecureServerSocket.bind(HOST_NAME, 0, 5, certificate).then((server) { | 84 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, certificate).then((server) { |
| 84 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); | 85 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); |
| 85 server.listen((serverEnd) { | 86 server.listen((serverEnd) { |
| 86 clientEndFuture.then((clientEnd) { | 87 clientEndFuture.then((clientEnd) { |
| 87 clientEnd.shutdown(SocketDirection.SEND); | 88 clientEnd.shutdown(SocketDirection.SEND); |
| 88 serverEnd.shutdown(SocketDirection.SEND); | 89 serverEnd.shutdown(SocketDirection.SEND); |
| 89 server.close(); | 90 server.close(); |
| 90 port.close(); | 91 port.close(); |
| 91 }); | 92 }); |
| 92 }); | 93 }); |
| 93 }); | 94 }); |
| 94 } | 95 } |
| 95 | 96 |
| 96 void testSimpleConnectFail(String certificate) { | 97 void testSimpleConnectFail(String certificate) { |
| 97 ReceivePort port = new ReceivePort(); | 98 ReceivePort port = new ReceivePort(); |
| 98 RawSecureServerSocket.bind(HOST_NAME, 0, 5, certificate).then((server) { | 99 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, certificate).then((server) { |
| 99 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port) | 100 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port) |
| 100 .then((clientEnd) { | 101 .then((clientEnd) { |
| 101 Expect.fail("No client connection expected."); | 102 Expect.fail("No client connection expected."); |
| 102 }) | 103 }) |
| 103 .catchError((error) { | 104 .catchError((error) { |
| 104 Expect.isTrue(error is SocketIOException); | 105 Expect.isTrue(error is SocketIOException); |
| 105 }); | 106 }); |
| 106 server.listen((serverEnd) { | 107 server.listen((serverEnd) { |
| 107 Expect.fail("No server connection expected."); | 108 Expect.fail("No server connection expected."); |
| 108 }, | 109 }, |
| 109 onError: (error) { | 110 onError: (error) { |
| 110 Expect.isTrue(error is SocketIOException); | 111 Expect.isTrue(error is SocketIOException); |
| 111 clientEndFuture.then((_) => port.close()); | 112 clientEndFuture.then((_) => port.close()); |
| 112 }); | 113 }); |
| 113 }); | 114 }); |
| 114 } | 115 } |
| 115 | 116 |
| 116 void testServerListenAfterConnect() { | 117 void testServerListenAfterConnect() { |
| 117 ReceivePort port = new ReceivePort(); | 118 ReceivePort port = new ReceivePort(); |
| 118 RawSecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((server) { | 119 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { |
| 119 Expect.isTrue(server.port > 0); | 120 Expect.isTrue(server.port > 0); |
| 120 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); | 121 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); |
| 121 new Timer(const Duration(milliseconds: 500), () { | 122 new Timer(const Duration(milliseconds: 500), () { |
| 122 server.listen((serverEnd) { | 123 server.listen((serverEnd) { |
| 123 clientEndFuture.then((clientEnd) { | 124 clientEndFuture.then((clientEnd) { |
| 124 clientEnd.shutdown(SocketDirection.SEND); | 125 clientEnd.shutdown(SocketDirection.SEND); |
| 125 serverEnd.shutdown(SocketDirection.SEND); | 126 serverEnd.shutdown(SocketDirection.SEND); |
| 126 server.close(); | 127 server.close(); |
| 127 port.close(); | 128 port.close(); |
| 128 }); | 129 }); |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 }); | 434 }); |
| 434 | 435 |
| 435 connectClient(server.port).then(runClient).then((socket) { | 436 connectClient(server.port).then(runClient).then((socket) { |
| 436 socket.close(); | 437 socket.close(); |
| 437 port.close(); | 438 port.close(); |
| 438 }); | 439 }); |
| 439 } | 440 } |
| 440 | 441 |
| 441 if (listenSecure) { | 442 if (listenSecure) { |
| 442 RawSecureServerSocket.bind( | 443 RawSecureServerSocket.bind( |
| 443 HOST_NAME, 0, 5, CERTIFICATE).then(serverReady); | 444 SERVER_ADDRESS, 0, 5, CERTIFICATE).then(serverReady); |
| 444 } else { | 445 } else { |
| 445 RawServerSocket.bind(HOST_NAME, 0, 5).then(serverReady); | 446 RawServerSocket.bind(SERVER_ADDRESS, 0, 5).then(serverReady); |
| 446 } | 447 } |
| 447 } | 448 } |
| 448 | 449 |
| 449 main() { | 450 main() { |
| 450 Path scriptDir = new Path(new Options().script).directoryPath; | 451 Path scriptDir = new Path(new Options().script).directoryPath; |
| 451 Path certificateDatabase = scriptDir.append('pkcert'); | 452 Path certificateDatabase = scriptDir.append('pkcert'); |
| 452 SecureSocket.initialize(database: certificateDatabase.toNativePath(), | 453 SecureSocket.initialize(database: certificateDatabase.toNativePath(), |
| 453 password: 'dartdart', | 454 password: 'dartdart', |
| 454 useBuiltinRoots: false); | 455 useBuiltinRoots: false); |
| 455 testArguments(); | 456 testArguments(); |
| 456 testSimpleBind(); | 457 testSimpleBind(); |
| 457 testInvalidBind(); | 458 testInvalidBind(); |
| 458 testSimpleConnect(CERTIFICATE); | 459 testSimpleConnect(CERTIFICATE); |
| 459 testSimpleConnect("CN=localhost"); | 460 testSimpleConnect("CN=localhost"); |
| 460 testSimpleConnectFail("not_a_nickname"); | 461 testSimpleConnectFail("not_a_nickname"); |
| 461 testSimpleConnectFail("CN=notARealDistinguishedName"); | 462 testSimpleConnectFail("CN=notARealDistinguishedName"); |
| 462 testServerListenAfterConnect(); | 463 testServerListenAfterConnect(); |
| 463 testSimpleReadWrite(true, true, false); | 464 testSimpleReadWrite(true, true, false); |
| 464 testSimpleReadWrite(true, false, false); | 465 testSimpleReadWrite(true, false, false); |
| 465 testSimpleReadWrite(false, true, false); | 466 testSimpleReadWrite(false, true, false); |
| 466 testSimpleReadWrite(false, false, false); | 467 testSimpleReadWrite(false, false, false); |
| 467 testSimpleReadWrite(false, false, true, true); | 468 testSimpleReadWrite(false, false, true, true); |
| 468 testSimpleReadWrite(false, false, true, false); | 469 testSimpleReadWrite(false, false, true, false); |
| 469 } | 470 } |
| OLD | NEW |