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 11 matching lines...) Expand all Loading... |
22 } | 22 } |
23 } | 23 } |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 Future testArguments(connectFunction) async { | 27 Future testArguments(connectFunction) async { |
28 int freePort = await freeIPv4AndIPv6Port(); | 28 int freePort = await freeIPv4AndIPv6Port(); |
29 | 29 |
30 var sourceAddress; | 30 var sourceAddress; |
31 asyncStart(); | 31 asyncStart(); |
32 var server = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, | 32 var server = |
33 freePort); | 33 await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, freePort); |
34 server.listen((_) { | 34 server.listen((_) { |
35 throw 'Unexpected connection from address $sourceAddress'; | 35 throw 'Unexpected connection from address $sourceAddress'; |
36 }, onDone: () => asyncEnd()); | 36 }, onDone: () => asyncEnd()); |
37 | 37 |
38 asyncStart(); | 38 asyncStart(); |
39 // Illegal type for sourceAddress. | 39 // Illegal type for sourceAddress. |
40 for (sourceAddress in ['www.google.com', 'abc']) { | 40 for (sourceAddress in ['www.google.com', 'abc']) { |
41 await throws(() => connectFunction('127.0.0.1', | 41 await throws( |
42 server.port, | 42 () => connectFunction('127.0.0.1', server.port, |
43 sourceAddress: sourceAddress), | 43 sourceAddress: sourceAddress), |
44 (e) => e is ArgumentError); | 44 (e) => e is ArgumentError); |
45 } | 45 } |
46 // Unsupported local address. | 46 // Unsupported local address. |
47 for (sourceAddress in ['8.8.8.8', new InternetAddress('8.8.8.8')]) { | 47 for (sourceAddress in ['8.8.8.8', new InternetAddress('8.8.8.8')]) { |
48 await throws(() => connectFunction('127.0.0.1', | 48 await throws( |
49 server.port, | 49 () => connectFunction('127.0.0.1', server.port, |
50 sourceAddress: sourceAddress), | 50 sourceAddress: sourceAddress), |
51 (e) => e is SocketException && | 51 (e) => |
52 e.address == new InternetAddress('8.8.8.8')); | 52 e is SocketException && |
| 53 e.address == new InternetAddress('8.8.8.8')); |
53 } | 54 } |
54 // Address family mismatch. | 55 // Address family mismatch. |
55 for (sourceAddress in ['::1', InternetAddress.LOOPBACK_IP_V6]) { | 56 for (sourceAddress in ['::1', InternetAddress.LOOPBACK_IP_V6]) { |
56 await throws(() => connectFunction('127.0.0.1', | 57 await throws( |
57 server.port, | 58 () => connectFunction('127.0.0.1', server.port, |
58 sourceAddress: sourceAddress), | 59 sourceAddress: sourceAddress), |
59 (e) => e is SocketException); | 60 (e) => e is SocketException); |
60 } | 61 } |
61 asyncEnd(); | 62 asyncEnd(); |
62 server.close(); | 63 server.close(); |
63 } | 64 } |
64 | 65 |
65 // IPv4 addresses to use as source address when connecting locally. | 66 // IPv4 addresses to use as source address when connecting locally. |
66 var ipV4SourceAddresses = [InternetAddress.LOOPBACK_IP_V4, | 67 var ipV4SourceAddresses = [ |
67 InternetAddress.ANY_IP_V4, | 68 InternetAddress.LOOPBACK_IP_V4, |
68 '127.0.0.1', | 69 InternetAddress.ANY_IP_V4, |
69 '0.0.0.0']; | 70 '127.0.0.1', |
| 71 '0.0.0.0' |
| 72 ]; |
70 | 73 |
71 // IPv6 addresses to use as source address when connecting locally. | 74 // IPv6 addresses to use as source address when connecting locally. |
72 var ipV6SourceAddresses = [InternetAddress.LOOPBACK_IP_V6, | 75 var ipV6SourceAddresses = [ |
73 InternetAddress.ANY_IP_V6, | 76 InternetAddress.LOOPBACK_IP_V6, |
74 '::1', | 77 InternetAddress.ANY_IP_V6, |
75 '::']; | 78 '::1', |
| 79 '::' |
| 80 ]; |
76 | 81 |
77 Future testConnect(InternetAddress bindAddress, | 82 Future testConnect(InternetAddress bindAddress, bool v6Only, |
78 bool v6Only, | 83 Function connectFunction, Function closeDestroyFunction) async { |
79 Function connectFunction, | |
80 Function closeDestroyFunction) async { | |
81 int freePort = await freeIPv4AndIPv6Port(); | 84 int freePort = await freeIPv4AndIPv6Port(); |
82 | 85 |
83 var successCount = 0; | 86 var successCount = 0; |
84 if (!v6Only) successCount += ipV4SourceAddresses.length; | 87 if (!v6Only) successCount += ipV4SourceAddresses.length; |
85 if (bindAddress.type == InternetAddressType.IP_V6) { | 88 if (bindAddress.type == InternetAddressType.IP_V6) { |
86 successCount += ipV6SourceAddresses.length; | 89 successCount += ipV6SourceAddresses.length; |
87 } | 90 } |
88 var count = 0; | 91 var count = 0; |
89 var allConnected = new Completer(); | 92 var allConnected = new Completer(); |
90 if (successCount == 0) allConnected.complete(); | 93 if (successCount == 0) allConnected.complete(); |
91 | 94 |
92 asyncStart(); | 95 asyncStart(); |
93 var server = await ServerSocket.bind(bindAddress, freePort, v6Only: v6Only); | 96 var server = await ServerSocket.bind(bindAddress, freePort, v6Only: v6Only); |
94 server.listen((s) { | 97 server.listen((s) { |
95 s.destroy(); | 98 s.destroy(); |
96 count++; | 99 count++; |
97 if (count == successCount) allConnected.complete(); | 100 if (count == successCount) allConnected.complete(); |
98 }, onDone: () => asyncEnd()); | 101 }, onDone: () => asyncEnd()); |
99 | 102 |
100 asyncStart(); | 103 asyncStart(); |
101 | 104 |
102 // Connect with IPv4 source addesses. | 105 // Connect with IPv4 source addesses. |
103 for (var sourceAddress in ipV4SourceAddresses) { | 106 for (var sourceAddress in ipV4SourceAddresses) { |
104 if (!v6Only) { | 107 if (!v6Only) { |
105 var s = await connectFunction(InternetAddress.LOOPBACK_IP_V4, | 108 var s = await connectFunction(InternetAddress.LOOPBACK_IP_V4, server.port, |
106 server.port, | 109 sourceAddress: sourceAddress); |
107 sourceAddress: sourceAddress); | |
108 closeDestroyFunction(s); | 110 closeDestroyFunction(s); |
109 } else { | 111 } else { |
110 // Cannot use an IPv4 source address to connect to IPv6 if | 112 // Cannot use an IPv4 source address to connect to IPv6 if |
111 // v6Only is specified. | 113 // v6Only is specified. |
112 await throws(() => connectFunction(InternetAddress.LOOPBACK_IP_V6, | 114 await throws( |
113 server.port, | 115 () => connectFunction(InternetAddress.LOOPBACK_IP_V6, server.port, |
114 sourceAddress: sourceAddress), | 116 sourceAddress: sourceAddress), |
115 (e) => e is SocketException); | 117 (e) => e is SocketException); |
116 } | 118 } |
117 } | 119 } |
118 | 120 |
119 // Connect with IPv6 source addesses. | 121 // Connect with IPv6 source addesses. |
120 for (var sourceAddress in ipV6SourceAddresses) { | 122 for (var sourceAddress in ipV6SourceAddresses) { |
121 if (bindAddress.type == InternetAddressType.IP_V6) { | 123 if (bindAddress.type == InternetAddressType.IP_V6) { |
122 var s = await connectFunction(InternetAddress.LOOPBACK_IP_V6, | 124 var s = await connectFunction(InternetAddress.LOOPBACK_IP_V6, server.port, |
123 server.port, | 125 sourceAddress: sourceAddress); |
124 sourceAddress: sourceAddress); | |
125 closeDestroyFunction(s); | 126 closeDestroyFunction(s); |
126 } else { | 127 } else { |
127 // Cannot use an IPv6 source address to connect to IPv4. | 128 // Cannot use an IPv6 source address to connect to IPv4. |
128 await throws(() => connectFunction(InternetAddress.LOOPBACK_IP_V4, | 129 await throws( |
129 server.port, | 130 () => connectFunction(InternetAddress.LOOPBACK_IP_V4, server.port, |
130 sourceAddress: sourceAddress), | 131 sourceAddress: sourceAddress), |
131 (e) => e is SocketException); | 132 (e) => e is SocketException); |
132 } | 133 } |
133 } | 134 } |
134 | 135 |
135 await allConnected.future; | 136 await allConnected.future; |
136 await server.close(); | 137 await server.close(); |
137 asyncEnd(); | 138 asyncEnd(); |
138 } | 139 } |
139 | 140 |
140 main() async { | 141 main() async { |
141 asyncStart(); | 142 asyncStart(); |
(...skipping 25 matching lines...) Expand all Loading... |
167 await testConnect( | 168 await testConnect( |
168 InternetAddress.ANY_IP_V6, true, RawSocket.connect, (s) => s.close()); | 169 InternetAddress.ANY_IP_V6, true, RawSocket.connect, (s) => s.close()); |
169 }); | 170 }); |
170 await retry(() async { | 171 await retry(() async { |
171 await testConnect( | 172 await testConnect( |
172 InternetAddress.ANY_IP_V6, true, Socket.connect, (s) => s.destroy()); | 173 InternetAddress.ANY_IP_V6, true, Socket.connect, (s) => s.destroy()); |
173 }); | 174 }); |
174 | 175 |
175 asyncEnd(); | 176 asyncEnd(); |
176 } | 177 } |
OLD | NEW |