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