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 TlsTestServer { |
11 void onConnection(Socket connection) { | 11 void onConnection(Socket connection) { |
12 connection.onConnect = () { | 12 numConnections++; |
13 numConnections++; | 13 var input = connection.inputStream; |
14 }; | 14 input.onData = () { |
15 connection.onData = () { | 15 var received = new String.fromCharCodes(input.read()); |
Søren Gjesse
2012/11/20 21:13:07
The read is guaranteed to receive all the expected
Mads Ager (google)
2012/11/21 06:59:53
is *not* guaranteed. :-)
| |
16 var data = connection.read(); | |
17 var received = new String.fromCharCodes(data); | |
18 Expect.isTrue(received.contains("Hello from client ")); | 16 Expect.isTrue(received.contains("Hello from client ")); |
19 string name = received.substring(received.indexOf("client ") + 7); | 17 String name = received.substring(received.indexOf("client ") + 7); |
20 var reply_bytes = "Welcome, client $name".charCodes; | 18 connection.outputStream.write("Welcome, client $name".charCodes); |
21 connection.writeList(reply_bytes, 0, reply_bytes.length); | |
22 }; | 19 }; |
23 } | 20 } |
24 | 21 |
25 void errorHandlerServer(Exception e) { | 22 void errorHandlerServer(Exception e) { |
26 Expect.fail("Server socket error $e"); | 23 Expect.fail("Server socket error $e"); |
27 } | 24 } |
28 | 25 |
29 int start() { | 26 int start() { |
30 server = new TlsServerSocket(SERVER_ADDRESS, 0, 10, "CN=$HOST_NAME"); | 27 server = new TlsServerSocket(SERVER_ADDRESS, 0, 10, "CN=$HOST_NAME"); |
31 Expect.isNotNull(server); | 28 Expect.equals(true, server != null); |
32 server.onConnection = onConnection; | 29 server.onConnection = onConnection; |
33 server.onError = errorHandlerServer; | 30 server.onError = errorHandlerServer; |
34 return server.port; | 31 return server.port; |
35 } | 32 } |
36 | 33 |
37 void stop() { | 34 void stop() { |
38 server.close(); | 35 server.close(); |
39 } | 36 } |
40 | 37 |
41 int numConnections = 0; | 38 int numConnections = 0; |
42 TlsServerSocket server; | 39 TlsServerSocket server; |
43 } | 40 } |
44 | 41 |
45 class TlsTestClient { | 42 class TlsTestClient { |
46 TlsTestClient(int this.port, String this.name) { | 43 TlsTestClient(int this.port, String this.name) { |
47 socket = new TlsSocket(HOST_NAME, port); | 44 socket = new TlsSocket(HOST_NAME, port); |
48 socket.onConnect = this.onConnect; | 45 numRequests++; |
49 socket.onData = this.onData; | 46 socket.outputStream.write("Hello from client $name".charCodes); |
47 socket.inputStream.onData = this.onData; | |
50 reply = ""; | 48 reply = ""; |
51 } | 49 } |
52 | 50 |
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() { | 51 void onData() { |
61 var data = socket.read(); | 52 reply = reply.concat(new String.fromCharCodes(socket.inputStream.read())); |
Søren Gjesse
2012/11/20 21:13:07
Ditto.
| |
62 var received = new String.fromCharCodes(data); | |
63 reply = reply.concat(received); | |
64 if (reply.contains("Welcome") && reply.contains(name)) { | 53 if (reply.contains("Welcome") && reply.contains(name)) { |
65 done(); | 54 done(); |
66 } | 55 } |
67 } | 56 } |
68 | 57 |
69 void done() { | 58 void done() { |
70 Expect.equals("Welcome, client $name", reply); | 59 Expect.equals("Welcome, client $name", reply); |
71 socket.close(true); | 60 socket.outputStream.close(); |
72 numReplies++; | 61 numReplies++; |
73 if (numReplies == CLIENT_NAMES.length) { | 62 if (numReplies == CLIENT_NAMES.length) { |
74 Expect.equals(numRequests, numReplies); | 63 Expect.equals(numRequests, numReplies); |
75 EndTest(); | 64 EndTest(); |
76 } | 65 } |
77 } | 66 } |
78 | 67 |
79 static int numRequests = 0; | 68 static int numRequests = 0; |
80 static int numReplies = 0; | 69 static int numReplies = 0; |
81 | 70 |
(...skipping 18 matching lines...) Expand all Loading... | |
100 | 89 |
101 EndTest = () { | 90 EndTest = () { |
102 Expect.equals(CLIENT_NAMES.length, server.numConnections); | 91 Expect.equals(CLIENT_NAMES.length, server.numConnections); |
103 server.stop(); | 92 server.stop(); |
104 }; | 93 }; |
105 | 94 |
106 for (var x in CLIENT_NAMES) { | 95 for (var x in CLIENT_NAMES) { |
107 new TlsTestClient(port, x); | 96 new TlsTestClient(port, x); |
108 } | 97 } |
109 } | 98 } |
OLD | NEW |