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