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