| 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 "dart:async"; |
| 11 import "dart:io"; |
| 12 |
| 13 import "package:async_helper/async_helper.dart"; |
| 10 import "package:expect/expect.dart"; | 14 import "package:expect/expect.dart"; |
| 11 import "package:path/path.dart"; | 15 import "package:path/path.dart"; |
| 12 import "dart:async"; | |
| 13 import "dart:io"; | |
| 14 import "dart:isolate"; | |
| 15 | 16 |
| 16 const HOST_NAME = "localhost"; | 17 const HOST_NAME = "localhost"; |
| 17 const CERTIFICATE = "localhost_cert"; | 18 const CERTIFICATE = "localhost_cert"; |
| 18 | 19 |
| 19 void testSimpleBind() { | 20 void testSimpleBind() { |
| 20 ReceivePort port = new ReceivePort(); | 21 asyncStart(); |
| 21 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((s) { | 22 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((s) { |
| 22 Expect.isTrue(s.port > 0); | 23 Expect.isTrue(s.port > 0); |
| 23 s.close(); | 24 s.close(); |
| 24 port.close(); | 25 asyncEnd(); |
| 25 }); | 26 }); |
| 26 } | 27 } |
| 27 | 28 |
| 28 void testInvalidBind() { | 29 void testInvalidBind() { |
| 29 int count = 0; | 30 int count = 0; |
| 30 ReceivePort port = new ReceivePort(); | |
| 31 port.receive((_, __) { count++; if (count == 3) port.close(); }); | |
| 32 | 31 |
| 33 // Bind to a unknown DNS name. | 32 // Bind to a unknown DNS name. |
| 33 asyncStart(); |
| 34 SecureServerSocket.bind("ko.faar.__hest__", 0, CERTIFICATE).then((_) { | 34 SecureServerSocket.bind("ko.faar.__hest__", 0, CERTIFICATE).then((_) { |
| 35 Expect.fail("Failure expected"); | 35 Expect.fail("Failure expected"); |
| 36 }).catchError((error) { | 36 }).catchError((error) { |
| 37 Expect.isTrue(error is SocketException); | 37 Expect.isTrue(error is SocketException); |
| 38 port.toSendPort().send(1); | 38 asyncEnd(); |
| 39 }); | 39 }); |
| 40 | 40 |
| 41 // Bind to an unavaliable IP-address. | 41 // Bind to an unavaliable IP-address. |
| 42 asyncStart(); |
| 42 SecureServerSocket.bind("8.8.8.8", 0, CERTIFICATE).then((_) { | 43 SecureServerSocket.bind("8.8.8.8", 0, CERTIFICATE).then((_) { |
| 43 Expect.fail("Failure expected"); | 44 Expect.fail("Failure expected"); |
| 44 }).catchError((error) { | 45 }).catchError((error) { |
| 45 Expect.isTrue(error is SocketException); | 46 Expect.isTrue(error is SocketException); |
| 46 port.toSendPort().send(1); | 47 asyncEnd(); |
| 47 }); | 48 }); |
| 48 | 49 |
| 49 // Bind to a port already in use. | 50 // Bind to a port already in use. |
| 51 asyncStart(); |
| 50 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((s) { | 52 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((s) { |
| 51 SecureServerSocket.bind(HOST_NAME, | 53 SecureServerSocket.bind(HOST_NAME, |
| 52 s.port, | 54 s.port, |
| 53 CERTIFICATE).then((t) { | 55 CERTIFICATE).then((t) { |
| 54 Expect.fail("Multiple listens on same port"); | 56 Expect.fail("Multiple listens on same port"); |
| 55 port.toSendPort().send(1); | |
| 56 }).catchError((error) { | 57 }).catchError((error) { |
| 57 Expect.isTrue(error is SocketException); | 58 Expect.isTrue(error is SocketException); |
| 58 s.close(); | 59 s.close(); |
| 59 port.toSendPort().send(1); | 60 asyncEnd(); |
| 60 }); | 61 }); |
| 61 }); | 62 }); |
| 62 } | 63 } |
| 63 | 64 |
| 64 void testSimpleConnect(String certificate) { | 65 void testSimpleConnect(String certificate) { |
| 65 ReceivePort port = new ReceivePort(); | 66 asyncStart(); |
| 66 SecureServerSocket.bind(HOST_NAME, 0, certificate).then((server) { | 67 SecureServerSocket.bind(HOST_NAME, 0, certificate).then((server) { |
| 67 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); | 68 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); |
| 68 server.listen((serverEnd) { | 69 server.listen((serverEnd) { |
| 69 clientEndFuture.then((clientEnd) { | 70 clientEndFuture.then((clientEnd) { |
| 70 clientEnd.close(); | 71 clientEnd.close(); |
| 71 serverEnd.close(); | 72 serverEnd.close(); |
| 72 server.close(); | 73 server.close(); |
| 73 port.close(); | 74 asyncEnd(); |
| 74 }); | 75 }); |
| 75 }); | 76 }); |
| 76 }); | 77 }); |
| 77 } | 78 } |
| 78 | 79 |
| 79 void testSimpleConnectFail(String certificate, bool cancelOnError) { | 80 void testSimpleConnectFail(String certificate, bool cancelOnError) { |
| 80 ReceivePort port = new ReceivePort(); | 81 asyncStart(); |
| 81 SecureServerSocket.bind(HOST_NAME, 0, certificate).then((server) { | 82 SecureServerSocket.bind(HOST_NAME, 0, certificate).then((server) { |
| 82 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port) | 83 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port) |
| 83 .then((clientEnd) { | 84 .then((clientEnd) { |
| 84 Expect.fail("No client connection expected."); | 85 Expect.fail("No client connection expected."); |
| 85 }) | 86 }) |
| 86 .catchError((error) { | 87 .catchError((error) { |
| 87 Expect.isTrue(error is HandshakeException || | 88 Expect.isTrue(error is HandshakeException || |
| 88 error is SocketException); | 89 error is SocketException); |
| 89 }); | 90 }); |
| 90 server.listen((serverEnd) { | 91 server.listen((serverEnd) { |
| 91 Expect.fail("No server connection expected."); | 92 Expect.fail("No server connection expected."); |
| 92 }, | 93 }, |
| 93 onError: (error) { | 94 onError: (error) { |
| 94 Expect.isTrue(error is CertificateException); | 95 Expect.isTrue(error is CertificateException); |
| 95 clientEndFuture.then((_) { | 96 clientEndFuture.then((_) { |
| 96 if (!cancelOnError) server.close(); | 97 if (!cancelOnError) server.close(); |
| 97 port.close(); | 98 asyncEnd(); |
| 98 }); | 99 }); |
| 99 }, | 100 }, |
| 100 cancelOnError: cancelOnError); | 101 cancelOnError: cancelOnError); |
| 101 }); | 102 }); |
| 102 } | 103 } |
| 103 | 104 |
| 104 void testServerListenAfterConnect() { | 105 void testServerListenAfterConnect() { |
| 105 ReceivePort port = new ReceivePort(); | 106 asyncStart(); |
| 106 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((server) { | 107 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((server) { |
| 107 Expect.isTrue(server.port > 0); | 108 Expect.isTrue(server.port > 0); |
| 108 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); | 109 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); |
| 109 new Timer(const Duration(milliseconds: 500), () { | 110 new Timer(const Duration(milliseconds: 500), () { |
| 110 server.listen((serverEnd) { | 111 server.listen((serverEnd) { |
| 111 clientEndFuture.then((clientEnd) { | 112 clientEndFuture.then((clientEnd) { |
| 112 clientEnd.close(); | 113 clientEnd.close(); |
| 113 serverEnd.close(); | 114 serverEnd.close(); |
| 114 server.close(); | 115 server.close(); |
| 115 port.close(); | 116 asyncEnd(); |
| 116 }); | 117 }); |
| 117 }); | 118 }); |
| 118 }); | 119 }); |
| 119 }); | 120 }); |
| 120 } | 121 } |
| 121 | 122 |
| 122 void testSimpleReadWrite() { | 123 void testSimpleReadWrite() { |
| 123 // This test creates a server and a client connects. The client then | 124 // This test creates a server and a client connects. The client then |
| 124 // writes and the server echos. When the server has finished its | 125 // writes and the server echos. When the server has finished its |
| 125 // echo it half-closes. When the client gets the close event is | 126 // echo it half-closes. When the client gets the close event is |
| 126 // closes fully. | 127 // closes fully. |
| 127 ReceivePort port = new ReceivePort(); | 128 asyncStart(); |
| 128 | 129 |
| 129 const messageSize = 1000; | 130 const messageSize = 1000; |
| 130 | 131 |
| 131 List<int> createTestData() { | 132 List<int> createTestData() { |
| 132 List<int> data = new List<int>(messageSize); | 133 List<int> data = new List<int>(messageSize); |
| 133 for (int i = 0; i < messageSize; i++) { | 134 for (int i = 0; i < messageSize; i++) { |
| 134 data[i] = i & 0xff; | 135 data[i] = i & 0xff; |
| 135 } | 136 } |
| 136 return data; | 137 return data; |
| 137 } | 138 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 socket.add(dataSent); | 175 socket.add(dataSent); |
| 175 socket.close(); // Can also be delayed. | 176 socket.close(); // Can also be delayed. |
| 176 socket.listen( | 177 socket.listen( |
| 177 (List<int> buffer) { | 178 (List<int> buffer) { |
| 178 dataReceived.setRange(bytesRead, bytesRead + buffer.length, buffer); | 179 dataReceived.setRange(bytesRead, bytesRead + buffer.length, buffer); |
| 179 bytesRead += buffer.length; | 180 bytesRead += buffer.length; |
| 180 }, | 181 }, |
| 181 onDone: () { | 182 onDone: () { |
| 182 verifyTestData(dataReceived); | 183 verifyTestData(dataReceived); |
| 183 socket.close(); | 184 socket.close(); |
| 184 port.close(); | 185 asyncEnd(); |
| 185 }); | 186 }); |
| 186 }); | 187 }); |
| 187 }); | 188 }); |
| 188 } | 189 } |
| 189 | 190 |
| 190 main() { | 191 main() { |
| 192 asyncStart(); |
| 191 String certificateDatabase = join(dirname(Platform.script), 'pkcert'); | 193 String certificateDatabase = join(dirname(Platform.script), 'pkcert'); |
| 192 SecureSocket.initialize(database: certificateDatabase, | 194 SecureSocket.initialize(database: certificateDatabase, |
| 193 password: 'dartdart', | 195 password: 'dartdart', |
| 194 useBuiltinRoots: false); | 196 useBuiltinRoots: false); |
| 195 testSimpleBind(); | 197 testSimpleBind(); |
| 196 testInvalidBind(); | 198 testInvalidBind(); |
| 197 testSimpleConnect(CERTIFICATE); | 199 testSimpleConnect(CERTIFICATE); |
| 198 testSimpleConnect("CN=localhost"); | 200 testSimpleConnect("CN=localhost"); |
| 199 testSimpleConnectFail("not_a_nickname", false); | 201 testSimpleConnectFail("not_a_nickname", false); |
| 200 testSimpleConnectFail("CN=notARealDistinguishedName", false); | 202 testSimpleConnectFail("CN=notARealDistinguishedName", false); |
| 201 testSimpleConnectFail("not_a_nickname", true); | 203 testSimpleConnectFail("not_a_nickname", true); |
| 202 testSimpleConnectFail("CN=notARealDistinguishedName", true); | 204 testSimpleConnectFail("CN=notARealDistinguishedName", true); |
| 203 testServerListenAfterConnect(); | 205 testServerListenAfterConnect(); |
| 204 testSimpleReadWrite(); | 206 testSimpleReadWrite(); |
| 207 asyncEnd(); |
| 205 } | 208 } |
| OLD | NEW |