| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // |
| 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write |
| 8 // VMOptions=--short_socket_read --short_socket_write |
| 9 |
| 10 import "dart:async"; |
| 11 import "dart:io"; |
| 12 import "dart:isolate"; |
| 13 |
| 14 const SERVER_ADDRESS = "127.0.0.1"; |
| 15 const HOST_NAME = "localhost"; |
| 16 const CERTIFICATE = "localhost_cert"; |
| 17 |
| 18 void testArguments() { |
| 19 Expect.throws(() => |
| 20 SecureServerSocket.bind(SERVER_ADDRESS, 65536, 5, CERTIFICATE)); |
| 21 Expect.throws(() => |
| 22 SecureServerSocket.bind(SERVER_ADDRESS, -1, CERTIFICATE)); |
| 23 Expect.throws(() => |
| 24 SecureServerSocket.bind(SERVER_ADDRESS, 0, -1, CERTIFICATE)); |
| 25 } |
| 26 |
| 27 void testSimpleBind() { |
| 28 ReceivePort port = new ReceivePort(); |
| 29 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((s) { |
| 30 Expect.isTrue(s.port > 0); |
| 31 s.close(); |
| 32 port.close(); |
| 33 }); |
| 34 } |
| 35 |
| 36 void testInvalidBind() { |
| 37 int count = 0; |
| 38 ReceivePort port = new ReceivePort(); |
| 39 port.receive((_, __) { count++; if (count == 3) port.close(); }); |
| 40 |
| 41 // Bind to a unknown DNS name. |
| 42 SecureServerSocket.bind("ko.faar.__hest__", 0, 5, CERTIFICATE).then((_) { |
| 43 Expect.fail("Failure expected"); |
| 44 }).catchError((e) { |
| 45 Expect.isTrue(e.error is SocketIOException); |
| 46 port.toSendPort().send(1); |
| 47 }); |
| 48 |
| 49 // Bind to an unavaliable IP-address. |
| 50 SecureServerSocket.bind("8.8.8.8", 0, 5, CERTIFICATE).then((_) { |
| 51 Expect.fail("Failure expected"); |
| 52 }).catchError((e) { |
| 53 Expect.isTrue(e.error is SocketIOException); |
| 54 port.toSendPort().send(1); |
| 55 }); |
| 56 |
| 57 // Bind to a port already in use. |
| 58 // Either an error or a successful bind is allowed. |
| 59 // Windows platforms allow multiple binding to the same socket, with |
| 60 // unpredictable results. |
| 61 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((s) { |
| 62 SecureServerSocket.bind(SERVER_ADDRESS, |
| 63 s.port, |
| 64 5, |
| 65 CERTIFICATE).then((t) { |
| 66 Expect.equals('windows', Platform.operatingSystem); |
| 67 Expect.equals(s.port, t.port); |
| 68 s.close(); |
| 69 t.close(); |
| 70 port.toSendPort().send(1); |
| 71 }).catchError((e) { |
| 72 Expect.notEquals('windows', Platform.operatingSystem); |
| 73 Expect.isTrue(e.error is SocketIOException); |
| 74 s.close(); |
| 75 port.toSendPort().send(1); |
| 76 }); |
| 77 }); |
| 78 } |
| 79 |
| 80 void testSimpleConnect(String certificate) { |
| 81 ReceivePort port = new ReceivePort(); |
| 82 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, certificate).then((server) { |
| 83 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); |
| 84 server.listen((serverEnd) { |
| 85 clientEndFuture.then((clientEnd) { |
| 86 clientEnd.close(); |
| 87 serverEnd.close(); |
| 88 server.close(); |
| 89 port.close(); |
| 90 }); |
| 91 }); |
| 92 }); |
| 93 } |
| 94 |
| 95 void testSimpleConnectFail(String certificate) { |
| 96 ReceivePort port = new ReceivePort(); |
| 97 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, certificate).then((server) { |
| 98 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port) |
| 99 .then((clientEnd) { |
| 100 Expect.fail("No client connection expected."); |
| 101 }) |
| 102 .catchError((e) { |
| 103 Expect.isTrue(e is AsyncError); |
| 104 Expect.isTrue(e.error is SocketIOException); |
| 105 }); |
| 106 server.listen((serverEnd) { |
| 107 Expect.fail("No server connection expected."); |
| 108 }, |
| 109 onError: (e) { |
| 110 Expect.isTrue(e is AsyncError); |
| 111 Expect.isTrue(e.error is SocketIOException); |
| 112 clientEndFuture.then((_) => port.close()); |
| 113 }); |
| 114 }); |
| 115 } |
| 116 |
| 117 void testServerListenAfterConnect() { |
| 118 ReceivePort port = new ReceivePort(); |
| 119 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { |
| 120 Expect.isTrue(server.port > 0); |
| 121 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); |
| 122 new Timer(500, (_) { |
| 123 server.listen((serverEnd) { |
| 124 clientEndFuture.then((clientEnd) { |
| 125 clientEnd.close(); |
| 126 serverEnd.close(); |
| 127 server.close(); |
| 128 port.close(); |
| 129 }); |
| 130 }); |
| 131 }); |
| 132 }); |
| 133 } |
| 134 |
| 135 void testSimpleReadWrite() { |
| 136 // This test creates a server and a client connects. The client then |
| 137 // writes and the server echos. When the server has finished its |
| 138 // echo it half-closes. When the client gets the close event is |
| 139 // closes fully. |
| 140 ReceivePort port = new ReceivePort(); |
| 141 |
| 142 const messageSize = 1000; |
| 143 |
| 144 List<int> createTestData() { |
| 145 List<int> data = new List.fixedLength(messageSize); |
| 146 for (int i = 0; i < messageSize; i++) { |
| 147 data[i] = i & 0xff; |
| 148 } |
| 149 return data; |
| 150 } |
| 151 |
| 152 void verifyTestData(List<int> data) { |
| 153 Expect.equals(messageSize, data.length); |
| 154 List<int> expected = createTestData(); |
| 155 for (int i = 0; i < messageSize; i++) { |
| 156 Expect.equals(expected[i], data[i]); |
| 157 } |
| 158 } |
| 159 |
| 160 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { |
| 161 server.listen((client) { |
| 162 int bytesRead = 0; |
| 163 int bytesWritten = 0; |
| 164 List<int> data = new List.fixedLength(messageSize); |
| 165 |
| 166 client.listen( |
| 167 (buffer) { |
| 168 Expect.isTrue(bytesWritten == 0); |
| 169 data.setRange(bytesRead, buffer.length, buffer); |
| 170 bytesRead += buffer.length; |
| 171 if (bytesRead == data.length) { |
| 172 verifyTestData(data); |
| 173 client.add(data); |
| 174 client.close(); |
| 175 } |
| 176 }, |
| 177 onDone: () { |
| 178 server.close(); |
| 179 }); |
| 180 }); |
| 181 |
| 182 SecureSocket.connect(HOST_NAME, server.port).then((socket) { |
| 183 int bytesRead = 0; |
| 184 int bytesWritten = 0; |
| 185 List<int> dataSent = createTestData(); |
| 186 List<int> dataReceived = new List<int>.fixedLength(dataSent.length); |
| 187 socket.add(dataSent); |
| 188 socket.close(); // Can also be delayed. |
| 189 socket.listen( |
| 190 (List<int> buffer) { |
| 191 dataReceived.setRange(bytesRead, buffer.length, buffer); |
| 192 bytesRead += buffer.length; |
| 193 }, |
| 194 onDone: () { |
| 195 verifyTestData(dataReceived); |
| 196 socket.close(); |
| 197 port.close(); |
| 198 }); |
| 199 }); |
| 200 }); |
| 201 } |
| 202 |
| 203 main() { |
| 204 Path scriptDir = new Path(new Options().script).directoryPath; |
| 205 Path certificateDatabase = scriptDir.append('pkcert'); |
| 206 SecureSocket.initialize(database: certificateDatabase.toNativePath(), |
| 207 password: 'dartdart', |
| 208 useBuiltinRoots: false); |
| 209 testArguments(); |
| 210 testSimpleBind(); |
| 211 testInvalidBind(); |
| 212 testSimpleConnect(CERTIFICATE); |
| 213 testSimpleConnect("CN=localhost"); |
| 214 testSimpleConnectFail("not_a_nickname"); |
| 215 testSimpleConnectFail("CN=notARealDistinguishedName"); |
| 216 testServerListenAfterConnect(); |
| 217 testSimpleReadWrite(); |
| 218 } |
| OLD | NEW |