1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/browser_sync/active_devices_provider_impl.h"
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "base/functional/callback.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/mock_callback.h"
#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "components/sync/base/data_type.h"
#include "components/sync/engine/active_devices_invalidation_info.h"
#include "components/sync/protocol/sync_enums.pb.h"
#include "components/sync_device_info/fake_device_info_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"
using syncer::ActiveDevicesInvalidationInfo;
using syncer::DataTypeSet;
using syncer::DeviceInfo;
using syncer::FakeDeviceInfoTracker;
using testing::Contains;
using testing::IsEmpty;
using testing::SizeIs;
using testing::UnorderedElementsAre;
namespace browser_sync {
namespace {
constexpr int kPulseIntervalMinutes = 60;
std::unique_ptr<DeviceInfo> CreateFakeDeviceInfo(
const std::string& name,
const std::string& fcm_registration_token,
const DataTypeSet& interested_data_types,
base::Time last_updated_timestamp,
const std::string& chrome_version) {
return std::make_unique<syncer::DeviceInfo>(
base::Uuid::GenerateRandomV4().AsLowercaseString(), name, chrome_version,
"user_agent", sync_pb::SyncEnums::TYPE_UNSET,
syncer::DeviceInfo::OsType::kUnknown,
syncer::DeviceInfo::FormFactor::kUnknown, "device_id",
"manufacturer_name", "model_name", "full_hardware_class",
last_updated_timestamp, base::Minutes(kPulseIntervalMinutes),
/*send_tab_to_self_receiving_enabled=*/
false,
/*send_tab_to_self_receiving_type=*/
sync_pb::
SyncEnums_SendTabReceivingType_SEND_TAB_RECEIVING_TYPE_CHROME_OR_UNSPECIFIED,
/*sharing_info=*/std::nullopt, /*paask_info=*/std::nullopt,
fcm_registration_token, interested_data_types,
/*floating_workspace_last_signin_timestamp=*/std::nullopt);
}
DataTypeSet DefaultInterestedDataTypes() {
return Difference(syncer::ProtocolTypes(), syncer::CommitOnlyTypes());
}
class ActiveDevicesProviderImplTest : public testing::Test {
public:
ActiveDevicesProviderImplTest()
: active_devices_provider_(&fake_device_info_tracker_, &clock_) {}
~ActiveDevicesProviderImplTest() override = default;
void AddDevice(const std::string& name,
const std::string& fcm_registration_token,
const DataTypeSet& interested_data_types,
base::Time last_updated_timestamp) {
device_list_.push_back(CreateFakeDeviceInfo(
name, fcm_registration_token, interested_data_types,
last_updated_timestamp, "chrome_version"));
fake_device_info_tracker_.Add(device_list_.back().get());
}
void AddDeviceWithoutChromeVersion(const std::string& name,
const std::string& fcm_registration_token,
const DataTypeSet& interested_data_types,
base::Time last_updated_timestamp) {
device_list_.push_back(CreateFakeDeviceInfo(
name, fcm_registration_token, interested_data_types,
last_updated_timestamp, /*chrome_version=*/""));
fake_device_info_tracker_.Add(device_list_.back().get());
}
protected:
std::vector<std::unique_ptr<DeviceInfo>> device_list_;
FakeDeviceInfoTracker fake_device_info_tracker_;
base::SimpleTestClock clock_;
ActiveDevicesProviderImpl active_devices_provider_;
};
TEST_F(ActiveDevicesProviderImplTest, ShouldFilterInactiveDevices) {
AddDevice("local_device_pulse_interval",
/*fcm_registration_token=*/"token_1", DefaultInterestedDataTypes(),
clock_.Now() - base::Minutes(kPulseIntervalMinutes + 1));
// Very old device.
AddDevice("device_inactive", /*fcm_registration_token=*/"token_2",
DefaultInterestedDataTypes(), clock_.Now() - base::Days(100));
// The local device should be considered active due to margin even though the
// device is outside the pulse interval. This is not a single client because
// the device waits to receive self-invalidations (because it has not
// specified a |local_cache_guid|).
const ActiveDevicesInvalidationInfo result_no_guid =
active_devices_provider_.CalculateInvalidationInfo(
/*local_cache_guid=*/std::string());
EXPECT_FALSE(result_no_guid.IsSingleClientForTypes({syncer::BOOKMARKS}));
EXPECT_THAT(result_no_guid.all_fcm_registration_tokens(),
Contains("token_1"));
EXPECT_FALSE(result_no_guid.IsSingleClientWithStandaloneInvalidationsForTypes(
{syncer::BOOKMARKS}));
EXPECT_THAT(result_no_guid.GetFcmRegistrationTokensForInterestedClients(
{syncer::BOOKMARKS}),
Contains("token_1"));
// Should ignore the local device and ignore the old device even if it's
// interested in bookmarks.
ASSERT_THAT(device_list_, SizeIs(2));
const ActiveDevicesInvalidationInfo result_local_guid =
active_devices_provider_.CalculateInvalidationInfo(
device_list_.front()->guid());
EXPECT_TRUE(result_local_guid.IsSingleClientForTypes({syncer::BOOKMARKS}));
EXPECT_TRUE(
result_local_guid.IsSingleClientWithStandaloneInvalidationsForTypes(
{syncer::BOOKMARKS}));
}
TEST_F(ActiveDevicesProviderImplTest, ShouldKeepDevicesWithoutChromeVersion) {
// Add two old devices, one with chrome version and one without.
AddDeviceWithoutChromeVersion("device_without_chrome_version",
/*fcm_registration_token=*/"token_1",
DefaultInterestedDataTypes(),
clock_.Now() - base::Days(100));
AddDevice("device_inactive", /*fcm_registration_token=*/"token_2",
DefaultInterestedDataTypes(), clock_.Now() - base::Days(100));
// The device without chrome version should be considered always active even
// though the device is outside the pulse interval.
const ActiveDevicesInvalidationInfo active_devices_invalidation_info =
active_devices_provider_.CalculateInvalidationInfo(
/*local_cache_guid=*/std::string());
EXPECT_FALSE(active_devices_invalidation_info.IsSingleClientForTypes(
{syncer::BOOKMARKS}));
EXPECT_THAT(active_devices_invalidation_info.all_fcm_registration_tokens(),
Contains("token_1"));
EXPECT_FALSE(active_devices_invalidation_info
.IsSingleClientWithStandaloneInvalidationsForTypes(
{syncer::BOOKMARKS}));
EXPECT_THAT(
active_devices_invalidation_info
.GetFcmRegistrationTokensForInterestedClients({syncer::BOOKMARKS}),
Contains("token_1"));
}
TEST_F(ActiveDevicesProviderImplTest, ShouldReturnIfSingleDeviceByDataType) {
AddDevice("local_device", /*fcm_registration_token=*/"",
DefaultInterestedDataTypes(), clock_.Now());
AddDevice("remote_device", /*fcm_registration_token=*/"",
Difference(DefaultInterestedDataTypes(), {syncer::SESSIONS}),
clock_.Now());
// Remote device has disabled sessions data type and current device should be
// considered as the only client.
const ActiveDevicesInvalidationInfo result_local_guid =
active_devices_provider_.CalculateInvalidationInfo(
device_list_.front()->guid());
EXPECT_TRUE(result_local_guid.IsSingleClientForTypes({syncer::SESSIONS}));
EXPECT_FALSE(result_local_guid.IsSingleClientForTypes({syncer::BOOKMARKS}));
// Standalone invalidations are disabled, hence it should be a single device
// for BOOKMARKS as well.
EXPECT_TRUE(
result_local_guid.IsSingleClientWithStandaloneInvalidationsForTypes(
{syncer::BOOKMARKS}));
}
TEST_F(ActiveDevicesProviderImplTest,
ShouldReturnIfSingleClientWithStandaloneInvalidations) {
AddDevice("local_device", /*fcm_registration_token=*/"token_1",
DefaultInterestedDataTypes(), clock_.Now());
AddDevice("remote_device", /*fcm_registration_token=*/"token_2",
Difference(DefaultInterestedDataTypes(), {syncer::SESSIONS}),
clock_.Now());
// Remote device has disabled sessions data type and current device should be
// considered as the only client.
const ActiveDevicesInvalidationInfo result_local_guid =
active_devices_provider_.CalculateInvalidationInfo(
device_list_.front()->guid());
EXPECT_TRUE(
result_local_guid.IsSingleClientWithStandaloneInvalidationsForTypes(
{syncer::SESSIONS}));
EXPECT_FALSE(
result_local_guid.IsSingleClientWithStandaloneInvalidationsForTypes(
{syncer::BOOKMARKS}));
// Since all the clients with enabled standalone invalidations, single client
// should have the same results.
EXPECT_TRUE(result_local_guid.IsSingleClientForTypes({syncer::SESSIONS}));
EXPECT_FALSE(result_local_guid.IsSingleClientForTypes({syncer::BOOKMARKS}));
}
TEST_F(ActiveDevicesProviderImplTest,
ShouldReturnSingleClientForStandaloneInvalidationsOnly) {
AddDevice("local_device", /*fcm_registration_token=*/"token_1",
DefaultInterestedDataTypes(), clock_.Now());
// A remote device doesn't subscribe to standalone invalidations.
AddDevice("remote_device", /*fcm_registration_token=*/"",
DefaultInterestedDataTypes(), clock_.Now());
const ActiveDevicesInvalidationInfo result =
active_devices_provider_.CalculateInvalidationInfo(
device_list_.front()->guid());
EXPECT_FALSE(result.IsSingleClientForTypes({syncer::BOOKMARKS}));
EXPECT_TRUE(result.IsSingleClientWithStandaloneInvalidationsForTypes(
{syncer::BOOKMARKS}));
}
TEST_F(ActiveDevicesProviderImplTest,
ShouldReturnSingleClientForOldInvalidations) {
// Add only devices with standalone invalidations.
AddDevice("local_device", /*fcm_registration_token=*/"token_1",
DefaultInterestedDataTypes(), clock_.Now());
AddDevice("remote_device", /*fcm_registration_token=*/"token_2",
DefaultInterestedDataTypes(), clock_.Now());
EXPECT_TRUE(
active_devices_provider_
.CalculateInvalidationInfo(device_list_.front()->guid())
.IsSingleClientWithOldInvalidationsForTypes({syncer::BOOKMARKS}));
// Add a remote device subscribed to old invalidatoins.
AddDevice("remote_deivce_2", /*fcm_registration_token=*/"",
DefaultInterestedDataTypes(), clock_.Now());
EXPECT_FALSE(
active_devices_provider_
.CalculateInvalidationInfo(device_list_.front()->guid())
.IsSingleClientWithOldInvalidationsForTypes({syncer::BOOKMARKS}));
}
TEST_F(ActiveDevicesProviderImplTest,
ShouldReturnSingleClientForOldInvalidationsForInterestedDataTypes) {
AddDevice("local_device", /*fcm_registration_token=*/"token_1",
DefaultInterestedDataTypes(), clock_.Now());
// Add a remote device which is not interested in SESSIONS.
AddDevice("remote_device", /*fcm_registration_token=*/"",
Difference(DefaultInterestedDataTypes(), {syncer::SESSIONS}),
clock_.Now());
EXPECT_FALSE(
active_devices_provider_
.CalculateInvalidationInfo(device_list_.front()->guid())
.IsSingleClientWithOldInvalidationsForTypes({syncer::BOOKMARKS}));
EXPECT_TRUE(
active_devices_provider_
.CalculateInvalidationInfo(device_list_.front()->guid())
.IsSingleClientWithOldInvalidationsForTypes({syncer::SESSIONS}));
// Add a remote device which does not support interested data types.
AddDevice("old_remote_device", /*fcm_registration_token=*/"",
/*interested_data_types=*/{}, clock_.Now());
EXPECT_FALSE(
active_devices_provider_
.CalculateInvalidationInfo(device_list_.front()->guid())
.IsSingleClientWithOldInvalidationsForTypes({syncer::BOOKMARKS}));
EXPECT_FALSE(
active_devices_provider_
.CalculateInvalidationInfo(device_list_.front()->guid())
.IsSingleClientWithOldInvalidationsForTypes({syncer::SESSIONS}));
}
TEST_F(ActiveDevicesProviderImplTest, ShouldReturnZeroDevices) {
const ActiveDevicesInvalidationInfo result =
active_devices_provider_.CalculateInvalidationInfo(
/*local_cache_guid=*/std::string());
// If there are no devices at all (including the local device), that means we
// just don't have the device information yet, so we should *not* consider
// this a single-client situation.
EXPECT_THAT(result.all_fcm_registration_tokens(), IsEmpty());
EXPECT_FALSE(result.IsSingleClientForTypes({syncer::BOOKMARKS}));
EXPECT_FALSE(result.IsSingleClientWithStandaloneInvalidationsForTypes(
{syncer::BOOKMARKS}));
EXPECT_FALSE(
result.IsSingleClientWithOldInvalidationsForTypes({syncer::BOOKMARKS}));
EXPECT_THAT(
result.GetFcmRegistrationTokensForInterestedClients({syncer::BOOKMARKS}),
IsEmpty());
}
TEST_F(ActiveDevicesProviderImplTest, ShouldInvokeCallback) {
base::MockCallback<
syncer::ActiveDevicesProvider::ActiveDevicesChangedCallback>
callback;
active_devices_provider_.SetActiveDevicesChangedCallback(callback.Get());
EXPECT_CALL(callback, Run());
active_devices_provider_.OnDeviceInfoChange();
active_devices_provider_.SetActiveDevicesChangedCallback(
base::RepeatingClosure());
}
TEST_F(ActiveDevicesProviderImplTest, ShouldReturnActiveFCMRegistrationTokens) {
AddDevice("device_1", "fcm_token_1", DefaultInterestedDataTypes(),
clock_.Now() - base::Minutes(1));
AddDevice("device_2", "fcm_token_2", DefaultInterestedDataTypes(),
clock_.Now() - base::Minutes(1));
AddDevice("device_inactive", "fcm_token_3", DefaultInterestedDataTypes(),
clock_.Now() - base::Days(100));
ASSERT_EQ(3u, device_list_.size());
const ActiveDevicesInvalidationInfo result_no_guid =
active_devices_provider_.CalculateInvalidationInfo(
/*local_cache_guid=*/std::string());
EXPECT_THAT(result_no_guid.all_fcm_registration_tokens(),
UnorderedElementsAre(device_list_[0]->fcm_registration_token(),
device_list_[1]->fcm_registration_token()));
const ActiveDevicesInvalidationInfo result_local_guid =
active_devices_provider_.CalculateInvalidationInfo(
device_list_[0]->guid());
EXPECT_THAT(result_local_guid.all_fcm_registration_tokens(),
UnorderedElementsAre(device_list_[1]->fcm_registration_token()));
EXPECT_THAT(result_local_guid.GetFcmRegistrationTokensForInterestedClients(
{syncer::BOOKMARKS}),
UnorderedElementsAre(device_list_[1]->fcm_registration_token()));
}
TEST_F(ActiveDevicesProviderImplTest, ShouldReturnEmptyListWhenTooManyDevices) {
// Create many devices to exceed the limit of the list.
const size_t kActiveDevicesNumber = 10;
for (size_t i = 0; i < kActiveDevicesNumber; ++i) {
const std::string device_name = "device_" + base::NumberToString(i);
const std::string fcm_token = "fcm_token_" + device_name;
AddDevice(device_name, fcm_token, DefaultInterestedDataTypes(),
clock_.Now() - base::Minutes(1));
}
ActiveDevicesInvalidationInfo result =
active_devices_provider_.CalculateInvalidationInfo(
/*local_cache_guid=*/std::string());
EXPECT_THAT(result.all_fcm_registration_tokens(), IsEmpty());
EXPECT_FALSE(result.IsSingleClientWithStandaloneInvalidationsForTypes(
{syncer::BOOKMARKS}));
EXPECT_THAT(
result.GetFcmRegistrationTokensForInterestedClients({syncer::BOOKMARKS}),
IsEmpty());
// Double check that all other devices will result in an empty FCM
// registration token list.
AddDevice("extra_device", "extra_token", DefaultInterestedDataTypes(),
clock_.Now());
result = active_devices_provider_.CalculateInvalidationInfo(
/*local_cache_guid=*/std::string());
EXPECT_THAT(result.all_fcm_registration_tokens(), IsEmpty());
EXPECT_FALSE(result.IsSingleClientWithStandaloneInvalidationsForTypes(
{syncer::BOOKMARKS}));
EXPECT_THAT(
result.GetFcmRegistrationTokensForInterestedClients({syncer::BOOKMARKS}),
IsEmpty());
}
TEST_F(ActiveDevicesProviderImplTest,
ShouldReturnFCMRegistrationTokensFromLatestDeviceInfo) {
AddDevice("device_1", "fcm_token", DefaultInterestedDataTypes(),
clock_.Now() - base::Minutes(2));
// A newer device with the same FCM registration token but different
// interested data types.
AddDevice("device_2", "fcm_token",
Difference(DefaultInterestedDataTypes(), {syncer::BOOKMARKS}),
clock_.Now() - base::Minutes(1));
ASSERT_EQ(2u, device_list_.size());
const ActiveDevicesInvalidationInfo result_no_guid =
active_devices_provider_.CalculateInvalidationInfo(
/*local_cache_guid=*/std::string());
EXPECT_THAT(result_no_guid.GetFcmRegistrationTokensForInterestedClients(
{syncer::SESSIONS}),
UnorderedElementsAre(device_list_[1]->fcm_registration_token()));
EXPECT_THAT(result_no_guid.GetFcmRegistrationTokensForInterestedClients(
{syncer::BOOKMARKS}),
IsEmpty());
}
} // namespace
} // namespace browser_sync
|