| 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"; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // unencrypted data on it, in the middle of an encrypted data transfer. | 22 // unencrypted data on it, in the middle of an encrypted data transfer. |
| 23 | 23 |
| 24 | 24 |
| 25 // This test creates a server and then connects a client to the server. | 25 // This test creates a server and then connects a client to the server. |
| 26 // After connecting, the connection is upgraded to a secure connection. | 26 // After connecting, the connection is upgraded to a secure connection. |
| 27 // The client writes data to the server, then writes unencrypted data | 27 // The client writes data to the server, then writes unencrypted data |
| 28 // on the underlying socket. When the server gets the unencrypted data, | 28 // on the underlying socket. When the server gets the unencrypted data, |
| 29 // this causes an error in the NSS code. When the NSS code is in debug | 29 // this causes an error in the NSS code. When the NSS code is in debug |
| 30 // mode, bad data on the connection can cause an assertion failure, rather | 30 // mode, bad data on the connection can cause an assertion failure, rather |
| 31 // than an exception. | 31 // than an exception. |
| 32 void test(bool hostnameInConnect) { | |
| 33 ReceivePort port = new ReceivePort(); | |
| 34 | 32 |
| 35 const messageSize = 1000; | 33 const messageSize = 1000; |
| 36 | 34 |
| 37 List<int> createTestData() { | 35 |
| 38 List<int> data = new List<int>(messageSize); | 36 List<int> createTestData() { |
| 39 for (int i = 0; i < messageSize; i++) { | 37 List<int> data = new List<int>(messageSize); |
| 40 data[i] = i & 0xff; | 38 for (int i = 0; i < messageSize; i++) { |
| 39 data[i] = i & 0xff; |
| 40 } |
| 41 return data; |
| 42 } |
| 43 |
| 44 |
| 45 void verifyTestData(List<int> data) { |
| 46 Expect.equals(messageSize, data.length); |
| 47 List<int> expected = createTestData(); |
| 48 for (int i = 0; i < messageSize; i++) { |
| 49 Expect.equals(expected[i], data[i]); |
| 50 } |
| 51 } |
| 52 |
| 53 |
| 54 Future runServer(RawSocket client) { |
| 55 final completer = new Completer(); |
| 56 final dataReceived = new List<int>(messageSize); |
| 57 int bytesRead = 0; |
| 58 client.writeEventsEnabled = false; |
| 59 client.listen((event) { |
| 60 switch (event) { |
| 61 case RawSocketEvent.READ: |
| 62 Expect.isTrue(client.available() > 0); |
| 63 var buffer = client.read(200); |
| 64 dataReceived.setRange(bytesRead, bytesRead + buffer.length, buffer); |
| 65 bytesRead += buffer.length; |
| 66 if (bytesRead == dataReceived.length) { |
| 67 verifyTestData(dataReceived); |
| 68 } |
| 69 break; |
| 70 case RawSocketEvent.WRITE: |
| 71 Expect.fail('WRITE event received'); |
| 72 break; |
| 73 case RawSocketEvent.READ_CLOSED: |
| 74 Expect.fail('READ_CLOSED event received'); |
| 75 client.shutdown(SocketDirection.SEND); |
| 76 completer.complete(null); |
| 77 break; |
| 41 } | 78 } |
| 42 return data; | 79 }, |
| 43 } | 80 onError: (e) { |
| 81 Expect.isTrue(e is TlsException); |
| 82 Expect.isTrue(e.toString().contains( |
| 83 'received a record with an incorrect Message Authentication Code')); |
| 84 completer.complete(null); |
| 85 }); |
| 86 return completer.future; |
| 87 } |
| 44 | 88 |
| 45 void verifyTestData(List<int> data) { | 89 |
| 46 Expect.equals(messageSize, data.length); | 90 Future<RawSocket> runClient(List sockets) { |
| 47 List<int> expected = createTestData(); | 91 final RawSocket baseSocket = sockets[0]; |
| 48 for (int i = 0; i < messageSize; i++) { | 92 final RawSecureSocket socket = sockets[1]; |
| 49 Expect.equals(expected[i], data[i]); | 93 final data = createTestData(); |
| 94 var completer = new Completer(); |
| 95 void tryComplete() { |
| 96 if (completer != null) { |
| 97 completer.complete(null); |
| 98 completer = null; |
| 50 } | 99 } |
| 51 } | 100 } |
| 52 | 101 |
| 53 Future runServer(RawSocket client) { | 102 int bytesWritten = 0; |
| 54 final completer = new Completer(); | 103 socket.listen((event) { |
| 55 final dataReceived = new List<int>(messageSize); | 104 switch (event) { |
| 56 int bytesRead = 0; | 105 case RawSocketEvent.READ: |
| 57 client.writeEventsEnabled = false; | 106 Expect.fail('READ event received'); |
| 58 client.listen((event) { | 107 break; |
| 59 switch (event) { | 108 case RawSocketEvent.WRITE: |
| 60 case RawSocketEvent.READ: | 109 if (bytesWritten < data.length) { |
| 61 Expect.isTrue(client.available() > 0); | 110 bytesWritten += socket.write(data, bytesWritten); |
| 62 var buffer = client.read(200); | 111 } |
| 63 dataReceived.setRange(bytesRead, bytesRead + buffer.length, buffer); | 112 if (bytesWritten < data.length) { |
| 64 bytesRead += buffer.length; | 113 socket.writeEventsEnabled = true; |
| 65 if (bytesRead == dataReceived.length) { | 114 } |
| 66 verifyTestData(dataReceived); | 115 if (bytesWritten == data.length) { |
| 67 } | 116 baseSocket.write(data, 0, 300); |
| 68 break; | 117 socket.shutdown(SocketDirection.SEND); |
| 69 case RawSocketEvent.WRITE: | 118 } |
| 70 Expect.fail('WRITE event received'); | 119 break; |
| 71 break; | 120 case RawSocketEvent.READ_CLOSED: |
| 72 case RawSocketEvent.READ_CLOSED: | 121 tryComplete(); |
| 73 Expect.fail('READ_CLOSED event received'); | 122 break; |
| 74 client.shutdown(SocketDirection.SEND); | |
| 75 completer.complete(null); | |
| 76 break; | |
| 77 default: throw "Unexpected event $event"; | |
| 78 } | |
| 79 }, | |
| 80 onError: (e) { | |
| 81 Expect.isTrue(e is TlsException); | |
| 82 Expect.isTrue(e.toString().contains( | |
| 83 'received a record with an incorrect Message Authentication Code')); | |
| 84 completer.complete(null); | |
| 85 }); | |
| 86 return completer.future; | |
| 87 } | |
| 88 | |
| 89 Future<RawSocket> runClient(List sockets) { | |
| 90 RawSocket baseSocket = sockets[0]; | |
| 91 RawSecureSocket socket = sockets[1]; | |
| 92 final completer = new Completer(); | |
| 93 bool completed = false; | |
| 94 void tryComplete() { | |
| 95 if (!completed) { | |
| 96 completed = true; | |
| 97 completer.complete(null); | |
| 98 } | |
| 99 } | 123 } |
| 100 final data = createTestData(); | 124 }, |
| 101 int bytesWritten = 0; | 125 onError: (e) { |
| 102 socket.listen((event) { | 126 Expect.isTrue(e is IOException); |
| 103 switch (event) { | 127 tryComplete(); |
| 104 case RawSocketEvent.READ: | 128 }); |
| 105 Expect.fail('READ event received'); | 129 return completer.future; |
| 106 break; | 130 } |
| 107 case RawSocketEvent.WRITE: | |
| 108 if (bytesWritten < data.length) { | |
| 109 bytesWritten += socket.write(data, bytesWritten); | |
| 110 } | |
| 111 if (bytesWritten < data.length) { | |
| 112 socket.writeEventsEnabled = true; | |
| 113 } | |
| 114 if (bytesWritten == data.length) { | |
| 115 baseSocket.write(data, 0, 300); | |
| 116 socket.shutdown(SocketDirection.SEND); | |
| 117 } | |
| 118 break; | |
| 119 case RawSocketEvent.READ_CLOSED: | |
| 120 tryComplete(); | |
| 121 break; | |
| 122 default: throw "Unexpected event $event"; | |
| 123 } | |
| 124 }, | |
| 125 onError: (e) { | |
| 126 Expect.isTrue(e is IOException); | |
| 127 tryComplete(); | |
| 128 }); | |
| 129 return completer.future; | |
| 130 } | |
| 131 | 131 |
| 132 | 132 |
| 133 Future<List> connectClient(int port) => | 133 Future<List> connectClient(int port, bool hostnameInConnect) => |
| 134 RawSocket.connect(HOST_NAME, port) | 134 RawSocket.connect(HOST_NAME, port) |
| 135 .then((socket) => | 135 .then((socket) => |
| 136 (hostnameInConnect ? RawSecureSocket.secure(socket) | 136 (hostnameInConnect ? RawSecureSocket.secure(socket) |
| 137 : RawSecureSocket.secure(socket, host: HOST_NAME)) | 137 : RawSecureSocket.secure(socket, host: HOST_NAME)) |
| 138 .then((secureSocket) => [socket, secureSocket])); | 138 .then((secureSocket) => [socket, secureSocket])); |
| 139 | 139 |
| 140 | 140 |
| 141 serverReady(server) { | 141 Future test(bool hostnameInConnect) { |
| 142 return RawServerSocket.bind(HOST_NAME, 0) |
| 143 .then((server) { |
| 142 server.listen((client) { | 144 server.listen((client) { |
| 143 RawSecureSocket.secureServer(client, CERTIFICATE).then((secureClient) { | 145 RawSecureSocket.secureServer(client, CERTIFICATE) |
| 146 .then((secureClient) { |
| 144 Expect.throws(() => client.add([0])); | 147 Expect.throws(() => client.add([0])); |
| 145 runServer(secureClient).then((_) => server.close()); | 148 return runServer(secureClient); |
| 146 }); | 149 }) |
| 150 .then((_) => server.close()); |
| 147 }); | 151 }); |
| 148 | 152 |
| 149 connectClient(server.port) | 153 return connectClient(server.port, hostnameInConnect).then(runClient); |
| 150 .then(runClient) | 154 }); |
| 151 .then((_) => port.close()); | 155 } |
| 152 } | |
| 153 | 156 |
| 154 RawServerSocket.bind(HOST_NAME, 0).then(serverReady); | |
| 155 } | |
| 156 | 157 |
| 157 main() { | 158 main() { |
| 158 Path scriptDir = new Path(Platform.script).directoryPath; | 159 Path scriptDir = new Path(Platform.script).directoryPath; |
| 159 Path certificateDatabase = scriptDir.append('pkcert'); | 160 Path certificateDatabase = scriptDir.append('pkcert'); |
| 160 SecureSocket.initialize(database: certificateDatabase.toNativePath(), | 161 SecureSocket.initialize(database: certificateDatabase.toNativePath(), |
| 161 password: 'dartdart', | 162 password: 'dartdart', |
| 162 useBuiltinRoots: false); | 163 useBuiltinRoots: false); |
| 163 test(false); | 164 var keepAlive = new ReceivePort(); |
| 164 test(true); | 165 Future.wait([test(false), test(true)]) |
| 166 .then((_) => keepAlive.close()); |
| 165 } | 167 } |
| OLD | NEW |