| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/cryptauth/secure_channel.h" | 5 #include "components/cryptauth/secure_channel.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "components/cryptauth/fake_authenticator.h" | 12 #include "components/cryptauth/fake_authenticator.h" |
| 13 #include "components/cryptauth/fake_connection.h" | 13 #include "components/cryptauth/fake_connection.h" |
| 14 #include "components/cryptauth/fake_cryptauth_service.h" |
| 14 #include "components/cryptauth/fake_secure_context.h" | 15 #include "components/cryptauth/fake_secure_context.h" |
| 15 #include "components/cryptauth/fake_secure_message_delegate.h" | 16 #include "components/cryptauth/fake_secure_message_delegate.h" |
| 16 #include "components/cryptauth/remote_device_test_util.h" | 17 #include "components/cryptauth/remote_device_test_util.h" |
| 17 #include "components/cryptauth/wire_message.h" | 18 #include "components/cryptauth/wire_message.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 namespace cryptauth { | 21 namespace cryptauth { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 const std::string test_user_id = "testUserId"; | 25 const std::string test_user_id = "testUserId"; |
| 25 | 26 |
| 26 class TestDelegate : public SecureChannel::Delegate { | |
| 27 public: | |
| 28 TestDelegate(std::unique_ptr<SecureMessageDelegate> secure_message_delegate) | |
| 29 : secure_message_delegate_(std::move(secure_message_delegate)) {} | |
| 30 ~TestDelegate() override {} | |
| 31 | |
| 32 std::unique_ptr<SecureMessageDelegate> CreateSecureMessageDelegate() | |
| 33 override { | |
| 34 return std::move(secure_message_delegate_); | |
| 35 } | |
| 36 | |
| 37 private: | |
| 38 std::unique_ptr<SecureMessageDelegate> secure_message_delegate_; | |
| 39 }; | |
| 40 | |
| 41 struct SecureChannelStatusChange { | 27 struct SecureChannelStatusChange { |
| 42 SecureChannelStatusChange( | 28 SecureChannelStatusChange( |
| 43 const SecureChannel::Status& old_status, | 29 const SecureChannel::Status& old_status, |
| 44 const SecureChannel::Status& new_status) | 30 const SecureChannel::Status& new_status) |
| 45 : old_status(old_status), new_status(new_status) {} | 31 : old_status(old_status), new_status(new_status) {} |
| 46 | 32 |
| 47 SecureChannel::Status old_status; | 33 SecureChannel::Status old_status; |
| 48 SecureChannel::Status new_status; | 34 SecureChannel::Status new_status; |
| 49 }; | 35 }; |
| 50 | 36 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 101 |
| 116 RemoteDevice CreateTestRemoteDevice() { | 102 RemoteDevice CreateTestRemoteDevice() { |
| 117 RemoteDevice remote_device = GenerateTestRemoteDevices(1)[0]; | 103 RemoteDevice remote_device = GenerateTestRemoteDevices(1)[0]; |
| 118 remote_device.user_id = test_user_id; | 104 remote_device.user_id = test_user_id; |
| 119 return remote_device; | 105 return remote_device; |
| 120 } | 106 } |
| 121 | 107 |
| 122 class TestSecureChannel : public SecureChannel { | 108 class TestSecureChannel : public SecureChannel { |
| 123 public: | 109 public: |
| 124 TestSecureChannel(std::unique_ptr<Connection> connection, | 110 TestSecureChannel(std::unique_ptr<Connection> connection, |
| 125 std::unique_ptr<Delegate> delegate) | 111 CryptAuthService* cryptauth_service) |
| 126 : SecureChannel(std::move(connection), std::move(delegate)) {} | 112 : SecureChannel(std::move(connection), cryptauth_service) {} |
| 127 }; | 113 }; |
| 128 | 114 |
| 129 } // namespace | 115 } // namespace |
| 130 | 116 |
| 131 class CryptAuthSecureChannelTest : public testing::Test { | 117 class CryptAuthSecureChannelTest : public testing::Test { |
| 132 protected: | 118 protected: |
| 133 CryptAuthSecureChannelTest() | 119 CryptAuthSecureChannelTest() |
| 134 : test_device_(CreateTestRemoteDevice()), | 120 : test_device_(CreateTestRemoteDevice()), |
| 135 weak_ptr_factory_(this) {} | 121 weak_ptr_factory_(this) {} |
| 136 | 122 |
| 137 void SetUp() override { | 123 void SetUp() override { |
| 138 test_authenticator_factory_ = base::MakeUnique<TestAuthenticatorFactory>(); | 124 test_authenticator_factory_ = base::MakeUnique<TestAuthenticatorFactory>(); |
| 139 DeviceToDeviceAuthenticator::Factory::SetInstanceForTesting( | 125 DeviceToDeviceAuthenticator::Factory::SetInstanceForTesting( |
| 140 test_authenticator_factory_.get()); | 126 test_authenticator_factory_.get()); |
| 141 | 127 |
| 142 fake_secure_context_ = nullptr; | 128 fake_secure_context_ = nullptr; |
| 143 | 129 |
| 144 fake_secure_message_delegate_ = new FakeSecureMessageDelegate(); | 130 fake_cryptauth_service_ = base::MakeUnique<FakeCryptAuthService>(); |
| 145 | |
| 146 test_delegate_ = | |
| 147 new TestDelegate(base::WrapUnique(fake_secure_message_delegate_)); | |
| 148 | 131 |
| 149 fake_connection_ = | 132 fake_connection_ = |
| 150 new FakeConnection(test_device_, /* should_auto_connect */ false); | 133 new FakeConnection(test_device_, /* should_auto_connect */ false); |
| 151 | 134 |
| 152 EXPECT_FALSE(fake_connection_->observers().size()); | 135 EXPECT_FALSE(fake_connection_->observers().size()); |
| 153 secure_channel_ = base::MakeUnique<TestSecureChannel>( | 136 secure_channel_ = base::MakeUnique<TestSecureChannel>( |
| 154 base::WrapUnique(fake_connection_), base::WrapUnique(test_delegate_)); | 137 base::WrapUnique(fake_connection_), fake_cryptauth_service_.get()); |
| 155 EXPECT_EQ(static_cast<size_t>(1), fake_connection_->observers().size()); | 138 EXPECT_EQ(static_cast<size_t>(1), fake_connection_->observers().size()); |
| 156 EXPECT_EQ(secure_channel_.get(), fake_connection_->observers()[0]); | 139 EXPECT_EQ(secure_channel_.get(), fake_connection_->observers()[0]); |
| 157 | 140 |
| 158 test_observer_ = base::MakeUnique<TestObserver>(secure_channel_.get()); | 141 test_observer_ = base::MakeUnique<TestObserver>(secure_channel_.get()); |
| 159 secure_channel_->AddObserver(test_observer_.get()); | 142 secure_channel_->AddObserver(test_observer_.get()); |
| 160 } | 143 } |
| 161 | 144 |
| 162 void TearDown() override { | 145 void TearDown() override { |
| 163 // All state changes should have already been verified. This ensures that | 146 // All state changes should have already been verified. This ensures that |
| 164 // no test has missed one. | 147 // no test has missed one. |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 WireMessage* wire_message, | 276 WireMessage* wire_message, |
| 294 const std::string& expected_feature, | 277 const std::string& expected_feature, |
| 295 const std::string& expected_payload) { | 278 const std::string& expected_payload) { |
| 296 EXPECT_EQ(expected_feature, wire_message->feature()); | 279 EXPECT_EQ(expected_feature, wire_message->feature()); |
| 297 EXPECT_EQ(expected_payload, wire_message->payload()); | 280 EXPECT_EQ(expected_payload, wire_message->payload()); |
| 298 } | 281 } |
| 299 | 282 |
| 300 // Owned by secure_channel_. | 283 // Owned by secure_channel_. |
| 301 FakeConnection* fake_connection_; | 284 FakeConnection* fake_connection_; |
| 302 | 285 |
| 303 // Owned by secure_chanel_. | 286 std::unique_ptr<FakeCryptAuthService> fake_cryptauth_service_; |
| 304 TestDelegate* test_delegate_; | |
| 305 | |
| 306 // Owned by test_delegate_. | |
| 307 FakeSecureMessageDelegate* fake_secure_message_delegate_; | |
| 308 | 287 |
| 309 // Owned by secure_channel_ once authentication has completed successfully. | 288 // Owned by secure_channel_ once authentication has completed successfully. |
| 310 FakeSecureContext* fake_secure_context_; | 289 FakeSecureContext* fake_secure_context_; |
| 311 | 290 |
| 312 std::vector<SecureChannelStatusChange> verified_status_changes_; | 291 std::vector<SecureChannelStatusChange> verified_status_changes_; |
| 313 | 292 |
| 314 std::vector<ReceivedMessage> verified_received_messages_; | 293 std::vector<ReceivedMessage> verified_received_messages_; |
| 315 | 294 |
| 316 std::unique_ptr<SecureChannel> secure_channel_; | 295 std::unique_ptr<SecureChannel> secure_channel_; |
| 317 | 296 |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 | 525 |
| 547 StartAndFinishSendingMessage("feature", "request2", /* success */ true); | 526 StartAndFinishSendingMessage("feature", "request2", /* success */ true); |
| 548 | 527 |
| 549 fake_connection_->ReceiveMessage("feature", "response2, but encoded"); | 528 fake_connection_->ReceiveMessage("feature", "response2, but encoded"); |
| 550 VerifyReceivedMessages(std::vector<ReceivedMessage> { | 529 VerifyReceivedMessages(std::vector<ReceivedMessage> { |
| 551 {"feature", "response2"} | 530 {"feature", "response2"} |
| 552 }); | 531 }); |
| 553 } | 532 } |
| 554 | 533 |
| 555 } // namespace cryptauth | 534 } // namespace cryptauth |
| OLD | NEW |