Change RawSocket.secureServer and RawSoccket.secure to fail on paused subscriptions

R=whesse@google.com
BUG=

Review URL: https://codereview.chromium.org//158773002

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@32499 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
sgjesse@google.com 2014-02-10 13:43:28 +00:00
parent 7a7d8ec896
commit e8c1b76526
2 changed files with 78 additions and 7 deletions

View file

@ -263,10 +263,11 @@ abstract class RawSecureSocket implements RawSocket {
* connection is prepared for TLS handshake.
*
* If the [socket] already has a subscription, pass the existing
* subscription in the [subscription] parameter. The secure socket
* will take over the subscription and process any subsequent
* events. In most cases calling `pause` on this subscription before
* starting TLS handshake is the right thing to do.
* subscription in the [subscription] parameter. The [secure]
* operation will take over the subscription by replacing the
* handlers with it own secure processing. The caller must not touch
* this subscription anymore. Passing a paused subscription is an
* error.
*
* If the [host] argument is passed it will be used as the host name
* for the TLS handshake. If [host] is not passed the host name from
@ -310,9 +311,11 @@ abstract class RawSecureSocket implements RawSocket {
* connection is going to start the TLS handshake.
*
* If the [socket] already has a subscription, pass the existing
* subscription in the [subscription] parameter. The secure socket
* will take over the subscription and process any subsequent
* events.
* subscription in the [subscription] parameter. The [secureServer]
* operation will take over the subscription by replacing the
* handlers with it own secure processing. The caller must not touch
* this subscription anymore. Passing a paused subscription is an
* error.
*
* If some of the data of the TLS handshake has already been read
* from the socket this data can be passed in the [bufferedData]
@ -535,6 +538,9 @@ class _RawSecureSocket extends Stream<RawSocketEvent>
onError: _reportError,
onDone: _doneHandler);
} else {
if (_socketSubscription.isPaused) {
throw new StateError("Subscription passed to TLS upgrade is paused");
}
_socketSubscription
..onData(_eventDispatcher)
..onError(_reportError)

View file

@ -472,6 +472,67 @@ void testSimpleReadWrite({bool listenSecure,
}
}
testPausedSecuringSubscription(bool pausedServer, bool pausedClient) {
bool expectFail = pausedServer || pausedClient;
asyncStart();
var clientComplete = new Completer();
RawServerSocket.bind(HOST_NAME, 0).then((server) {
server.listen((client) {
var subscription;
subscription = client.listen((_) {
if (pausedServer) {
subscription.pause();
}
RawSecureSocket.secureServer(
client, CERTIFICATE, subscription: subscription).then((client) {
if (expectFail) {
Expect.fail("secureServer succeeded with paused subscription");
}
}).catchError((e) {
if (!expectFail) {
Expect.fail("secureServer failed with non-paused subscriptions");
}
if (pausedServer) {
Expect.isTrue(e is StateError);
}
}).whenComplete(() {
server.close();
clientComplete.future.then((_) {
client.close();
asyncEnd();
});
});
});
});
RawSocket.connect(HOST_NAME, server.port).then((socket) {
var subscription;
subscription = socket.listen((_) {
if (pausedClient) {
subscription.pause();
}
RawSecureSocket.secure(
socket, subscription: subscription).then((socket) {
if (expectFail) {
Expect.fail("secure succeeded with paused subscription");
}
socket.close();
}).catchError((e) {
if (!expectFail) {
Expect.fail("secure failed with non-paused subscriptions ($e)");
}
if (pausedClient) {
Expect.isTrue(e is StateError);
}
}).whenComplete(() {
clientComplete.complete(null);
});
});
});
});
}
main() {
var certificateDatabase = Platform.script.resolve('pkcert').toFilePath();
SecureSocket.initialize(database: certificateDatabase,
@ -526,4 +587,8 @@ main() {
handshakeBeforeSecure: true,
postponeSecure: true,
dropReads: true);
testPausedSecuringSubscription(false, false);
testPausedSecuringSubscription(true, false);
testPausedSecuringSubscription(false, true);
testPausedSecuringSubscription(true, true);
}