OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 | 9 |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 import "dart:async"; | 11 import "dart:async"; |
12 import "dart:io"; | 12 import "dart:io"; |
13 import "dart:isolate"; | 13 import "dart:isolate"; |
14 | 14 |
15 const SERVER_ADDRESS = "127.0.0.1"; | |
16 const HOST_NAME = "localhost"; | 15 const HOST_NAME = "localhost"; |
17 const CERTIFICATE = "localhost_cert"; | 16 const CERTIFICATE = "localhost_cert"; |
18 | 17 |
19 void testSimpleBind() { | 18 void testSimpleBind() { |
20 ReceivePort port = new ReceivePort(); | 19 ReceivePort port = new ReceivePort(); |
21 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((s) { | 20 SecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((s) { |
22 Expect.isTrue(s.port > 0); | 21 Expect.isTrue(s.port > 0); |
23 s.close(); | 22 s.close(); |
24 port.close(); | 23 port.close(); |
25 }); | 24 }); |
26 } | 25 } |
27 | 26 |
28 void testInvalidBind() { | 27 void testInvalidBind() { |
29 int count = 0; | 28 int count = 0; |
30 ReceivePort port = new ReceivePort(); | 29 ReceivePort port = new ReceivePort(); |
31 port.receive((_, __) { count++; if (count == 3) port.close(); }); | 30 port.receive((_, __) { count++; if (count == 3) port.close(); }); |
(...skipping 11 matching lines...) Expand all Loading... |
43 Expect.fail("Failure expected"); | 42 Expect.fail("Failure expected"); |
44 }).catchError((error) { | 43 }).catchError((error) { |
45 Expect.isTrue(error is SocketIOException); | 44 Expect.isTrue(error is SocketIOException); |
46 port.toSendPort().send(1); | 45 port.toSendPort().send(1); |
47 }); | 46 }); |
48 | 47 |
49 // Bind to a port already in use. | 48 // Bind to a port already in use. |
50 // Either an error or a successful bind is allowed. | 49 // Either an error or a successful bind is allowed. |
51 // Windows platforms allow multiple binding to the same socket, with | 50 // Windows platforms allow multiple binding to the same socket, with |
52 // unpredictable results. | 51 // unpredictable results. |
53 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((s) { | 52 SecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((s) { |
54 SecureServerSocket.bind(SERVER_ADDRESS, | 53 SecureServerSocket.bind(HOST_NAME, |
55 s.port, | 54 s.port, |
56 5, | 55 5, |
57 CERTIFICATE).then((t) { | 56 CERTIFICATE).then((t) { |
58 Expect.equals('windows', Platform.operatingSystem); | 57 Expect.equals('windows', Platform.operatingSystem); |
59 Expect.equals(s.port, t.port); | 58 Expect.equals(s.port, t.port); |
60 s.close(); | 59 s.close(); |
61 t.close(); | 60 t.close(); |
62 port.toSendPort().send(1); | 61 port.toSendPort().send(1); |
63 }).catchError((error) { | 62 }).catchError((error) { |
64 Expect.notEquals('windows', Platform.operatingSystem); | 63 Expect.notEquals('windows', Platform.operatingSystem); |
65 Expect.isTrue(error is SocketIOException); | 64 Expect.isTrue(error is SocketIOException); |
66 s.close(); | 65 s.close(); |
67 port.toSendPort().send(1); | 66 port.toSendPort().send(1); |
68 }); | 67 }); |
69 }); | 68 }); |
70 } | 69 } |
71 | 70 |
72 void testSimpleConnect(String certificate) { | 71 void testSimpleConnect(String certificate) { |
73 ReceivePort port = new ReceivePort(); | 72 ReceivePort port = new ReceivePort(); |
74 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, certificate).then((server) { | 73 SecureServerSocket.bind(HOST_NAME, 0, 5, certificate).then((server) { |
75 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); | 74 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); |
76 server.listen((serverEnd) { | 75 server.listen((serverEnd) { |
77 clientEndFuture.then((clientEnd) { | 76 clientEndFuture.then((clientEnd) { |
78 clientEnd.close(); | 77 clientEnd.close(); |
79 serverEnd.close(); | 78 serverEnd.close(); |
80 server.close(); | 79 server.close(); |
81 port.close(); | 80 port.close(); |
82 }); | 81 }); |
83 }); | 82 }); |
84 }); | 83 }); |
85 } | 84 } |
86 | 85 |
87 void testSimpleConnectFail(String certificate) { | 86 void testSimpleConnectFail(String certificate) { |
88 ReceivePort port = new ReceivePort(); | 87 ReceivePort port = new ReceivePort(); |
89 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, certificate).then((server) { | 88 SecureServerSocket.bind(HOST_NAME, 0, 5, certificate).then((server) { |
90 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port) | 89 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port) |
91 .then((clientEnd) { | 90 .then((clientEnd) { |
92 Expect.fail("No client connection expected."); | 91 Expect.fail("No client connection expected."); |
93 }) | 92 }) |
94 .catchError((error) { | 93 .catchError((error) { |
95 Expect.isTrue(error is SocketIOException); | 94 Expect.isTrue(error is SocketIOException); |
96 }); | 95 }); |
97 server.listen((serverEnd) { | 96 server.listen((serverEnd) { |
98 Expect.fail("No server connection expected."); | 97 Expect.fail("No server connection expected."); |
99 }, | 98 }, |
100 onError: (error) { | 99 onError: (error) { |
101 Expect.isTrue(error is SocketIOException); | 100 Expect.isTrue(error is SocketIOException); |
102 clientEndFuture.then((_) => port.close()); | 101 clientEndFuture.then((_) => port.close()); |
103 }); | 102 }); |
104 }); | 103 }); |
105 } | 104 } |
106 | 105 |
107 void testServerListenAfterConnect() { | 106 void testServerListenAfterConnect() { |
108 ReceivePort port = new ReceivePort(); | 107 ReceivePort port = new ReceivePort(); |
109 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { | 108 SecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((server) { |
110 Expect.isTrue(server.port > 0); | 109 Expect.isTrue(server.port > 0); |
111 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); | 110 var clientEndFuture = SecureSocket.connect(HOST_NAME, server.port); |
112 new Timer(const Duration(milliseconds: 500), () { | 111 new Timer(const Duration(milliseconds: 500), () { |
113 server.listen((serverEnd) { | 112 server.listen((serverEnd) { |
114 clientEndFuture.then((clientEnd) { | 113 clientEndFuture.then((clientEnd) { |
115 clientEnd.close(); | 114 clientEnd.close(); |
116 serverEnd.close(); | 115 serverEnd.close(); |
117 server.close(); | 116 server.close(); |
118 port.close(); | 117 port.close(); |
119 }); | 118 }); |
(...skipping 20 matching lines...) Expand all Loading... |
140 } | 139 } |
141 | 140 |
142 void verifyTestData(List<int> data) { | 141 void verifyTestData(List<int> data) { |
143 Expect.equals(messageSize, data.length); | 142 Expect.equals(messageSize, data.length); |
144 List<int> expected = createTestData(); | 143 List<int> expected = createTestData(); |
145 for (int i = 0; i < messageSize; i++) { | 144 for (int i = 0; i < messageSize; i++) { |
146 Expect.equals(expected[i], data[i]); | 145 Expect.equals(expected[i], data[i]); |
147 } | 146 } |
148 } | 147 } |
149 | 148 |
150 SecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { | 149 SecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((server) { |
151 server.listen((client) { | 150 server.listen((client) { |
152 int bytesRead = 0; | 151 int bytesRead = 0; |
153 int bytesWritten = 0; | 152 int bytesWritten = 0; |
154 List<int> data = new List<int>(messageSize); | 153 List<int> data = new List<int>(messageSize); |
155 | 154 |
156 client.listen( | 155 client.listen( |
157 (buffer) { | 156 (buffer) { |
158 Expect.isTrue(bytesWritten == 0); | 157 Expect.isTrue(bytesWritten == 0); |
159 data.setRange(bytesRead, bytesRead + buffer.length, buffer); | 158 data.setRange(bytesRead, bytesRead + buffer.length, buffer); |
160 bytesRead += buffer.length; | 159 bytesRead += buffer.length; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 useBuiltinRoots: false); | 197 useBuiltinRoots: false); |
199 testSimpleBind(); | 198 testSimpleBind(); |
200 testInvalidBind(); | 199 testInvalidBind(); |
201 testSimpleConnect(CERTIFICATE); | 200 testSimpleConnect(CERTIFICATE); |
202 testSimpleConnect("CN=localhost"); | 201 testSimpleConnect("CN=localhost"); |
203 testSimpleConnectFail("not_a_nickname"); | 202 testSimpleConnectFail("not_a_nickname"); |
204 testSimpleConnectFail("CN=notARealDistinguishedName"); | 203 testSimpleConnectFail("CN=notARealDistinguishedName"); |
205 testServerListenAfterConnect(); | 204 testServerListenAfterConnect(); |
206 testSimpleReadWrite(); | 205 testSimpleReadWrite(); |
207 } | 206 } |
OLD | NEW |