| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Unit tests for the SyncApi. Note that a lot of the underlying | |
| 6 // functionality is provided by the Syncable layer, which has its own | |
| 7 // unit tests. We'll test SyncApi specific things in this harness. | |
| 8 | |
| 9 #include "components/sync/core_impl/sync_manager_impl.h" | |
| 10 | |
| 11 #include <cstddef> | |
| 12 #include <memory> | |
| 13 #include <utility> | |
| 14 | |
| 15 #include "base/callback.h" | |
| 16 #include "base/compiler_specific.h" | |
| 17 #include "base/files/scoped_temp_dir.h" | |
| 18 #include "base/format_macros.h" | |
| 19 #include "base/location.h" | |
| 20 #include "base/run_loop.h" | |
| 21 #include "base/strings/string_number_conversions.h" | |
| 22 #include "base/strings/stringprintf.h" | |
| 23 #include "base/strings/utf_string_conversions.h" | |
| 24 #include "base/test/values_test_util.h" | |
| 25 #include "base/values.h" | |
| 26 #include "components/sync/base/attachment_id_proto.h" | |
| 27 #include "components/sync/base/cancelation_signal.h" | |
| 28 #include "components/sync/base/extensions_activity.h" | |
| 29 #include "components/sync/base/fake_encryptor.h" | |
| 30 #include "components/sync/base/mock_unrecoverable_error_handler.h" | |
| 31 #include "components/sync/base/model_type_test_util.h" | |
| 32 #include "components/sync/core/test/test_entry_factory.h" | |
| 33 #include "components/sync/core/test/test_internal_components_factory.h" | |
| 34 #include "components/sync/core_impl/syncapi_internal.h" | |
| 35 #include "components/sync/engine/events/protocol_event.h" | |
| 36 #include "components/sync/engine/model_safe_worker.h" | |
| 37 #include "components/sync/engine/net/http_post_provider_factory.h" | |
| 38 #include "components/sync/engine/net/http_post_provider_interface.h" | |
| 39 #include "components/sync/engine/polling_constants.h" | |
| 40 #include "components/sync/engine_impl/cycle/sync_cycle.h" | |
| 41 #include "components/sync/engine_impl/sync_scheduler.h" | |
| 42 #include "components/sync/js/js_event_handler.h" | |
| 43 #include "components/sync/js/js_test_util.h" | |
| 44 #include "components/sync/protocol/bookmark_specifics.pb.h" | |
| 45 #include "components/sync/protocol/encryption.pb.h" | |
| 46 #include "components/sync/protocol/extension_specifics.pb.h" | |
| 47 #include "components/sync/protocol/password_specifics.pb.h" | |
| 48 #include "components/sync/protocol/preference_specifics.pb.h" | |
| 49 #include "components/sync/protocol/proto_value_conversions.h" | |
| 50 #include "components/sync/protocol/sync.pb.h" | |
| 51 #include "components/sync/syncable/change_record.h" | |
| 52 #include "components/sync/syncable/directory.h" | |
| 53 #include "components/sync/syncable/entry.h" | |
| 54 #include "components/sync/syncable/mutable_entry.h" | |
| 55 #include "components/sync/syncable/nigori_util.h" | |
| 56 #include "components/sync/syncable/read_node.h" | |
| 57 #include "components/sync/syncable/read_transaction.h" | |
| 58 #include "components/sync/syncable/syncable_id.h" | |
| 59 #include "components/sync/syncable/syncable_read_transaction.h" | |
| 60 #include "components/sync/syncable/syncable_util.h" | |
| 61 #include "components/sync/syncable/syncable_write_transaction.h" | |
| 62 #include "components/sync/syncable/test_user_share.h" | |
| 63 #include "components/sync/syncable/write_node.h" | |
| 64 #include "components/sync/syncable/write_transaction.h" | |
| 65 #include "components/sync/test/callback_counter.h" | |
| 66 #include "components/sync/test/engine/fake_model_worker.h" | |
| 67 #include "components/sync/test/engine/fake_sync_scheduler.h" | |
| 68 #include "components/sync/test/engine/test_id_factory.h" | |
| 69 #include "google_apis/gaia/gaia_constants.h" | |
| 70 #include "testing/gmock/include/gmock/gmock.h" | |
| 71 #include "testing/gtest/include/gtest/gtest.h" | |
| 72 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | |
| 73 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite
.h" | |
| 74 #include "url/gurl.h" | |
| 75 | |
| 76 using base::ExpectDictStringValue; | |
| 77 using testing::_; | |
| 78 using testing::DoAll; | |
| 79 using testing::InSequence; | |
| 80 using testing::Return; | |
| 81 using testing::SaveArg; | |
| 82 using testing::StrictMock; | |
| 83 | |
| 84 namespace syncer { | |
| 85 | |
| 86 using syncable::GET_BY_HANDLE; | |
| 87 using syncable::IS_DEL; | |
| 88 using syncable::IS_UNSYNCED; | |
| 89 using syncable::NON_UNIQUE_NAME; | |
| 90 using syncable::SPECIFICS; | |
| 91 using syncable::kEncryptedString; | |
| 92 | |
| 93 namespace { | |
| 94 | |
| 95 // Makes a child node under the type root folder. Returns the id of the | |
| 96 // newly-created node. | |
| 97 int64_t MakeNode(UserShare* share, | |
| 98 ModelType model_type, | |
| 99 const std::string& client_tag) { | |
| 100 WriteTransaction trans(FROM_HERE, share); | |
| 101 WriteNode node(&trans); | |
| 102 WriteNode::InitUniqueByCreationResult result = | |
| 103 node.InitUniqueByCreation(model_type, client_tag); | |
| 104 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); | |
| 105 node.SetIsFolder(false); | |
| 106 return node.GetId(); | |
| 107 } | |
| 108 | |
| 109 // Makes a non-folder child of the root node. Returns the id of the | |
| 110 // newly-created node. | |
| 111 int64_t MakeNodeWithRoot(UserShare* share, | |
| 112 ModelType model_type, | |
| 113 const std::string& client_tag) { | |
| 114 WriteTransaction trans(FROM_HERE, share); | |
| 115 ReadNode root_node(&trans); | |
| 116 root_node.InitByRootLookup(); | |
| 117 WriteNode node(&trans); | |
| 118 WriteNode::InitUniqueByCreationResult result = | |
| 119 node.InitUniqueByCreation(model_type, root_node, client_tag); | |
| 120 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); | |
| 121 node.SetIsFolder(false); | |
| 122 return node.GetId(); | |
| 123 } | |
| 124 | |
| 125 // Makes a folder child of a non-root node. Returns the id of the | |
| 126 // newly-created node. | |
| 127 int64_t MakeFolderWithParent(UserShare* share, | |
| 128 ModelType model_type, | |
| 129 int64_t parent_id, | |
| 130 BaseNode* predecessor) { | |
| 131 WriteTransaction trans(FROM_HERE, share); | |
| 132 ReadNode parent_node(&trans); | |
| 133 EXPECT_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id)); | |
| 134 WriteNode node(&trans); | |
| 135 EXPECT_TRUE(node.InitBookmarkByCreation(parent_node, predecessor)); | |
| 136 node.SetIsFolder(true); | |
| 137 return node.GetId(); | |
| 138 } | |
| 139 | |
| 140 int64_t MakeBookmarkWithParent(UserShare* share, | |
| 141 int64_t parent_id, | |
| 142 BaseNode* predecessor) { | |
| 143 WriteTransaction trans(FROM_HERE, share); | |
| 144 ReadNode parent_node(&trans); | |
| 145 EXPECT_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id)); | |
| 146 WriteNode node(&trans); | |
| 147 EXPECT_TRUE(node.InitBookmarkByCreation(parent_node, predecessor)); | |
| 148 return node.GetId(); | |
| 149 } | |
| 150 | |
| 151 // Creates the "synced" root node for a particular datatype. We use the syncable | |
| 152 // methods here so that the syncer treats these nodes as if they were already | |
| 153 // received from the server. | |
| 154 int64_t MakeTypeRoot(UserShare* share, ModelType model_type) { | |
| 155 sync_pb::EntitySpecifics specifics; | |
| 156 AddDefaultFieldValue(model_type, &specifics); | |
| 157 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, | |
| 158 share->directory.get()); | |
| 159 // Attempt to lookup by nigori tag. | |
| 160 std::string type_tag = ModelTypeToRootTag(model_type); | |
| 161 syncable::Id node_id = syncable::Id::CreateFromServerId(type_tag); | |
| 162 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | |
| 163 node_id); | |
| 164 EXPECT_TRUE(entry.good()); | |
| 165 entry.PutBaseVersion(1); | |
| 166 entry.PutServerVersion(1); | |
| 167 entry.PutIsUnappliedUpdate(false); | |
| 168 entry.PutParentId(syncable::Id::GetRoot()); | |
| 169 entry.PutServerParentId(syncable::Id::GetRoot()); | |
| 170 entry.PutServerIsDir(true); | |
| 171 entry.PutIsDir(true); | |
| 172 entry.PutServerSpecifics(specifics); | |
| 173 entry.PutSpecifics(specifics); | |
| 174 entry.PutUniqueServerTag(type_tag); | |
| 175 entry.PutNonUniqueName(type_tag); | |
| 176 entry.PutIsDel(false); | |
| 177 return entry.GetMetahandle(); | |
| 178 } | |
| 179 | |
| 180 // Simulates creating a "synced" node as a child of the root datatype node. | |
| 181 int64_t MakeServerNode(UserShare* share, | |
| 182 ModelType model_type, | |
| 183 const std::string& client_tag, | |
| 184 const std::string& hashed_tag, | |
| 185 const sync_pb::EntitySpecifics& specifics) { | |
| 186 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, | |
| 187 share->directory.get()); | |
| 188 syncable::Entry root_entry(&trans, syncable::GET_TYPE_ROOT, model_type); | |
| 189 EXPECT_TRUE(root_entry.good()); | |
| 190 syncable::Id root_id = root_entry.GetId(); | |
| 191 syncable::Id node_id = syncable::Id::CreateFromServerId(client_tag); | |
| 192 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | |
| 193 node_id); | |
| 194 EXPECT_TRUE(entry.good()); | |
| 195 entry.PutBaseVersion(1); | |
| 196 entry.PutServerVersion(1); | |
| 197 entry.PutIsUnappliedUpdate(false); | |
| 198 entry.PutServerParentId(root_id); | |
| 199 entry.PutParentId(root_id); | |
| 200 entry.PutServerIsDir(false); | |
| 201 entry.PutIsDir(false); | |
| 202 entry.PutServerSpecifics(specifics); | |
| 203 entry.PutSpecifics(specifics); | |
| 204 entry.PutNonUniqueName(client_tag); | |
| 205 entry.PutUniqueClientTag(hashed_tag); | |
| 206 entry.PutIsDel(false); | |
| 207 return entry.GetMetahandle(); | |
| 208 } | |
| 209 | |
| 210 int GetTotalNodeCount(UserShare* share, int64_t root) { | |
| 211 ReadTransaction trans(FROM_HERE, share); | |
| 212 ReadNode node(&trans); | |
| 213 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(root)); | |
| 214 return node.GetTotalNodeCount(); | |
| 215 } | |
| 216 | |
| 217 const char kUrl[] = "example.com"; | |
| 218 const char kPasswordValue[] = "secret"; | |
| 219 const char kClientTag[] = "tag"; | |
| 220 | |
| 221 } // namespace | |
| 222 | |
| 223 class SyncApiTest : public testing::Test { | |
| 224 public: | |
| 225 void SetUp() override { test_user_share_.SetUp(); } | |
| 226 | |
| 227 void TearDown() override { test_user_share_.TearDown(); } | |
| 228 | |
| 229 protected: | |
| 230 // Create an entry with the given |model_type|, |client_tag| and | |
| 231 // |attachment_metadata|. | |
| 232 void CreateEntryWithAttachmentMetadata( | |
| 233 const ModelType& model_type, | |
| 234 const std::string& client_tag, | |
| 235 const sync_pb::AttachmentMetadata& attachment_metadata); | |
| 236 | |
| 237 // Attempts to load the entry specified by |model_type| and |client_tag| and | |
| 238 // returns the lookup result code. | |
| 239 BaseNode::InitByLookupResult LookupEntryByClientTag( | |
| 240 const ModelType& model_type, | |
| 241 const std::string& client_tag); | |
| 242 | |
| 243 // Replace the entry specified by |model_type| and |client_tag| with a | |
| 244 // tombstone. | |
| 245 void ReplaceWithTombstone(const ModelType& model_type, | |
| 246 const std::string& client_tag); | |
| 247 | |
| 248 // Save changes to the Directory, destroy it then reload it. | |
| 249 bool ReloadDir(); | |
| 250 | |
| 251 UserShare* user_share(); | |
| 252 syncable::Directory* dir(); | |
| 253 SyncEncryptionHandler* encryption_handler(); | |
| 254 PassphraseType GetPassphraseType(BaseTransaction* trans); | |
| 255 | |
| 256 private: | |
| 257 base::MessageLoop message_loop_; | |
| 258 TestUserShare test_user_share_; | |
| 259 }; | |
| 260 | |
| 261 UserShare* SyncApiTest::user_share() { | |
| 262 return test_user_share_.user_share(); | |
| 263 } | |
| 264 | |
| 265 syncable::Directory* SyncApiTest::dir() { | |
| 266 return test_user_share_.user_share()->directory.get(); | |
| 267 } | |
| 268 | |
| 269 SyncEncryptionHandler* SyncApiTest::encryption_handler() { | |
| 270 return test_user_share_.encryption_handler(); | |
| 271 } | |
| 272 | |
| 273 PassphraseType SyncApiTest::GetPassphraseType(BaseTransaction* trans) { | |
| 274 return dir()->GetNigoriHandler()->GetPassphraseType(trans->GetWrappedTrans()); | |
| 275 } | |
| 276 | |
| 277 bool SyncApiTest::ReloadDir() { | |
| 278 return test_user_share_.Reload(); | |
| 279 } | |
| 280 | |
| 281 void SyncApiTest::CreateEntryWithAttachmentMetadata( | |
| 282 const ModelType& model_type, | |
| 283 const std::string& client_tag, | |
| 284 const sync_pb::AttachmentMetadata& attachment_metadata) { | |
| 285 WriteTransaction trans(FROM_HERE, user_share()); | |
| 286 ReadNode root_node(&trans); | |
| 287 root_node.InitByRootLookup(); | |
| 288 WriteNode node(&trans); | |
| 289 ASSERT_EQ(node.InitUniqueByCreation(model_type, root_node, client_tag), | |
| 290 WriteNode::INIT_SUCCESS); | |
| 291 node.SetAttachmentMetadata(attachment_metadata); | |
| 292 } | |
| 293 | |
| 294 BaseNode::InitByLookupResult SyncApiTest::LookupEntryByClientTag( | |
| 295 const ModelType& model_type, | |
| 296 const std::string& client_tag) { | |
| 297 ReadTransaction trans(FROM_HERE, user_share()); | |
| 298 ReadNode node(&trans); | |
| 299 return node.InitByClientTagLookup(model_type, client_tag); | |
| 300 } | |
| 301 | |
| 302 void SyncApiTest::ReplaceWithTombstone(const ModelType& model_type, | |
| 303 const std::string& client_tag) { | |
| 304 WriteTransaction trans(FROM_HERE, user_share()); | |
| 305 WriteNode node(&trans); | |
| 306 ASSERT_EQ(node.InitByClientTagLookup(model_type, client_tag), | |
| 307 WriteNode::INIT_OK); | |
| 308 node.Tombstone(); | |
| 309 } | |
| 310 | |
| 311 TEST_F(SyncApiTest, SanityCheckTest) { | |
| 312 { | |
| 313 ReadTransaction trans(FROM_HERE, user_share()); | |
| 314 EXPECT_TRUE(trans.GetWrappedTrans()); | |
| 315 } | |
| 316 { | |
| 317 WriteTransaction trans(FROM_HERE, user_share()); | |
| 318 EXPECT_TRUE(trans.GetWrappedTrans()); | |
| 319 } | |
| 320 { | |
| 321 // No entries but root should exist | |
| 322 ReadTransaction trans(FROM_HERE, user_share()); | |
| 323 ReadNode node(&trans); | |
| 324 // Metahandle 1 can be root, sanity check 2 | |
| 325 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, node.InitByIdLookup(2)); | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 TEST_F(SyncApiTest, BasicTagWrite) { | |
| 330 { | |
| 331 ReadTransaction trans(FROM_HERE, user_share()); | |
| 332 ReadNode root_node(&trans); | |
| 333 root_node.InitByRootLookup(); | |
| 334 EXPECT_EQ(kInvalidId, root_node.GetFirstChildId()); | |
| 335 } | |
| 336 | |
| 337 ignore_result(MakeNodeWithRoot(user_share(), BOOKMARKS, "testtag")); | |
| 338 | |
| 339 { | |
| 340 ReadTransaction trans(FROM_HERE, user_share()); | |
| 341 ReadNode node(&trans); | |
| 342 EXPECT_EQ(BaseNode::INIT_OK, | |
| 343 node.InitByClientTagLookup(BOOKMARKS, "testtag")); | |
| 344 EXPECT_NE(0, node.GetId()); | |
| 345 | |
| 346 ReadNode root_node(&trans); | |
| 347 root_node.InitByRootLookup(); | |
| 348 EXPECT_EQ(node.GetId(), root_node.GetFirstChildId()); | |
| 349 } | |
| 350 } | |
| 351 | |
| 352 TEST_F(SyncApiTest, BasicTagWriteWithImplicitParent) { | |
| 353 int64_t type_root = MakeTypeRoot(user_share(), PREFERENCES); | |
| 354 | |
| 355 { | |
| 356 ReadTransaction trans(FROM_HERE, user_share()); | |
| 357 ReadNode type_root_node(&trans); | |
| 358 EXPECT_EQ(BaseNode::INIT_OK, type_root_node.InitByIdLookup(type_root)); | |
| 359 EXPECT_EQ(kInvalidId, type_root_node.GetFirstChildId()); | |
| 360 } | |
| 361 | |
| 362 ignore_result(MakeNode(user_share(), PREFERENCES, "testtag")); | |
| 363 | |
| 364 { | |
| 365 ReadTransaction trans(FROM_HERE, user_share()); | |
| 366 ReadNode node(&trans); | |
| 367 EXPECT_EQ(BaseNode::INIT_OK, | |
| 368 node.InitByClientTagLookup(PREFERENCES, "testtag")); | |
| 369 EXPECT_EQ(kInvalidId, node.GetParentId()); | |
| 370 | |
| 371 ReadNode type_root_node(&trans); | |
| 372 EXPECT_EQ(BaseNode::INIT_OK, type_root_node.InitByIdLookup(type_root)); | |
| 373 EXPECT_EQ(node.GetId(), type_root_node.GetFirstChildId()); | |
| 374 } | |
| 375 } | |
| 376 | |
| 377 TEST_F(SyncApiTest, ModelTypesSiloed) { | |
| 378 { | |
| 379 WriteTransaction trans(FROM_HERE, user_share()); | |
| 380 ReadNode root_node(&trans); | |
| 381 root_node.InitByRootLookup(); | |
| 382 EXPECT_EQ(root_node.GetFirstChildId(), 0); | |
| 383 } | |
| 384 | |
| 385 ignore_result(MakeNodeWithRoot(user_share(), BOOKMARKS, "collideme")); | |
| 386 ignore_result(MakeNodeWithRoot(user_share(), PREFERENCES, "collideme")); | |
| 387 ignore_result(MakeNodeWithRoot(user_share(), AUTOFILL, "collideme")); | |
| 388 | |
| 389 { | |
| 390 ReadTransaction trans(FROM_HERE, user_share()); | |
| 391 | |
| 392 ReadNode bookmarknode(&trans); | |
| 393 EXPECT_EQ(BaseNode::INIT_OK, | |
| 394 bookmarknode.InitByClientTagLookup(BOOKMARKS, "collideme")); | |
| 395 | |
| 396 ReadNode prefnode(&trans); | |
| 397 EXPECT_EQ(BaseNode::INIT_OK, | |
| 398 prefnode.InitByClientTagLookup(PREFERENCES, "collideme")); | |
| 399 | |
| 400 ReadNode autofillnode(&trans); | |
| 401 EXPECT_EQ(BaseNode::INIT_OK, | |
| 402 autofillnode.InitByClientTagLookup(AUTOFILL, "collideme")); | |
| 403 | |
| 404 EXPECT_NE(bookmarknode.GetId(), prefnode.GetId()); | |
| 405 EXPECT_NE(autofillnode.GetId(), prefnode.GetId()); | |
| 406 EXPECT_NE(bookmarknode.GetId(), autofillnode.GetId()); | |
| 407 } | |
| 408 } | |
| 409 | |
| 410 TEST_F(SyncApiTest, ReadMissingTagsFails) { | |
| 411 { | |
| 412 ReadTransaction trans(FROM_HERE, user_share()); | |
| 413 ReadNode node(&trans); | |
| 414 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, | |
| 415 node.InitByClientTagLookup(BOOKMARKS, "testtag")); | |
| 416 } | |
| 417 { | |
| 418 WriteTransaction trans(FROM_HERE, user_share()); | |
| 419 WriteNode node(&trans); | |
| 420 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, | |
| 421 node.InitByClientTagLookup(BOOKMARKS, "testtag")); | |
| 422 } | |
| 423 } | |
| 424 | |
| 425 // TODO(chron): Hook this all up to the server and write full integration tests | |
| 426 // for update->undelete behavior. | |
| 427 TEST_F(SyncApiTest, TestDeleteBehavior) { | |
| 428 int64_t node_id; | |
| 429 int64_t folder_id; | |
| 430 std::string test_title("test1"); | |
| 431 | |
| 432 { | |
| 433 WriteTransaction trans(FROM_HERE, user_share()); | |
| 434 ReadNode root_node(&trans); | |
| 435 root_node.InitByRootLookup(); | |
| 436 | |
| 437 // we'll use this spare folder later | |
| 438 WriteNode folder_node(&trans); | |
| 439 EXPECT_TRUE(folder_node.InitBookmarkByCreation(root_node, NULL)); | |
| 440 folder_id = folder_node.GetId(); | |
| 441 | |
| 442 WriteNode wnode(&trans); | |
| 443 WriteNode::InitUniqueByCreationResult result = | |
| 444 wnode.InitUniqueByCreation(BOOKMARKS, root_node, "testtag"); | |
| 445 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); | |
| 446 wnode.SetIsFolder(false); | |
| 447 wnode.SetTitle(test_title); | |
| 448 | |
| 449 node_id = wnode.GetId(); | |
| 450 } | |
| 451 | |
| 452 // Ensure we can delete something with a tag. | |
| 453 { | |
| 454 WriteTransaction trans(FROM_HERE, user_share()); | |
| 455 WriteNode wnode(&trans); | |
| 456 EXPECT_EQ(BaseNode::INIT_OK, | |
| 457 wnode.InitByClientTagLookup(BOOKMARKS, "testtag")); | |
| 458 EXPECT_FALSE(wnode.GetIsFolder()); | |
| 459 EXPECT_EQ(wnode.GetTitle(), test_title); | |
| 460 | |
| 461 wnode.Tombstone(); | |
| 462 } | |
| 463 | |
| 464 // Lookup of a node which was deleted should return failure, | |
| 465 // but have found some data about the node. | |
| 466 { | |
| 467 ReadTransaction trans(FROM_HERE, user_share()); | |
| 468 ReadNode node(&trans); | |
| 469 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_IS_DEL, | |
| 470 node.InitByClientTagLookup(BOOKMARKS, "testtag")); | |
| 471 // Note that for proper function of this API this doesn't need to be | |
| 472 // filled, we're checking just to make sure the DB worked in this test. | |
| 473 EXPECT_EQ(node.GetTitle(), test_title); | |
| 474 } | |
| 475 | |
| 476 { | |
| 477 WriteTransaction trans(FROM_HERE, user_share()); | |
| 478 ReadNode folder_node(&trans); | |
| 479 EXPECT_EQ(BaseNode::INIT_OK, folder_node.InitByIdLookup(folder_id)); | |
| 480 | |
| 481 WriteNode wnode(&trans); | |
| 482 // This will undelete the tag. | |
| 483 WriteNode::InitUniqueByCreationResult result = | |
| 484 wnode.InitUniqueByCreation(BOOKMARKS, folder_node, "testtag"); | |
| 485 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); | |
| 486 EXPECT_EQ(wnode.GetIsFolder(), false); | |
| 487 EXPECT_EQ(wnode.GetParentId(), folder_node.GetId()); | |
| 488 EXPECT_EQ(wnode.GetId(), node_id); | |
| 489 EXPECT_NE(wnode.GetTitle(), test_title); // Title should be cleared | |
| 490 wnode.SetTitle(test_title); | |
| 491 } | |
| 492 | |
| 493 // Now look up should work. | |
| 494 { | |
| 495 ReadTransaction trans(FROM_HERE, user_share()); | |
| 496 ReadNode node(&trans); | |
| 497 EXPECT_EQ(BaseNode::INIT_OK, | |
| 498 node.InitByClientTagLookup(BOOKMARKS, "testtag")); | |
| 499 EXPECT_EQ(node.GetTitle(), test_title); | |
| 500 EXPECT_EQ(node.GetModelType(), BOOKMARKS); | |
| 501 } | |
| 502 } | |
| 503 | |
| 504 TEST_F(SyncApiTest, WriteAndReadPassword) { | |
| 505 KeyParams params = {"localhost", "username", "passphrase"}; | |
| 506 { | |
| 507 ReadTransaction trans(FROM_HERE, user_share()); | |
| 508 trans.GetCryptographer()->AddKey(params); | |
| 509 } | |
| 510 { | |
| 511 WriteTransaction trans(FROM_HERE, user_share()); | |
| 512 ReadNode root_node(&trans); | |
| 513 root_node.InitByRootLookup(); | |
| 514 | |
| 515 WriteNode password_node(&trans); | |
| 516 WriteNode::InitUniqueByCreationResult result = | |
| 517 password_node.InitUniqueByCreation(PASSWORDS, root_node, kClientTag); | |
| 518 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); | |
| 519 sync_pb::PasswordSpecificsData data; | |
| 520 data.set_password_value(kPasswordValue); | |
| 521 password_node.SetPasswordSpecifics(data); | |
| 522 } | |
| 523 { | |
| 524 ReadTransaction trans(FROM_HERE, user_share()); | |
| 525 | |
| 526 ReadNode password_node(&trans); | |
| 527 EXPECT_EQ(BaseNode::INIT_OK, | |
| 528 password_node.InitByClientTagLookup(PASSWORDS, kClientTag)); | |
| 529 const sync_pb::PasswordSpecificsData& data = | |
| 530 password_node.GetPasswordSpecifics(); | |
| 531 EXPECT_EQ(kPasswordValue, data.password_value()); | |
| 532 // Check that nothing has appeared in the unencrypted field. | |
| 533 EXPECT_FALSE(password_node.GetEntitySpecifics() | |
| 534 .password() | |
| 535 .has_unencrypted_metadata()); | |
| 536 } | |
| 537 } | |
| 538 | |
| 539 TEST_F(SyncApiTest, WriteEncryptedTitle) { | |
| 540 KeyParams params = {"localhost", "username", "passphrase"}; | |
| 541 { | |
| 542 ReadTransaction trans(FROM_HERE, user_share()); | |
| 543 trans.GetCryptographer()->AddKey(params); | |
| 544 } | |
| 545 encryption_handler()->EnableEncryptEverything(); | |
| 546 int bookmark_id; | |
| 547 { | |
| 548 WriteTransaction trans(FROM_HERE, user_share()); | |
| 549 ReadNode root_node(&trans); | |
| 550 root_node.InitByRootLookup(); | |
| 551 | |
| 552 WriteNode bookmark_node(&trans); | |
| 553 ASSERT_TRUE(bookmark_node.InitBookmarkByCreation(root_node, NULL)); | |
| 554 bookmark_id = bookmark_node.GetId(); | |
| 555 bookmark_node.SetTitle("foo"); | |
| 556 | |
| 557 WriteNode pref_node(&trans); | |
| 558 WriteNode::InitUniqueByCreationResult result = | |
| 559 pref_node.InitUniqueByCreation(PREFERENCES, root_node, "bar"); | |
| 560 ASSERT_EQ(WriteNode::INIT_SUCCESS, result); | |
| 561 pref_node.SetTitle("bar"); | |
| 562 } | |
| 563 { | |
| 564 ReadTransaction trans(FROM_HERE, user_share()); | |
| 565 | |
| 566 ReadNode bookmark_node(&trans); | |
| 567 ASSERT_EQ(BaseNode::INIT_OK, bookmark_node.InitByIdLookup(bookmark_id)); | |
| 568 EXPECT_EQ("foo", bookmark_node.GetTitle()); | |
| 569 EXPECT_EQ(kEncryptedString, bookmark_node.GetEntry()->GetNonUniqueName()); | |
| 570 | |
| 571 ReadNode pref_node(&trans); | |
| 572 ASSERT_EQ(BaseNode::INIT_OK, | |
| 573 pref_node.InitByClientTagLookup(PREFERENCES, "bar")); | |
| 574 EXPECT_EQ(kEncryptedString, pref_node.GetTitle()); | |
| 575 } | |
| 576 } | |
| 577 | |
| 578 // Non-unique name should not be empty. For bookmarks non-unique name is copied | |
| 579 // from bookmark title. This test verifies that setting bookmark title to "" | |
| 580 // results in single space title and non-unique name in internal representation. | |
| 581 // GetTitle should still return empty string. | |
| 582 TEST_F(SyncApiTest, WriteEmptyBookmarkTitle) { | |
| 583 int bookmark_id; | |
| 584 { | |
| 585 WriteTransaction trans(FROM_HERE, user_share()); | |
| 586 ReadNode root_node(&trans); | |
| 587 root_node.InitByRootLookup(); | |
| 588 | |
| 589 WriteNode bookmark_node(&trans); | |
| 590 ASSERT_TRUE(bookmark_node.InitBookmarkByCreation(root_node, NULL)); | |
| 591 bookmark_id = bookmark_node.GetId(); | |
| 592 bookmark_node.SetTitle(""); | |
| 593 } | |
| 594 { | |
| 595 ReadTransaction trans(FROM_HERE, user_share()); | |
| 596 | |
| 597 ReadNode bookmark_node(&trans); | |
| 598 ASSERT_EQ(BaseNode::INIT_OK, bookmark_node.InitByIdLookup(bookmark_id)); | |
| 599 EXPECT_EQ("", bookmark_node.GetTitle()); | |
| 600 EXPECT_EQ(" ", bookmark_node.GetEntitySpecifics().bookmark().title()); | |
| 601 EXPECT_EQ(" ", bookmark_node.GetEntry()->GetNonUniqueName()); | |
| 602 } | |
| 603 } | |
| 604 | |
| 605 TEST_F(SyncApiTest, BaseNodeSetSpecifics) { | |
| 606 int64_t child_id = MakeNodeWithRoot(user_share(), BOOKMARKS, "testtag"); | |
| 607 WriteTransaction trans(FROM_HERE, user_share()); | |
| 608 WriteNode node(&trans); | |
| 609 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id)); | |
| 610 | |
| 611 sync_pb::EntitySpecifics entity_specifics; | |
| 612 entity_specifics.mutable_bookmark()->set_url("http://www.google.com"); | |
| 613 | |
| 614 EXPECT_NE(entity_specifics.SerializeAsString(), | |
| 615 node.GetEntitySpecifics().SerializeAsString()); | |
| 616 node.SetEntitySpecifics(entity_specifics); | |
| 617 EXPECT_EQ(entity_specifics.SerializeAsString(), | |
| 618 node.GetEntitySpecifics().SerializeAsString()); | |
| 619 } | |
| 620 | |
| 621 TEST_F(SyncApiTest, BaseNodeSetSpecificsPreservesUnknownFields) { | |
| 622 int64_t child_id = MakeNodeWithRoot(user_share(), BOOKMARKS, "testtag"); | |
| 623 WriteTransaction trans(FROM_HERE, user_share()); | |
| 624 WriteNode node(&trans); | |
| 625 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id)); | |
| 626 EXPECT_TRUE(node.GetEntitySpecifics().unknown_fields().empty()); | |
| 627 | |
| 628 sync_pb::EntitySpecifics entity_specifics; | |
| 629 entity_specifics.mutable_bookmark()->set_url("http://www.google.com"); | |
| 630 std::string unknown_fields; | |
| 631 { | |
| 632 ::google::protobuf::io::StringOutputStream unknown_fields_stream( | |
| 633 &unknown_fields); | |
| 634 ::google::protobuf::io::CodedOutputStream output(&unknown_fields_stream); | |
| 635 const int tag = 5; | |
| 636 const int value = 100; | |
| 637 output.WriteTag(tag); | |
| 638 output.WriteLittleEndian32(value); | |
| 639 } | |
| 640 *entity_specifics.mutable_unknown_fields() = unknown_fields; | |
| 641 node.SetEntitySpecifics(entity_specifics); | |
| 642 EXPECT_FALSE(node.GetEntitySpecifics().unknown_fields().empty()); | |
| 643 EXPECT_EQ(unknown_fields, node.GetEntitySpecifics().unknown_fields()); | |
| 644 | |
| 645 entity_specifics.mutable_unknown_fields()->clear(); | |
| 646 node.SetEntitySpecifics(entity_specifics); | |
| 647 EXPECT_FALSE(node.GetEntitySpecifics().unknown_fields().empty()); | |
| 648 EXPECT_EQ(unknown_fields, node.GetEntitySpecifics().unknown_fields()); | |
| 649 } | |
| 650 | |
| 651 TEST_F(SyncApiTest, EmptyTags) { | |
| 652 WriteTransaction trans(FROM_HERE, user_share()); | |
| 653 ReadNode root_node(&trans); | |
| 654 root_node.InitByRootLookup(); | |
| 655 WriteNode node(&trans); | |
| 656 std::string empty_tag; | |
| 657 WriteNode::InitUniqueByCreationResult result = | |
| 658 node.InitUniqueByCreation(TYPED_URLS, root_node, empty_tag); | |
| 659 EXPECT_NE(WriteNode::INIT_SUCCESS, result); | |
| 660 EXPECT_EQ(BaseNode::INIT_FAILED_PRECONDITION, | |
| 661 node.InitByClientTagLookup(TYPED_URLS, empty_tag)); | |
| 662 } | |
| 663 | |
| 664 // Test counting nodes when the type's root node has no children. | |
| 665 TEST_F(SyncApiTest, GetTotalNodeCountEmpty) { | |
| 666 int64_t type_root = MakeTypeRoot(user_share(), BOOKMARKS); | |
| 667 EXPECT_EQ(1, GetTotalNodeCount(user_share(), type_root)); | |
| 668 } | |
| 669 | |
| 670 // Test counting nodes when there is one child beneath the type's root. | |
| 671 TEST_F(SyncApiTest, GetTotalNodeCountOneChild) { | |
| 672 int64_t type_root = MakeTypeRoot(user_share(), BOOKMARKS); | |
| 673 int64_t parent = | |
| 674 MakeFolderWithParent(user_share(), BOOKMARKS, type_root, NULL); | |
| 675 EXPECT_EQ(2, GetTotalNodeCount(user_share(), type_root)); | |
| 676 EXPECT_EQ(1, GetTotalNodeCount(user_share(), parent)); | |
| 677 } | |
| 678 | |
| 679 // Test counting nodes when there are multiple children beneath the type root, | |
| 680 // and one of those children has children of its own. | |
| 681 TEST_F(SyncApiTest, GetTotalNodeCountMultipleChildren) { | |
| 682 int64_t type_root = MakeTypeRoot(user_share(), BOOKMARKS); | |
| 683 int64_t parent = | |
| 684 MakeFolderWithParent(user_share(), BOOKMARKS, type_root, NULL); | |
| 685 ignore_result(MakeFolderWithParent(user_share(), BOOKMARKS, type_root, NULL)); | |
| 686 int64_t child1 = MakeFolderWithParent(user_share(), BOOKMARKS, parent, NULL); | |
| 687 ignore_result(MakeBookmarkWithParent(user_share(), parent, NULL)); | |
| 688 ignore_result(MakeBookmarkWithParent(user_share(), child1, NULL)); | |
| 689 EXPECT_EQ(6, GetTotalNodeCount(user_share(), type_root)); | |
| 690 EXPECT_EQ(4, GetTotalNodeCount(user_share(), parent)); | |
| 691 } | |
| 692 | |
| 693 // Verify that Directory keeps track of which attachments are referenced by | |
| 694 // which entries. | |
| 695 TEST_F(SyncApiTest, AttachmentLinking) { | |
| 696 // Add an entry with an attachment. | |
| 697 std::string tag1("some tag"); | |
| 698 AttachmentId attachment_id(AttachmentId::Create(0, 0)); | |
| 699 sync_pb::AttachmentMetadata attachment_metadata; | |
| 700 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record(); | |
| 701 *record->mutable_id() = attachment_id.GetProto(); | |
| 702 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id.GetProto())); | |
| 703 CreateEntryWithAttachmentMetadata(PREFERENCES, tag1, attachment_metadata); | |
| 704 | |
| 705 // See that the directory knows it's linked. | |
| 706 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto())); | |
| 707 | |
| 708 // Add a second entry referencing the same attachment. | |
| 709 std::string tag2("some other tag"); | |
| 710 CreateEntryWithAttachmentMetadata(PREFERENCES, tag2, attachment_metadata); | |
| 711 | |
| 712 // See that the directory knows it's still linked. | |
| 713 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto())); | |
| 714 | |
| 715 // Tombstone the first entry. | |
| 716 ReplaceWithTombstone(PREFERENCES, tag1); | |
| 717 | |
| 718 // See that the attachment is still considered linked because the entry hasn't | |
| 719 // been purged from the Directory. | |
| 720 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto())); | |
| 721 | |
| 722 // Save changes and see that the entry is truly gone. | |
| 723 ASSERT_TRUE(dir()->SaveChanges()); | |
| 724 ASSERT_EQ(LookupEntryByClientTag(PREFERENCES, tag1), | |
| 725 WriteNode::INIT_FAILED_ENTRY_NOT_GOOD); | |
| 726 | |
| 727 // However, the attachment is still linked. | |
| 728 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto())); | |
| 729 | |
| 730 // Save, destroy, and recreate the directory. See that it's still linked. | |
| 731 ASSERT_TRUE(ReloadDir()); | |
| 732 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto())); | |
| 733 | |
| 734 // Tombstone the second entry, save changes, see that it's truly gone. | |
| 735 ReplaceWithTombstone(PREFERENCES, tag2); | |
| 736 ASSERT_TRUE(dir()->SaveChanges()); | |
| 737 ASSERT_EQ(LookupEntryByClientTag(PREFERENCES, tag2), | |
| 738 WriteNode::INIT_FAILED_ENTRY_NOT_GOOD); | |
| 739 | |
| 740 // Finally, the attachment is no longer linked. | |
| 741 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id.GetProto())); | |
| 742 } | |
| 743 | |
| 744 // This tests directory integrity in the case of creating a new unique node | |
| 745 // with client tag matching that of an existing unapplied node with server only | |
| 746 // data. See crbug.com/505761. | |
| 747 TEST_F(SyncApiTest, WriteNode_UniqueByCreation_UndeleteCase) { | |
| 748 int64_t preferences_root = MakeTypeRoot(user_share(), PREFERENCES); | |
| 749 | |
| 750 // Create a node with server only data. | |
| 751 int64_t item1 = 0; | |
| 752 { | |
| 753 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, | |
| 754 user_share()->directory.get()); | |
| 755 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | |
| 756 syncable::Id::CreateFromServerId("foo1")); | |
| 757 DCHECK(entry.good()); | |
| 758 entry.PutServerVersion(10); | |
| 759 entry.PutIsUnappliedUpdate(true); | |
| 760 sync_pb::EntitySpecifics specifics; | |
| 761 AddDefaultFieldValue(PREFERENCES, &specifics); | |
| 762 entry.PutServerSpecifics(specifics); | |
| 763 const std::string hash = syncable::GenerateSyncableHash(PREFERENCES, "foo"); | |
| 764 entry.PutUniqueClientTag(hash); | |
| 765 item1 = entry.GetMetahandle(); | |
| 766 } | |
| 767 | |
| 768 // Verify that the server-only item is invisible as a child of | |
| 769 // of |preferences_root| because at this point it should have the | |
| 770 // "deleted" flag set. | |
| 771 EXPECT_EQ(1, GetTotalNodeCount(user_share(), preferences_root)); | |
| 772 | |
| 773 // Create a client node with the same tag as the node above. | |
| 774 int64_t item2 = MakeNode(user_share(), PREFERENCES, "foo"); | |
| 775 // Expect this to be the same directory entry as |item1|. | |
| 776 EXPECT_EQ(item1, item2); | |
| 777 // Expect it to be visible as a child of |preferences_root|. | |
| 778 EXPECT_EQ(2, GetTotalNodeCount(user_share(), preferences_root)); | |
| 779 | |
| 780 // Tombstone the new item | |
| 781 { | |
| 782 WriteTransaction trans(FROM_HERE, user_share()); | |
| 783 WriteNode node(&trans); | |
| 784 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(item1)); | |
| 785 node.Tombstone(); | |
| 786 } | |
| 787 | |
| 788 // Verify that it is gone from the index. | |
| 789 EXPECT_EQ(1, GetTotalNodeCount(user_share(), preferences_root)); | |
| 790 } | |
| 791 | |
| 792 // Tests that InitUniqueByCreation called for existing encrypted entry properly | |
| 793 // decrypts specifics and pust them in BaseNode::unencrypted_data_. | |
| 794 TEST_F(SyncApiTest, WriteNode_UniqueByCreation_EncryptedExistingEntry) { | |
| 795 KeyParams params = {"localhost", "username", "passphrase"}; | |
| 796 { | |
| 797 ReadTransaction trans(FROM_HERE, user_share()); | |
| 798 trans.GetCryptographer()->AddKey(params); | |
| 799 } | |
| 800 encryption_handler()->EnableEncryptEverything(); | |
| 801 WriteTransaction trans(FROM_HERE, user_share()); | |
| 802 ReadNode root_node(&trans); | |
| 803 root_node.InitByRootLookup(); | |
| 804 | |
| 805 { | |
| 806 WriteNode pref_node(&trans); | |
| 807 WriteNode::InitUniqueByCreationResult result = | |
| 808 pref_node.InitUniqueByCreation(PREFERENCES, root_node, "bar"); | |
| 809 ASSERT_EQ(WriteNode::INIT_SUCCESS, result); | |
| 810 pref_node.SetTitle("bar"); | |
| 811 sync_pb::EntitySpecifics entity_specifics; | |
| 812 entity_specifics.mutable_preference(); | |
| 813 pref_node.SetEntitySpecifics(entity_specifics); | |
| 814 } | |
| 815 { | |
| 816 WriteNode pref_node(&trans); | |
| 817 WriteNode::InitUniqueByCreationResult result = | |
| 818 pref_node.InitUniqueByCreation(PREFERENCES, root_node, "bar"); | |
| 819 ASSERT_EQ(WriteNode::INIT_SUCCESS, result); | |
| 820 // Call GetEntitySpecifics, ensure it doesn't DCHECK. | |
| 821 pref_node.GetEntitySpecifics(); | |
| 822 } | |
| 823 } | |
| 824 | |
| 825 // Tests that undeleting deleted password doesn't trigger any issues. | |
| 826 // See crbug/440430. | |
| 827 TEST_F(SyncApiTest, WriteNode_PasswordUniqueByCreationAfterDelete) { | |
| 828 KeyParams params = {"localhost", "username", "passphrase"}; | |
| 829 { | |
| 830 ReadTransaction trans(FROM_HERE, user_share()); | |
| 831 trans.GetCryptographer()->AddKey(params); | |
| 832 } | |
| 833 | |
| 834 WriteTransaction trans(FROM_HERE, user_share()); | |
| 835 ReadNode root_node(&trans); | |
| 836 root_node.InitByRootLookup(); | |
| 837 // Create new password. | |
| 838 { | |
| 839 WriteNode password_node(&trans); | |
| 840 WriteNode::InitUniqueByCreationResult result = | |
| 841 password_node.InitUniqueByCreation(PASSWORDS, root_node, "foo"); | |
| 842 ASSERT_EQ(WriteNode::INIT_SUCCESS, result); | |
| 843 sync_pb::PasswordSpecificsData password_specifics; | |
| 844 password_specifics.set_password_value("secret"); | |
| 845 password_node.SetPasswordSpecifics(password_specifics); | |
| 846 } | |
| 847 // Delete password. | |
| 848 { | |
| 849 WriteNode password_node(&trans); | |
| 850 BaseNode::InitByLookupResult result = | |
| 851 password_node.InitByClientTagLookup(PASSWORDS, "foo"); | |
| 852 ASSERT_EQ(BaseNode::INIT_OK, result); | |
| 853 password_node.Tombstone(); | |
| 854 } | |
| 855 // Create password again triggering undeletion. | |
| 856 { | |
| 857 WriteNode password_node(&trans); | |
| 858 WriteNode::InitUniqueByCreationResult result = | |
| 859 password_node.InitUniqueByCreation(PASSWORDS, root_node, "foo"); | |
| 860 ASSERT_EQ(WriteNode::INIT_SUCCESS, result); | |
| 861 } | |
| 862 } | |
| 863 | |
| 864 namespace { | |
| 865 | |
| 866 class TestHttpPostProviderInterface : public HttpPostProviderInterface { | |
| 867 public: | |
| 868 ~TestHttpPostProviderInterface() override {} | |
| 869 | |
| 870 void SetExtraRequestHeaders(const char* headers) override {} | |
| 871 void SetURL(const char* url, int port) override {} | |
| 872 void SetPostPayload(const char* content_type, | |
| 873 int content_length, | |
| 874 const char* content) override {} | |
| 875 bool MakeSynchronousPost(int* error_code, int* response_code) override { | |
| 876 return false; | |
| 877 } | |
| 878 int GetResponseContentLength() const override { return 0; } | |
| 879 const char* GetResponseContent() const override { return ""; } | |
| 880 const std::string GetResponseHeaderValue( | |
| 881 const std::string& name) const override { | |
| 882 return std::string(); | |
| 883 } | |
| 884 void Abort() override {} | |
| 885 }; | |
| 886 | |
| 887 class TestHttpPostProviderFactory : public HttpPostProviderFactory { | |
| 888 public: | |
| 889 ~TestHttpPostProviderFactory() override {} | |
| 890 void Init(const std::string& user_agent, | |
| 891 const BindToTrackerCallback& bind_to_tracker_callback) override {} | |
| 892 HttpPostProviderInterface* Create() override { | |
| 893 return new TestHttpPostProviderInterface(); | |
| 894 } | |
| 895 void Destroy(HttpPostProviderInterface* http) override { | |
| 896 delete static_cast<TestHttpPostProviderInterface*>(http); | |
| 897 } | |
| 898 }; | |
| 899 | |
| 900 class SyncManagerObserverMock : public SyncManager::Observer { | |
| 901 public: | |
| 902 MOCK_METHOD1(OnSyncCycleCompleted, void(const SyncCycleSnapshot&)); // NOLINT | |
| 903 MOCK_METHOD4(OnInitializationComplete, | |
| 904 void(const WeakHandle<JsBackend>&, | |
| 905 const WeakHandle<DataTypeDebugInfoListener>&, | |
| 906 bool, | |
| 907 ModelTypeSet)); // NOLINT | |
| 908 MOCK_METHOD1(OnConnectionStatusChange, void(ConnectionStatus)); // NOLINT | |
| 909 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT | |
| 910 MOCK_METHOD1(OnActionableError, void(const SyncProtocolError&)); // NOLINT | |
| 911 MOCK_METHOD1(OnMigrationRequested, void(ModelTypeSet)); // NOLINT | |
| 912 MOCK_METHOD1(OnProtocolEvent, void(const ProtocolEvent&)); // NOLINT | |
| 913 }; | |
| 914 | |
| 915 class SyncEncryptionHandlerObserverMock | |
| 916 : public SyncEncryptionHandler::Observer { | |
| 917 public: | |
| 918 MOCK_METHOD2(OnPassphraseRequired, | |
| 919 void(PassphraseRequiredReason, | |
| 920 const sync_pb::EncryptedData&)); // NOLINT | |
| 921 MOCK_METHOD0(OnPassphraseAccepted, void()); // NOLINT | |
| 922 MOCK_METHOD2(OnBootstrapTokenUpdated, | |
| 923 void(const std::string&, BootstrapTokenType type)); // NOLINT | |
| 924 MOCK_METHOD2(OnEncryptedTypesChanged, void(ModelTypeSet, bool)); // NOLINT | |
| 925 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT | |
| 926 MOCK_METHOD1(OnCryptographerStateChanged, void(Cryptographer*)); // NOLINT | |
| 927 MOCK_METHOD2(OnPassphraseTypeChanged, | |
| 928 void(PassphraseType, | |
| 929 base::Time)); // NOLINT | |
| 930 MOCK_METHOD1(OnLocalSetPassphraseEncryption, | |
| 931 void(const SyncEncryptionHandler::NigoriState&)); // NOLINT | |
| 932 }; | |
| 933 | |
| 934 } // namespace | |
| 935 | |
| 936 class SyncManagerTest : public testing::Test, | |
| 937 public SyncManager::ChangeDelegate { | |
| 938 protected: | |
| 939 enum NigoriStatus { DONT_WRITE_NIGORI, WRITE_TO_NIGORI }; | |
| 940 | |
| 941 enum EncryptionStatus { UNINITIALIZED, DEFAULT_ENCRYPTION, FULL_ENCRYPTION }; | |
| 942 | |
| 943 SyncManagerTest() : sync_manager_("Test sync manager") { | |
| 944 switches_.encryption_method = | |
| 945 InternalComponentsFactory::ENCRYPTION_KEYSTORE; | |
| 946 } | |
| 947 | |
| 948 virtual ~SyncManagerTest() {} | |
| 949 | |
| 950 // Test implementation. | |
| 951 void SetUp() { | |
| 952 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 953 | |
| 954 extensions_activity_ = new ExtensionsActivity(); | |
| 955 | |
| 956 SyncCredentials credentials; | |
| 957 credentials.account_id = "foo@bar.com"; | |
| 958 credentials.email = "foo@bar.com"; | |
| 959 credentials.sync_token = "sometoken"; | |
| 960 OAuth2TokenService::ScopeSet scope_set; | |
| 961 scope_set.insert(GaiaConstants::kChromeSyncOAuth2Scope); | |
| 962 credentials.scope_set = scope_set; | |
| 963 | |
| 964 sync_manager_.AddObserver(&manager_observer_); | |
| 965 EXPECT_CALL(manager_observer_, OnInitializationComplete(_, _, _, _)) | |
| 966 .WillOnce(DoAll(SaveArg<0>(&js_backend_), | |
| 967 SaveArg<2>(&initialization_succeeded_))); | |
| 968 | |
| 969 EXPECT_FALSE(js_backend_.IsInitialized()); | |
| 970 | |
| 971 std::vector<scoped_refptr<ModelSafeWorker>> workers; | |
| 972 ModelSafeRoutingInfo routing_info; | |
| 973 GetModelSafeRoutingInfo(&routing_info); | |
| 974 | |
| 975 // This works only because all routing info types are GROUP_PASSIVE. | |
| 976 // If we had types in other groups, we would need additional workers | |
| 977 // to support them. | |
| 978 scoped_refptr<ModelSafeWorker> worker = new FakeModelWorker(GROUP_PASSIVE); | |
| 979 workers.push_back(worker); | |
| 980 | |
| 981 SyncManager::InitArgs args; | |
| 982 args.database_location = temp_dir_.GetPath(); | |
| 983 args.service_url = GURL("https://example.com/"); | |
| 984 args.post_factory = std::unique_ptr<HttpPostProviderFactory>( | |
| 985 new TestHttpPostProviderFactory()); | |
| 986 args.workers = workers; | |
| 987 args.extensions_activity = extensions_activity_.get(), | |
| 988 args.change_delegate = this; | |
| 989 args.credentials = credentials; | |
| 990 args.invalidator_client_id = "fake_invalidator_client_id"; | |
| 991 args.internal_components_factory.reset(GetFactory()); | |
| 992 args.encryptor = &encryptor_; | |
| 993 args.unrecoverable_error_handler = | |
| 994 MakeWeakHandle(mock_unrecoverable_error_handler_.GetWeakPtr()); | |
| 995 args.cancelation_signal = &cancelation_signal_; | |
| 996 sync_manager_.Init(&args); | |
| 997 | |
| 998 sync_manager_.GetEncryptionHandler()->AddObserver(&encryption_observer_); | |
| 999 | |
| 1000 EXPECT_TRUE(js_backend_.IsInitialized()); | |
| 1001 EXPECT_EQ(InternalComponentsFactory::STORAGE_ON_DISK, storage_used_); | |
| 1002 | |
| 1003 if (initialization_succeeded_) { | |
| 1004 for (ModelSafeRoutingInfo::iterator i = routing_info.begin(); | |
| 1005 i != routing_info.end(); ++i) { | |
| 1006 type_roots_[i->first] = | |
| 1007 MakeTypeRoot(sync_manager_.GetUserShare(), i->first); | |
| 1008 } | |
| 1009 } | |
| 1010 | |
| 1011 PumpLoop(); | |
| 1012 } | |
| 1013 | |
| 1014 void TearDown() { | |
| 1015 sync_manager_.RemoveObserver(&manager_observer_); | |
| 1016 sync_manager_.ShutdownOnSyncThread(STOP_SYNC); | |
| 1017 PumpLoop(); | |
| 1018 } | |
| 1019 | |
| 1020 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { | |
| 1021 (*out)[NIGORI] = GROUP_PASSIVE; | |
| 1022 (*out)[DEVICE_INFO] = GROUP_PASSIVE; | |
| 1023 (*out)[EXPERIMENTS] = GROUP_PASSIVE; | |
| 1024 (*out)[BOOKMARKS] = GROUP_PASSIVE; | |
| 1025 (*out)[THEMES] = GROUP_PASSIVE; | |
| 1026 (*out)[SESSIONS] = GROUP_PASSIVE; | |
| 1027 (*out)[PASSWORDS] = GROUP_PASSIVE; | |
| 1028 (*out)[PREFERENCES] = GROUP_PASSIVE; | |
| 1029 (*out)[PRIORITY_PREFERENCES] = GROUP_PASSIVE; | |
| 1030 (*out)[ARTICLES] = GROUP_PASSIVE; | |
| 1031 } | |
| 1032 | |
| 1033 ModelTypeSet GetEnabledTypes() { | |
| 1034 ModelSafeRoutingInfo routing_info; | |
| 1035 GetModelSafeRoutingInfo(&routing_info); | |
| 1036 return GetRoutingInfoTypes(routing_info); | |
| 1037 } | |
| 1038 | |
| 1039 void OnChangesApplied(ModelType model_type, | |
| 1040 int64_t model_version, | |
| 1041 const BaseTransaction* trans, | |
| 1042 const ImmutableChangeRecordList& changes) override {} | |
| 1043 | |
| 1044 void OnChangesComplete(ModelType model_type) override {} | |
| 1045 | |
| 1046 // Helper methods. | |
| 1047 bool SetUpEncryption(NigoriStatus nigori_status, | |
| 1048 EncryptionStatus encryption_status) { | |
| 1049 UserShare* share = sync_manager_.GetUserShare(); | |
| 1050 | |
| 1051 // We need to create the nigori node as if it were an applied server update. | |
| 1052 int64_t nigori_id = GetIdForDataType(NIGORI); | |
| 1053 if (nigori_id == kInvalidId) | |
| 1054 return false; | |
| 1055 | |
| 1056 // Set the nigori cryptographer information. | |
| 1057 if (encryption_status == FULL_ENCRYPTION) | |
| 1058 sync_manager_.GetEncryptionHandler()->EnableEncryptEverything(); | |
| 1059 | |
| 1060 WriteTransaction trans(FROM_HERE, share); | |
| 1061 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1062 if (!cryptographer) | |
| 1063 return false; | |
| 1064 if (encryption_status != UNINITIALIZED) { | |
| 1065 KeyParams params = {"localhost", "dummy", "foobar"}; | |
| 1066 cryptographer->AddKey(params); | |
| 1067 } else { | |
| 1068 DCHECK_NE(nigori_status, WRITE_TO_NIGORI); | |
| 1069 } | |
| 1070 if (nigori_status == WRITE_TO_NIGORI) { | |
| 1071 sync_pb::NigoriSpecifics nigori; | |
| 1072 cryptographer->GetKeys(nigori.mutable_encryption_keybag()); | |
| 1073 share->directory->GetNigoriHandler()->UpdateNigoriFromEncryptedTypes( | |
| 1074 &nigori, trans.GetWrappedTrans()); | |
| 1075 WriteNode node(&trans); | |
| 1076 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(nigori_id)); | |
| 1077 node.SetNigoriSpecifics(nigori); | |
| 1078 } | |
| 1079 return cryptographer->is_ready(); | |
| 1080 } | |
| 1081 | |
| 1082 int64_t GetIdForDataType(ModelType type) { | |
| 1083 if (type_roots_.count(type) == 0) | |
| 1084 return 0; | |
| 1085 return type_roots_[type]; | |
| 1086 } | |
| 1087 | |
| 1088 void PumpLoop() { base::RunLoop().RunUntilIdle(); } | |
| 1089 | |
| 1090 void SetJsEventHandler(const WeakHandle<JsEventHandler>& event_handler) { | |
| 1091 js_backend_.Call(FROM_HERE, &JsBackend::SetJsEventHandler, event_handler); | |
| 1092 PumpLoop(); | |
| 1093 } | |
| 1094 | |
| 1095 // Looks up an entry by client tag and resets IS_UNSYNCED value to false. | |
| 1096 // Returns true if entry was previously unsynced, false if IS_UNSYNCED was | |
| 1097 // already false. | |
| 1098 bool ResetUnsyncedEntry(ModelType type, const std::string& client_tag) { | |
| 1099 UserShare* share = sync_manager_.GetUserShare(); | |
| 1100 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, | |
| 1101 share->directory.get()); | |
| 1102 const std::string hash = syncable::GenerateSyncableHash(type, client_tag); | |
| 1103 syncable::MutableEntry entry(&trans, syncable::GET_BY_CLIENT_TAG, hash); | |
| 1104 EXPECT_TRUE(entry.good()); | |
| 1105 if (!entry.GetIsUnsynced()) | |
| 1106 return false; | |
| 1107 entry.PutIsUnsynced(false); | |
| 1108 return true; | |
| 1109 } | |
| 1110 | |
| 1111 virtual InternalComponentsFactory* GetFactory() { | |
| 1112 return new TestInternalComponentsFactory( | |
| 1113 GetSwitches(), InternalComponentsFactory::STORAGE_IN_MEMORY, | |
| 1114 &storage_used_); | |
| 1115 } | |
| 1116 | |
| 1117 // Returns true if we are currently encrypting all sync data. May | |
| 1118 // be called on any thread. | |
| 1119 bool IsEncryptEverythingEnabledForTest() { | |
| 1120 return sync_manager_.GetEncryptionHandler()->IsEncryptEverythingEnabled(); | |
| 1121 } | |
| 1122 | |
| 1123 // Gets the set of encrypted types from the cryptographer | |
| 1124 // Note: opens a transaction. May be called from any thread. | |
| 1125 ModelTypeSet GetEncryptedTypes() { | |
| 1126 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1127 return GetEncryptedTypesWithTrans(&trans); | |
| 1128 } | |
| 1129 | |
| 1130 ModelTypeSet GetEncryptedTypesWithTrans(BaseTransaction* trans) { | |
| 1131 return trans->GetDirectory()->GetNigoriHandler()->GetEncryptedTypes( | |
| 1132 trans->GetWrappedTrans()); | |
| 1133 } | |
| 1134 | |
| 1135 PassphraseType GetPassphraseType() { | |
| 1136 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1137 return GetPassphraseTypeWithTrans(&trans); | |
| 1138 } | |
| 1139 | |
| 1140 PassphraseType GetPassphraseTypeWithTrans(BaseTransaction* trans) { | |
| 1141 return trans->GetDirectory()->GetNigoriHandler()->GetPassphraseType( | |
| 1142 trans->GetWrappedTrans()); | |
| 1143 } | |
| 1144 | |
| 1145 void SimulateInvalidatorEnabledForTest(bool is_enabled) { | |
| 1146 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); | |
| 1147 sync_manager_.SetInvalidatorEnabled(is_enabled); | |
| 1148 } | |
| 1149 | |
| 1150 void SetProgressMarkerForType(ModelType type, bool set) { | |
| 1151 if (set) { | |
| 1152 sync_pb::DataTypeProgressMarker marker; | |
| 1153 marker.set_token("token"); | |
| 1154 marker.set_data_type_id(GetSpecificsFieldNumberFromModelType(type)); | |
| 1155 sync_manager_.directory()->SetDownloadProgress(type, marker); | |
| 1156 } else { | |
| 1157 sync_pb::DataTypeProgressMarker marker; | |
| 1158 sync_manager_.directory()->SetDownloadProgress(type, marker); | |
| 1159 } | |
| 1160 } | |
| 1161 | |
| 1162 InternalComponentsFactory::Switches GetSwitches() const { return switches_; } | |
| 1163 | |
| 1164 void ExpectPassphraseAcceptance() { | |
| 1165 EXPECT_CALL(encryption_observer_, OnPassphraseAccepted()); | |
| 1166 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 1167 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 1168 } | |
| 1169 | |
| 1170 void SetImplicitPassphraseAndCheck(const std::string& passphrase) { | |
| 1171 sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase(passphrase, | |
| 1172 false); | |
| 1173 EXPECT_EQ(PassphraseType::IMPLICIT_PASSPHRASE, GetPassphraseType()); | |
| 1174 } | |
| 1175 | |
| 1176 void SetCustomPassphraseAndCheck(const std::string& passphrase) { | |
| 1177 EXPECT_CALL(encryption_observer_, | |
| 1178 OnPassphraseTypeChanged(PassphraseType::CUSTOM_PASSPHRASE, _)); | |
| 1179 sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase(passphrase, | |
| 1180 true); | |
| 1181 EXPECT_EQ(PassphraseType::CUSTOM_PASSPHRASE, GetPassphraseType()); | |
| 1182 } | |
| 1183 | |
| 1184 bool HasUnrecoverableError() { | |
| 1185 return mock_unrecoverable_error_handler_.invocation_count() > 0; | |
| 1186 } | |
| 1187 | |
| 1188 private: | |
| 1189 // Needed by |sync_manager_|. | |
| 1190 base::MessageLoop message_loop_; | |
| 1191 // Needed by |sync_manager_|. | |
| 1192 base::ScopedTempDir temp_dir_; | |
| 1193 // Sync Id's for the roots of the enabled datatypes. | |
| 1194 std::map<ModelType, int64_t> type_roots_; | |
| 1195 scoped_refptr<ExtensionsActivity> extensions_activity_; | |
| 1196 | |
| 1197 protected: | |
| 1198 FakeEncryptor encryptor_; | |
| 1199 SyncManagerImpl sync_manager_; | |
| 1200 CancelationSignal cancelation_signal_; | |
| 1201 WeakHandle<JsBackend> js_backend_; | |
| 1202 bool initialization_succeeded_; | |
| 1203 StrictMock<SyncManagerObserverMock> manager_observer_; | |
| 1204 StrictMock<SyncEncryptionHandlerObserverMock> encryption_observer_; | |
| 1205 InternalComponentsFactory::Switches switches_; | |
| 1206 InternalComponentsFactory::StorageOption storage_used_; | |
| 1207 MockUnrecoverableErrorHandler mock_unrecoverable_error_handler_; | |
| 1208 }; | |
| 1209 | |
| 1210 TEST_F(SyncManagerTest, RefreshEncryptionReady) { | |
| 1211 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 1212 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 1213 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 1214 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false)); | |
| 1215 | |
| 1216 sync_manager_.GetEncryptionHandler()->Init(); | |
| 1217 PumpLoop(); | |
| 1218 | |
| 1219 const ModelTypeSet encrypted_types = GetEncryptedTypes(); | |
| 1220 EXPECT_TRUE(encrypted_types.Has(PASSWORDS)); | |
| 1221 EXPECT_FALSE(IsEncryptEverythingEnabledForTest()); | |
| 1222 | |
| 1223 { | |
| 1224 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1225 ReadNode node(&trans); | |
| 1226 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(GetIdForDataType(NIGORI))); | |
| 1227 sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics(); | |
| 1228 EXPECT_TRUE(nigori.has_encryption_keybag()); | |
| 1229 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1230 EXPECT_TRUE(cryptographer->is_ready()); | |
| 1231 EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encryption_keybag())); | |
| 1232 } | |
| 1233 } | |
| 1234 | |
| 1235 // Attempt to refresh encryption when nigori not downloaded. | |
| 1236 TEST_F(SyncManagerTest, RefreshEncryptionNotReady) { | |
| 1237 // Don't set up encryption (no nigori node created). | |
| 1238 | |
| 1239 // Should fail. Triggers an OnPassphraseRequired because the cryptographer | |
| 1240 // is not ready. | |
| 1241 EXPECT_CALL(encryption_observer_, OnPassphraseRequired(_, _)).Times(1); | |
| 1242 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 1243 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false)); | |
| 1244 sync_manager_.GetEncryptionHandler()->Init(); | |
| 1245 PumpLoop(); | |
| 1246 | |
| 1247 const ModelTypeSet encrypted_types = GetEncryptedTypes(); | |
| 1248 EXPECT_TRUE(encrypted_types.Has(PASSWORDS)); // Hardcoded. | |
| 1249 EXPECT_FALSE(IsEncryptEverythingEnabledForTest()); | |
| 1250 } | |
| 1251 | |
| 1252 // Attempt to refresh encryption when nigori is empty. | |
| 1253 TEST_F(SyncManagerTest, RefreshEncryptionEmptyNigori) { | |
| 1254 EXPECT_TRUE(SetUpEncryption(DONT_WRITE_NIGORI, DEFAULT_ENCRYPTION)); | |
| 1255 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()).Times(1); | |
| 1256 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 1257 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false)); | |
| 1258 | |
| 1259 // Should write to nigori. | |
| 1260 sync_manager_.GetEncryptionHandler()->Init(); | |
| 1261 PumpLoop(); | |
| 1262 | |
| 1263 const ModelTypeSet encrypted_types = GetEncryptedTypes(); | |
| 1264 EXPECT_TRUE(encrypted_types.Has(PASSWORDS)); // Hardcoded. | |
| 1265 EXPECT_FALSE(IsEncryptEverythingEnabledForTest()); | |
| 1266 | |
| 1267 { | |
| 1268 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1269 ReadNode node(&trans); | |
| 1270 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(GetIdForDataType(NIGORI))); | |
| 1271 sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics(); | |
| 1272 EXPECT_TRUE(nigori.has_encryption_keybag()); | |
| 1273 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1274 EXPECT_TRUE(cryptographer->is_ready()); | |
| 1275 EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encryption_keybag())); | |
| 1276 } | |
| 1277 } | |
| 1278 | |
| 1279 TEST_F(SyncManagerTest, EncryptDataTypesWithNoData) { | |
| 1280 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 1281 EXPECT_CALL( | |
| 1282 encryption_observer_, | |
| 1283 OnEncryptedTypesChanged(HasModelTypes(EncryptableUserTypes()), true)); | |
| 1284 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 1285 sync_manager_.GetEncryptionHandler()->EnableEncryptEverything(); | |
| 1286 EXPECT_TRUE(IsEncryptEverythingEnabledForTest()); | |
| 1287 } | |
| 1288 | |
| 1289 TEST_F(SyncManagerTest, EncryptDataTypesWithData) { | |
| 1290 size_t batch_size = 5; | |
| 1291 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 1292 | |
| 1293 // Create some unencrypted unsynced data. | |
| 1294 int64_t folder = MakeFolderWithParent(sync_manager_.GetUserShare(), BOOKMARKS, | |
| 1295 GetIdForDataType(BOOKMARKS), NULL); | |
| 1296 // First batch_size nodes are children of folder. | |
| 1297 size_t i; | |
| 1298 for (i = 0; i < batch_size; ++i) { | |
| 1299 MakeBookmarkWithParent(sync_manager_.GetUserShare(), folder, NULL); | |
| 1300 } | |
| 1301 // Next batch_size nodes are a different type and on their own. | |
| 1302 for (; i < 2 * batch_size; ++i) { | |
| 1303 MakeNodeWithRoot(sync_manager_.GetUserShare(), SESSIONS, | |
| 1304 base::StringPrintf("%" PRIuS "", i)); | |
| 1305 } | |
| 1306 // Last batch_size nodes are a third type that will not need encryption. | |
| 1307 for (; i < 3 * batch_size; ++i) { | |
| 1308 MakeNodeWithRoot(sync_manager_.GetUserShare(), THEMES, | |
| 1309 base::StringPrintf("%" PRIuS "", i)); | |
| 1310 } | |
| 1311 | |
| 1312 { | |
| 1313 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1314 EXPECT_EQ(SyncEncryptionHandler::SensitiveTypes(), | |
| 1315 GetEncryptedTypesWithTrans(&trans)); | |
| 1316 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
| 1317 trans.GetWrappedTrans(), BOOKMARKS, false /* not encrypted */)); | |
| 1318 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
| 1319 trans.GetWrappedTrans(), SESSIONS, false /* not encrypted */)); | |
| 1320 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
| 1321 trans.GetWrappedTrans(), THEMES, false /* not encrypted */)); | |
| 1322 } | |
| 1323 | |
| 1324 EXPECT_CALL( | |
| 1325 encryption_observer_, | |
| 1326 OnEncryptedTypesChanged(HasModelTypes(EncryptableUserTypes()), true)); | |
| 1327 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 1328 sync_manager_.GetEncryptionHandler()->EnableEncryptEverything(); | |
| 1329 EXPECT_TRUE(IsEncryptEverythingEnabledForTest()); | |
| 1330 { | |
| 1331 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1332 EXPECT_EQ(EncryptableUserTypes(), GetEncryptedTypesWithTrans(&trans)); | |
| 1333 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
| 1334 trans.GetWrappedTrans(), BOOKMARKS, true /* is encrypted */)); | |
| 1335 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
| 1336 trans.GetWrappedTrans(), SESSIONS, true /* is encrypted */)); | |
| 1337 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
| 1338 trans.GetWrappedTrans(), THEMES, true /* is encrypted */)); | |
| 1339 } | |
| 1340 | |
| 1341 // Trigger's a ReEncryptEverything with new passphrase. | |
| 1342 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | |
| 1343 EXPECT_CALL(encryption_observer_, | |
| 1344 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); | |
| 1345 ExpectPassphraseAcceptance(); | |
| 1346 SetCustomPassphraseAndCheck("new_passphrase"); | |
| 1347 EXPECT_TRUE(IsEncryptEverythingEnabledForTest()); | |
| 1348 { | |
| 1349 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1350 EXPECT_EQ(EncryptableUserTypes(), GetEncryptedTypesWithTrans(&trans)); | |
| 1351 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
| 1352 trans.GetWrappedTrans(), BOOKMARKS, true /* is encrypted */)); | |
| 1353 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
| 1354 trans.GetWrappedTrans(), SESSIONS, true /* is encrypted */)); | |
| 1355 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
| 1356 trans.GetWrappedTrans(), THEMES, true /* is encrypted */)); | |
| 1357 } | |
| 1358 // Calling EncryptDataTypes with an empty encrypted types should not trigger | |
| 1359 // a reencryption and should just notify immediately. | |
| 1360 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | |
| 1361 EXPECT_CALL(encryption_observer_, | |
| 1362 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)) | |
| 1363 .Times(0); | |
| 1364 EXPECT_CALL(encryption_observer_, OnPassphraseAccepted()).Times(0); | |
| 1365 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()).Times(0); | |
| 1366 sync_manager_.GetEncryptionHandler()->EnableEncryptEverything(); | |
| 1367 } | |
| 1368 | |
| 1369 // Test that when there are no pending keys and the cryptographer is not | |
| 1370 // initialized, we add a key based on the current GAIA password. | |
| 1371 // (case 1 in SyncManager::SyncInternal::SetEncryptionPassphrase) | |
| 1372 TEST_F(SyncManagerTest, SetInitialGaiaPass) { | |
| 1373 EXPECT_FALSE(SetUpEncryption(DONT_WRITE_NIGORI, UNINITIALIZED)); | |
| 1374 EXPECT_CALL(encryption_observer_, | |
| 1375 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); | |
| 1376 ExpectPassphraseAcceptance(); | |
| 1377 SetImplicitPassphraseAndCheck("new_passphrase"); | |
| 1378 EXPECT_FALSE(IsEncryptEverythingEnabledForTest()); | |
| 1379 { | |
| 1380 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1381 ReadNode node(&trans); | |
| 1382 EXPECT_EQ(BaseNode::INIT_OK, node.InitTypeRoot(NIGORI)); | |
| 1383 sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics(); | |
| 1384 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1385 EXPECT_TRUE(cryptographer->is_ready()); | |
| 1386 EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encryption_keybag())); | |
| 1387 } | |
| 1388 } | |
| 1389 | |
| 1390 // Test that when there are no pending keys and we have on the old GAIA | |
| 1391 // password, we update and re-encrypt everything with the new GAIA password. | |
| 1392 // (case 1 in SyncManager::SyncInternal::SetEncryptionPassphrase) | |
| 1393 TEST_F(SyncManagerTest, UpdateGaiaPass) { | |
| 1394 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 1395 Cryptographer verifier(&encryptor_); | |
| 1396 { | |
| 1397 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1398 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1399 std::string bootstrap_token; | |
| 1400 cryptographer->GetBootstrapToken(&bootstrap_token); | |
| 1401 verifier.Bootstrap(bootstrap_token); | |
| 1402 } | |
| 1403 EXPECT_CALL(encryption_observer_, | |
| 1404 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); | |
| 1405 ExpectPassphraseAcceptance(); | |
| 1406 SetImplicitPassphraseAndCheck("new_passphrase"); | |
| 1407 EXPECT_FALSE(IsEncryptEverythingEnabledForTest()); | |
| 1408 { | |
| 1409 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1410 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1411 EXPECT_TRUE(cryptographer->is_ready()); | |
| 1412 // Verify the default key has changed. | |
| 1413 sync_pb::EncryptedData encrypted; | |
| 1414 cryptographer->GetKeys(&encrypted); | |
| 1415 EXPECT_FALSE(verifier.CanDecrypt(encrypted)); | |
| 1416 } | |
| 1417 } | |
| 1418 | |
| 1419 // Sets a new explicit passphrase. This should update the bootstrap token | |
| 1420 // and re-encrypt everything. | |
| 1421 // (case 2 in SyncManager::SyncInternal::SetEncryptionPassphrase) | |
| 1422 TEST_F(SyncManagerTest, SetPassphraseWithPassword) { | |
| 1423 Cryptographer verifier(&encryptor_); | |
| 1424 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 1425 { | |
| 1426 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1427 // Store the default (soon to be old) key. | |
| 1428 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1429 std::string bootstrap_token; | |
| 1430 cryptographer->GetBootstrapToken(&bootstrap_token); | |
| 1431 verifier.Bootstrap(bootstrap_token); | |
| 1432 | |
| 1433 ReadNode root_node(&trans); | |
| 1434 root_node.InitByRootLookup(); | |
| 1435 | |
| 1436 WriteNode password_node(&trans); | |
| 1437 WriteNode::InitUniqueByCreationResult result = | |
| 1438 password_node.InitUniqueByCreation(PASSWORDS, root_node, "foo"); | |
| 1439 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); | |
| 1440 sync_pb::PasswordSpecificsData data; | |
| 1441 data.set_password_value("secret"); | |
| 1442 password_node.SetPasswordSpecifics(data); | |
| 1443 } | |
| 1444 EXPECT_CALL(encryption_observer_, | |
| 1445 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); | |
| 1446 ExpectPassphraseAcceptance(); | |
| 1447 SetCustomPassphraseAndCheck("new_passphrase"); | |
| 1448 EXPECT_FALSE(IsEncryptEverythingEnabledForTest()); | |
| 1449 { | |
| 1450 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1451 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1452 EXPECT_TRUE(cryptographer->is_ready()); | |
| 1453 // Verify the default key has changed. | |
| 1454 sync_pb::EncryptedData encrypted; | |
| 1455 cryptographer->GetKeys(&encrypted); | |
| 1456 EXPECT_FALSE(verifier.CanDecrypt(encrypted)); | |
| 1457 | |
| 1458 ReadNode password_node(&trans); | |
| 1459 EXPECT_EQ(BaseNode::INIT_OK, | |
| 1460 password_node.InitByClientTagLookup(PASSWORDS, "foo")); | |
| 1461 const sync_pb::PasswordSpecificsData& data = | |
| 1462 password_node.GetPasswordSpecifics(); | |
| 1463 EXPECT_EQ("secret", data.password_value()); | |
| 1464 } | |
| 1465 } | |
| 1466 | |
| 1467 // Manually set the pending keys in the cryptographer/nigori to reflect the data | |
| 1468 // being encrypted with a new (unprovided) GAIA password, then supply the | |
| 1469 // password. | |
| 1470 // (case 7 in SyncManager::SyncInternal::SetDecryptionPassphrase) | |
| 1471 TEST_F(SyncManagerTest, SupplyPendingGAIAPass) { | |
| 1472 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 1473 Cryptographer other_cryptographer(&encryptor_); | |
| 1474 { | |
| 1475 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1476 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1477 std::string bootstrap_token; | |
| 1478 cryptographer->GetBootstrapToken(&bootstrap_token); | |
| 1479 other_cryptographer.Bootstrap(bootstrap_token); | |
| 1480 | |
| 1481 // Now update the nigori to reflect the new keys, and update the | |
| 1482 // cryptographer to have pending keys. | |
| 1483 KeyParams params = {"localhost", "dummy", "passphrase2"}; | |
| 1484 other_cryptographer.AddKey(params); | |
| 1485 WriteNode node(&trans); | |
| 1486 EXPECT_EQ(BaseNode::INIT_OK, node.InitTypeRoot(NIGORI)); | |
| 1487 sync_pb::NigoriSpecifics nigori; | |
| 1488 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag()); | |
| 1489 cryptographer->SetPendingKeys(nigori.encryption_keybag()); | |
| 1490 EXPECT_TRUE(cryptographer->has_pending_keys()); | |
| 1491 node.SetNigoriSpecifics(nigori); | |
| 1492 } | |
| 1493 EXPECT_CALL(encryption_observer_, | |
| 1494 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); | |
| 1495 ExpectPassphraseAcceptance(); | |
| 1496 sync_manager_.GetEncryptionHandler()->SetDecryptionPassphrase("passphrase2"); | |
| 1497 EXPECT_EQ(PassphraseType::IMPLICIT_PASSPHRASE, GetPassphraseType()); | |
| 1498 EXPECT_FALSE(IsEncryptEverythingEnabledForTest()); | |
| 1499 { | |
| 1500 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1501 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1502 EXPECT_TRUE(cryptographer->is_ready()); | |
| 1503 // Verify we're encrypting with the new key. | |
| 1504 sync_pb::EncryptedData encrypted; | |
| 1505 cryptographer->GetKeys(&encrypted); | |
| 1506 EXPECT_TRUE(other_cryptographer.CanDecrypt(encrypted)); | |
| 1507 } | |
| 1508 } | |
| 1509 | |
| 1510 // Manually set the pending keys in the cryptographer/nigori to reflect the data | |
| 1511 // being encrypted with an old (unprovided) GAIA password. Attempt to supply | |
| 1512 // the current GAIA password and verify the bootstrap token is updated. Then | |
| 1513 // supply the old GAIA password, and verify we re-encrypt all data with the | |
| 1514 // new GAIA password. | |
| 1515 // (cases 4 and 5 in SyncManager::SyncInternal::SetEncryptionPassphrase) | |
| 1516 TEST_F(SyncManagerTest, SupplyPendingOldGAIAPass) { | |
| 1517 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 1518 Cryptographer other_cryptographer(&encryptor_); | |
| 1519 { | |
| 1520 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1521 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1522 std::string bootstrap_token; | |
| 1523 cryptographer->GetBootstrapToken(&bootstrap_token); | |
| 1524 other_cryptographer.Bootstrap(bootstrap_token); | |
| 1525 | |
| 1526 // Now update the nigori to reflect the new keys, and update the | |
| 1527 // cryptographer to have pending keys. | |
| 1528 KeyParams params = {"localhost", "dummy", "old_gaia"}; | |
| 1529 other_cryptographer.AddKey(params); | |
| 1530 WriteNode node(&trans); | |
| 1531 EXPECT_EQ(BaseNode::INIT_OK, node.InitTypeRoot(NIGORI)); | |
| 1532 sync_pb::NigoriSpecifics nigori; | |
| 1533 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag()); | |
| 1534 node.SetNigoriSpecifics(nigori); | |
| 1535 cryptographer->SetPendingKeys(nigori.encryption_keybag()); | |
| 1536 | |
| 1537 // other_cryptographer now contains all encryption keys, and is encrypting | |
| 1538 // with the newest gaia. | |
| 1539 KeyParams new_params = {"localhost", "dummy", "new_gaia"}; | |
| 1540 other_cryptographer.AddKey(new_params); | |
| 1541 } | |
| 1542 // The bootstrap token should have been updated. Save it to ensure it's based | |
| 1543 // on the new GAIA password. | |
| 1544 std::string bootstrap_token; | |
| 1545 EXPECT_CALL(encryption_observer_, | |
| 1546 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)) | |
| 1547 .WillOnce(SaveArg<0>(&bootstrap_token)); | |
| 1548 EXPECT_CALL(encryption_observer_, OnPassphraseRequired(_, _)); | |
| 1549 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 1550 SetImplicitPassphraseAndCheck("new_gaia"); | |
| 1551 EXPECT_FALSE(IsEncryptEverythingEnabledForTest()); | |
| 1552 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | |
| 1553 { | |
| 1554 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1555 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1556 EXPECT_TRUE(cryptographer->is_initialized()); | |
| 1557 EXPECT_FALSE(cryptographer->is_ready()); | |
| 1558 // Verify we're encrypting with the new key, even though we have pending | |
| 1559 // keys. | |
| 1560 sync_pb::EncryptedData encrypted; | |
| 1561 other_cryptographer.GetKeys(&encrypted); | |
| 1562 EXPECT_TRUE(cryptographer->CanDecrypt(encrypted)); | |
| 1563 } | |
| 1564 EXPECT_CALL(encryption_observer_, | |
| 1565 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); | |
| 1566 ExpectPassphraseAcceptance(); | |
| 1567 SetImplicitPassphraseAndCheck("old_gaia"); | |
| 1568 { | |
| 1569 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1570 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1571 EXPECT_TRUE(cryptographer->is_ready()); | |
| 1572 | |
| 1573 // Verify we're encrypting with the new key. | |
| 1574 sync_pb::EncryptedData encrypted; | |
| 1575 other_cryptographer.GetKeys(&encrypted); | |
| 1576 EXPECT_TRUE(cryptographer->CanDecrypt(encrypted)); | |
| 1577 | |
| 1578 // Verify the saved bootstrap token is based on the new gaia password. | |
| 1579 Cryptographer temp_cryptographer(&encryptor_); | |
| 1580 temp_cryptographer.Bootstrap(bootstrap_token); | |
| 1581 EXPECT_TRUE(temp_cryptographer.CanDecrypt(encrypted)); | |
| 1582 } | |
| 1583 } | |
| 1584 | |
| 1585 // Manually set the pending keys in the cryptographer/nigori to reflect the data | |
| 1586 // being encrypted with an explicit (unprovided) passphrase, then supply the | |
| 1587 // passphrase. | |
| 1588 // (case 9 in SyncManager::SyncInternal::SetDecryptionPassphrase) | |
| 1589 TEST_F(SyncManagerTest, SupplyPendingExplicitPass) { | |
| 1590 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 1591 Cryptographer other_cryptographer(&encryptor_); | |
| 1592 { | |
| 1593 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1594 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1595 std::string bootstrap_token; | |
| 1596 cryptographer->GetBootstrapToken(&bootstrap_token); | |
| 1597 other_cryptographer.Bootstrap(bootstrap_token); | |
| 1598 | |
| 1599 // Now update the nigori to reflect the new keys, and update the | |
| 1600 // cryptographer to have pending keys. | |
| 1601 KeyParams params = {"localhost", "dummy", "explicit"}; | |
| 1602 other_cryptographer.AddKey(params); | |
| 1603 WriteNode node(&trans); | |
| 1604 EXPECT_EQ(BaseNode::INIT_OK, node.InitTypeRoot(NIGORI)); | |
| 1605 sync_pb::NigoriSpecifics nigori; | |
| 1606 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag()); | |
| 1607 cryptographer->SetPendingKeys(nigori.encryption_keybag()); | |
| 1608 EXPECT_TRUE(cryptographer->has_pending_keys()); | |
| 1609 nigori.set_keybag_is_frozen(true); | |
| 1610 node.SetNigoriSpecifics(nigori); | |
| 1611 } | |
| 1612 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 1613 EXPECT_CALL(encryption_observer_, | |
| 1614 OnPassphraseTypeChanged(PassphraseType::CUSTOM_PASSPHRASE, _)); | |
| 1615 EXPECT_CALL(encryption_observer_, OnPassphraseRequired(_, _)); | |
| 1616 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false)); | |
| 1617 sync_manager_.GetEncryptionHandler()->Init(); | |
| 1618 EXPECT_CALL(encryption_observer_, | |
| 1619 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); | |
| 1620 ExpectPassphraseAcceptance(); | |
| 1621 sync_manager_.GetEncryptionHandler()->SetDecryptionPassphrase("explicit"); | |
| 1622 EXPECT_EQ(PassphraseType::CUSTOM_PASSPHRASE, GetPassphraseType()); | |
| 1623 EXPECT_FALSE(IsEncryptEverythingEnabledForTest()); | |
| 1624 { | |
| 1625 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1626 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1627 EXPECT_TRUE(cryptographer->is_ready()); | |
| 1628 // Verify we're encrypting with the new key. | |
| 1629 sync_pb::EncryptedData encrypted; | |
| 1630 cryptographer->GetKeys(&encrypted); | |
| 1631 EXPECT_TRUE(other_cryptographer.CanDecrypt(encrypted)); | |
| 1632 } | |
| 1633 } | |
| 1634 | |
| 1635 // Manually set the pending keys in the cryptographer/nigori to reflect the data | |
| 1636 // being encrypted with a new (unprovided) GAIA password, then supply the | |
| 1637 // password as a user-provided password. | |
| 1638 // This is the android case 7/8. | |
| 1639 TEST_F(SyncManagerTest, SupplyPendingGAIAPassUserProvided) { | |
| 1640 EXPECT_FALSE(SetUpEncryption(DONT_WRITE_NIGORI, UNINITIALIZED)); | |
| 1641 Cryptographer other_cryptographer(&encryptor_); | |
| 1642 { | |
| 1643 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1644 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1645 // Now update the nigori to reflect the new keys, and update the | |
| 1646 // cryptographer to have pending keys. | |
| 1647 KeyParams params = {"localhost", "dummy", "passphrase"}; | |
| 1648 other_cryptographer.AddKey(params); | |
| 1649 WriteNode node(&trans); | |
| 1650 EXPECT_EQ(BaseNode::INIT_OK, node.InitTypeRoot(NIGORI)); | |
| 1651 sync_pb::NigoriSpecifics nigori; | |
| 1652 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag()); | |
| 1653 node.SetNigoriSpecifics(nigori); | |
| 1654 cryptographer->SetPendingKeys(nigori.encryption_keybag()); | |
| 1655 EXPECT_FALSE(cryptographer->is_ready()); | |
| 1656 } | |
| 1657 EXPECT_CALL(encryption_observer_, | |
| 1658 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); | |
| 1659 ExpectPassphraseAcceptance(); | |
| 1660 SetImplicitPassphraseAndCheck("passphrase"); | |
| 1661 EXPECT_FALSE(IsEncryptEverythingEnabledForTest()); | |
| 1662 { | |
| 1663 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1664 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1665 EXPECT_TRUE(cryptographer->is_ready()); | |
| 1666 } | |
| 1667 } | |
| 1668 | |
| 1669 TEST_F(SyncManagerTest, SetPassphraseWithEmptyPasswordNode) { | |
| 1670 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 1671 int64_t node_id = 0; | |
| 1672 std::string tag = "foo"; | |
| 1673 { | |
| 1674 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1675 ReadNode root_node(&trans); | |
| 1676 root_node.InitByRootLookup(); | |
| 1677 | |
| 1678 WriteNode password_node(&trans); | |
| 1679 WriteNode::InitUniqueByCreationResult result = | |
| 1680 password_node.InitUniqueByCreation(PASSWORDS, root_node, tag); | |
| 1681 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); | |
| 1682 node_id = password_node.GetId(); | |
| 1683 } | |
| 1684 EXPECT_CALL(encryption_observer_, | |
| 1685 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); | |
| 1686 ExpectPassphraseAcceptance(); | |
| 1687 SetCustomPassphraseAndCheck("new_passphrase"); | |
| 1688 EXPECT_FALSE(IsEncryptEverythingEnabledForTest()); | |
| 1689 { | |
| 1690 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1691 ReadNode password_node(&trans); | |
| 1692 EXPECT_EQ(BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY, | |
| 1693 password_node.InitByClientTagLookup(PASSWORDS, tag)); | |
| 1694 } | |
| 1695 { | |
| 1696 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1697 ReadNode password_node(&trans); | |
| 1698 EXPECT_EQ(BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY, | |
| 1699 password_node.InitByIdLookup(node_id)); | |
| 1700 } | |
| 1701 } | |
| 1702 | |
| 1703 // Friended by WriteNode, so can't be in an anonymouse namespace. | |
| 1704 TEST_F(SyncManagerTest, EncryptBookmarksWithLegacyData) { | |
| 1705 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 1706 std::string title; | |
| 1707 SyncAPINameToServerName("Google", &title); | |
| 1708 std::string url = "http://www.google.com"; | |
| 1709 std::string raw_title2 = ".."; // An invalid cosmo title. | |
| 1710 std::string title2; | |
| 1711 SyncAPINameToServerName(raw_title2, &title2); | |
| 1712 std::string url2 = "http://www.bla.com"; | |
| 1713 | |
| 1714 // Create a bookmark using the legacy format. | |
| 1715 int64_t node_id1 = | |
| 1716 MakeNodeWithRoot(sync_manager_.GetUserShare(), BOOKMARKS, "testtag"); | |
| 1717 int64_t node_id2 = | |
| 1718 MakeNodeWithRoot(sync_manager_.GetUserShare(), BOOKMARKS, "testtag2"); | |
| 1719 { | |
| 1720 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1721 WriteNode node(&trans); | |
| 1722 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(node_id1)); | |
| 1723 | |
| 1724 sync_pb::EntitySpecifics entity_specifics; | |
| 1725 entity_specifics.mutable_bookmark()->set_url(url); | |
| 1726 node.SetEntitySpecifics(entity_specifics); | |
| 1727 | |
| 1728 // Set the old style title. | |
| 1729 syncable::MutableEntry* node_entry = node.entry_; | |
| 1730 node_entry->PutNonUniqueName(title); | |
| 1731 | |
| 1732 WriteNode node2(&trans); | |
| 1733 EXPECT_EQ(BaseNode::INIT_OK, node2.InitByIdLookup(node_id2)); | |
| 1734 | |
| 1735 sync_pb::EntitySpecifics entity_specifics2; | |
| 1736 entity_specifics2.mutable_bookmark()->set_url(url2); | |
| 1737 node2.SetEntitySpecifics(entity_specifics2); | |
| 1738 | |
| 1739 // Set the old style title. | |
| 1740 syncable::MutableEntry* node_entry2 = node2.entry_; | |
| 1741 node_entry2->PutNonUniqueName(title2); | |
| 1742 } | |
| 1743 | |
| 1744 { | |
| 1745 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1746 ReadNode node(&trans); | |
| 1747 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(node_id1)); | |
| 1748 EXPECT_EQ(BOOKMARKS, node.GetModelType()); | |
| 1749 EXPECT_EQ(title, node.GetTitle()); | |
| 1750 EXPECT_EQ(title, node.GetBookmarkSpecifics().title()); | |
| 1751 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); | |
| 1752 | |
| 1753 ReadNode node2(&trans); | |
| 1754 EXPECT_EQ(BaseNode::INIT_OK, node2.InitByIdLookup(node_id2)); | |
| 1755 EXPECT_EQ(BOOKMARKS, node2.GetModelType()); | |
| 1756 // We should de-canonicalize the title in GetTitle(), but the title in the | |
| 1757 // specifics should be stored in the server legal form. | |
| 1758 EXPECT_EQ(raw_title2, node2.GetTitle()); | |
| 1759 EXPECT_EQ(title2, node2.GetBookmarkSpecifics().title()); | |
| 1760 EXPECT_EQ(url2, node2.GetBookmarkSpecifics().url()); | |
| 1761 } | |
| 1762 | |
| 1763 { | |
| 1764 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1765 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
| 1766 trans.GetWrappedTrans(), BOOKMARKS, false /* not encrypted */)); | |
| 1767 } | |
| 1768 | |
| 1769 EXPECT_CALL( | |
| 1770 encryption_observer_, | |
| 1771 OnEncryptedTypesChanged(HasModelTypes(EncryptableUserTypes()), true)); | |
| 1772 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 1773 sync_manager_.GetEncryptionHandler()->EnableEncryptEverything(); | |
| 1774 EXPECT_TRUE(IsEncryptEverythingEnabledForTest()); | |
| 1775 | |
| 1776 { | |
| 1777 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1778 EXPECT_EQ(EncryptableUserTypes(), GetEncryptedTypesWithTrans(&trans)); | |
| 1779 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
| 1780 trans.GetWrappedTrans(), BOOKMARKS, true /* is encrypted */)); | |
| 1781 | |
| 1782 ReadNode node(&trans); | |
| 1783 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(node_id1)); | |
| 1784 EXPECT_EQ(BOOKMARKS, node.GetModelType()); | |
| 1785 EXPECT_EQ(title, node.GetTitle()); | |
| 1786 EXPECT_EQ(title, node.GetBookmarkSpecifics().title()); | |
| 1787 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); | |
| 1788 | |
| 1789 ReadNode node2(&trans); | |
| 1790 EXPECT_EQ(BaseNode::INIT_OK, node2.InitByIdLookup(node_id2)); | |
| 1791 EXPECT_EQ(BOOKMARKS, node2.GetModelType()); | |
| 1792 // We should de-canonicalize the title in GetTitle(), but the title in the | |
| 1793 // specifics should be stored in the server legal form. | |
| 1794 EXPECT_EQ(raw_title2, node2.GetTitle()); | |
| 1795 EXPECT_EQ(title2, node2.GetBookmarkSpecifics().title()); | |
| 1796 EXPECT_EQ(url2, node2.GetBookmarkSpecifics().url()); | |
| 1797 } | |
| 1798 } | |
| 1799 | |
| 1800 // Create a bookmark and set the title/url, then verify the data was properly | |
| 1801 // set. This replicates the unique way bookmarks have of creating sync nodes. | |
| 1802 // See BookmarkChangeProcessor::PlaceSyncNode(..). | |
| 1803 TEST_F(SyncManagerTest, CreateLocalBookmark) { | |
| 1804 std::string title = "title"; | |
| 1805 std::string url = "url"; | |
| 1806 { | |
| 1807 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1808 ReadNode bookmark_root(&trans); | |
| 1809 ASSERT_EQ(BaseNode::INIT_OK, bookmark_root.InitTypeRoot(BOOKMARKS)); | |
| 1810 WriteNode node(&trans); | |
| 1811 ASSERT_TRUE(node.InitBookmarkByCreation(bookmark_root, NULL)); | |
| 1812 node.SetIsFolder(false); | |
| 1813 node.SetTitle(title); | |
| 1814 | |
| 1815 sync_pb::BookmarkSpecifics bookmark_specifics(node.GetBookmarkSpecifics()); | |
| 1816 bookmark_specifics.set_url(url); | |
| 1817 node.SetBookmarkSpecifics(bookmark_specifics); | |
| 1818 } | |
| 1819 { | |
| 1820 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1821 ReadNode bookmark_root(&trans); | |
| 1822 ASSERT_EQ(BaseNode::INIT_OK, bookmark_root.InitTypeRoot(BOOKMARKS)); | |
| 1823 int64_t child_id = bookmark_root.GetFirstChildId(); | |
| 1824 | |
| 1825 ReadNode node(&trans); | |
| 1826 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id)); | |
| 1827 EXPECT_FALSE(node.GetIsFolder()); | |
| 1828 EXPECT_EQ(title, node.GetTitle()); | |
| 1829 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); | |
| 1830 } | |
| 1831 } | |
| 1832 | |
| 1833 // Verifies WriteNode::UpdateEntryWithEncryption does not make unnecessary | |
| 1834 // changes. | |
| 1835 TEST_F(SyncManagerTest, UpdateEntryWithEncryption) { | |
| 1836 std::string client_tag = "title"; | |
| 1837 sync_pb::EntitySpecifics entity_specifics; | |
| 1838 entity_specifics.mutable_bookmark()->set_url("url"); | |
| 1839 entity_specifics.mutable_bookmark()->set_title("title"); | |
| 1840 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag, | |
| 1841 syncable::GenerateSyncableHash(BOOKMARKS, client_tag), | |
| 1842 entity_specifics); | |
| 1843 // New node shouldn't start off unsynced. | |
| 1844 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 1845 // Manually change to the same data. Should not set is_unsynced. | |
| 1846 { | |
| 1847 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1848 WriteNode node(&trans); | |
| 1849 EXPECT_EQ(BaseNode::INIT_OK, | |
| 1850 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 1851 node.SetEntitySpecifics(entity_specifics); | |
| 1852 } | |
| 1853 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 1854 | |
| 1855 // Encrypt the datatatype, should set is_unsynced. | |
| 1856 EXPECT_CALL( | |
| 1857 encryption_observer_, | |
| 1858 OnEncryptedTypesChanged(HasModelTypes(EncryptableUserTypes()), true)); | |
| 1859 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 1860 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION)); | |
| 1861 | |
| 1862 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 1863 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, true)); | |
| 1864 sync_manager_.GetEncryptionHandler()->Init(); | |
| 1865 PumpLoop(); | |
| 1866 { | |
| 1867 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1868 ReadNode node(&trans); | |
| 1869 EXPECT_EQ(BaseNode::INIT_OK, | |
| 1870 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 1871 const syncable::Entry* node_entry = node.GetEntry(); | |
| 1872 const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics(); | |
| 1873 EXPECT_TRUE(specifics.has_encrypted()); | |
| 1874 EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName()); | |
| 1875 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1876 EXPECT_TRUE(cryptographer->is_ready()); | |
| 1877 EXPECT_TRUE( | |
| 1878 cryptographer->CanDecryptUsingDefaultKey(specifics.encrypted())); | |
| 1879 } | |
| 1880 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 1881 | |
| 1882 // Set a new passphrase. Should set is_unsynced. | |
| 1883 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | |
| 1884 EXPECT_CALL(encryption_observer_, | |
| 1885 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); | |
| 1886 ExpectPassphraseAcceptance(); | |
| 1887 SetCustomPassphraseAndCheck("new_passphrase"); | |
| 1888 { | |
| 1889 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1890 ReadNode node(&trans); | |
| 1891 EXPECT_EQ(BaseNode::INIT_OK, | |
| 1892 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 1893 const syncable::Entry* node_entry = node.GetEntry(); | |
| 1894 const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics(); | |
| 1895 EXPECT_TRUE(specifics.has_encrypted()); | |
| 1896 EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName()); | |
| 1897 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1898 EXPECT_TRUE(cryptographer->is_ready()); | |
| 1899 EXPECT_TRUE( | |
| 1900 cryptographer->CanDecryptUsingDefaultKey(specifics.encrypted())); | |
| 1901 } | |
| 1902 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 1903 | |
| 1904 // Force a re-encrypt everything. Should not set is_unsynced. | |
| 1905 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | |
| 1906 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 1907 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 1908 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, true)); | |
| 1909 | |
| 1910 sync_manager_.GetEncryptionHandler()->Init(); | |
| 1911 PumpLoop(); | |
| 1912 | |
| 1913 { | |
| 1914 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1915 ReadNode node(&trans); | |
| 1916 EXPECT_EQ(BaseNode::INIT_OK, | |
| 1917 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 1918 const syncable::Entry* node_entry = node.GetEntry(); | |
| 1919 const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics(); | |
| 1920 EXPECT_TRUE(specifics.has_encrypted()); | |
| 1921 EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName()); | |
| 1922 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1923 EXPECT_TRUE( | |
| 1924 cryptographer->CanDecryptUsingDefaultKey(specifics.encrypted())); | |
| 1925 } | |
| 1926 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 1927 | |
| 1928 // Manually change to the same data. Should not set is_unsynced. | |
| 1929 { | |
| 1930 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1931 WriteNode node(&trans); | |
| 1932 EXPECT_EQ(BaseNode::INIT_OK, | |
| 1933 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 1934 node.SetEntitySpecifics(entity_specifics); | |
| 1935 const syncable::Entry* node_entry = node.GetEntry(); | |
| 1936 const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics(); | |
| 1937 EXPECT_TRUE(specifics.has_encrypted()); | |
| 1938 EXPECT_FALSE(node_entry->GetIsUnsynced()); | |
| 1939 EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName()); | |
| 1940 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1941 EXPECT_TRUE( | |
| 1942 cryptographer->CanDecryptUsingDefaultKey(specifics.encrypted())); | |
| 1943 } | |
| 1944 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 1945 | |
| 1946 // Manually change to different data. Should set is_unsynced. | |
| 1947 { | |
| 1948 entity_specifics.mutable_bookmark()->set_url("url2"); | |
| 1949 entity_specifics.mutable_bookmark()->set_title("title2"); | |
| 1950 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1951 WriteNode node(&trans); | |
| 1952 EXPECT_EQ(BaseNode::INIT_OK, | |
| 1953 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 1954 node.SetEntitySpecifics(entity_specifics); | |
| 1955 const syncable::Entry* node_entry = node.GetEntry(); | |
| 1956 const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics(); | |
| 1957 EXPECT_TRUE(specifics.has_encrypted()); | |
| 1958 EXPECT_TRUE(node_entry->GetIsUnsynced()); | |
| 1959 EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName()); | |
| 1960 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1961 EXPECT_TRUE( | |
| 1962 cryptographer->CanDecryptUsingDefaultKey(specifics.encrypted())); | |
| 1963 } | |
| 1964 } | |
| 1965 | |
| 1966 // Passwords have their own handling for encryption. Verify it does not result | |
| 1967 // in unnecessary writes via SetEntitySpecifics. | |
| 1968 TEST_F(SyncManagerTest, UpdatePasswordSetEntitySpecificsNoChange) { | |
| 1969 std::string client_tag = "title"; | |
| 1970 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 1971 sync_pb::EntitySpecifics entity_specifics; | |
| 1972 { | |
| 1973 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1974 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 1975 sync_pb::PasswordSpecificsData data; | |
| 1976 data.set_password_value("secret"); | |
| 1977 cryptographer->Encrypt( | |
| 1978 data, entity_specifics.mutable_password()->mutable_encrypted()); | |
| 1979 } | |
| 1980 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag, | |
| 1981 syncable::GenerateSyncableHash(PASSWORDS, client_tag), | |
| 1982 entity_specifics); | |
| 1983 // New node shouldn't start off unsynced. | |
| 1984 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag)); | |
| 1985 | |
| 1986 // Manually change to the same data via SetEntitySpecifics. Should not set | |
| 1987 // is_unsynced. | |
| 1988 { | |
| 1989 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 1990 WriteNode node(&trans); | |
| 1991 EXPECT_EQ(BaseNode::INIT_OK, | |
| 1992 node.InitByClientTagLookup(PASSWORDS, client_tag)); | |
| 1993 node.SetEntitySpecifics(entity_specifics); | |
| 1994 } | |
| 1995 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag)); | |
| 1996 } | |
| 1997 | |
| 1998 // Passwords have their own handling for encryption. Verify it does not result | |
| 1999 // in unnecessary writes via SetPasswordSpecifics. | |
| 2000 TEST_F(SyncManagerTest, UpdatePasswordSetPasswordSpecifics) { | |
| 2001 std::string client_tag = "title"; | |
| 2002 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 2003 sync_pb::EntitySpecifics entity_specifics; | |
| 2004 { | |
| 2005 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2006 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 2007 sync_pb::PasswordSpecificsData data; | |
| 2008 data.set_password_value("secret"); | |
| 2009 cryptographer->Encrypt( | |
| 2010 data, entity_specifics.mutable_password()->mutable_encrypted()); | |
| 2011 } | |
| 2012 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag, | |
| 2013 syncable::GenerateSyncableHash(PASSWORDS, client_tag), | |
| 2014 entity_specifics); | |
| 2015 // New node shouldn't start off unsynced. | |
| 2016 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag)); | |
| 2017 | |
| 2018 // Manually change to the same data via SetPasswordSpecifics. Should not set | |
| 2019 // is_unsynced. | |
| 2020 { | |
| 2021 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2022 WriteNode node(&trans); | |
| 2023 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2024 node.InitByClientTagLookup(PASSWORDS, client_tag)); | |
| 2025 node.SetPasswordSpecifics(node.GetPasswordSpecifics()); | |
| 2026 } | |
| 2027 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag)); | |
| 2028 | |
| 2029 // Manually change to different data. Should set is_unsynced. | |
| 2030 { | |
| 2031 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2032 WriteNode node(&trans); | |
| 2033 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2034 node.InitByClientTagLookup(PASSWORDS, client_tag)); | |
| 2035 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 2036 sync_pb::PasswordSpecificsData data; | |
| 2037 data.set_password_value("secret2"); | |
| 2038 cryptographer->Encrypt( | |
| 2039 data, entity_specifics.mutable_password()->mutable_encrypted()); | |
| 2040 node.SetPasswordSpecifics(data); | |
| 2041 const syncable::Entry* node_entry = node.GetEntry(); | |
| 2042 EXPECT_TRUE(node_entry->GetIsUnsynced()); | |
| 2043 } | |
| 2044 } | |
| 2045 | |
| 2046 // Passwords have their own handling for encryption. Verify setting a new | |
| 2047 // passphrase updates the data and clears the unencrypted metadta for passwords. | |
| 2048 TEST_F(SyncManagerTest, UpdatePasswordNewPassphrase) { | |
| 2049 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 2050 sync_pb::EntitySpecifics entity_specifics; | |
| 2051 { | |
| 2052 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2053 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 2054 sync_pb::PasswordSpecificsData data; | |
| 2055 data.set_password_value(kPasswordValue); | |
| 2056 entity_specifics.mutable_password() | |
| 2057 ->mutable_unencrypted_metadata() | |
| 2058 ->set_url(kUrl); | |
| 2059 cryptographer->Encrypt( | |
| 2060 data, entity_specifics.mutable_password()->mutable_encrypted()); | |
| 2061 } | |
| 2062 EXPECT_TRUE(entity_specifics.password().has_unencrypted_metadata()); | |
| 2063 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, kClientTag, | |
| 2064 syncable::GenerateSyncableHash(PASSWORDS, kClientTag), | |
| 2065 entity_specifics); | |
| 2066 // New node shouldn't start off unsynced. | |
| 2067 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, kClientTag)); | |
| 2068 | |
| 2069 // Set a new passphrase. Should set is_unsynced. | |
| 2070 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | |
| 2071 EXPECT_CALL(encryption_observer_, | |
| 2072 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); | |
| 2073 ExpectPassphraseAcceptance(); | |
| 2074 SetCustomPassphraseAndCheck("new_passphrase"); | |
| 2075 { | |
| 2076 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2077 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 2078 EXPECT_TRUE(cryptographer->is_ready()); | |
| 2079 ReadNode password_node(&trans); | |
| 2080 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2081 password_node.InitByClientTagLookup(PASSWORDS, kClientTag)); | |
| 2082 const sync_pb::PasswordSpecificsData& data = | |
| 2083 password_node.GetPasswordSpecifics(); | |
| 2084 EXPECT_EQ(kPasswordValue, data.password_value()); | |
| 2085 EXPECT_FALSE(password_node.GetEntitySpecifics() | |
| 2086 .password() | |
| 2087 .has_unencrypted_metadata()); | |
| 2088 } | |
| 2089 EXPECT_TRUE(ResetUnsyncedEntry(PASSWORDS, kClientTag)); | |
| 2090 } | |
| 2091 | |
| 2092 // Passwords have their own handling for encryption. Verify it does not result | |
| 2093 // in unnecessary writes via ReencryptEverything. | |
| 2094 TEST_F(SyncManagerTest, UpdatePasswordReencryptEverything) { | |
| 2095 std::string client_tag = "title"; | |
| 2096 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 2097 sync_pb::EntitySpecifics entity_specifics; | |
| 2098 { | |
| 2099 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2100 Cryptographer* cryptographer = trans.GetCryptographer(); | |
| 2101 sync_pb::PasswordSpecificsData data; | |
| 2102 data.set_password_value("secret"); | |
| 2103 cryptographer->Encrypt( | |
| 2104 data, entity_specifics.mutable_password()->mutable_encrypted()); | |
| 2105 } | |
| 2106 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag, | |
| 2107 syncable::GenerateSyncableHash(PASSWORDS, client_tag), | |
| 2108 entity_specifics); | |
| 2109 // New node shouldn't start off unsynced. | |
| 2110 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag)); | |
| 2111 | |
| 2112 // Force a re-encrypt everything. Should not set is_unsynced. | |
| 2113 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | |
| 2114 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 2115 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 2116 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false)); | |
| 2117 sync_manager_.GetEncryptionHandler()->Init(); | |
| 2118 PumpLoop(); | |
| 2119 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag)); | |
| 2120 } | |
| 2121 | |
| 2122 // Test that attempting to start up with corrupted password data triggers | |
| 2123 // an unrecoverable error (rather than crashing). | |
| 2124 TEST_F(SyncManagerTest, ReencryptEverythingWithUnrecoverableErrorPasswords) { | |
| 2125 const char kClientTag[] = "client_tag"; | |
| 2126 | |
| 2127 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 2128 sync_pb::EntitySpecifics entity_specifics; | |
| 2129 { | |
| 2130 // Create a synced bookmark with undecryptable data. | |
| 2131 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2132 | |
| 2133 Cryptographer other_cryptographer(&encryptor_); | |
| 2134 KeyParams fake_params = {"localhost", "dummy", "fake_key"}; | |
| 2135 other_cryptographer.AddKey(fake_params); | |
| 2136 sync_pb::PasswordSpecificsData data; | |
| 2137 data.set_password_value("secret"); | |
| 2138 other_cryptographer.Encrypt( | |
| 2139 data, entity_specifics.mutable_password()->mutable_encrypted()); | |
| 2140 | |
| 2141 // Set up the real cryptographer with a different key. | |
| 2142 KeyParams real_params = {"localhost", "username", "real_key"}; | |
| 2143 trans.GetCryptographer()->AddKey(real_params); | |
| 2144 } | |
| 2145 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, kClientTag, | |
| 2146 syncable::GenerateSyncableHash(PASSWORDS, kClientTag), | |
| 2147 entity_specifics); | |
| 2148 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, kClientTag)); | |
| 2149 | |
| 2150 // Force a re-encrypt everything. Should trigger an unrecoverable error due | |
| 2151 // to being unable to decrypt the data that was previously applied. | |
| 2152 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | |
| 2153 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 2154 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 2155 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false)); | |
| 2156 EXPECT_FALSE(HasUnrecoverableError()); | |
| 2157 sync_manager_.GetEncryptionHandler()->Init(); | |
| 2158 PumpLoop(); | |
| 2159 EXPECT_TRUE(HasUnrecoverableError()); | |
| 2160 } | |
| 2161 | |
| 2162 // Test that attempting to start up with corrupted bookmark data triggers | |
| 2163 // an unrecoverable error (rather than crashing). | |
| 2164 TEST_F(SyncManagerTest, ReencryptEverythingWithUnrecoverableErrorBookmarks) { | |
| 2165 const char kClientTag[] = "client_tag"; | |
| 2166 EXPECT_CALL( | |
| 2167 encryption_observer_, | |
| 2168 OnEncryptedTypesChanged(HasModelTypes(EncryptableUserTypes()), true)); | |
| 2169 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION)); | |
| 2170 sync_pb::EntitySpecifics entity_specifics; | |
| 2171 { | |
| 2172 // Create a synced bookmark with undecryptable data. | |
| 2173 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2174 | |
| 2175 Cryptographer other_cryptographer(&encryptor_); | |
| 2176 KeyParams fake_params = {"localhost", "dummy", "fake_key"}; | |
| 2177 other_cryptographer.AddKey(fake_params); | |
| 2178 sync_pb::EntitySpecifics bm_specifics; | |
| 2179 bm_specifics.mutable_bookmark()->set_title("title"); | |
| 2180 bm_specifics.mutable_bookmark()->set_url("url"); | |
| 2181 sync_pb::EncryptedData encrypted; | |
| 2182 other_cryptographer.Encrypt(bm_specifics, &encrypted); | |
| 2183 entity_specifics.mutable_encrypted()->CopyFrom(encrypted); | |
| 2184 | |
| 2185 // Set up the real cryptographer with a different key. | |
| 2186 KeyParams real_params = {"localhost", "username", "real_key"}; | |
| 2187 trans.GetCryptographer()->AddKey(real_params); | |
| 2188 } | |
| 2189 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, kClientTag, | |
| 2190 syncable::GenerateSyncableHash(BOOKMARKS, kClientTag), | |
| 2191 entity_specifics); | |
| 2192 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, kClientTag)); | |
| 2193 | |
| 2194 // Force a re-encrypt everything. Should trigger an unrecoverable error due | |
| 2195 // to being unable to decrypt the data that was previously applied. | |
| 2196 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | |
| 2197 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 2198 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 2199 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, true)); | |
| 2200 EXPECT_FALSE(HasUnrecoverableError()); | |
| 2201 sync_manager_.GetEncryptionHandler()->Init(); | |
| 2202 PumpLoop(); | |
| 2203 EXPECT_TRUE(HasUnrecoverableError()); | |
| 2204 } | |
| 2205 | |
| 2206 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for bookmarks | |
| 2207 // when we write the same data, but does set it when we write new data. | |
| 2208 TEST_F(SyncManagerTest, SetBookmarkTitle) { | |
| 2209 std::string client_tag = "title"; | |
| 2210 sync_pb::EntitySpecifics entity_specifics; | |
| 2211 entity_specifics.mutable_bookmark()->set_url("url"); | |
| 2212 entity_specifics.mutable_bookmark()->set_title("title"); | |
| 2213 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag, | |
| 2214 syncable::GenerateSyncableHash(BOOKMARKS, client_tag), | |
| 2215 entity_specifics); | |
| 2216 // New node shouldn't start off unsynced. | |
| 2217 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 2218 | |
| 2219 // Manually change to the same title. Should not set is_unsynced. | |
| 2220 { | |
| 2221 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2222 WriteNode node(&trans); | |
| 2223 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2224 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 2225 node.SetTitle(client_tag); | |
| 2226 } | |
| 2227 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 2228 | |
| 2229 // Manually change to new title. Should set is_unsynced. | |
| 2230 { | |
| 2231 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2232 WriteNode node(&trans); | |
| 2233 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2234 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 2235 node.SetTitle("title2"); | |
| 2236 } | |
| 2237 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 2238 } | |
| 2239 | |
| 2240 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for encrypted | |
| 2241 // bookmarks when we write the same data, but does set it when we write new | |
| 2242 // data. | |
| 2243 TEST_F(SyncManagerTest, SetBookmarkTitleWithEncryption) { | |
| 2244 std::string client_tag = "title"; | |
| 2245 sync_pb::EntitySpecifics entity_specifics; | |
| 2246 entity_specifics.mutable_bookmark()->set_url("url"); | |
| 2247 entity_specifics.mutable_bookmark()->set_title("title"); | |
| 2248 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag, | |
| 2249 syncable::GenerateSyncableHash(BOOKMARKS, client_tag), | |
| 2250 entity_specifics); | |
| 2251 // New node shouldn't start off unsynced. | |
| 2252 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 2253 | |
| 2254 // Encrypt the datatatype, should set is_unsynced. | |
| 2255 EXPECT_CALL( | |
| 2256 encryption_observer_, | |
| 2257 OnEncryptedTypesChanged(HasModelTypes(EncryptableUserTypes()), true)); | |
| 2258 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 2259 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION)); | |
| 2260 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 2261 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, true)); | |
| 2262 sync_manager_.GetEncryptionHandler()->Init(); | |
| 2263 PumpLoop(); | |
| 2264 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 2265 | |
| 2266 // Manually change to the same title. Should not set is_unsynced. | |
| 2267 // NON_UNIQUE_NAME should be kEncryptedString. | |
| 2268 { | |
| 2269 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2270 WriteNode node(&trans); | |
| 2271 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2272 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 2273 node.SetTitle(client_tag); | |
| 2274 const syncable::Entry* node_entry = node.GetEntry(); | |
| 2275 const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics(); | |
| 2276 EXPECT_TRUE(specifics.has_encrypted()); | |
| 2277 EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName()); | |
| 2278 } | |
| 2279 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 2280 | |
| 2281 // Manually change to new title. Should set is_unsynced. NON_UNIQUE_NAME | |
| 2282 // should still be kEncryptedString. | |
| 2283 { | |
| 2284 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2285 WriteNode node(&trans); | |
| 2286 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2287 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 2288 node.SetTitle("title2"); | |
| 2289 const syncable::Entry* node_entry = node.GetEntry(); | |
| 2290 const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics(); | |
| 2291 EXPECT_TRUE(specifics.has_encrypted()); | |
| 2292 EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName()); | |
| 2293 } | |
| 2294 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag)); | |
| 2295 } | |
| 2296 | |
| 2297 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for non-bookmarks | |
| 2298 // when we write the same data, but does set it when we write new data. | |
| 2299 TEST_F(SyncManagerTest, SetNonBookmarkTitle) { | |
| 2300 std::string client_tag = "title"; | |
| 2301 sync_pb::EntitySpecifics entity_specifics; | |
| 2302 entity_specifics.mutable_preference()->set_name("name"); | |
| 2303 entity_specifics.mutable_preference()->set_value("value"); | |
| 2304 MakeServerNode(sync_manager_.GetUserShare(), PREFERENCES, client_tag, | |
| 2305 syncable::GenerateSyncableHash(PREFERENCES, client_tag), | |
| 2306 entity_specifics); | |
| 2307 // New node shouldn't start off unsynced. | |
| 2308 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, client_tag)); | |
| 2309 | |
| 2310 // Manually change to the same title. Should not set is_unsynced. | |
| 2311 { | |
| 2312 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2313 WriteNode node(&trans); | |
| 2314 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2315 node.InitByClientTagLookup(PREFERENCES, client_tag)); | |
| 2316 node.SetTitle(client_tag); | |
| 2317 } | |
| 2318 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, client_tag)); | |
| 2319 | |
| 2320 // Manually change to new title. Should set is_unsynced. | |
| 2321 { | |
| 2322 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2323 WriteNode node(&trans); | |
| 2324 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2325 node.InitByClientTagLookup(PREFERENCES, client_tag)); | |
| 2326 node.SetTitle("title2"); | |
| 2327 } | |
| 2328 EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES, client_tag)); | |
| 2329 } | |
| 2330 | |
| 2331 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for encrypted | |
| 2332 // non-bookmarks when we write the same data or when we write new data | |
| 2333 // data (should remained kEncryptedString). | |
| 2334 TEST_F(SyncManagerTest, SetNonBookmarkTitleWithEncryption) { | |
| 2335 std::string client_tag = "title"; | |
| 2336 sync_pb::EntitySpecifics entity_specifics; | |
| 2337 entity_specifics.mutable_preference()->set_name("name"); | |
| 2338 entity_specifics.mutable_preference()->set_value("value"); | |
| 2339 MakeServerNode(sync_manager_.GetUserShare(), PREFERENCES, client_tag, | |
| 2340 syncable::GenerateSyncableHash(PREFERENCES, client_tag), | |
| 2341 entity_specifics); | |
| 2342 // New node shouldn't start off unsynced. | |
| 2343 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, client_tag)); | |
| 2344 | |
| 2345 // Encrypt the datatatype, should set is_unsynced. | |
| 2346 EXPECT_CALL( | |
| 2347 encryption_observer_, | |
| 2348 OnEncryptedTypesChanged(HasModelTypes(EncryptableUserTypes()), true)); | |
| 2349 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 2350 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION)); | |
| 2351 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 2352 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, true)); | |
| 2353 sync_manager_.GetEncryptionHandler()->Init(); | |
| 2354 PumpLoop(); | |
| 2355 EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES, client_tag)); | |
| 2356 | |
| 2357 // Manually change to the same title. Should not set is_unsynced. | |
| 2358 // NON_UNIQUE_NAME should be kEncryptedString. | |
| 2359 { | |
| 2360 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2361 WriteNode node(&trans); | |
| 2362 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2363 node.InitByClientTagLookup(PREFERENCES, client_tag)); | |
| 2364 node.SetTitle(client_tag); | |
| 2365 const syncable::Entry* node_entry = node.GetEntry(); | |
| 2366 const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics(); | |
| 2367 EXPECT_TRUE(specifics.has_encrypted()); | |
| 2368 EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName()); | |
| 2369 } | |
| 2370 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, client_tag)); | |
| 2371 | |
| 2372 // Manually change to new title. Should not set is_unsynced because the | |
| 2373 // NON_UNIQUE_NAME should still be kEncryptedString. | |
| 2374 { | |
| 2375 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2376 WriteNode node(&trans); | |
| 2377 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2378 node.InitByClientTagLookup(PREFERENCES, client_tag)); | |
| 2379 node.SetTitle("title2"); | |
| 2380 const syncable::Entry* node_entry = node.GetEntry(); | |
| 2381 const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics(); | |
| 2382 EXPECT_TRUE(specifics.has_encrypted()); | |
| 2383 EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName()); | |
| 2384 EXPECT_FALSE(node_entry->GetIsUnsynced()); | |
| 2385 } | |
| 2386 } | |
| 2387 | |
| 2388 // Ensure that titles are truncated to 255 bytes, and attempting to reset | |
| 2389 // them to their longer version does not set IS_UNSYNCED. | |
| 2390 TEST_F(SyncManagerTest, SetLongTitle) { | |
| 2391 const int kNumChars = 512; | |
| 2392 const std::string kClientTag = "tag"; | |
| 2393 std::string title(kNumChars, '0'); | |
| 2394 sync_pb::EntitySpecifics entity_specifics; | |
| 2395 entity_specifics.mutable_preference()->set_name("name"); | |
| 2396 entity_specifics.mutable_preference()->set_value("value"); | |
| 2397 MakeServerNode(sync_manager_.GetUserShare(), PREFERENCES, "short_title", | |
| 2398 syncable::GenerateSyncableHash(PREFERENCES, kClientTag), | |
| 2399 entity_specifics); | |
| 2400 // New node shouldn't start off unsynced. | |
| 2401 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, kClientTag)); | |
| 2402 | |
| 2403 // Manually change to the long title. Should set is_unsynced. | |
| 2404 { | |
| 2405 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2406 WriteNode node(&trans); | |
| 2407 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2408 node.InitByClientTagLookup(PREFERENCES, kClientTag)); | |
| 2409 node.SetTitle(title); | |
| 2410 EXPECT_EQ(node.GetTitle(), title.substr(0, 255)); | |
| 2411 } | |
| 2412 EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES, kClientTag)); | |
| 2413 | |
| 2414 // Manually change to the same title. Should not set is_unsynced. | |
| 2415 { | |
| 2416 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2417 WriteNode node(&trans); | |
| 2418 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2419 node.InitByClientTagLookup(PREFERENCES, kClientTag)); | |
| 2420 node.SetTitle(title); | |
| 2421 EXPECT_EQ(node.GetTitle(), title.substr(0, 255)); | |
| 2422 } | |
| 2423 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, kClientTag)); | |
| 2424 | |
| 2425 // Manually change to new title. Should set is_unsynced. | |
| 2426 { | |
| 2427 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2428 WriteNode node(&trans); | |
| 2429 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2430 node.InitByClientTagLookup(PREFERENCES, kClientTag)); | |
| 2431 node.SetTitle("title2"); | |
| 2432 } | |
| 2433 EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES, kClientTag)); | |
| 2434 } | |
| 2435 | |
| 2436 // Create an encrypted entry when the cryptographer doesn't think the type is | |
| 2437 // marked for encryption. Ensure reads/writes don't break and don't unencrypt | |
| 2438 // the data. | |
| 2439 TEST_F(SyncManagerTest, SetPreviouslyEncryptedSpecifics) { | |
| 2440 std::string client_tag = "tag"; | |
| 2441 std::string url = "url"; | |
| 2442 std::string url2 = "new_url"; | |
| 2443 std::string title = "title"; | |
| 2444 sync_pb::EntitySpecifics entity_specifics; | |
| 2445 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 2446 { | |
| 2447 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2448 Cryptographer* crypto = trans.GetCryptographer(); | |
| 2449 sync_pb::EntitySpecifics bm_specifics; | |
| 2450 bm_specifics.mutable_bookmark()->set_title("title"); | |
| 2451 bm_specifics.mutable_bookmark()->set_url("url"); | |
| 2452 sync_pb::EncryptedData encrypted; | |
| 2453 crypto->Encrypt(bm_specifics, &encrypted); | |
| 2454 entity_specifics.mutable_encrypted()->CopyFrom(encrypted); | |
| 2455 AddDefaultFieldValue(BOOKMARKS, &entity_specifics); | |
| 2456 } | |
| 2457 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag, | |
| 2458 syncable::GenerateSyncableHash(BOOKMARKS, client_tag), | |
| 2459 entity_specifics); | |
| 2460 | |
| 2461 { | |
| 2462 // Verify the data. | |
| 2463 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2464 ReadNode node(&trans); | |
| 2465 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2466 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 2467 EXPECT_EQ(title, node.GetTitle()); | |
| 2468 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); | |
| 2469 } | |
| 2470 | |
| 2471 { | |
| 2472 // Overwrite the url (which overwrites the specifics). | |
| 2473 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2474 WriteNode node(&trans); | |
| 2475 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2476 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 2477 | |
| 2478 sync_pb::BookmarkSpecifics bookmark_specifics(node.GetBookmarkSpecifics()); | |
| 2479 bookmark_specifics.set_url(url2); | |
| 2480 node.SetBookmarkSpecifics(bookmark_specifics); | |
| 2481 } | |
| 2482 | |
| 2483 { | |
| 2484 // Verify it's still encrypted and it has the most recent url. | |
| 2485 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2486 ReadNode node(&trans); | |
| 2487 EXPECT_EQ(BaseNode::INIT_OK, | |
| 2488 node.InitByClientTagLookup(BOOKMARKS, client_tag)); | |
| 2489 EXPECT_EQ(title, node.GetTitle()); | |
| 2490 EXPECT_EQ(url2, node.GetBookmarkSpecifics().url()); | |
| 2491 const syncable::Entry* node_entry = node.GetEntry(); | |
| 2492 EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName()); | |
| 2493 const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics(); | |
| 2494 EXPECT_TRUE(specifics.has_encrypted()); | |
| 2495 } | |
| 2496 } | |
| 2497 | |
| 2498 // Verify transaction version of a model type is incremented when node of | |
| 2499 // that type is updated. | |
| 2500 TEST_F(SyncManagerTest, IncrementTransactionVersion) { | |
| 2501 ModelSafeRoutingInfo routing_info; | |
| 2502 GetModelSafeRoutingInfo(&routing_info); | |
| 2503 | |
| 2504 { | |
| 2505 ReadTransaction read_trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2506 for (ModelSafeRoutingInfo::iterator i = routing_info.begin(); | |
| 2507 i != routing_info.end(); ++i) { | |
| 2508 // Transaction version is incremented when SyncManagerTest::SetUp() | |
| 2509 // creates a node of each type. | |
| 2510 EXPECT_EQ(1, | |
| 2511 sync_manager_.GetUserShare()->directory->GetTransactionVersion( | |
| 2512 i->first)); | |
| 2513 } | |
| 2514 } | |
| 2515 | |
| 2516 // Create bookmark node to increment transaction version of bookmark model. | |
| 2517 std::string client_tag = "title"; | |
| 2518 sync_pb::EntitySpecifics entity_specifics; | |
| 2519 entity_specifics.mutable_bookmark()->set_url("url"); | |
| 2520 entity_specifics.mutable_bookmark()->set_title("title"); | |
| 2521 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag, | |
| 2522 syncable::GenerateSyncableHash(BOOKMARKS, client_tag), | |
| 2523 entity_specifics); | |
| 2524 | |
| 2525 { | |
| 2526 ReadTransaction read_trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2527 for (ModelSafeRoutingInfo::iterator i = routing_info.begin(); | |
| 2528 i != routing_info.end(); ++i) { | |
| 2529 EXPECT_EQ(i->first == BOOKMARKS ? 2 : 1, | |
| 2530 sync_manager_.GetUserShare()->directory->GetTransactionVersion( | |
| 2531 i->first)); | |
| 2532 } | |
| 2533 } | |
| 2534 } | |
| 2535 | |
| 2536 class MockSyncScheduler : public FakeSyncScheduler { | |
| 2537 public: | |
| 2538 MockSyncScheduler() : FakeSyncScheduler() {} | |
| 2539 virtual ~MockSyncScheduler() {} | |
| 2540 | |
| 2541 MOCK_METHOD2(Start, void(SyncScheduler::Mode, base::Time)); | |
| 2542 MOCK_METHOD1(ScheduleConfiguration, void(const ConfigurationParams&)); | |
| 2543 }; | |
| 2544 | |
| 2545 class ComponentsFactory : public TestInternalComponentsFactory { | |
| 2546 public: | |
| 2547 ComponentsFactory(const Switches& switches, | |
| 2548 SyncScheduler* scheduler_to_use, | |
| 2549 SyncCycleContext** cycle_context, | |
| 2550 InternalComponentsFactory::StorageOption* storage_used) | |
| 2551 : TestInternalComponentsFactory( | |
| 2552 switches, | |
| 2553 InternalComponentsFactory::STORAGE_IN_MEMORY, | |
| 2554 storage_used), | |
| 2555 scheduler_to_use_(scheduler_to_use), | |
| 2556 cycle_context_(cycle_context) {} | |
| 2557 ~ComponentsFactory() override {} | |
| 2558 | |
| 2559 std::unique_ptr<SyncScheduler> BuildScheduler( | |
| 2560 const std::string& name, | |
| 2561 SyncCycleContext* context, | |
| 2562 CancelationSignal* stop_handle) override { | |
| 2563 *cycle_context_ = context; | |
| 2564 return std::move(scheduler_to_use_); | |
| 2565 } | |
| 2566 | |
| 2567 private: | |
| 2568 std::unique_ptr<SyncScheduler> scheduler_to_use_; | |
| 2569 SyncCycleContext** cycle_context_; | |
| 2570 }; | |
| 2571 | |
| 2572 class SyncManagerTestWithMockScheduler : public SyncManagerTest { | |
| 2573 public: | |
| 2574 SyncManagerTestWithMockScheduler() : scheduler_(NULL) {} | |
| 2575 InternalComponentsFactory* GetFactory() override { | |
| 2576 scheduler_ = new MockSyncScheduler(); | |
| 2577 return new ComponentsFactory(GetSwitches(), scheduler_, &cycle_context_, | |
| 2578 &storage_used_); | |
| 2579 } | |
| 2580 | |
| 2581 MockSyncScheduler* scheduler() { return scheduler_; } | |
| 2582 SyncCycleContext* cycle_context() { return cycle_context_; } | |
| 2583 | |
| 2584 private: | |
| 2585 MockSyncScheduler* scheduler_; | |
| 2586 SyncCycleContext* cycle_context_; | |
| 2587 }; | |
| 2588 | |
| 2589 // Test that the configuration params are properly created and sent to | |
| 2590 // ScheduleConfigure. No callback should be invoked. Any disabled datatypes | |
| 2591 // should be purged. | |
| 2592 TEST_F(SyncManagerTestWithMockScheduler, BasicConfiguration) { | |
| 2593 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; | |
| 2594 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES); | |
| 2595 ModelSafeRoutingInfo new_routing_info; | |
| 2596 GetModelSafeRoutingInfo(&new_routing_info); | |
| 2597 ModelTypeSet enabled_types = GetRoutingInfoTypes(new_routing_info); | |
| 2598 ModelTypeSet disabled_types = Difference(ModelTypeSet::All(), enabled_types); | |
| 2599 | |
| 2600 ConfigurationParams params; | |
| 2601 EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE, _)); | |
| 2602 EXPECT_CALL(*scheduler(), ScheduleConfiguration(_)) | |
| 2603 .WillOnce(SaveArg<0>(¶ms)); | |
| 2604 | |
| 2605 // Set data for all types. | |
| 2606 ModelTypeSet protocol_types = ProtocolTypes(); | |
| 2607 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good(); | |
| 2608 iter.Inc()) { | |
| 2609 SetProgressMarkerForType(iter.Get(), true); | |
| 2610 } | |
| 2611 | |
| 2612 CallbackCounter ready_task_counter, retry_task_counter; | |
| 2613 sync_manager_.ConfigureSyncer( | |
| 2614 reason, types_to_download, disabled_types, ModelTypeSet(), ModelTypeSet(), | |
| 2615 new_routing_info, base::Bind(&CallbackCounter::Callback, | |
| 2616 base::Unretained(&ready_task_counter)), | |
| 2617 base::Bind(&CallbackCounter::Callback, | |
| 2618 base::Unretained(&retry_task_counter))); | |
| 2619 EXPECT_EQ(0, ready_task_counter.times_called()); | |
| 2620 EXPECT_EQ(0, retry_task_counter.times_called()); | |
| 2621 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, params.source); | |
| 2622 EXPECT_EQ(types_to_download, params.types_to_download); | |
| 2623 EXPECT_EQ(new_routing_info, params.routing_info); | |
| 2624 | |
| 2625 // Verify all the disabled types were purged. | |
| 2626 EXPECT_EQ(enabled_types, | |
| 2627 sync_manager_.GetUserShare()->directory->InitialSyncEndedTypes()); | |
| 2628 EXPECT_EQ(disabled_types, sync_manager_.GetTypesWithEmptyProgressMarkerToken( | |
| 2629 ModelTypeSet::All())); | |
| 2630 } | |
| 2631 | |
| 2632 // Test that on a reconfiguration (configuration where the session context | |
| 2633 // already has routing info), only those recently disabled types are purged. | |
| 2634 TEST_F(SyncManagerTestWithMockScheduler, ReConfiguration) { | |
| 2635 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; | |
| 2636 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES); | |
| 2637 ModelTypeSet disabled_types = ModelTypeSet(THEMES, SESSIONS); | |
| 2638 ModelSafeRoutingInfo old_routing_info; | |
| 2639 ModelSafeRoutingInfo new_routing_info; | |
| 2640 GetModelSafeRoutingInfo(&old_routing_info); | |
| 2641 new_routing_info = old_routing_info; | |
| 2642 new_routing_info.erase(THEMES); | |
| 2643 new_routing_info.erase(SESSIONS); | |
| 2644 ModelTypeSet enabled_types = GetRoutingInfoTypes(new_routing_info); | |
| 2645 | |
| 2646 ConfigurationParams params; | |
| 2647 EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE, _)); | |
| 2648 EXPECT_CALL(*scheduler(), ScheduleConfiguration(_)) | |
| 2649 .WillOnce(SaveArg<0>(¶ms)); | |
| 2650 | |
| 2651 // Set data for all types except those recently disabled (so we can verify | |
| 2652 // only those recently disabled are purged) . | |
| 2653 ModelTypeSet protocol_types = ProtocolTypes(); | |
| 2654 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good(); | |
| 2655 iter.Inc()) { | |
| 2656 if (!disabled_types.Has(iter.Get())) { | |
| 2657 SetProgressMarkerForType(iter.Get(), true); | |
| 2658 } else { | |
| 2659 SetProgressMarkerForType(iter.Get(), false); | |
| 2660 } | |
| 2661 } | |
| 2662 | |
| 2663 // Set the context to have the old routing info. | |
| 2664 cycle_context()->SetRoutingInfo(old_routing_info); | |
| 2665 | |
| 2666 CallbackCounter ready_task_counter, retry_task_counter; | |
| 2667 sync_manager_.ConfigureSyncer( | |
| 2668 reason, types_to_download, ModelTypeSet(), ModelTypeSet(), ModelTypeSet(), | |
| 2669 new_routing_info, base::Bind(&CallbackCounter::Callback, | |
| 2670 base::Unretained(&ready_task_counter)), | |
| 2671 base::Bind(&CallbackCounter::Callback, | |
| 2672 base::Unretained(&retry_task_counter))); | |
| 2673 EXPECT_EQ(0, ready_task_counter.times_called()); | |
| 2674 EXPECT_EQ(0, retry_task_counter.times_called()); | |
| 2675 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, params.source); | |
| 2676 EXPECT_EQ(types_to_download, params.types_to_download); | |
| 2677 EXPECT_EQ(new_routing_info, params.routing_info); | |
| 2678 | |
| 2679 // Verify only the recently disabled types were purged. | |
| 2680 EXPECT_EQ(disabled_types, sync_manager_.GetTypesWithEmptyProgressMarkerToken( | |
| 2681 ProtocolTypes())); | |
| 2682 } | |
| 2683 | |
| 2684 // Test that SyncManager::ClearServerData invokes the scheduler. | |
| 2685 TEST_F(SyncManagerTestWithMockScheduler, ClearServerData) { | |
| 2686 EXPECT_CALL(*scheduler(), Start(SyncScheduler::CLEAR_SERVER_DATA_MODE, _)); | |
| 2687 CallbackCounter callback_counter; | |
| 2688 sync_manager_.ClearServerData(base::Bind( | |
| 2689 &CallbackCounter::Callback, base::Unretained(&callback_counter))); | |
| 2690 PumpLoop(); | |
| 2691 EXPECT_EQ(1, callback_counter.times_called()); | |
| 2692 } | |
| 2693 | |
| 2694 // Test that PurgePartiallySyncedTypes purges only those types that have not | |
| 2695 // fully completed their initial download and apply. | |
| 2696 TEST_F(SyncManagerTest, PurgePartiallySyncedTypes) { | |
| 2697 ModelSafeRoutingInfo routing_info; | |
| 2698 GetModelSafeRoutingInfo(&routing_info); | |
| 2699 ModelTypeSet enabled_types = GetRoutingInfoTypes(routing_info); | |
| 2700 | |
| 2701 UserShare* share = sync_manager_.GetUserShare(); | |
| 2702 | |
| 2703 // The test harness automatically initializes all types in the routing info. | |
| 2704 // Check that autofill is not among them. | |
| 2705 ASSERT_FALSE(enabled_types.Has(AUTOFILL)); | |
| 2706 | |
| 2707 // Further ensure that the test harness did not create its root node. | |
| 2708 { | |
| 2709 syncable::ReadTransaction trans(FROM_HERE, share->directory.get()); | |
| 2710 syncable::Entry autofill_root_node(&trans, syncable::GET_TYPE_ROOT, | |
| 2711 AUTOFILL); | |
| 2712 ASSERT_FALSE(autofill_root_node.good()); | |
| 2713 } | |
| 2714 | |
| 2715 // One more redundant check. | |
| 2716 ASSERT_FALSE( | |
| 2717 sync_manager_.GetUserShare()->directory->InitialSyncEndedTypes().Has( | |
| 2718 AUTOFILL)); | |
| 2719 | |
| 2720 // Give autofill a progress marker. | |
| 2721 sync_pb::DataTypeProgressMarker autofill_marker; | |
| 2722 autofill_marker.set_data_type_id( | |
| 2723 GetSpecificsFieldNumberFromModelType(AUTOFILL)); | |
| 2724 autofill_marker.set_token("token"); | |
| 2725 share->directory->SetDownloadProgress(AUTOFILL, autofill_marker); | |
| 2726 | |
| 2727 // Also add a pending autofill root node update from the server. | |
| 2728 TestEntryFactory factory_(share->directory.get()); | |
| 2729 int autofill_meta = factory_.CreateUnappliedRootNode(AUTOFILL); | |
| 2730 | |
| 2731 // Preferences is an enabled type. Check that the harness initialized it. | |
| 2732 ASSERT_TRUE(enabled_types.Has(PREFERENCES)); | |
| 2733 ASSERT_TRUE( | |
| 2734 sync_manager_.GetUserShare()->directory->InitialSyncEndedTypes().Has( | |
| 2735 PREFERENCES)); | |
| 2736 | |
| 2737 // Give preferencse a progress marker. | |
| 2738 sync_pb::DataTypeProgressMarker prefs_marker; | |
| 2739 prefs_marker.set_data_type_id( | |
| 2740 GetSpecificsFieldNumberFromModelType(PREFERENCES)); | |
| 2741 prefs_marker.set_token("token"); | |
| 2742 share->directory->SetDownloadProgress(PREFERENCES, prefs_marker); | |
| 2743 | |
| 2744 // Add a fully synced preferences node under the root. | |
| 2745 std::string pref_client_tag = "prefABC"; | |
| 2746 std::string pref_hashed_tag = "hashXYZ"; | |
| 2747 sync_pb::EntitySpecifics pref_specifics; | |
| 2748 AddDefaultFieldValue(PREFERENCES, &pref_specifics); | |
| 2749 int pref_meta = MakeServerNode(share, PREFERENCES, pref_client_tag, | |
| 2750 pref_hashed_tag, pref_specifics); | |
| 2751 | |
| 2752 // And now, the purge. | |
| 2753 EXPECT_TRUE(sync_manager_.PurgePartiallySyncedTypes()); | |
| 2754 | |
| 2755 // Ensure that autofill lost its progress marker, but preferences did not. | |
| 2756 ModelTypeSet empty_tokens = | |
| 2757 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); | |
| 2758 EXPECT_TRUE(empty_tokens.Has(AUTOFILL)); | |
| 2759 EXPECT_FALSE(empty_tokens.Has(PREFERENCES)); | |
| 2760 | |
| 2761 // Ensure that autofill lost its node, but preferences did not. | |
| 2762 { | |
| 2763 syncable::ReadTransaction trans(FROM_HERE, share->directory.get()); | |
| 2764 syncable::Entry autofill_node(&trans, GET_BY_HANDLE, autofill_meta); | |
| 2765 syncable::Entry pref_node(&trans, GET_BY_HANDLE, pref_meta); | |
| 2766 EXPECT_FALSE(autofill_node.good()); | |
| 2767 EXPECT_TRUE(pref_node.good()); | |
| 2768 } | |
| 2769 } | |
| 2770 | |
| 2771 // Test CleanupDisabledTypes properly purges all disabled types as specified | |
| 2772 // by the previous and current enabled params. | |
| 2773 TEST_F(SyncManagerTest, PurgeDisabledTypes) { | |
| 2774 ModelSafeRoutingInfo routing_info; | |
| 2775 GetModelSafeRoutingInfo(&routing_info); | |
| 2776 ModelTypeSet enabled_types = GetRoutingInfoTypes(routing_info); | |
| 2777 ModelTypeSet disabled_types = Difference(ModelTypeSet::All(), enabled_types); | |
| 2778 | |
| 2779 // The harness should have initialized the enabled_types for us. | |
| 2780 EXPECT_EQ(enabled_types, | |
| 2781 sync_manager_.GetUserShare()->directory->InitialSyncEndedTypes()); | |
| 2782 | |
| 2783 // Set progress markers for all types. | |
| 2784 ModelTypeSet protocol_types = ProtocolTypes(); | |
| 2785 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good(); | |
| 2786 iter.Inc()) { | |
| 2787 SetProgressMarkerForType(iter.Get(), true); | |
| 2788 } | |
| 2789 | |
| 2790 // Verify all the enabled types remain after cleanup, and all the disabled | |
| 2791 // types were purged. | |
| 2792 sync_manager_.PurgeDisabledTypes(disabled_types, ModelTypeSet(), | |
| 2793 ModelTypeSet()); | |
| 2794 EXPECT_EQ(enabled_types, | |
| 2795 sync_manager_.GetUserShare()->directory->InitialSyncEndedTypes()); | |
| 2796 EXPECT_EQ(disabled_types, sync_manager_.GetTypesWithEmptyProgressMarkerToken( | |
| 2797 ModelTypeSet::All())); | |
| 2798 | |
| 2799 // Disable some more types. | |
| 2800 disabled_types.Put(BOOKMARKS); | |
| 2801 disabled_types.Put(PREFERENCES); | |
| 2802 ModelTypeSet new_enabled_types = | |
| 2803 Difference(ModelTypeSet::All(), disabled_types); | |
| 2804 | |
| 2805 // Verify only the non-disabled types remain after cleanup. | |
| 2806 sync_manager_.PurgeDisabledTypes(disabled_types, ModelTypeSet(), | |
| 2807 ModelTypeSet()); | |
| 2808 EXPECT_EQ(new_enabled_types, | |
| 2809 sync_manager_.GetUserShare()->directory->InitialSyncEndedTypes()); | |
| 2810 EXPECT_EQ(disabled_types, sync_manager_.GetTypesWithEmptyProgressMarkerToken( | |
| 2811 ModelTypeSet::All())); | |
| 2812 } | |
| 2813 | |
| 2814 // Test PurgeDisabledTypes properly unapplies types by deleting their local data | |
| 2815 // and preserving their server data and progress marker. | |
| 2816 TEST_F(SyncManagerTest, PurgeUnappliedTypes) { | |
| 2817 ModelSafeRoutingInfo routing_info; | |
| 2818 GetModelSafeRoutingInfo(&routing_info); | |
| 2819 ModelTypeSet unapplied_types = ModelTypeSet(BOOKMARKS, PREFERENCES); | |
| 2820 ModelTypeSet enabled_types = GetRoutingInfoTypes(routing_info); | |
| 2821 ModelTypeSet disabled_types = Difference(ModelTypeSet::All(), enabled_types); | |
| 2822 | |
| 2823 // The harness should have initialized the enabled_types for us. | |
| 2824 EXPECT_EQ(enabled_types, | |
| 2825 sync_manager_.GetUserShare()->directory->InitialSyncEndedTypes()); | |
| 2826 | |
| 2827 // Set progress markers for all types. | |
| 2828 ModelTypeSet protocol_types = ProtocolTypes(); | |
| 2829 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good(); | |
| 2830 iter.Inc()) { | |
| 2831 SetProgressMarkerForType(iter.Get(), true); | |
| 2832 } | |
| 2833 | |
| 2834 // Add the following kinds of items: | |
| 2835 // 1. Fully synced preference. | |
| 2836 // 2. Locally created preference, server unknown, unsynced | |
| 2837 // 3. Locally deleted preference, server known, unsynced | |
| 2838 // 4. Server deleted preference, locally known. | |
| 2839 // 5. Server created preference, locally unknown, unapplied. | |
| 2840 // 6. A fully synced bookmark (no unique_client_tag). | |
| 2841 UserShare* share = sync_manager_.GetUserShare(); | |
| 2842 sync_pb::EntitySpecifics pref_specifics; | |
| 2843 AddDefaultFieldValue(PREFERENCES, &pref_specifics); | |
| 2844 sync_pb::EntitySpecifics bm_specifics; | |
| 2845 AddDefaultFieldValue(BOOKMARKS, &bm_specifics); | |
| 2846 int pref1_meta = | |
| 2847 MakeServerNode(share, PREFERENCES, "pref1", "hash1", pref_specifics); | |
| 2848 int64_t pref2_meta = MakeNodeWithRoot(share, PREFERENCES, "pref2"); | |
| 2849 int pref3_meta = | |
| 2850 MakeServerNode(share, PREFERENCES, "pref3", "hash3", pref_specifics); | |
| 2851 int pref4_meta = | |
| 2852 MakeServerNode(share, PREFERENCES, "pref4", "hash4", pref_specifics); | |
| 2853 int pref5_meta = | |
| 2854 MakeServerNode(share, PREFERENCES, "pref5", "hash5", pref_specifics); | |
| 2855 int bookmark_meta = | |
| 2856 MakeServerNode(share, BOOKMARKS, "bookmark", "", bm_specifics); | |
| 2857 | |
| 2858 { | |
| 2859 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 2860 share->directory.get()); | |
| 2861 // Pref's 1 and 2 are already set up properly. | |
| 2862 // Locally delete pref 3. | |
| 2863 syncable::MutableEntry pref3(&trans, GET_BY_HANDLE, pref3_meta); | |
| 2864 pref3.PutIsDel(true); | |
| 2865 pref3.PutIsUnsynced(true); | |
| 2866 // Delete pref 4 at the server. | |
| 2867 syncable::MutableEntry pref4(&trans, GET_BY_HANDLE, pref4_meta); | |
| 2868 pref4.PutServerIsDel(true); | |
| 2869 pref4.PutIsUnappliedUpdate(true); | |
| 2870 pref4.PutServerVersion(2); | |
| 2871 // Pref 5 is an new unapplied update. | |
| 2872 syncable::MutableEntry pref5(&trans, GET_BY_HANDLE, pref5_meta); | |
| 2873 pref5.PutIsUnappliedUpdate(true); | |
| 2874 pref5.PutIsDel(true); | |
| 2875 pref5.PutBaseVersion(-1); | |
| 2876 // Bookmark is already set up properly | |
| 2877 } | |
| 2878 | |
| 2879 // Take a snapshot to clear all the dirty bits. | |
| 2880 share->directory.get()->SaveChanges(); | |
| 2881 | |
| 2882 // Now request a purge for the unapplied types. | |
| 2883 disabled_types.PutAll(unapplied_types); | |
| 2884 sync_manager_.PurgeDisabledTypes(disabled_types, ModelTypeSet(), | |
| 2885 unapplied_types); | |
| 2886 | |
| 2887 // Verify the unapplied types still have progress markers and initial sync | |
| 2888 // ended after cleanup. | |
| 2889 EXPECT_TRUE( | |
| 2890 sync_manager_.GetUserShare()->directory->InitialSyncEndedTypes().HasAll( | |
| 2891 unapplied_types)); | |
| 2892 EXPECT_TRUE( | |
| 2893 sync_manager_.GetTypesWithEmptyProgressMarkerToken(unapplied_types) | |
| 2894 .Empty()); | |
| 2895 | |
| 2896 // Ensure the items were unapplied as necessary. | |
| 2897 { | |
| 2898 syncable::ReadTransaction trans(FROM_HERE, share->directory.get()); | |
| 2899 syncable::Entry pref_node(&trans, GET_BY_HANDLE, pref1_meta); | |
| 2900 ASSERT_TRUE(pref_node.good()); | |
| 2901 EXPECT_TRUE(pref_node.GetKernelCopy().is_dirty()); | |
| 2902 EXPECT_FALSE(pref_node.GetIsUnsynced()); | |
| 2903 EXPECT_TRUE(pref_node.GetIsUnappliedUpdate()); | |
| 2904 EXPECT_TRUE(pref_node.GetIsDel()); | |
| 2905 EXPECT_GT(pref_node.GetServerVersion(), 0); | |
| 2906 EXPECT_EQ(pref_node.GetBaseVersion(), -1); | |
| 2907 | |
| 2908 // Pref 2 should just be locally deleted. | |
| 2909 syncable::Entry pref2_node(&trans, GET_BY_HANDLE, pref2_meta); | |
| 2910 ASSERT_TRUE(pref2_node.good()); | |
| 2911 EXPECT_TRUE(pref2_node.GetKernelCopy().is_dirty()); | |
| 2912 EXPECT_FALSE(pref2_node.GetIsUnsynced()); | |
| 2913 EXPECT_TRUE(pref2_node.GetIsDel()); | |
| 2914 EXPECT_FALSE(pref2_node.GetIsUnappliedUpdate()); | |
| 2915 EXPECT_TRUE(pref2_node.GetIsDel()); | |
| 2916 EXPECT_EQ(pref2_node.GetServerVersion(), 0); | |
| 2917 EXPECT_EQ(pref2_node.GetBaseVersion(), -1); | |
| 2918 | |
| 2919 syncable::Entry pref3_node(&trans, GET_BY_HANDLE, pref3_meta); | |
| 2920 ASSERT_TRUE(pref3_node.good()); | |
| 2921 EXPECT_TRUE(pref3_node.GetKernelCopy().is_dirty()); | |
| 2922 EXPECT_FALSE(pref3_node.GetIsUnsynced()); | |
| 2923 EXPECT_TRUE(pref3_node.GetIsUnappliedUpdate()); | |
| 2924 EXPECT_TRUE(pref3_node.GetIsDel()); | |
| 2925 EXPECT_GT(pref3_node.GetServerVersion(), 0); | |
| 2926 EXPECT_EQ(pref3_node.GetBaseVersion(), -1); | |
| 2927 | |
| 2928 syncable::Entry pref4_node(&trans, GET_BY_HANDLE, pref4_meta); | |
| 2929 ASSERT_TRUE(pref4_node.good()); | |
| 2930 EXPECT_TRUE(pref4_node.GetKernelCopy().is_dirty()); | |
| 2931 EXPECT_FALSE(pref4_node.GetIsUnsynced()); | |
| 2932 EXPECT_TRUE(pref4_node.GetIsUnappliedUpdate()); | |
| 2933 EXPECT_TRUE(pref4_node.GetIsDel()); | |
| 2934 EXPECT_GT(pref4_node.GetServerVersion(), 0); | |
| 2935 EXPECT_EQ(pref4_node.GetBaseVersion(), -1); | |
| 2936 | |
| 2937 // Pref 5 should remain untouched. | |
| 2938 syncable::Entry pref5_node(&trans, GET_BY_HANDLE, pref5_meta); | |
| 2939 ASSERT_TRUE(pref5_node.good()); | |
| 2940 EXPECT_FALSE(pref5_node.GetKernelCopy().is_dirty()); | |
| 2941 EXPECT_FALSE(pref5_node.GetIsUnsynced()); | |
| 2942 EXPECT_TRUE(pref5_node.GetIsUnappliedUpdate()); | |
| 2943 EXPECT_TRUE(pref5_node.GetIsDel()); | |
| 2944 EXPECT_GT(pref5_node.GetServerVersion(), 0); | |
| 2945 EXPECT_EQ(pref5_node.GetBaseVersion(), -1); | |
| 2946 | |
| 2947 syncable::Entry bookmark_node(&trans, GET_BY_HANDLE, bookmark_meta); | |
| 2948 ASSERT_TRUE(bookmark_node.good()); | |
| 2949 EXPECT_TRUE(bookmark_node.GetKernelCopy().is_dirty()); | |
| 2950 EXPECT_FALSE(bookmark_node.GetIsUnsynced()); | |
| 2951 EXPECT_TRUE(bookmark_node.GetIsUnappliedUpdate()); | |
| 2952 EXPECT_TRUE(bookmark_node.GetIsDel()); | |
| 2953 EXPECT_GT(bookmark_node.GetServerVersion(), 0); | |
| 2954 EXPECT_EQ(bookmark_node.GetBaseVersion(), -1); | |
| 2955 } | |
| 2956 } | |
| 2957 | |
| 2958 // A test harness to exercise the code that processes and passes changes from | |
| 2959 // the "SYNCER"-WriteTransaction destructor, through the SyncManager, to the | |
| 2960 // ChangeProcessor. | |
| 2961 class SyncManagerChangeProcessingTest : public SyncManagerTest { | |
| 2962 public: | |
| 2963 void OnChangesApplied(ModelType model_type, | |
| 2964 int64_t model_version, | |
| 2965 const BaseTransaction* trans, | |
| 2966 const ImmutableChangeRecordList& changes) override { | |
| 2967 last_changes_ = changes; | |
| 2968 } | |
| 2969 | |
| 2970 void OnChangesComplete(ModelType model_type) override {} | |
| 2971 | |
| 2972 const ImmutableChangeRecordList& GetRecentChangeList() { | |
| 2973 return last_changes_; | |
| 2974 } | |
| 2975 | |
| 2976 UserShare* share() { return sync_manager_.GetUserShare(); } | |
| 2977 | |
| 2978 // Set some flags so our nodes reasonably approximate the real world scenario | |
| 2979 // and can get past CheckTreeInvariants. | |
| 2980 // | |
| 2981 // It's never going to be truly accurate, since we're squashing update | |
| 2982 // receipt, processing and application into a single transaction. | |
| 2983 void SetNodeProperties(syncable::MutableEntry* entry) { | |
| 2984 entry->PutId(id_factory_.NewServerId()); | |
| 2985 entry->PutBaseVersion(10); | |
| 2986 entry->PutServerVersion(10); | |
| 2987 } | |
| 2988 | |
| 2989 // Looks for the given change in the list. Returns the index at which it was | |
| 2990 // found. Returns -1 on lookup failure. | |
| 2991 size_t FindChangeInList(int64_t id, ChangeRecord::Action action) { | |
| 2992 SCOPED_TRACE(id); | |
| 2993 for (size_t i = 0; i < last_changes_.Get().size(); ++i) { | |
| 2994 if (last_changes_.Get()[i].id == id && | |
| 2995 last_changes_.Get()[i].action == action) { | |
| 2996 return i; | |
| 2997 } | |
| 2998 } | |
| 2999 ADD_FAILURE() << "Failed to find specified change"; | |
| 3000 return static_cast<size_t>(-1); | |
| 3001 } | |
| 3002 | |
| 3003 // Returns the current size of the change list. | |
| 3004 // | |
| 3005 // Note that spurious changes do not necessarily indicate a problem. | |
| 3006 // Assertions on change list size can help detect problems, but it may be | |
| 3007 // necessary to reduce their strictness if the implementation changes. | |
| 3008 size_t GetChangeListSize() { return last_changes_.Get().size(); } | |
| 3009 | |
| 3010 void ClearChangeList() { last_changes_ = ImmutableChangeRecordList(); } | |
| 3011 | |
| 3012 protected: | |
| 3013 ImmutableChangeRecordList last_changes_; | |
| 3014 TestIdFactory id_factory_; | |
| 3015 }; | |
| 3016 | |
| 3017 // Test creation of a folder and a bookmark. | |
| 3018 TEST_F(SyncManagerChangeProcessingTest, AddBookmarks) { | |
| 3019 int64_t type_root = GetIdForDataType(BOOKMARKS); | |
| 3020 int64_t folder_id = kInvalidId; | |
| 3021 int64_t child_id = kInvalidId; | |
| 3022 | |
| 3023 // Create a folder and a bookmark under it. | |
| 3024 { | |
| 3025 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 3026 share()->directory.get()); | |
| 3027 syncable::Entry root(&trans, syncable::GET_BY_HANDLE, type_root); | |
| 3028 ASSERT_TRUE(root.good()); | |
| 3029 | |
| 3030 syncable::MutableEntry folder(&trans, syncable::CREATE, BOOKMARKS, | |
| 3031 root.GetId(), "folder"); | |
| 3032 ASSERT_TRUE(folder.good()); | |
| 3033 SetNodeProperties(&folder); | |
| 3034 folder.PutIsDir(true); | |
| 3035 folder_id = folder.GetMetahandle(); | |
| 3036 | |
| 3037 syncable::MutableEntry child(&trans, syncable::CREATE, BOOKMARKS, | |
| 3038 folder.GetId(), "child"); | |
| 3039 ASSERT_TRUE(child.good()); | |
| 3040 SetNodeProperties(&child); | |
| 3041 child_id = child.GetMetahandle(); | |
| 3042 } | |
| 3043 | |
| 3044 // The closing of the above scope will delete the transaction. Its processed | |
| 3045 // changes should be waiting for us in a member of the test harness. | |
| 3046 EXPECT_EQ(2UL, GetChangeListSize()); | |
| 3047 | |
| 3048 // We don't need to check these return values here. The function will add a | |
| 3049 // non-fatal failure if these changes are not found. | |
| 3050 size_t folder_change_pos = | |
| 3051 FindChangeInList(folder_id, ChangeRecord::ACTION_ADD); | |
| 3052 size_t child_change_pos = | |
| 3053 FindChangeInList(child_id, ChangeRecord::ACTION_ADD); | |
| 3054 | |
| 3055 // Parents are delivered before children. | |
| 3056 EXPECT_LT(folder_change_pos, child_change_pos); | |
| 3057 } | |
| 3058 | |
| 3059 // Test creation of a preferences (with implicit parent Id) | |
| 3060 TEST_F(SyncManagerChangeProcessingTest, AddPreferences) { | |
| 3061 int64_t item1_id = kInvalidId; | |
| 3062 int64_t item2_id = kInvalidId; | |
| 3063 | |
| 3064 // Create two preferences. | |
| 3065 { | |
| 3066 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 3067 share()->directory.get()); | |
| 3068 | |
| 3069 syncable::MutableEntry item1(&trans, syncable::CREATE, PREFERENCES, | |
| 3070 "test_item_1"); | |
| 3071 ASSERT_TRUE(item1.good()); | |
| 3072 SetNodeProperties(&item1); | |
| 3073 item1_id = item1.GetMetahandle(); | |
| 3074 | |
| 3075 // Need at least two items to ensure hitting all possible codepaths in | |
| 3076 // ChangeReorderBuffer::Traversal::ExpandToInclude. | |
| 3077 syncable::MutableEntry item2(&trans, syncable::CREATE, PREFERENCES, | |
| 3078 "test_item_2"); | |
| 3079 ASSERT_TRUE(item2.good()); | |
| 3080 SetNodeProperties(&item2); | |
| 3081 item2_id = item2.GetMetahandle(); | |
| 3082 } | |
| 3083 | |
| 3084 // The closing of the above scope will delete the transaction. Its processed | |
| 3085 // changes should be waiting for us in a member of the test harness. | |
| 3086 EXPECT_EQ(2UL, GetChangeListSize()); | |
| 3087 | |
| 3088 FindChangeInList(item1_id, ChangeRecord::ACTION_ADD); | |
| 3089 FindChangeInList(item2_id, ChangeRecord::ACTION_ADD); | |
| 3090 } | |
| 3091 | |
| 3092 // Test moving a bookmark into an empty folder. | |
| 3093 TEST_F(SyncManagerChangeProcessingTest, MoveBookmarkIntoEmptyFolder) { | |
| 3094 int64_t type_root = GetIdForDataType(BOOKMARKS); | |
| 3095 int64_t folder_b_id = kInvalidId; | |
| 3096 int64_t child_id = kInvalidId; | |
| 3097 | |
| 3098 // Create two folders. Place a child under folder A. | |
| 3099 { | |
| 3100 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 3101 share()->directory.get()); | |
| 3102 syncable::Entry root(&trans, syncable::GET_BY_HANDLE, type_root); | |
| 3103 ASSERT_TRUE(root.good()); | |
| 3104 | |
| 3105 syncable::MutableEntry folder_a(&trans, syncable::CREATE, BOOKMARKS, | |
| 3106 root.GetId(), "folderA"); | |
| 3107 ASSERT_TRUE(folder_a.good()); | |
| 3108 SetNodeProperties(&folder_a); | |
| 3109 folder_a.PutIsDir(true); | |
| 3110 | |
| 3111 syncable::MutableEntry folder_b(&trans, syncable::CREATE, BOOKMARKS, | |
| 3112 root.GetId(), "folderB"); | |
| 3113 ASSERT_TRUE(folder_b.good()); | |
| 3114 SetNodeProperties(&folder_b); | |
| 3115 folder_b.PutIsDir(true); | |
| 3116 folder_b_id = folder_b.GetMetahandle(); | |
| 3117 | |
| 3118 syncable::MutableEntry child(&trans, syncable::CREATE, BOOKMARKS, | |
| 3119 folder_a.GetId(), "child"); | |
| 3120 ASSERT_TRUE(child.good()); | |
| 3121 SetNodeProperties(&child); | |
| 3122 child_id = child.GetMetahandle(); | |
| 3123 } | |
| 3124 | |
| 3125 // Close that transaction. The above was to setup the initial scenario. The | |
| 3126 // real test starts now. | |
| 3127 | |
| 3128 // Move the child from folder A to folder B. | |
| 3129 { | |
| 3130 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 3131 share()->directory.get()); | |
| 3132 | |
| 3133 syncable::Entry folder_b(&trans, syncable::GET_BY_HANDLE, folder_b_id); | |
| 3134 syncable::MutableEntry child(&trans, syncable::GET_BY_HANDLE, child_id); | |
| 3135 | |
| 3136 child.PutParentId(folder_b.GetId()); | |
| 3137 } | |
| 3138 | |
| 3139 EXPECT_EQ(1UL, GetChangeListSize()); | |
| 3140 | |
| 3141 // Verify that this was detected as a real change. An early version of the | |
| 3142 // UniquePosition code had a bug where moves from one folder to another were | |
| 3143 // ignored unless the moved node's UniquePosition value was also changed in | |
| 3144 // some way. | |
| 3145 FindChangeInList(child_id, ChangeRecord::ACTION_UPDATE); | |
| 3146 } | |
| 3147 | |
| 3148 // Test moving a bookmark into a non-empty folder. | |
| 3149 TEST_F(SyncManagerChangeProcessingTest, MoveIntoPopulatedFolder) { | |
| 3150 int64_t type_root = GetIdForDataType(BOOKMARKS); | |
| 3151 int64_t child_a_id = kInvalidId; | |
| 3152 int64_t child_b_id = kInvalidId; | |
| 3153 | |
| 3154 // Create two folders. Place one child each under folder A and folder B. | |
| 3155 { | |
| 3156 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 3157 share()->directory.get()); | |
| 3158 syncable::Entry root(&trans, syncable::GET_BY_HANDLE, type_root); | |
| 3159 ASSERT_TRUE(root.good()); | |
| 3160 | |
| 3161 syncable::MutableEntry folder_a(&trans, syncable::CREATE, BOOKMARKS, | |
| 3162 root.GetId(), "folderA"); | |
| 3163 ASSERT_TRUE(folder_a.good()); | |
| 3164 SetNodeProperties(&folder_a); | |
| 3165 folder_a.PutIsDir(true); | |
| 3166 | |
| 3167 syncable::MutableEntry folder_b(&trans, syncable::CREATE, BOOKMARKS, | |
| 3168 root.GetId(), "folderB"); | |
| 3169 ASSERT_TRUE(folder_b.good()); | |
| 3170 SetNodeProperties(&folder_b); | |
| 3171 folder_b.PutIsDir(true); | |
| 3172 | |
| 3173 syncable::MutableEntry child_a(&trans, syncable::CREATE, BOOKMARKS, | |
| 3174 folder_a.GetId(), "childA"); | |
| 3175 ASSERT_TRUE(child_a.good()); | |
| 3176 SetNodeProperties(&child_a); | |
| 3177 child_a_id = child_a.GetMetahandle(); | |
| 3178 | |
| 3179 syncable::MutableEntry child_b(&trans, syncable::CREATE, BOOKMARKS, | |
| 3180 folder_b.GetId(), "childB"); | |
| 3181 SetNodeProperties(&child_b); | |
| 3182 child_b_id = child_b.GetMetahandle(); | |
| 3183 } | |
| 3184 | |
| 3185 // Close that transaction. The above was to setup the initial scenario. The | |
| 3186 // real test starts now. | |
| 3187 | |
| 3188 { | |
| 3189 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 3190 share()->directory.get()); | |
| 3191 | |
| 3192 syncable::MutableEntry child_a(&trans, syncable::GET_BY_HANDLE, child_a_id); | |
| 3193 syncable::MutableEntry child_b(&trans, syncable::GET_BY_HANDLE, child_b_id); | |
| 3194 | |
| 3195 // Move child A from folder A to folder B and update its position. | |
| 3196 child_a.PutParentId(child_b.GetParentId()); | |
| 3197 child_a.PutPredecessor(child_b.GetId()); | |
| 3198 } | |
| 3199 | |
| 3200 EXPECT_EQ(1UL, GetChangeListSize()); | |
| 3201 | |
| 3202 // Verify that only child a is in the change list. | |
| 3203 // (This function will add a failure if the lookup fails.) | |
| 3204 FindChangeInList(child_a_id, ChangeRecord::ACTION_UPDATE); | |
| 3205 } | |
| 3206 | |
| 3207 // Tests the ordering of deletion changes. | |
| 3208 TEST_F(SyncManagerChangeProcessingTest, DeletionsAndChanges) { | |
| 3209 int64_t type_root = GetIdForDataType(BOOKMARKS); | |
| 3210 int64_t folder_a_id = kInvalidId; | |
| 3211 int64_t folder_b_id = kInvalidId; | |
| 3212 int64_t child_id = kInvalidId; | |
| 3213 | |
| 3214 // Create two folders. Place a child under folder A. | |
| 3215 { | |
| 3216 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 3217 share()->directory.get()); | |
| 3218 syncable::Entry root(&trans, syncable::GET_BY_HANDLE, type_root); | |
| 3219 ASSERT_TRUE(root.good()); | |
| 3220 | |
| 3221 syncable::MutableEntry folder_a(&trans, syncable::CREATE, BOOKMARKS, | |
| 3222 root.GetId(), "folderA"); | |
| 3223 ASSERT_TRUE(folder_a.good()); | |
| 3224 SetNodeProperties(&folder_a); | |
| 3225 folder_a.PutIsDir(true); | |
| 3226 folder_a_id = folder_a.GetMetahandle(); | |
| 3227 | |
| 3228 syncable::MutableEntry folder_b(&trans, syncable::CREATE, BOOKMARKS, | |
| 3229 root.GetId(), "folderB"); | |
| 3230 ASSERT_TRUE(folder_b.good()); | |
| 3231 SetNodeProperties(&folder_b); | |
| 3232 folder_b.PutIsDir(true); | |
| 3233 folder_b_id = folder_b.GetMetahandle(); | |
| 3234 | |
| 3235 syncable::MutableEntry child(&trans, syncable::CREATE, BOOKMARKS, | |
| 3236 folder_a.GetId(), "child"); | |
| 3237 ASSERT_TRUE(child.good()); | |
| 3238 SetNodeProperties(&child); | |
| 3239 child_id = child.GetMetahandle(); | |
| 3240 } | |
| 3241 | |
| 3242 // Close that transaction. The above was to setup the initial scenario. The | |
| 3243 // real test starts now. | |
| 3244 | |
| 3245 { | |
| 3246 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 3247 share()->directory.get()); | |
| 3248 | |
| 3249 syncable::MutableEntry folder_a(&trans, syncable::GET_BY_HANDLE, | |
| 3250 folder_a_id); | |
| 3251 syncable::MutableEntry folder_b(&trans, syncable::GET_BY_HANDLE, | |
| 3252 folder_b_id); | |
| 3253 syncable::MutableEntry child(&trans, syncable::GET_BY_HANDLE, child_id); | |
| 3254 | |
| 3255 // Delete folder B and its child. | |
| 3256 child.PutIsDel(true); | |
| 3257 folder_b.PutIsDel(true); | |
| 3258 | |
| 3259 // Make an unrelated change to folder A. | |
| 3260 folder_a.PutNonUniqueName("NewNameA"); | |
| 3261 } | |
| 3262 | |
| 3263 EXPECT_EQ(3UL, GetChangeListSize()); | |
| 3264 | |
| 3265 size_t folder_a_pos = | |
| 3266 FindChangeInList(folder_a_id, ChangeRecord::ACTION_UPDATE); | |
| 3267 size_t folder_b_pos = | |
| 3268 FindChangeInList(folder_b_id, ChangeRecord::ACTION_DELETE); | |
| 3269 size_t child_pos = FindChangeInList(child_id, ChangeRecord::ACTION_DELETE); | |
| 3270 | |
| 3271 // Deletes should appear before updates. | |
| 3272 EXPECT_LT(child_pos, folder_a_pos); | |
| 3273 EXPECT_LT(folder_b_pos, folder_a_pos); | |
| 3274 } | |
| 3275 | |
| 3276 // See that attachment metadata changes are not filtered out by | |
| 3277 // SyncManagerImpl::VisiblePropertiesDiffer. | |
| 3278 TEST_F(SyncManagerChangeProcessingTest, AttachmentMetadataOnlyChanges) { | |
| 3279 // Create an article with no attachments. See that a change is generated. | |
| 3280 int64_t article_id = kInvalidId; | |
| 3281 { | |
| 3282 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 3283 share()->directory.get()); | |
| 3284 int64_t type_root = GetIdForDataType(ARTICLES); | |
| 3285 syncable::Entry root(&trans, syncable::GET_BY_HANDLE, type_root); | |
| 3286 ASSERT_TRUE(root.good()); | |
| 3287 syncable::MutableEntry article(&trans, syncable::CREATE, ARTICLES, | |
| 3288 root.GetId(), "article"); | |
| 3289 ASSERT_TRUE(article.good()); | |
| 3290 SetNodeProperties(&article); | |
| 3291 article_id = article.GetMetahandle(); | |
| 3292 } | |
| 3293 ASSERT_EQ(1UL, GetChangeListSize()); | |
| 3294 FindChangeInList(article_id, ChangeRecord::ACTION_ADD); | |
| 3295 ClearChangeList(); | |
| 3296 | |
| 3297 // Modify the article by adding one attachment. Don't touch anything else. | |
| 3298 // See that a change is generated. | |
| 3299 { | |
| 3300 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 3301 share()->directory.get()); | |
| 3302 syncable::MutableEntry article(&trans, syncable::GET_BY_HANDLE, article_id); | |
| 3303 sync_pb::AttachmentMetadata metadata; | |
| 3304 *metadata.add_record()->mutable_id() = CreateAttachmentIdProto(0, 0); | |
| 3305 article.PutAttachmentMetadata(metadata); | |
| 3306 } | |
| 3307 ASSERT_EQ(1UL, GetChangeListSize()); | |
| 3308 FindChangeInList(article_id, ChangeRecord::ACTION_UPDATE); | |
| 3309 ClearChangeList(); | |
| 3310 | |
| 3311 // Modify the article by replacing its attachment with a different one. See | |
| 3312 // that a change is generated. | |
| 3313 { | |
| 3314 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 3315 share()->directory.get()); | |
| 3316 syncable::MutableEntry article(&trans, syncable::GET_BY_HANDLE, article_id); | |
| 3317 sync_pb::AttachmentMetadata metadata = article.GetAttachmentMetadata(); | |
| 3318 *metadata.add_record()->mutable_id() = CreateAttachmentIdProto(0, 0); | |
| 3319 article.PutAttachmentMetadata(metadata); | |
| 3320 } | |
| 3321 ASSERT_EQ(1UL, GetChangeListSize()); | |
| 3322 FindChangeInList(article_id, ChangeRecord::ACTION_UPDATE); | |
| 3323 ClearChangeList(); | |
| 3324 | |
| 3325 // Modify the article by replacing its attachment metadata with the same | |
| 3326 // attachment metadata. No change should be generated. | |
| 3327 { | |
| 3328 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, | |
| 3329 share()->directory.get()); | |
| 3330 syncable::MutableEntry article(&trans, syncable::GET_BY_HANDLE, article_id); | |
| 3331 article.PutAttachmentMetadata(article.GetAttachmentMetadata()); | |
| 3332 } | |
| 3333 ASSERT_EQ(0UL, GetChangeListSize()); | |
| 3334 } | |
| 3335 | |
| 3336 // During initialization SyncManagerImpl loads sqlite database. If it fails to | |
| 3337 // do so it should fail initialization. This test verifies this behavior. | |
| 3338 // Test reuses SyncManagerImpl initialization from SyncManagerTest but overrides | |
| 3339 // InternalComponentsFactory to return DirectoryBackingStore that always fails | |
| 3340 // to load. | |
| 3341 class SyncManagerInitInvalidStorageTest : public SyncManagerTest { | |
| 3342 public: | |
| 3343 SyncManagerInitInvalidStorageTest() {} | |
| 3344 | |
| 3345 InternalComponentsFactory* GetFactory() override { | |
| 3346 return new TestInternalComponentsFactory( | |
| 3347 GetSwitches(), InternalComponentsFactory::STORAGE_INVALID, | |
| 3348 &storage_used_); | |
| 3349 } | |
| 3350 }; | |
| 3351 | |
| 3352 // SyncManagerInitInvalidStorageTest::GetFactory will return | |
| 3353 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails. | |
| 3354 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's | |
| 3355 // task is to ensure that SyncManagerImpl reported initialization failure in | |
| 3356 // OnInitializationComplete callback. | |
| 3357 TEST_F(SyncManagerInitInvalidStorageTest, FailToOpenDatabase) { | |
| 3358 EXPECT_FALSE(initialization_succeeded_); | |
| 3359 } | |
| 3360 | |
| 3361 } // namespace syncer | |
| OLD | NEW |