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"; |
11 | 11 |
| 12 import 'test_utils.dart' show freeIPv4AndIPv6Port, retry; |
| 13 |
12 Future throws(Function f, Function check) async { | 14 Future throws(Function f, Function check) async { |
13 try { | 15 try { |
14 await f(); | 16 await f(); |
15 Expect.fail('Did not throw'); | 17 Expect.fail('Did not throw'); |
16 } catch (e) { | 18 } catch (e) { |
17 if (check != null) { | 19 if (check != null) { |
18 if (!check(e)) { | 20 if (!check(e)) { |
19 Expect.fail('Unexpected: $e'); | 21 Expect.fail('Unexpected: $e'); |
20 } | 22 } |
21 } | 23 } |
22 } | 24 } |
23 } | 25 } |
24 | 26 |
25 Future testArguments(connectFunction) async { | 27 Future testArguments(connectFunction) async { |
| 28 int freePort = await freeIPv4AndIPv6Port(); |
| 29 |
26 var sourceAddress; | 30 var sourceAddress; |
27 asyncStart(); | 31 asyncStart(); |
28 var server = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); | 32 var server = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, |
| 33 freePort); |
29 server.listen((_) { | 34 server.listen((_) { |
30 throw 'Unexpected connection from address $sourceAddress'; | 35 throw 'Unexpected connection from address $sourceAddress'; |
31 }, onDone: () => asyncEnd()); | 36 }, onDone: () => asyncEnd()); |
32 | 37 |
33 asyncStart(); | 38 asyncStart(); |
34 // Illegal type for sourceAddress. | 39 // Illegal type for sourceAddress. |
35 for (sourceAddress in ['www.google.com', 'abc']) { | 40 for (sourceAddress in ['www.google.com', 'abc']) { |
36 await throws(() => connectFunction('127.0.0.1', | 41 await throws(() => connectFunction('127.0.0.1', |
37 server.port, | 42 server.port, |
38 sourceAddress: sourceAddress), | 43 sourceAddress: sourceAddress), |
(...skipping 27 matching lines...) Expand all Loading... |
66 // IPv6 addresses to use as source address when connecting locally. | 71 // IPv6 addresses to use as source address when connecting locally. |
67 var ipV6SourceAddresses = [InternetAddress.LOOPBACK_IP_V6, | 72 var ipV6SourceAddresses = [InternetAddress.LOOPBACK_IP_V6, |
68 InternetAddress.ANY_IP_V6, | 73 InternetAddress.ANY_IP_V6, |
69 '::1', | 74 '::1', |
70 '::']; | 75 '::']; |
71 | 76 |
72 Future testConnect(InternetAddress bindAddress, | 77 Future testConnect(InternetAddress bindAddress, |
73 bool v6Only, | 78 bool v6Only, |
74 Function connectFunction, | 79 Function connectFunction, |
75 Function closeDestroyFunction) async { | 80 Function closeDestroyFunction) async { |
| 81 int freePort = await freeIPv4AndIPv6Port(); |
| 82 |
76 var successCount = 0; | 83 var successCount = 0; |
77 if (!v6Only) successCount += ipV4SourceAddresses.length; | 84 if (!v6Only) successCount += ipV4SourceAddresses.length; |
78 if (bindAddress.type == InternetAddressType.IP_V6) { | 85 if (bindAddress.type == InternetAddressType.IP_V6) { |
79 successCount += ipV6SourceAddresses.length; | 86 successCount += ipV6SourceAddresses.length; |
80 } | 87 } |
81 var count = 0; | 88 var count = 0; |
82 var allConnected = new Completer(); | 89 var allConnected = new Completer(); |
83 if (successCount == 0) allConnected.complete(); | 90 if (successCount == 0) allConnected.complete(); |
84 | 91 |
85 asyncStart(); | 92 asyncStart(); |
86 var server = await ServerSocket.bind(bindAddress, 0, v6Only: v6Only); | 93 var server = await ServerSocket.bind(bindAddress, freePort, v6Only: v6Only); |
87 server.listen((s) { | 94 server.listen((s) { |
88 s.destroy(); | 95 s.destroy(); |
89 count++; | 96 count++; |
90 if (count == successCount) allConnected.complete(); | 97 if (count == successCount) allConnected.complete(); |
91 }, onDone: () => asyncEnd()); | 98 }, onDone: () => asyncEnd()); |
92 | 99 |
93 asyncStart(); | 100 asyncStart(); |
94 | 101 |
95 // Connect with IPv4 source addesses. | 102 // Connect with IPv4 source addesses. |
96 for (var sourceAddress in ipV4SourceAddresses) { | 103 for (var sourceAddress in ipV4SourceAddresses) { |
(...skipping 26 matching lines...) Expand all Loading... |
123 sourceAddress: sourceAddress), | 130 sourceAddress: sourceAddress), |
124 (e) => e is SocketException); | 131 (e) => e is SocketException); |
125 } | 132 } |
126 } | 133 } |
127 | 134 |
128 await allConnected.future; | 135 await allConnected.future; |
129 await server.close(); | 136 await server.close(); |
130 asyncEnd(); | 137 asyncEnd(); |
131 } | 138 } |
132 | 139 |
133 main() { | 140 main() async { |
134 testArguments(RawSocket.connect); | 141 asyncStart(); |
135 testArguments(Socket.connect); | 142 |
136 testConnect( | 143 await retry(() async { |
137 InternetAddress.ANY_IP_V4, false, RawSocket.connect, (s) => s.close()); | 144 await testArguments(RawSocket.connect); |
138 testConnect( | 145 }); |
139 InternetAddress.ANY_IP_V4, false, Socket.connect, (s) => s.destroy()); | 146 await retry(() async { |
140 testConnect( | 147 await testArguments(Socket.connect); |
141 InternetAddress.ANY_IP_V6, false, RawSocket.connect, (s) => s.close()); | 148 }); |
142 testConnect( | 149 |
143 InternetAddress.ANY_IP_V6, false, Socket.connect, (s) => s.destroy()); | 150 await retry(() async { |
144 testConnect( | 151 await testConnect( |
145 InternetAddress.ANY_IP_V6, true, RawSocket.connect, (s) => s.close()); | 152 InternetAddress.ANY_IP_V4, false, RawSocket.connect, (s) => s.close()); |
146 testConnect( | 153 }); |
147 InternetAddress.ANY_IP_V6, true, Socket.connect, (s) => s.destroy()); | 154 await retry(() async { |
| 155 await testConnect( |
| 156 InternetAddress.ANY_IP_V4, false, Socket.connect, (s) => s.destroy()); |
| 157 }); |
| 158 await retry(() async { |
| 159 await testConnect( |
| 160 InternetAddress.ANY_IP_V6, false, RawSocket.connect, (s) => s.close()); |
| 161 }); |
| 162 await retry(() async { |
| 163 await testConnect( |
| 164 InternetAddress.ANY_IP_V6, false, Socket.connect, (s) => s.destroy()); |
| 165 }); |
| 166 await retry(() async { |
| 167 await testConnect( |
| 168 InternetAddress.ANY_IP_V6, true, RawSocket.connect, (s) => s.close()); |
| 169 }); |
| 170 await retry(() async { |
| 171 await testConnect( |
| 172 InternetAddress.ANY_IP_V6, true, Socket.connect, (s) => s.destroy()); |
| 173 }); |
| 174 |
| 175 asyncEnd(); |
148 } | 176 } |
OLD | NEW |