| 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/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/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 13 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 14 #include "base/timer/mock_timer.h" | 15 #include "base/timer/mock_timer.h" |
| 15 #include "components/proximity_auth/connection.h" | 16 #include "components/proximity_auth/connection.h" |
| 16 #include "components/proximity_auth/cryptauth/fake_secure_message_delegate.h" | 17 #include "components/proximity_auth/cryptauth/fake_secure_message_delegate.h" |
| 17 #include "components/proximity_auth/device_to_device_responder_operations.h" | 18 #include "components/proximity_auth/device_to_device_responder_operations.h" |
| 18 #include "components/proximity_auth/proximity_auth_test_util.h" | 19 #include "components/proximity_auth/proximity_auth_test_util.h" |
| 19 #include "components/proximity_auth/secure_context.h" | 20 #include "components/proximity_auth/secure_context.h" |
| 20 #include "components/proximity_auth/wire_message.h" | 21 #include "components/proximity_auth/wire_message.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 const ScopedVector<WireMessage>& message_buffer() { return message_buffer_; } | 78 const ScopedVector<WireMessage>& message_buffer() { return message_buffer_; } |
| 78 | 79 |
| 79 void set_connection_blocked(bool connection_blocked) { | 80 void set_connection_blocked(bool connection_blocked) { |
| 80 connection_blocked_ = connection_blocked; | 81 connection_blocked_ = connection_blocked; |
| 81 } | 82 } |
| 82 | 83 |
| 83 bool connection_blocked() { return connection_blocked_; } | 84 bool connection_blocked() { return connection_blocked_; } |
| 84 | 85 |
| 85 protected: | 86 protected: |
| 86 // Connection: | 87 // Connection: |
| 87 void SendMessageImpl(scoped_ptr<WireMessage> message) override { | 88 void SendMessageImpl(std::unique_ptr<WireMessage> message) override { |
| 88 const WireMessage& message_alias = *message; | 89 const WireMessage& message_alias = *message; |
| 89 message_buffer_.push_back(std::move(message)); | 90 message_buffer_.push_back(std::move(message)); |
| 90 OnDidSendMessage(message_alias, !connection_blocked_); | 91 OnDidSendMessage(message_alias, !connection_blocked_); |
| 91 } | 92 } |
| 92 | 93 |
| 93 private: | 94 private: |
| 94 ScopedVector<WireMessage> message_buffer_; | 95 ScopedVector<WireMessage> message_buffer_; |
| 95 | 96 |
| 96 bool connection_blocked_; | 97 bool connection_blocked_; |
| 97 | 98 |
| 98 DISALLOW_COPY_AND_ASSIGN(FakeConnection); | 99 DISALLOW_COPY_AND_ASSIGN(FakeConnection); |
| 99 }; | 100 }; |
| 100 | 101 |
| 101 // Harness for testing DeviceToDeviceAuthenticator. | 102 // Harness for testing DeviceToDeviceAuthenticator. |
| 102 class DeviceToDeviceAuthenticatorForTest : public DeviceToDeviceAuthenticator { | 103 class DeviceToDeviceAuthenticatorForTest : public DeviceToDeviceAuthenticator { |
| 103 public: | 104 public: |
| 104 DeviceToDeviceAuthenticatorForTest( | 105 DeviceToDeviceAuthenticatorForTest( |
| 105 Connection* connection, | 106 Connection* connection, |
| 106 scoped_ptr<SecureMessageDelegate> secure_message_delegate) | 107 std::unique_ptr<SecureMessageDelegate> secure_message_delegate) |
| 107 : DeviceToDeviceAuthenticator(connection, | 108 : DeviceToDeviceAuthenticator(connection, |
| 108 kAccountId, | 109 kAccountId, |
| 109 std::move(secure_message_delegate)), | 110 std::move(secure_message_delegate)), |
| 110 timer_(nullptr) {} | 111 timer_(nullptr) {} |
| 111 ~DeviceToDeviceAuthenticatorForTest() override {} | 112 ~DeviceToDeviceAuthenticatorForTest() override {} |
| 112 | 113 |
| 113 base::MockTimer* timer() { return timer_; } | 114 base::MockTimer* timer() { return timer_; } |
| 114 | 115 |
| 115 private: | 116 private: |
| 116 // DeviceToDeviceAuthenticator: | 117 // DeviceToDeviceAuthenticator: |
| 117 scoped_ptr<base::Timer> CreateTimer() override { | 118 std::unique_ptr<base::Timer> CreateTimer() override { |
| 118 bool retain_user_task = false; | 119 bool retain_user_task = false; |
| 119 bool is_repeating = false; | 120 bool is_repeating = false; |
| 120 | 121 |
| 121 scoped_ptr<base::MockTimer> timer( | 122 std::unique_ptr<base::MockTimer> timer( |
| 122 new base::MockTimer(retain_user_task, is_repeating)); | 123 new base::MockTimer(retain_user_task, is_repeating)); |
| 123 | 124 |
| 124 timer_ = timer.get(); | 125 timer_ = timer.get(); |
| 125 return std::move(timer); | 126 return std::move(timer); |
| 126 } | 127 } |
| 127 | 128 |
| 128 // This instance is owned by the super class. | 129 // This instance is owned by the super class. |
| 129 base::MockTimer* timer_; | 130 base::MockTimer* timer_; |
| 130 | 131 |
| 131 DISALLOW_COPY_AND_ASSIGN(DeviceToDeviceAuthenticatorForTest); | 132 DISALLOW_COPY_AND_ASSIGN(DeviceToDeviceAuthenticatorForTest); |
| 132 }; | 133 }; |
| 133 | 134 |
| 134 } // namespace | 135 } // namespace |
| 135 | 136 |
| 136 class ProximityAuthDeviceToDeviceAuthenticatorTest : public testing::Test { | 137 class ProximityAuthDeviceToDeviceAuthenticatorTest : public testing::Test { |
| 137 public: | 138 public: |
| 138 ProximityAuthDeviceToDeviceAuthenticatorTest() | 139 ProximityAuthDeviceToDeviceAuthenticatorTest() |
| 139 : remote_device_(CreateClassicRemoteDeviceForTest()), | 140 : remote_device_(CreateClassicRemoteDeviceForTest()), |
| 140 connection_(remote_device_), | 141 connection_(remote_device_), |
| 141 secure_message_delegate_(new FakeSecureMessageDelegate), | 142 secure_message_delegate_(new FakeSecureMessageDelegate), |
| 142 authenticator_(&connection_, | 143 authenticator_(&connection_, |
| 143 make_scoped_ptr(secure_message_delegate_)) {} | 144 base::WrapUnique(secure_message_delegate_)) {} |
| 144 ~ProximityAuthDeviceToDeviceAuthenticatorTest() override {} | 145 ~ProximityAuthDeviceToDeviceAuthenticatorTest() override {} |
| 145 | 146 |
| 146 void SetUp() override { | 147 void SetUp() override { |
| 147 // Set up the session asymmetric keys for both the local and remote devices. | 148 // Set up the session asymmetric keys for both the local and remote devices. |
| 148 ASSERT_TRUE( | 149 ASSERT_TRUE( |
| 149 base::Base64UrlDecode(kInitiatorSessionPublicKeyBase64, | 150 base::Base64UrlDecode(kInitiatorSessionPublicKeyBase64, |
| 150 base::Base64UrlDecodePolicy::REQUIRE_PADDING, | 151 base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 151 &local_session_public_key_)); | 152 &local_session_public_key_)); |
| 152 ASSERT_TRUE( | 153 ASSERT_TRUE( |
| 153 base::Base64UrlDecode(kResponderSessionPublicKeyBase64, | 154 base::Base64UrlDecode(kResponderSessionPublicKeyBase64, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 base::Bind(&SaveStringResult, &responder_auth_message)); | 207 base::Bind(&SaveStringResult, &responder_auth_message)); |
| 207 EXPECT_FALSE(responder_auth_message.empty()); | 208 EXPECT_FALSE(responder_auth_message.empty()); |
| 208 | 209 |
| 209 WireMessage wire_message(responder_auth_message); | 210 WireMessage wire_message(responder_auth_message); |
| 210 connection_.OnBytesReceived(wire_message.Serialize()); | 211 connection_.OnBytesReceived(wire_message.Serialize()); |
| 211 | 212 |
| 212 return responder_auth_message; | 213 return responder_auth_message; |
| 213 } | 214 } |
| 214 | 215 |
| 215 void OnAuthenticationResult(Authenticator::Result result, | 216 void OnAuthenticationResult(Authenticator::Result result, |
| 216 scoped_ptr<SecureContext> secure_context) { | 217 std::unique_ptr<SecureContext> secure_context) { |
| 217 secure_context_ = std::move(secure_context); | 218 secure_context_ = std::move(secure_context); |
| 218 OnAuthenticationResultProxy(result); | 219 OnAuthenticationResultProxy(result); |
| 219 } | 220 } |
| 220 | 221 |
| 221 MOCK_METHOD1(OnAuthenticationResultProxy, void(Authenticator::Result result)); | 222 MOCK_METHOD1(OnAuthenticationResultProxy, void(Authenticator::Result result)); |
| 222 | 223 |
| 223 // Contains information about the remote device. | 224 // Contains information about the remote device. |
| 224 const RemoteDevice remote_device_; | 225 const RemoteDevice remote_device_; |
| 225 | 226 |
| 226 // Simulates the connection to the remote device. | 227 // Simulates the connection to the remote device. |
| 227 FakeConnection connection_; | 228 FakeConnection connection_; |
| 228 | 229 |
| 229 // The SecureMessageDelegate used by the authenticator. | 230 // The SecureMessageDelegate used by the authenticator. |
| 230 // Owned by |authenticator_|. | 231 // Owned by |authenticator_|. |
| 231 FakeSecureMessageDelegate* secure_message_delegate_; | 232 FakeSecureMessageDelegate* secure_message_delegate_; |
| 232 | 233 |
| 233 // The DeviceToDeviceAuthenticator under test. | 234 // The DeviceToDeviceAuthenticator under test. |
| 234 DeviceToDeviceAuthenticatorForTest authenticator_; | 235 DeviceToDeviceAuthenticatorForTest authenticator_; |
| 235 | 236 |
| 236 // The session keys in play during authentication. | 237 // The session keys in play during authentication. |
| 237 std::string local_session_public_key_; | 238 std::string local_session_public_key_; |
| 238 std::string remote_session_public_key_; | 239 std::string remote_session_public_key_; |
| 239 std::string remote_session_private_key_; | 240 std::string remote_session_private_key_; |
| 240 std::string session_symmetric_key_; | 241 std::string session_symmetric_key_; |
| 241 | 242 |
| 242 // Stores the SecureContext returned after authentication succeeds. | 243 // Stores the SecureContext returned after authentication succeeds. |
| 243 scoped_ptr<SecureContext> secure_context_; | 244 std::unique_ptr<SecureContext> secure_context_; |
| 244 | 245 |
| 245 DISALLOW_COPY_AND_ASSIGN(ProximityAuthDeviceToDeviceAuthenticatorTest); | 246 DISALLOW_COPY_AND_ASSIGN(ProximityAuthDeviceToDeviceAuthenticatorTest); |
| 246 }; | 247 }; |
| 247 | 248 |
| 248 TEST_F(ProximityAuthDeviceToDeviceAuthenticatorTest, AuthenticateSucceeds) { | 249 TEST_F(ProximityAuthDeviceToDeviceAuthenticatorTest, AuthenticateSucceeds) { |
| 249 // Starts the authentication protocol and grab [Hello] message. | 250 // Starts the authentication protocol and grab [Hello] message. |
| 250 std::string hello_message = BeginAuthentication(); | 251 std::string hello_message = BeginAuthentication(); |
| 251 | 252 |
| 252 // Simulate receiving a valid [Responder Auth] from the remote device. | 253 // Simulate receiving a valid [Responder Auth] from the remote device. |
| 253 EXPECT_CALL(*this, | 254 EXPECT_CALL(*this, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 SendMessagesAfterAuthenticationSuccess) { | 334 SendMessagesAfterAuthenticationSuccess) { |
| 334 std::string hello_message = BeginAuthentication(); | 335 std::string hello_message = BeginAuthentication(); |
| 335 EXPECT_CALL(*this, | 336 EXPECT_CALL(*this, |
| 336 OnAuthenticationResultProxy(Authenticator::Result::SUCCESS)); | 337 OnAuthenticationResultProxy(Authenticator::Result::SUCCESS)); |
| 337 SimulateResponderAuth(hello_message); | 338 SimulateResponderAuth(hello_message); |
| 338 | 339 |
| 339 // Test that the authenticator is properly cleaned up after authentication | 340 // Test that the authenticator is properly cleaned up after authentication |
| 340 // completes. | 341 // completes. |
| 341 WireMessage wire_message(base::RandBytesAsString(300u)); | 342 WireMessage wire_message(base::RandBytesAsString(300u)); |
| 342 connection_.SendMessage( | 343 connection_.SendMessage( |
| 343 make_scoped_ptr(new WireMessage(base::RandBytesAsString(300u)))); | 344 base::WrapUnique(new WireMessage(base::RandBytesAsString(300u)))); |
| 344 connection_.OnBytesReceived(wire_message.Serialize()); | 345 connection_.OnBytesReceived(wire_message.Serialize()); |
| 345 connection_.SendMessage( | 346 connection_.SendMessage( |
| 346 make_scoped_ptr(new WireMessage(base::RandBytesAsString(300u)))); | 347 base::WrapUnique(new WireMessage(base::RandBytesAsString(300u)))); |
| 347 connection_.OnBytesReceived(wire_message.Serialize()); | 348 connection_.OnBytesReceived(wire_message.Serialize()); |
| 348 } | 349 } |
| 349 | 350 |
| 350 } // namespace proximity_auth | 351 } // namespace proximity_auth |
| OLD | NEW |