diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-25 04:34:01 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-25 04:34:01 +0000 |
commit | 85488f2665b99d49b9cfe905275a4ee98f4d1fb5 (patch) | |
tree | a56f89ca1abbd60e9b86ec0149277f4c0d757a62 /remoting | |
parent | 284f24f79cdf350cbbaefdc298aab16bf405d1b1 (diff) | |
download | chromium_src-85488f2665b99d49b9cfe905275a4ee98f4d1fb5.zip chromium_src-85488f2665b99d49b9cfe905275a4ee98f4d1fb5.tar.gz chromium_src-85488f2665b99d49b9cfe905275a4ee98f4d1fb5.tar.bz2 |
Move common authenticators code to a separate file.
Pulled code common between V1AuthenticatorTest and V2AuthenticatorTest
to a separate file. It will be later used for NegotiatingAuthenticator
unittest.
BUG=105214
Review URL: http://codereview.chromium.org/9285022
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@119001 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting')
-rw-r--r-- | remoting/protocol/authenticator_test_base.cc | 125 | ||||
-rw-r--r-- | remoting/protocol/authenticator_test_base.h | 68 | ||||
-rw-r--r-- | remoting/protocol/v1_authenticator_unittest.cc | 119 | ||||
-rw-r--r-- | remoting/protocol/v2_authenticator_unittest.cc | 120 | ||||
-rw-r--r-- | remoting/remoting.gyp | 2 |
5 files changed, 199 insertions, 235 deletions
diff --git a/remoting/protocol/authenticator_test_base.cc b/remoting/protocol/authenticator_test_base.cc new file mode 100644 index 0000000..4e6b894 --- /dev/null +++ b/remoting/protocol/authenticator_test_base.cc @@ -0,0 +1,125 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "remoting/protocol/authenticator_test_base.h" + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/path_service.h" +#include "crypto/rsa_private_key.h" +#include "remoting/protocol/authenticator.h" +#include "remoting/protocol/channel_authenticator.h" +#include "remoting/protocol/fake_session.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/libjingle/source/talk/xmllite/xmlelement.h" + +using testing::_; +using testing::SaveArg; + +namespace remoting { +namespace protocol { + +AuthenticatorTestBase::MockChannelDoneCallback::MockChannelDoneCallback() { +} +AuthenticatorTestBase::MockChannelDoneCallback::~MockChannelDoneCallback() { +} + +AuthenticatorTestBase::AuthenticatorTestBase() { +} +AuthenticatorTestBase::~AuthenticatorTestBase() { +} + +void AuthenticatorTestBase::SetUp() { + FilePath certs_dir; + PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir); + certs_dir = certs_dir.AppendASCII("net"); + certs_dir = certs_dir.AppendASCII("data"); + certs_dir = certs_dir.AppendASCII("ssl"); + certs_dir = certs_dir.AppendASCII("certificates"); + + FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); + ASSERT_TRUE(file_util::ReadFileToString(cert_path, &host_cert_)); + + FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); + std::string key_string; + ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string)); + std::vector<uint8> key_vector( + reinterpret_cast<const uint8*>(key_string.data()), + reinterpret_cast<const uint8*>(key_string.data() + + key_string.length())); + private_key_.reset( + crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); +} + +void AuthenticatorTestBase::RunAuthExchange() { + do { + scoped_ptr<buzz::XmlElement> message; + + // Pass message from client to host. + ASSERT_EQ(Authenticator::MESSAGE_READY, client_->state()); + message = client_->GetNextMessage(); + ASSERT_TRUE(message.get()); + ASSERT_NE(Authenticator::MESSAGE_READY, client_->state()); + + ASSERT_EQ(Authenticator::WAITING_MESSAGE, host_->state()); + host_->ProcessMessage(message.get()); + ASSERT_NE(Authenticator::WAITING_MESSAGE, host_->state()); + + // Are we done yet? + if (host_->state() == Authenticator::ACCEPTED || + host_->state() == Authenticator::REJECTED) { + break; + } + + // Pass message from host to client. + ASSERT_EQ(Authenticator::MESSAGE_READY, host_->state()); + message = host_->GetNextMessage(); + ASSERT_TRUE(message.get()); + ASSERT_NE(Authenticator::MESSAGE_READY, host_->state()); + + ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state()); + client_->ProcessMessage(message.get()); + ASSERT_NE(Authenticator::WAITING_MESSAGE, client_->state()); + } while (client_->state() != Authenticator::ACCEPTED && + client_->state() != Authenticator::REJECTED); +} + +void AuthenticatorTestBase::RunChannelAuth(bool expected_fail) { + client_fake_socket_.reset(new FakeSocket()); + host_fake_socket_.reset(new FakeSocket()); + client_fake_socket_->PairWith(host_fake_socket_.get()); + + client_auth_->SecureAndAuthenticate( + client_fake_socket_.release(), + base::Bind(&MockChannelDoneCallback::OnDone, + base::Unretained(&client_callback_))); + + host_auth_->SecureAndAuthenticate( + host_fake_socket_.release(), + base::Bind(&MockChannelDoneCallback::OnDone, + base::Unretained(&host_callback_))); + + net::StreamSocket* client_socket = NULL; + net::StreamSocket* host_socket = NULL; + + EXPECT_CALL(client_callback_, OnDone(net::OK, _)) + .WillOnce(SaveArg<1>(&client_socket)); + if (expected_fail) { + EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED, NULL)); + } else { + EXPECT_CALL(host_callback_, OnDone(net::OK, _)) + .WillOnce(SaveArg<1>(&host_socket)); + } + + message_loop_.RunAllPending(); + + testing::Mock::VerifyAndClearExpectations(&client_callback_); + testing::Mock::VerifyAndClearExpectations(&host_callback_); + + client_socket_.reset(client_socket); + host_socket_.reset(host_socket); +} + +} // namespace protocol +} // namespace remoting diff --git a/remoting/protocol/authenticator_test_base.h b/remoting/protocol/authenticator_test_base.h new file mode 100644 index 0000000..5463867 --- /dev/null +++ b/remoting/protocol/authenticator_test_base.h @@ -0,0 +1,68 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef REMOTING_PROTOCOL_AUTHENTICATOR_TEST_BASE_H_ +#define REMOTING_PROTOCOL_AUTHENTICATOR_TEST_BASE_H_ + +#include <string> + +#include "base/message_loop.h" +#include "net/base/net_errors.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace crypto { +class RSAPrivateKey; +} // namespace crypto + +namespace net { +class StreamSocket; +} // namespace net + +namespace remoting { +namespace protocol { + +class Authenticator; +class ChannelAuthenticator; +class FakeSocket; + +class AuthenticatorTestBase : public testing::Test { + public: + AuthenticatorTestBase(); + virtual ~AuthenticatorTestBase(); + + protected: + class MockChannelDoneCallback { + public: + MockChannelDoneCallback(); + ~MockChannelDoneCallback(); + MOCK_METHOD2(OnDone, void(net::Error error, net::StreamSocket* socket)); + }; + + virtual void SetUp() OVERRIDE; + void RunAuthExchange(); + void RunChannelAuth(bool expected_fail); + + MessageLoop message_loop_; + + scoped_ptr<crypto::RSAPrivateKey> private_key_; + std::string host_cert_; + scoped_ptr<Authenticator> host_; + scoped_ptr<Authenticator> client_; + scoped_ptr<FakeSocket> client_fake_socket_; + scoped_ptr<FakeSocket> host_fake_socket_; + scoped_ptr<ChannelAuthenticator> client_auth_; + scoped_ptr<ChannelAuthenticator> host_auth_; + MockChannelDoneCallback client_callback_; + MockChannelDoneCallback host_callback_; + scoped_ptr<net::StreamSocket> client_socket_; + scoped_ptr<net::StreamSocket> host_socket_; + + DISALLOW_COPY_AND_ASSIGN(AuthenticatorTestBase); +}; + +} // namespace protocol +} // namespace remoting + +#endif // REMOTING_PROTOCOL_AUTHENTICATOR_TEST_BASE_H_ diff --git a/remoting/protocol/v1_authenticator_unittest.cc b/remoting/protocol/v1_authenticator_unittest.cc index 4869ddd..23cb9ec 100644 --- a/remoting/protocol/v1_authenticator_unittest.cc +++ b/remoting/protocol/v1_authenticator_unittest.cc @@ -5,13 +5,9 @@ #include "remoting/protocol/v1_authenticator.h" #include "base/bind.h" -#include "base/file_path.h" -#include "base/file_util.h" -#include "base/message_loop.h" -#include "base/path_service.h" -#include "crypto/rsa_private_key.h" #include "net/base/net_errors.h" #include "remoting/protocol/authenticator.h" +#include "remoting/protocol/authenticator_test_base.h" #include "remoting/protocol/channel_authenticator.h" #include "remoting/protocol/connection_tester.h" #include "remoting/protocol/fake_session.h" @@ -37,14 +33,9 @@ const char kClientJid[] = "host2@gmail.com/321"; const char kTestSharedSecret[] = "1234-1234-5678"; const char kTestSharedSecretBad[] = "0000-0000-0001"; -class MockChannelDoneCallback { - public: - MOCK_METHOD2(OnDone, void(net::Error error, net::StreamSocket* socket)); -}; - } // namespace -class V1AuthenticatorTest : public testing::Test { +class V1AuthenticatorTest : public AuthenticatorTestBase { public: V1AuthenticatorTest() { } @@ -52,28 +43,6 @@ class V1AuthenticatorTest : public testing::Test { } protected: - virtual void SetUp() OVERRIDE { - FilePath certs_dir; - PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir); - certs_dir = certs_dir.AppendASCII("net"); - certs_dir = certs_dir.AppendASCII("data"); - certs_dir = certs_dir.AppendASCII("ssl"); - certs_dir = certs_dir.AppendASCII("certificates"); - - FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); - ASSERT_TRUE(file_util::ReadFileToString(cert_path, &host_cert_)); - - FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); - std::string key_string; - ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string)); - std::vector<uint8> key_vector( - reinterpret_cast<const uint8*>(key_string.data()), - reinterpret_cast<const uint8*>(key_string.data() + - key_string.length())); - private_key_.reset( - crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); - } - void InitAuthenticators(const std::string& client_secret, const std::string& host_secret) { host_.reset(new V1HostAuthenticator( @@ -81,90 +50,6 @@ class V1AuthenticatorTest : public testing::Test { client_.reset(new V1ClientAuthenticator(kClientJid, client_secret)); } - void RunAuthExchange() { - do { - scoped_ptr<buzz::XmlElement> message; - - // Pass message from client to host. - ASSERT_EQ(Authenticator::MESSAGE_READY, client_->state()); - message = client_->GetNextMessage(); - ASSERT_TRUE(message.get()); - ASSERT_NE(Authenticator::MESSAGE_READY, client_->state()); - - ASSERT_EQ(Authenticator::WAITING_MESSAGE, host_->state()); - host_->ProcessMessage(message.get()); - ASSERT_NE(Authenticator::WAITING_MESSAGE, host_->state()); - - // Are we done yet? - if (host_->state() == Authenticator::ACCEPTED || - host_->state() == Authenticator::REJECTED) { - break; - } - - // Pass message from host to client. - ASSERT_EQ(Authenticator::MESSAGE_READY, host_->state()); - message = host_->GetNextMessage(); - ASSERT_TRUE(message.get()); - ASSERT_NE(Authenticator::MESSAGE_READY, host_->state()); - - ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state()); - client_->ProcessMessage(message.get()); - ASSERT_NE(Authenticator::WAITING_MESSAGE, client_->state()); - } while (host_->state() != Authenticator::ACCEPTED && - host_->state() != Authenticator::REJECTED); - } - - void RunChannelAuth(bool expected_fail) { - client_fake_socket_.reset(new FakeSocket()); - host_fake_socket_.reset(new FakeSocket()); - client_fake_socket_->PairWith(host_fake_socket_.get()); - - client_auth_->SecureAndAuthenticate( - client_fake_socket_.release(), - base::Bind(&MockChannelDoneCallback::OnDone, - base::Unretained(&client_callback_))); - - host_auth_->SecureAndAuthenticate( - host_fake_socket_.release(), - base::Bind(&MockChannelDoneCallback::OnDone, - base::Unretained(&host_callback_))); - - net::StreamSocket* client_socket = NULL; - net::StreamSocket* host_socket = NULL; - - EXPECT_CALL(client_callback_, OnDone(net::OK, _)) - .WillOnce(SaveArg<1>(&client_socket)); - if (expected_fail) { - EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED, NULL)); - } else { - EXPECT_CALL(host_callback_, OnDone(net::OK, _)) - .WillOnce(SaveArg<1>(&host_socket)); - } - - message_loop_.RunAllPending(); - - Mock::VerifyAndClearExpectations(&client_callback_); - Mock::VerifyAndClearExpectations(&host_callback_); - - client_socket_.reset(client_socket); - host_socket_.reset(host_socket); - } - - MessageLoop message_loop_; - - scoped_ptr<crypto::RSAPrivateKey> private_key_; - std::string host_cert_; - scoped_ptr<V1HostAuthenticator> host_; - scoped_ptr<V1ClientAuthenticator> client_; - scoped_ptr<FakeSocket> client_fake_socket_; - scoped_ptr<FakeSocket> host_fake_socket_; - scoped_ptr<ChannelAuthenticator> client_auth_; - scoped_ptr<ChannelAuthenticator> host_auth_; - MockChannelDoneCallback client_callback_; - MockChannelDoneCallback host_callback_; - scoped_ptr<net::StreamSocket> client_socket_; - scoped_ptr<net::StreamSocket> host_socket_; - DISALLOW_COPY_AND_ASSIGN(V1AuthenticatorTest); }; diff --git a/remoting/protocol/v2_authenticator_unittest.cc b/remoting/protocol/v2_authenticator_unittest.cc index 715fa2e..fe2458e 100644 --- a/remoting/protocol/v2_authenticator_unittest.cc +++ b/remoting/protocol/v2_authenticator_unittest.cc @@ -5,16 +5,10 @@ #include "remoting/protocol/v2_authenticator.h" #include "base/bind.h" -#include "base/file_path.h" -#include "base/file_util.h" -#include "base/message_loop.h" -#include "base/path_service.h" -#include "crypto/rsa_private_key.h" #include "net/base/net_errors.h" -#include "remoting/protocol/authenticator.h" +#include "remoting/protocol/authenticator_test_base.h" #include "remoting/protocol/channel_authenticator.h" #include "remoting/protocol/connection_tester.h" -#include "remoting/protocol/fake_session.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" @@ -31,19 +25,12 @@ namespace { const int kMessageSize = 100; const int kMessages = 1; -const char kClientJid[] = "host2@gmail.com/321"; - const char kTestSharedSecret[] = "1234-1234-5678"; const char kTestSharedSecretBad[] = "0000-0000-0001"; -class MockChannelDoneCallback { - public: - MOCK_METHOD2(OnDone, void(net::Error error, net::StreamSocket* socket)); -}; - } // namespace -class V2AuthenticatorTest : public testing::Test { +class V2AuthenticatorTest : public AuthenticatorTestBase { public: V2AuthenticatorTest() { } @@ -51,28 +38,6 @@ class V2AuthenticatorTest : public testing::Test { } protected: - virtual void SetUp() OVERRIDE { - FilePath certs_dir; - PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir); - certs_dir = certs_dir.AppendASCII("net"); - certs_dir = certs_dir.AppendASCII("data"); - certs_dir = certs_dir.AppendASCII("ssl"); - certs_dir = certs_dir.AppendASCII("certificates"); - - FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); - ASSERT_TRUE(file_util::ReadFileToString(cert_path, &host_cert_)); - - FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); - std::string key_string; - ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string)); - std::vector<uint8> key_vector( - reinterpret_cast<const uint8*>(key_string.data()), - reinterpret_cast<const uint8*>(key_string.data() + - key_string.length())); - private_key_.reset( - crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); - } - void InitAuthenticators(const std::string& client_secret, const std::string& host_secret) { host_ = V2Authenticator::CreateForHost( @@ -80,87 +45,6 @@ class V2AuthenticatorTest : public testing::Test { client_ = V2Authenticator::CreateForClient(client_secret); } - void RunAuthExchange() { - do { - scoped_ptr<buzz::XmlElement> message; - - // Pass message from client to host. - ASSERT_EQ(Authenticator::MESSAGE_READY, client_->state()); - message = client_->GetNextMessage(); - ASSERT_TRUE(message.get()); - ASSERT_NE(Authenticator::MESSAGE_READY, client_->state()); - - ASSERT_EQ(Authenticator::WAITING_MESSAGE, host_->state()); - host_->ProcessMessage(message.get()); - ASSERT_NE(Authenticator::WAITING_MESSAGE, host_->state()); - - // Are we done yet? - if (host_->state() == Authenticator::ACCEPTED || - host_->state() == Authenticator::REJECTED) { - break; - } - - // Pass message from host to client. - ASSERT_EQ(Authenticator::MESSAGE_READY, host_->state()); - message = host_->GetNextMessage(); - ASSERT_TRUE(message.get()); - ASSERT_NE(Authenticator::MESSAGE_READY, host_->state()); - - ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state()); - client_->ProcessMessage(message.get()); - ASSERT_NE(Authenticator::WAITING_MESSAGE, client_->state()); - } while (client_->state() != Authenticator::ACCEPTED && - client_->state() != Authenticator::REJECTED); - } - - void RunChannelAuth(bool expected_fail) { - client_fake_socket_.reset(new FakeSocket()); - host_fake_socket_.reset(new FakeSocket()); - client_fake_socket_->PairWith(host_fake_socket_.get()); - - client_auth_->SecureAndAuthenticate( - client_fake_socket_.release(), - base::Bind(&MockChannelDoneCallback::OnDone, - base::Unretained(&client_callback_))); - - host_auth_->SecureAndAuthenticate( - host_fake_socket_.release(), - base::Bind(&MockChannelDoneCallback::OnDone, - base::Unretained(&host_callback_))); - - net::StreamSocket* client_socket = NULL; - net::StreamSocket* host_socket = NULL; - - EXPECT_CALL(client_callback_, OnDone(net::OK, _)) - .WillOnce(SaveArg<1>(&client_socket)); - if (expected_fail) { - EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED, NULL)); - } else { - EXPECT_CALL(host_callback_, OnDone(net::OK, _)) - .WillOnce(SaveArg<1>(&host_socket)); - } - - message_loop_.RunAllPending(); - - client_socket_.reset(client_socket); - host_socket_.reset(host_socket); - } - - MessageLoop message_loop_; - - scoped_ptr<crypto::RSAPrivateKey> private_key_; - std::string host_cert_; - scoped_ptr<Authenticator> host_; - scoped_ptr<Authenticator> client_; - scoped_ptr<FakeSocket> client_fake_socket_; - scoped_ptr<FakeSocket> host_fake_socket_; - scoped_ptr<ChannelAuthenticator> client_auth_; - scoped_ptr<ChannelAuthenticator> host_auth_; - MockChannelDoneCallback client_callback_; - MockChannelDoneCallback host_callback_; - scoped_ptr<net::StreamSocket> client_socket_; - scoped_ptr<net::StreamSocket> host_socket_; - DISALLOW_COPY_AND_ASSIGN(V2AuthenticatorTest); }; diff --git a/remoting/remoting.gyp b/remoting/remoting.gyp index a4dc8c3..3df81a2 100644 --- a/remoting/remoting.gyp +++ b/remoting/remoting.gyp @@ -941,6 +941,8 @@ 'jingle_glue/jingle_thread_unittest.cc', 'jingle_glue/mock_objects.cc', 'jingle_glue/mock_objects.h', + 'protocol/authenticator_test_base.cc', + 'protocol/authenticator_test_base.h', 'protocol/connection_tester.cc', 'protocol/connection_tester.h', 'protocol/connection_to_client_unittest.cc', |