| 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 import "dart:isolate"; | 6 import "dart:isolate"; |
| 7 | 7 |
| 8 const SERVER_ADDRESS = "127.0.0.1"; | 8 const SERVER_ADDRESS = "127.0.0.1"; |
| 9 const HOST_NAME = "localhost"; | 9 const HOST_NAME = "localhost"; |
| 10 | 10 |
| 11 void WriteAndClose(Socket socket, String message) { | 11 void WriteAndClose(Socket socket, String message) { |
| 12 var data = message.charCodes; | 12 var data = message.charCodes; |
| 13 int written = 0; | 13 int written = 0; |
| 14 void write() { | 14 void write() { |
| 15 written += socket.writeList(data, written, data.length - written); | 15 written += socket.writeList(data, written, data.length - written); |
| 16 if (written < data.length) { | 16 if (written < data.length) { |
| 17 socket.onWrite = write; | 17 socket.onWrite = write; |
| 18 } else { | 18 } else { |
| 19 socket.close(true); | 19 socket.close(true); |
| 20 } | 20 } |
| 21 } | 21 } |
| 22 write(); | 22 write(); |
| 23 } | 23 } |
| 24 | 24 |
| 25 class SecureTestServer { | 25 class SecureTestServer { |
| 26 void onConnection(Socket connection) { | 26 void onConnection(Socket connection) { |
| 27 connection.onConnect = () { | 27 connection.onConnect = () { |
| 28 numConnections++; | 28 numConnections++; |
| 29 var certificate = connection.peerCertificate; |
| 30 Expect.isTrue(certificate.subject.contains("CN=")); |
| 29 }; | 31 }; |
| 30 String received = ""; | 32 String received = ""; |
| 31 connection.onData = () { | 33 connection.onData = () { |
| 32 received = received.concat(new String.fromCharCodes(connection.read())); | 34 received = received.concat(new String.fromCharCodes(connection.read())); |
| 33 }; | 35 }; |
| 34 connection.onClosed = () { | 36 connection.onClosed = () { |
| 35 Expect.isTrue(received.contains("Hello from client ")); | 37 Expect.isTrue(received.contains("Hello from client ")); |
| 36 String name = received.substring(received.indexOf("client ") + 7); | 38 String name = received.substring(received.indexOf("client ") + 7); |
| 37 WriteAndClose(connection, "Welcome, client $name"); | 39 WriteAndClose(connection, "Welcome, client $name"); |
| 38 }; | 40 }; |
| 39 } | 41 } |
| 40 | 42 |
| 41 void errorHandlerServer(Exception e) { | 43 void errorHandlerServer(Exception e) { |
| 42 Expect.fail("Server socket error $e"); | 44 Expect.fail("Server socket error $e"); |
| 43 } | 45 } |
| 44 | 46 |
| 45 int start() { | 47 int start() { |
| 46 server = new SecureServerSocket(SERVER_ADDRESS, 0, 10, "CN=$HOST_NAME"); | 48 server = new SecureServerSocket(SERVER_ADDRESS, |
| 49 0, |
| 50 10, |
| 51 "CN=$HOST_NAME", |
| 52 requireClientCertificate: true); |
| 47 Expect.isNotNull(server); | 53 Expect.isNotNull(server); |
| 48 server.onConnection = onConnection; | 54 server.onConnection = onConnection; |
| 49 server.onError = errorHandlerServer; | 55 server.onError = errorHandlerServer; |
| 50 return server.port; | 56 return server.port; |
| 51 } | 57 } |
| 52 | 58 |
| 53 void stop() { | 59 void stop() { |
| 54 server.close(); | 60 server.close(); |
| 55 } | 61 } |
| 56 | 62 |
| 57 int numConnections = 0; | 63 int numConnections = 0; |
| 58 SecureServerSocket server; | 64 SecureServerSocket server; |
| 59 } | 65 } |
| 60 | 66 |
| 61 class SecureTestClient { | 67 class SecureTestClient { |
| 62 SecureTestClient(int this.port, String this.name) { | 68 SecureTestClient(int this.port, String this.name) { |
| 63 socket = new SecureSocket(HOST_NAME, port); | 69 socket = new SecureSocket(HOST_NAME, port, sendClientCertificate: true); |
| 64 socket.onConnect = this.onConnect; | 70 socket.onConnect = this.onConnect; |
| 65 socket.onData = () { | 71 socket.onData = () { |
| 66 reply = reply.concat(new String.fromCharCodes(socket.read())); | 72 reply = reply.concat(new String.fromCharCodes(socket.read())); |
| 67 }; | 73 }; |
| 68 socket.onClosed = done; | 74 socket.onClosed = done; |
| 69 reply = ""; | 75 reply = ""; |
| 70 } | 76 } |
| 71 | 77 |
| 72 void onConnect() { | 78 void onConnect() { |
| 73 numRequests++; | 79 numRequests++; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 94 | 100 |
| 95 Function EndTest; | 101 Function EndTest; |
| 96 | 102 |
| 97 const CLIENT_NAMES = const ['able', 'baker', 'camera', 'donut', 'echo']; | 103 const CLIENT_NAMES = const ['able', 'baker', 'camera', 'donut', 'echo']; |
| 98 | 104 |
| 99 void main() { | 105 void main() { |
| 100 ReceivePort keepAlive = new ReceivePort(); | 106 ReceivePort keepAlive = new ReceivePort(); |
| 101 Path scriptDir = new Path.fromNative(new Options().script).directoryPath; | 107 Path scriptDir = new Path.fromNative(new Options().script).directoryPath; |
| 102 Path certificateDatabase = scriptDir.append('pkcert'); | 108 Path certificateDatabase = scriptDir.append('pkcert'); |
| 103 SecureSocket.initialize(database: certificateDatabase.toNativePath(), | 109 SecureSocket.initialize(database: certificateDatabase.toNativePath(), |
| 104 password: 'dartdart'); | 110 password: 'dartdart', |
| 111 useBuiltinRoots: false); |
| 105 | 112 |
| 106 var server = new SecureTestServer(); | 113 var server = new SecureTestServer(); |
| 107 int port = server.start(); | 114 int port = server.start(); |
| 108 | 115 |
| 109 EndTest = () { | 116 EndTest = () { |
| 110 Expect.equals(CLIENT_NAMES.length, server.numConnections); | 117 Expect.equals(CLIENT_NAMES.length, server.numConnections); |
| 111 server.stop(); | 118 server.stop(); |
| 112 keepAlive.close(); | 119 keepAlive.close(); |
| 113 }; | 120 }; |
| 114 | 121 |
| 115 for (var x in CLIENT_NAMES) { | 122 for (var x in CLIENT_NAMES) { |
| 116 new SecureTestClient(port, x); | 123 new SecureTestClient(port, x); |
| 117 } | 124 } |
| 118 } | 125 } |
| OLD | NEW |