| OLD | NEW |
| 1 // Copyright (c) 2012, 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 // This test tests TLS session resume, by making multiple client connections | 5 // This test tests TLS session resume, by making multiple client connections |
| 6 // on the same port to the same server, with a delay of 200 ms between them. | 6 // on the same port to the same server, with a delay of 200 ms between them. |
| 7 // The unmodified secure_server_test creates all sessions simultaneously, | 7 // The unmodified secure_server_test creates all sessions simultaneously, |
| 8 // which means that no handshake completes and caches its keys in the session | 8 // which means that no handshake completes and caches its keys in the session |
| 9 // cache in time for other connections to use it. | 9 // cache in time for other connections to use it. |
| 10 // | 10 // |
| 11 // Session resume is currently disabled - see issue | 11 // Session resume is currently disabled - see issue |
| 12 // https://code.google.com/p/dart/issues/detail?id=7230 | 12 // https://code.google.com/p/dart/issues/detail?id=7230 |
| 13 // |
| 14 // VMOptions= |
| 15 // VMOptions=--short_socket_read |
| 16 // VMOptions=--short_socket_write |
| 17 // VMOptions=--short_socket_read --short_socket_write |
| 13 | 18 |
| 14 import "dart:async"; | 19 import "dart:async"; |
| 15 import "dart:io"; | 20 import "dart:io"; |
| 16 import "dart:isolate"; | 21 import "dart:isolate"; |
| 17 | 22 |
| 18 const SERVER_ADDRESS = "127.0.0.1"; | 23 const SERVER_ADDRESS = "127.0.0.1"; |
| 19 const HOST_NAME = "localhost"; | 24 const HOST_NAME = "localhost"; |
| 20 | 25 const CERTIFICATE = "localhost_cert"; |
| 21 void WriteAndClose(Socket socket, String message) { | 26 Future<SecureServerSocket> startServer() { |
| 22 var data = message.charCodes; | 27 return SecureServerSocket.bind(SERVER_ADDRESS, |
| 23 int written = 0; | 28 0, |
| 24 void write() { | 29 5, |
| 25 written += socket.writeList(data, written, data.length - written); | 30 CERTIFICATE).then((server) { |
| 26 if (written < data.length) { | 31 server.listen((SecureSocket client) { |
| 27 socket.onWrite = write; | 32 client.reduce(<int>[], (message, data) => message..addAll(data)) |
| 28 } else { | 33 .then((message) { |
| 29 socket.close(true); | 34 String received = new String.fromCharCodes(message); |
| 30 } | 35 Expect.isTrue(received.contains("Hello from client ")); |
| 31 } | 36 String name = received.substring(received.indexOf("client ") + 7); |
| 32 write(); | 37 client.add("Welcome, client $name".charCodes); |
| 38 client.close(); |
| 39 }); |
| 40 }); |
| 41 return server; |
| 42 }); |
| 33 } | 43 } |
| 34 | 44 |
| 35 class SecureTestServer { | 45 Future testClient(server, name) { |
| 36 void onConnection(Socket connection) { | 46 return SecureSocket.connect(HOST_NAME, server.port).then((socket) { |
| 37 connection.onConnect = () { | 47 socket.add("Hello from client $name".charCodes); |
| 38 numConnections++; | 48 socket.close(); |
| 39 }; | 49 return socket.reduce(<int>[], (message, data) => message..addAll(data)) |
| 40 String received = ""; | 50 .then((message) { |
| 41 connection.onData = () { | 51 Expect.listEquals("Welcome, client $name".charCodes, message); |
| 42 received = received.concat(new String.fromCharCodes(connection.read())); | 52 return server; |
| 43 }; | 53 }); |
| 44 connection.onClosed = () { | 54 }); |
| 45 Expect.isTrue(received.contains("Hello from client ")); | |
| 46 String name = received.substring(received.indexOf("client ") + 7); | |
| 47 WriteAndClose(connection, "Welcome, client $name"); | |
| 48 }; | |
| 49 } | |
| 50 | |
| 51 void errorHandlerServer(Exception e) { | |
| 52 Expect.fail("Server socket error $e"); | |
| 53 } | |
| 54 | |
| 55 int start() { | |
| 56 server = new SecureServerSocket(SERVER_ADDRESS, 0, 10, "CN=$HOST_NAME"); | |
| 57 Expect.isNotNull(server); | |
| 58 server.onConnection = onConnection; | |
| 59 server.onError = errorHandlerServer; | |
| 60 return server.port; | |
| 61 } | |
| 62 | |
| 63 void stop() { | |
| 64 server.close(); | |
| 65 } | |
| 66 | |
| 67 int numConnections = 0; | |
| 68 SecureServerSocket server; | |
| 69 } | 55 } |
| 70 | 56 |
| 71 class SecureTestClient { | |
| 72 SecureTestClient(int this.port, String this.name) { | |
| 73 socket = new SecureSocket(HOST_NAME, port); | |
| 74 socket.onConnect = this.onConnect; | |
| 75 socket.onData = () { | |
| 76 reply = reply.concat(new String.fromCharCodes(socket.read())); | |
| 77 }; | |
| 78 socket.onClosed = done; | |
| 79 reply = ""; | |
| 80 } | |
| 81 | |
| 82 void onConnect() { | |
| 83 numRequests++; | |
| 84 WriteAndClose(socket, "Hello from client $name"); | |
| 85 } | |
| 86 | |
| 87 void done() { | |
| 88 Expect.equals("Welcome, client $name", reply); | |
| 89 numReplies++; | |
| 90 if (numReplies == CLIENT_NAMES.length) { | |
| 91 Expect.equals(numRequests, numReplies); | |
| 92 EndTest(); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 static int numRequests = 0; | |
| 97 static int numReplies = 0; | |
| 98 | |
| 99 int port; | |
| 100 String name; | |
| 101 SecureSocket socket; | |
| 102 String reply; | |
| 103 } | |
| 104 | |
| 105 Function EndTest; | |
| 106 | |
| 107 const CLIENT_NAMES = const ['able', 'baker']; | |
| 108 | |
| 109 void main() { | 57 void main() { |
| 110 ReceivePort keepAlive = new ReceivePort(); | |
| 111 Path scriptDir = new Path(new Options().script).directoryPath; | 58 Path scriptDir = new Path(new Options().script).directoryPath; |
| 112 Path certificateDatabase = scriptDir.append('pkcert'); | 59 Path certificateDatabase = scriptDir.append('pkcert'); |
| 113 SecureSocket.initialize(database: certificateDatabase.toNativePath(), | 60 SecureSocket.initialize(database: certificateDatabase.toNativePath(), |
| 114 password: 'dartdart', | 61 password: 'dartdart'); |
| 115 useBuiltinRoots: false); | |
| 116 | |
| 117 var server = new SecureTestServer(); | |
| 118 int port = server.start(); | |
| 119 | |
| 120 EndTest = () { | |
| 121 Expect.equals(CLIENT_NAMES.length, server.numConnections); | |
| 122 server.stop(); | |
| 123 keepAlive.close(); | |
| 124 }; | |
| 125 | 62 |
| 126 Duration delay = const Duration(milliseconds: 0); | 63 Duration delay = const Duration(milliseconds: 0); |
| 127 Duration delay_between_connections = const Duration(milliseconds: 300); | 64 Duration delay_between_connections = const Duration(milliseconds: 300); |
| 128 | 65 |
| 129 for (var x in CLIENT_NAMES) { | 66 startServer() |
| 130 new Timer(delay, () { | 67 .then((server) => Future.wait( |
| 131 new SecureTestClient(port, x); | 68 ['able', 'baker', 'charlie', 'dozen', 'elapse'] |
| 132 }); | 69 .map((name) { |
| 133 delay += delay_between_connections; | 70 delay += delay_between_connections; |
| 134 } | 71 return new Future.delayed(delay, () => server) |
| 72 .then((server) => testClient(server, name)); |
| 73 }))) |
| 74 .then((servers) => servers.first.close()); |
| 135 } | 75 } |
| OLD | NEW |