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