| 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 "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/test/test_simple_task_runner.h" | 9 #include "base/test/test_simple_task_runner.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 | 79 |
| 80 FakeConnection* connection_; | 80 FakeConnection* connection_; |
| 81 | 81 |
| 82 ConnectionCallback connection_callback_; | 82 ConnectionCallback connection_callback_; |
| 83 | 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(FakeConnectionFinder); | 84 DISALLOW_COPY_AND_ASSIGN(FakeConnectionFinder); |
| 85 }; | 85 }; |
| 86 | 86 |
| 87 class FakeAuthenticator : public Authenticator { | 87 class FakeAuthenticator : public Authenticator { |
| 88 public: | 88 public: |
| 89 FakeAuthenticator() {} | 89 FakeAuthenticator(Connection* connection) : connection_(connection) {} |
| 90 ~FakeAuthenticator() override {} | 90 ~FakeAuthenticator() override { |
| 91 // This object should be destroyed immediately after authentication is |
| 92 // complete in order not to outlive the underlying connection. |
| 93 EXPECT_FALSE(callback_.is_null()); |
| 94 EXPECT_EQ(kTestRemoteDevicePublicKey, |
| 95 connection_->remote_device().public_key); |
| 96 } |
| 91 | 97 |
| 92 void OnAuthenticationResult(Authenticator::Result result) { | 98 void OnAuthenticationResult(Authenticator::Result result) { |
| 93 ASSERT_FALSE(callback_.is_null()); | 99 ASSERT_FALSE(callback_.is_null()); |
| 94 scoped_ptr<SecureContext> secure_context; | 100 scoped_ptr<SecureContext> secure_context; |
| 95 if (result == Authenticator::Result::SUCCESS) | 101 if (result == Authenticator::Result::SUCCESS) |
| 96 secure_context.reset(new StubSecureContext()); | 102 secure_context.reset(new StubSecureContext()); |
| 97 callback_.Run(result, secure_context.Pass()); | 103 callback_.Run(result, secure_context.Pass()); |
| 98 } | 104 } |
| 99 | 105 |
| 100 private: | 106 private: |
| 101 // Authenticator: | 107 // Authenticator: |
| 102 void Authenticate(const AuthenticationCallback& callback) override { | 108 void Authenticate(const AuthenticationCallback& callback) override { |
| 103 ASSERT_TRUE(callback_.is_null()); | 109 ASSERT_TRUE(callback_.is_null()); |
| 104 callback_ = callback; | 110 callback_ = callback; |
| 105 } | 111 } |
| 106 | 112 |
| 113 Connection* connection_; |
| 114 |
| 107 AuthenticationCallback callback_; | 115 AuthenticationCallback callback_; |
| 108 | 116 |
| 109 DISALLOW_COPY_AND_ASSIGN(FakeAuthenticator); | 117 DISALLOW_COPY_AND_ASSIGN(FakeAuthenticator); |
| 110 }; | 118 }; |
| 111 | 119 |
| 112 // Subclass of RemoteDeviceLifeCycleImpl to make it testable. | 120 // Subclass of RemoteDeviceLifeCycleImpl to make it testable. |
| 113 class TestableRemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycleImpl { | 121 class TestableRemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycleImpl { |
| 114 public: | 122 public: |
| 115 TestableRemoteDeviceLifeCycleImpl(const RemoteDevice& remote_device) | 123 TestableRemoteDeviceLifeCycleImpl(const RemoteDevice& remote_device) |
| 116 : RemoteDeviceLifeCycleImpl(remote_device, nullptr), | 124 : RemoteDeviceLifeCycleImpl(remote_device, nullptr), |
| 117 remote_device_(remote_device) {} | 125 remote_device_(remote_device) {} |
| 118 | 126 |
| 119 ~TestableRemoteDeviceLifeCycleImpl() override {} | 127 ~TestableRemoteDeviceLifeCycleImpl() override {} |
| 120 | 128 |
| 121 FakeConnectionFinder* connection_finder() { return connection_finder_; } | 129 FakeConnectionFinder* connection_finder() { return connection_finder_; } |
| 122 FakeAuthenticator* authenticator() { return authenticator_; } | 130 FakeAuthenticator* authenticator() { return authenticator_; } |
| 123 | 131 |
| 124 private: | 132 private: |
| 125 scoped_ptr<ConnectionFinder> CreateConnectionFinder() override { | 133 scoped_ptr<ConnectionFinder> CreateConnectionFinder() override { |
| 126 scoped_ptr<FakeConnectionFinder> scoped_connection_finder( | 134 scoped_ptr<FakeConnectionFinder> scoped_connection_finder( |
| 127 new FakeConnectionFinder(remote_device_)); | 135 new FakeConnectionFinder(remote_device_)); |
| 128 connection_finder_ = scoped_connection_finder.get(); | 136 connection_finder_ = scoped_connection_finder.get(); |
| 129 return scoped_connection_finder.Pass(); | 137 return scoped_connection_finder.Pass(); |
| 130 } | 138 } |
| 131 | 139 |
| 132 scoped_ptr<Authenticator> CreateAuthenticator() override { | 140 scoped_ptr<Authenticator> CreateAuthenticator() override { |
| 133 scoped_ptr<FakeAuthenticator> scoped_authenticator(new FakeAuthenticator()); | 141 EXPECT_TRUE(connection_finder_); |
| 142 scoped_ptr<FakeAuthenticator> scoped_authenticator( |
| 143 new FakeAuthenticator(connection_finder_->connection())); |
| 134 authenticator_ = scoped_authenticator.get(); | 144 authenticator_ = scoped_authenticator.get(); |
| 135 return scoped_authenticator.Pass(); | 145 return scoped_authenticator.Pass(); |
| 136 } | 146 } |
| 137 | 147 |
| 138 const RemoteDevice remote_device_; | 148 const RemoteDevice remote_device_; |
| 139 FakeConnectionFinder* connection_finder_; | 149 FakeConnectionFinder* connection_finder_; |
| 140 FakeAuthenticator* authenticator_; | 150 FakeAuthenticator* authenticator_; |
| 141 | 151 |
| 142 DISALLOW_COPY_AND_ASSIGN(TestableRemoteDeviceLifeCycleImpl); | 152 DISALLOW_COPY_AND_ASSIGN(TestableRemoteDeviceLifeCycleImpl); |
| 143 }; | 153 }; |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 | 288 |
| 279 // Authentication succeeds on second pass. | 289 // Authentication succeeds on second pass. |
| 280 Connection* connection = OnConnectionFound(); | 290 Connection* connection = OnConnectionFound(); |
| 281 Authenticate(Authenticator::Result::SUCCESS); | 291 Authenticate(Authenticator::Result::SUCCESS); |
| 282 EXPECT_TRUE(life_cycle_.GetMessenger()); | 292 EXPECT_TRUE(life_cycle_.GetMessenger()); |
| 283 EXPECT_CALL(*this, OnLifeCycleStateChanged(_, _)); | 293 EXPECT_CALL(*this, OnLifeCycleStateChanged(_, _)); |
| 284 connection->Disconnect(); | 294 connection->Disconnect(); |
| 285 } | 295 } |
| 286 | 296 |
| 287 } // namespace proximity_auth | 297 } // namespace proximity_auth |
| OLD | NEW |