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

Side by Side Diff: components/proximity_auth/device_to_device_authenticator_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/device_to_device_authenticator.h" 5 #include "components/proximity_auth/device_to_device_authenticator.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/base64url.h" 9 #include "base/base64url.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/memory/scoped_vector.h" 13 #include "base/memory/scoped_vector.h"
14 #include "base/rand_util.h" 14 #include "base/rand_util.h"
15 #include "base/timer/mock_timer.h" 15 #include "base/timer/mock_timer.h"
16 #include "components/cryptauth/connection.h"
17 #include "components/cryptauth/cryptauth_test_util.h"
16 #include "components/cryptauth/fake_secure_message_delegate.h" 18 #include "components/cryptauth/fake_secure_message_delegate.h"
17 #include "components/proximity_auth/connection.h" 19 #include "components/cryptauth/wire_message.h"
18 #include "components/proximity_auth/device_to_device_responder_operations.h" 20 #include "components/proximity_auth/device_to_device_responder_operations.h"
19 #include "components/proximity_auth/proximity_auth_test_util.h"
20 #include "components/proximity_auth/secure_context.h" 21 #include "components/proximity_auth/secure_context.h"
21 #include "components/proximity_auth/wire_message.h"
22 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
24 24
25 namespace proximity_auth { 25 namespace proximity_auth {
26 26
27 namespace { 27 namespace {
28 28
29 // The account id of the user. 29 // The account id of the user.
30 const char kAccountId[] = "example@gmail.com"; 30 const char kAccountId[] = "example@gmail.com";
31 31
(...skipping 22 matching lines...) Expand all
54 // Callback saving the result of ValidateHelloMessage(). 54 // Callback saving the result of ValidateHelloMessage().
55 void SaveValidateHelloMessageResult(bool* validated_out, 55 void SaveValidateHelloMessageResult(bool* validated_out,
56 std::string* public_key_out, 56 std::string* public_key_out,
57 bool validated, 57 bool validated,
58 const std::string& public_key) { 58 const std::string& public_key) {
59 *validated_out = validated; 59 *validated_out = validated;
60 *public_key_out = public_key; 60 *public_key_out = public_key;
61 } 61 }
62 62
63 // Connection implementation for testing. 63 // Connection implementation for testing.
64 class FakeConnection : public Connection { 64 class FakeConnection : public cryptauth::Connection {
65 public: 65 public:
66 FakeConnection(const cryptauth::RemoteDevice& remote_device) 66 FakeConnection(const cryptauth::RemoteDevice& remote_device)
67 : Connection(remote_device), connection_blocked_(false) {} 67 : cryptauth::Connection(remote_device), connection_blocked_(false) {}
68 ~FakeConnection() override {} 68 ~FakeConnection() override {}
69 69
70 // Connection: 70 // Connection:
71 void Connect() override { SetStatus(Connection::Status::CONNECTED); } 71 void Connect() override {
72 void Disconnect() override { SetStatus(Connection::Status::DISCONNECTED); } 72 SetStatus(cryptauth::Connection::Status::CONNECTED);
73 }
74 void Disconnect() override {
75 SetStatus(cryptauth::Connection::Status::DISCONNECTED);
76 }
73 77
74 using Connection::OnBytesReceived; 78 using cryptauth::Connection::OnBytesReceived;
75 79
76 void ClearMessageBuffer() { message_buffer_.clear(); } 80 void ClearMessageBuffer() { message_buffer_.clear(); }
77 81
78 const ScopedVector<WireMessage>& message_buffer() { return message_buffer_; } 82 const ScopedVector<cryptauth::WireMessage>& message_buffer() {
83 return message_buffer_;
84 }
79 85
80 void set_connection_blocked(bool connection_blocked) { 86 void set_connection_blocked(bool connection_blocked) {
81 connection_blocked_ = connection_blocked; 87 connection_blocked_ = connection_blocked;
82 } 88 }
83 89
84 bool connection_blocked() { return connection_blocked_; } 90 bool connection_blocked() { return connection_blocked_; }
85 91
86 protected: 92 protected:
87 // Connection: 93 // cryptauth::Connection:
88 void SendMessageImpl(std::unique_ptr<WireMessage> message) override { 94 void SendMessageImpl(
89 const WireMessage& message_alias = *message; 95 std::unique_ptr<cryptauth::WireMessage> message) override {
96 const cryptauth::WireMessage& message_alias = *message;
90 message_buffer_.push_back(std::move(message)); 97 message_buffer_.push_back(std::move(message));
91 OnDidSendMessage(message_alias, !connection_blocked_); 98 OnDidSendMessage(message_alias, !connection_blocked_);
92 } 99 }
93 100
94 private: 101 private:
95 ScopedVector<WireMessage> message_buffer_; 102 ScopedVector<cryptauth::WireMessage> message_buffer_;
96 103
97 bool connection_blocked_; 104 bool connection_blocked_;
98 105
99 DISALLOW_COPY_AND_ASSIGN(FakeConnection); 106 DISALLOW_COPY_AND_ASSIGN(FakeConnection);
100 }; 107 };
101 108
102 // Harness for testing DeviceToDeviceAuthenticator. 109 // Harness for testing DeviceToDeviceAuthenticator.
103 class DeviceToDeviceAuthenticatorForTest : public DeviceToDeviceAuthenticator { 110 class DeviceToDeviceAuthenticatorForTest : public DeviceToDeviceAuthenticator {
104 public: 111 public:
105 DeviceToDeviceAuthenticatorForTest( 112 DeviceToDeviceAuthenticatorForTest(
106 Connection* connection, 113 cryptauth::Connection* connection,
107 std::unique_ptr<cryptauth::SecureMessageDelegate> secure_message_delegate) 114 std::unique_ptr<cryptauth::SecureMessageDelegate> secure_message_delegate)
108 : DeviceToDeviceAuthenticator(connection, 115 : DeviceToDeviceAuthenticator(connection,
109 kAccountId, 116 kAccountId,
110 std::move(secure_message_delegate)), 117 std::move(secure_message_delegate)),
111 timer_(nullptr) {} 118 timer_(nullptr) {}
112 ~DeviceToDeviceAuthenticatorForTest() override {} 119 ~DeviceToDeviceAuthenticatorForTest() override {}
113 120
114 base::MockTimer* timer() { return timer_; } 121 base::MockTimer* timer() { return timer_; }
115 122
116 private: 123 private:
(...skipping 13 matching lines...) Expand all
130 base::MockTimer* timer_; 137 base::MockTimer* timer_;
131 138
132 DISALLOW_COPY_AND_ASSIGN(DeviceToDeviceAuthenticatorForTest); 139 DISALLOW_COPY_AND_ASSIGN(DeviceToDeviceAuthenticatorForTest);
133 }; 140 };
134 141
135 } // namespace 142 } // namespace
136 143
137 class ProximityAuthDeviceToDeviceAuthenticatorTest : public testing::Test { 144 class ProximityAuthDeviceToDeviceAuthenticatorTest : public testing::Test {
138 public: 145 public:
139 ProximityAuthDeviceToDeviceAuthenticatorTest() 146 ProximityAuthDeviceToDeviceAuthenticatorTest()
140 : remote_device_(CreateClassicRemoteDeviceForTest()), 147 : remote_device_(cryptauth::CreateClassicRemoteDeviceForTest()),
141 connection_(remote_device_), 148 connection_(remote_device_),
142 secure_message_delegate_(new cryptauth::FakeSecureMessageDelegate), 149 secure_message_delegate_(new cryptauth::FakeSecureMessageDelegate),
143 authenticator_(&connection_, 150 authenticator_(&connection_,
144 base::WrapUnique(secure_message_delegate_)) {} 151 base::WrapUnique(secure_message_delegate_)) {}
145 ~ProximityAuthDeviceToDeviceAuthenticatorTest() override {} 152 ~ProximityAuthDeviceToDeviceAuthenticatorTest() override {}
146 153
147 void SetUp() override { 154 void SetUp() override {
148 // Set up the session asymmetric keys for both the local and remote devices. 155 // Set up the session asymmetric keys for both the local and remote devices.
149 ASSERT_TRUE( 156 ASSERT_TRUE(
150 base::Base64UrlDecode(kInitiatorSessionPublicKeyBase64, 157 base::Base64UrlDecode(kInitiatorSessionPublicKeyBase64,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 EXPECT_TRUE(validated); 197 EXPECT_TRUE(validated);
191 EXPECT_EQ(local_session_public_key_, local_session_public_key); 198 EXPECT_EQ(local_session_public_key_, local_session_public_key);
192 199
193 return hello_message; 200 return hello_message;
194 } 201 }
195 202
196 // Simulate receiving a valid [Responder Auth] message from the remote device. 203 // Simulate receiving a valid [Responder Auth] message from the remote device.
197 std::string SimulateResponderAuth(const std::string& hello_message) { 204 std::string SimulateResponderAuth(const std::string& hello_message) {
198 std::string remote_device_private_key = 205 std::string remote_device_private_key =
199 secure_message_delegate_->GetPrivateKeyForPublicKey( 206 secure_message_delegate_->GetPrivateKeyForPublicKey(
200 kTestRemoteDevicePublicKey); 207 cryptauth::kTestRemoteDevicePublicKey);
201 208
202 std::string responder_auth_message; 209 std::string responder_auth_message;
203 DeviceToDeviceResponderOperations::CreateResponderAuthMessage( 210 DeviceToDeviceResponderOperations::CreateResponderAuthMessage(
204 hello_message, remote_session_public_key_, remote_session_private_key_, 211 hello_message, remote_session_public_key_, remote_session_private_key_,
205 remote_device_private_key, remote_device_.persistent_symmetric_key, 212 remote_device_private_key, remote_device_.persistent_symmetric_key,
206 secure_message_delegate_, 213 secure_message_delegate_,
207 base::Bind(&SaveStringResult, &responder_auth_message)); 214 base::Bind(&SaveStringResult, &responder_auth_message));
208 EXPECT_FALSE(responder_auth_message.empty()); 215 EXPECT_FALSE(responder_auth_message.empty());
209 216
210 WireMessage wire_message(responder_auth_message); 217 cryptauth::WireMessage wire_message(responder_auth_message);
211 connection_.OnBytesReceived(wire_message.Serialize()); 218 connection_.OnBytesReceived(wire_message.Serialize());
212 219
213 return responder_auth_message; 220 return responder_auth_message;
214 } 221 }
215 222
216 void OnAuthenticationResult(Authenticator::Result result, 223 void OnAuthenticationResult(Authenticator::Result result,
217 std::unique_ptr<SecureContext> secure_context) { 224 std::unique_ptr<SecureContext> secure_context) {
218 secure_context_ = std::move(secure_context); 225 secure_context_ = std::move(secure_context);
219 OnAuthenticationResultProxy(result); 226 OnAuthenticationResultProxy(result);
220 } 227 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 secure_message_delegate_, 274 secure_message_delegate_,
268 base::Bind(&SaveBooleanResult, &initiator_auth_validated)); 275 base::Bind(&SaveBooleanResult, &initiator_auth_validated));
269 ASSERT_TRUE(initiator_auth_validated); 276 ASSERT_TRUE(initiator_auth_validated);
270 } 277 }
271 278
272 TEST_F(ProximityAuthDeviceToDeviceAuthenticatorTest, ResponderRejectsHello) { 279 TEST_F(ProximityAuthDeviceToDeviceAuthenticatorTest, ResponderRejectsHello) {
273 std::string hello_message = BeginAuthentication(); 280 std::string hello_message = BeginAuthentication();
274 281
275 // If the responder could not validate the [Hello message], it essentially 282 // If the responder could not validate the [Hello message], it essentially
276 // sends random bytes back for privacy reasons. 283 // sends random bytes back for privacy reasons.
277 WireMessage wire_message(base::RandBytesAsString(300u)); 284 cryptauth::WireMessage wire_message(base::RandBytesAsString(300u));
278 EXPECT_CALL(*this, 285 EXPECT_CALL(*this,
279 OnAuthenticationResultProxy(Authenticator::Result::FAILURE)); 286 OnAuthenticationResultProxy(Authenticator::Result::FAILURE));
280 connection_.OnBytesReceived(wire_message.Serialize()); 287 connection_.OnBytesReceived(wire_message.Serialize());
281 EXPECT_FALSE(secure_context_); 288 EXPECT_FALSE(secure_context_);
282 } 289 }
283 290
284 TEST_F(ProximityAuthDeviceToDeviceAuthenticatorTest, ResponderAuthTimesOut) { 291 TEST_F(ProximityAuthDeviceToDeviceAuthenticatorTest, ResponderAuthTimesOut) {
285 // Starts the authentication protocol and grab [Hello] message. 292 // Starts the authentication protocol and grab [Hello] message.
286 std::string hello_message = BeginAuthentication(); 293 std::string hello_message = BeginAuthentication();
287 ASSERT_TRUE(authenticator_.timer()); 294 ASSERT_TRUE(authenticator_.timer());
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 339
333 TEST_F(ProximityAuthDeviceToDeviceAuthenticatorTest, 340 TEST_F(ProximityAuthDeviceToDeviceAuthenticatorTest,
334 SendMessagesAfterAuthenticationSuccess) { 341 SendMessagesAfterAuthenticationSuccess) {
335 std::string hello_message = BeginAuthentication(); 342 std::string hello_message = BeginAuthentication();
336 EXPECT_CALL(*this, 343 EXPECT_CALL(*this,
337 OnAuthenticationResultProxy(Authenticator::Result::SUCCESS)); 344 OnAuthenticationResultProxy(Authenticator::Result::SUCCESS));
338 SimulateResponderAuth(hello_message); 345 SimulateResponderAuth(hello_message);
339 346
340 // Test that the authenticator is properly cleaned up after authentication 347 // Test that the authenticator is properly cleaned up after authentication
341 // completes. 348 // completes.
342 WireMessage wire_message(base::RandBytesAsString(300u)); 349 cryptauth::WireMessage wire_message(base::RandBytesAsString(300u));
343 connection_.SendMessage( 350 connection_.SendMessage(
344 base::MakeUnique<WireMessage>(base::RandBytesAsString(300u))); 351 base::MakeUnique<cryptauth::WireMessage>(base::RandBytesAsString(300u)));
345 connection_.OnBytesReceived(wire_message.Serialize()); 352 connection_.OnBytesReceived(wire_message.Serialize());
346 connection_.SendMessage( 353 connection_.SendMessage(
347 base::MakeUnique<WireMessage>(base::RandBytesAsString(300u))); 354 base::MakeUnique<cryptauth::WireMessage>(base::RandBytesAsString(300u)));
348 connection_.OnBytesReceived(wire_message.Serialize()); 355 connection_.OnBytesReceived(wire_message.Serialize());
349 } 356 }
350 357
351 } // namespace proximity_auth 358 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698