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