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
|
// Copyright 2014 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.
#include "components/sync/engine_impl/syncer_util.h"
#include <memory>
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/rand_util.h"
#include "components/sync/base/unique_position.h"
#include "components/sync/engine_impl/test_entry_factory.h"
#include "components/sync/protocol/sync.pb.h"
#include "components/sync/syncable/syncable_write_transaction.h"
#include "components/sync/test/engine/test_directory_setter_upper.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
class GetUpdatePositionTest : public ::testing::Test {
public:
void SetUp() override {
dir_maker_.SetUp();
entry_factory_ = base::MakeUnique<TestEntryFactory>(directory());
}
void TearDown() override { dir_maker_.TearDown(); }
syncable::Directory* directory() { return dir_maker_.directory(); }
TestEntryFactory* entry_factory() { return entry_factory_.get(); }
GetUpdatePositionTest() {
InitUpdate();
// Init test_position to some valid position value, but don't assign
// it to the update just yet.
std::string pos_suffix = UniquePosition::RandomSuffix();
test_position = UniquePosition::InitialPosition(pos_suffix);
}
void InitUpdate() {
update.set_id_string("I");
update.set_parent_id_string("P");
update.set_version(10);
update.set_mtime(100);
update.set_ctime(100);
update.set_deleted(false);
update.mutable_specifics()->mutable_bookmark()->set_title("Chrome");
update.mutable_specifics()->mutable_bookmark()->set_url(
"https://www.chrome.com");
}
void InitSuffixIngredients() {
update.set_originator_cache_guid("CacheGUID");
update.set_originator_client_item_id("OrigID");
}
void InitProtoPosition() {
test_position.ToProto(update.mutable_unique_position());
}
void InitInt64Position(int64_t pos_value) {
update.set_position_in_parent(pos_value);
}
sync_pb::SyncEntity update;
UniquePosition test_position;
base::MessageLoop message_loop_;
TestDirectorySetterUpper dir_maker_;
std::unique_ptr<TestEntryFactory> entry_factory_;
};
// Generate a suffix from originator client GUID and client-assigned ID. These
// values should always be present in updates sent down to the client, and
// combine to create a globally unique value.
TEST_F(GetUpdatePositionTest, SuffixFromUpdate) {
InitSuffixIngredients();
// Expect suffix is valid and consistent.
std::string suffix1 = GetUniqueBookmarkTagFromUpdate(update);
std::string suffix2 = GetUniqueBookmarkTagFromUpdate(update);
EXPECT_EQ(suffix1, suffix2);
EXPECT_TRUE(UniquePosition::IsValidSuffix(suffix1));
}
// Receive an update without the ingredients used to make a consistent suffix.
//
// The server should never send us an update like this. If it does,
// that's a bug and it needs to be fixed. Still, we'd like to not
// crash and have fairly reasonable results in this scenario.
TEST_F(GetUpdatePositionTest, SuffixFromRandom) {
// Intentonally do not call InitSuffixIngredients()
// Expect suffix is valid but inconsistent.
std::string suffix1 = GetUniqueBookmarkTagFromUpdate(update);
std::string suffix2 = GetUniqueBookmarkTagFromUpdate(update);
EXPECT_NE(suffix1, suffix2);
EXPECT_TRUE(UniquePosition::IsValidSuffix(suffix1));
EXPECT_TRUE(UniquePosition::IsValidSuffix(suffix2));
}
TEST_F(GetUpdatePositionTest, FromInt64) {
InitSuffixIngredients();
InitInt64Position(10);
std::string suffix = GetUniqueBookmarkTagFromUpdate(update);
// Expect the result is valid.
UniquePosition pos = GetUpdatePosition(update, suffix);
EXPECT_TRUE(pos.IsValid());
// Expect the position had some effect on ordering.
EXPECT_TRUE(pos.LessThan(
UniquePosition::FromInt64(11, UniquePosition::RandomSuffix())));
}
TEST_F(GetUpdatePositionTest, FromProto) {
InitSuffixIngredients();
InitInt64Position(10);
std::string suffix = GetUniqueBookmarkTagFromUpdate(update);
// The proto position is not set, so we should get one based on the int64_t.
// It should not match the proto we defined in the test harness.
UniquePosition int64_pos = GetUpdatePosition(update, suffix);
EXPECT_FALSE(int64_pos.Equals(test_position));
// Move the test harness' position value into the update proto.
// Expect that it takes precedence over the int64_t-based position.
InitProtoPosition();
UniquePosition pos = GetUpdatePosition(update, suffix);
EXPECT_TRUE(pos.Equals(test_position));
}
TEST_F(GetUpdatePositionTest, FromNothing) {
// Init none of the ingredients necessary to make a position.
// Verify we still generate a valid position locally.
std::string suffix = GetUniqueBookmarkTagFromUpdate(update);
UniquePosition pos = GetUpdatePosition(update, suffix);
EXPECT_TRUE(pos.IsValid());
}
namespace {
sync_pb::EntitySpecifics DefaultBookmarkSpecifics() {
sync_pb::EntitySpecifics result;
AddDefaultFieldValue(BOOKMARKS, &result);
return result;
}
} // namespace
// Checks that whole cycle of unique_position updating from
// server works fine and does not browser crash.
TEST_F(GetUpdatePositionTest, UpdateServerFieldsFromUpdateTest) {
InitSuffixIngredients(); // Initialize update with valid data.
std::string root_server_id = syncable::Id::GetRoot().GetServerId();
int64_t handle = entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
"I", DefaultBookmarkSpecifics(), root_server_id);
syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, directory());
syncable::MutableEntry target(&trans, syncable::GET_BY_HANDLE, handle);
// Before update, target has invalid bookmark tag and unique position.
EXPECT_FALSE(UniquePosition::IsValidSuffix(target.GetUniqueBookmarkTag()));
EXPECT_FALSE(target.GetServerUniquePosition().IsValid());
UpdateServerFieldsFromUpdate(&target, update, "name");
// After update, target has valid bookmark tag and unique position.
EXPECT_TRUE(UniquePosition::IsValidSuffix(target.GetUniqueBookmarkTag()));
EXPECT_TRUE(target.GetServerUniquePosition().IsValid());
}
// Checks that whole cycle of unique_position updating does not
// browser crash even data from server is invalid.
// It looks like server bug, but browser should not crash and work further.
TEST_F(GetUpdatePositionTest, UpdateServerFieldsFromInvalidUpdateTest) {
// Do not initialize data in update, update is invalid.
std::string root_server_id = syncable::Id::GetRoot().GetServerId();
int64_t handle = entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
"I", DefaultBookmarkSpecifics(), root_server_id);
syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, directory());
syncable::MutableEntry target(&trans, syncable::GET_BY_HANDLE, handle);
// Before update, target has invalid bookmark tag and unique position.
EXPECT_FALSE(UniquePosition::IsValidSuffix(target.GetUniqueBookmarkTag()));
EXPECT_FALSE(target.GetServerUniquePosition().IsValid());
UpdateServerFieldsFromUpdate(&target, update, "name");
// After update, target has valid bookmark tag and unique position.
EXPECT_TRUE(UniquePosition::IsValidSuffix(target.GetUniqueBookmarkTag()));
EXPECT_TRUE(target.GetServerUniquePosition().IsValid());
}
TEST_F(GetUpdatePositionTest, UpdateServerFieldsFromInvalidUniquePositionTest) {
InitSuffixIngredients(); // Initialize update with valid data.
sync_pb::SyncEntity invalid_update(update);
// Create and Setup an invalid position
sync_pb::UniquePosition* invalid_position = new sync_pb::UniquePosition();
invalid_position->set_value("");
invalid_update.set_allocated_unique_position(invalid_position);
std::string root_server_id = syncable::Id::GetRoot().GetServerId();
int64_t handle = entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
"I", DefaultBookmarkSpecifics(), root_server_id);
syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, directory());
syncable::MutableEntry target(&trans, syncable::GET_BY_HANDLE, handle);
// Before update, target has invalid bookmark tag and unique position.
EXPECT_FALSE(UniquePosition::IsValidSuffix(target.GetUniqueBookmarkTag()));
EXPECT_FALSE(target.GetServerUniquePosition().IsValid());
UpdateServerFieldsFromUpdate(&target, invalid_update, "name");
// After update, target has valid bookmark tag and unique position.
EXPECT_TRUE(UniquePosition::IsValidSuffix(target.GetUniqueBookmarkTag()));
EXPECT_TRUE(target.GetServerUniquePosition().IsValid());
}
} // namespace syncer
|