| 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 SecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((s) { | 20 SecureServerSocket.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 SecureServerSocket.bind("ko.faar.__hest__", 0, 5, CERTIFICATE).then((_) { | 33 SecureServerSocket.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 SecureServerSocket.bind("8.8.8.8", 0, 5, CERTIFICATE).then((_) { | 41 SecureServerSocket.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 SecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((s) { | 52 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((s) { |
| 53 SecureServerSocket.bind(HOST_NAME, | 53 SecureServerSocket.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 }).catchError((error) { | 61 }).catchError((error) { |
| 63 Expect.notEquals('windows', Platform.operatingSystem); | 62 Expect.notEquals('windows', Platform.operatingSystem); |
| 64 Expect.isTrue(error is SocketIOException); | 63 Expect.isTrue(error is SocketIOException); |
| 65 s.close(); | 64 s.close(); |
| 66 port.toSendPort().send(1); | 65 port.toSendPort().send(1); |
| 67 }); | 66 }); |
| 68 }); | 67 }); |
| 69 } | 68 } |
| 70 | 69 |
| 71 void testSimpleConnect(String certificate) { | 70 void testSimpleConnect(String certificate) { |
| 72 ReceivePort port = new ReceivePort(); | 71 ReceivePort port = new ReceivePort(); |
| 73 SecureServerSocket.bind(HOST_NAME, 0, 5, certificate).then((server) { | 72 SecureServerSocket.bind(HOST_NAME, 0, certificate).then((server) { |
| 74 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); | 73 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); |
| 75 server.listen((serverEnd) { | 74 server.listen((serverEnd) { |
| 76 clientEndFuture.then((clientEnd) { | 75 clientEndFuture.then((clientEnd) { |
| 77 clientEnd.close(); | 76 clientEnd.close(); |
| 78 serverEnd.close(); | 77 serverEnd.close(); |
| 79 server.close(); | 78 server.close(); |
| 80 port.close(); | 79 port.close(); |
| 81 }); | 80 }); |
| 82 }); | 81 }); |
| 83 }); | 82 }); |
| 84 } | 83 } |
| 85 | 84 |
| 86 void testSimpleConnectFail(String certificate) { | 85 void testSimpleConnectFail(String certificate) { |
| 87 ReceivePort port = new ReceivePort(); | 86 ReceivePort port = new ReceivePort(); |
| 88 SecureServerSocket.bind(HOST_NAME, 0, 5, certificate).then((server) { | 87 SecureServerSocket.bind(HOST_NAME, 0, certificate).then((server) { |
| 89 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port) | 88 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port) |
| 90 .then((clientEnd) { | 89 .then((clientEnd) { |
| 91 Expect.fail("No client connection expected."); | 90 Expect.fail("No client connection expected."); |
| 92 }) | 91 }) |
| 93 .catchError((error) { | 92 .catchError((error) { |
| 94 Expect.isTrue(error is SocketIOException); | 93 Expect.isTrue(error is SocketIOException); |
| 95 }); | 94 }); |
| 96 server.listen((serverEnd) { | 95 server.listen((serverEnd) { |
| 97 Expect.fail("No server connection expected."); | 96 Expect.fail("No server connection expected."); |
| 98 }, | 97 }, |
| 99 onError: (error) { | 98 onError: (error) { |
| 100 Expect.isTrue(error is SocketIOException); | 99 Expect.isTrue(error is SocketIOException); |
| 101 clientEndFuture.then((_) => port.close()); | 100 clientEndFuture.then((_) => port.close()); |
| 102 }); | 101 }); |
| 103 }); | 102 }); |
| 104 } | 103 } |
| 105 | 104 |
| 106 void testServerListenAfterConnect() { | 105 void testServerListenAfterConnect() { |
| 107 ReceivePort port = new ReceivePort(); | 106 ReceivePort port = new ReceivePort(); |
| 108 SecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((server) { | 107 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((server) { |
| 109 Expect.isTrue(server.port > 0); | 108 Expect.isTrue(server.port > 0); |
| 110 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); | 109 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); |
| 111 new Timer(const Duration(milliseconds: 500), () { | 110 new Timer(const Duration(milliseconds: 500), () { |
| 112 server.listen((serverEnd) { | 111 server.listen((serverEnd) { |
| 113 clientEndFuture.then((clientEnd) { | 112 clientEndFuture.then((clientEnd) { |
| 114 clientEnd.close(); | 113 clientEnd.close(); |
| 115 serverEnd.close(); | 114 serverEnd.close(); |
| 116 server.close(); | 115 server.close(); |
| 117 port.close(); | 116 port.close(); |
| 118 }); | 117 }); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 139 } | 138 } |
| 140 | 139 |
| 141 void verifyTestData(List<int> data) { | 140 void verifyTestData(List<int> data) { |
| 142 Expect.equals(messageSize, data.length); | 141 Expect.equals(messageSize, data.length); |
| 143 List<int> expected = createTestData(); | 142 List<int> expected = createTestData(); |
| 144 for (int i = 0; i < messageSize; i++) { | 143 for (int i = 0; i < messageSize; i++) { |
| 145 Expect.equals(expected[i], data[i]); | 144 Expect.equals(expected[i], data[i]); |
| 146 } | 145 } |
| 147 } | 146 } |
| 148 | 147 |
| 149 SecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((server) { | 148 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((server) { |
| 150 server.listen((client) { | 149 server.listen((client) { |
| 151 int bytesRead = 0; | 150 int bytesRead = 0; |
| 152 int bytesWritten = 0; | 151 int bytesWritten = 0; |
| 153 List<int> data = new List<int>(messageSize); | 152 List<int> data = new List<int>(messageSize); |
| 154 | 153 |
| 155 client.listen( | 154 client.listen( |
| 156 (buffer) { | 155 (buffer) { |
| 157 Expect.isTrue(bytesWritten == 0); | 156 Expect.isTrue(bytesWritten == 0); |
| 158 data.setRange(bytesRead, bytesRead + buffer.length, buffer); | 157 data.setRange(bytesRead, bytesRead + buffer.length, buffer); |
| 159 bytesRead += buffer.length; | 158 bytesRead += buffer.length; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 useBuiltinRoots: false); | 196 useBuiltinRoots: false); |
| 198 testSimpleBind(); | 197 testSimpleBind(); |
| 199 testInvalidBind(); | 198 testInvalidBind(); |
| 200 testSimpleConnect(CERTIFICATE); | 199 testSimpleConnect(CERTIFICATE); |
| 201 testSimpleConnect("CN=localhost"); | 200 testSimpleConnect("CN=localhost"); |
| 202 testSimpleConnectFail("not_a_nickname"); | 201 testSimpleConnectFail("not_a_nickname"); |
| 203 testSimpleConnectFail("CN=notARealDistinguishedName"); | 202 testSimpleConnectFail("CN=notARealDistinguishedName"); |
| 204 testServerListenAfterConnect(); | 203 testServerListenAfterConnect(); |
| 205 testSimpleReadWrite(); | 204 testSimpleReadWrite(); |
| 206 } | 205 } |
| OLD | NEW |