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 testArguments() { | 18 void testArguments() { |
20 Expect.throws(() => | 19 Expect.throws(() => |
21 RawSecureServerSocket.bind(SERVER_ADDRESS, 65536, 5, CERTIFICATE)); | 20 RawSecureServerSocket.bind(HOST_NAME, 65536, 5, CERTIFICATE)); |
22 Expect.throws(() => | 21 Expect.throws(() => |
23 RawSecureServerSocket.bind(SERVER_ADDRESS, -1, CERTIFICATE)); | 22 RawSecureServerSocket.bind(HOST_NAME, -1, CERTIFICATE)); |
24 Expect.throws(() => | 23 Expect.throws(() => |
25 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, -1, CERTIFICATE)); | 24 RawSecureServerSocket.bind(HOST_NAME, 0, -1, CERTIFICATE)); |
26 } | 25 } |
27 | 26 |
28 void testSimpleBind() { | 27 void testSimpleBind() { |
29 ReceivePort port = new ReceivePort(); | 28 ReceivePort port = new ReceivePort(); |
30 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((s) { | 29 RawSecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((s) { |
31 Expect.isTrue(s.port > 0); | 30 Expect.isTrue(s.port > 0); |
32 s.close(); | 31 s.close(); |
33 port.close(); | 32 port.close(); |
34 }); | 33 }); |
35 } | 34 } |
36 | 35 |
37 void testInvalidBind() { | 36 void testInvalidBind() { |
38 int count = 0; | 37 int count = 0; |
39 ReceivePort port = new ReceivePort(); | 38 ReceivePort port = new ReceivePort(); |
40 port.receive((_, __) { count++; if (count == 3) port.close(); }); | 39 port.receive((_, __) { count++; if (count == 3) port.close(); }); |
(...skipping 11 matching lines...) Expand all Loading... |
52 Expect.fail("Failure expected"); | 51 Expect.fail("Failure expected"); |
53 }).catchError((error) { | 52 }).catchError((error) { |
54 Expect.isTrue(error is SocketIOException); | 53 Expect.isTrue(error is SocketIOException); |
55 port.toSendPort().send(1); | 54 port.toSendPort().send(1); |
56 }); | 55 }); |
57 | 56 |
58 // Bind to a port already in use. | 57 // Bind to a port already in use. |
59 // Either an error or a successful bind is allowed. | 58 // Either an error or a successful bind is allowed. |
60 // Windows platforms allow multiple binding to the same socket, with | 59 // Windows platforms allow multiple binding to the same socket, with |
61 // unpredictable results. | 60 // unpredictable results. |
62 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((s) { | 61 RawSecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((s) { |
63 RawSecureServerSocket.bind(SERVER_ADDRESS, | 62 RawSecureServerSocket.bind(HOST_NAME, |
64 s.port, | 63 s.port, |
65 5, | 64 5, |
66 CERTIFICATE).then((t) { | 65 CERTIFICATE).then((t) { |
67 Expect.equals('windows', Platform.operatingSystem); | 66 Expect.equals('windows', Platform.operatingSystem); |
68 Expect.equals(s.port, t.port); | 67 Expect.equals(s.port, t.port); |
69 s.close(); | 68 s.close(); |
70 t.close(); | 69 t.close(); |
71 port.toSendPort().send(1); | 70 port.toSendPort().send(1); |
72 }) | 71 }) |
73 .catchError((error) { | 72 .catchError((error) { |
74 Expect.notEquals('windows', Platform.operatingSystem); | 73 Expect.notEquals('windows', Platform.operatingSystem); |
75 Expect.isTrue(error is SocketIOException); | 74 Expect.isTrue(error is SocketIOException); |
76 s.close(); | 75 s.close(); |
77 port.toSendPort().send(1); | 76 port.toSendPort().send(1); |
78 }); | 77 }); |
79 }); | 78 }); |
80 } | 79 } |
81 | 80 |
82 void testSimpleConnect(String certificate) { | 81 void testSimpleConnect(String certificate) { |
83 ReceivePort port = new ReceivePort(); | 82 ReceivePort port = new ReceivePort(); |
84 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, certificate).then((server) { | 83 RawSecureServerSocket.bind(HOST_NAME, 0, 5, certificate).then((server) { |
85 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); | 84 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); |
86 server.listen((serverEnd) { | 85 server.listen((serverEnd) { |
87 clientEndFuture.then((clientEnd) { | 86 clientEndFuture.then((clientEnd) { |
88 clientEnd.shutdown(SocketDirection.SEND); | 87 clientEnd.shutdown(SocketDirection.SEND); |
89 serverEnd.shutdown(SocketDirection.SEND); | 88 serverEnd.shutdown(SocketDirection.SEND); |
90 server.close(); | 89 server.close(); |
91 port.close(); | 90 port.close(); |
92 }); | 91 }); |
93 }); | 92 }); |
94 }); | 93 }); |
95 } | 94 } |
96 | 95 |
97 void testSimpleConnectFail(String certificate) { | 96 void testSimpleConnectFail(String certificate) { |
98 ReceivePort port = new ReceivePort(); | 97 ReceivePort port = new ReceivePort(); |
99 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, certificate).then((server) { | 98 RawSecureServerSocket.bind(HOST_NAME, 0, 5, certificate).then((server) { |
100 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port) | 99 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port) |
101 .then((clientEnd) { | 100 .then((clientEnd) { |
102 Expect.fail("No client connection expected."); | 101 Expect.fail("No client connection expected."); |
103 }) | 102 }) |
104 .catchError((error) { | 103 .catchError((error) { |
105 Expect.isTrue(error is SocketIOException); | 104 Expect.isTrue(error is SocketIOException); |
106 }); | 105 }); |
107 server.listen((serverEnd) { | 106 server.listen((serverEnd) { |
108 Expect.fail("No server connection expected."); | 107 Expect.fail("No server connection expected."); |
109 }, | 108 }, |
110 onError: (error) { | 109 onError: (error) { |
111 Expect.isTrue(error is SocketIOException); | 110 Expect.isTrue(error is SocketIOException); |
112 clientEndFuture.then((_) => port.close()); | 111 clientEndFuture.then((_) => port.close()); |
113 }); | 112 }); |
114 }); | 113 }); |
115 } | 114 } |
116 | 115 |
117 void testServerListenAfterConnect() { | 116 void testServerListenAfterConnect() { |
118 ReceivePort port = new ReceivePort(); | 117 ReceivePort port = new ReceivePort(); |
119 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { | 118 RawSecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((server) { |
120 Expect.isTrue(server.port > 0); | 119 Expect.isTrue(server.port > 0); |
121 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); | 120 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); |
122 new Timer(const Duration(milliseconds: 500), () { | 121 new Timer(const Duration(milliseconds: 500), () { |
123 server.listen((serverEnd) { | 122 server.listen((serverEnd) { |
124 clientEndFuture.then((clientEnd) { | 123 clientEndFuture.then((clientEnd) { |
125 clientEnd.shutdown(SocketDirection.SEND); | 124 clientEnd.shutdown(SocketDirection.SEND); |
126 serverEnd.shutdown(SocketDirection.SEND); | 125 serverEnd.shutdown(SocketDirection.SEND); |
127 server.close(); | 126 server.close(); |
128 port.close(); | 127 port.close(); |
129 }); | 128 }); |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 }); | 433 }); |
435 | 434 |
436 connectClient(server.port).then(runClient).then((socket) { | 435 connectClient(server.port).then(runClient).then((socket) { |
437 socket.close(); | 436 socket.close(); |
438 port.close(); | 437 port.close(); |
439 }); | 438 }); |
440 } | 439 } |
441 | 440 |
442 if (listenSecure) { | 441 if (listenSecure) { |
443 RawSecureServerSocket.bind( | 442 RawSecureServerSocket.bind( |
444 SERVER_ADDRESS, 0, 5, CERTIFICATE).then(serverReady); | 443 HOST_NAME, 0, 5, CERTIFICATE).then(serverReady); |
445 } else { | 444 } else { |
446 RawServerSocket.bind(SERVER_ADDRESS, 0, 5).then(serverReady); | 445 RawServerSocket.bind(HOST_NAME, 0, 5).then(serverReady); |
447 } | 446 } |
448 } | 447 } |
449 | 448 |
450 main() { | 449 main() { |
451 Path scriptDir = new Path(new Options().script).directoryPath; | 450 Path scriptDir = new Path(new Options().script).directoryPath; |
452 Path certificateDatabase = scriptDir.append('pkcert'); | 451 Path certificateDatabase = scriptDir.append('pkcert'); |
453 SecureSocket.initialize(database: certificateDatabase.toNativePath(), | 452 SecureSocket.initialize(database: certificateDatabase.toNativePath(), |
454 password: 'dartdart', | 453 password: 'dartdart', |
455 useBuiltinRoots: false); | 454 useBuiltinRoots: false); |
456 testArguments(); | 455 testArguments(); |
457 testSimpleBind(); | 456 testSimpleBind(); |
458 testInvalidBind(); | 457 testInvalidBind(); |
459 testSimpleConnect(CERTIFICATE); | 458 testSimpleConnect(CERTIFICATE); |
460 testSimpleConnect("CN=localhost"); | 459 testSimpleConnect("CN=localhost"); |
461 testSimpleConnectFail("not_a_nickname"); | 460 testSimpleConnectFail("not_a_nickname"); |
462 testSimpleConnectFail("CN=notARealDistinguishedName"); | 461 testSimpleConnectFail("CN=notARealDistinguishedName"); |
463 testServerListenAfterConnect(); | 462 testServerListenAfterConnect(); |
464 testSimpleReadWrite(true, true, false); | 463 testSimpleReadWrite(true, true, false); |
465 testSimpleReadWrite(true, false, false); | 464 testSimpleReadWrite(true, false, false); |
466 testSimpleReadWrite(false, true, false); | 465 testSimpleReadWrite(false, true, false); |
467 testSimpleReadWrite(false, false, false); | 466 testSimpleReadWrite(false, false, false); |
468 testSimpleReadWrite(false, false, true, true); | 467 testSimpleReadWrite(false, false, true, true); |
469 testSimpleReadWrite(false, false, true, false); | 468 testSimpleReadWrite(false, false, true, false); |
470 } | 469 } |
OLD | NEW |