Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(318)

Unified Diff: remoting/protocol/authenticator_test_base.cc

Issue 10454066: Move the core state machine of SSLClientSocketNSS into a thread-safe Core (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Actually quit the loop Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: remoting/protocol/authenticator_test_base.cc
diff --git a/remoting/protocol/authenticator_test_base.cc b/remoting/protocol/authenticator_test_base.cc
index edff6d361ad20b9d9cfddb5921a9486d15184ff0..eda9420153914aa4e809246a4ffb8013a2eaabfe 100644
--- a/remoting/protocol/authenticator_test_base.cc
+++ b/remoting/protocol/authenticator_test_base.cc
@@ -7,6 +7,8 @@
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
+#include "base/test/test_timeouts.h"
+#include "base/timer.h"
#include "crypto/rsa_private_key.h"
#include "net/base/cert_test_util.h"
#include "remoting/protocol/authenticator.h"
@@ -21,15 +23,59 @@ using testing::SaveArg;
namespace remoting {
namespace protocol {
-AuthenticatorTestBase::MockChannelDoneCallback::MockChannelDoneCallback() {
-}
-AuthenticatorTestBase::MockChannelDoneCallback::~MockChannelDoneCallback() {
-}
+namespace {
+
+// Helper to ensure that both the client and host callbacks are invoked
+// within a reasonable time or that the message loop is aborted.
+class ChannelDoneHelper {
Sergey Ulanov 2012/06/05 04:29:46 In jingle_session_unittest.cc there is QuitThreadO
+ public:
+ typedef base::Callback<
+ void(net::Error, scoped_ptr<net::StreamSocket>)> ConnectedCallback;
Sergey Ulanov 2012/06/05 04:29:46 Can we just reuse ChannelAuthenticator::DoneCallba
+
+ ChannelDoneHelper() : expected_calls_(1), actual_calls_(0) {}
Sergey Ulanov 2012/06/05 04:29:46 nit: why initialize expected_calls_ to 1?
+
+ // Starts the shutdown timer, expecting |OnConnected()| to be called at
Sergey Ulanov 2012/06/05 04:29:46 nit: OnConnected(), || are redundant here.
+ // least |expected_calls| times.
+ void Start(int expected_calls) {
+ DCHECK_GT(expected_calls, 0);
+ expected_calls_ = expected_calls_;
+ actual_calls_ = 0;
+ timer_.Start(FROM_HERE, TestTimeouts::action_timeout(), this,
+ &ChannelDoneHelper::OnTimerFired);
+ }
-AuthenticatorTestBase::AuthenticatorTestBase() {
-}
-AuthenticatorTestBase::~AuthenticatorTestBase() {
-}
+ // Helper function that passes |error| and |socket| to |callback| whenever
+ // it is called, disarming the shutdown timer if called the required number
+ // of times.
+ void OnConnected(const ConnectedCallback& callback,
+ net::Error error,
+ scoped_ptr<net::StreamSocket> socket) {
+ if (++actual_calls_ == expected_calls_) {
+ MessageLoop::current()->Quit();
+ timer_.Stop();
+ }
+ callback.Run(error, socket.Pass());
+ }
+
+ private:
+ void OnTimerFired() {
+ MessageLoop::current()->QuitNow();
+ }
+
+ int expected_calls_;
+ int actual_calls_;
+ base::OneShotTimer<ChannelDoneHelper> timer_;
+};
+
+} // namespace
+
+AuthenticatorTestBase::MockChannelDoneCallback::MockChannelDoneCallback() {}
+
+AuthenticatorTestBase::MockChannelDoneCallback::~MockChannelDoneCallback() {}
+
+AuthenticatorTestBase::AuthenticatorTestBase() {}
+
+AuthenticatorTestBase::~AuthenticatorTestBase() {}
void AuthenticatorTestBase::SetUp() {
FilePath certs_dir(net::GetTestCertsDirectory());
@@ -86,15 +132,21 @@ void AuthenticatorTestBase::RunChannelAuth(bool expected_fail) {
host_fake_socket_.reset(new FakeSocket());
client_fake_socket_->PairWith(host_fake_socket_.get());
+ ChannelDoneHelper helper;
+
client_auth_->SecureAndAuthenticate(
client_fake_socket_.PassAs<net::StreamSocket>(),
- base::Bind(&AuthenticatorTestBase::OnClientConnected,
- base::Unretained(this)));
+ base::Bind(&ChannelDoneHelper::OnConnected,
+ base::Unretained(&helper),
+ base::Bind(&AuthenticatorTestBase::OnClientConnected,
+ base::Unretained(this))));
host_auth_->SecureAndAuthenticate(
host_fake_socket_.PassAs<net::StreamSocket>(),
- base::Bind(&AuthenticatorTestBase::OnHostConnected,
- base::Unretained(this)));
+ base::Bind(&ChannelDoneHelper::OnConnected,
+ base::Unretained(&helper),
+ base::Bind(&AuthenticatorTestBase::OnHostConnected,
+ base::Unretained(this))));
net::StreamSocket* client_socket = NULL;
net::StreamSocket* host_socket = NULL;
@@ -108,13 +160,20 @@ void AuthenticatorTestBase::RunChannelAuth(bool expected_fail) {
.WillOnce(SaveArg<1>(&host_socket));
}
- message_loop_.RunAllPending();
+ // Expect two calls - once for the host, once for the client.
+ helper.Start(2);
+ message_loop_.Run();
testing::Mock::VerifyAndClearExpectations(&client_callback_);
testing::Mock::VerifyAndClearExpectations(&host_callback_);
client_socket_.reset(client_socket);
host_socket_.reset(host_socket);
+
+ if (!expected_fail) {
+ ASSERT_TRUE(client_socket_.get() != NULL);
+ ASSERT_TRUE(host_socket_.get() != NULL);
+ }
}
void AuthenticatorTestBase::OnHostConnected(

Powered by Google App Engine
This is Rietveld 408576698