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