OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import "dart:async"; | 5 import "dart:async"; |
6 import "dart:convert"; | 6 import "dart:convert"; |
7 import "dart:io"; | 7 import "dart:io"; |
8 | 8 |
9 testAddressParse() async { | 9 testAddressParse() async { |
10 print(new InternetAddress("1.0.2.3").rawAddress); | 10 print(new InternetAddress("1.0.2.3").rawAddress); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 bytesRead += buffer.length; | 92 bytesRead += buffer.length; |
93 if (bytesRead == data.length) { | 93 if (bytesRead == data.length) { |
94 verifyTestData(data); | 94 verifyTestData(data); |
95 print("client READ event. Done reading, enabling writes"); | 95 print("client READ event. Done reading, enabling writes"); |
96 client.writeEventsEnabled = true; | 96 client.writeEventsEnabled = true; |
97 doneReading = true; | 97 doneReading = true; |
98 } | 98 } |
99 break; | 99 break; |
100 case RawSocketEvent.WRITE: | 100 case RawSocketEvent.WRITE: |
101 assert(!client.writeEventsEnabled); | 101 assert(!client.writeEventsEnabled); |
102 bytesWritten += client.write( | 102 bytesWritten += |
103 data, bytesWritten, data.length - bytesWritten); | 103 client.write(data, bytesWritten, data.length - bytesWritten); |
104 print("client WRITE event: $bytesWritten written"); | 104 print("client WRITE event: $bytesWritten written"); |
105 if (bytesWritten < data.length) { | 105 if (bytesWritten < data.length) { |
106 client.writeEventsEnabled = true; | 106 client.writeEventsEnabled = true; |
107 } | 107 } |
108 if (bytesWritten == data.length) { | 108 if (bytesWritten == data.length) { |
109 print("client WRITE event: done writing."); | 109 print("client WRITE event: done writing."); |
110 } | 110 } |
111 break; | 111 break; |
112 case RawSocketEvent.READ_CLOSED: | 112 case RawSocketEvent.READ_CLOSED: |
113 print("client READ_CLOSED event"); | 113 print("client READ_CLOSED event"); |
114 client.close(); | 114 client.close(); |
115 server.close(); | 115 server.close(); |
116 break; | 116 break; |
117 case RawSocketEvent.CLOSED: | 117 case RawSocketEvent.CLOSED: |
118 assert(!closedEventReceived); | 118 assert(!closedEventReceived); |
119 print("client CLOSED event"); | 119 print("client CLOSED event"); |
120 closedEventReceived = true; | 120 closedEventReceived = true; |
121 break; | 121 break; |
122 default: throw "Unexpected event $event"; | 122 default: |
| 123 throw "Unexpected event $event"; |
123 } | 124 } |
124 }, | 125 }, onError: (e) { |
125 onError: (e) { print("client ERROR $e"); }, | 126 print("client ERROR $e"); |
126 onDone: () {assert(closedEventReceived);}); | 127 }, onDone: () { |
| 128 assert(closedEventReceived); |
| 129 }); |
127 }); | 130 }); |
128 | 131 |
129 { | 132 { |
130 var completer = new Completer(); | 133 var completer = new Completer(); |
131 var socket = await RawSocket.connect("127.0.0.1", server.port); | 134 var socket = await RawSocket.connect("127.0.0.1", server.port); |
132 int bytesRead = 0; | 135 int bytesRead = 0; |
133 int bytesWritten = 0; | 136 int bytesWritten = 0; |
134 bool closedEventReceived = false; | 137 bool closedEventReceived = false; |
135 List<int> data = createTestData(); | 138 List<int> data = createTestData(); |
136 | 139 |
137 socket.listen((event) { | 140 socket.listen((event) { |
138 switch (event) { | 141 switch (event) { |
139 case RawSocketEvent.READ: | 142 case RawSocketEvent.READ: |
140 assert(socket.available() > 0); | 143 assert(socket.available() > 0); |
141 print("server READ event: ${bytesRead} read"); | 144 print("server READ event: ${bytesRead} read"); |
142 var buffer = socket.read(); | 145 var buffer = socket.read(); |
143 print("server READ event: read ${buffer.length} more bytes"); | 146 print("server READ event: read ${buffer.length} more bytes"); |
144 data.setRange(bytesRead, bytesRead + buffer.length, buffer); | 147 data.setRange(bytesRead, bytesRead + buffer.length, buffer); |
145 bytesRead += buffer.length; | 148 bytesRead += buffer.length; |
146 if (bytesRead == messageSize) { | 149 if (bytesRead == messageSize) { |
147 print("server READ event: done reading"); | 150 print("server READ event: done reading"); |
148 socket.close(); | 151 socket.close(); |
149 } | 152 } |
150 break; | 153 break; |
151 case RawSocketEvent.WRITE: | 154 case RawSocketEvent.WRITE: |
152 assert(bytesRead == 0); | 155 assert(bytesRead == 0); |
153 assert(!socket.writeEventsEnabled); | 156 assert(!socket.writeEventsEnabled); |
154 bytesWritten += socket.write( | 157 bytesWritten += |
155 data, bytesWritten, data.length - bytesWritten); | 158 socket.write(data, bytesWritten, data.length - bytesWritten); |
156 print("server WRITE event: ${bytesWritten} written"); | 159 print("server WRITE event: ${bytesWritten} written"); |
157 if (bytesWritten < data.length) { | 160 if (bytesWritten < data.length) { |
158 socket.writeEventsEnabled = true; | 161 socket.writeEventsEnabled = true; |
159 } else { | 162 } else { |
160 print("server WRITE event: done writing"); | 163 print("server WRITE event: done writing"); |
161 data = new List<int>(messageSize); | 164 data = new List<int>(messageSize); |
162 } | 165 } |
163 break; | 166 break; |
164 case RawSocketEvent.READ_CLOSED: | 167 case RawSocketEvent.READ_CLOSED: |
165 print("server READ_CLOSED event"); | 168 print("server READ_CLOSED event"); |
166 verifyTestData(data); | 169 verifyTestData(data); |
167 socket.close(); | 170 socket.close(); |
168 break; | 171 break; |
169 case RawSocketEvent.CLOSED: | 172 case RawSocketEvent.CLOSED: |
170 assert(!closedEventReceived); | 173 assert(!closedEventReceived); |
171 print("server CLOSED event"); | 174 print("server CLOSED event"); |
172 closedEventReceived = true; | 175 closedEventReceived = true; |
173 break; | 176 break; |
174 default: throw "Unexpected event $event"; | 177 default: |
| 178 throw "Unexpected event $event"; |
175 } | 179 } |
176 }, | 180 }, onError: (e) { |
177 onError: (e) { print("server ERROR $e"); }, | 181 print("server ERROR $e"); |
178 onDone: () { | 182 }, onDone: () { |
179 assert(closedEventReceived); | 183 assert(closedEventReceived); |
180 completer.complete(null); | 184 completer.complete(null); |
181 }); | 185 }); |
182 | 186 |
183 return completer.future; | 187 return completer.future; |
184 } | 188 } |
185 } | 189 } |
186 | 190 |
187 testSimpleReadWriteShutdown({bool dropReads}) async { | 191 testSimpleReadWriteShutdown({bool dropReads}) async { |
188 // This test creates a server and a client connects. The client then | 192 // This test creates a server and a client connects. The client then |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 bytesRead += buffer.length; | 242 bytesRead += buffer.length; |
239 if (bytesRead == data.length) { | 243 if (bytesRead == data.length) { |
240 verifyTestData(data); | 244 verifyTestData(data); |
241 print("client READ event. Done reading, enabling writes"); | 245 print("client READ event. Done reading, enabling writes"); |
242 client.writeEventsEnabled = true; | 246 client.writeEventsEnabled = true; |
243 doneReading = true; | 247 doneReading = true; |
244 } | 248 } |
245 break; | 249 break; |
246 case RawSocketEvent.WRITE: | 250 case RawSocketEvent.WRITE: |
247 assert(!client.writeEventsEnabled); | 251 assert(!client.writeEventsEnabled); |
248 bytesWritten += client.write( | 252 bytesWritten += |
249 data, bytesWritten, data.length - bytesWritten); | 253 client.write(data, bytesWritten, data.length - bytesWritten); |
250 print("client WRITE event: $bytesWritten written"); | 254 print("client WRITE event: $bytesWritten written"); |
251 if (bytesWritten < data.length) { | 255 if (bytesWritten < data.length) { |
252 client.writeEventsEnabled = true; | 256 client.writeEventsEnabled = true; |
253 } | 257 } |
254 if (bytesWritten == data.length) { | 258 if (bytesWritten == data.length) { |
255 print("client WRITE event: done writing."); | 259 print("client WRITE event: done writing."); |
256 client.shutdown(SocketDirection.SEND); | 260 client.shutdown(SocketDirection.SEND); |
257 } | 261 } |
258 break; | 262 break; |
259 case RawSocketEvent.READ_CLOSED: | 263 case RawSocketEvent.READ_CLOSED: |
260 print("client READ_CLOSED event"); | 264 print("client READ_CLOSED event"); |
261 server.close(); | 265 server.close(); |
262 break; | 266 break; |
263 case RawSocketEvent.CLOSED: | 267 case RawSocketEvent.CLOSED: |
264 assert(!closedEventReceived); | 268 assert(!closedEventReceived); |
265 print("client CLOSED event"); | 269 print("client CLOSED event"); |
266 closedEventReceived = true; | 270 closedEventReceived = true; |
267 break; | 271 break; |
268 default: throw "Unexpected event $event"; | 272 default: |
| 273 throw "Unexpected event $event"; |
269 } | 274 } |
270 }, | 275 }, onDone: () { |
271 onDone: () {assert(closedEventReceived);}); | 276 assert(closedEventReceived); |
| 277 }); |
272 }); | 278 }); |
273 | 279 |
274 { | 280 { |
275 var completer = new Completer(); | 281 var completer = new Completer(); |
276 var socket = await RawSocket.connect("127.0.0.1", server.port); | 282 var socket = await RawSocket.connect("127.0.0.1", server.port); |
277 int bytesRead = 0; | 283 int bytesRead = 0; |
278 int bytesWritten = 0; | 284 int bytesWritten = 0; |
279 bool closedEventReceived = false; | 285 bool closedEventReceived = false; |
280 List<int> data = createTestData(); | 286 List<int> data = createTestData(); |
281 | 287 |
(...skipping 11 matching lines...) Expand all Loading... |
293 } | 299 } |
294 print("server READ event: ${bytesRead} read"); | 300 print("server READ event: ${bytesRead} read"); |
295 var buffer = socket.read(); | 301 var buffer = socket.read(); |
296 print("server READ event: read ${buffer.length} more bytes"); | 302 print("server READ event: read ${buffer.length} more bytes"); |
297 data.setRange(bytesRead, bytesRead + buffer.length, buffer); | 303 data.setRange(bytesRead, bytesRead + buffer.length, buffer); |
298 bytesRead += buffer.length; | 304 bytesRead += buffer.length; |
299 break; | 305 break; |
300 case RawSocketEvent.WRITE: | 306 case RawSocketEvent.WRITE: |
301 assert(bytesRead == 0); | 307 assert(bytesRead == 0); |
302 assert(!socket.writeEventsEnabled); | 308 assert(!socket.writeEventsEnabled); |
303 bytesWritten += socket.write( | 309 bytesWritten += |
304 data, bytesWritten, data.length - bytesWritten); | 310 socket.write(data, bytesWritten, data.length - bytesWritten); |
305 print("server WRITE event: ${bytesWritten} written"); | 311 print("server WRITE event: ${bytesWritten} written"); |
306 if (bytesWritten < data.length) { | 312 if (bytesWritten < data.length) { |
307 socket.writeEventsEnabled = true; | 313 socket.writeEventsEnabled = true; |
308 } else { | 314 } else { |
309 print("server WRITE event: done writing"); | 315 print("server WRITE event: done writing"); |
310 data = new List<int>(messageSize); | 316 data = new List<int>(messageSize); |
311 } | 317 } |
312 break; | 318 break; |
313 case RawSocketEvent.READ_CLOSED: | 319 case RawSocketEvent.READ_CLOSED: |
314 print("server READ_CLOSED event"); | 320 print("server READ_CLOSED event"); |
315 verifyTestData(data); | 321 verifyTestData(data); |
316 socket.close(); | 322 socket.close(); |
317 break; | 323 break; |
318 case RawSocketEvent.CLOSED: | 324 case RawSocketEvent.CLOSED: |
319 assert(!closedEventReceived); | 325 assert(!closedEventReceived); |
320 print("server CLOSED event"); | 326 print("server CLOSED event"); |
321 closedEventReceived = true; | 327 closedEventReceived = true; |
322 break; | 328 break; |
323 default: throw "Unexpected event $event"; | 329 default: |
| 330 throw "Unexpected event $event"; |
324 } | 331 } |
325 }, | 332 }, onDone: () { |
326 onDone: () { | |
327 assert(closedEventReceived); | 333 assert(closedEventReceived); |
328 completer.complete(null); | 334 completer.complete(null); |
329 }); | 335 }); |
330 | 336 |
331 return completer.future; | 337 return completer.future; |
332 } | 338 } |
333 } | 339 } |
334 | 340 |
335 Future testGoogleHttp(SecurityContext context, String outcome) async { | 341 Future testGoogleHttp(SecurityContext context, String outcome) async { |
336 var client = new HttpClient(context: context); | 342 var client = new HttpClient(context: context); |
337 try { | 343 try { |
338 // First, check if the lookup works. | 344 // First, check if the lookup works. |
339 var address = await InternetAddress.lookup('www.google.com'); | 345 var address = await InternetAddress.lookup('www.google.com'); |
340 print(address); | 346 print(address); |
341 var request = await client.getUrl(Uri.parse('http://www.google.com/')); | 347 var request = await client.getUrl(Uri.parse('http://www.google.com/')); |
342 request.followRedirects = false; | 348 request.followRedirects = false; |
343 var response = await request.close(); | 349 var response = await request.close(); |
344 assert('pass' == outcome); | 350 assert('pass' == outcome); |
345 try { await response.drain(); } catch (e) { | 351 try { |
| 352 await response.drain(); |
| 353 } catch (e) { |
346 print('drain failed: $e'); | 354 print('drain failed: $e'); |
347 } | 355 } |
348 } catch (e) { | 356 } catch (e) { |
349 // Lookup failed or connection failed. Don't report a failure. | 357 // Lookup failed or connection failed. Don't report a failure. |
350 print("SocketException: $e"); | 358 print("SocketException: $e"); |
351 } finally { | 359 } finally { |
352 client.close(); | 360 client.close(); |
353 } | 361 } |
354 } | 362 } |
355 | 363 |
356 Future testGoogleHttps(SecurityContext context, String outcome) async { | 364 Future testGoogleHttps(SecurityContext context, String outcome) async { |
357 // If this isn't reasonable, the certificate will be rejected. | 365 // If this isn't reasonable, the certificate will be rejected. |
358 print(new DateTime.now()); | 366 print(new DateTime.now()); |
359 | 367 |
360 var client = new HttpClient(context: context); | 368 var client = new HttpClient(context: context); |
361 // We need to use an external server that is backed by a | 369 // We need to use an external server that is backed by a |
362 // built-in root certificate authority. | 370 // built-in root certificate authority. |
363 try { | 371 try { |
364 // First, check if the lookup works. | 372 // First, check if the lookup works. |
365 var address = await InternetAddress.lookup('www.google.com'); | 373 var address = await InternetAddress.lookup('www.google.com'); |
366 print(address); | 374 print(address); |
367 var request = await client.getUrl(Uri.parse('https://www.google.com/')); | 375 var request = await client.getUrl(Uri.parse('https://www.google.com/')); |
368 request.followRedirects = false; | 376 request.followRedirects = false; |
369 var response = await request.close(); | 377 var response = await request.close(); |
370 assert('pass' == outcome); | 378 assert('pass' == outcome); |
371 try { await response.drain(); } catch (e) { | 379 try { |
| 380 await response.drain(); |
| 381 } catch (e) { |
372 print('drain failed: $e'); | 382 print('drain failed: $e'); |
373 } | 383 } |
374 } catch (e) { | 384 } catch (e) { |
375 // Lookup failed or connection failed. Don't report a failure. | 385 // Lookup failed or connection failed. Don't report a failure. |
376 print("SocketException: $e"); | 386 print("SocketException: $e"); |
377 } finally { | 387 } finally { |
378 client.close(); | 388 client.close(); |
379 } | 389 } |
380 } | 390 } |
381 | 391 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 print("testCopy"); | 534 print("testCopy"); |
525 await testCopy(); | 535 await testCopy(); |
526 print("testCopy done"); | 536 print("testCopy done"); |
527 | 537 |
528 print("testRecursiveDelete"); | 538 print("testRecursiveDelete"); |
529 await testRecursiveDelete(); | 539 await testRecursiveDelete(); |
530 print("testRecursiveDelete done"); | 540 print("testRecursiveDelete done"); |
531 | 541 |
532 print("Goodbyte, Fuchsia!"); | 542 print("Goodbyte, Fuchsia!"); |
533 } | 543 } |
OLD | NEW |