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 import "dart:isolate"; | 6 import "dart:isolate"; |
7 | 7 |
8 const SERVER_ADDRESS = "127.0.0.1"; | 8 const SERVER_ADDRESS = "127.0.0.1"; |
9 const HOST_NAME = "localhost"; | 9 const HOST_NAME = "localhost"; |
10 | 10 |
11 class SecureTestServer { | 11 class SecureTestServer { |
12 void onConnection(Socket connection) { | 12 void onConnection(Socket connection) { |
13 numConnections++; | 13 numConnections++; |
14 var input = connection.inputStream; | 14 var input = connection.inputStream; |
15 String received = ""; | 15 String received = ""; |
16 input.onData = () { | 16 input.onData = () { |
17 received = received.concat(new String.fromCharCodes(input.read())); | 17 received = received.concat(new String.fromCharCodes(input.read())); |
18 }; | 18 }; |
19 input.onClosed = () { | 19 input.onClosed = () { |
20 Expect.isTrue(received.contains("Hello from client ")); | 20 Expect.isTrue(received.contains("Hello from client ")); |
21 String name = received.substring(received.indexOf("client ") + 7); | 21 String name = received.substring(received.indexOf("client ") + 7); |
22 connection.outputStream.write("Welcome, client $name".charCodes); | 22 connection.outputStream.write("Welcome, client $name".codeUnits); |
23 connection.outputStream.close(); | 23 connection.outputStream.close(); |
24 }; | 24 }; |
25 } | 25 } |
26 | 26 |
27 void errorHandlerServer(Exception e) { | 27 void errorHandlerServer(Exception e) { |
28 Expect.fail("Server socket error $e"); | 28 Expect.fail("Server socket error $e"); |
29 } | 29 } |
30 | 30 |
31 int start() { | 31 int start() { |
32 server = new SecureServerSocket(SERVER_ADDRESS, 0, 10, "CN=$HOST_NAME"); | 32 server = new SecureServerSocket(SERVER_ADDRESS, 0, 10, "CN=$HOST_NAME"); |
33 Expect.isNotNull(server); | 33 Expect.isNotNull(server); |
34 server.onConnection = onConnection; | 34 server.onConnection = onConnection; |
35 server.onError = errorHandlerServer; | 35 server.onError = errorHandlerServer; |
36 return server.port; | 36 return server.port; |
37 } | 37 } |
38 | 38 |
39 void stop() { | 39 void stop() { |
40 server.close(); | 40 server.close(); |
41 } | 41 } |
42 | 42 |
43 int numConnections = 0; | 43 int numConnections = 0; |
44 SecureServerSocket server; | 44 SecureServerSocket server; |
45 } | 45 } |
46 | 46 |
47 class SecureTestClient { | 47 class SecureTestClient { |
48 SecureTestClient(int this.port, String this.name) { | 48 SecureTestClient(int this.port, String this.name) { |
49 socket = new SecureSocket(HOST_NAME, port); | 49 socket = new SecureSocket(HOST_NAME, port); |
50 numRequests++; | 50 numRequests++; |
51 socket.outputStream.write("Hello from client $name".charCodes); | 51 socket.outputStream.write("Hello from client $name".codeUnits); |
52 socket.outputStream.close(); | 52 socket.outputStream.close(); |
53 socket.inputStream.onData = () { | 53 socket.inputStream.onData = () { |
54 reply = reply.concat(new String.fromCharCodes(socket.inputStream.read())); | 54 reply = reply.concat(new String.fromCharCodes(socket.inputStream.read())); |
55 }; | 55 }; |
56 socket.inputStream.onClosed = this.done; | 56 socket.inputStream.onClosed = this.done; |
57 reply = ""; | 57 reply = ""; |
58 } | 58 } |
59 | 59 |
60 void done() { | 60 void done() { |
61 Expect.equals("Welcome, client $name", reply); | 61 Expect.equals("Welcome, client $name", reply); |
(...skipping 30 matching lines...) Expand all Loading... |
92 EndTest = () { | 92 EndTest = () { |
93 Expect.equals(CLIENT_NAMES.length, server.numConnections); | 93 Expect.equals(CLIENT_NAMES.length, server.numConnections); |
94 server.stop(); | 94 server.stop(); |
95 keepAlive.close(); | 95 keepAlive.close(); |
96 }; | 96 }; |
97 | 97 |
98 for (var x in CLIENT_NAMES) { | 98 for (var x in CLIENT_NAMES) { |
99 new SecureTestClient(port, x); | 99 new SecureTestClient(port, x); |
100 } | 100 } |
101 } | 101 } |
OLD | NEW |