| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 import "dart:io"; | 5 import "dart:io"; |
| 6 | 6 |
| 7 const SERVER_ADDRESS = "127.0.0.1"; | 7 const SERVER_ADDRESS = "127.0.0.1"; |
| 8 const HOST_NAME = "localhost"; | 8 const HOST_NAME = "localhost"; |
| 9 | 9 |
| 10 void WriteAndClose(Socket socket, String message) { | 10 void WriteAndClose(Socket socket, String message) { |
| 11 var data = message.charCodes; | 11 var data = message.charCodes; |
| 12 int written = 0; | 12 int written = 0; |
| 13 void write() { | 13 void write() { |
| 14 written += socket.writeList(data, written, data.length - written); | 14 written += socket.writeList(data, written, data.length - written); |
| 15 if (written < data.length) { | 15 if (written < data.length) { |
| 16 socket.onWrite = write; | 16 socket.onWrite = write; |
| 17 } else { | 17 } else { |
| 18 socket.close(true); | 18 socket.close(true); |
| 19 } | 19 } |
| 20 } | 20 } |
| 21 write(); | 21 write(); |
| 22 } | 22 } |
| 23 | 23 |
| 24 class TlsTestServer { | 24 class SecureTestServer { |
| 25 void onConnection(Socket connection) { | 25 void onConnection(Socket connection) { |
| 26 connection.onConnect = () { | 26 connection.onConnect = () { |
| 27 numConnections++; | 27 numConnections++; |
| 28 }; | 28 }; |
| 29 String received = ""; | 29 String received = ""; |
| 30 connection.onData = () { | 30 connection.onData = () { |
| 31 received = received.concat(new String.fromCharCodes(connection.read())); | 31 received = received.concat(new String.fromCharCodes(connection.read())); |
| 32 }; | 32 }; |
| 33 connection.onClosed = () { | 33 connection.onClosed = () { |
| 34 Expect.isTrue(received.contains("Hello from client ")); | 34 Expect.isTrue(received.contains("Hello from client ")); |
| 35 String name = received.substring(received.indexOf("client ") + 7); | 35 String name = received.substring(received.indexOf("client ") + 7); |
| 36 WriteAndClose(connection, "Welcome, client $name"); | 36 WriteAndClose(connection, "Welcome, client $name"); |
| 37 }; | 37 }; |
| 38 } | 38 } |
| 39 | 39 |
| 40 void errorHandlerServer(Exception e) { | 40 void errorHandlerServer(Exception e) { |
| 41 Expect.fail("Server socket error $e"); | 41 Expect.fail("Server socket error $e"); |
| 42 } | 42 } |
| 43 | 43 |
| 44 int start() { | 44 int start() { |
| 45 server = new TlsServerSocket(SERVER_ADDRESS, 0, 10, "CN=$HOST_NAME"); | 45 server = new SecureServerSocket(SERVER_ADDRESS, 0, 10, "CN=$HOST_NAME"); |
| 46 Expect.isNotNull(server); | 46 Expect.isNotNull(server); |
| 47 server.onConnection = onConnection; | 47 server.onConnection = onConnection; |
| 48 server.onError = errorHandlerServer; | 48 server.onError = errorHandlerServer; |
| 49 return server.port; | 49 return server.port; |
| 50 } | 50 } |
| 51 | 51 |
| 52 void stop() { | 52 void stop() { |
| 53 server.close(); | 53 server.close(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 int numConnections = 0; | 56 int numConnections = 0; |
| 57 TlsServerSocket server; | 57 SecureServerSocket server; |
| 58 } | 58 } |
| 59 | 59 |
| 60 class TlsTestClient { | 60 class SecureTestClient { |
| 61 TlsTestClient(int this.port, String this.name) { | 61 SecureTestClient(int this.port, String this.name) { |
| 62 socket = new TlsSocket(HOST_NAME, port); | 62 socket = new SecureSocket(HOST_NAME, port); |
| 63 socket.onConnect = this.onConnect; | 63 socket.onConnect = this.onConnect; |
| 64 socket.onData = () { | 64 socket.onData = () { |
| 65 reply = reply.concat(new String.fromCharCodes(socket.read())); | 65 reply = reply.concat(new String.fromCharCodes(socket.read())); |
| 66 }; | 66 }; |
| 67 socket.onClosed = done; | 67 socket.onClosed = done; |
| 68 reply = ""; | 68 reply = ""; |
| 69 } | 69 } |
| 70 | 70 |
| 71 void onConnect() { | 71 void onConnect() { |
| 72 numRequests++; | 72 numRequests++; |
| 73 WriteAndClose(socket, "Hello from client $name"); | 73 WriteAndClose(socket, "Hello from client $name"); |
| 74 } | 74 } |
| 75 | 75 |
| 76 void done() { | 76 void done() { |
| 77 Expect.equals("Welcome, client $name", reply); | 77 Expect.equals("Welcome, client $name", reply); |
| 78 numReplies++; | 78 numReplies++; |
| 79 if (numReplies == CLIENT_NAMES.length) { | 79 if (numReplies == CLIENT_NAMES.length) { |
| 80 Expect.equals(numRequests, numReplies); | 80 Expect.equals(numRequests, numReplies); |
| 81 EndTest(); | 81 EndTest(); |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 static int numRequests = 0; | 85 static int numRequests = 0; |
| 86 static int numReplies = 0; | 86 static int numReplies = 0; |
| 87 | 87 |
| 88 int port; | 88 int port; |
| 89 String name; | 89 String name; |
| 90 TlsSocket socket; | 90 SecureSocket socket; |
| 91 String reply; | 91 String reply; |
| 92 } | 92 } |
| 93 | 93 |
| 94 Function EndTest; | 94 Function EndTest; |
| 95 | 95 |
| 96 const CLIENT_NAMES = const ['able', 'baker', 'camera', 'donut', 'echo']; | 96 const CLIENT_NAMES = const ['able', 'baker', 'camera', 'donut', 'echo']; |
| 97 | 97 |
| 98 void main() { | 98 void main() { |
| 99 Path scriptDir = new Path.fromNative(new Options().script).directoryPath; | 99 Path scriptDir = new Path.fromNative(new Options().script).directoryPath; |
| 100 Path certificateDatabase = scriptDir.append('pkcert'); | 100 Path certificateDatabase = scriptDir.append('pkcert'); |
| 101 TlsSocket.setCertificateDatabase(certificateDatabase.toNativePath(), | 101 SecureSocket.setCertificateDatabase(certificateDatabase.toNativePath(), |
| 102 'dartdart'); | 102 'dartdart'); |
| 103 | 103 |
| 104 var server = new TlsTestServer(); | 104 var server = new SecureTestServer(); |
| 105 int port = server.start(); | 105 int port = server.start(); |
| 106 | 106 |
| 107 EndTest = () { | 107 EndTest = () { |
| 108 Expect.equals(CLIENT_NAMES.length, server.numConnections); | 108 Expect.equals(CLIENT_NAMES.length, server.numConnections); |
| 109 server.stop(); | 109 server.stop(); |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 for (var x in CLIENT_NAMES) { | 112 for (var x in CLIENT_NAMES) { |
| 113 new TlsTestClient(port, x); | 113 new SecureTestClient(port, x); |
| 114 } | 114 } |
| 115 } | 115 } |
| OLD | NEW |