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