| 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 class SecureTestServer { | |
| 12 void onConnection(Socket connection) { | |
| 13 numConnections++; | |
| 14 var input = connection.inputStream; | |
| 15 String received = ""; | |
| 16 input.onData = () { | |
| 17 received = received.concat(new String.fromCharCodes(input.read())); | |
| 18 }; | |
| 19 input.onClosed = () { | |
| 20 Expect.isTrue(received.contains("Hello from client ")); | |
| 21 String name = received.substring(received.indexOf("client ") + 7); | |
| 22 connection.outputStream.write("Welcome, client $name".charCodes); | |
| 23 connection.outputStream.close(); | |
| 24 }; | |
| 25 } | |
| 26 | |
| 27 void errorHandlerServer(Exception e) { | |
| 28 Expect.fail("Server socket error $e"); | |
| 29 } | |
| 30 | |
| 31 int start() { | |
| 32 server = new SecureServerSocket(SERVER_ADDRESS, 0, 10, "CN=$HOST_NAME"); | |
| 33 Expect.isNotNull(server); | |
| 34 server.onConnection = onConnection; | |
| 35 server.onError = errorHandlerServer; | |
| 36 return server.port; | |
| 37 } | |
| 38 | |
| 39 void stop() { | |
| 40 server.close(); | |
| 41 } | |
| 42 | |
| 43 int numConnections = 0; | |
| 44 SecureServerSocket server; | |
| 45 } | |
| 46 | |
| 47 class SecureTestClient { | |
| 48 SecureTestClient(int this.port, String this.name) { | |
| 49 socket = new SecureSocket(HOST_NAME, port); | |
| 50 numRequests++; | |
| 51 socket.outputStream.write("Hello from client $name".charCodes); | |
| 52 socket.outputStream.close(); | |
| 53 socket.inputStream.onData = () { | |
| 54 reply = reply.concat(new String.fromCharCodes(socket.inputStream.read())); | |
| 55 }; | |
| 56 socket.inputStream.onClosed = this.done; | |
| 57 reply = ""; | |
| 58 } | |
| 59 | |
| 60 void done() { | |
| 61 Expect.equals("Welcome, client $name", reply); | |
| 62 numReplies++; | |
| 63 if (numReplies == CLIENT_NAMES.length) { | |
| 64 Expect.equals(numRequests, numReplies); | |
| 65 EndTest(); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 static int numRequests = 0; | |
| 70 static int numReplies = 0; | |
| 71 | |
| 72 int port; | |
| 73 String name; | |
| 74 SecureSocket socket; | |
| 75 String reply; | |
| 76 } | |
| 77 | |
| 78 Function EndTest; | |
| 79 | |
| 80 const CLIENT_NAMES = const ['able', 'baker', 'camera', 'donut', 'echo']; | |
| 81 | |
| 82 void main() { | |
| 83 ReceivePort keepAlive = new ReceivePort(); | |
| 84 Path scriptDir = new Path(new Options().script).directoryPath; | |
| 85 Path certificateDatabase = scriptDir.append('pkcert'); | |
| 86 SecureSocket.initialize(database: certificateDatabase.toNativePath(), | |
| 87 password: 'dartdart'); | |
| 88 | |
| 89 var server = new SecureTestServer(); | |
| 90 int port = server.start(); | |
| 91 | |
| 92 EndTest = () { | |
| 93 Expect.equals(CLIENT_NAMES.length, server.numConnections); | |
| 94 server.stop(); | |
| 95 keepAlive.close(); | |
| 96 }; | |
| 97 | |
| 98 for (var x in CLIENT_NAMES) { | |
| 99 new SecureTestClient(port, x); | |
| 100 } | |
| 101 } | |
| OLD | NEW |