| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // |
| 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write |
| 8 // VMOptions=--short_socket_read --short_socket_write |
| 9 |
| 10 import "dart:async"; |
| 11 import "dart:io"; |
| 12 import "dart:isolate"; |
| 13 |
| 14 const SERVER_ADDRESS = "127.0.0.1"; |
| 15 const HOST_NAME = "localhost"; |
| 16 const CERTIFICATE = "localhost_cert"; |
| 17 |
| 18 void testCloseOneEnd(String toClose) { |
| 19 ReceivePort port = new ReceivePort(); |
| 20 Completer serverDone = new Completer(); |
| 21 Completer serverEndDone = new Completer(); |
| 22 Completer clientEndDone = new Completer(); |
| 23 Future.wait([serverDone.future, serverEndDone.future, clientEndDone.future]) |
| 24 .then((_) { |
| 25 port.close(); |
| 26 }); |
| 27 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { |
| 28 server.listen((serverConnection) { |
| 29 serverConnection.listen((event) { |
| 30 if (toClose == "server" || event == RawSocketEvent.READ_CLOSED) { |
| 31 serverConnection.shutdown(SocketDirection.SEND); |
| 32 } |
| 33 }, |
| 34 onDone: () { |
| 35 serverEndDone.complete(null); |
| 36 }); |
| 37 }, |
| 38 onDone: () { |
| 39 serverDone.complete(null); |
| 40 }); |
| 41 RawSecureSocket.connect(HOST_NAME, server.port).then((clientConnection) { |
| 42 clientConnection.listen((event){ |
| 43 if (toClose == "client" || event == RawSocketEvent.READ_CLOSED) { |
| 44 clientConnection.shutdown(SocketDirection.SEND); |
| 45 } |
| 46 }, |
| 47 onDone: () { |
| 48 clientEndDone.complete(null); |
| 49 server.close(); |
| 50 }); |
| 51 }); |
| 52 }); |
| 53 } |
| 54 |
| 55 void testCloseBothEnds() { |
| 56 ReceivePort port = new ReceivePort(); |
| 57 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { |
| 58 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); |
| 59 server.listen((serverEnd) { |
| 60 clientEndFuture.then((clientEnd) { |
| 61 clientEnd.close(); |
| 62 serverEnd.close(); |
| 63 server.close(); |
| 64 port.close(); |
| 65 }); |
| 66 }); |
| 67 }); |
| 68 } |
| 69 |
| 70 testPauseServerSocket() { |
| 71 const int socketCount = 10; |
| 72 var acceptCount = 0; |
| 73 var resumed = false; |
| 74 |
| 75 ReceivePort port = new ReceivePort(); |
| 76 |
| 77 RawSecureServerSocket.bind(SERVER_ADDRESS, |
| 78 0, |
| 79 2 * socketCount, |
| 80 CERTIFICATE).then((server) { |
| 81 Expect.isTrue(server.port > 0); |
| 82 var subscription; |
| 83 subscription = server.listen((connection) { |
| 84 Expect.isTrue(resumed); |
| 85 connection.shutdown(SocketDirection.SEND); |
| 86 if (++acceptCount == 2 * socketCount) { |
| 87 server.close(); |
| 88 port.close(); |
| 89 } |
| 90 }); |
| 91 |
| 92 // Pause the server socket subscription and resume it after having |
| 93 // connected a number client sockets. Then connect more client |
| 94 // sockets. |
| 95 subscription.pause(); |
| 96 var connectCount = 0; |
| 97 for (int i = 0; i < socketCount; i++) { |
| 98 RawSecureSocket.connect(HOST_NAME, server.port).then((connection) { |
| 99 connection.shutdown(SocketDirection.SEND); |
| 100 }); |
| 101 } |
| 102 new Timer(500, (_) { |
| 103 subscription.resume(); |
| 104 resumed = true; |
| 105 for (int i = 0; i < socketCount; i++) { |
| 106 RawSecureSocket.connect(HOST_NAME, server.port).then((connection) { |
| 107 connection.shutdown(SocketDirection.SEND); |
| 108 }); |
| 109 } |
| 110 }); |
| 111 }); |
| 112 } |
| 113 |
| 114 testCloseServer() { |
| 115 const int socketCount = 3; |
| 116 ReceivePort port = new ReceivePort(); |
| 117 List ends = []; |
| 118 |
| 119 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 15, CERTIFICATE).then((server) { |
| 120 Expect.isTrue(server.port > 0); |
| 121 void checkDone() { |
| 122 if (ends.length < 2 * socketCount) return; |
| 123 for (var end in ends) { |
| 124 end.close(); |
| 125 } |
| 126 server.close(); |
| 127 port.close(); |
| 128 } |
| 129 |
| 130 server.listen((connection) { |
| 131 ends.add(connection); |
| 132 checkDone(); |
| 133 }); |
| 134 |
| 135 for (int i = 0; i < socketCount; i++) { |
| 136 RawSecureSocket.connect(HOST_NAME, server.port).then((connection) { |
| 137 ends.add(connection); |
| 138 checkDone(); |
| 139 }); |
| 140 } |
| 141 }); |
| 142 } |
| 143 |
| 144 |
| 145 main() { |
| 146 Path scriptDir = new Path(new Options().script).directoryPath; |
| 147 Path certificateDatabase = scriptDir.append('pkcert'); |
| 148 SecureSocket.initialize(database: certificateDatabase.toNativePath(), |
| 149 password: 'dartdart', |
| 150 useBuiltinRoots: false); |
| 151 |
| 152 testCloseOneEnd("client"); |
| 153 testCloseOneEnd("server"); |
| 154 testCloseBothEnds(); |
| 155 testCloseServer(); |
| 156 testPauseServerSocket(); |
| 157 // TODO(whesse): Add testPauseSocket from raw_socket_test.dart. |
| 158 // TODO(whesse): Add testCancelResubscribeSocket from raw_socket_test.dart. |
| 159 } |
| OLD | NEW |