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(); | |
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); | |
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); | |
31 Expect.equals(true, server != null); | |
Mads Ager (google)
2012/11/20 14:59:45
Expect.isTrue(server != null);
Bill Hesse
2012/11/20 17:46:55
Replaced by Expect.isNotNull(server).
There are a
| |
32 server.onConnection = onConnection; | |
33 server.onError = errorHandlerServer; | |
34 server.setCertificate("CN=$HOST_NAME"); | |
35 print("port: ${server.port}"); | |
Mads Ager (google)
2012/11/20 14:59:45
remove debug printing.
Bill Hesse
2012/11/20 17:46:55
Done.
| |
36 return server.port; | |
37 } | |
38 | |
39 void stop() { | |
40 server.close(); | |
41 } | |
42 | |
43 int numConnections = 0; | |
44 TlsServerSocket server; | |
45 } | |
46 | |
47 class TlsTestClient { | |
48 TlsTestClient(int this.port, String this.name) { | |
49 socket = new TlsSocket(HOST_NAME, port); | |
50 socket.onConnect = this.onConnect; | |
51 socket.onData = this.onData; | |
52 reply = ""; | |
53 } | |
54 | |
55 void onConnect() { | |
56 numRequests++; | |
57 var request_bytes = | |
58 "Hello from client $name".charCodes; | |
59 socket.writeList(request_bytes, 0, request_bytes.length); | |
60 } | |
61 | |
62 void onData() { | |
63 var data = socket.read(); | |
64 var received = new String.fromCharCodes(data); | |
65 reply = reply.concat(received); | |
66 if (reply.contains("Welcome") && reply.contains(name)) { | |
67 done(); | |
68 } | |
69 } | |
70 | |
71 void done() { | |
72 Expect.equals("Welcome, client $name", reply); | |
73 socket.close(true); | |
74 numReplies++; | |
75 if (numReplies == CLIENT_NAMES.length) { | |
76 Expect.equals(numRequests, numReplies); | |
77 EndTest(); | |
78 } | |
79 } | |
80 | |
81 static int numRequests = 0; | |
82 static int numReplies = 0; | |
83 | |
84 int port; | |
85 String name; | |
86 TlsSocket socket; | |
87 String reply; | |
88 } | |
89 | |
90 Function EndTest; | |
91 | |
92 const CLIENT_NAMES = const ['able', 'baker', 'camera', 'donut', 'echo']; | |
93 | |
94 void main() { | |
95 Path scriptDir = new Path.fromNative(new Options().script).directoryPath; | |
96 Path certificateDatabase = scriptDir.append('pkcert'); | |
97 TlsSocket.setCertificateDatabase(certificateDatabase.toNativePath(), | |
98 'dartdart'); | |
99 | |
100 var server = new TlsTestServer(); | |
101 int port = server.start(); | |
102 | |
103 EndTest = () { | |
104 Expect.equals(CLIENT_NAMES.length, server.numConnections); | |
105 server.stop(); | |
106 print("Test PASSED"); | |
Mads Ager (google)
2012/11/20 14:59:45
remove print.
Bill Hesse
2012/11/20 17:46:55
I thought this was a nice print, but removed.
If w
| |
107 }; | |
108 | |
109 for (var x in CLIENT_NAMES) { | |
110 new TlsTestClient(port, x); | |
111 } | |
112 } | |
OLD | NEW |