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

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

Issue 2801353002: [CrOS Tether] Fill out the Initializer class. Tether will now initialize fully once the flag is ena… (Closed)
Patch Set: hansberry@ comments. 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/local_device_data_provider.h" 5 #include "chromeos/components/tether/local_device_data_provider.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/logging.h" 10 #include "base/logging.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"
13 #include "components/cryptauth/cryptauth_enrollment_manager.h" 14 #include "components/cryptauth/cryptauth_enrollment_manager.h"
15 #include "components/cryptauth/fake_cryptauth_gcm_manager.h"
14 #include "components/cryptauth/proto/cryptauth_api.pb.h" 16 #include "components/cryptauth/proto/cryptauth_api.pb.h"
17 #include "components/cryptauth/secure_message_delegate.h"
15 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
17 20
18 using testing::NiceMock; 21 using testing::NiceMock;
19 using testing::Return; 22 using testing::Return;
20 23
21 namespace chromeos { 24 namespace chromeos {
22 25
23 namespace tether { 26 namespace tether {
24 27
25 namespace { 28 namespace {
26 const std::string kDefaultPublicKey = "publicKey";
27 29
28 const std::string kBeaconSeed1Data = "beaconSeed1Data"; 30 const char kDefaultPublicKey[] = "publicKey";
31
32 const char kBeaconSeed1Data[] = "beaconSeed1Data";
29 const int64_t kBeaconSeed1StartMs = 1000L; 33 const int64_t kBeaconSeed1StartMs = 1000L;
30 const int64_t kBeaconSeed1EndMs = 2000L; 34 const int64_t kBeaconSeed1EndMs = 2000L;
31 35
32 const std::string kBeaconSeed2Data = "beaconSeed2Data"; 36 const char kBeaconSeed2Data[] = "beaconSeed2Data";
33 const int64_t kBeaconSeed2StartMs = 2000L; 37 const int64_t kBeaconSeed2StartMs = 2000L;
34 const int64_t kBeaconSeed2EndMs = 3000L; 38 const int64_t kBeaconSeed2EndMs = 3000L;
35 39
40 class MockCryptAuthDeviceManager : public cryptauth::CryptAuthDeviceManager {
41 public:
42 MockCryptAuthDeviceManager() {}
43 ~MockCryptAuthDeviceManager() override {}
44
45 MOCK_CONST_METHOD0(GetSyncedDevices,
46 std::vector<cryptauth::ExternalDeviceInfo>());
47 };
48
49 class MockCryptAuthEnrollmentManager
50 : public cryptauth::CryptAuthEnrollmentManager {
51 public:
52 MockCryptAuthEnrollmentManager(
53 cryptauth::FakeCryptAuthGCMManager* fake_cryptauth_gcm_manager)
stevenjb 2017/04/10 20:26:59 explicit
Kyle Horimoto 2017/04/11 01:40:40 Done.
54 : cryptauth::CryptAuthEnrollmentManager(nullptr,
stevenjb 2017/04/10 20:26:59 comment what each of these are e.g. nullptr /* clo
Kyle Horimoto 2017/04/11 01:40:40 Done.
55 nullptr,
56 nullptr,
57 cryptauth::GcmDeviceInfo(),
58 fake_cryptauth_gcm_manager,
59 nullptr) {}
60 ~MockCryptAuthEnrollmentManager() override {}
61
62 MOCK_CONST_METHOD0(GetUserPublicKey, std::string());
63 };
64
36 cryptauth::BeaconSeed CreateBeaconSeed(const std::string& data, 65 cryptauth::BeaconSeed CreateBeaconSeed(const std::string& data,
37 int64_t start_ms, 66 int64_t start_ms,
38 int64_t end_ms) { 67 int64_t end_ms) {
39 cryptauth::BeaconSeed seed; 68 cryptauth::BeaconSeed seed;
40 seed.set_data(data); 69 seed.set_data(data);
41 seed.set_start_time_millis(start_ms); 70 seed.set_start_time_millis(start_ms);
42 seed.set_end_time_millis(end_ms); 71 seed.set_end_time_millis(end_ms);
43 return seed; 72 return seed;
44 } 73 }
74
45 } // namespace 75 } // namespace
46 76
47 class LocalDeviceDataProviderTest : public testing::Test { 77 class LocalDeviceDataProviderTest : public testing::Test {
48 protected: 78 protected:
49 class MockProviderDelegate
50 : public LocalDeviceDataProvider::LocalDeviceDataProviderDelegate {
51 public:
52 MockProviderDelegate() {}
53 ~MockProviderDelegate() override {}
54
55 MOCK_CONST_METHOD0(GetUserPublicKey, std::string());
56 MOCK_CONST_METHOD0(GetSyncedDevices,
57 std::vector<cryptauth::ExternalDeviceInfo>());
58 };
59
60 LocalDeviceDataProviderTest() { 79 LocalDeviceDataProviderTest() {
61 fake_beacon_seeds_.push_back(CreateBeaconSeed( 80 fake_beacon_seeds_.push_back(CreateBeaconSeed(
62 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); 81 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs));
63 fake_beacon_seeds_.push_back(CreateBeaconSeed( 82 fake_beacon_seeds_.push_back(CreateBeaconSeed(
64 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); 83 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs));
65 84
66 // Has no public key and no BeaconSeeds. 85 // Has no public key and no BeaconSeeds.
67 cryptauth::ExternalDeviceInfo synced_device1; 86 cryptauth::ExternalDeviceInfo synced_device1;
68 fake_synced_devices_.push_back(synced_device1); 87 fake_synced_devices_.push_back(synced_device1);
69 88
(...skipping 28 matching lines...) Expand all
98 cryptauth::ExternalDeviceInfo synced_device6; 117 cryptauth::ExternalDeviceInfo synced_device6;
99 synced_device6.set_public_key(kDefaultPublicKey); 118 synced_device6.set_public_key(kDefaultPublicKey);
100 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( 119 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
101 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); 120 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs));
102 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( 121 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
103 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); 122 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs));
104 fake_synced_devices_.push_back(synced_device6); 123 fake_synced_devices_.push_back(synced_device6);
105 } 124 }
106 125
107 void SetUp() override { 126 void SetUp() override {
108 mock_delegate_ = new NiceMock<MockProviderDelegate>(); 127 mock_device_manager_ =
128 base::WrapUnique(new NiceMock<MockCryptAuthDeviceManager>());
129 fake_cryptauth_gcm_manager_ =
130 base::MakeUnique<cryptauth::FakeCryptAuthGCMManager>("registrationId");
131 mock_enrollment_manager_ =
132 base::WrapUnique(new NiceMock<MockCryptAuthEnrollmentManager>(
133 fake_cryptauth_gcm_manager_.get()));
109 134
110 provider_ = base::WrapUnique( 135 provider_ = base::WrapUnique(new LocalDeviceDataProvider(
111 new LocalDeviceDataProvider(base::WrapUnique(mock_delegate_))); 136 mock_device_manager_.get(), mock_enrollment_manager_.get()));
112 } 137 }
113 138
114 std::unique_ptr<LocalDeviceDataProvider> provider_;
115 NiceMock<MockProviderDelegate>* mock_delegate_;
116
117 std::vector<cryptauth::BeaconSeed> fake_beacon_seeds_; 139 std::vector<cryptauth::BeaconSeed> fake_beacon_seeds_;
118 std::vector<cryptauth::ExternalDeviceInfo> fake_synced_devices_; 140 std::vector<cryptauth::ExternalDeviceInfo> fake_synced_devices_;
119 141
142 std::unique_ptr<cryptauth::FakeCryptAuthGCMManager>
143 fake_cryptauth_gcm_manager_;
144 std::unique_ptr<NiceMock<MockCryptAuthDeviceManager>> mock_device_manager_;
145 std::unique_ptr<NiceMock<MockCryptAuthEnrollmentManager>>
146 mock_enrollment_manager_;
147
148 std::unique_ptr<LocalDeviceDataProvider> provider_;
149
120 private: 150 private:
121 DISALLOW_COPY_AND_ASSIGN(LocalDeviceDataProviderTest); 151 DISALLOW_COPY_AND_ASSIGN(LocalDeviceDataProviderTest);
122 }; 152 };
123 153
124 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoPublicKey) { 154 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoPublicKey) {
125 ON_CALL(*mock_delegate_, GetUserPublicKey()) 155 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
126 .WillByDefault(Return(std::string())); 156 .WillByDefault(Return(std::string()));
127 ON_CALL(*mock_delegate_, GetSyncedDevices()) 157 ON_CALL(*mock_device_manager_, GetSyncedDevices())
128 .WillByDefault(Return(fake_synced_devices_)); 158 .WillByDefault(Return(fake_synced_devices_));
129 159
130 std::string public_key; 160 std::string public_key;
131 std::vector<cryptauth::BeaconSeed> beacon_seeds; 161 std::vector<cryptauth::BeaconSeed> beacon_seeds;
132 162
133 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); 163 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
134 } 164 }
135 165
136 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoSyncedDevices) { 166 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoSyncedDevices) {
137 ON_CALL(*mock_delegate_, GetUserPublicKey()) 167 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
138 .WillByDefault(Return(kDefaultPublicKey)); 168 .WillByDefault(Return(kDefaultPublicKey));
139 ON_CALL(*mock_delegate_, GetSyncedDevices()) 169 ON_CALL(*mock_device_manager_, GetSyncedDevices())
140 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>())); 170 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>()));
141 171
142 std::string public_key; 172 std::string public_key;
143 std::vector<cryptauth::BeaconSeed> beacon_seeds; 173 std::vector<cryptauth::BeaconSeed> beacon_seeds;
144 174
145 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); 175 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
146 } 176 }
147 177
148 TEST_F(LocalDeviceDataProviderTest, 178 TEST_F(LocalDeviceDataProviderTest,
149 TestGetLocalDeviceData_NoSyncedDeviceMatchingPublicKey) { 179 TestGetLocalDeviceData_NoSyncedDeviceMatchingPublicKey) {
150 ON_CALL(*mock_delegate_, GetUserPublicKey()) 180 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
151 .WillByDefault(Return(kDefaultPublicKey)); 181 .WillByDefault(Return(kDefaultPublicKey));
152 ON_CALL(*mock_delegate_, GetSyncedDevices()) 182 ON_CALL(*mock_device_manager_, GetSyncedDevices())
153 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>{ 183 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>{
154 fake_synced_devices_[0], fake_synced_devices_[1], 184 fake_synced_devices_[0], fake_synced_devices_[1],
155 fake_synced_devices_[2], fake_synced_devices_[3]})); 185 fake_synced_devices_[2], fake_synced_devices_[3]}));
156 186
157 std::string public_key; 187 std::string public_key;
158 std::vector<cryptauth::BeaconSeed> beacon_seeds; 188 std::vector<cryptauth::BeaconSeed> beacon_seeds;
159 189
160 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); 190 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
161 } 191 }
162 192
163 TEST_F(LocalDeviceDataProviderTest, 193 TEST_F(LocalDeviceDataProviderTest,
164 TestGetLocalDeviceData_SyncedDeviceIncludesPublicKeyButNoBeaconSeeds) { 194 TestGetLocalDeviceData_SyncedDeviceIncludesPublicKeyButNoBeaconSeeds) {
165 ON_CALL(*mock_delegate_, GetUserPublicKey()) 195 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
166 .WillByDefault(Return(kDefaultPublicKey)); 196 .WillByDefault(Return(kDefaultPublicKey));
167 ON_CALL(*mock_delegate_, GetSyncedDevices()) 197 ON_CALL(*mock_device_manager_, GetSyncedDevices())
168 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>{ 198 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>{
169 fake_synced_devices_[4], 199 fake_synced_devices_[4],
170 })); 200 }));
171 201
172 std::string public_key; 202 std::string public_key;
173 std::vector<cryptauth::BeaconSeed> beacon_seeds; 203 std::vector<cryptauth::BeaconSeed> beacon_seeds;
174 204
175 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); 205 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
176 } 206 }
177 207
178 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_Success) { 208 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_Success) {
179 ON_CALL(*mock_delegate_, GetUserPublicKey()) 209 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
180 .WillByDefault(Return(kDefaultPublicKey)); 210 .WillByDefault(Return(kDefaultPublicKey));
181 ON_CALL(*mock_delegate_, GetSyncedDevices()) 211 ON_CALL(*mock_device_manager_, GetSyncedDevices())
182 .WillByDefault(Return(fake_synced_devices_)); 212 .WillByDefault(Return(fake_synced_devices_));
183 213
184 std::string public_key; 214 std::string public_key;
185 std::vector<cryptauth::BeaconSeed> beacon_seeds; 215 std::vector<cryptauth::BeaconSeed> beacon_seeds;
186 216
187 EXPECT_TRUE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); 217 EXPECT_TRUE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
188 218
189 EXPECT_EQ(kDefaultPublicKey, public_key); 219 EXPECT_EQ(kDefaultPublicKey, public_key);
190 220
191 ASSERT_EQ(fake_beacon_seeds_.size(), beacon_seeds.size()); 221 ASSERT_EQ(fake_beacon_seeds_.size(), beacon_seeds.size());
192 for (size_t i = 0; i < fake_beacon_seeds_.size(); i++) { 222 for (size_t i = 0; i < fake_beacon_seeds_.size(); i++) {
193 // Note: google::protobuf::util::MessageDifferencer can only be used to diff 223 // Note: google::protobuf::util::MessageDifferencer can only be used to diff
194 // Message, but BeaconSeed derives from the incompatible MessageLite class. 224 // Message, but BeaconSeed derives from the incompatible MessageLite class.
195 cryptauth::BeaconSeed expected = fake_beacon_seeds_[i]; 225 cryptauth::BeaconSeed expected = fake_beacon_seeds_[i];
196 cryptauth::BeaconSeed actual = beacon_seeds[i]; 226 cryptauth::BeaconSeed actual = beacon_seeds[i];
197 EXPECT_EQ(expected.data(), actual.data()); 227 EXPECT_EQ(expected.data(), actual.data());
198 EXPECT_EQ(expected.start_time_millis(), actual.start_time_millis()); 228 EXPECT_EQ(expected.start_time_millis(), actual.start_time_millis());
199 EXPECT_EQ(expected.end_time_millis(), actual.end_time_millis()); 229 EXPECT_EQ(expected.end_time_millis(), actual.end_time_millis());
200 } 230 }
201 } 231 }
202 232
203 } // namespace tether 233 } // namespace tether
204 234
205 } // namespace chromeos 235 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698