Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Unified Diff: tests/standalone/io/socket_source_address_test.dart

Issue 908873002: Add support to specify the source address for socket connect (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added dart2js patch file Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« sdk/lib/io/socket.dart ('K') | « sdk/lib/io/socket.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/standalone/io/socket_source_address_test.dart
diff --git a/tests/standalone/io/socket_source_address_test.dart b/tests/standalone/io/socket_source_address_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..b3c4289b992ec7c01c3dcd28a71f7e2acf1b0fac
--- /dev/null
+++ b/tests/standalone/io/socket_source_address_test.dart
@@ -0,0 +1,136 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+//
+
+import "dart:async";
+import "dart:io";
+
+import "package:async_helper/async_helper.dart";
+import "package:expect/expect.dart";
+
+Future throws(Function f, Function check) async {
+ try {
+ await f();
+ Expect.fail('Did not throw');
+ } catch (e) {
+ if (check != null) {
+ if (!check(e)) {
+ Expect.fail('Unexpected: $e');
+ }
+ }
+ }
+}
+
+Future testArguments(connectFunction) async {
+ asyncStart();
+ var server = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0);
+ server.listen((_) { throw 'Connection unexpected'; },
+ onDone: () => asyncEnd());
+
+ asyncStart();
+ // Illegal type for sourceAddress.
+ for (var sourceAddress in ['www.google.com', 'abc']) {
+ await throws(() => connectFunction('127.0.0.1',
+ server.port,
+ sourceAddress: sourceAddress),
+ (e) => e is ArgumentError);
+ }
+ // Unsupported local address.
+ for (var sourceAddress in ['8.8.8.8', new InternetAddress('8.8.8.8')]) {
+ await throws(() => connectFunction('127.0.0.1',
+ server.port,
+ sourceAddress: sourceAddress),
+ (e) => e is SocketException);
+ }
+ // Address family mismatch.
+ for (var sourceAddress in ['::1', InternetAddress.LOOPBACK_IP_V6]) {
+ await throws(() => connectFunction('127.0.0.1',
+ server.port,
+ sourceAddress: sourceAddress),
+ (e) => e is SocketException);
+ }
+ asyncEnd();
+ server.close();
+}
+
+// IPv4 addresses to use as source address when connecting locally.
+var ipV4SourceAddresses = [InternetAddress.LOOPBACK_IP_V4,
+ InternetAddress.ANY_IP_V4,
+ '127.0.0.1',
+ '0.0.0.0'];
+
+// IPv6 addresses to use as source address when connecting locally.
+var ipV6SourceAddresses = [InternetAddress.LOOPBACK_IP_V6,
+ InternetAddress.ANY_IP_V6,
+ '::1',
+ '::'];
+
+Future testConnect(InternetAddress bindAddress,
+ bool v6Only,
+ Function connectFunction,
+ Function closeDestroyFunction) async {
+ asyncStart();
+ var server = await ServerSocket.bind(bindAddress, 0, v6Only: v6Only);
+ server.listen((s) {
+ s.destroy();
+ asyncEnd();
+ }, onDone: () => asyncEnd());
+
+ asyncStart();
+
+ // Connect with IPv4 source addesses.
+ for (var sourceAddress in ipV4SourceAddresses) {
+ if (!v6Only) {
+ asyncStart(); // Matching asyncEnd in server.listen above.
+ var s = await connectFunction(InternetAddress.LOOPBACK_IP_V4,
+ server.port,
+ sourceAddress: sourceAddress);
+ closeDestroyFunction(s);
+ } else {
+ // Cannot use an IPv6 source address to connect to IPv6 if
+ // v6Only is specified.
+ await throws(() => connectFunction(InternetAddress.LOOPBACK_IP_V4,
+ server.port,
+ sourceAddress: sourceAddress),
+ (e) => e is SocketException);
+ }
+ }
+
+ // Connect with IPv6 source addesses.
+ for (var sourceAddress in ipV6SourceAddresses) {
+ if (bindAddress.type == InternetAddressType.IP_V6) {
+ asyncStart(); // Matching asyncEnd in server.listen above.
+ var s = await connectFunction(InternetAddress.LOOPBACK_IP_V6,
+ server.port,
+ sourceAddress: sourceAddress);
+ closeDestroyFunction(s);
+ } else {
+ // Cannot use an IPv6 source address to connect to IPv4.
+ await throws(() => connectFunction(InternetAddress.LOOPBACK_IP_V6,
+ server.port,
+ sourceAddress: sourceAddress),
+ (e) => e is SocketException);
+ }
+ }
+
+ server.close();
+ asyncEnd();
+}
+
+main() {
+ testArguments(RawSocket.connect);
+ testArguments(Socket.connect);
+ testConnect(
+ InternetAddress.ANY_IP_V4, false, RawSocket.connect, (s) => s.close());
+ testConnect(
+ InternetAddress.ANY_IP_V4, false, Socket.connect, (s) => s.destroy());
+ testConnect(
+ InternetAddress.ANY_IP_V6, false, RawSocket.connect, (s) => s.close());
+ testConnect(
+ InternetAddress.ANY_IP_V6, false, Socket.connect, (s) => s.destroy());
+ testConnect(
+ InternetAddress.ANY_IP_V6, true, RawSocket.connect, (s) => s.close());
+ testConnect(
+ InternetAddress.ANY_IP_V6, true, Socket.connect, (s) => s.destroy());
+}
« sdk/lib/io/socket.dart ('K') | « sdk/lib/io/socket.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698