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