Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(495)

Side by Side Diff: components/proximity_auth/remote_device_life_cycle_impl_unittest.cc

Issue 2561203002: Migrate weave-related classes from proximity_auth/ble to cryptauth/ble. (Closed)
Patch Set: Rebase. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/connection_finder.h"
17 #include "components/cryptauth/cryptauth_test_util.h"
18 #include "components/cryptauth/fake_connection.h"
19 #include "components/cryptauth/wire_message.h"
16 #include "components/proximity_auth/authenticator.h" 20 #include "components/proximity_auth/authenticator.h"
17 #include "components/proximity_auth/connection_finder.h"
18 #include "components/proximity_auth/fake_connection.h"
19 #include "components/proximity_auth/messenger.h" 21 #include "components/proximity_auth/messenger.h"
20 #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 19 matching lines...) Expand all
52 NOTREACHED(); 52 NOTREACHED();
53 return SecureContext::PROTOCOL_VERSION_THREE_ONE; 53 return SecureContext::PROTOCOL_VERSION_THREE_ONE;
54 } 54 }
55 55
56 std::string GetChannelBindingData() const override { return std::string(); } 56 std::string GetChannelBindingData() const override { return std::string(); }
57 57
58 private: 58 private:
59 DISALLOW_COPY_AND_ASSIGN(StubSecureContext); 59 DISALLOW_COPY_AND_ASSIGN(StubSecureContext);
60 }; 60 };
61 61
62 class FakeConnectionFinder : public ConnectionFinder { 62 class FakeConnectionFinder : public cryptauth::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<cryptauth::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 // cryptauth::ConnectionFinder:
80 void Find(const ConnectionCallback& connection_callback) override { 80 void Find(const cryptauth::ConnectionFinder::ConnectionCallback&
81 connection_callback) override {
81 ASSERT_TRUE(connection_callback_.is_null()); 82 ASSERT_TRUE(connection_callback_.is_null());
82 connection_callback_ = connection_callback; 83 connection_callback_ = connection_callback;
83 } 84 }
84 85
85 const cryptauth::RemoteDevice remote_device_; 86 const cryptauth::RemoteDevice remote_device_;
86 87
87 FakeConnection* connection_; 88 cryptauth::FakeConnection* connection_;
88 89
89 ConnectionCallback connection_callback_; 90 cryptauth::ConnectionFinder::ConnectionCallback connection_callback_;
90 91
91 DISALLOW_COPY_AND_ASSIGN(FakeConnectionFinder); 92 DISALLOW_COPY_AND_ASSIGN(FakeConnectionFinder);
92 }; 93 };
93 94
94 class FakeAuthenticator : public Authenticator { 95 class FakeAuthenticator : public Authenticator {
95 public: 96 public:
96 FakeAuthenticator(Connection* connection) : connection_(connection) {} 97 FakeAuthenticator(cryptauth::Connection* connection)
98 : connection_(connection) {}
97 ~FakeAuthenticator() override { 99 ~FakeAuthenticator() override {
98 // This object should be destroyed immediately after authentication is 100 // This object should be destroyed immediately after authentication is
99 // complete in order not to outlive the underlying connection. 101 // complete in order not to outlive the underlying connection.
100 EXPECT_FALSE(callback_.is_null()); 102 EXPECT_FALSE(callback_.is_null());
101 EXPECT_EQ(kTestRemoteDevicePublicKey, 103 EXPECT_EQ(cryptauth::kTestRemoteDevicePublicKey,
102 connection_->remote_device().public_key); 104 connection_->remote_device().public_key);
103 } 105 }
104 106
105 void OnAuthenticationResult(Authenticator::Result result) { 107 void OnAuthenticationResult(Authenticator::Result result) {
106 ASSERT_FALSE(callback_.is_null()); 108 ASSERT_FALSE(callback_.is_null());
107 std::unique_ptr<SecureContext> secure_context; 109 std::unique_ptr<SecureContext> secure_context;
108 if (result == Authenticator::Result::SUCCESS) 110 if (result == Authenticator::Result::SUCCESS)
109 secure_context.reset(new StubSecureContext()); 111 secure_context.reset(new StubSecureContext());
110 callback_.Run(result, std::move(secure_context)); 112 callback_.Run(result, std::move(secure_context));
111 } 113 }
112 114
113 private: 115 private:
114 // Authenticator: 116 // Authenticator:
115 void Authenticate(const AuthenticationCallback& callback) override { 117 void Authenticate(const AuthenticationCallback& callback) override {
116 ASSERT_TRUE(callback_.is_null()); 118 ASSERT_TRUE(callback_.is_null());
117 callback_ = callback; 119 callback_ = callback;
118 } 120 }
119 121
120 Connection* connection_; 122 cryptauth::Connection* connection_;
121 123
122 AuthenticationCallback callback_; 124 AuthenticationCallback callback_;
123 125
124 DISALLOW_COPY_AND_ASSIGN(FakeAuthenticator); 126 DISALLOW_COPY_AND_ASSIGN(FakeAuthenticator);
125 }; 127 };
126 128
127 // Subclass of RemoteDeviceLifeCycleImpl to make it testable. 129 // Subclass of RemoteDeviceLifeCycleImpl to make it testable.
128 class TestableRemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycleImpl { 130 class TestableRemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycleImpl {
129 public: 131 public:
130 TestableRemoteDeviceLifeCycleImpl( 132 TestableRemoteDeviceLifeCycleImpl(
131 const cryptauth::RemoteDevice& remote_device) 133 const cryptauth::RemoteDevice& remote_device)
132 : RemoteDeviceLifeCycleImpl(remote_device, nullptr), 134 : RemoteDeviceLifeCycleImpl(remote_device, nullptr),
133 remote_device_(remote_device) {} 135 remote_device_(remote_device) {}
134 136
135 ~TestableRemoteDeviceLifeCycleImpl() override {} 137 ~TestableRemoteDeviceLifeCycleImpl() override {}
136 138
137 FakeConnectionFinder* connection_finder() { return connection_finder_; } 139 FakeConnectionFinder* connection_finder() { return connection_finder_; }
138 FakeAuthenticator* authenticator() { return authenticator_; } 140 FakeAuthenticator* authenticator() { return authenticator_; }
139 141
140 private: 142 private:
141 std::unique_ptr<ConnectionFinder> CreateConnectionFinder() override { 143 std::unique_ptr<cryptauth::ConnectionFinder> CreateConnectionFinder()
144 override {
142 std::unique_ptr<FakeConnectionFinder> scoped_connection_finder( 145 std::unique_ptr<FakeConnectionFinder> scoped_connection_finder(
143 new FakeConnectionFinder(remote_device_)); 146 new FakeConnectionFinder(remote_device_));
144 connection_finder_ = scoped_connection_finder.get(); 147 connection_finder_ = scoped_connection_finder.get();
145 return std::move(scoped_connection_finder); 148 return std::move(scoped_connection_finder);
146 } 149 }
147 150
148 std::unique_ptr<Authenticator> CreateAuthenticator() override { 151 std::unique_ptr<Authenticator> CreateAuthenticator() override {
149 EXPECT_TRUE(connection_finder_); 152 EXPECT_TRUE(connection_finder_);
150 std::unique_ptr<FakeAuthenticator> scoped_authenticator( 153 std::unique_ptr<FakeAuthenticator> scoped_authenticator(
151 new FakeAuthenticator(connection_finder_->connection())); 154 new FakeAuthenticator(connection_finder_->connection()));
152 authenticator_ = scoped_authenticator.get(); 155 authenticator_ = scoped_authenticator.get();
153 return std::move(scoped_authenticator); 156 return std::move(scoped_authenticator);
154 } 157 }
155 158
156 const cryptauth::RemoteDevice remote_device_; 159 const cryptauth::RemoteDevice remote_device_;
157 FakeConnectionFinder* connection_finder_; 160 FakeConnectionFinder* connection_finder_;
158 FakeAuthenticator* authenticator_; 161 FakeAuthenticator* authenticator_;
159 162
160 DISALLOW_COPY_AND_ASSIGN(TestableRemoteDeviceLifeCycleImpl); 163 DISALLOW_COPY_AND_ASSIGN(TestableRemoteDeviceLifeCycleImpl);
161 }; 164 };
162 165
163 } // namespace 166 } // namespace
164 167
165 class ProximityAuthRemoteDeviceLifeCycleImplTest 168 class ProximityAuthRemoteDeviceLifeCycleImplTest
166 : public testing::Test, 169 : public testing::Test,
167 public RemoteDeviceLifeCycle::Observer { 170 public RemoteDeviceLifeCycle::Observer {
168 protected: 171 protected:
169 ProximityAuthRemoteDeviceLifeCycleImplTest() 172 ProximityAuthRemoteDeviceLifeCycleImplTest()
170 : life_cycle_(CreateClassicRemoteDeviceForTest()), 173 : life_cycle_(cryptauth::CreateClassicRemoteDeviceForTest()),
171 task_runner_(new base::TestSimpleTaskRunner()), 174 task_runner_(new base::TestSimpleTaskRunner()),
172 thread_task_runner_handle_(task_runner_) {} 175 thread_task_runner_handle_(task_runner_) {}
173 176
174 ~ProximityAuthRemoteDeviceLifeCycleImplTest() override { 177 ~ProximityAuthRemoteDeviceLifeCycleImplTest() override {
175 life_cycle_.RemoveObserver(this); 178 life_cycle_.RemoveObserver(this);
176 } 179 }
177 180
178 void StartLifeCycle() { 181 void StartLifeCycle() {
179 EXPECT_EQ(RemoteDeviceLifeCycle::State::STOPPED, life_cycle_.GetState()); 182 EXPECT_EQ(RemoteDeviceLifeCycle::State::STOPPED, life_cycle_.GetState());
180 life_cycle_.AddObserver(this); 183 life_cycle_.AddObserver(this);
181 184
182 EXPECT_CALL(*this, OnLifeCycleStateChanged( 185 EXPECT_CALL(*this, OnLifeCycleStateChanged(
183 RemoteDeviceLifeCycle::State::STOPPED, 186 RemoteDeviceLifeCycle::State::STOPPED,
184 RemoteDeviceLifeCycle::State::FINDING_CONNECTION)); 187 RemoteDeviceLifeCycle::State::FINDING_CONNECTION));
185 life_cycle_.Start(); 188 life_cycle_.Start();
186 task_runner_->RunUntilIdle(); 189 task_runner_->RunUntilIdle();
187 Mock::VerifyAndClearExpectations(this); 190 Mock::VerifyAndClearExpectations(this);
188 191
189 EXPECT_EQ(RemoteDeviceLifeCycle::State::FINDING_CONNECTION, 192 EXPECT_EQ(RemoteDeviceLifeCycle::State::FINDING_CONNECTION,
190 life_cycle_.GetState()); 193 life_cycle_.GetState());
191 } 194 }
192 195
193 FakeConnection* OnConnectionFound() { 196 cryptauth::FakeConnection* OnConnectionFound() {
194 EXPECT_EQ(RemoteDeviceLifeCycle::State::FINDING_CONNECTION, 197 EXPECT_EQ(RemoteDeviceLifeCycle::State::FINDING_CONNECTION,
195 life_cycle_.GetState()); 198 life_cycle_.GetState());
196 199
197 EXPECT_CALL(*this, OnLifeCycleStateChanged( 200 EXPECT_CALL(*this, OnLifeCycleStateChanged(
198 RemoteDeviceLifeCycle::State::FINDING_CONNECTION, 201 RemoteDeviceLifeCycle::State::FINDING_CONNECTION,
199 RemoteDeviceLifeCycle::State::AUTHENTICATING)); 202 RemoteDeviceLifeCycle::State::AUTHENTICATING));
200 life_cycle_.connection_finder()->OnConnectionFound(); 203 life_cycle_.connection_finder()->OnConnectionFound();
201 task_runner_->RunUntilIdle(); 204 task_runner_->RunUntilIdle();
202 Mock::VerifyAndClearExpectations(this); 205 Mock::VerifyAndClearExpectations(this);
203 206
(...skipping 30 matching lines...) Expand all
234 TestableRemoteDeviceLifeCycleImpl life_cycle_; 237 TestableRemoteDeviceLifeCycleImpl life_cycle_;
235 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 238 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
236 base::ThreadTaskRunnerHandle thread_task_runner_handle_; 239 base::ThreadTaskRunnerHandle thread_task_runner_handle_;
237 240
238 private: 241 private:
239 DISALLOW_COPY_AND_ASSIGN(ProximityAuthRemoteDeviceLifeCycleImplTest); 242 DISALLOW_COPY_AND_ASSIGN(ProximityAuthRemoteDeviceLifeCycleImplTest);
240 }; 243 };
241 244
242 TEST_F(ProximityAuthRemoteDeviceLifeCycleImplTest, GetRemoteDevice) { 245 TEST_F(ProximityAuthRemoteDeviceLifeCycleImplTest, GetRemoteDevice) {
243 cryptauth::RemoteDevice expected_remote_device = 246 cryptauth::RemoteDevice expected_remote_device =
244 CreateClassicRemoteDeviceForTest(); 247 cryptauth::CreateClassicRemoteDeviceForTest();
245 cryptauth::RemoteDevice remote_device = life_cycle_.GetRemoteDevice(); 248 cryptauth::RemoteDevice remote_device = life_cycle_.GetRemoteDevice();
246 EXPECT_EQ(expected_remote_device.user_id, remote_device.user_id); 249 EXPECT_EQ(expected_remote_device.user_id, remote_device.user_id);
247 EXPECT_EQ(expected_remote_device.name, remote_device.name); 250 EXPECT_EQ(expected_remote_device.name, remote_device.name);
248 EXPECT_EQ(expected_remote_device.public_key, remote_device.public_key); 251 EXPECT_EQ(expected_remote_device.public_key, remote_device.public_key);
249 EXPECT_EQ(expected_remote_device.bluetooth_address, 252 EXPECT_EQ(expected_remote_device.bluetooth_address,
250 remote_device.bluetooth_address); 253 remote_device.bluetooth_address);
251 EXPECT_EQ(expected_remote_device.persistent_symmetric_key, 254 EXPECT_EQ(expected_remote_device.persistent_symmetric_key,
252 remote_device.persistent_symmetric_key); 255 remote_device.persistent_symmetric_key);
253 } 256 }
254 257
255 TEST_F(ProximityAuthRemoteDeviceLifeCycleImplTest, AuthenticateAndDisconnect) { 258 TEST_F(ProximityAuthRemoteDeviceLifeCycleImplTest, AuthenticateAndDisconnect) {
256 StartLifeCycle(); 259 StartLifeCycle();
257 for (size_t i = 0; i < 3; ++i) { 260 for (size_t i = 0; i < 3; ++i) {
258 Connection* connection = OnConnectionFound(); 261 cryptauth::Connection* connection = OnConnectionFound();
259 Authenticate(Authenticator::Result::SUCCESS); 262 Authenticate(Authenticator::Result::SUCCESS);
260 EXPECT_TRUE(life_cycle_.GetMessenger()); 263 EXPECT_TRUE(life_cycle_.GetMessenger());
261 264
262 EXPECT_CALL(*this, 265 EXPECT_CALL(*this,
263 OnLifeCycleStateChanged( 266 OnLifeCycleStateChanged(
264 RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED, 267 RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED,
265 RemoteDeviceLifeCycle::State::FINDING_CONNECTION)); 268 RemoteDeviceLifeCycle::State::FINDING_CONNECTION));
266 connection->Disconnect(); 269 connection->Disconnect();
267 Mock::VerifyAndClearExpectations(this); 270 Mock::VerifyAndClearExpectations(this);
268 } 271 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 AuthenticationFailsThenSucceeds) { 304 AuthenticationFailsThenSucceeds) {
302 // Authentication fails on first pass. 305 // Authentication fails on first pass.
303 StartLifeCycle(); 306 StartLifeCycle();
304 OnConnectionFound(); 307 OnConnectionFound();
305 Authenticate(Authenticator::Result::FAILURE); 308 Authenticate(Authenticator::Result::FAILURE);
306 EXPECT_FALSE(life_cycle_.GetMessenger()); 309 EXPECT_FALSE(life_cycle_.GetMessenger());
307 EXPECT_CALL(*this, OnLifeCycleStateChanged(_, _)); 310 EXPECT_CALL(*this, OnLifeCycleStateChanged(_, _));
308 task_runner_->RunUntilIdle(); 311 task_runner_->RunUntilIdle();
309 312
310 // Authentication succeeds on second pass. 313 // Authentication succeeds on second pass.
311 Connection* connection = OnConnectionFound(); 314 cryptauth::Connection* connection = OnConnectionFound();
312 Authenticate(Authenticator::Result::SUCCESS); 315 Authenticate(Authenticator::Result::SUCCESS);
313 EXPECT_TRUE(life_cycle_.GetMessenger()); 316 EXPECT_TRUE(life_cycle_.GetMessenger());
314 EXPECT_CALL(*this, OnLifeCycleStateChanged(_, _)); 317 EXPECT_CALL(*this, OnLifeCycleStateChanged(_, _));
315 connection->Disconnect(); 318 connection->Disconnect();
316 } 319 }
317 320
318 } // namespace proximity_auth 321 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698