Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: tests/standalone/io/secure_socket_bad_data_test.dart

Issue 18500006: dart:io | Pass errors from multithreaded encryption back to Dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Compile libssl_dart without DEBUG defined. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/io/socket.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "package:expect/expect.dart";
11 import "dart:async";
12 import "dart:io";
13 import "dart:isolate";
14
15 const HOST_NAME = "localhost";
16 const CERTIFICATE = "localhost_cert";
17
18 // This test sends corrupt data in the middle of a secure network connection.
19 // This tests the error handling of RawSecureSocket.
20 // A RawSocket connection is upgraded to a secure connection, so that we
21 // have a reference to the underlying socket. Then we send some
22 // unencrypted data on it, in the middle of an encrypted data transfer.
23
24
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.
27 // The client writes data to the server, then writes 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
30 // mode, bad data on the connection can cause an assertion failure, rather
31 // than an exception.
32 void test(bool hostnameInConnect) {
33 ReceivePort port = new ReceivePort();
34
35 const messageSize = 1000;
36
37 List<int> createTestData() {
38 List<int> data = new List<int>(messageSize);
39 for (int i = 0; i < messageSize; i++) {
40 data[i] = i & 0xff;
41 }
42 return data;
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 Future runServer(RawSocket client) {
54 final completer = new Completer();
55 final dataReceived = new List<int>(messageSize);
56 int bytesRead = 0;
57 client.writeEventsEnabled = false;
58 client.listen((event) {
59 switch (event) {
60 case RawSocketEvent.READ:
61 Expect.isTrue(client.available() > 0);
62 var buffer = client.read(200);
63 dataReceived.setRange(bytesRead, bytesRead + buffer.length, buffer);
64 bytesRead += buffer.length;
65 if (bytesRead == dataReceived.length) {
66 verifyTestData(dataReceived);
67 }
68 break;
69 case RawSocketEvent.WRITE:
70 Expect.fail('WRITE event received');
71 break;
72 case RawSocketEvent.READ_CLOSED:
73 Expect.fail('READ_CLOSED event received');
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 final data = createTestData();
94 int bytesWritten = 0;
95 socket.listen((event) {
96 switch (event) {
97 case RawSocketEvent.READ:
98 Expect.fail('READ event received');
99 break;
100 case RawSocketEvent.WRITE:
101 if (bytesWritten < data.length) {
102 bytesWritten += socket.write(data, bytesWritten);
103 }
104 if (bytesWritten < data.length) {
105 socket.writeEventsEnabled = true;
106 }
107 if (bytesWritten == data.length) {
108 baseSocket.write(data, 0, 300);
109 socket.shutdown(SocketDirection.SEND);
110 }
111 break;
112 case RawSocketEvent.READ_CLOSED:
113 completer.complete(null);
114 break;
115 default: throw "Unexpected event $event";
116 }
117 });
118 return completer.future;
119 }
120
121
122 Future<List> connectClient(int port) =>
123 RawSocket.connect(HOST_NAME, port)
124 .then((socket) =>
125 (hostnameInConnect ? RawSecureSocket.secure(socket)
126 : RawSecureSocket.secure(socket, host: HOST_NAME))
127 .then((secureSocket) => [socket, secureSocket]));
128
129
130 serverReady(server) {
131 server.listen((client) {
132 RawSecureSocket.secureServer(client, CERTIFICATE).then((secureClient) {
133 Expect.throws(() => client.add([0]));
134 runServer(secureClient).then((_) => server.close());
135 });
136 });
137
138 connectClient(server.port)
139 .then(runClient)
140 .then((_) => port.close());
141 }
142
143 RawServerSocket.bind(HOST_NAME, 0).then(serverReady);
144 }
145
146 main() {
147 Path scriptDir = new Path(Platform.script).directoryPath;
148 Path certificateDatabase = scriptDir.append('pkcert');
149 SecureSocket.initialize(database: certificateDatabase.toNativePath(),
150 password: 'dartdart',
151 useBuiltinRoots: false);
152 test(false);
153 test(true);
154 }
OLDNEW
« no previous file with comments | « sdk/lib/io/socket.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698