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

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

Issue 11889036: Enable WebSocket secure connection using "wss" in W3C interface. Add testing of secure WebSocket co… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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
« no previous file with comments | « sdk/lib/io/websocket_impl.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/web_socket_test.dart
diff --git a/tests/standalone/io/web_socket_test.dart b/tests/standalone/io/web_socket_test.dart
index 87a3223ddd5dd8477fb33949db50d983268a0b9b..950bc6a5985a2b099607fafc59463e8b0bb686fe 100644
--- a/tests/standalone/io/web_socket_test.dart
+++ b/tests/standalone/io/web_socket_test.dart
@@ -3,17 +3,30 @@
// BSD-style license that can be found in the LICENSE file.
//
+import "dart:async";
import "dart:io";
+import "dart:isolate";
import "dart:scalarlist";
+import "dart:uri";
-void testRequestResponseClientCloses(
+const SERVER_ADDRESS = "127.0.0.1";
+const HOST_NAME = "localhost";
+
+// We will run the tests once over HTTP and once over HTTPS
Søren Gjesse 2013/01/15 14:53:33 End comment with .
+bool secure = false;
+
+Future testRequestResponseClientCloses(
int totalConnections, int closeStatus, String closeReason) {
- HttpServer server = new HttpServer();
+ Completer done = new Completer();
+ HttpServer server = secure ? new HttpsServer() : new HttpServer();
HttpClient client = new HttpClient();
- server.listen("127.0.0.1", 0, backlog: totalConnections);
+ server.listen(SERVER_ADDRESS,
+ 0,
+ backlog: totalConnections,
+ certificate_name: "CN=$HOST_NAME");
- // Create a web socket handler and set is as the HTTP server default
+ // Create a web socket handler and set it as the HTTP server default
// handler.
WebSocketHandler wsHandler = new WebSocketHandler();
wsHandler.onOpen = (WebSocketConnection conn) {
@@ -32,7 +45,8 @@ void testRequestResponseClientCloses(
String messageText = "Hello, world!";
for (int i = 0; i < totalConnections; i++) {
int messageCount = 0;
- HttpClientConnection conn = client.get("127.0.0.1", server.port, "/");
+ HttpClientConnection conn = client.getUrl(new Uri.fromString(
+ '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/'));
WebSocketClientConnection wsconn = new WebSocketClientConnection(conn);
wsconn.onOpen = () => wsconn.send(messageText);
wsconn.onMessage = (message) {
@@ -53,18 +67,24 @@ void testRequestResponseClientCloses(
if (closeCount == totalConnections) {
client.shutdown();
server.close();
+ done.complete(null);
}
};
}
+ return done.future;
}
-void testRequestResponseServerCloses(
+Future testRequestResponseServerCloses(
int totalConnections, int closeStatus, String closeReason) {
- HttpServer server = new HttpServer();
+ Completer done = new Completer();
+ HttpServer server = secure ? new HttpsServer() : new HttpServer();
HttpClient client = new HttpClient();
- server.listen("127.0.0.1", 0, backlog: totalConnections);
+ server.listen(SERVER_ADDRESS,
+ 0,
+ backlog: totalConnections,
+ certificate_name: "CN=$HOST_NAME");
// Create a web socket handler and set is as the HTTP server default
// handler.
@@ -91,6 +111,7 @@ void testRequestResponseServerCloses(
if (closeCount == totalConnections) {
client.shutdown();
server.close();
+ done.complete(null);
}
};
conn.send(messageText);
@@ -98,7 +119,8 @@ void testRequestResponseServerCloses(
server.defaultRequestHandler = wsHandler.onRequest;
for (int i = 0; i < totalConnections; i++) {
- HttpClientConnection conn = client.get("127.0.0.1", server.port, "/");
+ HttpClientConnection conn = client.getUrl(new Uri.fromString(
+ '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/'));
WebSocketClientConnection wsconn = new WebSocketClientConnection(conn);
wsconn.onMessage = (message) => wsconn.send(message);
wsconn.onClosed = (status, reason) {
@@ -108,14 +130,21 @@ void testRequestResponseServerCloses(
Expect.equals(closeReason == null ? "" : closeReason, reason);
};
}
+ return done.future;
}
-void testMessageLength(int messageLength) {
- HttpServer server = new HttpServer();
+Future testMessageLength(int messageLength) {
+ Completer done = new Completer();
+ HttpServer server = secure ? new HttpsServer() : new HttpServer();
HttpClient client = new HttpClient();
+ bool serverReceivedMessage = false;
+ bool clientReceivedMessage = false;
- server.listen("127.0.0.1", 0, backlog: 1);
+ server.listen(SERVER_ADDRESS,
+ 0,
+ backlog: 1,
+ certificate_name: "CN=$HOST_NAME");
// Create a web socket handler and set is as the HTTP server default
// handler.
@@ -123,6 +152,7 @@ void testMessageLength(int messageLength) {
WebSocketHandler wsHandler = new WebSocketHandler();
wsHandler.onOpen = (WebSocketConnection conn) {
conn.onMessage = (Object message) {
+ serverReceivedMessage = true;
Expect.listEquals(originalMessage, message);
conn.send(message);
};
@@ -131,26 +161,37 @@ void testMessageLength(int messageLength) {
};
server.defaultRequestHandler = wsHandler.onRequest;
- HttpClientConnection conn = client.get("127.0.0.1", server.port, "/");
+ HttpClientConnection conn = client.getUrl(new Uri.fromString(
+ '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/'));
WebSocketClientConnection wsconn = new WebSocketClientConnection(conn);
wsconn.onMessage = (message) {
+ clientReceivedMessage = true;
Expect.listEquals(originalMessage, message);
wsconn.close();
};
wsconn.onClosed = (status, reason) {
+ Expect.isTrue(serverReceivedMessage);
+ Expect.isTrue(clientReceivedMessage);
+ client.shutdown();
server.close();
+ done.complete(null);
};
wsconn.onOpen = () {
wsconn.send(originalMessage);
};
+ return done.future;
}
-void testNoUpgrade() {
- HttpServer server = new HttpServer();
+Future testNoUpgrade() {
+ Completer done = new Completer();
+ HttpServer server = secure ? new HttpsServer() : new HttpServer();
HttpClient client = new HttpClient();
- server.listen("127.0.0.1", 0, backlog: 5);
+ server.listen(SERVER_ADDRESS,
+ 0,
+ backlog: 5,
+ certificate_name: "CN=$HOST_NAME");
// Create a server which always responds with a redirect.
server.defaultRequestHandler = (request, response) {
@@ -158,22 +199,29 @@ void testNoUpgrade() {
response.outputStream.close();
};
- HttpClientConnection conn = client.get("127.0.0.1", server.port, "/");
+ HttpClientConnection conn = client.getUrl(new Uri.fromString(
+ '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/'));
conn.followRedirects = false;
WebSocketClientConnection wsconn = new WebSocketClientConnection(conn);
wsconn.onNoUpgrade = (response) {
Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode);
client.shutdown();
server.close();
+ done.complete(null);
};
+ return done.future;
}
-void testUsePOST() {
- HttpServer server = new HttpServer();
+Future testUsePOST() {
+ Completer done = new Completer();
+ HttpServer server = secure ? new HttpsServer() : new HttpServer();
HttpClient client = new HttpClient();
- server.listen("127.0.0.1", 0, backlog: 5);
+ server.listen(SERVER_ADDRESS,
+ 0,
+ backlog: 5,
+ certificate_name: "CN=$HOST_NAME");
// Create a web socket handler and set is as the HTTP server default
// handler.
@@ -184,13 +232,16 @@ void testUsePOST() {
};
server.defaultRequestHandler = wsHandler.onRequest;
- HttpClientConnection conn = client.post("127.0.0.1", server.port, "/");
+ HttpClientConnection conn = client.postUrl(new Uri.fromString(
+ '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/'));
WebSocketClientConnection wsconn = new WebSocketClientConnection(conn);
wsconn.onNoUpgrade = (response) {
Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode);
client.shutdown();
server.close();
+ done.complete(null);
};
+ return done.future;
}
@@ -199,12 +250,16 @@ class WebSocketInfo {
}
-void testHashCode(int totalConnections) {
- HttpServer server = new HttpServer();
+Future testHashCode(int totalConnections) {
+ Completer done = new Completer();
+ HttpServer server = secure ? new HttpsServer() : new HttpServer();
HttpClient client = new HttpClient();
Map connections = new Map();
- server.listen("127.0.0.1", 0, backlog: totalConnections);
+ server.listen(SERVER_ADDRESS,
+ 0,
+ backlog: totalConnections,
+ certificate_name: "CN=$HOST_NAME");
void handleMessage(conn, message) {
var info = connections[conn];
@@ -234,6 +289,7 @@ void testHashCode(int totalConnections) {
if (closeCount == totalConnections) {
client.shutdown();
server.close();
+ done.complete();
}
};
conn.send(messageText);
@@ -241,23 +297,31 @@ void testHashCode(int totalConnections) {
server.defaultRequestHandler = wsHandler.onRequest;
for (int i = 0; i < totalConnections; i++) {
- HttpClientConnection conn = client.get("127.0.0.1", server.port, "/");
+ HttpClientConnection conn = client.getUrl(new Uri.fromString(
+ '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/'));
WebSocketClientConnection wsconn = new WebSocketClientConnection(conn);
wsconn.onMessage = (message) => wsconn.send(message);
}
+ return done.future;
}
-void testW3CInterface(
+Future testW3CInterface(
int totalConnections, int closeStatus, String closeReason) {
- HttpServer server = new HttpServer();
+ List<Future> tasks = [];
+ HttpServer server = secure ? new HttpsServer() : new HttpServer();
- server.listen("127.0.0.1", 0, backlog: totalConnections);
+ server.listen(SERVER_ADDRESS,
+ 0,
+ backlog: totalConnections,
+ certificate_name: "CN=$HOST_NAME");
// Create a web socket handler and set is as the HTTP server default
// handler.
int closeCount = 0;
WebSocketHandler wsHandler = new WebSocketHandler();
+ Completer serverDone = new Completer();
+ tasks.add(serverDone.future);
wsHandler.onOpen = (WebSocketConnection conn) {
String messageText = "Hello, world!";
int messageCount = 0;
@@ -276,6 +340,7 @@ void testW3CInterface(
closeCount++;
if (closeCount == totalConnections) {
server.close();
+ serverDone.complete(null);
}
};
conn.send(messageText);
@@ -283,11 +348,14 @@ void testW3CInterface(
server.defaultRequestHandler = wsHandler.onRequest;
void webSocketConnection() {
+ Completer clientDone = new Completer();
+ tasks.add(clientDone.future);
bool onopenCalled = false;
int onmessageCalled = 0;
bool oncloseCalled = false;
- var websocket = new WebSocket("ws://127.0.0.1:${server.port}");
+ var websocket =
+ new WebSocket('${secure ? "wss" : "ws"}://$HOST_NAME:${server.port}');
Expect.equals(WebSocket.CONNECTING, websocket.readyState);
websocket.onopen = () {
Expect.isFalse(onopenCalled);
@@ -312,30 +380,50 @@ void testW3CInterface(
Expect.equals(3002, event.code);
Expect.equals("Got tired", event.reason);
Expect.equals(WebSocket.CLOSED, websocket.readyState);
+ clientDone.complete(null);
};
}
for (int i = 0; i < totalConnections; i++) {
webSocketConnection();
}
+ return Future.wait(tasks);
}
+void InitializeSSL() {
+ var testPkcertDatabase =
+ new Path(new Options().script).directoryPath.append("pkcert/");
+ SecureSocket.initialize(database: testPkcertDatabase.toNativePath(),
+ password: "dartdart");
+}
+
+
+Future runTests() =>
+ testRequestResponseClientCloses(2, null, null).then((_) =>
+ testRequestResponseClientCloses(2, 3001, null)).then((_) =>
+ testRequestResponseClientCloses(2, 3002, "Got tired")).then((_) =>
+ testRequestResponseServerCloses(2, null, null)).then((_) =>
+ testRequestResponseServerCloses(2, 3001, null)).then((_) =>
+ testRequestResponseServerCloses(2, 3002, "Got tired")).then((_) =>
+ testMessageLength(125)).then((_) =>
+ testMessageLength(126)).then((_) =>
+ testMessageLength(127)).then((_) =>
+ testMessageLength(65535)).then((_) =>
+ testMessageLength(65536)).then((_) =>
+ testNoUpgrade()).then((_) =>
+ testUsePOST()).then((_) =>
+ testHashCode(2)).then((_) =>
+ testW3CInterface(2, 3002, "Got tired"));
+
+
main() {
- testRequestResponseClientCloses(2, null, null);
- testRequestResponseClientCloses(2, 3001, null);
- testRequestResponseClientCloses(2, 3002, "Got tired");
- testRequestResponseServerCloses(2, null, null);
- testRequestResponseServerCloses(2, 3001, null);
- testRequestResponseServerCloses(2, 3002, "Got tired");
- testMessageLength(125);
- testMessageLength(126);
- testMessageLength(127);
- testMessageLength(65535);
- testMessageLength(65536);
- testNoUpgrade();
- testUsePOST();
- testHashCode(2);
-
- testW3CInterface(2, 3002, "Got tired");
+ ReceivePort keepAlive = new ReceivePort();
+ runTests().then((_) {
+ InitializeSSL();
+ secure = true;
+ }).then((_) =>
+ runTests()).then((_) {
+ keepAlive.close();
+ });
}
« no previous file with comments | « sdk/lib/io/websocket_impl.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698