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

Side by Side Diff: components/cryptauth/local_device_data_provider_unittest.cc

Issue 2951303002: [CrOS Tether] Move LocalDeviceDataProvider from //chromeos/components/tether to //components/crypta… (Closed)
Patch Set: Rebased. Created 3 years, 6 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 "components/cryptauth/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_enroller.h"
14 #include "components/cryptauth/cryptauth_enrollment_manager.h" 14 #include "components/cryptauth/cryptauth_enrollment_manager.h"
15 #include "components/cryptauth/fake_cryptauth_gcm_manager.h" 15 #include "components/cryptauth/fake_cryptauth_gcm_manager.h"
16 #include "components/cryptauth/fake_cryptauth_service.h" 16 #include "components/cryptauth/fake_cryptauth_service.h"
17 #include "components/cryptauth/proto/cryptauth_api.pb.h" 17 #include "components/cryptauth/proto/cryptauth_api.pb.h"
18 #include "components/cryptauth/secure_message_delegate.h" 18 #include "components/cryptauth/secure_message_delegate.h"
19 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 using testing::NiceMock; 22 using testing::NiceMock;
23 using testing::Return; 23 using testing::Return;
24 24
25 namespace chromeos { 25 namespace cryptauth {
26
27 namespace tether {
28 26
29 namespace { 27 namespace {
30 28
31 const char kDefaultPublicKey[] = "publicKey"; 29 const char kDefaultPublicKey[] = "publicKey";
32 30
33 const char kBeaconSeed1Data[] = "beaconSeed1Data"; 31 const char kBeaconSeed1Data[] = "beaconSeed1Data";
34 const int64_t kBeaconSeed1StartMs = 1000L; 32 const int64_t kBeaconSeed1StartMs = 1000L;
35 const int64_t kBeaconSeed1EndMs = 2000L; 33 const int64_t kBeaconSeed1EndMs = 2000L;
36 34
37 const char kBeaconSeed2Data[] = "beaconSeed2Data"; 35 const char kBeaconSeed2Data[] = "beaconSeed2Data";
38 const int64_t kBeaconSeed2StartMs = 2000L; 36 const int64_t kBeaconSeed2StartMs = 2000L;
39 const int64_t kBeaconSeed2EndMs = 3000L; 37 const int64_t kBeaconSeed2EndMs = 3000L;
40 38
41 class MockCryptAuthDeviceManager : public cryptauth::CryptAuthDeviceManager { 39 class MockCryptAuthDeviceManager : public CryptAuthDeviceManager {
42 public: 40 public:
43 MockCryptAuthDeviceManager() {} 41 MockCryptAuthDeviceManager() {}
44 ~MockCryptAuthDeviceManager() override {} 42 ~MockCryptAuthDeviceManager() override {}
45 43
46 MOCK_CONST_METHOD0(GetSyncedDevices, 44 MOCK_CONST_METHOD0(GetSyncedDevices, std::vector<ExternalDeviceInfo>());
47 std::vector<cryptauth::ExternalDeviceInfo>());
48 }; 45 };
49 46
50 class MockCryptAuthEnrollmentManager 47 class MockCryptAuthEnrollmentManager : public CryptAuthEnrollmentManager {
51 : public cryptauth::CryptAuthEnrollmentManager {
52 public: 48 public:
53 explicit MockCryptAuthEnrollmentManager( 49 explicit MockCryptAuthEnrollmentManager(
54 cryptauth::FakeCryptAuthGCMManager* fake_cryptauth_gcm_manager) 50 FakeCryptAuthGCMManager* fake_cryptauth_gcm_manager)
55 : cryptauth::CryptAuthEnrollmentManager( 51 : CryptAuthEnrollmentManager(nullptr /* clock */,
56 nullptr /* clock */, 52 nullptr /* enroller_factory */,
57 nullptr /* enroller_factory */, 53 nullptr /* secure_message_delegate */,
58 nullptr /* secure_message_delegate */, 54 GcmDeviceInfo(),
59 cryptauth::GcmDeviceInfo(), 55 fake_cryptauth_gcm_manager,
60 fake_cryptauth_gcm_manager, 56 nullptr /* pref_service */) {}
61 nullptr /* pref_service */) {}
62 ~MockCryptAuthEnrollmentManager() override {} 57 ~MockCryptAuthEnrollmentManager() override {}
63 58
64 MOCK_CONST_METHOD0(GetUserPublicKey, std::string()); 59 MOCK_CONST_METHOD0(GetUserPublicKey, std::string());
65 }; 60 };
66 61
67 cryptauth::BeaconSeed CreateBeaconSeed(const std::string& data, 62 BeaconSeed CreateBeaconSeed(const std::string& data,
68 int64_t start_ms, 63 int64_t start_ms,
69 int64_t end_ms) { 64 int64_t end_ms) {
70 cryptauth::BeaconSeed seed; 65 BeaconSeed seed;
71 seed.set_data(data); 66 seed.set_data(data);
72 seed.set_start_time_millis(start_ms); 67 seed.set_start_time_millis(start_ms);
73 seed.set_end_time_millis(end_ms); 68 seed.set_end_time_millis(end_ms);
74 return seed; 69 return seed;
75 } 70 }
76 71
77 } // namespace 72 } // namespace
78 73
79 class LocalDeviceDataProviderTest : public testing::Test { 74 class LocalDeviceDataProviderTest : public testing::Test {
80 protected: 75 protected:
81 LocalDeviceDataProviderTest() { 76 LocalDeviceDataProviderTest() {
82 fake_beacon_seeds_.push_back(CreateBeaconSeed( 77 fake_beacon_seeds_.push_back(CreateBeaconSeed(
83 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); 78 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs));
84 fake_beacon_seeds_.push_back(CreateBeaconSeed( 79 fake_beacon_seeds_.push_back(CreateBeaconSeed(
85 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); 80 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs));
86 81
87 // Has no public key and no BeaconSeeds. 82 // Has no public key and no BeaconSeeds.
88 cryptauth::ExternalDeviceInfo synced_device1; 83 ExternalDeviceInfo synced_device1;
89 fake_synced_devices_.push_back(synced_device1); 84 fake_synced_devices_.push_back(synced_device1);
90 85
91 // Has no public key and some BeaconSeeds. 86 // Has no public key and some BeaconSeeds.
92 cryptauth::ExternalDeviceInfo synced_device2; 87 ExternalDeviceInfo synced_device2;
93 synced_device2.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( 88 synced_device2.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
94 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); 89 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs));
95 synced_device2.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( 90 synced_device2.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
96 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); 91 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs));
97 fake_synced_devices_.push_back(synced_device2); 92 fake_synced_devices_.push_back(synced_device2);
98 93
99 // Has another different public key and no BeaconSeeds. 94 // Has another different public key and no BeaconSeeds.
100 cryptauth::ExternalDeviceInfo synced_device3; 95 ExternalDeviceInfo synced_device3;
101 synced_device3.set_public_key("anotherPublicKey"); 96 synced_device3.set_public_key("anotherPublicKey");
102 fake_synced_devices_.push_back(synced_device3); 97 fake_synced_devices_.push_back(synced_device3);
103 98
104 // Has different public key and BeaconSeeds. 99 // Has different public key and BeaconSeeds.
105 cryptauth::ExternalDeviceInfo synced_device4; 100 ExternalDeviceInfo synced_device4;
106 synced_device4.set_public_key("otherPublicKey"); 101 synced_device4.set_public_key("otherPublicKey");
107 synced_device4.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( 102 synced_device4.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
108 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); 103 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs));
109 synced_device4.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( 104 synced_device4.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
110 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); 105 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs));
111 fake_synced_devices_.push_back(synced_device4); 106 fake_synced_devices_.push_back(synced_device4);
112 107
113 // Has public key but no BeaconSeeds. 108 // Has public key but no BeaconSeeds.
114 cryptauth::ExternalDeviceInfo synced_device5; 109 ExternalDeviceInfo synced_device5;
115 synced_device5.set_public_key(kDefaultPublicKey); 110 synced_device5.set_public_key(kDefaultPublicKey);
116 fake_synced_devices_.push_back(synced_device5); 111 fake_synced_devices_.push_back(synced_device5);
117 112
118 // Has public key and BeaconSeeds. 113 // Has public key and BeaconSeeds.
119 cryptauth::ExternalDeviceInfo synced_device6; 114 ExternalDeviceInfo synced_device6;
120 synced_device6.set_public_key(kDefaultPublicKey); 115 synced_device6.set_public_key(kDefaultPublicKey);
121 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( 116 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
122 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs)); 117 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs));
123 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed( 118 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
124 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs)); 119 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs));
125 fake_synced_devices_.push_back(synced_device6); 120 fake_synced_devices_.push_back(synced_device6);
126 } 121 }
127 122
128 void SetUp() override { 123 void SetUp() override {
129 mock_device_manager_ = 124 mock_device_manager_ =
130 base::WrapUnique(new NiceMock<MockCryptAuthDeviceManager>()); 125 base::WrapUnique(new NiceMock<MockCryptAuthDeviceManager>());
131 fake_cryptauth_gcm_manager_ = 126 fake_cryptauth_gcm_manager_ =
132 base::MakeUnique<cryptauth::FakeCryptAuthGCMManager>("registrationId"); 127 base::MakeUnique<FakeCryptAuthGCMManager>("registrationId");
133 mock_enrollment_manager_ = 128 mock_enrollment_manager_ =
134 base::WrapUnique(new NiceMock<MockCryptAuthEnrollmentManager>( 129 base::WrapUnique(new NiceMock<MockCryptAuthEnrollmentManager>(
135 fake_cryptauth_gcm_manager_.get())); 130 fake_cryptauth_gcm_manager_.get()));
136 131
137 fake_cryptauth_service_ = 132 fake_cryptauth_service_ = base::MakeUnique<FakeCryptAuthService>();
138 base::MakeUnique<cryptauth::FakeCryptAuthService>();
139 fake_cryptauth_service_->set_cryptauth_device_manager( 133 fake_cryptauth_service_->set_cryptauth_device_manager(
140 mock_device_manager_.get()); 134 mock_device_manager_.get());
141 fake_cryptauth_service_->set_cryptauth_enrollment_manager( 135 fake_cryptauth_service_->set_cryptauth_enrollment_manager(
142 mock_enrollment_manager_.get()); 136 mock_enrollment_manager_.get());
143 137
144 provider_ = base::WrapUnique( 138 provider_ = base::WrapUnique(
145 new LocalDeviceDataProvider(fake_cryptauth_service_.get())); 139 new LocalDeviceDataProvider(fake_cryptauth_service_.get()));
146 } 140 }
147 141
148 std::vector<cryptauth::BeaconSeed> fake_beacon_seeds_; 142 std::vector<BeaconSeed> fake_beacon_seeds_;
149 std::vector<cryptauth::ExternalDeviceInfo> fake_synced_devices_; 143 std::vector<ExternalDeviceInfo> fake_synced_devices_;
150 144
151 std::unique_ptr<cryptauth::FakeCryptAuthGCMManager> 145 std::unique_ptr<FakeCryptAuthGCMManager> fake_cryptauth_gcm_manager_;
152 fake_cryptauth_gcm_manager_;
153 std::unique_ptr<NiceMock<MockCryptAuthDeviceManager>> mock_device_manager_; 146 std::unique_ptr<NiceMock<MockCryptAuthDeviceManager>> mock_device_manager_;
154 std::unique_ptr<NiceMock<MockCryptAuthEnrollmentManager>> 147 std::unique_ptr<NiceMock<MockCryptAuthEnrollmentManager>>
155 mock_enrollment_manager_; 148 mock_enrollment_manager_;
156 std::unique_ptr<cryptauth::FakeCryptAuthService> fake_cryptauth_service_; 149 std::unique_ptr<FakeCryptAuthService> fake_cryptauth_service_;
157 150
158 std::unique_ptr<LocalDeviceDataProvider> provider_; 151 std::unique_ptr<LocalDeviceDataProvider> provider_;
159 152
160 private: 153 private:
161 DISALLOW_COPY_AND_ASSIGN(LocalDeviceDataProviderTest); 154 DISALLOW_COPY_AND_ASSIGN(LocalDeviceDataProviderTest);
162 }; 155 };
163 156
164 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoPublicKey) { 157 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoPublicKey) {
165 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) 158 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
166 .WillByDefault(Return(std::string())); 159 .WillByDefault(Return(std::string()));
167 ON_CALL(*mock_device_manager_, GetSyncedDevices()) 160 ON_CALL(*mock_device_manager_, GetSyncedDevices())
168 .WillByDefault(Return(fake_synced_devices_)); 161 .WillByDefault(Return(fake_synced_devices_));
169 162
170 std::string public_key; 163 std::string public_key;
171 std::vector<cryptauth::BeaconSeed> beacon_seeds; 164 std::vector<BeaconSeed> beacon_seeds;
172 165
173 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); 166 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
174 } 167 }
175 168
176 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoSyncedDevices) { 169 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_NoSyncedDevices) {
177 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) 170 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
178 .WillByDefault(Return(kDefaultPublicKey)); 171 .WillByDefault(Return(kDefaultPublicKey));
179 ON_CALL(*mock_device_manager_, GetSyncedDevices()) 172 ON_CALL(*mock_device_manager_, GetSyncedDevices())
180 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>())); 173 .WillByDefault(Return(std::vector<ExternalDeviceInfo>()));
181 174
182 std::string public_key; 175 std::string public_key;
183 std::vector<cryptauth::BeaconSeed> beacon_seeds; 176 std::vector<BeaconSeed> beacon_seeds;
184 177
185 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); 178 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
186 } 179 }
187 180
188 TEST_F(LocalDeviceDataProviderTest, 181 TEST_F(LocalDeviceDataProviderTest,
189 TestGetLocalDeviceData_NoSyncedDeviceMatchingPublicKey) { 182 TestGetLocalDeviceData_NoSyncedDeviceMatchingPublicKey) {
190 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) 183 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
191 .WillByDefault(Return(kDefaultPublicKey)); 184 .WillByDefault(Return(kDefaultPublicKey));
192 ON_CALL(*mock_device_manager_, GetSyncedDevices()) 185 ON_CALL(*mock_device_manager_, GetSyncedDevices())
193 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>{ 186 .WillByDefault(Return(std::vector<ExternalDeviceInfo>{
194 fake_synced_devices_[0], fake_synced_devices_[1], 187 fake_synced_devices_[0], fake_synced_devices_[1],
195 fake_synced_devices_[2], fake_synced_devices_[3]})); 188 fake_synced_devices_[2], fake_synced_devices_[3]}));
196 189
197 std::string public_key; 190 std::string public_key;
198 std::vector<cryptauth::BeaconSeed> beacon_seeds; 191 std::vector<BeaconSeed> beacon_seeds;
199 192
200 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); 193 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
201 } 194 }
202 195
203 TEST_F(LocalDeviceDataProviderTest, 196 TEST_F(LocalDeviceDataProviderTest,
204 TestGetLocalDeviceData_SyncedDeviceIncludesPublicKeyButNoBeaconSeeds) { 197 TestGetLocalDeviceData_SyncedDeviceIncludesPublicKeyButNoBeaconSeeds) {
205 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) 198 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
206 .WillByDefault(Return(kDefaultPublicKey)); 199 .WillByDefault(Return(kDefaultPublicKey));
207 ON_CALL(*mock_device_manager_, GetSyncedDevices()) 200 ON_CALL(*mock_device_manager_, GetSyncedDevices())
208 .WillByDefault(Return(std::vector<cryptauth::ExternalDeviceInfo>{ 201 .WillByDefault(Return(std::vector<ExternalDeviceInfo>{
209 fake_synced_devices_[4], 202 fake_synced_devices_[4],
210 })); 203 }));
211 204
212 std::string public_key; 205 std::string public_key;
213 std::vector<cryptauth::BeaconSeed> beacon_seeds; 206 std::vector<BeaconSeed> beacon_seeds;
214 207
215 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); 208 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
216 } 209 }
217 210
218 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_Success) { 211 TEST_F(LocalDeviceDataProviderTest, TestGetLocalDeviceData_Success) {
219 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey()) 212 ON_CALL(*mock_enrollment_manager_, GetUserPublicKey())
220 .WillByDefault(Return(kDefaultPublicKey)); 213 .WillByDefault(Return(kDefaultPublicKey));
221 ON_CALL(*mock_device_manager_, GetSyncedDevices()) 214 ON_CALL(*mock_device_manager_, GetSyncedDevices())
222 .WillByDefault(Return(fake_synced_devices_)); 215 .WillByDefault(Return(fake_synced_devices_));
223 216
224 std::string public_key; 217 std::string public_key;
225 std::vector<cryptauth::BeaconSeed> beacon_seeds; 218 std::vector<BeaconSeed> beacon_seeds;
226 219
227 EXPECT_TRUE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds)); 220 EXPECT_TRUE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
228 221
229 EXPECT_EQ(kDefaultPublicKey, public_key); 222 EXPECT_EQ(kDefaultPublicKey, public_key);
230 223
231 ASSERT_EQ(fake_beacon_seeds_.size(), beacon_seeds.size()); 224 ASSERT_EQ(fake_beacon_seeds_.size(), beacon_seeds.size());
232 for (size_t i = 0; i < fake_beacon_seeds_.size(); i++) { 225 for (size_t i = 0; i < fake_beacon_seeds_.size(); i++) {
233 // Note: google::protobuf::util::MessageDifferencer can only be used to diff 226 // Note: google::protobuf::util::MessageDifferencer can only be used to diff
234 // Message, but BeaconSeed derives from the incompatible MessageLite class. 227 // Message, but BeaconSeed derives from the incompatible MessageLite class.
235 cryptauth::BeaconSeed expected = fake_beacon_seeds_[i]; 228 BeaconSeed expected = fake_beacon_seeds_[i];
236 cryptauth::BeaconSeed actual = beacon_seeds[i]; 229 BeaconSeed actual = beacon_seeds[i];
237 EXPECT_EQ(expected.data(), actual.data()); 230 EXPECT_EQ(expected.data(), actual.data());
238 EXPECT_EQ(expected.start_time_millis(), actual.start_time_millis()); 231 EXPECT_EQ(expected.start_time_millis(), actual.start_time_millis());
239 EXPECT_EQ(expected.end_time_millis(), actual.end_time_millis()); 232 EXPECT_EQ(expected.end_time_millis(), actual.end_time_millis());
240 } 233 }
241 } 234 }
242 235
243 } // namespace tether 236 } // namespace cryptauth
244
245 } // namespace chromeos
OLDNEW
« no previous file with comments | « components/cryptauth/local_device_data_provider.cc ('k') | components/cryptauth/mock_local_device_data_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698