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

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

Issue 12328037: Add missing files from previous commit (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 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
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 "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 RawSecureServerSocket.bind(SERVER_ADDRESS, 65536, 5, CERTIFICATE));
21 Expect.throws(() =>
22 RawSecureServerSocket.bind(SERVER_ADDRESS, -1, CERTIFICATE));
23 Expect.throws(() =>
24 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, -1, CERTIFICATE));
25 }
26
27 void testSimpleBind() {
28 ReceivePort port = new ReceivePort();
29 RawSecureServerSocket.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 RawSecureServerSocket.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 RawSecureServerSocket.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 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((s) {
62 RawSecureServerSocket.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 })
72 .catchError((e) {
73 Expect.notEquals('windows', Platform.operatingSystem);
74 Expect.isTrue(e.error is SocketIOException);
75 s.close();
76 port.toSendPort().send(1);
77 });
78 });
79 }
80
81 void testSimpleConnect(String certificate) {
82 ReceivePort port = new ReceivePort();
83 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, certificate).then((server) {
84 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port);
85 server.listen((serverEnd) {
86 clientEndFuture.then((clientEnd) {
87 clientEnd.shutdown(SocketDirection.SEND);
88 serverEnd.shutdown(SocketDirection.SEND);
89 server.close();
90 port.close();
91 });
92 });
93 });
94 }
95
96 void testSimpleConnectFail(String certificate) {
97 ReceivePort port = new ReceivePort();
98 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, certificate).then((server) {
99 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port)
100 .then((clientEnd) {
101 Expect.fail("No client connection expected.");
102 })
103 .catchError((e) {
104 Expect.isTrue(e is AsyncError);
105 Expect.isTrue(e.error is SocketIOException);
106 });
107 server.listen((serverEnd) {
108 Expect.fail("No server connection expected.");
109 },
110 onError: (e) {
111 Expect.isTrue(e is AsyncError);
112 Expect.isTrue(e.error is SocketIOException);
113 clientEndFuture.then((_) => port.close());
114 });
115 });
116 }
117
118 void testServerListenAfterConnect() {
119 ReceivePort port = new ReceivePort();
120 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) {
121 Expect.isTrue(server.port > 0);
122 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port);
123 new Timer(500, (_) {
124 server.listen((serverEnd) {
125 clientEndFuture.then((clientEnd) {
126 clientEnd.shutdown(SocketDirection.SEND);
127 serverEnd.shutdown(SocketDirection.SEND);
128 server.close();
129 port.close();
130 });
131 });
132 });
133 });
134 }
135
136 void testSimpleReadWrite() {
137 // This test creates a server and a client connects. The client then
138 // writes and the server echos. When the server has finished its
139 // echo it half-closes. When the client gets the close event is
140 // closes fully.
141 ReceivePort port = new ReceivePort();
142
143 const messageSize = 1000;
144
145 List<int> createTestData() {
146 List<int> data = new List.fixedLength(messageSize);
147 for (int i = 0; i < messageSize; i++) {
148 data[i] = i & 0xff;
149 }
150 return data;
151 }
152
153 void verifyTestData(List<int> data) {
154 Expect.equals(messageSize, data.length);
155 List<int> expected = createTestData();
156 for (int i = 0; i < messageSize; i++) {
157 Expect.equals(expected[i], data[i]);
158 }
159 }
160
161 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) {
162 server.listen((client) {
163 int bytesRead = 0;
164 int bytesWritten = 0;
165 List<int> data = new List.fixedLength(messageSize);
166
167 client.writeEventsEnabled = false;
168 client.listen((event) {
169 switch (event) {
170 case RawSocketEvent.READ:
171 Expect.isTrue(bytesWritten == 0);
172 Expect.isTrue(client.available() > 0);
173 var buffer = client.read();
174 data.setRange(bytesRead, buffer.length, buffer);
175 bytesRead += buffer.length;
176 if (bytesRead == data.length) {
177 verifyTestData(data);
178 client.writeEventsEnabled = true;
179 }
180 break;
181 case RawSocketEvent.WRITE:
182 Expect.isFalse(client.writeEventsEnabled);
183 bytesWritten += client.write(
184 data, bytesWritten, data.length - bytesWritten);
185 if (bytesWritten < data.length) {
186 client.writeEventsEnabled = true;
187 }
188 if (bytesWritten == data.length) {
189 client.shutdown(SocketDirection.SEND);
190 }
191 break;
192 case RawSocketEvent.READ_CLOSED:
193 server.close();
194 break;
195 default: throw "Unexpected event $event";
196 }
197 });
198 });
199
200 RawSecureSocket.connect(HOST_NAME, server.port).then((socket) {
201 int bytesRead = 0;
202 int bytesWritten = 0;
203 List<int> dataSent = createTestData();
204 List<int> dataReceived = new List<int>.fixedLength(dataSent.length);
205 socket.listen((event) {
206 switch (event) {
207 case RawSocketEvent.READ:
208 Expect.isTrue(socket.available() > 0);
209 var buffer = socket.read();
210 dataReceived.setRange(bytesRead, buffer.length, buffer);
211 bytesRead += buffer.length;
212 break;
213 case RawSocketEvent.WRITE:
214 Expect.isTrue(bytesRead == 0);
215 Expect.isFalse(socket.writeEventsEnabled);
216 bytesWritten += socket.write(
217 dataSent, bytesWritten, dataSent.length - bytesWritten);
218 if (bytesWritten < dataSent.length) {
219 socket.writeEventsEnabled = true;
220 }
221 break;
222 case RawSocketEvent.READ_CLOSED:
223 verifyTestData(dataReceived);
224 socket.close();
225 port.close();
226 break;
227 default: throw "Unexpected event $event";
228 }
229 });
230 });
231 });
232 }
233
234 main() {
235 Path scriptDir = new Path(new Options().script).directoryPath;
236 Path certificateDatabase = scriptDir.append('pkcert');
237 SecureSocket.initialize(database: certificateDatabase.toNativePath(),
238 password: 'dartdart',
239 useBuiltinRoots: false);
240 testArguments();
241 testSimpleBind();
242 testInvalidBind();
243 testSimpleConnect(CERTIFICATE);
244 testSimpleConnect("CN=localhost");
245 testSimpleConnectFail("not_a_nickname");
246 testSimpleConnectFail("CN=notARealDistinguishedName");
247 testServerListenAfterConnect();
248 testSimpleReadWrite();
249 }
OLDNEW
« no previous file with comments | « tests/standalone/io/raw_secure_server_closing_test.dart ('k') | tests/standalone/io/raw_secure_socket_pause_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698