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 void WriteAndClose(Socket socket, String message) { | |
11 var data = message.charCodes; | |
12 int written = 0; | |
13 void write() { | |
14 written += socket.writeList(data, written, data.length - written); | |
15 if (written < data.length) { | |
16 socket.onWrite = write; | |
17 } else { | |
18 socket.close(true); | |
19 } | |
20 } | |
21 write(); | |
22 } | |
23 | |
24 class TlsTestServer { | 10 class TlsTestServer { |
25 void onConnection(Socket connection) { | 11 void onConnection(Socket connection) { |
26 connection.onConnect = () { | 12 numConnections++; |
27 numConnections++; | 13 var input = connection.inputStream; |
| 14 String received = ""; |
| 15 input.onData = () { |
| 16 received = received.concat(new String.fromCharCodes(input.read())); |
28 }; | 17 }; |
29 String received = ""; | 18 input.onClosed = () { |
30 connection.onData = () { | |
31 received = received.concat(new String.fromCharCodes(connection.read())); | |
32 }; | |
33 connection.onClosed = () { | |
34 Expect.isTrue(received.contains("Hello from client ")); | 19 Expect.isTrue(received.contains("Hello from client ")); |
35 String name = received.substring(received.indexOf("client ") + 7); | 20 String name = received.substring(received.indexOf("client ") + 7); |
36 WriteAndClose(connection, "Welcome, client $name"); | 21 connection.outputStream.write("Welcome, client $name".charCodes); |
| 22 connection.outputStream.close(); |
37 }; | 23 }; |
38 } | 24 } |
39 | 25 |
40 void errorHandlerServer(Exception e) { | 26 void errorHandlerServer(Exception e) { |
41 Expect.fail("Server socket error $e"); | 27 Expect.fail("Server socket error $e"); |
42 } | 28 } |
43 | 29 |
44 int start() { | 30 int start() { |
45 server = new TlsServerSocket(SERVER_ADDRESS, 0, 10, "CN=$HOST_NAME"); | 31 server = new TlsServerSocket(SERVER_ADDRESS, 0, 10, "CN=$HOST_NAME"); |
46 Expect.isNotNull(server); | 32 Expect.isNotNull(server); |
47 server.onConnection = onConnection; | 33 server.onConnection = onConnection; |
48 server.onError = errorHandlerServer; | 34 server.onError = errorHandlerServer; |
49 return server.port; | 35 return server.port; |
50 } | 36 } |
51 | 37 |
52 void stop() { | 38 void stop() { |
53 server.close(); | 39 server.close(); |
54 } | 40 } |
55 | 41 |
56 int numConnections = 0; | 42 int numConnections = 0; |
57 TlsServerSocket server; | 43 TlsServerSocket server; |
58 } | 44 } |
59 | 45 |
60 class TlsTestClient { | 46 class TlsTestClient { |
61 TlsTestClient(int this.port, String this.name) { | 47 TlsTestClient(int this.port, String this.name) { |
62 socket = new TlsSocket(HOST_NAME, port); | 48 socket = new TlsSocket(HOST_NAME, port); |
63 socket.onConnect = this.onConnect; | 49 numRequests++; |
64 socket.onData = () { | 50 socket.outputStream.write("Hello from client $name".charCodes); |
65 reply = reply.concat(new String.fromCharCodes(socket.read())); | 51 socket.outputStream.close(); |
| 52 socket.inputStream.onData = () { |
| 53 reply = reply.concat(new String.fromCharCodes(socket.inputStream.read())); |
66 }; | 54 }; |
67 socket.onClosed = done; | 55 socket.inputStream.onClosed = this.done; |
68 reply = ""; | 56 reply = ""; |
69 } | 57 } |
70 | 58 |
71 void onConnect() { | |
72 numRequests++; | |
73 WriteAndClose(socket, "Hello from client $name"); | |
74 } | |
75 | |
76 void done() { | 59 void done() { |
77 Expect.equals("Welcome, client $name", reply); | 60 Expect.equals("Welcome, client $name", reply); |
78 numReplies++; | 61 numReplies++; |
79 if (numReplies == CLIENT_NAMES.length) { | 62 if (numReplies == CLIENT_NAMES.length) { |
80 Expect.equals(numRequests, numReplies); | 63 Expect.equals(numRequests, numReplies); |
81 EndTest(); | 64 EndTest(); |
82 } | 65 } |
83 } | 66 } |
84 | 67 |
85 static int numRequests = 0; | 68 static int numRequests = 0; |
(...skipping 20 matching lines...) Expand all Loading... |
106 | 89 |
107 EndTest = () { | 90 EndTest = () { |
108 Expect.equals(CLIENT_NAMES.length, server.numConnections); | 91 Expect.equals(CLIENT_NAMES.length, server.numConnections); |
109 server.stop(); | 92 server.stop(); |
110 }; | 93 }; |
111 | 94 |
112 for (var x in CLIENT_NAMES) { | 95 for (var x in CLIENT_NAMES) { |
113 new TlsTestClient(port, x); | 96 new TlsTestClient(port, x); |
114 } | 97 } |
115 } | 98 } |
OLD | NEW |