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

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

Issue 14034012: Support upgrading sockets to secure sockets (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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/secure_socket.dart ('K') | « sdk/lib/io/secure_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_upgrade_to_secure_test.dart
diff --git a/tests/standalone/io/socket_upgrade_to_secure_test.dart b/tests/standalone/io/socket_upgrade_to_secure_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..11f2ca4a97888910fd8028c551af7383bed89f3e
--- /dev/null
+++ b/tests/standalone/io/socket_upgrade_to_secure_test.dart
@@ -0,0 +1,210 @@
+// 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.
+//
+// VMOptions=
+// VMOptions=--short_socket_read
+// VMOptions=--short_socket_write
+// VMOptions=--short_socket_read --short_socket_write
+
+import "package:expect/expect.dart";
+import "dart:async";
+import "dart:io";
+import "dart:isolate";
+
+const SERVER_ADDRESS = "127.0.0.1";
+const HOST_NAME = "localhost";
+const CERTIFICATE = "localhost_cert";
+
+// This test creates a server and a client connects. After connecting
+// and an optional initial handshake the connection is secured by
+// upgrading to a secure connection The client then writes and the
+// server echos. When the server has finished its echo it
+// half-closes. When the client gets the close event is closes fully.
+//
+// The test can be run in different configurations based on
+// the boolean arguments:
+//
+// handshakeBeforeSecure
+// When this argument is true some initial clear text handshake is done
+// between client and server before the connection is secured. This argument
+// only makes sense when both listenSecure and connectSecure are false.
+//
+// postponeSecure
+// When this argument is false the securing of the server end will
+// happen as soon as the last byte of the handshake before securing
+// has been written. When this argument is true the securing of the
+// server will not happen until the first TLS handshake data has been
+// received from the client. This argument only takes effect when
+// handshakeBeforeSecure is true.
+void test(bool handshakeBeforeSecure,
+ [bool postponeSecure = false]) {
+ ReceivePort port = new ReceivePort();
+
+ const messageSize = 1000;
+ const handshakeMessageSize = 100;
+
+ List<int> createTestData() {
+ List<int> data = new List<int>(messageSize);
+ for (int i = 0; i < messageSize; i++) {
+ data[i] = i & 0xff;
+ }
+ return data;
+ }
+
+ List<int> createHandshakeTestData() {
+ List<int> data = new List<int>(handshakeMessageSize);
+ for (int i = 0; i < handshakeMessageSize; i++) {
+ data[i] = i & 0xff;
+ }
+ return data;
+ }
+
+ void verifyTestData(List<int> data) {
+ Expect.equals(messageSize, data.length);
+ List<int> expected = createTestData();
+ for (int i = 0; i < messageSize; i++) {
+ Expect.equals(expected[i], data[i]);
+ }
+ }
+
+ void verifyHandshakeTestData(List<int> data) {
+ Expect.equals(handshakeMessageSize, data.length);
+ List<int> expected = createHandshakeTestData();
+ for (int i = 0; i < handshakeMessageSize; i++) {
+ Expect.equals(expected[i], data[i]);
+ }
+ }
+
+ Future runServer(Socket client) {
+ var completer = new Completer();
+ var dataReceived = [];
+ client.listen(
+ (data) {
+ dataReceived.addAll(data);
+ if (dataReceived.length == messageSize) {
+ verifyTestData(dataReceived);
+ client.add(dataReceived);
+ client.close();
+ }
+ },
+ onDone: () => completer.complete(null));
+ return completer.future;
+ }
+
+ Future<RawSocket> runClient(Socket socket) {
+ var completer = new Completer();
+ var dataReceived = [];
+ socket.listen(
+ (data) {
+ dataReceived.addAll(data);
+ },
+ onDone: () {
+ Expect.equals(messageSize, dataReceived.length);
+ verifyTestData(dataReceived);
+ socket.close();
+ completer.complete(null);
+ });
+ socket.add(createTestData());
+ return completer.future;
+ }
+
+ Future runServerHandshake(Socket client) {
+ var completer = new Completer();
+ var dataReceived = [];
+ var subscription;
+ subscription = client.listen(
+ (data) {
+ if (dataReceived.length == handshakeMessageSize) {
+ Expect.isTrue(postponeSecure);
+ subscription.pause();
+ completer.complete(data);
+ }
+ dataReceived.addAll(data);
+ if (dataReceived.length == handshakeMessageSize) {
+ verifyHandshakeTestData(dataReceived);
+ client.add(dataReceived);
+ if (!postponeSecure) {
+ completer.complete(null);
+ }
+ }
+ },
+ onDone: () => completer.complete(null));
+ return completer.future;
+ }
+
+ Future<Socket> runClientHandshake(Socket socket) {
+ var completer = new Completer();
+ var dataReceived = [];
+ socket.listen(
+ (data) {
+ dataReceived.addAll(data);
+ if (dataReceived.length == handshakeMessageSize) {
+ verifyHandshakeTestData(dataReceived);
+ completer.complete(null);
+ }
+ },
+ onDone: () => Expect.fail("Should not be called")
+ );
+ socket.add(createHandshakeTestData());
+ return completer.future;
+ }
+
+ Future<SecureSocket> connectClient(int port) {
+ if (!handshakeBeforeSecure) {
+ return Socket.connect(HOST_NAME, port).then((socket) {
+ return SecureSocket.secure(socket).then((secureSocket) {
+ Expect.throws(() => socket.add([0]));
+ return secureSocket;
+ });
+ });
+ } else {
+ return Socket.connect(HOST_NAME, port).then((socket) {
+ return runClientHandshake(socket).then((_) {
+ return SecureSocket.secure(socket).then((secureSocket) {
+ Expect.throws(() => socket.add([0]));
+ return secureSocket;
+ });
+ });
+ });
+ }
+ }
+
+ serverReady(server) {
+ server.listen((client) {
+ if (!handshakeBeforeSecure) {
+ SecureSocket.secureServer(client, CERTIFICATE).then((secureClient) {
+ Expect.throws(() => client.add([0]));
+ runServer(secureClient).then((_) => server.close());
+ });
+ } else {
+ runServerHandshake(client).then((carryOverData) {
+ SecureSocket.secureServer(
+ client,
+ CERTIFICATE,
+ carryOverData: carryOverData).then((secureClient) {
+ Expect.throws(() => client.add([0]));
+ runServer(secureClient).then((_) => server.close());
+ });
+ });
+ }
+ });
+
+ connectClient(server.port).then(runClient).then((socket) {
+ port.close();
+ });
+ }
+
+ ServerSocket.bind(SERVER_ADDRESS, 0, 5).then(serverReady);
+}
+
+main() {
+ Path scriptDir = new Path(new Options().script).directoryPath;
+ Path certificateDatabase = scriptDir.append('pkcert');
+ SecureSocket.initialize(database: certificateDatabase.toNativePath(),
+ password: 'dartdart',
+ useBuiltinRoots: false);
+ test(false);
+ test(true);
+ test(true, true);
+}
« sdk/lib/io/secure_socket.dart ('K') | « sdk/lib/io/secure_socket.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698