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