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

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

Issue 201993002: Fix SecureSocket tests to look up localhost only once, where possible. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove useless change to https_client_certificate_test.dart Created 6 years, 9 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
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"; 10 import "dart:async";
11 import "dart:io"; 11 import "dart:io";
12 12
13 import "package:async_helper/async_helper.dart"; 13 import "package:async_helper/async_helper.dart";
14 import "package:expect/expect.dart"; 14 import "package:expect/expect.dart";
15 import "package:path/path.dart";
16 15
17 const HOST_NAME = "localhost"; 16 InternetAddress HOST;
18 const CERTIFICATE = "localhost_cert"; 17 const CERTIFICATE = "localhost_cert";
19 18
20 void testSimpleBind() { 19 void testSimpleBind() {
21 asyncStart(); 20 asyncStart();
22 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((s) { 21 SecureServerSocket.bind(HOST, 0, CERTIFICATE).then((s) {
23 Expect.isTrue(s.port > 0); 22 Expect.isTrue(s.port > 0);
24 s.close(); 23 s.close();
25 asyncEnd(); 24 asyncEnd();
26 }); 25 });
27 } 26 }
28 27
29 void testInvalidBind() { 28 void testInvalidBind() {
30 int count = 0; 29 int count = 0;
31 30
32 // Bind to a unknown DNS name. 31 // Bind to a unknown DNS name.
33 asyncStart(); 32 asyncStart();
34 SecureServerSocket.bind("ko.faar.__hest__", 0, CERTIFICATE).then((_) { 33 SecureServerSocket.bind("ko.faar.__hest__", 0, CERTIFICATE).then((_) {
35 Expect.fail("Failure expected"); 34 Expect.fail("Failure expected");
36 }).catchError((error) { 35 }).catchError((error) {
37 Expect.isTrue(error is SocketException); 36 Expect.isTrue(error is SocketException);
38 asyncEnd(); 37 asyncEnd();
39 }); 38 });
40 39
41 // Bind to an unavaliable IP-address. 40 // Bind to an unavaliable IP-address.
42 asyncStart(); 41 asyncStart();
43 SecureServerSocket.bind("8.8.8.8", 0, CERTIFICATE).then((_) { 42 SecureServerSocket.bind("8.8.8.8", 0, CERTIFICATE).then((_) {
44 Expect.fail("Failure expected"); 43 Expect.fail("Failure expected");
45 }).catchError((error) { 44 }).catchError((error) {
46 Expect.isTrue(error is SocketException); 45 Expect.isTrue(error is SocketException);
47 asyncEnd(); 46 asyncEnd();
48 }); 47 });
49 48
50 // Bind to a port already in use. 49 // Bind to a port already in use.
51 asyncStart(); 50 asyncStart();
52 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((s) { 51 SecureServerSocket.bind(HOST, 0, CERTIFICATE).then((s) {
53 SecureServerSocket.bind(HOST_NAME, 52 SecureServerSocket.bind(HOST,
54 s.port, 53 s.port,
55 CERTIFICATE).then((t) { 54 CERTIFICATE).then((t) {
56 Expect.fail("Multiple listens on same port"); 55 Expect.fail("Multiple listens on same port");
57 }).catchError((error) { 56 }).catchError((error) {
58 Expect.isTrue(error is SocketException); 57 Expect.isTrue(error is SocketException);
59 s.close(); 58 s.close();
60 asyncEnd(); 59 asyncEnd();
61 }); 60 });
62 }); 61 });
63 } 62 }
64 63
65 void testSimpleConnect(String certificate) { 64 void testSimpleConnect(String certificate) {
66 asyncStart(); 65 asyncStart();
67 SecureServerSocket.bind(HOST_NAME, 0, certificate).then((server) { 66 SecureServerSocket.bind(HOST, 0, certificate).then((server) {
68 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); 67 var clientEndFuture = SecureSocket.connect(HOST, server.port);
69 server.listen((serverEnd) { 68 server.listen((serverEnd) {
70 clientEndFuture.then((clientEnd) { 69 clientEndFuture.then((clientEnd) {
71 clientEnd.close(); 70 clientEnd.close();
72 serverEnd.close(); 71 serverEnd.close();
73 server.close(); 72 server.close();
74 asyncEnd(); 73 asyncEnd();
75 }); 74 });
76 }); 75 });
77 }); 76 });
78 } 77 }
79 78
80 void testSimpleConnectFail(String certificate, bool cancelOnError) { 79 void testSimpleConnectFail(String certificate, bool cancelOnError) {
81 asyncStart(); 80 asyncStart();
82 SecureServerSocket.bind(HOST_NAME, 0, certificate).then((server) { 81 SecureServerSocket.bind(HOST, 0, certificate).then((server) {
83 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port) 82 var clientEndFuture = SecureSocket.connect(HOST, server.port)
84 .then((clientEnd) { 83 .then((clientEnd) {
85 Expect.fail("No client connection expected."); 84 Expect.fail("No client connection expected.");
86 }) 85 })
87 .catchError((error) { 86 .catchError((error) {
88 Expect.isTrue(error is HandshakeException || 87 Expect.isTrue(error is HandshakeException ||
89 error is SocketException); 88 error is SocketException);
90 }); 89 });
91 server.listen((serverEnd) { 90 server.listen((serverEnd) {
92 Expect.fail("No server connection expected."); 91 Expect.fail("No server connection expected.");
93 }, 92 },
94 onError: (error) { 93 onError: (error) {
95 Expect.isTrue(error is CertificateException); 94 Expect.isTrue(error is CertificateException);
96 clientEndFuture.then((_) { 95 clientEndFuture.then((_) {
97 if (!cancelOnError) server.close(); 96 if (!cancelOnError) server.close();
98 asyncEnd(); 97 asyncEnd();
99 }); 98 });
100 }, 99 },
101 cancelOnError: cancelOnError); 100 cancelOnError: cancelOnError);
102 }); 101 });
103 } 102 }
104 103
105 void testServerListenAfterConnect() { 104 void testServerListenAfterConnect() {
106 asyncStart(); 105 asyncStart();
107 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((server) { 106 SecureServerSocket.bind(HOST, 0, CERTIFICATE).then((server) {
108 Expect.isTrue(server.port > 0); 107 Expect.isTrue(server.port > 0);
109 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); 108 var clientEndFuture = SecureSocket.connect(HOST, server.port);
110 new Timer(const Duration(milliseconds: 500), () { 109 new Timer(const Duration(milliseconds: 500), () {
111 server.listen((serverEnd) { 110 server.listen((serverEnd) {
112 clientEndFuture.then((clientEnd) { 111 clientEndFuture.then((clientEnd) {
113 clientEnd.close(); 112 clientEnd.close();
114 serverEnd.close(); 113 serverEnd.close();
115 server.close(); 114 server.close();
116 asyncEnd(); 115 asyncEnd();
117 }); 116 });
118 }); 117 });
119 }); 118 });
(...skipping 18 matching lines...) Expand all
138 } 137 }
139 138
140 void verifyTestData(List<int> data) { 139 void verifyTestData(List<int> data) {
141 Expect.equals(messageSize, data.length); 140 Expect.equals(messageSize, data.length);
142 List<int> expected = createTestData(); 141 List<int> expected = createTestData();
143 for (int i = 0; i < messageSize; i++) { 142 for (int i = 0; i < messageSize; i++) {
144 Expect.equals(expected[i], data[i]); 143 Expect.equals(expected[i], data[i]);
145 } 144 }
146 } 145 }
147 146
148 SecureServerSocket.bind(HOST_NAME, 0, CERTIFICATE).then((server) { 147 SecureServerSocket.bind(HOST, 0, CERTIFICATE).then((server) {
149 server.listen((client) { 148 server.listen((client) {
150 int bytesRead = 0; 149 int bytesRead = 0;
151 int bytesWritten = 0; 150 int bytesWritten = 0;
152 List<int> data = new List<int>(messageSize); 151 List<int> data = new List<int>(messageSize);
153 152
154 client.listen( 153 client.listen(
155 (buffer) { 154 (buffer) {
156 Expect.isTrue(bytesWritten == 0); 155 Expect.isTrue(bytesWritten == 0);
157 data.setRange(bytesRead, bytesRead + buffer.length, buffer); 156 data.setRange(bytesRead, bytesRead + buffer.length, buffer);
158 bytesRead += buffer.length; 157 bytesRead += buffer.length;
159 if (bytesRead == data.length) { 158 if (bytesRead == data.length) {
160 verifyTestData(data); 159 verifyTestData(data);
161 client.add(data); 160 client.add(data);
162 client.close(); 161 client.close();
163 } 162 }
164 }, 163 },
165 onDone: () { 164 onDone: () {
166 server.close(); 165 server.close();
167 }); 166 });
168 }); 167 });
169 168
170 SecureSocket.connect(HOST_NAME, server.port).then((socket) { 169 SecureSocket.connect(HOST, server.port).then((socket) {
171 int bytesRead = 0; 170 int bytesRead = 0;
172 int bytesWritten = 0; 171 int bytesWritten = 0;
173 List<int> dataSent = createTestData(); 172 List<int> dataSent = createTestData();
174 List<int> dataReceived = new List<int>(dataSent.length); 173 List<int> dataReceived = new List<int>(dataSent.length);
175 socket.add(dataSent); 174 socket.add(dataSent);
176 socket.close(); // Can also be delayed. 175 socket.close(); // Can also be delayed.
177 socket.listen( 176 socket.listen(
178 (List<int> buffer) { 177 (List<int> buffer) {
179 dataReceived.setRange(bytesRead, bytesRead + buffer.length, buffer); 178 dataReceived.setRange(bytesRead, bytesRead + buffer.length, buffer);
180 bytesRead += buffer.length; 179 bytesRead += buffer.length;
181 }, 180 },
182 onDone: () { 181 onDone: () {
183 verifyTestData(dataReceived); 182 verifyTestData(dataReceived);
184 socket.close(); 183 socket.close();
185 asyncEnd(); 184 asyncEnd();
186 }); 185 });
187 }); 186 });
188 }); 187 });
189 } 188 }
190 189
191 main() { 190 main() {
192 asyncStart(); 191 asyncStart();
193 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); 192 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath();
194 SecureSocket.initialize(database: certificateDatabase, 193 SecureSocket.initialize(database: certificateDatabase,
195 password: 'dartdart', 194 password: 'dartdart',
196 useBuiltinRoots: false); 195 useBuiltinRoots: false);
196 InternetAddress.lookup("localhost").then((hosts) {
197 HOST = hosts.first;
198 runTests();
199 asyncEnd();
200 });
201 }
202
203 runTests() {
197 testSimpleBind(); 204 testSimpleBind();
198 testInvalidBind(); 205 testInvalidBind();
199 testSimpleConnect(CERTIFICATE); 206 testSimpleConnect(CERTIFICATE);
200 testSimpleConnect("CN=localhost"); 207 testSimpleConnect("CN=localhost");
201 testSimpleConnectFail("not_a_nickname", false); 208 testSimpleConnectFail("not_a_nickname", false);
202 testSimpleConnectFail("CN=notARealDistinguishedName", false); 209 testSimpleConnectFail("CN=notARealDistinguishedName", false);
203 testSimpleConnectFail("not_a_nickname", true); 210 testSimpleConnectFail("not_a_nickname", true);
204 testSimpleConnectFail("CN=notARealDistinguishedName", true); 211 testSimpleConnectFail("CN=notARealDistinguishedName", true);
205 testServerListenAfterConnect(); 212 testServerListenAfterConnect();
206 testSimpleReadWrite(); 213 testSimpleReadWrite();
207 asyncEnd();
208 } 214 }
OLDNEW
« no previous file with comments | « tests/standalone/io/secure_server_closing_test.dart ('k') | tests/standalone/io/secure_session_resume_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698