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