| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/protocol/ssl_hmac_channel_authenticator.h" | 5 #include "remoting/protocol/ssl_hmac_channel_authenticator.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/test/test_timeouts.h" |
| 12 #include "base/timer.h" |
| 11 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 12 #include "crypto/rsa_private_key.h" | 14 #include "crypto/rsa_private_key.h" |
| 13 #include "net/base/cert_test_util.h" | 15 #include "net/base/cert_test_util.h" |
| 14 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 15 #include "remoting/protocol/connection_tester.h" | 17 #include "remoting/protocol/connection_tester.h" |
| 16 #include "remoting/protocol/fake_session.h" | 18 #include "remoting/protocol/fake_session.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 21 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 20 | 22 |
| 21 using testing::_; | 23 using testing::_; |
| 22 using testing::NotNull; | 24 using testing::NotNull; |
| 23 using testing::SaveArg; | 25 using testing::SaveArg; |
| 24 | 26 |
| 25 namespace remoting { | 27 namespace remoting { |
| 26 namespace protocol { | 28 namespace protocol { |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 const char kTestSharedSecret[] = "1234-1234-5678"; | 32 const char kTestSharedSecret[] = "1234-1234-5678"; |
| 31 const char kTestSharedSecretBad[] = "0000-0000-0001"; | 33 const char kTestSharedSecretBad[] = "0000-0000-0001"; |
| 32 | 34 |
| 33 class MockChannelDoneCallback { | 35 class MockChannelDoneCallback { |
| 34 public: | 36 public: |
| 35 MOCK_METHOD2(OnDone, void(net::Error error, net::StreamSocket* socket)); | 37 MOCK_METHOD2(OnDone, void(net::Error error, net::StreamSocket* socket)); |
| 36 }; | 38 }; |
| 37 | 39 |
| 40 ACTION_P(QuitThreadOnCounter, counter) { |
| 41 --(*counter); |
| 42 EXPECT_GE(*counter, 0); |
| 43 if (*counter == 0) |
| 44 MessageLoop::current()->Quit(); |
| 45 } |
| 46 |
| 38 } // namespace | 47 } // namespace |
| 39 | 48 |
| 40 class SslHmacChannelAuthenticatorTest : public testing::Test { | 49 class SslHmacChannelAuthenticatorTest : public testing::Test { |
| 41 public: | 50 public: |
| 42 SslHmacChannelAuthenticatorTest() { | 51 SslHmacChannelAuthenticatorTest() {} |
| 43 } | 52 virtual ~SslHmacChannelAuthenticatorTest() {} |
| 44 virtual ~SslHmacChannelAuthenticatorTest() { | |
| 45 } | |
| 46 | 53 |
| 47 protected: | 54 protected: |
| 48 virtual void SetUp() OVERRIDE { | 55 virtual void SetUp() OVERRIDE { |
| 49 FilePath certs_dir(net::GetTestCertsDirectory()); | 56 FilePath certs_dir(net::GetTestCertsDirectory()); |
| 50 | 57 |
| 51 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); | 58 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); |
| 52 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &host_cert_)); | 59 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &host_cert_)); |
| 53 | 60 |
| 54 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); | 61 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); |
| 55 std::string key_string; | 62 std::string key_string; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 70 client_auth_->SecureAndAuthenticate( | 77 client_auth_->SecureAndAuthenticate( |
| 71 client_fake_socket_.PassAs<net::StreamSocket>(), | 78 client_fake_socket_.PassAs<net::StreamSocket>(), |
| 72 base::Bind(&SslHmacChannelAuthenticatorTest::OnClientConnected, | 79 base::Bind(&SslHmacChannelAuthenticatorTest::OnClientConnected, |
| 73 base::Unretained(this))); | 80 base::Unretained(this))); |
| 74 | 81 |
| 75 host_auth_->SecureAndAuthenticate( | 82 host_auth_->SecureAndAuthenticate( |
| 76 host_fake_socket_.PassAs<net::StreamSocket>(), | 83 host_fake_socket_.PassAs<net::StreamSocket>(), |
| 77 base::Bind(&SslHmacChannelAuthenticatorTest::OnHostConnected, | 84 base::Bind(&SslHmacChannelAuthenticatorTest::OnHostConnected, |
| 78 base::Unretained(this))); | 85 base::Unretained(this))); |
| 79 | 86 |
| 87 // Expect two callbacks to be called - the client callback and the host |
| 88 // callback. |
| 89 int callback_counter = 2; |
| 90 |
| 80 if (expected_fail) { | 91 if (expected_fail) { |
| 81 EXPECT_CALL(client_callback_, OnDone(net::ERR_FAILED, NULL)); | 92 EXPECT_CALL(client_callback_, OnDone(net::ERR_FAILED, NULL)) |
| 82 EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED, NULL)); | 93 .WillOnce(QuitThreadOnCounter(&callback_counter)); |
| 94 EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED, NULL)) |
| 95 .WillOnce(QuitThreadOnCounter(&callback_counter)); |
| 83 } else { | 96 } else { |
| 84 EXPECT_CALL(client_callback_, OnDone(net::OK, NotNull())); | 97 EXPECT_CALL(client_callback_, OnDone(net::OK, NotNull())) |
| 85 EXPECT_CALL(host_callback_, OnDone(net::OK, NotNull())); | 98 .WillOnce(QuitThreadOnCounter(&callback_counter)); |
| 99 EXPECT_CALL(host_callback_, OnDone(net::OK, NotNull())) |
| 100 .WillOnce(QuitThreadOnCounter(&callback_counter)); |
| 86 } | 101 } |
| 87 | 102 |
| 88 message_loop_.RunAllPending(); | 103 // Ensure that .Run() does not run unbounded if the callbacks are never |
| 104 // called. |
| 105 base::Timer shutdown_timer(false, false); |
| 106 shutdown_timer.Start(FROM_HERE, TestTimeouts::action_timeout(), |
| 107 MessageLoop::QuitClosure()); |
| 108 message_loop_.Run(); |
| 89 } | 109 } |
| 90 | 110 |
| 91 void OnHostConnected(net::Error error, | 111 void OnHostConnected(net::Error error, |
| 92 scoped_ptr<net::StreamSocket> socket) { | 112 scoped_ptr<net::StreamSocket> socket) { |
| 93 host_callback_.OnDone(error, socket.get()); | 113 host_callback_.OnDone(error, socket.get()); |
| 94 host_socket_ = socket.Pass(); | 114 host_socket_ = socket.Pass(); |
| 95 } | 115 } |
| 96 | 116 |
| 97 void OnClientConnected(net::Error error, | 117 void OnClientConnected(net::Error error, |
| 98 scoped_ptr<net::StreamSocket> socket) { | 118 scoped_ptr<net::StreamSocket> socket) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 118 | 138 |
| 119 // Verify that a channel can be connected using a valid shared secret. | 139 // Verify that a channel can be connected using a valid shared secret. |
| 120 TEST_F(SslHmacChannelAuthenticatorTest, SuccessfulAuth) { | 140 TEST_F(SslHmacChannelAuthenticatorTest, SuccessfulAuth) { |
| 121 client_auth_ = SslHmacChannelAuthenticator::CreateForClient( | 141 client_auth_ = SslHmacChannelAuthenticator::CreateForClient( |
| 122 host_cert_, kTestSharedSecret); | 142 host_cert_, kTestSharedSecret); |
| 123 host_auth_ = SslHmacChannelAuthenticator::CreateForHost( | 143 host_auth_ = SslHmacChannelAuthenticator::CreateForHost( |
| 124 host_cert_, private_key_.get(), kTestSharedSecret); | 144 host_cert_, private_key_.get(), kTestSharedSecret); |
| 125 | 145 |
| 126 RunChannelAuth(false); | 146 RunChannelAuth(false); |
| 127 | 147 |
| 128 EXPECT_TRUE(client_socket_.get() != NULL); | 148 ASSERT_TRUE(client_socket_.get() != NULL); |
| 129 EXPECT_TRUE(host_socket_.get() != NULL); | 149 ASSERT_TRUE(host_socket_.get() != NULL); |
| 130 | 150 |
| 131 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), | 151 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), |
| 132 100, 2); | 152 100, 2); |
| 133 | 153 |
| 134 tester.Start(); | 154 tester.Start(); |
| 135 message_loop_.Run(); | 155 message_loop_.Run(); |
| 136 tester.CheckResults(); | 156 tester.CheckResults(); |
| 137 } | 157 } |
| 138 | 158 |
| 139 // Verify that channels cannot be using invalid shared secret. | 159 // Verify that channels cannot be using invalid shared secret. |
| 140 TEST_F(SslHmacChannelAuthenticatorTest, InvalidChannelSecret) { | 160 TEST_F(SslHmacChannelAuthenticatorTest, InvalidChannelSecret) { |
| 141 client_auth_ = SslHmacChannelAuthenticator::CreateForClient( | 161 client_auth_ = SslHmacChannelAuthenticator::CreateForClient( |
| 142 host_cert_, kTestSharedSecretBad); | 162 host_cert_, kTestSharedSecretBad); |
| 143 host_auth_ = SslHmacChannelAuthenticator::CreateForHost( | 163 host_auth_ = SslHmacChannelAuthenticator::CreateForHost( |
| 144 host_cert_, private_key_.get(), kTestSharedSecret); | 164 host_cert_, private_key_.get(), kTestSharedSecret); |
| 145 | 165 |
| 146 RunChannelAuth(true); | 166 RunChannelAuth(true); |
| 147 | 167 |
| 148 EXPECT_TRUE(host_socket_.get() == NULL); | 168 ASSERT_TRUE(host_socket_.get() == NULL); |
| 149 } | 169 } |
| 150 | 170 |
| 151 } // namespace protocol | 171 } // namespace protocol |
| 152 } // namespace remoting | 172 } // namespace remoting |
| OLD | NEW |