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
|
// Copyright 2012 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/sync_manager_impl.h"
#include <cstddef>
#include <utility>
#include "base/compiler_specific.h"
#include "base/files/scoped_temp_dir.h"
#include "base/format_macros.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gmock_move_support.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "components/sync/base/client_tag_hash.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/extensions_activity.h"
#include "components/sync/engine/cancelation_signal.h"
#include "components/sync/engine/cycle/sync_cycle.h"
#include "components/sync/engine/events/protocol_event.h"
#include "components/sync/engine/net/http_post_provider.h"
#include "components/sync/engine/net/http_post_provider_factory.h"
#include "components/sync/engine/nigori/key_derivation_params.h"
#include "components/sync/engine/polling_constants.h"
#include "components/sync/engine/sync_scheduler.h"
#include "components/sync/protocol/encryption.pb.h"
#include "components/sync/protocol/proto_value_conversions.h"
#include "components/sync/protocol/sync_enums.pb.h"
#include "components/sync/test/data_type_test_util.h"
#include "components/sync/test/fake_sync_encryption_handler.h"
#include "components/sync/test/fake_sync_scheduler.h"
#include "components/sync/test/test_engine_components_factory.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
#include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "url/gurl.h"
using testing::_;
using testing::SaveArg;
using testing::Sequence;
using testing::StrictMock;
namespace syncer {
namespace {
class TestHttpPostProvider : public HttpPostProvider {
public:
void SetExtraRequestHeaders(const char* headers) override {}
void SetURL(const GURL& url) override {}
void SetPostPayload(const char* content_type,
int content_length,
const char* content) override {}
bool MakeSynchronousPost(int* net_error_code,
int* http_status_code) override {
return false;
}
int GetResponseContentLength() const override { return 0; }
const char* GetResponseContent() const override { return ""; }
const std::string GetResponseHeaderValue(
const std::string& name) const override {
return std::string();
}
void Abort() override {}
private:
~TestHttpPostProvider() override = default;
};
class TestHttpPostProviderFactory : public HttpPostProviderFactory {
public:
~TestHttpPostProviderFactory() override = default;
scoped_refptr<HttpPostProvider> Create() override {
return new TestHttpPostProvider();
}
};
class SyncManagerObserverMock : public SyncManager::Observer {
public:
MOCK_METHOD(void,
OnSyncCycleCompleted,
(const SyncCycleSnapshot&),
(override));
MOCK_METHOD(void, OnConnectionStatusChange, (ConnectionStatus), (override));
MOCK_METHOD(void,
OnActionableProtocolError,
(const SyncProtocolError&),
(override));
MOCK_METHOD(void, OnMigrationRequested, (DataTypeSet), (override));
MOCK_METHOD(void, OnProtocolEvent, (const ProtocolEvent&), (override));
MOCK_METHOD(void, OnSyncStatusChanged, (const SyncStatus&), (override));
};
class SyncEncryptionHandlerObserverMock
: public SyncEncryptionHandler::Observer {
public:
MOCK_METHOD(void,
OnPassphraseRequired,
(const KeyDerivationParams&, const sync_pb::EncryptedData&),
(override));
MOCK_METHOD(void, OnPassphraseAccepted, (), (override));
MOCK_METHOD(void, OnTrustedVaultKeyRequired, (), (override));
MOCK_METHOD(void, OnTrustedVaultKeyAccepted, (), (override));
MOCK_METHOD(void, OnEncryptedTypesChanged, (DataTypeSet, bool), (override));
MOCK_METHOD(void,
OnCryptographerStateChanged,
(Cryptographer*, bool),
(override));
MOCK_METHOD(void,
OnPassphraseTypeChanged,
(PassphraseType, base::Time),
(override));
};
class MockSyncScheduler : public FakeSyncScheduler {
public:
MockSyncScheduler() = default;
~MockSyncScheduler() override = default;
MOCK_METHOD(void, Start, (SyncScheduler::Mode, base::Time), (override));
MOCK_METHOD(void,
ScheduleConfiguration,
(sync_pb::SyncEnums::GetUpdatesOrigin origin,
DataTypeSet types_to_download,
base::OnceClosure ready_task),
(override));
MOCK_METHOD(void, SetHasPendingInvalidations, (DataType, bool), (override));
};
class ComponentsFactory : public TestEngineComponentsFactory {
public:
explicit ComponentsFactory(std::unique_ptr<SyncScheduler> scheduler_to_use)
: scheduler_to_use_(std::move(scheduler_to_use)) {}
~ComponentsFactory() override = default;
std::unique_ptr<SyncScheduler> BuildScheduler(
const std::string& name,
SyncCycleContext* context,
CancelationSignal* stop_handle,
bool local_sync_backend_enabled) override {
DCHECK(scheduler_to_use_);
return std::move(scheduler_to_use_);
}
private:
std::unique_ptr<SyncScheduler> scheduler_to_use_;
};
class SyncManagerImplTest : public testing::Test {
protected:
SyncManagerImplTest()
: sync_manager_("Test sync manager",
network::TestNetworkConnectionTracker::GetInstance()) {}
~SyncManagerImplTest() override = default;
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
extensions_activity_ = new ExtensionsActivity();
sync_manager_.AddObserver(&manager_observer_);
// Save raw pointers to the objects that won't be owned by the fixture.
auto encryption_observer =
std::make_unique<StrictMock<SyncEncryptionHandlerObserverMock>>();
encryption_observer_ = encryption_observer.get();
auto scheduler = std::make_unique<MockSyncScheduler>();
scheduler_ = scheduler.get();
// This should be the only method called by the Init() in the observer.
EXPECT_CALL(manager_observer_, OnSyncStatusChanged).Times(2);
SyncManager::InitArgs args;
args.service_url = GURL("https://example.com/");
args.post_factory = std::make_unique<TestHttpPostProviderFactory>();
args.encryption_observer_proxy = std::move(encryption_observer);
args.extensions_activity = extensions_activity_.get();
args.cache_guid = "fake_cache_guid";
args.enable_local_sync_backend = false;
args.local_sync_backend_folder = temp_dir_.GetPath();
args.engine_components_factory =
std::make_unique<ComponentsFactory>(std::move(scheduler));
args.encryption_handler = &encryption_handler_;
args.cancelation_signal = &cancelation_signal_;
args.poll_interval = base::Minutes(60);
sync_manager_.Init(&args);
base::RunLoop().RunUntilIdle();
}
void TearDown() override {
sync_manager_.RemoveObserver(&manager_observer_);
sync_manager_.ShutdownOnSyncThread();
base::RunLoop().RunUntilIdle();
}
SyncManagerImpl* sync_manager() { return &sync_manager_; }
MockSyncScheduler* scheduler() { return scheduler_; }
SyncManagerObserverMock* manager_observer() { return &manager_observer_; }
private:
base::test::SingleThreadTaskEnvironment task_environment_;
base::ScopedTempDir temp_dir_;
scoped_refptr<ExtensionsActivity> extensions_activity_;
FakeSyncEncryptionHandler encryption_handler_;
SyncManagerImpl sync_manager_;
CancelationSignal cancelation_signal_;
StrictMock<SyncManagerObserverMock> manager_observer_;
// Owned by `sync_manager_`.
raw_ptr<StrictMock<SyncEncryptionHandlerObserverMock>> encryption_observer_ =
nullptr;
raw_ptr<MockSyncScheduler, DanglingUntriaged> scheduler_ = nullptr;
};
// Test that the configuration params are properly created and sent to
// ScheduleConfigure. No callback should be invoked.
TEST_F(SyncManagerImplTest, BasicConfiguration) {
const DataTypeSet types_to_download = {BOOKMARKS, PREFERENCES};
base::MockOnceClosure ready_task;
EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE, _));
EXPECT_CALL(*scheduler(),
ScheduleConfiguration(sync_pb::SyncEnums::RECONFIGURATION,
types_to_download, _));
EXPECT_CALL(ready_task, Run).Times(0);
sync_manager()->ConfigureSyncer(
CONFIGURE_REASON_RECONFIGURATION, types_to_download,
SyncManager::SyncFeatureState::ON, ready_task.Get());
}
TEST_F(SyncManagerImplTest, ShouldSetHasPendingInvalidations) {
Sequence s1;
EXPECT_CALL(*scheduler(),
SetHasPendingInvalidations(BOOKMARKS, /*has_invalidation=*/true))
.InSequence(s1);
EXPECT_CALL(*scheduler(),
SetHasPendingInvalidations(BOOKMARKS, /*has_invalidation=*/false))
.InSequence(s1);
SyncStatus status;
EXPECT_CALL(*manager_observer(), OnSyncStatusChanged)
.Times(2)
.WillRepeatedly(SaveArg<0>(&status));
sync_manager()->SetHasPendingInvalidations(
BOOKMARKS, /*has_pending_invalidations=*/true);
EXPECT_EQ(status.invalidated_data_types.size(), 1u);
EXPECT_TRUE(status.invalidated_data_types.Has(BOOKMARKS));
sync_manager()->SetHasPendingInvalidations(
BOOKMARKS, /*has_pending_invalidations=*/false);
EXPECT_TRUE(status.invalidated_data_types.empty());
}
} // namespace
} // namespace syncer
|