| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/proximity_auth/remote_device_life_cycle_impl.h" | 5 #include "components/proximity_auth/remote_device_life_cycle_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/test/test_simple_task_runner.h" | 14 #include "base/test/test_simple_task_runner.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "components/cryptauth/fake_connection.h" |
| 17 #include "components/cryptauth/wire_message.h" |
| 16 #include "components/proximity_auth/authenticator.h" | 18 #include "components/proximity_auth/authenticator.h" |
| 17 #include "components/proximity_auth/connection_finder.h" | 19 #include "components/proximity_auth/connection_finder.h" |
| 18 #include "components/proximity_auth/fake_connection.h" | |
| 19 #include "components/proximity_auth/messenger.h" | 20 #include "components/proximity_auth/messenger.h" |
| 20 #include "components/proximity_auth/proximity_auth_test_util.h" | 21 #include "components/proximity_auth/proximity_auth_test_util.h" |
| 21 #include "components/proximity_auth/secure_context.h" | 22 #include "components/proximity_auth/secure_context.h" |
| 22 #include "components/proximity_auth/wire_message.h" | |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 25 |
| 26 using testing::_; | 26 using testing::_; |
| 27 using testing::Mock; | 27 using testing::Mock; |
| 28 using testing::NiceMock; | 28 using testing::NiceMock; |
| 29 using testing::Return; | 29 using testing::Return; |
| 30 using testing::StrictMock; | 30 using testing::StrictMock; |
| 31 | 31 |
| 32 namespace proximity_auth { | 32 namespace proximity_auth { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 61 | 61 |
| 62 class FakeConnectionFinder : public ConnectionFinder { | 62 class FakeConnectionFinder : public ConnectionFinder { |
| 63 public: | 63 public: |
| 64 FakeConnectionFinder(const cryptauth::RemoteDevice& remote_device) | 64 FakeConnectionFinder(const cryptauth::RemoteDevice& remote_device) |
| 65 : remote_device_(remote_device), connection_(nullptr) {} | 65 : remote_device_(remote_device), connection_(nullptr) {} |
| 66 ~FakeConnectionFinder() override {} | 66 ~FakeConnectionFinder() override {} |
| 67 | 67 |
| 68 void OnConnectionFound() { | 68 void OnConnectionFound() { |
| 69 ASSERT_FALSE(connection_callback_.is_null()); | 69 ASSERT_FALSE(connection_callback_.is_null()); |
| 70 std::unique_ptr<FakeConnection> scoped_connection_( | 70 std::unique_ptr<FakeConnection> scoped_connection_( |
| 71 new FakeConnection(remote_device_)); | 71 new cryptauth::FakeConnection(remote_device_)); |
| 72 connection_ = scoped_connection_.get(); | 72 connection_ = scoped_connection_.get(); |
| 73 connection_callback_.Run(std::move(scoped_connection_)); | 73 connection_callback_.Run(std::move(scoped_connection_)); |
| 74 } | 74 } |
| 75 | 75 |
| 76 FakeConnection* connection() { return connection_; } | 76 cryptauth::FakeConnection* connection() { return connection_; } |
| 77 | 77 |
| 78 private: | 78 private: |
| 79 // ConnectionFinder: | 79 // ConnectionFinder: |
| 80 void Find(const ConnectionCallback& connection_callback) override { | 80 void Find(const ConnectionCallback& connection_callback) override { |
| 81 ASSERT_TRUE(connection_callback_.is_null()); | 81 ASSERT_TRUE(connection_callback_.is_null()); |
| 82 connection_callback_ = connection_callback; | 82 connection_callback_ = connection_callback; |
| 83 } | 83 } |
| 84 | 84 |
| 85 const cryptauth::RemoteDevice remote_device_; | 85 const cryptauth::RemoteDevice remote_device_; |
| 86 | 86 |
| 87 FakeConnection* connection_; | 87 cryptauth::FakeConnection* connection_; |
| 88 | 88 |
| 89 ConnectionCallback connection_callback_; | 89 ConnectionCallback connection_callback_; |
| 90 | 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(FakeConnectionFinder); | 91 DISALLOW_COPY_AND_ASSIGN(FakeConnectionFinder); |
| 92 }; | 92 }; |
| 93 | 93 |
| 94 class FakeAuthenticator : public Authenticator { | 94 class FakeAuthenticator : public Authenticator { |
| 95 public: | 95 public: |
| 96 FakeAuthenticator(Connection* connection) : connection_(connection) {} | 96 FakeAuthenticator(cryptauth::Connection* connection) |
| 97 : connection_(connection) {} |
| 97 ~FakeAuthenticator() override { | 98 ~FakeAuthenticator() override { |
| 98 // This object should be destroyed immediately after authentication is | 99 // This object should be destroyed immediately after authentication is |
| 99 // complete in order not to outlive the underlying connection. | 100 // complete in order not to outlive the underlying connection. |
| 100 EXPECT_FALSE(callback_.is_null()); | 101 EXPECT_FALSE(callback_.is_null()); |
| 101 EXPECT_EQ(kTestRemoteDevicePublicKey, | 102 EXPECT_EQ(kTestRemoteDevicePublicKey, |
| 102 connection_->remote_device().public_key); | 103 connection_->remote_device().public_key); |
| 103 } | 104 } |
| 104 | 105 |
| 105 void OnAuthenticationResult(Authenticator::Result result) { | 106 void OnAuthenticationResult(Authenticator::Result result) { |
| 106 ASSERT_FALSE(callback_.is_null()); | 107 ASSERT_FALSE(callback_.is_null()); |
| 107 std::unique_ptr<SecureContext> secure_context; | 108 std::unique_ptr<SecureContext> secure_context; |
| 108 if (result == Authenticator::Result::SUCCESS) | 109 if (result == Authenticator::Result::SUCCESS) |
| 109 secure_context.reset(new StubSecureContext()); | 110 secure_context.reset(new StubSecureContext()); |
| 110 callback_.Run(result, std::move(secure_context)); | 111 callback_.Run(result, std::move(secure_context)); |
| 111 } | 112 } |
| 112 | 113 |
| 113 private: | 114 private: |
| 114 // Authenticator: | 115 // Authenticator: |
| 115 void Authenticate(const AuthenticationCallback& callback) override { | 116 void Authenticate(const AuthenticationCallback& callback) override { |
| 116 ASSERT_TRUE(callback_.is_null()); | 117 ASSERT_TRUE(callback_.is_null()); |
| 117 callback_ = callback; | 118 callback_ = callback; |
| 118 } | 119 } |
| 119 | 120 |
| 120 Connection* connection_; | 121 cryptauth::Connection* connection_; |
| 121 | 122 |
| 122 AuthenticationCallback callback_; | 123 AuthenticationCallback callback_; |
| 123 | 124 |
| 124 DISALLOW_COPY_AND_ASSIGN(FakeAuthenticator); | 125 DISALLOW_COPY_AND_ASSIGN(FakeAuthenticator); |
| 125 }; | 126 }; |
| 126 | 127 |
| 127 // Subclass of RemoteDeviceLifeCycleImpl to make it testable. | 128 // Subclass of RemoteDeviceLifeCycleImpl to make it testable. |
| 128 class TestableRemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycleImpl { | 129 class TestableRemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycleImpl { |
| 129 public: | 130 public: |
| 130 TestableRemoteDeviceLifeCycleImpl( | 131 TestableRemoteDeviceLifeCycleImpl( |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 RemoteDeviceLifeCycle::State::STOPPED, | 184 RemoteDeviceLifeCycle::State::STOPPED, |
| 184 RemoteDeviceLifeCycle::State::FINDING_CONNECTION)); | 185 RemoteDeviceLifeCycle::State::FINDING_CONNECTION)); |
| 185 life_cycle_.Start(); | 186 life_cycle_.Start(); |
| 186 task_runner_->RunUntilIdle(); | 187 task_runner_->RunUntilIdle(); |
| 187 Mock::VerifyAndClearExpectations(this); | 188 Mock::VerifyAndClearExpectations(this); |
| 188 | 189 |
| 189 EXPECT_EQ(RemoteDeviceLifeCycle::State::FINDING_CONNECTION, | 190 EXPECT_EQ(RemoteDeviceLifeCycle::State::FINDING_CONNECTION, |
| 190 life_cycle_.GetState()); | 191 life_cycle_.GetState()); |
| 191 } | 192 } |
| 192 | 193 |
| 193 FakeConnection* OnConnectionFound() { | 194 cryptauth::FakeConnection* OnConnectionFound() { |
| 194 EXPECT_EQ(RemoteDeviceLifeCycle::State::FINDING_CONNECTION, | 195 EXPECT_EQ(RemoteDeviceLifeCycle::State::FINDING_CONNECTION, |
| 195 life_cycle_.GetState()); | 196 life_cycle_.GetState()); |
| 196 | 197 |
| 197 EXPECT_CALL(*this, OnLifeCycleStateChanged( | 198 EXPECT_CALL(*this, OnLifeCycleStateChanged( |
| 198 RemoteDeviceLifeCycle::State::FINDING_CONNECTION, | 199 RemoteDeviceLifeCycle::State::FINDING_CONNECTION, |
| 199 RemoteDeviceLifeCycle::State::AUTHENTICATING)); | 200 RemoteDeviceLifeCycle::State::AUTHENTICATING)); |
| 200 life_cycle_.connection_finder()->OnConnectionFound(); | 201 life_cycle_.connection_finder()->OnConnectionFound(); |
| 201 task_runner_->RunUntilIdle(); | 202 task_runner_->RunUntilIdle(); |
| 202 Mock::VerifyAndClearExpectations(this); | 203 Mock::VerifyAndClearExpectations(this); |
| 203 | 204 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 | 310 |
| 310 // Authentication succeeds on second pass. | 311 // Authentication succeeds on second pass. |
| 311 Connection* connection = OnConnectionFound(); | 312 Connection* connection = OnConnectionFound(); |
| 312 Authenticate(Authenticator::Result::SUCCESS); | 313 Authenticate(Authenticator::Result::SUCCESS); |
| 313 EXPECT_TRUE(life_cycle_.GetMessenger()); | 314 EXPECT_TRUE(life_cycle_.GetMessenger()); |
| 314 EXPECT_CALL(*this, OnLifeCycleStateChanged(_, _)); | 315 EXPECT_CALL(*this, OnLifeCycleStateChanged(_, _)); |
| 315 connection->Disconnect(); | 316 connection->Disconnect(); |
| 316 } | 317 } |
| 317 | 318 |
| 318 } // namespace proximity_auth | 319 } // namespace proximity_auth |
| OLD | NEW |