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" |
11 #include "components/proximity_auth/authenticator.h" | 11 #include "components/proximity_auth/authenticator.h" |
12 #include "components/proximity_auth/connection.h" | |
13 #include "components/proximity_auth/connection_finder.h" | 12 #include "components/proximity_auth/connection_finder.h" |
| 13 #include "components/proximity_auth/fake_connection.h" |
14 #include "components/proximity_auth/messenger.h" | 14 #include "components/proximity_auth/messenger.h" |
15 #include "components/proximity_auth/secure_context.h" | 15 #include "components/proximity_auth/secure_context.h" |
16 #include "components/proximity_auth/wire_message.h" | 16 #include "components/proximity_auth/wire_message.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 using testing::_; | 20 using testing::_; |
21 using testing::Mock; | 21 using testing::Mock; |
22 using testing::NiceMock; | 22 using testing::NiceMock; |
23 using testing::Return; | 23 using testing::Return; |
24 using testing::StrictMock; | 24 using testing::StrictMock; |
25 | 25 |
26 namespace proximity_auth { | 26 namespace proximity_auth { |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 // Attributes of the remote device under test. | 30 // Attributes of the remote device under test. |
31 const char kRemoteDeviceName[] = "remote device"; | 31 const char kRemoteDeviceName[] = "remote device"; |
32 const char kRemoteDevicePublicKey[] = "public key"; | 32 const char kRemoteDevicePublicKey[] = "public key"; |
33 const char kRemoteDeviceBluetoothAddress[] = "AA:BB:CC:DD:EE:FF"; | 33 const char kRemoteDeviceBluetoothAddress[] = "AA:BB:CC:DD:EE:FF"; |
34 const char kRemoteDevicePSK[] = "remote device psk"; | 34 const char kRemoteDevicePSK[] = "remote device psk"; |
35 | 35 |
36 class StubConnection : public Connection { | |
37 public: | |
38 StubConnection() : Connection(RemoteDevice()) { | |
39 SetStatus(Connection::Status::CONNECTED); | |
40 } | |
41 | |
42 ~StubConnection() override {} | |
43 | |
44 // Connection: | |
45 void Connect() override { NOTREACHED(); } | |
46 | |
47 void Disconnect() override { SetStatus(Connection::Status::DISCONNECTED); } | |
48 | |
49 void SendMessageImpl(scoped_ptr<WireMessage> message) override { | |
50 NOTREACHED(); | |
51 } | |
52 | |
53 private: | |
54 DISALLOW_COPY_AND_ASSIGN(StubConnection); | |
55 }; | |
56 | |
57 class StubSecureContext : public SecureContext { | 36 class StubSecureContext : public SecureContext { |
58 public: | 37 public: |
59 StubSecureContext() {} | 38 StubSecureContext() {} |
60 ~StubSecureContext() override {} | 39 ~StubSecureContext() override {} |
61 | 40 |
62 void Decode(const std::string& encoded_message, | 41 void Decode(const std::string& encoded_message, |
63 const MessageCallback& callback) override { | 42 const MessageCallback& callback) override { |
64 NOTREACHED(); | 43 NOTREACHED(); |
65 } | 44 } |
66 | 45 |
67 void Encode(const std::string& message, | 46 void Encode(const std::string& message, |
68 const MessageCallback& callback) override { | 47 const MessageCallback& callback) override { |
69 NOTREACHED(); | 48 NOTREACHED(); |
70 } | 49 } |
71 | 50 |
72 ProtocolVersion GetProtocolVersion() const override { | 51 ProtocolVersion GetProtocolVersion() const override { |
73 NOTREACHED(); | 52 NOTREACHED(); |
74 return SecureContext::PROTOCOL_VERSION_THREE_ONE; | 53 return SecureContext::PROTOCOL_VERSION_THREE_ONE; |
75 } | 54 } |
76 | 55 |
77 private: | 56 private: |
78 DISALLOW_COPY_AND_ASSIGN(StubSecureContext); | 57 DISALLOW_COPY_AND_ASSIGN(StubSecureContext); |
79 }; | 58 }; |
80 | 59 |
81 class FakeConnectionFinder : public ConnectionFinder { | 60 class FakeConnectionFinder : public ConnectionFinder { |
82 public: | 61 public: |
83 FakeConnectionFinder() : connection_(nullptr) {} | 62 FakeConnectionFinder(const RemoteDevice& remote_device) |
| 63 : remote_device_(remote_device), connection_(nullptr) {} |
84 ~FakeConnectionFinder() override {} | 64 ~FakeConnectionFinder() override {} |
85 | 65 |
86 void OnConnectionFound() { | 66 void OnConnectionFound() { |
87 ASSERT_FALSE(connection_callback_.is_null()); | 67 ASSERT_FALSE(connection_callback_.is_null()); |
88 scoped_ptr<StubConnection> scoped_connection_(new StubConnection()); | 68 scoped_ptr<FakeConnection> scoped_connection_( |
| 69 new FakeConnection(remote_device_)); |
89 connection_ = scoped_connection_.get(); | 70 connection_ = scoped_connection_.get(); |
90 connection_callback_.Run(scoped_connection_.Pass()); | 71 connection_callback_.Run(scoped_connection_.Pass()); |
91 } | 72 } |
92 | 73 |
93 StubConnection* connection() { return connection_; } | 74 FakeConnection* connection() { return connection_; } |
94 | 75 |
95 private: | 76 private: |
96 // ConnectionFinder: | 77 // ConnectionFinder: |
97 void Find(const ConnectionCallback& connection_callback) override { | 78 void Find(const ConnectionCallback& connection_callback) override { |
98 ASSERT_TRUE(connection_callback_.is_null()); | 79 ASSERT_TRUE(connection_callback_.is_null()); |
99 connection_callback_ = connection_callback; | 80 connection_callback_ = connection_callback; |
100 } | 81 } |
101 | 82 |
102 StubConnection* connection_; | 83 const RemoteDevice remote_device_; |
| 84 |
| 85 FakeConnection* connection_; |
103 | 86 |
104 ConnectionCallback connection_callback_; | 87 ConnectionCallback connection_callback_; |
105 | 88 |
106 DISALLOW_COPY_AND_ASSIGN(FakeConnectionFinder); | 89 DISALLOW_COPY_AND_ASSIGN(FakeConnectionFinder); |
107 }; | 90 }; |
108 | 91 |
109 class FakeAuthenticator : public Authenticator { | 92 class FakeAuthenticator : public Authenticator { |
110 public: | 93 public: |
111 FakeAuthenticator() {} | 94 FakeAuthenticator() {} |
112 ~FakeAuthenticator() override {} | 95 ~FakeAuthenticator() override {} |
(...skipping 14 matching lines...) Expand all Loading... |
127 } | 110 } |
128 | 111 |
129 AuthenticationCallback callback_; | 112 AuthenticationCallback callback_; |
130 | 113 |
131 DISALLOW_COPY_AND_ASSIGN(FakeAuthenticator); | 114 DISALLOW_COPY_AND_ASSIGN(FakeAuthenticator); |
132 }; | 115 }; |
133 | 116 |
134 // Subclass of RemoteDeviceLifeCycleImpl to make it testable. | 117 // Subclass of RemoteDeviceLifeCycleImpl to make it testable. |
135 class TestableRemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycleImpl { | 118 class TestableRemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycleImpl { |
136 public: | 119 public: |
137 TestableRemoteDeviceLifeCycleImpl() | 120 TestableRemoteDeviceLifeCycleImpl(const RemoteDevice& remote_device) |
138 : RemoteDeviceLifeCycleImpl(RemoteDevice(kRemoteDeviceName, | 121 : RemoteDeviceLifeCycleImpl(remote_device, nullptr), |
139 kRemoteDevicePublicKey, | 122 remote_device_(remote_device) {} |
140 kRemoteDeviceBluetoothAddress, | |
141 kRemoteDevicePSK), | |
142 nullptr) {} | |
143 | 123 |
144 ~TestableRemoteDeviceLifeCycleImpl() override {} | 124 ~TestableRemoteDeviceLifeCycleImpl() override {} |
145 | 125 |
146 FakeConnectionFinder* connection_finder() { return connection_finder_; } | 126 FakeConnectionFinder* connection_finder() { return connection_finder_; } |
147 FakeAuthenticator* authenticator() { return authenticator_; } | 127 FakeAuthenticator* authenticator() { return authenticator_; } |
148 | 128 |
149 private: | 129 private: |
150 scoped_ptr<ConnectionFinder> CreateConnectionFinder() override { | 130 scoped_ptr<ConnectionFinder> CreateConnectionFinder() override { |
151 scoped_ptr<FakeConnectionFinder> scoped_connection_finder( | 131 scoped_ptr<FakeConnectionFinder> scoped_connection_finder( |
152 new FakeConnectionFinder()); | 132 new FakeConnectionFinder(remote_device_)); |
153 connection_finder_ = scoped_connection_finder.get(); | 133 connection_finder_ = scoped_connection_finder.get(); |
154 return scoped_connection_finder.Pass(); | 134 return scoped_connection_finder.Pass(); |
155 } | 135 } |
156 | 136 |
157 scoped_ptr<Authenticator> CreateAuthenticator() override { | 137 scoped_ptr<Authenticator> CreateAuthenticator() override { |
158 scoped_ptr<FakeAuthenticator> scoped_authenticator(new FakeAuthenticator()); | 138 scoped_ptr<FakeAuthenticator> scoped_authenticator(new FakeAuthenticator()); |
159 authenticator_ = scoped_authenticator.get(); | 139 authenticator_ = scoped_authenticator.get(); |
160 return scoped_authenticator.Pass(); | 140 return scoped_authenticator.Pass(); |
161 } | 141 } |
162 | 142 |
| 143 const RemoteDevice remote_device_; |
163 FakeConnectionFinder* connection_finder_; | 144 FakeConnectionFinder* connection_finder_; |
164 FakeAuthenticator* authenticator_; | 145 FakeAuthenticator* authenticator_; |
165 | 146 |
166 DISALLOW_COPY_AND_ASSIGN(TestableRemoteDeviceLifeCycleImpl); | 147 DISALLOW_COPY_AND_ASSIGN(TestableRemoteDeviceLifeCycleImpl); |
167 }; | 148 }; |
168 | 149 |
169 } // namespace | 150 } // namespace |
170 | 151 |
171 class ProximityAuthRemoteDeviceLifeCycleImplTest | 152 class ProximityAuthRemoteDeviceLifeCycleImplTest |
172 : public testing::Test, | 153 : public testing::Test, |
173 public RemoteDeviceLifeCycle::Observer { | 154 public RemoteDeviceLifeCycle::Observer { |
174 protected: | 155 protected: |
175 ProximityAuthRemoteDeviceLifeCycleImplTest() | 156 ProximityAuthRemoteDeviceLifeCycleImplTest() |
176 : task_runner_(new base::TestSimpleTaskRunner()), | 157 : life_cycle_(RemoteDevice(kRemoteDeviceName, |
| 158 kRemoteDevicePublicKey, |
| 159 kRemoteDeviceBluetoothAddress, |
| 160 kRemoteDevicePSK)), |
| 161 task_runner_(new base::TestSimpleTaskRunner()), |
177 thread_task_runner_handle_(task_runner_) {} | 162 thread_task_runner_handle_(task_runner_) {} |
178 | 163 |
179 ~ProximityAuthRemoteDeviceLifeCycleImplTest() override { | 164 ~ProximityAuthRemoteDeviceLifeCycleImplTest() override { |
180 life_cycle_.RemoveObserver(this); | 165 life_cycle_.RemoveObserver(this); |
181 } | 166 } |
182 | 167 |
183 void StartLifeCycle() { | 168 void StartLifeCycle() { |
184 EXPECT_EQ(RemoteDeviceLifeCycle::State::STOPPED, life_cycle_.GetState()); | 169 EXPECT_EQ(RemoteDeviceLifeCycle::State::STOPPED, life_cycle_.GetState()); |
185 life_cycle_.AddObserver(this); | 170 life_cycle_.AddObserver(this); |
186 | 171 |
187 EXPECT_CALL(*this, OnLifeCycleStateChanged( | 172 EXPECT_CALL(*this, OnLifeCycleStateChanged( |
188 RemoteDeviceLifeCycle::State::STOPPED, | 173 RemoteDeviceLifeCycle::State::STOPPED, |
189 RemoteDeviceLifeCycle::State::FINDING_CONNECTION)); | 174 RemoteDeviceLifeCycle::State::FINDING_CONNECTION)); |
190 life_cycle_.Start(); | 175 life_cycle_.Start(); |
191 task_runner_->RunUntilIdle(); | 176 task_runner_->RunUntilIdle(); |
192 Mock::VerifyAndClearExpectations(this); | 177 Mock::VerifyAndClearExpectations(this); |
193 | 178 |
194 EXPECT_EQ(RemoteDeviceLifeCycle::State::FINDING_CONNECTION, | 179 EXPECT_EQ(RemoteDeviceLifeCycle::State::FINDING_CONNECTION, |
195 life_cycle_.GetState()); | 180 life_cycle_.GetState()); |
196 } | 181 } |
197 | 182 |
198 StubConnection* OnConnectionFound() { | 183 FakeConnection* OnConnectionFound() { |
199 EXPECT_EQ(RemoteDeviceLifeCycle::State::FINDING_CONNECTION, | 184 EXPECT_EQ(RemoteDeviceLifeCycle::State::FINDING_CONNECTION, |
200 life_cycle_.GetState()); | 185 life_cycle_.GetState()); |
201 | 186 |
202 EXPECT_CALL(*this, OnLifeCycleStateChanged( | 187 EXPECT_CALL(*this, OnLifeCycleStateChanged( |
203 RemoteDeviceLifeCycle::State::FINDING_CONNECTION, | 188 RemoteDeviceLifeCycle::State::FINDING_CONNECTION, |
204 RemoteDeviceLifeCycle::State::AUTHENTICATING)); | 189 RemoteDeviceLifeCycle::State::AUTHENTICATING)); |
205 life_cycle_.connection_finder()->OnConnectionFound(); | 190 life_cycle_.connection_finder()->OnConnectionFound(); |
206 task_runner_->RunUntilIdle(); | 191 task_runner_->RunUntilIdle(); |
207 Mock::VerifyAndClearExpectations(this); | 192 Mock::VerifyAndClearExpectations(this); |
208 | 193 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 | 286 |
302 // Authentication succeeds on second pass. | 287 // Authentication succeeds on second pass. |
303 Connection* connection = OnConnectionFound(); | 288 Connection* connection = OnConnectionFound(); |
304 Authenticate(Authenticator::Result::SUCCESS); | 289 Authenticate(Authenticator::Result::SUCCESS); |
305 EXPECT_TRUE(life_cycle_.GetMessenger()); | 290 EXPECT_TRUE(life_cycle_.GetMessenger()); |
306 EXPECT_CALL(*this, OnLifeCycleStateChanged(_, _)); | 291 EXPECT_CALL(*this, OnLifeCycleStateChanged(_, _)); |
307 connection->Disconnect(); | 292 connection->Disconnect(); |
308 } | 293 } |
309 | 294 |
310 } // namespace proximity_auth | 295 } // namespace proximity_auth |
OLD | NEW |