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