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 402 403 404 405 406 407 408 409 410 411 412
|
// Copyright 2014 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/sync/engine/get_updates_processor.h"
#include <stdint.h>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "components/sync/engine/cycle/debug_info_getter.h"
#include "components/sync/engine/cycle/nudge_tracker.h"
#include "components/sync/engine/cycle/status_controller.h"
#include "components/sync/engine/get_updates_delegate.h"
#include "components/sync/engine/update_handler.h"
#include "components/sync/protocol/data_type_progress_marker.pb.h"
#include "components/sync/protocol/sync.pb.h"
#include "components/sync/test/data_type_test_util.h"
#include "components/sync/test/mock_debug_info_getter.h"
#include "components/sync/test/mock_invalidation.h"
#include "components/sync/test/mock_update_handler.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
// A test fixture for tests exercising download updates functions.
class GetUpdatesProcessorBaseTest : public ::testing::Test {
public:
GetUpdatesProcessorBaseTest() = default;
GetUpdatesProcessorBaseTest(const GetUpdatesProcessorBaseTest&) = delete;
GetUpdatesProcessorBaseTest& operator=(const GetUpdatesProcessorBaseTest&) =
delete;
DataTypeSet enabled_types() { return enabled_types_; }
std::unique_ptr<GetUpdatesProcessor> BuildGetUpdatesProcessor(
const GetUpdatesDelegate& delegate) {
return std::make_unique<GetUpdatesProcessor>(&update_handler_map_,
delegate);
}
void InitFakeUpdateResponse(sync_pb::GetUpdatesResponse* response) {
DataTypeSet types = enabled_types();
for (DataType type : types) {
sync_pb::DataTypeProgressMarker* marker =
response->add_new_progress_marker();
marker->set_data_type_id(GetSpecificsFieldNumberFromDataType(type));
marker->set_token("foobarbaz");
sync_pb::DataTypeContext* context = response->add_context_mutations();
context->set_data_type_id(GetSpecificsFieldNumberFromDataType(type));
context->set_version(1);
context->set_context("context");
}
response->set_changes_remaining(0);
}
MockUpdateHandler* AddUpdateHandler(DataType type) {
enabled_types_.Put(type);
std::unique_ptr<MockUpdateHandler> handler =
std::make_unique<MockUpdateHandler>(type);
MockUpdateHandler* handler_ptr = handler.get();
update_handler_map_.insert(std::make_pair(type, handler_ptr));
update_handlers_.insert(std::move(handler));
return handler_ptr;
}
const base::TimeTicks kTestStartTime = base::TimeTicks::Now();
private:
DataTypeSet enabled_types_;
std::set<std::unique_ptr<MockUpdateHandler>> update_handlers_;
UpdateHandlerMap update_handler_map_;
std::unique_ptr<GetUpdatesProcessor> get_updates_processor_;
};
class GetUpdatesProcessorTest : public GetUpdatesProcessorBaseTest {
public:
MockUpdateHandler* GetBookmarksHandler() { return bookmarks_handler_; }
MockUpdateHandler* GetAutofillHandler() { return autofill_handler_; }
MockUpdateHandler* GetPreferencesHandler() { return preferences_handler_; }
private:
const raw_ptr<MockUpdateHandler> bookmarks_handler_ =
AddUpdateHandler(BOOKMARKS);
const raw_ptr<MockUpdateHandler> autofill_handler_ =
AddUpdateHandler(AUTOFILL);
const raw_ptr<MockUpdateHandler> preferences_handler_ =
AddUpdateHandler(PREFERENCES);
};
// Basic test to make sure nudges are expressed properly in the request.
TEST_F(GetUpdatesProcessorTest, BookmarkNudge) {
NudgeTracker nudge_tracker;
nudge_tracker.RecordLocalChange(BOOKMARKS, false);
sync_pb::ClientToServerMessage message;
NormalGetUpdatesDelegate normal_delegate(nudge_tracker);
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(normal_delegate));
processor->PrepareGetUpdates(enabled_types(), &message);
const sync_pb::GetUpdatesMessage& gu_msg = message.get_updates();
EXPECT_EQ(sync_pb::SyncEnums::GU_TRIGGER, gu_msg.get_updates_origin());
for (int i = 0; i < gu_msg.from_progress_marker_size(); ++i) {
DataType type = GetDataTypeFromSpecificsFieldNumber(
gu_msg.from_progress_marker(i).data_type_id());
const sync_pb::DataTypeProgressMarker& progress_marker =
gu_msg.from_progress_marker(i);
const sync_pb::GetUpdateTriggers& gu_trigger =
progress_marker.get_update_triggers();
// We perform some basic tests of GU trigger and source fields here. The
// more complicated scenarios are tested by the NudgeTracker tests.
if (type == BOOKMARKS) {
EXPECT_EQ(1, gu_trigger.local_modification_nudges());
EXPECT_EQ(0, gu_trigger.datatype_refresh_nudges());
} else {
EXPECT_EQ(0, gu_trigger.local_modification_nudges());
EXPECT_EQ(0, gu_trigger.datatype_refresh_nudges());
}
}
}
// Basic test to ensure invalidation payloads are expressed in the
// NormalDelegate requests.
TEST_F(GetUpdatesProcessorTest, NotifyNormalDelegate) {
MockUpdateHandler* autofill_handler = GetAutofillHandler();
MockUpdateHandler* bookmarks_handler = GetBookmarksHandler();
MockUpdateHandler* preferences_handler = GetPreferencesHandler();
DataTypeSet notified_types;
notified_types.Put(AUTOFILL);
notified_types.Put(BOOKMARKS);
notified_types.Put(PREFERENCES);
NudgeTracker nudge_tracker;
sync_pb::ClientToServerMessage message;
NormalGetUpdatesDelegate normal_delegate(nudge_tracker);
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(normal_delegate));
processor->PrepareGetUpdates(enabled_types(), &message);
const sync_pb::GetUpdatesMessage& gu_msg = message.get_updates();
EXPECT_EQ(sync_pb::SyncEnums::GU_TRIGGER, gu_msg.get_updates_origin());
EXPECT_EQ(1, autofill_handler->GetPrepareGetUpdatesCount());
EXPECT_EQ(1, bookmarks_handler->GetPrepareGetUpdatesCount());
EXPECT_EQ(1, preferences_handler->GetPrepareGetUpdatesCount());
}
// Basic test to ensure invalidation payloads are not expressed in
// ConfigureDelegate requests.
TEST_F(GetUpdatesProcessorTest, NotifyConfigureDelegate) {
MockUpdateHandler* autofill_handler = GetAutofillHandler();
MockUpdateHandler* bookmarks_handler = GetBookmarksHandler();
MockUpdateHandler* preferences_handler = GetPreferencesHandler();
DataTypeSet notified_types;
notified_types.Put(AUTOFILL);
notified_types.Put(BOOKMARKS);
notified_types.Put(PREFERENCES);
sync_pb::ClientToServerMessage message;
ConfigureGetUpdatesDelegate configure_delegate(
sync_pb::SyncEnums::RECONFIGURATION);
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(configure_delegate));
processor->PrepareGetUpdates(enabled_types(), &message);
EXPECT_EQ(0, autofill_handler->GetPrepareGetUpdatesCount());
EXPECT_EQ(0, bookmarks_handler->GetPrepareGetUpdatesCount());
EXPECT_EQ(0, preferences_handler->GetPrepareGetUpdatesCount());
}
// Basic test to ensure invalidation payloads are not expressed in
// PollGetUpdatesDelegate requests.
TEST_F(GetUpdatesProcessorTest, NotifyPollGetUpdatesDelegate) {
MockUpdateHandler* autofill_handler = GetAutofillHandler();
MockUpdateHandler* bookmarks_handler = GetBookmarksHandler();
MockUpdateHandler* preferences_handler = GetPreferencesHandler();
DataTypeSet notified_types;
notified_types.Put(AUTOFILL);
notified_types.Put(BOOKMARKS);
notified_types.Put(PREFERENCES);
sync_pb::ClientToServerMessage message;
PollGetUpdatesDelegate poll_delegate;
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(poll_delegate));
processor->PrepareGetUpdates(enabled_types(), &message);
EXPECT_EQ(0, autofill_handler->GetPrepareGetUpdatesCount());
EXPECT_EQ(0, bookmarks_handler->GetPrepareGetUpdatesCount());
EXPECT_EQ(0, preferences_handler->GetPrepareGetUpdatesCount());
}
// Basic test to ensure initial sync requests are expressed in the request.
TEST_F(GetUpdatesProcessorTest, InitialSyncRequest) {
NudgeTracker nudge_tracker;
nudge_tracker.RecordInitialSyncRequired(AUTOFILL);
nudge_tracker.RecordInitialSyncRequired(PREFERENCES);
const DataTypeSet initial_sync_types = {AUTOFILL, PREFERENCES};
sync_pb::ClientToServerMessage message;
NormalGetUpdatesDelegate normal_delegate(nudge_tracker);
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(normal_delegate));
processor->PrepareGetUpdates(enabled_types(), &message);
const sync_pb::GetUpdatesMessage& gu_msg = message.get_updates();
EXPECT_EQ(sync_pb::SyncEnums::GU_TRIGGER, gu_msg.get_updates_origin());
for (int i = 0; i < gu_msg.from_progress_marker_size(); ++i) {
DataType type = GetDataTypeFromSpecificsFieldNumber(
gu_msg.from_progress_marker(i).data_type_id());
const sync_pb::DataTypeProgressMarker& progress_marker =
gu_msg.from_progress_marker(i);
const sync_pb::GetUpdateTriggers& gu_trigger =
progress_marker.get_update_triggers();
// We perform some basic tests of GU trigger and source fields here. The
// more complicated scenarios are tested by the NudgeTracker tests.
if (initial_sync_types.Has(type)) {
EXPECT_TRUE(gu_trigger.initial_sync_in_progress());
} else {
EXPECT_TRUE(gu_trigger.has_initial_sync_in_progress());
EXPECT_FALSE(gu_trigger.initial_sync_in_progress());
}
}
}
TEST_F(GetUpdatesProcessorTest, ConfigureTest) {
sync_pb::ClientToServerMessage message;
ConfigureGetUpdatesDelegate configure_delegate(
sync_pb::SyncEnums::RECONFIGURATION);
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(configure_delegate));
processor->PrepareGetUpdates(enabled_types(), &message);
const sync_pb::GetUpdatesMessage& gu_msg = message.get_updates();
EXPECT_EQ(sync_pb::SyncEnums::RECONFIGURATION, gu_msg.get_updates_origin());
DataTypeSet progress_types;
for (int i = 0; i < gu_msg.from_progress_marker_size(); ++i) {
DataType type = GetDataTypeFromSpecificsFieldNumber(
gu_msg.from_progress_marker(i).data_type_id());
progress_types.Put(type);
}
EXPECT_EQ(enabled_types(), progress_types);
}
TEST_F(GetUpdatesProcessorTest, PollTest) {
sync_pb::ClientToServerMessage message;
PollGetUpdatesDelegate poll_delegate;
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(poll_delegate));
processor->PrepareGetUpdates(enabled_types(), &message);
const sync_pb::GetUpdatesMessage& gu_msg = message.get_updates();
EXPECT_EQ(sync_pb::SyncEnums::PERIODIC, gu_msg.get_updates_origin());
DataTypeSet progress_types;
for (int i = 0; i < gu_msg.from_progress_marker_size(); ++i) {
DataType type = GetDataTypeFromSpecificsFieldNumber(
gu_msg.from_progress_marker(i).data_type_id());
progress_types.Put(type);
}
EXPECT_EQ(enabled_types(), progress_types);
}
// Verify that a bogus response message is detected.
TEST_F(GetUpdatesProcessorTest, InvalidResponse) {
sync_pb::GetUpdatesResponse gu_response;
InitFakeUpdateResponse(&gu_response);
// This field is essential for making the client stop looping. If it's unset
// then something is very wrong. The client should detect this.
gu_response.clear_changes_remaining();
NudgeTracker nudge_tracker;
NormalGetUpdatesDelegate normal_delegate(nudge_tracker);
StatusController status;
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(normal_delegate));
SyncerError error =
processor->ProcessResponse(gu_response, enabled_types(), &status);
EXPECT_EQ(error.type(), SyncerError::Type::kProtocolViolationError);
}
// Verify that we correctly detect when there's more work to be done.
TEST_F(GetUpdatesProcessorTest, MoreToDownloadResponse) {
sync_pb::GetUpdatesResponse gu_response;
InitFakeUpdateResponse(&gu_response);
gu_response.set_changes_remaining(1);
NudgeTracker nudge_tracker;
NormalGetUpdatesDelegate normal_delegate(nudge_tracker);
StatusController status;
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(normal_delegate));
processor->ProcessResponse(gu_response, enabled_types(), &status);
EXPECT_TRUE(processor->HasMoreUpdatesToDownload());
}
// A simple scenario: No updates returned and nothing more to download.
TEST_F(GetUpdatesProcessorTest, NormalResponseTest) {
sync_pb::GetUpdatesResponse gu_response;
InitFakeUpdateResponse(&gu_response);
gu_response.set_changes_remaining(0);
NudgeTracker nudge_tracker;
NormalGetUpdatesDelegate normal_delegate(nudge_tracker);
StatusController status;
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(normal_delegate));
SyncerError error =
processor->ProcessResponse(gu_response, enabled_types(), &status);
EXPECT_EQ(error.type(), SyncerError::Type::kSuccess);
}
// Variant of GetUpdatesProcessor test designed to test update application.
//
// Maintains two enabled types, but requests that updates be applied for only
// one of them.
class GetUpdatesProcessorApplyUpdatesTest : public GetUpdatesProcessorBaseTest {
public:
GetUpdatesProcessorApplyUpdatesTest() = default;
~GetUpdatesProcessorApplyUpdatesTest() override = default;
DataTypeSet GetGuTypes() { return {AUTOFILL}; }
MockUpdateHandler* GetNonAppliedHandler() { return bookmarks_handler_; }
MockUpdateHandler* GetAppliedHandler() { return autofill_handler_; }
private:
const raw_ptr<MockUpdateHandler> bookmarks_handler_ =
AddUpdateHandler(BOOKMARKS);
const raw_ptr<MockUpdateHandler> autofill_handler_ =
AddUpdateHandler(AUTOFILL);
};
// Verify that a normal cycle applies updates to the specified types.
TEST_F(GetUpdatesProcessorApplyUpdatesTest, Normal) {
NudgeTracker nudge_tracker;
NormalGetUpdatesDelegate normal_delegate(nudge_tracker);
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(normal_delegate));
EXPECT_EQ(0, GetNonAppliedHandler()->GetApplyUpdatesCount());
EXPECT_EQ(0, GetAppliedHandler()->GetApplyUpdatesCount());
StatusController status;
processor->ApplyUpdates(GetGuTypes(), /*data_types_with_failure=*/{},
&status);
EXPECT_EQ(0, GetNonAppliedHandler()->GetApplyUpdatesCount());
EXPECT_EQ(1, GetAppliedHandler()->GetApplyUpdatesCount());
}
// Verify that a configure cycle applies updates to the specified types.
TEST_F(GetUpdatesProcessorApplyUpdatesTest, Configure) {
ConfigureGetUpdatesDelegate configure_delegate(
sync_pb::SyncEnums::RECONFIGURATION);
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(configure_delegate));
EXPECT_EQ(0, GetNonAppliedHandler()->GetApplyUpdatesCount());
EXPECT_EQ(0, GetAppliedHandler()->GetApplyUpdatesCount());
StatusController status;
processor->ApplyUpdates(GetGuTypes(), /*data_types_with_failure=*/{},
&status);
EXPECT_EQ(0, GetNonAppliedHandler()->GetApplyUpdatesCount());
EXPECT_EQ(1, GetAppliedHandler()->GetApplyUpdatesCount());
}
// Verify that a poll cycle applies updates to the specified types.
TEST_F(GetUpdatesProcessorApplyUpdatesTest, Poll) {
PollGetUpdatesDelegate poll_delegate;
std::unique_ptr<GetUpdatesProcessor> processor(
BuildGetUpdatesProcessor(poll_delegate));
EXPECT_EQ(0, GetNonAppliedHandler()->GetApplyUpdatesCount());
EXPECT_EQ(0, GetAppliedHandler()->GetApplyUpdatesCount());
StatusController status;
processor->ApplyUpdates(GetGuTypes(), /*data_types_with_failure=*/{},
&status);
EXPECT_EQ(0, GetNonAppliedHandler()->GetApplyUpdatesCount());
EXPECT_EQ(1, GetAppliedHandler()->GetApplyUpdatesCount());
}
} // namespace syncer
|