OLD | NEW |
(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 |
| 6 import "dart:async"; |
| 7 import "dart:io"; |
| 8 |
| 9 import "package:async_helper/async_helper.dart"; |
| 10 import "package:expect/expect.dart"; |
| 11 |
| 12 Future throws(Function f, Function check) async { |
| 13 try { |
| 14 await f(); |
| 15 Expect.fail('Did not throw'); |
| 16 } catch (e) { |
| 17 if (check != null) { |
| 18 if (!check(e)) { |
| 19 Expect.fail('Unexpected: $e'); |
| 20 } |
| 21 } |
| 22 } |
| 23 } |
| 24 |
| 25 Future testArguments(connectFunction) async { |
| 26 asyncStart(); |
| 27 var server = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); |
| 28 server.listen((_) { throw 'Connection unexpected'; }, |
| 29 onDone: () => asyncEnd()); |
| 30 |
| 31 asyncStart(); |
| 32 // Illegal type for sourceAddress. |
| 33 for (var sourceAddress in ['www.google.com', 'abc']) { |
| 34 await throws(() => connectFunction('127.0.0.1', |
| 35 server.port, |
| 36 sourceAddress: sourceAddress), |
| 37 (e) => e is ArgumentError); |
| 38 } |
| 39 // Unsupported local address. |
| 40 for (var sourceAddress in ['8.8.8.8', new InternetAddress('8.8.8.8')]) { |
| 41 await throws(() => connectFunction('127.0.0.1', |
| 42 server.port, |
| 43 sourceAddress: sourceAddress), |
| 44 (e) => e is SocketException); |
| 45 } |
| 46 // Address family mismatch. |
| 47 for (var sourceAddress in ['::1', InternetAddress.LOOPBACK_IP_V6]) { |
| 48 await throws(() => connectFunction('127.0.0.1', |
| 49 server.port, |
| 50 sourceAddress: sourceAddress), |
| 51 (e) => e is SocketException); |
| 52 } |
| 53 asyncEnd(); |
| 54 server.close(); |
| 55 } |
| 56 |
| 57 // IPv4 addresses to use as source address when connecting locally. |
| 58 var ipV4SourceAddresses = [InternetAddress.LOOPBACK_IP_V4, |
| 59 InternetAddress.ANY_IP_V4, |
| 60 '127.0.0.1', |
| 61 '0.0.0.0']; |
| 62 |
| 63 // IPv6 addresses to use as source address when connecting locally. |
| 64 var ipV6SourceAddresses = [InternetAddress.LOOPBACK_IP_V6, |
| 65 InternetAddress.ANY_IP_V6, |
| 66 '::1', |
| 67 '::']; |
| 68 |
| 69 Future testConnect(InternetAddress bindAddress, |
| 70 bool v6Only, |
| 71 Function connectFunction, |
| 72 Function closeDestroyFunction) async { |
| 73 asyncStart(); |
| 74 var server = await ServerSocket.bind(bindAddress, 0, v6Only: v6Only); |
| 75 server.listen((s) { |
| 76 s.destroy(); |
| 77 asyncEnd(); |
| 78 }, onDone: () => asyncEnd()); |
| 79 |
| 80 asyncStart(); |
| 81 |
| 82 // Connect with IPv4 source addesses. |
| 83 for (var sourceAddress in ipV4SourceAddresses) { |
| 84 if (!v6Only) { |
| 85 asyncStart(); // Matching asyncEnd in server.listen above. |
| 86 var s = await connectFunction(InternetAddress.LOOPBACK_IP_V4, |
| 87 server.port, |
| 88 sourceAddress: sourceAddress); |
| 89 closeDestroyFunction(s); |
| 90 } else { |
| 91 // Cannot use an IPv6 source address to connect to IPv6 if |
| 92 // v6Only is specified. |
| 93 await throws(() => connectFunction(InternetAddress.LOOPBACK_IP_V4, |
| 94 server.port, |
| 95 sourceAddress: sourceAddress), |
| 96 (e) => e is SocketException); |
| 97 } |
| 98 } |
| 99 |
| 100 // Connect with IPv6 source addesses. |
| 101 for (var sourceAddress in ipV6SourceAddresses) { |
| 102 if (bindAddress.type == InternetAddressType.IP_V6) { |
| 103 asyncStart(); // Matching asyncEnd in server.listen above. |
| 104 var s = await connectFunction(InternetAddress.LOOPBACK_IP_V6, |
| 105 server.port, |
| 106 sourceAddress: sourceAddress); |
| 107 closeDestroyFunction(s); |
| 108 } else { |
| 109 // Cannot use an IPv6 source address to connect to IPv4. |
| 110 await throws(() => connectFunction(InternetAddress.LOOPBACK_IP_V6, |
| 111 server.port, |
| 112 sourceAddress: sourceAddress), |
| 113 (e) => e is SocketException); |
| 114 } |
| 115 } |
| 116 |
| 117 server.close(); |
| 118 asyncEnd(); |
| 119 } |
| 120 |
| 121 main() { |
| 122 testArguments(RawSocket.connect); |
| 123 testArguments(Socket.connect); |
| 124 testConnect( |
| 125 InternetAddress.ANY_IP_V4, false, RawSocket.connect, (s) => s.close()); |
| 126 testConnect( |
| 127 InternetAddress.ANY_IP_V4, false, Socket.connect, (s) => s.destroy()); |
| 128 testConnect( |
| 129 InternetAddress.ANY_IP_V6, false, RawSocket.connect, (s) => s.close()); |
| 130 testConnect( |
| 131 InternetAddress.ANY_IP_V6, false, Socket.connect, (s) => s.destroy()); |
| 132 testConnect( |
| 133 InternetAddress.ANY_IP_V6, true, RawSocket.connect, (s) => s.close()); |
| 134 testConnect( |
| 135 InternetAddress.ANY_IP_V6, true, Socket.connect, (s) => s.destroy()); |
| 136 } |
OLD | NEW |