blob: 0ba6e524ad4b0badf660d6c5a13f5419c880f636 [file] [log] [blame]
khorimoto0db25be2016-12-20 01:29:051// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
khorimoto25e8d1b2017-06-23 00:57:135#include "components/cryptauth/local_device_data_provider.h"
khorimoto0db25be2016-12-20 01:29:056
Gyuyoung Kim6afb5082018-01-19 13:35:577#include <memory>
khorimoto0db25be2016-12-20 01:29:058#include <string>
9#include <vector>
10
11#include "base/logging.h"
Jeremy Romane6533c72018-03-05 17:44:4612#include "base/memory/ptr_util.h"
khorimoto8e3c1852017-04-12 01:06:3213#include "components/cryptauth/cryptauth_enroller.h"
Kyle Horimoto3cb4b2b2018-02-14 18:07:1814#include "components/cryptauth/fake_cryptauth_device_manager.h"
Kyle Horimoto1bda9652018-02-14 20:21:0215#include "components/cryptauth/fake_cryptauth_enrollment_manager.h"
khorimoto8e3c1852017-04-12 01:06:3216#include "components/cryptauth/fake_cryptauth_gcm_manager.h"
17#include "components/cryptauth/fake_cryptauth_service.h"
khorimoto0db25be2016-12-20 01:29:0518#include "components/cryptauth/proto/cryptauth_api.pb.h"
khorimoto8e3c1852017-04-12 01:06:3219#include "components/cryptauth/secure_message_delegate.h"
khorimoto0db25be2016-12-20 01:29:0520#include "testing/gmock/include/gmock/gmock.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23using testing::NiceMock;
24using testing::Return;
25
khorimoto25e8d1b2017-06-23 00:57:1326namespace cryptauth {
khorimoto0db25be2016-12-20 01:29:0527
28namespace {
khorimoto0db25be2016-12-20 01:29:0529
khorimoto8e3c1852017-04-12 01:06:3230const char kDefaultPublicKey[] = "publicKey";
31
32const char kBeaconSeed1Data[] = "beaconSeed1Data";
khorimoto0db25be2016-12-20 01:29:0533const int64_t kBeaconSeed1StartMs = 1000L;
34const int64_t kBeaconSeed1EndMs = 2000L;
35
khorimoto8e3c1852017-04-12 01:06:3236const char kBeaconSeed2Data[] = "beaconSeed2Data";
khorimoto0db25be2016-12-20 01:29:0537const int64_t kBeaconSeed2StartMs = 2000L;
38const int64_t kBeaconSeed2EndMs = 3000L;
39
khorimoto25e8d1b2017-06-23 00:57:1340BeaconSeed CreateBeaconSeed(const std::string& data,
41 int64_t start_ms,
42 int64_t end_ms) {
43 BeaconSeed seed;
khorimoto0db25be2016-12-20 01:29:0544 seed.set_data(data);
45 seed.set_start_time_millis(start_ms);
46 seed.set_end_time_millis(end_ms);
47 return seed;
48}
khorimoto8e3c1852017-04-12 01:06:3249
khorimoto0db25be2016-12-20 01:29:0550} // namespace
51
Kyle Horimoto1bda9652018-02-14 20:21:0252class CryptAuthLocalDeviceDataProviderTest : public testing::Test {
khorimoto0db25be2016-12-20 01:29:0553 protected:
Kyle Horimoto1bda9652018-02-14 20:21:0254 CryptAuthLocalDeviceDataProviderTest() {
khorimoto0db25be2016-12-20 01:29:0555 fake_beacon_seeds_.push_back(CreateBeaconSeed(
56 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs));
57 fake_beacon_seeds_.push_back(CreateBeaconSeed(
58 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs));
59
60 // Has no public key and no BeaconSeeds.
khorimoto25e8d1b2017-06-23 00:57:1361 ExternalDeviceInfo synced_device1;
khorimoto0db25be2016-12-20 01:29:0562 fake_synced_devices_.push_back(synced_device1);
63
64 // Has no public key and some BeaconSeeds.
khorimoto25e8d1b2017-06-23 00:57:1365 ExternalDeviceInfo synced_device2;
khorimoto0db25be2016-12-20 01:29:0566 synced_device2.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
67 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs));
68 synced_device2.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
69 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs));
70 fake_synced_devices_.push_back(synced_device2);
71
72 // Has another different public key and no BeaconSeeds.
khorimoto25e8d1b2017-06-23 00:57:1373 ExternalDeviceInfo synced_device3;
khorimoto0db25be2016-12-20 01:29:0574 synced_device3.set_public_key("anotherPublicKey");
75 fake_synced_devices_.push_back(synced_device3);
76
77 // Has different public key and BeaconSeeds.
khorimoto25e8d1b2017-06-23 00:57:1378 ExternalDeviceInfo synced_device4;
khorimoto0db25be2016-12-20 01:29:0579 synced_device4.set_public_key("otherPublicKey");
80 synced_device4.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
81 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs));
82 synced_device4.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
83 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs));
84 fake_synced_devices_.push_back(synced_device4);
85
86 // Has public key but no BeaconSeeds.
khorimoto25e8d1b2017-06-23 00:57:1387 ExternalDeviceInfo synced_device5;
khorimoto0db25be2016-12-20 01:29:0588 synced_device5.set_public_key(kDefaultPublicKey);
89 fake_synced_devices_.push_back(synced_device5);
90
91 // Has public key and BeaconSeeds.
khorimoto25e8d1b2017-06-23 00:57:1392 ExternalDeviceInfo synced_device6;
khorimoto0db25be2016-12-20 01:29:0593 synced_device6.set_public_key(kDefaultPublicKey);
94 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
95 kBeaconSeed1Data, kBeaconSeed1StartMs, kBeaconSeed1EndMs));
96 synced_device6.add_beacon_seeds()->CopyFrom(CreateBeaconSeed(
97 kBeaconSeed2Data, kBeaconSeed2StartMs, kBeaconSeed2EndMs));
98 fake_synced_devices_.push_back(synced_device6);
99 }
100
101 void SetUp() override {
Kyle Horimoto3cb4b2b2018-02-14 18:07:18102 fake_device_manager_ = std::make_unique<FakeCryptAuthDeviceManager>();
Kyle Horimoto1bda9652018-02-14 20:21:02103 fake_enrollment_manager_ =
104 std::make_unique<FakeCryptAuthEnrollmentManager>();
khorimoto8e3c1852017-04-12 01:06:32105
Gyuyoung Kim6afb5082018-01-19 13:35:57106 fake_cryptauth_service_ = std::make_unique<FakeCryptAuthService>();
khorimoto8e3c1852017-04-12 01:06:32107 fake_cryptauth_service_->set_cryptauth_device_manager(
Kyle Horimoto3cb4b2b2018-02-14 18:07:18108 fake_device_manager_.get());
khorimoto8e3c1852017-04-12 01:06:32109 fake_cryptauth_service_->set_cryptauth_enrollment_manager(
Kyle Horimoto1bda9652018-02-14 20:21:02110 fake_enrollment_manager_.get());
khorimoto0db25be2016-12-20 01:29:05111
112 provider_ = base::WrapUnique(
khorimoto8e3c1852017-04-12 01:06:32113 new LocalDeviceDataProvider(fake_cryptauth_service_.get()));
khorimoto0db25be2016-12-20 01:29:05114 }
115
khorimoto25e8d1b2017-06-23 00:57:13116 std::vector<BeaconSeed> fake_beacon_seeds_;
117 std::vector<ExternalDeviceInfo> fake_synced_devices_;
khorimoto0db25be2016-12-20 01:29:05118
Kyle Horimoto3cb4b2b2018-02-14 18:07:18119 std::unique_ptr<FakeCryptAuthDeviceManager> fake_device_manager_;
Kyle Horimoto1bda9652018-02-14 20:21:02120 std::unique_ptr<FakeCryptAuthEnrollmentManager> fake_enrollment_manager_;
khorimoto25e8d1b2017-06-23 00:57:13121 std::unique_ptr<FakeCryptAuthService> fake_cryptauth_service_;
khorimoto8e3c1852017-04-12 01:06:32122
123 std::unique_ptr<LocalDeviceDataProvider> provider_;
124
khorimoto0db25be2016-12-20 01:29:05125 private:
Kyle Horimoto1bda9652018-02-14 20:21:02126 DISALLOW_COPY_AND_ASSIGN(CryptAuthLocalDeviceDataProviderTest);
khorimoto0db25be2016-12-20 01:29:05127};
128
Kyle Horimoto1bda9652018-02-14 20:21:02129TEST_F(CryptAuthLocalDeviceDataProviderTest,
130 TestGetLocalDeviceData_NoPublicKey) {
131 fake_enrollment_manager_->set_user_public_key(std::string());
Kyle Horimoto3cb4b2b2018-02-14 18:07:18132 fake_device_manager_->set_synced_devices(fake_synced_devices_);
khorimoto0db25be2016-12-20 01:29:05133
134 std::string public_key;
khorimoto25e8d1b2017-06-23 00:57:13135 std::vector<BeaconSeed> beacon_seeds;
khorimoto0db25be2016-12-20 01:29:05136
137 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
138}
139
Kyle Horimoto1bda9652018-02-14 20:21:02140TEST_F(CryptAuthLocalDeviceDataProviderTest,
141 TestGetLocalDeviceData_NoSyncedDevices) {
142 fake_enrollment_manager_->set_user_public_key(kDefaultPublicKey);
khorimoto0db25be2016-12-20 01:29:05143
144 std::string public_key;
khorimoto25e8d1b2017-06-23 00:57:13145 std::vector<BeaconSeed> beacon_seeds;
khorimoto0db25be2016-12-20 01:29:05146
147 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
148}
149
Kyle Horimoto1bda9652018-02-14 20:21:02150TEST_F(CryptAuthLocalDeviceDataProviderTest,
khorimoto0db25be2016-12-20 01:29:05151 TestGetLocalDeviceData_NoSyncedDeviceMatchingPublicKey) {
Kyle Horimoto1bda9652018-02-14 20:21:02152 fake_enrollment_manager_->set_user_public_key(kDefaultPublicKey);
Kyle Horimoto3cb4b2b2018-02-14 18:07:18153 fake_device_manager_->set_synced_devices(std::vector<ExternalDeviceInfo>{
154 fake_synced_devices_[0], fake_synced_devices_[1], fake_synced_devices_[2],
155 fake_synced_devices_[3]});
khorimoto0db25be2016-12-20 01:29:05156
157 std::string public_key;
khorimoto25e8d1b2017-06-23 00:57:13158 std::vector<BeaconSeed> beacon_seeds;
khorimoto0db25be2016-12-20 01:29:05159
160 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
161}
162
Kyle Horimoto1bda9652018-02-14 20:21:02163TEST_F(CryptAuthLocalDeviceDataProviderTest,
khorimoto0db25be2016-12-20 01:29:05164 TestGetLocalDeviceData_SyncedDeviceIncludesPublicKeyButNoBeaconSeeds) {
Kyle Horimoto1bda9652018-02-14 20:21:02165 fake_enrollment_manager_->set_user_public_key(kDefaultPublicKey);
Kyle Horimoto3cb4b2b2018-02-14 18:07:18166 fake_device_manager_->synced_devices().push_back(fake_synced_devices_[4]);
khorimoto0db25be2016-12-20 01:29:05167
168 std::string public_key;
khorimoto25e8d1b2017-06-23 00:57:13169 std::vector<BeaconSeed> beacon_seeds;
khorimoto0db25be2016-12-20 01:29:05170
171 EXPECT_FALSE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
172}
173
Kyle Horimoto1bda9652018-02-14 20:21:02174TEST_F(CryptAuthLocalDeviceDataProviderTest, TestGetLocalDeviceData_Success) {
175 fake_enrollment_manager_->set_user_public_key(kDefaultPublicKey);
Kyle Horimoto3cb4b2b2018-02-14 18:07:18176 fake_device_manager_->set_synced_devices(fake_synced_devices_);
khorimoto0db25be2016-12-20 01:29:05177
178 std::string public_key;
khorimoto25e8d1b2017-06-23 00:57:13179 std::vector<BeaconSeed> beacon_seeds;
khorimoto0db25be2016-12-20 01:29:05180
181 EXPECT_TRUE(provider_->GetLocalDeviceData(&public_key, &beacon_seeds));
182
183 EXPECT_EQ(kDefaultPublicKey, public_key);
184
185 ASSERT_EQ(fake_beacon_seeds_.size(), beacon_seeds.size());
186 for (size_t i = 0; i < fake_beacon_seeds_.size(); i++) {
187 // Note: google::protobuf::util::MessageDifferencer can only be used to diff
188 // Message, but BeaconSeed derives from the incompatible MessageLite class.
khorimoto25e8d1b2017-06-23 00:57:13189 BeaconSeed expected = fake_beacon_seeds_[i];
190 BeaconSeed actual = beacon_seeds[i];
khorimoto0db25be2016-12-20 01:29:05191 EXPECT_EQ(expected.data(), actual.data());
192 EXPECT_EQ(expected.start_time_millis(), actual.start_time_millis());
193 EXPECT_EQ(expected.end_time_millis(), actual.end_time_millis());
194 }
195}
196
khorimoto25e8d1b2017-06-23 00:57:13197} // namespace cryptauth