blob: 28650a68219f2857aa7b890d81d710fb434f4b0b [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Syncer unit tests. Unfortunately a lot of these tests
// are outdated and need to be reworked and updated.
#include <algorithm>
#include <limits>
#include <list>
#include <map>
#include <set>
#include <string>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/string_number_conversions.h"
#include "base/stringprintf.h"
#include "base/time.h"
#include "build/build_config.h"
#include "sync/engine/get_commit_ids_command.h"
#include "sync/engine/net/server_connection_manager.h"
#include "sync/engine/process_updates_command.h"
#include "sync/engine/sync_scheduler_impl.h"
#include "sync/engine/syncer.h"
#include "sync/engine/syncer_proto_util.h"
#include "sync/engine/throttled_data_type_tracker.h"
#include "sync/engine/traffic_recorder.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/internal_api/public/engine/model_safe_worker.h"
#include "sync/protocol/bookmark_specifics.pb.h"
#include "sync/protocol/nigori_specifics.pb.h"
#include "sync/protocol/preference_specifics.pb.h"
#include "sync/protocol/sync.pb.h"
#include "sync/sessions/sync_session_context.h"
#include "sync/syncable/mutable_entry.h"
#include "sync/syncable/nigori_util.h"
#include "sync/syncable/read_transaction.h"
#include "sync/syncable/syncable_util.h"
#include "sync/syncable/write_transaction.h"
#include "sync/test/engine/fake_model_worker.h"
#include "sync/test/engine/mock_connection_manager.h"
#include "sync/test/engine/test_directory_setter_upper.h"
#include "sync/test/engine/test_id_factory.h"
#include "sync/test/engine/test_syncable_utils.h"
#include "sync/test/fake_encryptor.h"
#include "sync/test/fake_extensions_activity_monitor.h"
#include "sync/test/fake_sync_encryption_handler.h"
#include "sync/util/cryptographer.h"
#include "sync/util/time.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::TimeDelta;
using std::map;
using std::multimap;
using std::set;
using std::string;
namespace syncer {
using syncable::BaseTransaction;
using syncable::Blob;
using syncable::CountEntriesWithName;
using syncable::Directory;
using syncable::Entry;
using syncable::GetFirstEntryWithName;
using syncable::GetOnlyEntryWithName;
using syncable::Id;
using syncable::kEncryptedString;
using syncable::MutableEntry;
using syncable::WriteTransaction;
using syncable::BASE_VERSION;
using syncable::CREATE;
using syncable::GET_BY_HANDLE;
using syncable::GET_BY_ID;
using syncable::GET_BY_CLIENT_TAG;
using syncable::GET_BY_SERVER_TAG;
using syncable::ID;
using syncable::IS_DEL;
using syncable::IS_DIR;
using syncable::IS_UNAPPLIED_UPDATE;
using syncable::IS_UNSYNCED;
using syncable::META_HANDLE;
using syncable::MTIME;
using syncable::NEXT_ID;
using syncable::NON_UNIQUE_NAME;
using syncable::PARENT_ID;
using syncable::PREV_ID;
using syncable::BASE_SERVER_SPECIFICS;
using syncable::SERVER_IS_DEL;
using syncable::SERVER_PARENT_ID;
using syncable::SERVER_POSITION_IN_PARENT;
using syncable::SERVER_SPECIFICS;
using syncable::SERVER_VERSION;
using syncable::UNIQUE_CLIENT_TAG;
using syncable::UNIQUE_SERVER_TAG;
using syncable::SPECIFICS;
using syncable::SYNCING;
using syncable::UNITTEST;
using sessions::ConflictProgress;
using sessions::ScopedSetSessionWriteTransaction;
using sessions::StatusController;
using sessions::SyncSessionContext;
using sessions::SyncSession;
class SyncerTest : public testing::Test,
public SyncSession::Delegate,
public SyncEngineEventListener {
protected:
SyncerTest()
: syncer_(NULL),
saw_syncer_event_(false),
traffic_recorder_(0, 0) {
}
// SyncSession::Delegate implementation.
virtual void OnSilencedUntil(const base::TimeTicks& silenced_until) OVERRIDE {
FAIL() << "Should not get silenced.";
}
virtual bool IsSyncingCurrentlySilenced() OVERRIDE {
return false;
}
virtual void OnReceivedLongPollIntervalUpdate(
const base::TimeDelta& new_interval) OVERRIDE {
last_long_poll_interval_received_ = new_interval;
}
virtual void OnReceivedShortPollIntervalUpdate(
const base::TimeDelta& new_interval) OVERRIDE {
last_short_poll_interval_received_ = new_interval;
}
virtual void OnReceivedSessionsCommitDelay(
const base::TimeDelta& new_delay) OVERRIDE {
last_sessions_commit_delay_seconds_ = new_delay;
}
virtual void OnShouldStopSyncingPermanently() OVERRIDE {
}
virtual void OnSyncProtocolError(
const sessions::SyncSessionSnapshot& snapshot) OVERRIDE {
}
void GetWorkers(std::vector<ModelSafeWorker*>* out) {
out->push_back(worker_.get());
}
void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) {
// We're just testing the sync engine here, so we shunt everything to
// the SyncerThread. Datatypes which aren't enabled aren't in the map.
for (ModelTypeSet::Iterator it = enabled_datatypes_.First();
it.Good(); it.Inc()) {
(*out)[it.Get()] = GROUP_PASSIVE;
}
}
virtual void OnSyncEngineEvent(const SyncEngineEvent& event) OVERRIDE {
DVLOG(1) << "HandleSyncEngineEvent in unittest " << event.what_happened;
// we only test for entry-specific events, not status changed ones.
switch (event.what_happened) {
case SyncEngineEvent::SYNC_CYCLE_BEGIN: // Fall through.
case SyncEngineEvent::STATUS_CHANGED:
case SyncEngineEvent::SYNC_CYCLE_ENDED:
return;
default:
CHECK(false) << "Handling unknown error type in unit tests!!";
}
saw_syncer_event_ = true;
}
SyncSession* MakeSession() {
ModelSafeRoutingInfo info;
std::vector<ModelSafeWorker*> workers;
GetModelSafeRoutingInfo(&info);
GetWorkers(&workers);
ModelTypeStateMap types =
ModelSafeRoutingInfoToStateMap(info, std::string());
return new SyncSession(context_.get(), this,
sessions::SyncSourceInfo(sync_pb::GetUpdatesCallerInfo::UNKNOWN, types),
info, workers);
}
bool SyncShareAsDelegate(
SyncSchedulerImpl::SyncSessionJob::SyncSessionJobPurpose purpose) {
SyncerStep start;
SyncerStep end;
SyncSchedulerImpl::SetSyncerStepsForPurpose(purpose, &start, &end);
session_.reset(MakeSession());
syncer_->SyncShare(session_.get(), start, end);
return session_->HasMoreToSync();
}
bool SyncShareNudge() {
session_.reset(MakeSession());
return SyncShareAsDelegate(SyncSchedulerImpl::SyncSessionJob::NUDGE);
}
bool SyncShareConfigure() {
session_.reset(MakeSession());
return SyncShareAsDelegate(
SyncSchedulerImpl::SyncSessionJob::CONFIGURATION);
}
void LoopSyncShare() {
bool should_loop = false;
int loop_iterations = 0;
do {
ASSERT_LT(++loop_iterations, 100) << "infinite loop detected. please fix";
should_loop = SyncShareNudge();
} while (should_loop);
}
virtual void SetUp() {
dir_maker_.SetUp();
mock_server_.reset(new MockConnectionManager(directory()));
EnableDatatype(BOOKMARKS);
EnableDatatype(NIGORI);
EnableDatatype(PREFERENCES);
EnableDatatype(NIGORI);
worker_ = new FakeModelWorker(GROUP_PASSIVE);
std::vector<SyncEngineEventListener*> listeners;
listeners.push_back(this);
ModelSafeRoutingInfo routing_info;
std::vector<ModelSafeWorker*> workers;
GetModelSafeRoutingInfo(&routing_info);
GetWorkers(&workers);
throttled_data_type_tracker_.reset(new ThrottledDataTypeTracker(NULL));
context_.reset(
new SyncSessionContext(
mock_server_.get(), directory(), workers,
&extensions_activity_monitor_, throttled_data_type_tracker_.get(),
listeners, NULL, &traffic_recorder_,
true /* enable keystore encryption */));
context_->set_routing_info(routing_info);
ASSERT_FALSE(context_->resolver());
syncer_ = new Syncer();
session_.reset(MakeSession());
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Directory::ChildHandles children;
directory()->GetChildHandlesById(&trans, trans.root_id(), &children);
ASSERT_EQ(0u, children.size());
saw_syncer_event_ = false;
root_id_ = TestIdFactory::root();
parent_id_ = ids_.MakeServer("parent id");
child_id_ = ids_.MakeServer("child id");
directory()->set_store_birthday(mock_server_->store_birthday());
mock_server_->SetKeystoreKey("encryption_key");
}
virtual void TearDown() {
mock_server_.reset();
delete syncer_;
syncer_ = NULL;
dir_maker_.TearDown();
}
void WriteTestDataToEntry(WriteTransaction* trans, MutableEntry* entry) {
EXPECT_FALSE(entry->Get(IS_DIR));
EXPECT_FALSE(entry->Get(IS_DEL));
sync_pb::EntitySpecifics specifics;
specifics.mutable_bookmark()->set_url("http://demo/");
specifics.mutable_bookmark()->set_favicon("PNG");
entry->Put(syncable::SPECIFICS, specifics);
entry->Put(syncable::IS_UNSYNCED, true);
}
void VerifyTestDataInEntry(BaseTransaction* trans, Entry* entry) {
EXPECT_FALSE(entry->Get(IS_DIR));
EXPECT_FALSE(entry->Get(IS_DEL));
VerifyTestBookmarkDataInEntry(entry);
}
void VerifyTestBookmarkDataInEntry(Entry* entry) {
const sync_pb::EntitySpecifics& specifics = entry->Get(syncable::SPECIFICS);
EXPECT_TRUE(specifics.has_bookmark());
EXPECT_EQ("PNG", specifics.bookmark().favicon());
EXPECT_EQ("http://demo/", specifics.bookmark().url());
}
bool initial_sync_ended_for_type(ModelType type) {
return directory()->initial_sync_ended_for_type(type);
}
void SyncRepeatedlyToTriggerConflictResolution(SyncSession* session) {
// We should trigger after less than 6 syncs, but extra does no harm.
for (int i = 0 ; i < 6 ; ++i)
syncer_->SyncShare(session, SYNCER_BEGIN, SYNCER_END);
}
void SyncRepeatedlyToTriggerStuckSignal(SyncSession* session) {
// We should trigger after less than 10 syncs, but we want to avoid brittle
// tests.
for (int i = 0 ; i < 12 ; ++i)
syncer_->SyncShare(session, SYNCER_BEGIN, SYNCER_END);
}
sync_pb::EntitySpecifics DefaultBookmarkSpecifics() {
sync_pb::EntitySpecifics result;
AddDefaultFieldValue(BOOKMARKS, &result);
return result;
}
sync_pb::EntitySpecifics DefaultPreferencesSpecifics() {
sync_pb::EntitySpecifics result;
AddDefaultFieldValue(PREFERENCES, &result);
return result;
}
// Enumeration of alterations to entries for commit ordering tests.
enum EntryFeature {
LIST_END = 0, // Denotes the end of the list of features from below.
SYNCED, // Items are unsynced by default
DELETED,
OLD_MTIME,
MOVED_FROM_ROOT,
};
struct CommitOrderingTest {
// expected commit index.
int commit_index;
// Details about the item
syncable::Id id;
syncable::Id parent_id;
EntryFeature features[10];
static CommitOrderingTest MakeLastCommitItem() {
CommitOrderingTest last_commit_item;
last_commit_item.commit_index = -1;
last_commit_item.id = TestIdFactory::root();
return last_commit_item;
}
};
void RunCommitOrderingTest(CommitOrderingTest* test) {
map<int, syncable::Id> expected_positions;
{ // Transaction scope.
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
while (!test->id.IsRoot()) {
if (test->commit_index >= 0) {
map<int, syncable::Id>::value_type entry(test->commit_index,
test->id);
bool double_position = !expected_positions.insert(entry).second;
ASSERT_FALSE(double_position) << "Two id's expected at one position";
}
string utf8_name = test->id.GetServerId();
string name(utf8_name.begin(), utf8_name.end());
MutableEntry entry(&trans, CREATE, test->parent_id, name);
entry.Put(syncable::ID, test->id);
if (test->id.ServerKnows()) {
entry.Put(BASE_VERSION, 5);
entry.Put(SERVER_VERSION, 5);
entry.Put(SERVER_PARENT_ID, test->parent_id);
}
entry.Put(syncable::IS_DIR, true);
entry.Put(syncable::IS_UNSYNCED, true);
entry.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
// Set the time to 30 seconds in the future to reduce the chance of
// flaky tests.
const base::Time& now_plus_30s =
base::Time::Now() + base::TimeDelta::FromSeconds(30);
const base::Time& now_minus_2h =
base::Time::Now() - base::TimeDelta::FromHours(2);
entry.Put(syncable::MTIME, now_plus_30s);
for (size_t i = 0 ; i < arraysize(test->features) ; ++i) {
switch (test->features[i]) {
case LIST_END:
break;
case SYNCED:
entry.Put(syncable::IS_UNSYNCED, false);
break;
case DELETED:
entry.Put(syncable::IS_DEL, true);
break;
case OLD_MTIME:
entry.Put(MTIME, now_minus_2h);
break;
case MOVED_FROM_ROOT:
entry.Put(SERVER_PARENT_ID, trans.root_id());
break;
default:
FAIL() << "Bad value in CommitOrderingTest list";
}
}
test++;
}
}
LoopSyncShare();
ASSERT_TRUE(expected_positions.size() ==
mock_server_->committed_ids().size());
// If this test starts failing, be aware other sort orders could be valid.
for (size_t i = 0; i < expected_positions.size(); ++i) {
EXPECT_EQ(1u, expected_positions.count(i));
EXPECT_TRUE(expected_positions[i] == mock_server_->committed_ids()[i]);
}
}
void DoTruncationTest(const vector<int64>& unsynced_handle_view,
const vector<syncable::Id>& expected_id_order) {
for (size_t limit = expected_id_order.size() + 2; limit > 0; --limit) {
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
ScopedSetSessionWriteTransaction set_trans(session_.get(), &wtrans);
ModelSafeRoutingInfo routes;
GetModelSafeRoutingInfo(&routes);
sessions::OrderedCommitSet output_set(routes);
GetCommitIdsCommand command(limit, &output_set);
std::set<int64> ready_unsynced_set;
command.FilterUnreadyEntries(&wtrans, ModelTypeSet(),
ModelTypeSet(), false,
unsynced_handle_view, &ready_unsynced_set);
command.BuildCommitIds(session_->write_transaction(), routes,
ready_unsynced_set);
size_t truncated_size = std::min(limit, expected_id_order.size());
ASSERT_EQ(truncated_size, output_set.Size());
for (size_t i = 0; i < truncated_size; ++i) {
ASSERT_EQ(expected_id_order[i], output_set.GetCommitIdAt(i))
<< "At index " << i << " with batch size limited to " << limit;
}
sessions::OrderedCommitSet::Projection proj;
proj = output_set.GetCommitIdProjection(GROUP_PASSIVE);
ASSERT_EQ(truncated_size, proj.size());
for (size_t i = 0; i < truncated_size; ++i) {
SCOPED_TRACE(::testing::Message("Projection mismatch with i = ") << i);
syncable::Id projected = output_set.GetCommitIdAt(proj[i]);
ASSERT_EQ(expected_id_order[proj[i]], projected);
// Since this projection is the identity, the following holds.
ASSERT_EQ(expected_id_order[i], projected);
}
}
}
const StatusController& status() {
return session_->status_controller();
}
Directory* directory() {
return dir_maker_.directory();
}
int64 CreateUnsyncedDirectory(const string& entry_name,
const string& idstring) {
return CreateUnsyncedDirectory(entry_name,
syncable::Id::CreateFromServerId(idstring));
}
int64 CreateUnsyncedDirectory(const string& entry_name,
const syncable::Id& id) {
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&wtrans, syncable::CREATE, wtrans.root_id(),
entry_name);
EXPECT_TRUE(entry.good());
entry.Put(syncable::IS_UNSYNCED, true);
entry.Put(syncable::IS_DIR, true);
entry.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
entry.Put(syncable::BASE_VERSION, id.ServerKnows() ? 1 : 0);
entry.Put(syncable::ID, id);
return entry.Get(META_HANDLE);
}
void EnableDatatype(ModelType model_type) {
enabled_datatypes_.Put(model_type);
ModelSafeRoutingInfo routing_info;
GetModelSafeRoutingInfo(&routing_info);
if (context_.get()) {
context_->set_routing_info(routing_info);
}
mock_server_->ExpectGetUpdatesRequestTypes(enabled_datatypes_);
}
void DisableDatatype(ModelType model_type) {
enabled_datatypes_.Remove(model_type);
ModelSafeRoutingInfo routing_info;
GetModelSafeRoutingInfo(&routing_info);
if (context_.get()) {
context_->set_routing_info(routing_info);
}
mock_server_->ExpectGetUpdatesRequestTypes(enabled_datatypes_);
}
template<typename FieldType, typename ValueType>
ValueType GetField(int64 metahandle, FieldType field,
ValueType default_value) {
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry entry(&trans, GET_BY_HANDLE, metahandle);
EXPECT_TRUE(entry.good());
if (!entry.good()) {
return default_value;
}
EXPECT_EQ(metahandle, entry.Get(META_HANDLE));
return entry.Get(field);
}
// Helper getters that work without a transaction, to reduce boilerplate.
Id Get(int64 metahandle, syncable::IdField field) {
return GetField(metahandle, field, syncable::GetNullId());
}
string Get(int64 metahandle, syncable::StringField field) {
return GetField(metahandle, field, string());
}
int64 Get(int64 metahandle, syncable::Int64Field field) {
return GetField(metahandle, field, syncable::kInvalidMetaHandle);
}
int64 Get(int64 metahandle, syncable::BaseVersion field) {
const int64 kDefaultValue = -100;
return GetField(metahandle, field, kDefaultValue);
}
bool Get(int64 metahandle, syncable::IndexedBitField field) {
return GetField(metahandle, field, false);
}
bool Get(int64 metahandle, syncable::IsDelField field) {
return GetField(metahandle, field, false);
}
bool Get(int64 metahandle, syncable::BitField field) {
return GetField(metahandle, field, false);
}
Cryptographer* GetCryptographer(syncable::BaseTransaction* trans) {
return directory()->GetCryptographer(trans);
}
MessageLoop message_loop_;
// Some ids to aid tests. Only the root one's value is specific. The rest
// are named for test clarity.
// TODO(chron): Get rid of these inbuilt IDs. They only make it
// more confusing.
syncable::Id root_id_;
syncable::Id parent_id_;
syncable::Id child_id_;
TestIdFactory ids_;
TestDirectorySetterUpper dir_maker_;
FakeEncryptor encryptor_;
FakeExtensionsActivityMonitor extensions_activity_monitor_;
scoped_ptr<ThrottledDataTypeTracker> throttled_data_type_tracker_;
scoped_ptr<MockConnectionManager> mock_server_;
Syncer* syncer_;
scoped_ptr<SyncSession> session_;
scoped_ptr<SyncSessionContext> context_;
bool saw_syncer_event_;
base::TimeDelta last_short_poll_interval_received_;
base::TimeDelta last_long_poll_interval_received_;
base::TimeDelta last_sessions_commit_delay_seconds_;
scoped_refptr<ModelSafeWorker> worker_;
ModelTypeSet enabled_datatypes_;
TrafficRecorder traffic_recorder_;
DISALLOW_COPY_AND_ASSIGN(SyncerTest);
};
TEST_F(SyncerTest, TestCallGatherUnsyncedEntries) {
{
Syncer::UnsyncedMetaHandles handles;
{
syncable::ReadTransaction trans(FROM_HERE, directory());
GetUnsyncedEntries(&trans, &handles);
}
ASSERT_EQ(0u, handles.size());
}
// TODO(sync): When we can dynamically connect and disconnect the mock
// ServerConnectionManager test disconnected GetUnsyncedEntries here. It's a
// regression for a very old bug.
}
TEST_F(SyncerTest, GetCommitIdsCommandTruncates) {
syncable::Id root = ids_.root();
// Create two server entries.
mock_server_->AddUpdateDirectory(ids_.MakeServer("x"), root, "X", 10, 10);
mock_server_->AddUpdateDirectory(ids_.MakeServer("w"), root, "W", 10, 10);
SyncShareNudge();
// Create some new client entries.
CreateUnsyncedDirectory("C", ids_.MakeLocal("c"));
CreateUnsyncedDirectory("B", ids_.MakeLocal("b"));
CreateUnsyncedDirectory("D", ids_.MakeLocal("d"));
CreateUnsyncedDirectory("E", ids_.MakeLocal("e"));
CreateUnsyncedDirectory("J", ids_.MakeLocal("j"));
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry entry_x(&wtrans, GET_BY_ID, ids_.MakeServer("x"));
MutableEntry entry_b(&wtrans, GET_BY_ID, ids_.MakeLocal("b"));
MutableEntry entry_c(&wtrans, GET_BY_ID, ids_.MakeLocal("c"));
MutableEntry entry_d(&wtrans, GET_BY_ID, ids_.MakeLocal("d"));
MutableEntry entry_e(&wtrans, GET_BY_ID, ids_.MakeLocal("e"));
MutableEntry entry_w(&wtrans, GET_BY_ID, ids_.MakeServer("w"));
MutableEntry entry_j(&wtrans, GET_BY_ID, ids_.MakeLocal("j"));
entry_x.Put(IS_UNSYNCED, true);
entry_b.Put(PARENT_ID, entry_x.Get(ID));
entry_d.Put(PARENT_ID, entry_b.Get(ID));
entry_c.Put(PARENT_ID, entry_x.Get(ID));
entry_c.PutPredecessor(entry_b.Get(ID));
entry_e.Put(PARENT_ID, entry_c.Get(ID));
entry_w.PutPredecessor(entry_x.Get(ID));
entry_w.Put(IS_UNSYNCED, true);
entry_w.Put(SERVER_VERSION, 20);
entry_w.Put(IS_UNAPPLIED_UPDATE, true); // Fake a conflict.
entry_j.PutPredecessor(entry_w.Get(ID));
}
// The arrangement is now: x (b (d) c (e)) w j
// Entry "w" is in conflict, making its sucessors unready to commit.
vector<int64> unsynced_handle_view;
vector<syncable::Id> expected_order;
{
syncable::ReadTransaction rtrans(FROM_HERE, directory());
GetUnsyncedEntries(&rtrans, &unsynced_handle_view);
}
// The expected order is "x", "b", "c", "d", "e", truncated appropriately.
expected_order.push_back(ids_.MakeServer("x"));
expected_order.push_back(ids_.MakeLocal("b"));
expected_order.push_back(ids_.MakeLocal("c"));
expected_order.push_back(ids_.MakeLocal("d"));
expected_order.push_back(ids_.MakeLocal("e"));
DoTruncationTest(unsynced_handle_view, expected_order);
}
TEST_F(SyncerTest, GetCommitIdsFiltersThrottledEntries) {
const ModelTypeSet throttled_types(BOOKMARKS);
sync_pb::EntitySpecifics bookmark_data;
AddDefaultFieldValue(BOOKMARKS, &bookmark_data);
mock_server_->AddUpdateDirectory(1, 0, "A", 10, 10);
SyncShareNudge();
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry A(&wtrans, GET_BY_ID, ids_.FromNumber(1));
ASSERT_TRUE(A.good());
A.Put(IS_UNSYNCED, true);
A.Put(SPECIFICS, bookmark_data);
A.Put(NON_UNIQUE_NAME, "bookmark");
}
// Now set the throttled types.
context_->throttled_data_type_tracker()->SetUnthrottleTime(
throttled_types,
base::TimeTicks::Now() + base::TimeDelta::FromSeconds(1200));
SyncShareNudge();
{
// Nothing should have been committed as bookmarks is throttled.
syncable::ReadTransaction rtrans(FROM_HERE, directory());
Entry entryA(&rtrans, syncable::GET_BY_ID, ids_.FromNumber(1));
ASSERT_TRUE(entryA.good());
EXPECT_TRUE(entryA.Get(IS_UNSYNCED));
}
// Now unthrottle.
context_->throttled_data_type_tracker()->SetUnthrottleTime(
throttled_types,
base::TimeTicks::Now() - base::TimeDelta::FromSeconds(1200));
SyncShareNudge();
{
// It should have been committed.
syncable::ReadTransaction rtrans(FROM_HERE, directory());
Entry entryA(&rtrans, syncable::GET_BY_ID, ids_.FromNumber(1));
ASSERT_TRUE(entryA.good());
EXPECT_FALSE(entryA.Get(IS_UNSYNCED));
}
}
// We use a macro so we can preserve the error location.
#define VERIFY_ENTRY(id, is_unapplied, is_unsynced, prev_initialized, \
parent_id, version, server_version, id_fac, rtrans) \
do { \
Entry entryA(rtrans, syncable::GET_BY_ID, id_fac.FromNumber(id)); \
ASSERT_TRUE(entryA.good()); \
/* We don't use EXPECT_EQ here because when the left side param is false,
gcc 4.6 warns about converting 'false' to pointer type for argument 1. */ \
EXPECT_TRUE(is_unsynced == entryA.Get(IS_UNSYNCED)); \
EXPECT_TRUE(is_unapplied == entryA.Get(IS_UNAPPLIED_UPDATE)); \
EXPECT_TRUE(prev_initialized == \
IsRealDataType(GetModelTypeFromSpecifics( \
entryA.Get(BASE_SERVER_SPECIFICS)))); \
EXPECT_TRUE(parent_id == -1 || \
entryA.Get(PARENT_ID) == id_fac.FromNumber(parent_id)); \
EXPECT_EQ(version, entryA.Get(BASE_VERSION)); \
EXPECT_EQ(server_version, entryA.Get(SERVER_VERSION)); \
} while (0)
TEST_F(SyncerTest, GetCommitIdsFiltersUnreadyEntries) {
KeyParams key_params = {"localhost", "dummy", "foobar"};
KeyParams other_params = {"localhost", "dummy", "foobar2"};
sync_pb::EntitySpecifics bookmark, encrypted_bookmark;
bookmark.mutable_bookmark()->set_url("url");
bookmark.mutable_bookmark()->set_title("title");
AddDefaultFieldValue(BOOKMARKS, &encrypted_bookmark);
mock_server_->AddUpdateDirectory(1, 0, "A", 10, 10);
mock_server_->AddUpdateDirectory(2, 0, "B", 10, 10);
mock_server_->AddUpdateDirectory(3, 0, "C", 10, 10);
mock_server_->AddUpdateDirectory(4, 0, "D", 10, 10);
SyncShareNudge();
// Server side change will put A in conflict.
mock_server_->AddUpdateDirectory(1, 0, "A", 20, 20);
{
// Mark bookmarks as encrypted and set the cryptographer to have pending
// keys.
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
Cryptographer other_cryptographer(&encryptor_);
other_cryptographer.AddKey(other_params);
sync_pb::EntitySpecifics specifics;
sync_pb::NigoriSpecifics* nigori = specifics.mutable_nigori();
other_cryptographer.GetKeys(nigori->mutable_encryption_keybag());
dir_maker_.encryption_handler()->EnableEncryptEverything();
// Set up with an old passphrase, but have pending keys
GetCryptographer(&wtrans)->AddKey(key_params);
GetCryptographer(&wtrans)->Encrypt(bookmark,
encrypted_bookmark.mutable_encrypted());
GetCryptographer(&wtrans)->SetPendingKeys(nigori->encryption_keybag());
// In conflict but properly encrypted.
MutableEntry A(&wtrans, GET_BY_ID, ids_.FromNumber(1));
ASSERT_TRUE(A.good());
A.Put(IS_UNSYNCED, true);
A.Put(SPECIFICS, encrypted_bookmark);
A.Put(NON_UNIQUE_NAME, kEncryptedString);
// Not in conflict and properly encrypted.
MutableEntry B(&wtrans, GET_BY_ID, ids_.FromNumber(2));
ASSERT_TRUE(B.good());
B.Put(IS_UNSYNCED, true);
B.Put(SPECIFICS, encrypted_bookmark);
B.Put(NON_UNIQUE_NAME, kEncryptedString);
// Unencrypted specifics.
MutableEntry C(&wtrans, GET_BY_ID, ids_.FromNumber(3));
ASSERT_TRUE(C.good());
C.Put(IS_UNSYNCED, true);
C.Put(NON_UNIQUE_NAME, kEncryptedString);
// Unencrypted non_unique_name.
MutableEntry D(&wtrans, GET_BY_ID, ids_.FromNumber(4));
ASSERT_TRUE(D.good());
D.Put(IS_UNSYNCED, true);
D.Put(SPECIFICS, encrypted_bookmark);
D.Put(NON_UNIQUE_NAME, "not encrypted");
}
SyncShareNudge();
{
// Nothing should have commited due to bookmarks being encrypted and
// the cryptographer having pending keys. A would have been resolved
// as a simple conflict, but still be unsynced until the next sync cycle.
syncable::ReadTransaction rtrans(FROM_HERE, directory());
VERIFY_ENTRY(1, false, true, false, 0, 20, 20, ids_, &rtrans);
VERIFY_ENTRY(2, false, true, false, 0, 10, 10, ids_, &rtrans);
VERIFY_ENTRY(3, false, true, false, 0, 10, 10, ids_, &rtrans);
VERIFY_ENTRY(4, false, true, false, 0, 10, 10, ids_, &rtrans);
// Resolve the pending keys.
GetCryptographer(&rtrans)->DecryptPendingKeys(other_params);
}
SyncShareNudge();
{
// All properly encrypted and non-conflicting items should commit. "A" was
// conflicting, but last sync cycle resolved it as simple conflict, so on
// this sync cycle it committed succesfullly.
syncable::ReadTransaction rtrans(FROM_HERE, directory());
// Committed successfully.
VERIFY_ENTRY(1, false, false, false, 0, 21, 21, ids_, &rtrans);
// Committed successfully.
VERIFY_ENTRY(2, false, false, false, 0, 11, 11, ids_, &rtrans);
// Was not properly encrypted.
VERIFY_ENTRY(3, false, true, false, 0, 10, 10, ids_, &rtrans);
// Was not properly encrypted.
VERIFY_ENTRY(4, false, true, false, 0, 10, 10, ids_, &rtrans);
}
{
// Fix the remaining items.
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry C(&wtrans, GET_BY_ID, ids_.FromNumber(3));
ASSERT_TRUE(C.good());
C.Put(SPECIFICS, encrypted_bookmark);
C.Put(NON_UNIQUE_NAME, kEncryptedString);
MutableEntry D(&wtrans, GET_BY_ID, ids_.FromNumber(4));
ASSERT_TRUE(D.good());
D.Put(SPECIFICS, encrypted_bookmark);
D.Put(NON_UNIQUE_NAME, kEncryptedString);
}
SyncShareNudge();
{
const StatusController& status_controller = session_->status_controller();
// Expect success.
EXPECT_EQ(status_controller.model_neutral_state().commit_result, SYNCER_OK);
// None should be unsynced anymore.
syncable::ReadTransaction rtrans(FROM_HERE, directory());
VERIFY_ENTRY(1, false, false, false, 0, 21, 21, ids_, &rtrans);
VERIFY_ENTRY(2, false, false, false, 0, 11, 11, ids_, &rtrans);
VERIFY_ENTRY(3, false, false, false, 0, 11, 11, ids_, &rtrans);
VERIFY_ENTRY(4, false, false, false, 0, 11, 11, ids_, &rtrans);
}
}
TEST_F(SyncerTest, EncryptionAwareConflicts) {
KeyParams key_params = {"localhost", "dummy", "foobar"};
Cryptographer other_cryptographer(&encryptor_);
other_cryptographer.AddKey(key_params);
sync_pb::EntitySpecifics bookmark, encrypted_bookmark, modified_bookmark;
bookmark.mutable_bookmark()->set_title("title");
other_cryptographer.Encrypt(bookmark,
encrypted_bookmark.mutable_encrypted());
AddDefaultFieldValue(BOOKMARKS, &encrypted_bookmark);
modified_bookmark.mutable_bookmark()->set_title("title2");
other_cryptographer.Encrypt(modified_bookmark,
modified_bookmark.mutable_encrypted());
sync_pb::EntitySpecifics pref, encrypted_pref, modified_pref;
pref.mutable_preference()->set_name("name");
AddDefaultFieldValue(PREFERENCES, &encrypted_pref);
other_cryptographer.Encrypt(pref,
encrypted_pref.mutable_encrypted());
modified_pref.mutable_preference()->set_name("name2");
other_cryptographer.Encrypt(modified_pref,
modified_pref.mutable_encrypted());
{
// Mark bookmarks and preferences as encrypted and set the cryptographer to
// have pending keys.
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
sync_pb::EntitySpecifics specifics;
sync_pb::NigoriSpecifics* nigori = specifics.mutable_nigori();
other_cryptographer.GetKeys(nigori->mutable_encryption_keybag());
dir_maker_.encryption_handler()->EnableEncryptEverything();
GetCryptographer(&wtrans)->SetPendingKeys(nigori->encryption_keybag());
EXPECT_TRUE(GetCryptographer(&wtrans)->has_pending_keys());
}
mock_server_->AddUpdateSpecifics(1, 0, "A", 10, 10, true, 0, bookmark);
mock_server_->AddUpdateSpecifics(2, 1, "B", 10, 10, false, 2, bookmark);
mock_server_->AddUpdateSpecifics(3, 1, "C", 10, 10, false, 1, bookmark);
mock_server_->AddUpdateSpecifics(4, 0, "D", 10, 10, false, 0, pref);
SyncShareNudge();
{
// Initial state. Everything is normal.
syncable::ReadTransaction rtrans(FROM_HERE, directory());
VERIFY_ENTRY(1, false, false, false, 0, 10, 10, ids_, &rtrans);
VERIFY_ENTRY(2, false, false, false, 1, 10, 10, ids_, &rtrans);
VERIFY_ENTRY(3, false, false, false, 1, 10, 10, ids_, &rtrans);
VERIFY_ENTRY(4, false, false, false, 0, 10, 10, ids_, &rtrans);
}
// Server side encryption will not be applied due to undecryptable data.
// At this point, BASE_SERVER_SPECIFICS should be filled for all four items.
mock_server_->AddUpdateSpecifics(1, 0, kEncryptedString, 20, 20, true, 0,
encrypted_bookmark);
mock_server_->AddUpdateSpecifics(2, 1, kEncryptedString, 20, 20, false, 2,
encrypted_bookmark);
mock_server_->AddUpdateSpecifics(3, 1, kEncryptedString, 20, 20, false, 1,
encrypted_bookmark);
mock_server_->AddUpdateSpecifics(4, 0, kEncryptedString, 20, 20, false, 0,
encrypted_pref);
SyncShareNudge();
{
// All should be unapplied due to being undecryptable and have a valid
// BASE_SERVER_SPECIFICS.
syncable::ReadTransaction rtrans(FROM_HERE, directory());
VERIFY_ENTRY(1, true, false, true, 0, 10, 20, ids_, &rtrans);
VERIFY_ENTRY(2, true, false, true, 1, 10, 20, ids_, &rtrans);
VERIFY_ENTRY(3, true, false, true, 1, 10, 20, ids_, &rtrans);
VERIFY_ENTRY(4, true, false, true, 0, 10, 20, ids_, &rtrans);
}
// Server side change that don't modify anything should not affect
// BASE_SERVER_SPECIFICS (such as name changes and mtime changes).
mock_server_->AddUpdateSpecifics(1, 0, kEncryptedString, 30, 30, true, 0,
encrypted_bookmark);
mock_server_->AddUpdateSpecifics(2, 1, kEncryptedString, 30, 30, false, 2,
encrypted_bookmark);
// Item 3 doesn't change.
mock_server_->AddUpdateSpecifics(4, 0, kEncryptedString, 30, 30, false, 0,
encrypted_pref);
SyncShareNudge();
{
// Items 1, 2, and 4 should have newer server versions, 3 remains the same.
// All should remain unapplied due to be undecryptable.
syncable::ReadTransaction rtrans(FROM_HERE, directory());
VERIFY_ENTRY(1, true, false, true, 0, 10, 30, ids_, &rtrans);
VERIFY_ENTRY(2, true, false, true, 1, 10, 30, ids_, &rtrans);
VERIFY_ENTRY(3, true, false, true, 1, 10, 20, ids_, &rtrans);
VERIFY_ENTRY(4, true, false, true, 0, 10, 30, ids_, &rtrans);
}
// Positional changes, parent changes, and specifics changes should reset
// BASE_SERVER_SPECIFICS.
// Became unencrypted.
mock_server_->AddUpdateSpecifics(1, 0, "A", 40, 40, true, 0, bookmark);
// Reordered to after item 2.
mock_server_->AddUpdateSpecifics(3, 1, kEncryptedString, 30, 30, false, 3,
encrypted_bookmark);
SyncShareNudge();
{
// Items 2 and 4 should be the only ones with BASE_SERVER_SPECIFICS set.
// Items 1 is now unencrypted, so should have applied normally.
syncable::ReadTransaction rtrans(FROM_HERE, directory());
VERIFY_ENTRY(1, false, false, false, 0, 40, 40, ids_, &rtrans);
VERIFY_ENTRY(2, true, false, true, 1, 10, 30, ids_, &rtrans);
VERIFY_ENTRY(3, true, false, false, 1, 10, 30, ids_, &rtrans);
VERIFY_ENTRY(4, true, false, true, 0, 10, 30, ids_, &rtrans);
}
// Make local changes, which should remain unsynced for items 2, 3, 4.
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry A(&wtrans, GET_BY_ID, ids_.FromNumber(1));
ASSERT_TRUE(A.good());
A.Put(SPECIFICS, modified_bookmark);
A.Put(NON_UNIQUE_NAME, kEncryptedString);
A.Put(IS_UNSYNCED, true);
MutableEntry B(&wtrans, GET_BY_ID, ids_.FromNumber(2));
ASSERT_TRUE(B.good());
B.Put(SPECIFICS, modified_bookmark);
B.Put(NON_UNIQUE_NAME, kEncryptedString);
B.Put(IS_UNSYNCED, true);
MutableEntry C(&wtrans, GET_BY_ID, ids_.FromNumber(3));
ASSERT_TRUE(C.good());
C.Put(SPECIFICS, modified_bookmark);
C.Put(NON_UNIQUE_NAME, kEncryptedString);
C.Put(IS_UNSYNCED, true);
MutableEntry D(&wtrans, GET_BY_ID, ids_.FromNumber(4));
ASSERT_TRUE(D.good());
D.Put(SPECIFICS, modified_pref);
D.Put(NON_UNIQUE_NAME, kEncryptedString);
D.Put(IS_UNSYNCED, true);
}
SyncShareNudge();
{
// Item 1 remains unsynced due to there being pending keys.
// Items 2, 3, 4 should remain unsynced since they were not up to date.
syncable::ReadTransaction rtrans(FROM_HERE, directory());
VERIFY_ENTRY(1, false, true, false, 0, 40, 40, ids_, &rtrans);
VERIFY_ENTRY(2, true, true, true, 1, 10, 30, ids_, &rtrans);
VERIFY_ENTRY(3, true, true, false, 1, 10, 30, ids_, &rtrans);
VERIFY_ENTRY(4, true, true, true, 0, 10, 30, ids_, &rtrans);
}
{
syncable::ReadTransaction rtrans(FROM_HERE, directory());
// Resolve the pending keys.
GetCryptographer(&rtrans)->DecryptPendingKeys(key_params);
}
// First cycle resolves conflicts, second cycle commits changes.
SyncShareNudge();
EXPECT_EQ(2, status().model_neutral_state().num_server_overwrites);
EXPECT_EQ(1, status().model_neutral_state().num_local_overwrites);
// We successfully commited item(s).
EXPECT_EQ(status().model_neutral_state().commit_result, SYNCER_OK);
SyncShareNudge();
// Everything should be resolved now. The local changes should have
// overwritten the server changes for 2 and 4, while the server changes
// overwrote the local for entry 3.
EXPECT_EQ(0, status().model_neutral_state().num_server_overwrites);
EXPECT_EQ(0, status().model_neutral_state().num_local_overwrites);
EXPECT_EQ(status().model_neutral_state().commit_result, SYNCER_OK);
syncable::ReadTransaction rtrans(FROM_HERE, directory());
VERIFY_ENTRY(1, false, false, false, 0, 41, 41, ids_, &rtrans);
VERIFY_ENTRY(2, false, false, false, 1, 31, 31, ids_, &rtrans);
VERIFY_ENTRY(3, false, false, false, 1, 30, 30, ids_, &rtrans);
VERIFY_ENTRY(4, false, false, false, 0, 31, 31, ids_, &rtrans);
}
#undef VERIFY_ENTRY
TEST_F(SyncerTest, TestGetUnsyncedAndSimpleCommit) {
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&wtrans, syncable::CREATE, wtrans.root_id(),
"Pete");
ASSERT_TRUE(parent.good());
parent.Put(syncable::IS_UNSYNCED, true);
parent.Put(syncable::IS_DIR, true);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(syncable::BASE_VERSION, 1);
parent.Put(syncable::ID, parent_id_);
MutableEntry child(&wtrans, syncable::CREATE, parent_id_, "Pete");
ASSERT_TRUE(child.good());
child.Put(syncable::ID, child_id_);
child.Put(syncable::BASE_VERSION, 1);
WriteTestDataToEntry(&wtrans, &child);
}
SyncShareNudge();
ASSERT_EQ(2u, mock_server_->committed_ids().size());
// If this test starts failing, be aware other sort orders could be valid.
EXPECT_TRUE(parent_id_ == mock_server_->committed_ids()[0]);
EXPECT_TRUE(child_id_ == mock_server_->committed_ids()[1]);
{
syncable::ReadTransaction rt(FROM_HERE, directory());
Entry entry(&rt, syncable::GET_BY_ID, child_id_);
ASSERT_TRUE(entry.good());
VerifyTestDataInEntry(&rt, &entry);
}
}
TEST_F(SyncerTest, TestPurgeWhileUnsynced) {
// Similar to above, but throw a purge operation into the mix. Bug 49278.
syncable::Id pref_node_id = TestIdFactory::MakeServer("Tim");
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&wtrans, syncable::CREATE, wtrans.root_id(), "Pete");
ASSERT_TRUE(parent.good());
parent.Put(syncable::IS_UNSYNCED, true);
parent.Put(syncable::IS_DIR, true);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(syncable::BASE_VERSION, 1);
parent.Put(syncable::ID, parent_id_);
MutableEntry child(&wtrans, syncable::CREATE, parent_id_, "Pete");
ASSERT_TRUE(child.good());
child.Put(syncable::ID, child_id_);
child.Put(syncable::BASE_VERSION, 1);
WriteTestDataToEntry(&wtrans, &child);
MutableEntry parent2(&wtrans, syncable::CREATE, wtrans.root_id(), "Tim");
ASSERT_TRUE(parent2.good());
parent2.Put(syncable::IS_UNSYNCED, true);
parent2.Put(syncable::IS_DIR, true);
parent2.Put(syncable::SPECIFICS, DefaultPreferencesSpecifics());
parent2.Put(syncable::BASE_VERSION, 1);
parent2.Put(syncable::ID, pref_node_id);
}
directory()->PurgeEntriesWithTypeIn(ModelTypeSet(PREFERENCES));
SyncShareNudge();
ASSERT_EQ(2U, mock_server_->committed_ids().size());
// If this test starts failing, be aware other sort orders could be valid.
EXPECT_TRUE(parent_id_ == mock_server_->committed_ids()[0]);
EXPECT_TRUE(child_id_ == mock_server_->committed_ids()[1]);
{
syncable::ReadTransaction rt(FROM_HERE, directory());
Entry entry(&rt, syncable::GET_BY_ID, child_id_);
ASSERT_TRUE(entry.good());
VerifyTestDataInEntry(&rt, &entry);
}
directory()->SaveChanges();
{
syncable::ReadTransaction rt(FROM_HERE, directory());
Entry entry(&rt, syncable::GET_BY_ID, pref_node_id);
ASSERT_FALSE(entry.good());
}
}
TEST_F(SyncerTest, TestPurgeWhileUnapplied) {
// Similar to above, but for unapplied items. Bug 49278.
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&wtrans, syncable::CREATE, wtrans.root_id(), "Pete");
ASSERT_TRUE(parent.good());
parent.Put(syncable::IS_UNAPPLIED_UPDATE, true);
parent.Put(syncable::IS_DIR, true);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(syncable::BASE_VERSION, 1);
parent.Put(syncable::ID, parent_id_);
}
directory()->PurgeEntriesWithTypeIn(
ModelTypeSet(BOOKMARKS));
SyncShareNudge();
directory()->SaveChanges();
{
syncable::ReadTransaction rt(FROM_HERE, directory());
Entry entry(&rt, syncable::GET_BY_ID, parent_id_);
ASSERT_FALSE(entry.good());
}
}
TEST_F(SyncerTest, TestCommitListOrderingTwoItemsTall) {
CommitOrderingTest items[] = {
{1, ids_.FromNumber(-1001), ids_.FromNumber(-1000)},
{0, ids_.FromNumber(-1000), ids_.FromNumber(0)},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest, TestCommitListOrderingThreeItemsTall) {
CommitOrderingTest items[] = {
{1, ids_.FromNumber(-2001), ids_.FromNumber(-2000)},
{0, ids_.FromNumber(-2000), ids_.FromNumber(0)},
{2, ids_.FromNumber(-2002), ids_.FromNumber(-2001)},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest, TestCommitListOrderingThreeItemsTallLimitedSize) {
context_->set_max_commit_batch_size(2);
CommitOrderingTest items[] = {
{1, ids_.FromNumber(-2001), ids_.FromNumber(-2000)},
{0, ids_.FromNumber(-2000), ids_.FromNumber(0)},
{2, ids_.FromNumber(-2002), ids_.FromNumber(-2001)},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest, TestCommitListOrderingSingleDeletedItem) {
CommitOrderingTest items[] = {
{0, ids_.FromNumber(1000), ids_.FromNumber(0), {DELETED}},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest, TestCommitListOrderingSingleUncommittedDeletedItem) {
CommitOrderingTest items[] = {
{-1, ids_.FromNumber(-1000), ids_.FromNumber(0), {DELETED}},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest, TestCommitListOrderingSingleDeletedItemWithUnroll) {
CommitOrderingTest items[] = {
{0, ids_.FromNumber(1000), ids_.FromNumber(0), {DELETED}},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest,
TestCommitListOrderingSingleLongDeletedItemWithUnroll) {
CommitOrderingTest items[] = {
{0, ids_.FromNumber(1000), ids_.FromNumber(0), {DELETED, OLD_MTIME}},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest, TestCommitListOrderingTwoLongDeletedItemWithUnroll) {
CommitOrderingTest items[] = {
{0, ids_.FromNumber(1000), ids_.FromNumber(0), {DELETED, OLD_MTIME}},
{-1, ids_.FromNumber(1001), ids_.FromNumber(1000), {DELETED, OLD_MTIME}},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest, TestCommitListOrdering3LongDeletedItemsWithSizeLimit) {
context_->set_max_commit_batch_size(2);
CommitOrderingTest items[] = {
{0, ids_.FromNumber(1000), ids_.FromNumber(0), {DELETED, OLD_MTIME}},
{1, ids_.FromNumber(1001), ids_.FromNumber(0), {DELETED, OLD_MTIME}},
{2, ids_.FromNumber(1002), ids_.FromNumber(0), {DELETED, OLD_MTIME}},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest, TestCommitListOrderingTwoDeletedItemsWithUnroll) {
CommitOrderingTest items[] = {
{0, ids_.FromNumber(1000), ids_.FromNumber(0), {DELETED}},
{-1, ids_.FromNumber(1001), ids_.FromNumber(1000), {DELETED}},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest, TestCommitListOrderingComplexDeletionScenario) {
CommitOrderingTest items[] = {
{ 0, ids_.FromNumber(1000), ids_.FromNumber(0), {DELETED, OLD_MTIME}},
{-1, ids_.FromNumber(1001), ids_.FromNumber(0), {SYNCED}},
{1, ids_.FromNumber(1002), ids_.FromNumber(1001), {DELETED, OLD_MTIME}},
{-1, ids_.FromNumber(1003), ids_.FromNumber(1001), {SYNCED}},
{2, ids_.FromNumber(1004), ids_.FromNumber(1003), {DELETED}},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest,
TestCommitListOrderingComplexDeletionScenarioWith2RecentDeletes) {
CommitOrderingTest items[] = {
{ 0, ids_.FromNumber(1000), ids_.FromNumber(0), {DELETED, OLD_MTIME}},
{-1, ids_.FromNumber(1001), ids_.FromNumber(0), {SYNCED}},
{1, ids_.FromNumber(1002), ids_.FromNumber(1001), {DELETED, OLD_MTIME}},
{-1, ids_.FromNumber(1003), ids_.FromNumber(1001), {SYNCED}},
{2, ids_.FromNumber(1004), ids_.FromNumber(1003), {DELETED}},
{3, ids_.FromNumber(1005), ids_.FromNumber(1003), {DELETED}},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest, TestCommitListOrderingDeleteMovedItems) {
CommitOrderingTest items[] = {
{1, ids_.FromNumber(1000), ids_.FromNumber(0), {DELETED, OLD_MTIME}},
{0, ids_.FromNumber(1001), ids_.FromNumber(1000), {DELETED, OLD_MTIME,
MOVED_FROM_ROOT}},
CommitOrderingTest::MakeLastCommitItem(),
};
RunCommitOrderingTest(items);
}
TEST_F(SyncerTest, TestCommitListOrderingWithNesting) {
const base::Time& now_minus_2h =
base::Time::Now() - base::TimeDelta::FromHours(2);
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
{
MutableEntry parent(&wtrans, syncable::CREATE, wtrans.root_id(),
"Bob");
ASSERT_TRUE(parent.good());
parent.Put(syncable::IS_UNSYNCED, true);
parent.Put(syncable::IS_DIR, true);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(syncable::ID, ids_.FromNumber(100));
parent.Put(syncable::BASE_VERSION, 1);
MutableEntry child(&wtrans, syncable::CREATE, ids_.FromNumber(100),
"Bob");
ASSERT_TRUE(child.good());
child.Put(syncable::IS_UNSYNCED, true);
child.Put(syncable::IS_DIR, true);
child.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
child.Put(syncable::ID, ids_.FromNumber(101));
child.Put(syncable::BASE_VERSION, 1);
MutableEntry grandchild(&wtrans, syncable::CREATE, ids_.FromNumber(101),
"Bob");
ASSERT_TRUE(grandchild.good());
grandchild.Put(syncable::ID, ids_.FromNumber(102));
grandchild.Put(syncable::IS_UNSYNCED, true);
grandchild.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
grandchild.Put(syncable::BASE_VERSION, 1);
}
{
// Create three deleted items which deletions we expect to be sent to the
// server.
MutableEntry parent(&wtrans, syncable::CREATE, wtrans.root_id(),
"Pete");
ASSERT_TRUE(parent.good());
parent.Put(syncable::ID, ids_.FromNumber(103));
parent.Put(syncable::IS_UNSYNCED, true);
parent.Put(syncable::IS_DIR, true);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(syncable::IS_DEL, true);
parent.Put(syncable::BASE_VERSION, 1);
parent.Put(syncable::MTIME, now_minus_2h);
MutableEntry child(&wtrans, syncable::CREATE, ids_.FromNumber(103),
"Pete");
ASSERT_TRUE(child.good());
child.Put(syncable::ID, ids_.FromNumber(104));
child.Put(syncable::IS_UNSYNCED, true);
child.Put(syncable::IS_DIR, true);
child.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
child.Put(syncable::IS_DEL, true);
child.Put(syncable::BASE_VERSION, 1);
child.Put(syncable::MTIME, now_minus_2h);
MutableEntry grandchild(&wtrans, syncable::CREATE, ids_.FromNumber(104),
"Pete");
ASSERT_TRUE(grandchild.good());
grandchild.Put(syncable::ID, ids_.FromNumber(105));
grandchild.Put(syncable::IS_UNSYNCED, true);
grandchild.Put(syncable::IS_DEL, true);
grandchild.Put(syncable::IS_DIR, false);
grandchild.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
grandchild.Put(syncable::BASE_VERSION, 1);
grandchild.Put(syncable::MTIME, now_minus_2h);
}
}
SyncShareNudge();
ASSERT_EQ(6u, mock_server_->committed_ids().size());
// This test will NOT unroll deletes because SERVER_PARENT_ID is not set.
// It will treat these like moves.
vector<syncable::Id> commit_ids(mock_server_->committed_ids());
EXPECT_TRUE(ids_.FromNumber(100) == commit_ids[0]);
EXPECT_TRUE(ids_.FromNumber(101) == commit_ids[1]);
EXPECT_TRUE(ids_.FromNumber(102) == commit_ids[2]);
// We don't guarantee the delete orders in this test, only that they occur
// at the end.
std::sort(commit_ids.begin() + 3, commit_ids.end());
EXPECT_TRUE(ids_.FromNumber(103) == commit_ids[3]);
EXPECT_TRUE(ids_.FromNumber(104) == commit_ids[4]);
EXPECT_TRUE(ids_.FromNumber(105) == commit_ids[5]);
}
TEST_F(SyncerTest, TestCommitListOrderingWithNewItems) {
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&wtrans, syncable::CREATE, wtrans.root_id(), "1");
ASSERT_TRUE(parent.good());
parent.Put(syncable::IS_UNSYNCED, true);
parent.Put(syncable::IS_DIR, true);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(syncable::ID, parent_id_);
MutableEntry child(&wtrans, syncable::CREATE, wtrans.root_id(), "2");
ASSERT_TRUE(child.good());
child.Put(syncable::IS_UNSYNCED, true);
child.Put(syncable::IS_DIR, true);
child.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
child.Put(syncable::ID, child_id_);
parent.Put(syncable::BASE_VERSION, 1);
child.Put(syncable::BASE_VERSION, 1);
}
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&wtrans, syncable::CREATE, parent_id_, "A");
ASSERT_TRUE(parent.good());
parent.Put(syncable::IS_UNSYNCED, true);
parent.Put(syncable::IS_DIR, true);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(syncable::ID, ids_.FromNumber(102));
MutableEntry child(&wtrans, syncable::CREATE, parent_id_, "B");
ASSERT_TRUE(child.good());
child.Put(syncable::IS_UNSYNCED, true);
child.Put(syncable::IS_DIR, true);
child.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
child.Put(syncable::ID, ids_.FromNumber(-103));
parent.Put(syncable::BASE_VERSION, 1);
}
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&wtrans, syncable::CREATE, child_id_, "A");
ASSERT_TRUE(parent.good());
parent.Put(syncable::IS_UNSYNCED, true);
parent.Put(syncable::IS_DIR, true);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(syncable::ID, ids_.FromNumber(-104));
MutableEntry child(&wtrans, syncable::CREATE, child_id_, "B");
ASSERT_TRUE(child.good());
child.Put(syncable::IS_UNSYNCED, true);
child.Put(syncable::IS_DIR, true);
child.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
child.Put(syncable::ID, ids_.FromNumber(105));
child.Put(syncable::BASE_VERSION, 1);
}
SyncShareNudge();
ASSERT_EQ(6u, mock_server_->committed_ids().size());
// If this test starts failing, be aware other sort orders could be valid.
EXPECT_TRUE(parent_id_ == mock_server_->committed_ids()[0]);
EXPECT_TRUE(child_id_ == mock_server_->committed_ids()[1]);
EXPECT_TRUE(ids_.FromNumber(102) == mock_server_->committed_ids()[2]);
EXPECT_TRUE(ids_.FromNumber(-103) == mock_server_->committed_ids()[3]);
EXPECT_TRUE(ids_.FromNumber(-104) == mock_server_->committed_ids()[4]);
EXPECT_TRUE(ids_.FromNumber(105) == mock_server_->committed_ids()[5]);
}
TEST_F(SyncerTest, TestCommitListOrderingCounterexample) {
syncable::Id child2_id = ids_.NewServerId();
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&wtrans, syncable::CREATE, wtrans.root_id(), "P");
ASSERT_TRUE(parent.good());
parent.Put(syncable::IS_UNSYNCED, true);
parent.Put(syncable::IS_DIR, true);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(syncable::ID, parent_id_);
MutableEntry child1(&wtrans, syncable::CREATE, parent_id_, "1");
ASSERT_TRUE(child1.good());
child1.Put(syncable::IS_UNSYNCED, true);
child1.Put(syncable::ID, child_id_);
child1.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
MutableEntry child2(&wtrans, syncable::CREATE, parent_id_, "2");
ASSERT_TRUE(child2.good());
child2.Put(syncable::IS_UNSYNCED, true);
child2.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
child2.Put(syncable::ID, child2_id);
parent.Put(syncable::BASE_VERSION, 1);
child1.Put(syncable::BASE_VERSION, 1);
child2.Put(syncable::BASE_VERSION, 1);
}
SyncShareNudge();
ASSERT_EQ(3u, mock_server_->committed_ids().size());
// If this test starts failing, be aware other sort orders could be valid.
EXPECT_TRUE(parent_id_ == mock_server_->committed_ids()[0]);
EXPECT_TRUE(child_id_ == mock_server_->committed_ids()[1]);
EXPECT_TRUE(child2_id == mock_server_->committed_ids()[2]);
}
TEST_F(SyncerTest, TestCommitListOrderingAndNewParent) {
string parent1_name = "1";
string parent2_name = "A";
string child_name = "B";
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&wtrans, syncable::CREATE, wtrans.root_id(),
parent1_name);
ASSERT_TRUE(parent.good());
parent.Put(syncable::IS_UNSYNCED, true);
parent.Put(syncable::IS_DIR, true);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(syncable::ID, parent_id_);
parent.Put(syncable::BASE_VERSION, 1);
}
syncable::Id parent2_id = ids_.NewLocalId();
syncable::Id child_id = ids_.NewServerId();
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent2(&wtrans, syncable::CREATE, parent_id_, parent2_name);
ASSERT_TRUE(parent2.good());
parent2.Put(syncable::IS_UNSYNCED, true);
parent2.Put(syncable::IS_DIR, true);
parent2.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent2.Put(syncable::ID, parent2_id);
MutableEntry child(&wtrans, syncable::CREATE, parent2_id, child_name);
ASSERT_TRUE(child.good());
child.Put(syncable::IS_UNSYNCED, true);
child.Put(syncable::IS_DIR, true);
child.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
child.Put(syncable::ID, child_id);
child.Put(syncable::BASE_VERSION, 1);
}
SyncShareNudge();
ASSERT_EQ(3u, mock_server_->committed_ids().size());
// If this test starts failing, be aware other sort orders could be valid.
EXPECT_TRUE(parent_id_ == mock_server_->committed_ids()[0]);
EXPECT_TRUE(parent2_id == mock_server_->committed_ids()[1]);
EXPECT_TRUE(child_id == mock_server_->committed_ids()[2]);
{
syncable::ReadTransaction rtrans(FROM_HERE, directory());
// Check that things committed correctly.
Entry entry_1(&rtrans, syncable::GET_BY_ID, parent_id_);
EXPECT_EQ(entry_1.Get(NON_UNIQUE_NAME), parent1_name);
// Check that parent2 is a subfolder of parent1.
EXPECT_EQ(1, CountEntriesWithName(&rtrans,
parent_id_,
parent2_name));
// Parent2 was a local ID and thus should have changed on commit!
Entry pre_commit_entry_parent2(&rtrans, syncable::GET_BY_ID, parent2_id);
ASSERT_FALSE(pre_commit_entry_parent2.good());
// Look up the new ID.
Id parent2_committed_id =
GetOnlyEntryWithName(&rtrans, parent_id_, parent2_name);
EXPECT_TRUE(parent2_committed_id.ServerKnows());
Entry child(&rtrans, syncable::GET_BY_ID, child_id);
EXPECT_EQ(parent2_committed_id, child.Get(syncable::PARENT_ID));
}
}
TEST_F(SyncerTest, TestCommitListOrderingAndNewParentAndChild) {
string parent_name = "1";
string parent2_name = "A";
string child_name = "B";
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&wtrans,
syncable::CREATE,
wtrans.root_id(),
parent_name);
ASSERT_TRUE(parent.good());
parent.Put(syncable::IS_UNSYNCED, true);
parent.Put(syncable::IS_DIR, true);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(syncable::ID, parent_id_);
parent.Put(syncable::BASE_VERSION, 1);
}
int64 meta_handle_b;
const Id parent2_local_id = ids_.NewLocalId();
const Id child_local_id = ids_.NewLocalId();
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent2(&wtrans, syncable::CREATE, parent_id_, parent2_name);
ASSERT_TRUE(parent2.good());
parent2.Put(syncable::IS_UNSYNCED, true);
parent2.Put(syncable::IS_DIR, true);
parent2.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent2.Put(syncable::ID, parent2_local_id);
MutableEntry child(&wtrans, syncable::CREATE, parent2_local_id, child_name);
ASSERT_TRUE(child.good());
child.Put(syncable::IS_UNSYNCED, true);
child.Put(syncable::IS_DIR, true);
child.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
child.Put(syncable::ID, child_local_id);
meta_handle_b = child.Get(syncable::META_HANDLE);
}
SyncShareNudge();
ASSERT_EQ(3u, mock_server_->committed_ids().size());
// If this test starts failing, be aware other sort orders could be valid.
EXPECT_TRUE(parent_id_ == mock_server_->committed_ids()[0]);
EXPECT_TRUE(parent2_local_id == mock_server_->committed_ids()[1]);
EXPECT_TRUE(child_local_id == mock_server_->committed_ids()[2]);
{
syncable::ReadTransaction rtrans(FROM_HERE, directory());
Entry parent(&rtrans, syncable::GET_BY_ID,
GetOnlyEntryWithName(&rtrans, rtrans.root_id(), parent_name));
ASSERT_TRUE(parent.good());
EXPECT_TRUE(parent.Get(syncable::ID).ServerKnows());
Entry parent2(&rtrans, syncable::GET_BY_ID,
GetOnlyEntryWithName(&rtrans, parent.Get(ID), parent2_name));
ASSERT_TRUE(parent2.good());
EXPECT_TRUE(parent2.Get(syncable::ID).ServerKnows());
// Id changed on commit, so this should fail.
Entry local_parent2_id_entry(&rtrans,
syncable::GET_BY_ID,
parent2_local_id);
ASSERT_FALSE(local_parent2_id_entry.good());
Entry entry_b(&rtrans, syncable::GET_BY_HANDLE, meta_handle_b);
EXPECT_TRUE(entry_b.Get(syncable::ID).ServerKnows());
EXPECT_TRUE(parent2.Get(syncable::ID) == entry_b.Get(syncable::PARENT_ID));
}
}
TEST_F(SyncerTest, UpdateWithZeroLengthName) {
// One illegal update
mock_server_->AddUpdateDirectory(1, 0, "", 1, 10);
// And one legal one that we're going to delete.
mock_server_->AddUpdateDirectory(2, 0, "FOO", 1, 10);
SyncShareNudge();
// Delete the legal one. The new update has a null name.
mock_server_->AddUpdateDirectory(2, 0, "", 2, 20);
mock_server_->SetLastUpdateDeleted();
SyncShareNudge();
}
TEST_F(SyncerTest, TestBasicUpdate) {
string id = "some_id";
string parent_id = "0";
string name = "in_root";
int64 version = 10;
int64 timestamp = 10;
mock_server_->AddUpdateDirectory(id, parent_id, name, version, timestamp);
SyncShareNudge();
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
Entry entry(&trans, GET_BY_ID,
syncable::Id::CreateFromServerId("some_id"));
ASSERT_TRUE(entry.good());
EXPECT_TRUE(entry.Get(IS_DIR));
EXPECT_TRUE(entry.Get(SERVER_VERSION) == version);
EXPECT_TRUE(entry.Get(BASE_VERSION) == version);
EXPECT_FALSE(entry.Get(IS_UNAPPLIED_UPDATE));
EXPECT_FALSE(entry.Get(IS_UNSYNCED));
EXPECT_FALSE(entry.Get(SERVER_IS_DEL));
EXPECT_FALSE(entry.Get(IS_DEL));
}
}
TEST_F(SyncerTest, IllegalAndLegalUpdates) {
Id root = TestIdFactory::root();
// Should apply just fine.
mock_server_->AddUpdateDirectory(1, 0, "in_root", 10, 10);
// Same name. But this SHOULD work.
mock_server_->AddUpdateDirectory(2, 0, "in_root", 10, 10);
// Unknown parent: should never be applied. "-80" is a legal server ID,
// because any string sent by the server is a legal server ID in the sync
// protocol, but it's not the ID of any item known to the client. This
// update should succeed validation, but be stuck in the unapplied state
// until an item with the server ID "-80" arrives.
mock_server_->AddUpdateDirectory(3, -80, "bad_parent", 10, 10);
SyncShareNudge();
// Id 3 should be in conflict now.
EXPECT_EQ(1, status().TotalNumConflictingItems());
{
sessions::ScopedModelSafeGroupRestriction r(
session_->mutable_status_controller(), GROUP_PASSIVE);
ASSERT_TRUE(status().conflict_progress());
EXPECT_EQ(1, status().conflict_progress()->HierarchyConflictingItemsSize());
}
// These entries will be used in the second set of updates.
mock_server_->AddUpdateDirectory(4, 0, "newer_version", 20, 10);
mock_server_->AddUpdateDirectory(5, 0, "circular1", 10, 10);
mock_server_->AddUpdateDirectory(6, 5, "circular2", 10, 10);
mock_server_->AddUpdateDirectory(9, 3, "bad_parent_child", 10, 10);
mock_server_->AddUpdateDirectory(100, 9, "bad_parent_child2", 10, 10);
mock_server_->AddUpdateDirectory(10, 0, "dir_to_bookmark", 10, 10);
SyncShareNudge();
// The three items with an unresolved parent should be unapplied (3, 9, 100).
// The name clash should also still be in conflict.
EXPECT_EQ(3, status().TotalNumConflictingItems());
{
sessions::ScopedModelSafeGroupRestriction r(
session_->mutable_status_controller(), GROUP_PASSIVE);
ASSERT_TRUE(status().conflict_progress());
EXPECT_EQ(3, status().conflict_progress()->HierarchyConflictingItemsSize());
}
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
// Even though it has the same name, it should work.
Entry name_clash(&trans, GET_BY_ID, ids_.FromNumber(2));
ASSERT_TRUE(name_clash.good());
EXPECT_FALSE(name_clash.Get(IS_UNAPPLIED_UPDATE))
<< "Duplicate name SHOULD be OK.";
Entry bad_parent(&trans, GET_BY_ID, ids_.FromNumber(3));
ASSERT_TRUE(bad_parent.good());
EXPECT_TRUE(bad_parent.Get(IS_UNAPPLIED_UPDATE))
<< "child of unknown parent should be in conflict";
Entry bad_parent_child(&trans, GET_BY_ID, ids_.FromNumber(9));
ASSERT_TRUE(bad_parent_child.good());
EXPECT_TRUE(bad_parent_child.Get(IS_UNAPPLIED_UPDATE))
<< "grandchild of unknown parent should be in conflict";
Entry bad_parent_child2(&trans, GET_BY_ID, ids_.FromNumber(100));
ASSERT_TRUE(bad_parent_child2.good());
EXPECT_TRUE(bad_parent_child2.Get(IS_UNAPPLIED_UPDATE))
<< "great-grandchild of unknown parent should be in conflict";
}
// Updating 1 should not affect item 2 of the same name.
mock_server_->AddUpdateDirectory(1, 0, "new_name", 20, 20);
// Moving 5 under 6 will create a cycle: a conflict.
mock_server_->AddUpdateDirectory(5, 6, "circular3", 20, 20);
// Flip the is_dir bit: should fail verify & be dropped.
mock_server_->AddUpdateBookmark(10, 0, "dir_to_bookmark", 20, 20);
SyncShareNudge();
// Version number older than last known: should fail verify & be dropped.
mock_server_->AddUpdateDirectory(4, 0, "old_version", 10, 10);
SyncShareNudge();
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry still_a_dir(&trans, GET_BY_ID, ids_.FromNumber(10));
ASSERT_TRUE(still_a_dir.good());
EXPECT_FALSE(still_a_dir.Get(IS_UNAPPLIED_UPDATE));
EXPECT_EQ(10u, still_a_dir.Get(BASE_VERSION));
EXPECT_EQ(10u, still_a_dir.Get(SERVER_VERSION));
EXPECT_TRUE(still_a_dir.Get(IS_DIR));
Entry rename(&trans, GET_BY_ID, ids_.FromNumber(1));
ASSERT_TRUE(rename.good());
EXPECT_EQ(root, rename.Get(PARENT_ID));
EXPECT_EQ("new_name", rename.Get(NON_UNIQUE_NAME));
EXPECT_FALSE(rename.Get(IS_UNAPPLIED_UPDATE));
EXPECT_TRUE(ids_.FromNumber(1) == rename.Get(ID));
EXPECT_EQ(20u, rename.Get(BASE_VERSION));
Entry name_clash(&trans, GET_BY_ID, ids_.FromNumber(2));
ASSERT_TRUE(name_clash.good());
EXPECT_EQ(root, name_clash.Get(PARENT_ID));
EXPECT_TRUE(ids_.FromNumber(2) == name_clash.Get(ID));
EXPECT_EQ(10u, name_clash.Get(BASE_VERSION));
EXPECT_EQ("in_root", name_clash.Get(NON_UNIQUE_NAME));
Entry ignored_old_version(&trans, GET_BY_ID, ids_.FromNumber(4));
ASSERT_TRUE(ignored_old_version.good());
EXPECT_TRUE(
ignored_old_version.Get(NON_UNIQUE_NAME) == "newer_version");
EXPECT_FALSE(ignored_old_version.Get(IS_UNAPPLIED_UPDATE));
EXPECT_EQ(20u, ignored_old_version.Get(BASE_VERSION));
Entry circular_parent_issue(&trans, GET_BY_ID, ids_.FromNumber(5));
ASSERT_TRUE(circular_parent_issue.good());
EXPECT_TRUE(circular_parent_issue.Get(IS_UNAPPLIED_UPDATE))
<< "circular move should be in conflict";
EXPECT_TRUE(circular_parent_issue.Get(PARENT_ID) == root_id_);
EXPECT_TRUE(circular_parent_issue.Get(SERVER_PARENT_ID) ==
ids_.FromNumber(6));
EXPECT_EQ(10u, circular_parent_issue.Get(BASE_VERSION));
Entry circular_parent_target(&trans, GET_BY_ID, ids_.FromNumber(6));
ASSERT_TRUE(circular_parent_target.good());
EXPECT_FALSE(circular_parent_target.Get(IS_UNAPPLIED_UPDATE));
EXPECT_TRUE(circular_parent_issue.Get(ID) ==
circular_parent_target.Get(PARENT_ID));
EXPECT_EQ(10u, circular_parent_target.Get(BASE_VERSION));
}
EXPECT_FALSE(saw_syncer_event_);
EXPECT_EQ(4, status().TotalNumConflictingItems());
{
sessions::ScopedModelSafeGroupRestriction r(
session_->mutable_status_controller(), GROUP_PASSIVE);
ASSERT_TRUE(status().conflict_progress());
EXPECT_EQ(4, status().conflict_progress()->HierarchyConflictingItemsSize());
}
}
TEST_F(SyncerTest, CommitTimeRename) {
int64 metahandle_folder;
int64 metahandle_new_entry;
// Create a folder and an entry.
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&trans, CREATE, root_id_, "Folder");
ASSERT_TRUE(parent.good());
parent.Put(IS_DIR, true);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(IS_UNSYNCED, true);
metahandle_folder = parent.Get(META_HANDLE);
MutableEntry entry(&trans, CREATE, parent.Get(ID), "new_entry");
ASSERT_TRUE(entry.good());
metahandle_new_entry = entry.Get(META_HANDLE);
WriteTestDataToEntry(&trans, &entry);
}
// Mix in a directory creation too for later.
mock_server_->AddUpdateDirectory(2, 0, "dir_in_root", 10, 10);
mock_server_->SetCommitTimeRename("renamed_");
SyncShareNudge();
// Verify it was correctly renamed.
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry entry_folder(&trans, GET_BY_HANDLE, metahandle_folder);
ASSERT_TRUE(entry_folder.good());
EXPECT_EQ("renamed_Folder", entry_folder.Get(NON_UNIQUE_NAME));
Entry entry_new(&trans, GET_BY_HANDLE, metahandle_new_entry);
ASSERT_TRUE(entry_new.good());
EXPECT_EQ(entry_folder.Get(ID), entry_new.Get(PARENT_ID));
EXPECT_EQ("renamed_new_entry", entry_new.Get(NON_UNIQUE_NAME));
// And that the unrelated directory creation worked without a rename.
Entry new_dir(&trans, GET_BY_ID, ids_.FromNumber(2));
EXPECT_TRUE(new_dir.good());
EXPECT_EQ("dir_in_root", new_dir.Get(NON_UNIQUE_NAME));
}
}
TEST_F(SyncerTest, CommitTimeRenameI18N) {
// This is utf-8 for the diacritized Internationalization.
const char* i18nString = "\xc3\x8e\xc3\xb1\x74\xc3\xa9\x72\xc3\xb1"
"\xc3\xa5\x74\xc3\xae\xc3\xb6\xc3\xb1\xc3\xa5\x6c\xc3\xae"
"\xc2\x9e\xc3\xa5\x74\xc3\xae\xc3\xb6\xc3\xb1";
int64 metahandle;
// Create a folder, expect a commit time rename.
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&trans, CREATE, root_id_, "Folder");
ASSERT_TRUE(parent.good());
parent.Put(IS_DIR, true);
parent.Put(SPECIFICS, DefaultBookmarkSpecifics());
parent.Put(IS_UNSYNCED, true);
metahandle = parent.Get(META_HANDLE);
}
mock_server_->SetCommitTimeRename(i18nString);
SyncShareNudge();
// Verify it was correctly renamed.
{
syncable::ReadTransaction trans(FROM_HERE, directory());
string expected_folder_name(i18nString);
expected_folder_name.append("Folder");
Entry entry_folder(&trans, GET_BY_HANDLE, metahandle);
ASSERT_TRUE(entry_folder.good());
EXPECT_EQ(expected_folder_name, entry_folder.Get(NON_UNIQUE_NAME));
}
}
// A commit with a lost response produces an update that has to be reunited with
// its parent.
TEST_F(SyncerTest, CommitReuniteUpdateAdjustsChildren) {
// Create a folder in the root.
int64 metahandle_folder;
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, CREATE, trans.root_id(), "new_folder");
ASSERT_TRUE(entry.good());
entry.Put(IS_DIR, true);
entry.Put(SPECIFICS, DefaultBookmarkSpecifics());
entry.Put(IS_UNSYNCED, true);
metahandle_folder = entry.Get(META_HANDLE);
}
// Verify it and pull the ID out of the folder.
syncable::Id folder_id;
int64 metahandle_entry;
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry entry(&trans, GET_BY_HANDLE, metahandle_folder);
ASSERT_TRUE(entry.good());
folder_id = entry.Get(ID);
ASSERT_TRUE(!folder_id.ServerKnows());
}
// Create an entry in the newly created folder.
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, CREATE, folder_id, "new_entry");
ASSERT_TRUE(entry.good());
metahandle_entry = entry.Get(META_HANDLE);
WriteTestDataToEntry(&trans, &entry);
}
// Verify it and pull the ID out of the entry.
syncable::Id entry_id;
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry entry(&trans, syncable::GET_BY_HANDLE, metahandle_entry);
ASSERT_TRUE(entry.good());
EXPECT_EQ(folder_id, entry.Get(PARENT_ID));
EXPECT_EQ("new_entry", entry.Get(NON_UNIQUE_NAME));
entry_id = entry.Get(ID);
EXPECT_TRUE(!entry_id.ServerKnows());
VerifyTestDataInEntry(&trans, &entry);
}
// Now, to emulate a commit response failure, we just don't commit it.
int64 new_version = 150; // any larger value.
int64 timestamp = 20; // arbitrary value.
syncable::Id new_folder_id =
syncable::Id::CreateFromServerId("folder_server_id");
// The following update should cause the folder to both apply the update, as
// well as reassociate the id.
mock_server_->AddUpdateDirectory(new_folder_id, root_id_,
"new_folder", new_version, timestamp);
mock_server_->SetLastUpdateOriginatorFields(
directory()->cache_guid(), folder_id.GetServerId());
// We don't want it accidentally committed, just the update applied.
mock_server_->set_conflict_all_commits(true);
// Alright! Apply that update!
SyncShareNudge();
{
// The folder's ID should have been updated.
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry folder(&trans, GET_BY_HANDLE, metahandle_folder);
ASSERT_TRUE(folder.good());
EXPECT_EQ("new_folder", folder.Get(NON_UNIQUE_NAME));
EXPECT_TRUE(new_version == folder.Get(BASE_VERSION));
EXPECT_TRUE(new_folder_id == folder.Get(ID));
EXPECT_TRUE(folder.Get(ID).ServerKnows());
EXPECT_EQ(trans.root_id(), folder.Get(PARENT_ID));
// Since it was updated, the old folder should not exist.
Entry old_dead_folder(&trans, GET_BY_ID, folder_id);
EXPECT_FALSE(old_dead_folder.good());
// The child's parent should have changed.
Entry entry(&trans, syncable::GET_BY_HANDLE, metahandle_entry);
ASSERT_TRUE(entry.good());
EXPECT_EQ("new_entry", entry.Get(NON_UNIQUE_NAME));
EXPECT_EQ(new_folder_id, entry.Get(PARENT_ID));
EXPECT_TRUE(!entry.Get(ID).ServerKnows());
VerifyTestDataInEntry(&trans, &entry);
}
}
// A commit with a lost response produces an update that has to be reunited with
// its parent.
TEST_F(SyncerTest, CommitReuniteUpdate) {
// Create an entry in the root.
int64 entry_metahandle;
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, CREATE, trans.root_id(), "new_entry");
ASSERT_TRUE(entry.good());
entry_metahandle = entry.Get(META_HANDLE);
WriteTestDataToEntry(&trans, &entry);
}
// Verify it and pull the ID out.
syncable::Id entry_id;
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry entry(&trans, GET_BY_HANDLE, entry_metahandle);
ASSERT_TRUE(entry.good());
entry_id = entry.Get(ID);
EXPECT_TRUE(!entry_id.ServerKnows());
VerifyTestDataInEntry(&trans, &entry);
}
// Now, to emulate a commit response failure, we just don't commit it.
int64 new_version = 150; // any larger value.
int64 timestamp = 20; // arbitrary value.
syncable::Id new_entry_id = syncable::Id::CreateFromServerId("server_id");
// Generate an update from the server with a relevant ID reassignment.
mock_server_->AddUpdateBookmark(new_entry_id, root_id_,
"new_entry", new_version, timestamp);
mock_server_->SetLastUpdateOriginatorFields(
directory()->cache_guid(), entry_id.GetServerId());
// We don't want it accidentally committed, just the update applied.
mock_server_->set_conflict_all_commits(true);
// Alright! Apply that update!
SyncShareNudge();
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry entry(&trans, GET_BY_HANDLE, entry_metahandle);
ASSERT_TRUE(entry.good());
EXPECT_TRUE(new_version == entry.Get(BASE_VERSION));
EXPECT_TRUE(new_entry_id == entry.Get(ID));
EXPECT_EQ("new_entry", entry.Get(NON_UNIQUE_NAME));
}
}
// A commit with a lost response must work even if the local entry was deleted
// before the update is applied. We should not duplicate the local entry in
// this case, but just create another one alongside. We may wish to examine
// this behavior in the future as it can create hanging uploads that never
// finish, that must be cleaned up on the server side after some time.
TEST_F(SyncerTest, CommitReuniteUpdateDoesNotChokeOnDeletedLocalEntry) {
// Create a entry in the root.
int64 entry_metahandle;
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, CREATE, trans.root_id(), "new_entry");
ASSERT_TRUE(entry.good());
entry_metahandle = entry.Get(META_HANDLE);
WriteTestDataToEntry(&trans, &entry);
}
// Verify it and pull the ID out.
syncable::Id entry_id;
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry entry(&trans, GET_BY_HANDLE, entry_metahandle);
ASSERT_TRUE(entry.good());
entry_id = entry.Get(ID);
EXPECT_TRUE(!entry_id.ServerKnows());
VerifyTestDataInEntry(&trans, &entry);
}
// Now, to emulate a commit response failure, we just don't commit it.
int64 new_version = 150; // any larger value.
int64 timestamp = 20; // arbitrary value.
syncable::Id new_entry_id = syncable::Id::CreateFromServerId("server_id");
// Generate an update from the server with a relevant ID reassignment.
mock_server_->AddUpdateBookmark(new_entry_id, root_id_,
"new_entry", new_version, timestamp);
mock_server_->SetLastUpdateOriginatorFields(
directory()->cache_guid(),
entry_id.GetServerId());
// We don't want it accidentally committed, just the update applied.
mock_server_->set_conflict_all_commits(true);
// Purposefully delete the entry now before the update application finishes.
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
Id new_entry_id = GetOnlyEntryWithName(
&trans, trans.root_id(), "new_entry");
MutableEntry entry(&trans, GET_BY_ID, new_entry_id);
ASSERT_TRUE(entry.good());
entry.Put(syncable::IS_DEL, true);
}
// Just don't CHECK fail in sync, have the update split.
SyncShareNudge();
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Id new_entry_id = GetOnlyEntryWithName(
&trans, trans.root_id(), "new_entry");
Entry entry(&trans, GET_BY_ID, new_entry_id);
ASSERT_TRUE(entry.good());
EXPECT_FALSE(entry.Get(IS_DEL));
Entry old_entry(&trans, GET_BY_ID, entry_id);
ASSERT_TRUE(old_entry.good());
EXPECT_TRUE(old_entry.Get(IS_DEL));
}
}
// TODO(chron): Add more unsanitized name tests.
TEST_F(SyncerTest, ConflictMatchingEntryHandlesUnsanitizedNames) {
mock_server_->AddUpdateDirectory(1, 0, "A/A", 10, 10);
mock_server_->AddUpdateDirectory(2, 0, "B/B", 10, 10);
mock_server_->set_conflict_all_commits(true);
SyncShareNudge();
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry A(&wtrans, GET_BY_ID, ids_.FromNumber(1));
ASSERT_TRUE(A.good());
A.Put(IS_UNSYNCED, true);
A.Put(IS_UNAPPLIED_UPDATE, true);
A.Put(SERVER_VERSION, 20);
MutableEntry B(&wtrans, GET_BY_ID, ids_.FromNumber(2));
ASSERT_TRUE(B.good());
B.Put(IS_UNAPPLIED_UPDATE, true);
B.Put(SERVER_VERSION, 20);
}
LoopSyncShare();
saw_syncer_event_ = false;
mock_server_->set_conflict_all_commits(false);
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry A(&trans, GET_BY_ID, ids_.FromNumber(1));
ASSERT_TRUE(A.good());
EXPECT_TRUE(A.Get(IS_UNSYNCED) == false);
EXPECT_TRUE(A.Get(IS_UNAPPLIED_UPDATE) == false);
EXPECT_TRUE(A.Get(SERVER_VERSION) == 20);
Entry B(&trans, GET_BY_ID, ids_.FromNumber(2));
ASSERT_TRUE(B.good());
EXPECT_TRUE(B.Get(IS_UNSYNCED) == false);
EXPECT_TRUE(B.Get(IS_UNAPPLIED_UPDATE) == false);
EXPECT_TRUE(B.Get(SERVER_VERSION) == 20);
}
}
TEST_F(SyncerTest, ConflictMatchingEntryHandlesNormalNames) {
mock_server_->AddUpdateDirectory(1, 0, "A", 10, 10);
mock_server_->AddUpdateDirectory(2, 0, "B", 10, 10);
mock_server_->set_conflict_all_commits(true);
SyncShareNudge();
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry A(&wtrans, GET_BY_ID, ids_.FromNumber(1));
ASSERT_TRUE(A.good());
A.Put(IS_UNSYNCED, true);
A.Put(IS_UNAPPLIED_UPDATE, true);
A.Put(SERVER_VERSION, 20);
MutableEntry B(&wtrans, GET_BY_ID, ids_.FromNumber(2));
ASSERT_TRUE(B.good());
B.Put(IS_UNAPPLIED_UPDATE, true);
B.Put(SERVER_VERSION, 20);
}
LoopSyncShare();
saw_syncer_event_ = false;
mock_server_->set_conflict_all_commits(false);
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry A(&trans, GET_BY_ID, ids_.FromNumber(1));
ASSERT_TRUE(A.good());
EXPECT_TRUE(A.Get(IS_UNSYNCED) == false);
EXPECT_TRUE(A.Get(IS_UNAPPLIED_UPDATE) == false);
EXPECT_TRUE(A.Get(SERVER_VERSION) == 20);
Entry B(&trans, GET_BY_ID, ids_.FromNumber(2));
ASSERT_TRUE(B.good());
EXPECT_TRUE(B.Get(IS_UNSYNCED) == false);
EXPECT_TRUE(B.Get(IS_UNAPPLIED_UPDATE) == false);
EXPECT_TRUE(B.Get(SERVER_VERSION) == 20);
}
}
TEST_F(SyncerTest, ReverseFolderOrderingTest) {
mock_server_->AddUpdateDirectory(4, 3, "ggchild", 10, 10);
mock_server_->AddUpdateDirectory(3, 2, "gchild", 10, 10);
mock_server_->AddUpdateDirectory(5, 4, "gggchild", 10, 10);
mock_server_->AddUpdateDirectory(2, 1, "child", 10, 10);
mock_server_->AddUpdateDirectory(1, 0, "parent", 10, 10);
LoopSyncShare();
syncable::ReadTransaction trans(FROM_HERE, directory());
Id child_id = GetOnlyEntryWithName(
&trans, ids_.FromNumber(4), "gggchild");
Entry child(&trans, GET_BY_ID, child_id);
ASSERT_TRUE(child.good());
}
class EntryCreatedInNewFolderTest : public SyncerTest {
public:
void CreateFolderInBob() {
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry bob(&trans,
syncable::GET_BY_ID,
GetOnlyEntryWithName(&trans,
TestIdFactory::root(),
"bob"));
CHECK(bob.good());
MutableEntry entry2(&trans, syncable::CREATE, bob.Get(syncable::ID),
"bob");
CHECK(entry2.good());
entry2.Put(syncable::IS_DIR, true);
entry2.Put(syncable::IS_UNSYNCED, true);
entry2.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
}
};
TEST_F(EntryCreatedInNewFolderTest, EntryCreatedInNewFolderMidSync) {
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, syncable::CREATE, trans.root_id(),
"bob");
ASSERT_TRUE(entry.good());
entry.Put(syncable::IS_DIR, true);
entry.Put(syncable::IS_UNSYNCED, true);
entry.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
}
mock_server_->SetMidCommitCallback(
base::Bind(&EntryCreatedInNewFolderTest::CreateFolderInBob,
base::Unretained(this)));
syncer_->SyncShare(session_.get(), COMMIT, SYNCER_END);
// We loop until no unsynced handles remain, so we will commit both ids.
EXPECT_EQ(2u, mock_server_->committed_ids().size());
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry parent_entry(&trans, syncable::GET_BY_ID,
GetOnlyEntryWithName(&trans, TestIdFactory::root(), "bob"));
ASSERT_TRUE(parent_entry.good());
Id child_id =
GetOnlyEntryWithName(&trans, parent_entry.Get(ID), "bob");
Entry child(&trans, syncable::GET_BY_ID, child_id);
ASSERT_TRUE(child.good());
EXPECT_EQ(parent_entry.Get(ID), child.Get(PARENT_ID));
}
}
TEST_F(SyncerTest, NegativeIDInUpdate) {
mock_server_->AddUpdateBookmark(-10, 0, "bad", 40, 40);
SyncShareNudge();
// The negative id would make us CHECK!
}
TEST_F(SyncerTest, UnappliedUpdateOnCreatedItemItemDoesNotCrash) {
int64 metahandle_fred;
{
// Create an item.
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry fred_match(&trans, CREATE, trans.root_id(),
"fred_match");
ASSERT_TRUE(fred_match.good());
metahandle_fred = fred_match.Get(META_HANDLE);
WriteTestDataToEntry(&trans, &fred_match);
}
// Commit it.
SyncShareNudge();
EXPECT_EQ(1u, mock_server_->committed_ids().size());
mock_server_->set_conflict_all_commits(true);
syncable::Id fred_match_id;
{
// Now receive a change from outside.
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry fred_match(&trans, GET_BY_HANDLE, metahandle_fred);
ASSERT_TRUE(fred_match.good());
EXPECT_TRUE(fred_match.Get(ID).ServerKnows());
fred_match_id = fred_match.Get(ID);
mock_server_->AddUpdateBookmark(fred_match_id, trans.root_id(),
"fred_match", 40, 40);
}
// Run the syncer.
for (int i = 0 ; i < 30 ; ++i) {
SyncShareNudge();
}
}
/**
* In the event that we have a double changed entry, that is changed on both
* the client and the server, the conflict resolver should just drop one of
* them and accept the other.
*/
TEST_F(SyncerTest, DoublyChangedWithResolver) {
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&wtrans, syncable::CREATE, root_id_, "Folder");
ASSERT_TRUE(parent.good());
parent.Put(syncable::IS_DIR, true);
parent.Put(syncable::ID, parent_id_);
parent.Put(syncable::BASE_VERSION, 5);
parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
MutableEntry child(&wtrans, syncable::CREATE, parent_id_, "Pete.htm");
ASSERT_TRUE(child.good());
child.Put(syncable::ID, child_id_);
child.Put(syncable::BASE_VERSION, 10);
WriteTestDataToEntry(&wtrans, &child);
}
mock_server_->AddUpdateBookmark(child_id_, parent_id_, "Pete2.htm", 11, 10);
mock_server_->set_conflict_all_commits(true);
LoopSyncShare();
syncable::Directory::ChildHandles children;
{
syncable::ReadTransaction trans(FROM_HERE, directory());
directory()->GetChildHandlesById(&trans, parent_id_, &children);
// We expect the conflict resolver to preserve the local entry.
Entry child(&trans, syncable::GET_BY_ID, child_id_);
ASSERT_TRUE(child.good());
EXPECT_TRUE(child.Get(syncable::IS_UNSYNCED));
EXPECT_FALSE(child.Get(syncable::IS_UNAPPLIED_UPDATE));
EXPECT_TRUE(child.Get(SPECIFICS).has_bookmark());
EXPECT_EQ("Pete.htm", child.Get(NON_UNIQUE_NAME));
VerifyTestBookmarkDataInEntry(&child);
}
// Only one entry, since we just overwrite one.
EXPECT_EQ(1u, children.size());
saw_syncer_event_ = false;
}
// We got this repro case when someone was editing bookmarks while sync was
// occuring. The entry had changed out underneath the user.
TEST_F(SyncerTest, CommitsUpdateDoesntAlterEntry) {
const base::Time& test_time = ProtoTimeToTime(123456);
int64 entry_metahandle;
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&wtrans, syncable::CREATE, root_id_, "Pete");
ASSERT_TRUE(entry.good());
EXPECT_FALSE(entry.Get(ID).ServerKnows());
entry.Put(syncable::IS_DIR, true);
entry.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics());
entry.Put(syncable::IS_UNSYNCED, true);
entry.Put(syncable::MTIME, test_time);
entry_metahandle = entry.Get(META_HANDLE);
}
SyncShareNudge();
syncable::Id id;
int64 version;
int64 server_position_in_parent;
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry entry(&trans, syncable::GET_BY_HANDLE, entry_metahandle);
ASSERT_TRUE(entry.good());
id = entry.Get(ID);
EXPECT_TRUE(id.ServerKnows());
version = entry.Get(BASE_VERSION);
server_position_in_parent = entry.Get(SERVER_POSITION_IN_PARENT);
}
sync_pb::SyncEntity* update = mock_server_->AddUpdateFromLastCommit();
EXPECT_EQ("Pete", update->name());
EXPECT_EQ(id.GetServerId(), update->id_string());
EXPECT_EQ(root_id_.GetServerId(), update->parent_id_string());
EXPECT_EQ(version, update->version());
EXPECT_EQ(server_position_in_parent, update->position_in_parent());
SyncShareNudge();
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Entry entry(&trans, syncable::GET_BY_ID, id);
ASSERT_TRUE(entry.good());
EXPECT_TRUE(entry.Get(MTIME) == test_time);
}
}
TEST_F(SyncerTest, ParentAndChildBothMatch) {
const FullModelTypeSet all_types = FullModelTypeSet::All();
syncable::Id parent_id = ids_.NewServerId();
syncable::Id child_id = ids_.NewServerId();
{
WriteTransaction wtrans(FROM_HERE, UNITTEST, directory());
MutableEntry parent(&wtrans, CREATE, root_id_, "Folder");
ASSERT_TRUE(parent.good());
parent.Put(IS_DIR, true);
parent.Put(IS_UNSYNCED, true);
parent.Put(ID, parent_id);
parent.Put(BASE_VERSION, 1);
parent.Put(SPECIFICS, DefaultBookmarkSpecifics());
MutableEntry child(&wtrans, CREATE, parent.Get(ID), "test.htm");
ASSERT_TRUE(child.good());
child.Put(ID, child_id);
child.Put(BASE_VERSION, 1);
child.Put(SPECIFICS, DefaultBookmarkSpecifics());
WriteTestDataToEntry(&wtrans, &child);
}
mock_server_->AddUpdateDirectory(parent_id, root_id_, "Folder", 10, 10);
mock_server_->AddUpdateBookmark(child_id, parent_id, "test.htm", 10, 10);
mock_server_->set_conflict_all_commits(true);
SyncShareNudge();
SyncShareNudge();
SyncShareNudge();
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Directory::ChildHandles children;
directory()->GetChildHandlesById(&trans, root_id_, &children);
EXPECT_EQ(1u, children.size());
directory()->GetChildHandlesById(&trans, parent_id, &children);
EXPECT_EQ(1u, children.size());
std::vector<int64> unapplied;
directory()->GetUnappliedUpdateMetaHandles(&trans, all_types, &unapplied);
EXPECT_EQ(0u, unapplied.size());
syncable::Directory::UnsyncedMetaHandles unsynced;
directory()->GetUnsyncedMetaHandles(&trans, &unsynced);
EXPECT_EQ(0u, unsynced.size());
saw_syncer_event_ = false;
}
}
TEST_F(SyncerTest, CommittingNewDeleted) {
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, CREATE, trans.root_id(), "bob");
entry.Put(IS_UNSYNCED, true);
entry.Put(IS_DEL, true);
}
SyncShareNudge();
EXPECT_EQ(0u, mock_server_->committed_ids().size());
}
// Original problem synopsis:
// Check failed: entry->Get(BASE_VERSION) <= entry->Get(SERVER_VERSION)
// Client creates entry, client finishes committing entry. Between
// commit and getting update back, we delete the entry.
// We get the update for the entry, but the local one was modified
// so we store the entry but don't apply it. IS_UNAPPLIED_UPDATE is set.
// We commit deletion and get a new version number.
// We apply unapplied updates again before we get the update about the deletion.
// This means we have an unapplied update where server_version < base_version.
TEST_F(SyncerTest, UnappliedUpdateDuringCommit) {
// This test is a little fake.
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, CREATE, trans.root_id(), "bob");
entry.Put(ID, ids_.FromNumber(20));
entry.Put(BASE_VERSION, 1);
entry.Put(SERVER_VERSION, 1);
entry.Put(SERVER_PARENT_ID, ids_.FromNumber(9999)); // Bad parent.
entry.Put(IS_UNSYNCED, true);
entry.Put(IS_UNAPPLIED_UPDATE, true);
entry.Put(SPECIFICS, DefaultBookmarkSpecifics());
entry.Put(SERVER_SPECIFICS, DefaultBookmarkSpecifics());
entry.Put(IS_DEL, false);
}
syncer_->SyncShare(session_.get(), SYNCER_BEGIN, SYNCER_END);
syncer_->SyncShare(session_.get(), SYNCER_BEGIN, SYNCER_END);
EXPECT_EQ(1, session_->status_controller().TotalNumConflictingItems());
saw_syncer_event_ = false;
}
// Original problem synopsis:
// Illegal parent
// Unexpected error during sync if we:
// make a new folder bob
// wait for sync
// make a new folder fred
// move bob into fred
// remove bob
// remove fred
// if no syncing occured midway, bob will have an illegal parent
TEST_F(SyncerTest, DeletingEntryInFolder) {
// This test is a little fake.
int64 existing_metahandle;
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, CREATE, trans.root_id(), "existing");
ASSERT_TRUE(entry.good());
entry.Put(IS_DIR, true);
entry.Put(SPECIFICS, DefaultBookmarkSpecifics());
entry.Put(IS_UNSYNCED, true);
existing_metahandle = entry.Get(META_HANDLE);
}
syncer_->SyncShare(session_.get(), SYNCER_BEGIN, SYNCER_END);
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry newfolder(&trans, CREATE, trans.root_id(), "new");
ASSERT_TRUE(newfolder.good());
newfolder.Put(IS_DIR, true);
newfolder.Put(SPECIFICS, DefaultBookmarkSpecifics());
newfolder.Put(IS_UNSYNCED, true);