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