| 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 testArguments() { | 18 void testArguments() { |
| 20 Expect.throws(() => | 19 Expect.throws(() => |
| 21 SecureServerSocket.bind(SERVER_ADDRESS, 65536, 5, CERTIFICATE)); | 20 SecureServerSocket.bind(HOST_NAME, 65536, 5, CERTIFICATE)); |
| 22 Expect.throws(() => | 21 Expect.throws(() => |
| 23 SecureServerSocket.bind(SERVER_ADDRESS, -1, CERTIFICATE)); | 22 SecureServerSocket.bind(HOST_NAME, -1, CERTIFICATE)); |
| 24 Expect.throws(() => | 23 Expect.throws(() => |
| 25 SecureServerSocket.bind(SERVER_ADDRESS, 0, -1, CERTIFICATE)); | 24 SecureServerSocket.bind(HOST_NAME, 0, -1, CERTIFICATE)); |
| 26 } | 25 } |
| 27 | 26 |
| 28 void testSimpleBind() { | 27 void testSimpleBind() { |
| 29 ReceivePort port = new ReceivePort(); | 28 ReceivePort port = new ReceivePort(); |
| 30 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((s) { | 29 SecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((s) { |
| 31 Expect.isTrue(s.port > 0); | 30 Expect.isTrue(s.port > 0); |
| 32 s.close(); | 31 s.close(); |
| 33 port.close(); | 32 port.close(); |
| 34 }); | 33 }); |
| 35 } | 34 } |
| 36 | 35 |
| 37 void testInvalidBind() { | 36 void testInvalidBind() { |
| 38 int count = 0; | 37 int count = 0; |
| 39 ReceivePort port = new ReceivePort(); | 38 ReceivePort port = new ReceivePort(); |
| 40 port.receive((_, __) { count++; if (count == 3) port.close(); }); | 39 port.receive((_, __) { count++; if (count == 3) port.close(); }); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 52 Expect.fail("Failure expected"); | 51 Expect.fail("Failure expected"); |
| 53 }).catchError((error) { | 52 }).catchError((error) { |
| 54 Expect.isTrue(error is SocketIOException); | 53 Expect.isTrue(error is SocketIOException); |
| 55 port.toSendPort().send(1); | 54 port.toSendPort().send(1); |
| 56 }); | 55 }); |
| 57 | 56 |
| 58 // Bind to a port already in use. | 57 // Bind to a port already in use. |
| 59 // Either an error or a successful bind is allowed. | 58 // Either an error or a successful bind is allowed. |
| 60 // Windows platforms allow multiple binding to the same socket, with | 59 // Windows platforms allow multiple binding to the same socket, with |
| 61 // unpredictable results. | 60 // unpredictable results. |
| 62 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((s) { | 61 SecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((s) { |
| 63 SecureServerSocket.bind(SERVER_ADDRESS, | 62 SecureServerSocket.bind(HOST_NAME, |
| 64 s.port, | 63 s.port, |
| 65 5, | 64 5, |
| 66 CERTIFICATE).then((t) { | 65 CERTIFICATE).then((t) { |
| 67 Expect.equals('windows', Platform.operatingSystem); | 66 Expect.equals('windows', Platform.operatingSystem); |
| 68 Expect.equals(s.port, t.port); | 67 Expect.equals(s.port, t.port); |
| 69 s.close(); | 68 s.close(); |
| 70 t.close(); | 69 t.close(); |
| 71 port.toSendPort().send(1); | 70 port.toSendPort().send(1); |
| 72 }).catchError((error) { | 71 }).catchError((error) { |
| 73 Expect.notEquals('windows', Platform.operatingSystem); | 72 Expect.notEquals('windows', Platform.operatingSystem); |
| 74 Expect.isTrue(error is SocketIOException); | 73 Expect.isTrue(error is SocketIOException); |
| 75 s.close(); | 74 s.close(); |
| 76 port.toSendPort().send(1); | 75 port.toSendPort().send(1); |
| 77 }); | 76 }); |
| 78 }); | 77 }); |
| 79 } | 78 } |
| 80 | 79 |
| 81 void testSimpleConnect(String certificate) { | 80 void testSimpleConnect(String certificate) { |
| 82 ReceivePort port = new ReceivePort(); | 81 ReceivePort port = new ReceivePort(); |
| 83 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, certificate).then((server) { | 82 SecureServerSocket.bind(HOST_NAME, 0, 5, certificate).then((server) { |
| 84 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); | 83 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); |
| 85 server.listen((serverEnd) { | 84 server.listen((serverEnd) { |
| 86 clientEndFuture.then((clientEnd) { | 85 clientEndFuture.then((clientEnd) { |
| 87 clientEnd.close(); | 86 clientEnd.close(); |
| 88 serverEnd.close(); | 87 serverEnd.close(); |
| 89 server.close(); | 88 server.close(); |
| 90 port.close(); | 89 port.close(); |
| 91 }); | 90 }); |
| 92 }); | 91 }); |
| 93 }); | 92 }); |
| 94 } | 93 } |
| 95 | 94 |
| 96 void testSimpleConnectFail(String certificate) { | 95 void testSimpleConnectFail(String certificate) { |
| 97 ReceivePort port = new ReceivePort(); | 96 ReceivePort port = new ReceivePort(); |
| 98 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, certificate).then((server) { | 97 SecureServerSocket.bind(HOST_NAME, 0, 5, certificate).then((server) { |
| 99 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port) | 98 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port) |
| 100 .then((clientEnd) { | 99 .then((clientEnd) { |
| 101 Expect.fail("No client connection expected."); | 100 Expect.fail("No client connection expected."); |
| 102 }) | 101 }) |
| 103 .catchError((error) { | 102 .catchError((error) { |
| 104 Expect.isTrue(error is SocketIOException); | 103 Expect.isTrue(error is SocketIOException); |
| 105 }); | 104 }); |
| 106 server.listen((serverEnd) { | 105 server.listen((serverEnd) { |
| 107 Expect.fail("No server connection expected."); | 106 Expect.fail("No server connection expected."); |
| 108 }, | 107 }, |
| 109 onError: (error) { | 108 onError: (error) { |
| 110 Expect.isTrue(error is SocketIOException); | 109 Expect.isTrue(error is SocketIOException); |
| 111 clientEndFuture.then((_) => port.close()); | 110 clientEndFuture.then((_) => port.close()); |
| 112 }); | 111 }); |
| 113 }); | 112 }); |
| 114 } | 113 } |
| 115 | 114 |
| 116 void testServerListenAfterConnect() { | 115 void testServerListenAfterConnect() { |
| 117 ReceivePort port = new ReceivePort(); | 116 ReceivePort port = new ReceivePort(); |
| 118 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { | 117 SecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((server) { |
| 119 Expect.isTrue(server.port > 0); | 118 Expect.isTrue(server.port > 0); |
| 120 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); | 119 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); |
| 121 new Timer(const Duration(milliseconds: 500), () { | 120 new Timer(const Duration(milliseconds: 500), () { |
| 122 server.listen((serverEnd) { | 121 server.listen((serverEnd) { |
| 123 clientEndFuture.then((clientEnd) { | 122 clientEndFuture.then((clientEnd) { |
| 124 clientEnd.close(); | 123 clientEnd.close(); |
| 125 serverEnd.close(); | 124 serverEnd.close(); |
| 126 server.close(); | 125 server.close(); |
| 127 port.close(); | 126 port.close(); |
| 128 }); | 127 }); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 149 } | 148 } |
| 150 | 149 |
| 151 void verifyTestData(List<int> data) { | 150 void verifyTestData(List<int> data) { |
| 152 Expect.equals(messageSize, data.length); | 151 Expect.equals(messageSize, data.length); |
| 153 List<int> expected = createTestData(); | 152 List<int> expected = createTestData(); |
| 154 for (int i = 0; i < messageSize; i++) { | 153 for (int i = 0; i < messageSize; i++) { |
| 155 Expect.equals(expected[i], data[i]); | 154 Expect.equals(expected[i], data[i]); |
| 156 } | 155 } |
| 157 } | 156 } |
| 158 | 157 |
| 159 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { | 158 SecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((server) { |
| 160 server.listen((client) { | 159 server.listen((client) { |
| 161 int bytesRead = 0; | 160 int bytesRead = 0; |
| 162 int bytesWritten = 0; | 161 int bytesWritten = 0; |
| 163 List<int> data = new List<int>(messageSize); | 162 List<int> data = new List<int>(messageSize); |
| 164 | 163 |
| 165 client.listen( | 164 client.listen( |
| 166 (buffer) { | 165 (buffer) { |
| 167 Expect.isTrue(bytesWritten == 0); | 166 Expect.isTrue(bytesWritten == 0); |
| 168 data.setRange(bytesRead, bytesRead + buffer.length, buffer); | 167 data.setRange(bytesRead, bytesRead + buffer.length, buffer); |
| 169 bytesRead += buffer.length; | 168 bytesRead += buffer.length; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 testArguments(); | 207 testArguments(); |
| 209 testSimpleBind(); | 208 testSimpleBind(); |
| 210 testInvalidBind(); | 209 testInvalidBind(); |
| 211 testSimpleConnect(CERTIFICATE); | 210 testSimpleConnect(CERTIFICATE); |
| 212 testSimpleConnect("CN=localhost"); | 211 testSimpleConnect("CN=localhost"); |
| 213 testSimpleConnectFail("not_a_nickname"); | 212 testSimpleConnectFail("not_a_nickname"); |
| 214 testSimpleConnectFail("CN=notARealDistinguishedName"); | 213 testSimpleConnectFail("CN=notARealDistinguishedName"); |
| 215 testServerListenAfterConnect(); | 214 testServerListenAfterConnect(); |
| 216 testSimpleReadWrite(); | 215 testSimpleReadWrite(); |
| 217 } | 216 } |
| OLD | NEW |