| OLD | NEW |
| 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 | 5 |
| 6 import "dart:async"; | 6 import "dart:async"; |
| 7 import "dart:io"; | 7 import "dart:io"; |
| 8 | 8 |
| 9 import "package:async_helper/async_helper.dart"; | 9 import "package:async_helper/async_helper.dart"; |
| 10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // IPv6 addresses to use as source address when connecting locally. | 63 // IPv6 addresses to use as source address when connecting locally. |
| 64 var ipV6SourceAddresses = [InternetAddress.LOOPBACK_IP_V6, | 64 var ipV6SourceAddresses = [InternetAddress.LOOPBACK_IP_V6, |
| 65 InternetAddress.ANY_IP_V6, | 65 InternetAddress.ANY_IP_V6, |
| 66 '::1', | 66 '::1', |
| 67 '::']; | 67 '::']; |
| 68 | 68 |
| 69 Future testConnect(InternetAddress bindAddress, | 69 Future testConnect(InternetAddress bindAddress, |
| 70 bool v6Only, | 70 bool v6Only, |
| 71 Function connectFunction, | 71 Function connectFunction, |
| 72 Function closeDestroyFunction) async { | 72 Function closeDestroyFunction) async { |
| 73 var successCount = 0; |
| 74 if (!v6Only) successCount += ipV4SourceAddresses.length; |
| 75 if (bindAddress.type == InternetAddressType.IP_V6) { |
| 76 successCount += ipV6SourceAddresses.length; |
| 77 } |
| 78 var count = 0; |
| 79 var allConnected = new Completer(); |
| 80 if (successCount == 0) allConnected.complete(); |
| 81 |
| 73 asyncStart(); | 82 asyncStart(); |
| 74 var server = await ServerSocket.bind(bindAddress, 0, v6Only: v6Only); | 83 var server = await ServerSocket.bind(bindAddress, 0, v6Only: v6Only); |
| 75 server.listen((s) { | 84 server.listen((s) { |
| 76 s.destroy(); | 85 s.destroy(); |
| 77 asyncEnd(); | 86 count++; |
| 87 if (count == successCount) allConnected.complete(); |
| 78 }, onDone: () => asyncEnd()); | 88 }, onDone: () => asyncEnd()); |
| 79 | 89 |
| 80 asyncStart(); | 90 asyncStart(); |
| 81 | 91 |
| 82 // Connect with IPv4 source addesses. | 92 // Connect with IPv4 source addesses. |
| 83 for (var sourceAddress in ipV4SourceAddresses) { | 93 for (var sourceAddress in ipV4SourceAddresses) { |
| 84 if (!v6Only) { | 94 if (!v6Only) { |
| 85 asyncStart(); // Matching asyncEnd in server.listen above. | |
| 86 var s = await connectFunction(InternetAddress.LOOPBACK_IP_V4, | 95 var s = await connectFunction(InternetAddress.LOOPBACK_IP_V4, |
| 87 server.port, | 96 server.port, |
| 88 sourceAddress: sourceAddress); | 97 sourceAddress: sourceAddress); |
| 89 closeDestroyFunction(s); | 98 closeDestroyFunction(s); |
| 90 } else { | 99 } else { |
| 91 // Cannot use an IPv6 source address to connect to IPv6 if | 100 // Cannot use an IPv6 source address to connect to IPv6 if |
| 92 // v6Only is specified. | 101 // v6Only is specified. |
| 93 await throws(() => connectFunction(InternetAddress.LOOPBACK_IP_V4, | 102 await throws(() => connectFunction(InternetAddress.LOOPBACK_IP_V4, |
| 94 server.port, | 103 server.port, |
| 95 sourceAddress: sourceAddress), | 104 sourceAddress: sourceAddress), |
| 96 (e) => e is SocketException); | 105 (e) => e is SocketException); |
| 97 } | 106 } |
| 98 } | 107 } |
| 99 | 108 |
| 100 // Connect with IPv6 source addesses. | 109 // Connect with IPv6 source addesses. |
| 101 for (var sourceAddress in ipV6SourceAddresses) { | 110 for (var sourceAddress in ipV6SourceAddresses) { |
| 102 if (bindAddress.type == InternetAddressType.IP_V6) { | 111 if (bindAddress.type == InternetAddressType.IP_V6) { |
| 103 asyncStart(); // Matching asyncEnd in server.listen above. | |
| 104 var s = await connectFunction(InternetAddress.LOOPBACK_IP_V6, | 112 var s = await connectFunction(InternetAddress.LOOPBACK_IP_V6, |
| 105 server.port, | 113 server.port, |
| 106 sourceAddress: sourceAddress); | 114 sourceAddress: sourceAddress); |
| 107 closeDestroyFunction(s); | 115 closeDestroyFunction(s); |
| 108 } else { | 116 } else { |
| 109 // Cannot use an IPv6 source address to connect to IPv4. | 117 // Cannot use an IPv6 source address to connect to IPv4. |
| 110 await throws(() => connectFunction(InternetAddress.LOOPBACK_IP_V6, | 118 await throws(() => connectFunction(InternetAddress.LOOPBACK_IP_V6, |
| 111 server.port, | 119 server.port, |
| 112 sourceAddress: sourceAddress), | 120 sourceAddress: sourceAddress), |
| 113 (e) => e is SocketException); | 121 (e) => e is SocketException); |
| 114 } | 122 } |
| 115 } | 123 } |
| 116 | 124 |
| 117 server.close(); | 125 await allConnected.future; |
| 126 await server.close(); |
| 118 asyncEnd(); | 127 asyncEnd(); |
| 119 } | 128 } |
| 120 | 129 |
| 121 main() { | 130 main() { |
| 122 testArguments(RawSocket.connect); | 131 testArguments(RawSocket.connect); |
| 123 testArguments(Socket.connect); | 132 testArguments(Socket.connect); |
| 124 testConnect( | 133 testConnect( |
| 125 InternetAddress.ANY_IP_V4, false, RawSocket.connect, (s) => s.close()); | 134 InternetAddress.ANY_IP_V4, false, RawSocket.connect, (s) => s.close()); |
| 126 testConnect( | 135 testConnect( |
| 127 InternetAddress.ANY_IP_V4, false, Socket.connect, (s) => s.destroy()); | 136 InternetAddress.ANY_IP_V4, false, Socket.connect, (s) => s.destroy()); |
| 128 testConnect( | 137 testConnect( |
| 129 InternetAddress.ANY_IP_V6, false, RawSocket.connect, (s) => s.close()); | 138 InternetAddress.ANY_IP_V6, false, RawSocket.connect, (s) => s.close()); |
| 130 testConnect( | 139 testConnect( |
| 131 InternetAddress.ANY_IP_V6, false, Socket.connect, (s) => s.destroy()); | 140 InternetAddress.ANY_IP_V6, false, Socket.connect, (s) => s.destroy()); |
| 132 testConnect( | 141 testConnect( |
| 133 InternetAddress.ANY_IP_V6, true, RawSocket.connect, (s) => s.close()); | 142 InternetAddress.ANY_IP_V6, true, RawSocket.connect, (s) => s.close()); |
| 134 testConnect( | 143 testConnect( |
| 135 InternetAddress.ANY_IP_V6, true, Socket.connect, (s) => s.destroy()); | 144 InternetAddress.ANY_IP_V6, true, Socket.connect, (s) => s.destroy()); |
| 136 } | 145 } |
| OLD | NEW |