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

Side by Side Diff: chromeos/components/tether/tether_host_fetcher_unittest.cc

Issue 2801353002: [CrOS Tether] Fill out the Initializer class. Tether will now initialize fully once the flag is ena… (Closed)
Patch Set: Added missing dep. Created 3 years, 8 months 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "chromeos/components/tether/tether_host_fetcher.h" 5 #include "chromeos/components/tether/tether_host_fetcher.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "components/cryptauth/cryptauth_device_manager.h" 12 #include "components/cryptauth/cryptauth_device_manager.h"
13 #include "components/cryptauth/cryptauth_enroller.h"
14 #include "components/cryptauth/cryptauth_enrollment_manager.h"
15 #include "components/cryptauth/fake_cryptauth_gcm_manager.h"
13 #include "components/cryptauth/fake_cryptauth_service.h" 16 #include "components/cryptauth/fake_cryptauth_service.h"
14 #include "components/cryptauth/fake_secure_message_delegate.h" 17 #include "components/cryptauth/fake_secure_message_delegate.h"
15 #include "components/cryptauth/proto/cryptauth_api.pb.h" 18 #include "components/cryptauth/proto/cryptauth_api.pb.h"
16 #include "components/cryptauth/remote_device_loader.h" 19 #include "components/cryptauth/remote_device_loader.h"
17 #include "components/cryptauth/remote_device_test_util.h" 20 #include "components/cryptauth/remote_device_test_util.h"
18 #include "testing/gmock/include/gmock/gmock.h" 21 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
20 23
21 using testing::_; 24 using testing::_;
22 using testing::Invoke; 25 using testing::Invoke;
23 using testing::NiceMock; 26 using testing::NiceMock;
24 using testing::Return; 27 using testing::Return;
25 28
26 namespace chromeos { 29 namespace chromeos {
27 30
28 namespace tether { 31 namespace tether {
29 32
30 namespace { 33 namespace {
31 34
32 const char kTestUserId[] = "testUserId"; 35 const char kTestUserId[] = "testUserId";
33 const char kTestUserPrivateKey[] = "kTestUserPrivateKey"; 36 const char kTestUserPrivateKey[] = "kTestUserPrivateKey";
34 37
38 class MockCryptAuthDeviceManager : public cryptauth::CryptAuthDeviceManager {
39 public:
40 ~MockCryptAuthDeviceManager() override {}
41
42 MOCK_CONST_METHOD0(GetTetherHosts,
43 std::vector<cryptauth::ExternalDeviceInfo>());
44 };
45
46 class MockCryptAuthEnrollmentManager
47 : public cryptauth::CryptAuthEnrollmentManager {
48 public:
49 explicit MockCryptAuthEnrollmentManager(
50 cryptauth::FakeCryptAuthGCMManager* fake_cryptauth_gcm_manager)
51 : cryptauth::CryptAuthEnrollmentManager(
52 nullptr /* clock */,
53 nullptr /* enroller_factory */,
54 nullptr /* secure_message_delegate */,
55 cryptauth::GcmDeviceInfo(),
56 fake_cryptauth_gcm_manager,
57 nullptr /* pref_service */) {}
58 ~MockCryptAuthEnrollmentManager() override {}
59
60 MOCK_CONST_METHOD0(GetUserPrivateKey, std::string());
61 };
62
35 class FakeCryptAuthServiceWithTracking 63 class FakeCryptAuthServiceWithTracking
36 : public cryptauth::FakeCryptAuthService { 64 : public cryptauth::FakeCryptAuthService {
37 public: 65 public:
38 // FakeCryptAuthService: 66 // FakeCryptAuthService:
39 std::unique_ptr<cryptauth::SecureMessageDelegate> 67 std::unique_ptr<cryptauth::SecureMessageDelegate>
40 CreateSecureMessageDelegate() override { 68 CreateSecureMessageDelegate() override {
41 cryptauth::FakeSecureMessageDelegate* delegate = 69 cryptauth::FakeSecureMessageDelegate* delegate =
42 new cryptauth::FakeSecureMessageDelegate(); 70 new cryptauth::FakeSecureMessageDelegate();
43 created_delegates_.push_back(delegate); 71 created_delegates_.push_back(delegate);
44 return base::WrapUnique(delegate); 72 return base::WrapUnique(delegate);
45 } 73 }
46 74
47 void VerifySecureMessageDelegateCreatedByFactory( 75 void VerifySecureMessageDelegateCreatedByFactory(
48 cryptauth::FakeSecureMessageDelegate* delegate) { 76 cryptauth::FakeSecureMessageDelegate* delegate) {
49 EXPECT_FALSE(std::find(created_delegates_.begin(), created_delegates_.end(), 77 EXPECT_FALSE(std::find(created_delegates_.begin(), created_delegates_.end(),
50 delegate) == created_delegates_.end()); 78 delegate) == created_delegates_.end());
51 } 79 }
52 80
53 private: 81 private:
54 std::vector<cryptauth::FakeSecureMessageDelegate*> created_delegates_; 82 std::vector<cryptauth::FakeSecureMessageDelegate*> created_delegates_;
55 }; 83 };
56 84
57 class MockDeviceManager : public cryptauth::CryptAuthDeviceManager {
58 public:
59 ~MockDeviceManager() override {}
60
61 MOCK_CONST_METHOD0(GetTetherHosts,
62 std::vector<cryptauth::ExternalDeviceInfo>());
63 };
64
65 class MockDeviceLoader : public cryptauth::RemoteDeviceLoader { 85 class MockDeviceLoader : public cryptauth::RemoteDeviceLoader {
66 public: 86 public:
67 MockDeviceLoader() 87 MockDeviceLoader()
68 : cryptauth::RemoteDeviceLoader( 88 : cryptauth::RemoteDeviceLoader(
69 std::vector<cryptauth::ExternalDeviceInfo>(), 89 std::vector<cryptauth::ExternalDeviceInfo>(),
70 "", 90 "",
71 "", 91 "",
72 nullptr) {} 92 nullptr) {}
73 ~MockDeviceLoader() override {} 93 ~MockDeviceLoader() override {}
74 94
(...skipping 15 matching lines...) Expand all
90 return device_infos; 110 return device_infos;
91 } 111 }
92 112
93 } // namespace 113 } // namespace
94 114
95 class TetherHostFetcherTest : public testing::Test { 115 class TetherHostFetcherTest : public testing::Test {
96 public: 116 public:
97 class TestRemoteDeviceLoaderFactory 117 class TestRemoteDeviceLoaderFactory
98 : public cryptauth::RemoteDeviceLoader::Factory { 118 : public cryptauth::RemoteDeviceLoader::Factory {
99 public: 119 public:
100 TestRemoteDeviceLoaderFactory(TetherHostFetcherTest* test) : test_(test) {} 120 explicit TestRemoteDeviceLoaderFactory(TetherHostFetcherTest* test)
121 : test_(test) {}
101 122
102 std::unique_ptr<cryptauth::RemoteDeviceLoader> BuildInstance( 123 std::unique_ptr<cryptauth::RemoteDeviceLoader> BuildInstance(
103 const std::vector<cryptauth::ExternalDeviceInfo>& device_info_list, 124 const std::vector<cryptauth::ExternalDeviceInfo>& device_info_list,
104 const std::string& user_id, 125 const std::string& user_id,
105 const std::string& user_private_key, 126 const std::string& user_private_key,
106 std::unique_ptr<cryptauth::SecureMessageDelegate> 127 std::unique_ptr<cryptauth::SecureMessageDelegate>
107 secure_message_delegate) override { 128 secure_message_delegate) override {
108 EXPECT_EQ(test_->test_device_infos_.size(), device_info_list.size()); 129 EXPECT_EQ(test_->test_device_infos_.size(), device_info_list.size());
109 EXPECT_EQ(std::string(kTestUserId), user_id); 130 EXPECT_EQ(std::string(kTestUserId), user_id);
110 EXPECT_EQ(std::string(kTestUserPrivateKey), user_private_key); 131 EXPECT_EQ(std::string(kTestUserPrivateKey), user_private_key);
(...skipping 27 matching lines...) Expand all
138 159
139 TetherHostFetcherTest() 160 TetherHostFetcherTest()
140 : test_devices_(cryptauth::GenerateTestRemoteDevices(5)), 161 : test_devices_(cryptauth::GenerateTestRemoteDevices(5)),
141 test_device_infos_( 162 test_device_infos_(
142 CreateTetherExternalDeviceInfosForRemoteDevices(test_devices_)) {} 163 CreateTetherExternalDeviceInfosForRemoteDevices(test_devices_)) {}
143 164
144 void SetUp() override { 165 void SetUp() override {
145 device_list_list_.clear(); 166 device_list_list_.clear();
146 single_device_list_.clear(); 167 single_device_list_.clear();
147 168
169 mock_device_manager_ =
170 base::WrapUnique(new NiceMock<MockCryptAuthDeviceManager>());
171 ON_CALL(*mock_device_manager_, GetTetherHosts())
172 .WillByDefault(Return(test_device_infos_));
173
174 fake_cryptauth_gcm_manager_ =
175 base::MakeUnique<cryptauth::FakeCryptAuthGCMManager>("registrationId");
176 mock_enrollment_manager_ =
177 base::WrapUnique(new NiceMock<MockCryptAuthEnrollmentManager>(
178 fake_cryptauth_gcm_manager_.get()));
179 ON_CALL(*mock_enrollment_manager_, GetUserPrivateKey())
180 .WillByDefault(Return(kTestUserPrivateKey));
181
148 fake_cryptauth_service_ = 182 fake_cryptauth_service_ =
149 base::WrapUnique(new FakeCryptAuthServiceWithTracking()); 183 base::WrapUnique(new FakeCryptAuthServiceWithTracking());
150 184 fake_cryptauth_service_->set_account_id(kTestUserId);
151 mock_device_manager_ = base::WrapUnique(new NiceMock<MockDeviceManager>()); 185 fake_cryptauth_service_->set_cryptauth_device_manager(
152 ON_CALL(*mock_device_manager_, GetTetherHosts()) 186 mock_device_manager_.get());
153 .WillByDefault(Return(test_device_infos_)); 187 fake_cryptauth_service_->set_cryptauth_enrollment_manager(
188 mock_enrollment_manager_.get());
154 189
155 test_device_loader_factory_ = 190 test_device_loader_factory_ =
156 base::WrapUnique(new TestRemoteDeviceLoaderFactory(this)); 191 base::WrapUnique(new TestRemoteDeviceLoaderFactory(this));
157 cryptauth::RemoteDeviceLoader::Factory::SetInstanceForTesting( 192 cryptauth::RemoteDeviceLoader::Factory::SetInstanceForTesting(
158 test_device_loader_factory_.get()); 193 test_device_loader_factory_.get());
159 194
160 tether_host_fetcher_ = base::MakeUnique<TetherHostFetcher>( 195 tether_host_fetcher_ =
161 std::string(kTestUserId), std::string(kTestUserPrivateKey), 196 base::MakeUnique<TetherHostFetcher>(fake_cryptauth_service_.get());
162 fake_cryptauth_service_.get(), mock_device_manager_.get());
163 } 197 }
164 198
165 void OnTetherHostListFetched(const cryptauth::RemoteDeviceList& device_list) { 199 void OnTetherHostListFetched(const cryptauth::RemoteDeviceList& device_list) {
166 device_list_list_.push_back(device_list); 200 device_list_list_.push_back(device_list);
167 } 201 }
168 202
169 void OnSingleTetherHostFetched( 203 void OnSingleTetherHostFetched(
170 std::unique_ptr<cryptauth::RemoteDevice> device) { 204 std::unique_ptr<cryptauth::RemoteDevice> device) {
171 single_device_list_.push_back(std::move(device)); 205 single_device_list_.push_back(std::move(device));
172 } 206 }
173 207
174 const std::vector<cryptauth::RemoteDevice> test_devices_; 208 const std::vector<cryptauth::RemoteDevice> test_devices_;
175 const std::vector<cryptauth::ExternalDeviceInfo> test_device_infos_; 209 const std::vector<cryptauth::ExternalDeviceInfo> test_device_infos_;
176 210
177 std::vector<cryptauth::RemoteDeviceList> device_list_list_; 211 std::vector<cryptauth::RemoteDeviceList> device_list_list_;
178 std::vector<std::shared_ptr<cryptauth::RemoteDevice>> single_device_list_; 212 std::vector<std::shared_ptr<cryptauth::RemoteDevice>> single_device_list_;
179 213
180 std::unique_ptr<FakeCryptAuthServiceWithTracking> fake_cryptauth_service_; 214 std::unique_ptr<FakeCryptAuthServiceWithTracking> fake_cryptauth_service_;
181 std::unique_ptr<NiceMock<MockDeviceManager>> mock_device_manager_; 215 std::unique_ptr<cryptauth::FakeCryptAuthGCMManager>
216 fake_cryptauth_gcm_manager_;
217 std::unique_ptr<NiceMock<MockCryptAuthDeviceManager>> mock_device_manager_;
218 std::unique_ptr<NiceMock<MockCryptAuthEnrollmentManager>>
219 mock_enrollment_manager_;
182 std::unique_ptr<TestRemoteDeviceLoaderFactory> test_device_loader_factory_; 220 std::unique_ptr<TestRemoteDeviceLoaderFactory> test_device_loader_factory_;
183 221
184 std::unique_ptr<TetherHostFetcher> tether_host_fetcher_; 222 std::unique_ptr<TetherHostFetcher> tether_host_fetcher_;
185 223
186 private: 224 private:
187 DISALLOW_COPY_AND_ASSIGN(TetherHostFetcherTest); 225 DISALLOW_COPY_AND_ASSIGN(TetherHostFetcherTest);
188 }; 226 };
189 227
190 TEST_F(TetherHostFetcherTest, TestFetchAllTetherHosts) { 228 TEST_F(TetherHostFetcherTest, TestFetchAllTetherHosts) {
191 tether_host_fetcher_->FetchAllTetherHosts(base::Bind( 229 tether_host_fetcher_->FetchAllTetherHosts(base::Bind(
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 296
259 EXPECT_EQ(1u, device_list_list_.size()); 297 EXPECT_EQ(1u, device_list_list_.size());
260 EXPECT_EQ(test_devices_, device_list_list_[0]); 298 EXPECT_EQ(test_devices_, device_list_list_[0]);
261 EXPECT_EQ(2u, single_device_list_.size()); 299 EXPECT_EQ(2u, single_device_list_.size());
262 EXPECT_EQ(test_devices_[0], *single_device_list_[0]); 300 EXPECT_EQ(test_devices_[0], *single_device_list_[0]);
263 EXPECT_EQ(nullptr, single_device_list_[1]); 301 EXPECT_EQ(nullptr, single_device_list_[1]);
264 } 302 }
265 303
266 } // namespace tether 304 } // namespace tether
267 305
268 } // namespace cryptauth 306 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/components/tether/tether_host_fetcher.cc ('k') | components/cryptauth/cryptauth_enrollment_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698