Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: sync/internal_api/sync_manager_impl_unittest.cc

Issue 11817010: sync: Initialize entries with a valid model type (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update comment Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Unit tests for the SyncApi. Note that a lot of the underlying 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 6 // functionality is provided by the Syncable layer, which has its own
7 // unit tests. We'll test SyncApi specific things in this harness. 7 // unit tests. We'll test SyncApi specific things in this harness.
8 8
9 #include <cstddef> 9 #include <cstddef>
10 #include <map> 10 #include <map>
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 ReadNode root_node(&trans); 123 ReadNode root_node(&trans);
124 root_node.InitByRootLookup(); 124 root_node.InitByRootLookup();
125 WriteNode node(&trans); 125 WriteNode node(&trans);
126 WriteNode::InitUniqueByCreationResult result = 126 WriteNode::InitUniqueByCreationResult result =
127 node.InitUniqueByCreation(model_type, root_node, client_tag); 127 node.InitUniqueByCreation(model_type, root_node, client_tag);
128 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); 128 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
129 node.SetIsFolder(false); 129 node.SetIsFolder(false);
130 return node.GetId(); 130 return node.GetId();
131 } 131 }
132 132
133 // Makes a non-folder child of a non-root node. Returns the id of the
134 // newly-created node.
135 int64 MakeNodeWithParent(UserShare* share,
136 ModelType model_type,
137 const std::string& client_tag,
138 int64 parent_id) {
139 WriteTransaction trans(FROM_HERE, share);
140 ReadNode parent_node(&trans);
141 EXPECT_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id));
142 WriteNode node(&trans);
143 WriteNode::InitUniqueByCreationResult result =
144 node.InitUniqueByCreation(model_type, parent_node, client_tag);
145 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
146 node.SetIsFolder(false);
147 return node.GetId();
148 }
149
150 // Makes a folder child of a non-root node. Returns the id of the 133 // Makes a folder child of a non-root node. Returns the id of the
151 // newly-created node. 134 // newly-created node.
152 int64 MakeFolderWithParent(UserShare* share, 135 int64 MakeFolderWithParent(UserShare* share,
153 ModelType model_type, 136 ModelType model_type,
154 int64 parent_id, 137 int64 parent_id,
155 BaseNode* predecessor) { 138 BaseNode* predecessor) {
156 WriteTransaction trans(FROM_HERE, share); 139 WriteTransaction trans(FROM_HERE, share);
157 ReadNode parent_node(&trans); 140 ReadNode parent_node(&trans);
158 EXPECT_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id)); 141 DCHECK_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id));
159 WriteNode node(&trans); 142 WriteNode node(&trans);
160 EXPECT_TRUE(node.InitByCreation(model_type, parent_node, predecessor)); 143 DCHECK(node.InitBookmarkByCreation(parent_node, predecessor));
161 node.SetIsFolder(true); 144 node.SetIsFolder(true);
162 return node.GetId(); 145 return node.GetId();
163 } 146 }
164 147
148 int64 MakeBookmarkWithParent(UserShare* share,
149 int64 parent_id,
150 BaseNode* predecessor) {
151 WriteTransaction trans(FROM_HERE, share);
152 ReadNode parent_node(&trans);
153 DCHECK_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id));
154 WriteNode node(&trans);
155 DCHECK(node.InitBookmarkByCreation(parent_node, predecessor));
156 return node.GetId();
157 }
158
165 // Creates the "synced" root node for a particular datatype. We use the syncable 159 // Creates the "synced" root node for a particular datatype. We use the syncable
166 // methods here so that the syncer treats these nodes as if they were already 160 // methods here so that the syncer treats these nodes as if they were already
167 // received from the server. 161 // received from the server.
168 int64 MakeServerNodeForType(UserShare* share, 162 int64 MakeServerNodeForType(UserShare* share,
169 ModelType model_type) { 163 ModelType model_type) {
170 sync_pb::EntitySpecifics specifics; 164 sync_pb::EntitySpecifics specifics;
171 AddDefaultFieldValue(model_type, &specifics); 165 AddDefaultFieldValue(model_type, &specifics);
172 syncable::WriteTransaction trans( 166 syncable::WriteTransaction trans(
173 FROM_HERE, syncable::UNITTEST, share->directory.get()); 167 FROM_HERE, syncable::UNITTEST, share->directory.get());
174 // Attempt to lookup by nigori tag. 168 // Attempt to lookup by nigori tag.
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 int64 folder_id; 337 int64 folder_id;
344 std::string test_title("test1"); 338 std::string test_title("test1");
345 339
346 { 340 {
347 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 341 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
348 ReadNode root_node(&trans); 342 ReadNode root_node(&trans);
349 root_node.InitByRootLookup(); 343 root_node.InitByRootLookup();
350 344
351 // we'll use this spare folder later 345 // we'll use this spare folder later
352 WriteNode folder_node(&trans); 346 WriteNode folder_node(&trans);
353 EXPECT_TRUE(folder_node.InitByCreation(BOOKMARKS, 347 EXPECT_TRUE(folder_node.InitBookmarkByCreation(root_node, NULL));
354 root_node, NULL));
355 folder_id = folder_node.GetId(); 348 folder_id = folder_node.GetId();
356 349
357 WriteNode wnode(&trans); 350 WriteNode wnode(&trans);
358 WriteNode::InitUniqueByCreationResult result = 351 WriteNode::InitUniqueByCreationResult result =
359 wnode.InitUniqueByCreation(BOOKMARKS, root_node, "testtag"); 352 wnode.InitUniqueByCreation(BOOKMARKS, root_node, "testtag");
360 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); 353 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
361 wnode.SetIsFolder(false); 354 wnode.SetIsFolder(false);
362 wnode.SetTitle(UTF8ToWide(test_title)); 355 wnode.SetTitle(UTF8ToWide(test_title));
363 356
364 node_id = wnode.GetId(); 357 node_id = wnode.GetId();
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 } 446 }
454 } 447 }
455 448
456 TEST_F(SyncApiTest, WriteEncryptedTitle) { 449 TEST_F(SyncApiTest, WriteEncryptedTitle) {
457 KeyParams params = {"localhost", "username", "passphrase"}; 450 KeyParams params = {"localhost", "username", "passphrase"};
458 { 451 {
459 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 452 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
460 trans.GetCryptographer()->AddKey(params); 453 trans.GetCryptographer()->AddKey(params);
461 } 454 }
462 test_user_share_.encryption_handler()->EnableEncryptEverything(); 455 test_user_share_.encryption_handler()->EnableEncryptEverything();
456 int bookmark_id;
463 { 457 {
464 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 458 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
465 ReadNode root_node(&trans); 459 ReadNode root_node(&trans);
466 root_node.InitByRootLookup(); 460 root_node.InitByRootLookup();
467 461
468 WriteNode bookmark_node(&trans); 462 WriteNode bookmark_node(&trans);
469 WriteNode::InitUniqueByCreationResult result = 463 ASSERT_TRUE(bookmark_node.InitBookmarkByCreation(root_node, NULL));
470 bookmark_node.InitUniqueByCreation(BOOKMARKS, 464 bookmark_id = bookmark_node.GetId();
471 root_node, "foo");
472 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
473 bookmark_node.SetTitle(UTF8ToWide("foo")); 465 bookmark_node.SetTitle(UTF8ToWide("foo"));
474 466
475 WriteNode pref_node(&trans); 467 WriteNode pref_node(&trans);
476 result = 468 WriteNode::InitUniqueByCreationResult result =
477 pref_node.InitUniqueByCreation(PREFERENCES, root_node, "bar"); 469 pref_node.InitUniqueByCreation(PREFERENCES, root_node, "bar");
478 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); 470 ASSERT_EQ(WriteNode::INIT_SUCCESS, result);
479 pref_node.SetTitle(UTF8ToWide("bar")); 471 pref_node.SetTitle(UTF8ToWide("bar"));
480 } 472 }
481 { 473 {
482 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 474 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
483 ReadNode root_node(&trans); 475 ReadNode root_node(&trans);
484 root_node.InitByRootLookup(); 476 root_node.InitByRootLookup();
485 477
486 ReadNode bookmark_node(&trans); 478 ReadNode bookmark_node(&trans);
487 EXPECT_EQ(BaseNode::INIT_OK, 479 ASSERT_EQ(BaseNode::INIT_OK, bookmark_node.InitByIdLookup(bookmark_id));
488 bookmark_node.InitByClientTagLookup(BOOKMARKS,
489 "foo"));
490 EXPECT_EQ("foo", bookmark_node.GetTitle()); 480 EXPECT_EQ("foo", bookmark_node.GetTitle());
491 EXPECT_EQ(kEncryptedString, 481 EXPECT_EQ(kEncryptedString,
492 bookmark_node.GetEntry()->Get(syncable::NON_UNIQUE_NAME)); 482 bookmark_node.GetEntry()->Get(syncable::NON_UNIQUE_NAME));
493 483
494 ReadNode pref_node(&trans); 484 ReadNode pref_node(&trans);
495 EXPECT_EQ(BaseNode::INIT_OK, 485 ASSERT_EQ(BaseNode::INIT_OK,
496 pref_node.InitByClientTagLookup(PREFERENCES, 486 pref_node.InitByClientTagLookup(PREFERENCES,
497 "bar")); 487 "bar"));
498 EXPECT_EQ(kEncryptedString, pref_node.GetTitle()); 488 EXPECT_EQ(kEncryptedString, pref_node.GetTitle());
499 } 489 }
500 } 490 }
501 491
502 TEST_F(SyncApiTest, BaseNodeSetSpecifics) { 492 TEST_F(SyncApiTest, BaseNodeSetSpecifics) {
503 int64 child_id = MakeNode(test_user_share_.user_share(), 493 int64 child_id = MakeNode(test_user_share_.user_share(),
504 BOOKMARKS, "testtag"); 494 BOOKMARKS, "testtag");
505 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 495 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
665 NULL); 655 NULL);
666 ignore_result(MakeFolderWithParent(test_user_share_.user_share(), 656 ignore_result(MakeFolderWithParent(test_user_share_.user_share(),
667 BOOKMARKS, 657 BOOKMARKS,
668 type_root, 658 type_root,
669 NULL)); 659 NULL));
670 int64 child1 = MakeFolderWithParent( 660 int64 child1 = MakeFolderWithParent(
671 test_user_share_.user_share(), 661 test_user_share_.user_share(),
672 BOOKMARKS, 662 BOOKMARKS,
673 parent, 663 parent,
674 NULL); 664 NULL);
675 ignore_result(MakeNodeWithParent( 665 ignore_result(MakeBookmarkWithParent(
676 test_user_share_.user_share(), 666 test_user_share_.user_share(),
677 BOOKMARKS, 667 parent,
678 "c2", 668 NULL));
679 parent)); 669 ignore_result(MakeBookmarkWithParent(
680 ignore_result(MakeNodeWithParent(
681 test_user_share_.user_share(), 670 test_user_share_.user_share(),
682 BOOKMARKS, 671 child1,
683 "c1c1", 672 NULL));
684 child1));
685 673
686 { 674 {
687 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 675 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
688 ReadNode type_root_node(&trans); 676 ReadNode type_root_node(&trans);
689 EXPECT_EQ(BaseNode::INIT_OK, 677 EXPECT_EQ(BaseNode::INIT_OK,
690 type_root_node.InitByIdLookup(type_root)); 678 type_root_node.InitByIdLookup(type_root));
691 EXPECT_EQ(6, type_root_node.GetTotalNodeCount()); 679 EXPECT_EQ(6, type_root_node.GetTotalNodeCount());
692 ReadNode node(&trans); 680 ReadNode node(&trans);
693 EXPECT_EQ(BaseNode::INIT_OK, 681 EXPECT_EQ(BaseNode::INIT_OK,
694 node.InitByIdLookup(parent)); 682 node.InitByIdLookup(parent));
(...skipping 837 matching lines...) Expand 10 before | Expand all | Expand 10 after
1532 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 1520 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1533 1521
1534 // Create some unencrypted unsynced data. 1522 // Create some unencrypted unsynced data.
1535 int64 folder = MakeFolderWithParent(sync_manager_.GetUserShare(), 1523 int64 folder = MakeFolderWithParent(sync_manager_.GetUserShare(),
1536 BOOKMARKS, 1524 BOOKMARKS,
1537 GetIdForDataType(BOOKMARKS), 1525 GetIdForDataType(BOOKMARKS),
1538 NULL); 1526 NULL);
1539 // First batch_size nodes are children of folder. 1527 // First batch_size nodes are children of folder.
1540 size_t i; 1528 size_t i;
1541 for (i = 0; i < batch_size; ++i) { 1529 for (i = 0; i < batch_size; ++i) {
1542 MakeNodeWithParent(sync_manager_.GetUserShare(), BOOKMARKS, 1530 MakeBookmarkWithParent(sync_manager_.GetUserShare(), folder, NULL);
1543 base::StringPrintf("%"PRIuS"", i), folder);
1544 } 1531 }
1545 // Next batch_size nodes are a different type and on their own. 1532 // Next batch_size nodes are a different type and on their own.
1546 for (; i < 2*batch_size; ++i) { 1533 for (; i < 2*batch_size; ++i) {
1547 MakeNodeWithParent(sync_manager_.GetUserShare(), SESSIONS, 1534 MakeNode(sync_manager_.GetUserShare(), SESSIONS,
1548 base::StringPrintf("%"PRIuS"", i), 1535 base::StringPrintf("%"PRIuS"", i));
1549 GetIdForDataType(SESSIONS));
1550 } 1536 }
1551 // Last batch_size nodes are a third type that will not need encryption. 1537 // Last batch_size nodes are a third type that will not need encryption.
1552 for (; i < 3*batch_size; ++i) { 1538 for (; i < 3*batch_size; ++i) {
1553 MakeNodeWithParent(sync_manager_.GetUserShare(), THEMES, 1539 MakeNode(sync_manager_.GetUserShare(), THEMES,
1554 base::StringPrintf("%"PRIuS"", i), 1540 base::StringPrintf("%"PRIuS"", i));
1555 GetIdForDataType(THEMES));
1556 } 1541 }
1557 1542
1558 { 1543 {
1559 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1544 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1560 EXPECT_TRUE(GetEncryptedTypesWithTrans(&trans).Equals( 1545 EXPECT_TRUE(GetEncryptedTypesWithTrans(&trans).Equals(
1561 SyncEncryptionHandler::SensitiveTypes())); 1546 SyncEncryptionHandler::SensitiveTypes()));
1562 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1547 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1563 trans.GetWrappedTrans(), 1548 trans.GetWrappedTrans(),
1564 BOOKMARKS, 1549 BOOKMARKS,
1565 false /* not encrypted */)); 1550 false /* not encrypted */));
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after
2142 } 2127 }
2143 2128
2144 // Create a bookmark and set the title/url, then verify the data was properly 2129 // Create a bookmark and set the title/url, then verify the data was properly
2145 // set. This replicates the unique way bookmarks have of creating sync nodes. 2130 // set. This replicates the unique way bookmarks have of creating sync nodes.
2146 // See BookmarkChangeProcessor::PlaceSyncNode(..). 2131 // See BookmarkChangeProcessor::PlaceSyncNode(..).
2147 TEST_F(SyncManagerTest, CreateLocalBookmark) { 2132 TEST_F(SyncManagerTest, CreateLocalBookmark) {
2148 std::string title = "title"; 2133 std::string title = "title";
2149 std::string url = "url"; 2134 std::string url = "url";
2150 { 2135 {
2151 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2136 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2152 ReadNode root_node(&trans); 2137 ReadNode bookmark_root(&trans);
2153 root_node.InitByRootLookup(); 2138 ASSERT_EQ(BaseNode::INIT_OK,
2139 bookmark_root.InitByTagLookup(ModelTypeToRootTag(BOOKMARKS)));
2154 WriteNode node(&trans); 2140 WriteNode node(&trans);
2155 ASSERT_TRUE(node.InitByCreation(BOOKMARKS, root_node, NULL)); 2141 ASSERT_TRUE(node.InitBookmarkByCreation(bookmark_root, NULL));
2156 node.SetIsFolder(false); 2142 node.SetIsFolder(false);
2157 node.SetTitle(UTF8ToWide(title)); 2143 node.SetTitle(UTF8ToWide(title));
2158 2144
2159 sync_pb::BookmarkSpecifics bookmark_specifics(node.GetBookmarkSpecifics()); 2145 sync_pb::BookmarkSpecifics bookmark_specifics(node.GetBookmarkSpecifics());
2160 bookmark_specifics.set_url(url); 2146 bookmark_specifics.set_url(url);
2161 node.SetBookmarkSpecifics(bookmark_specifics); 2147 node.SetBookmarkSpecifics(bookmark_specifics);
2162 } 2148 }
2163 { 2149 {
2164 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2150 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2165 ReadNode root_node(&trans); 2151 ReadNode bookmark_root(&trans);
2166 root_node.InitByRootLookup(); 2152 ASSERT_EQ(BaseNode::INIT_OK,
2167 int64 child_id = root_node.GetFirstChildId(); 2153 bookmark_root.InitByTagLookup(ModelTypeToRootTag(BOOKMARKS)));
2154 int64 child_id = bookmark_root.GetFirstChildId();
2168 2155
2169 ReadNode node(&trans); 2156 ReadNode node(&trans);
2170 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id)); 2157 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id));
2171 EXPECT_FALSE(node.GetIsFolder()); 2158 EXPECT_FALSE(node.GetIsFolder());
2172 EXPECT_EQ(title, node.GetTitle()); 2159 EXPECT_EQ(title, node.GetTitle());
2173 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); 2160 EXPECT_EQ(url, node.GetBookmarkSpecifics().url());
2174 } 2161 }
2175 } 2162 }
2176 2163
2177 // Verifies WriteNode::UpdateEntryWithEncryption does not make unnecessary 2164 // Verifies WriteNode::UpdateEntryWithEncryption does not make unnecessary
(...skipping 879 matching lines...) Expand 10 before | Expand all | Expand 10 after
3057 Difference(ModelTypeSet::All(), disabled_types); 3044 Difference(ModelTypeSet::All(), disabled_types);
3058 3045
3059 // Verify only the non-disabled types remain after cleanup. 3046 // Verify only the non-disabled types remain after cleanup.
3060 sync_manager_.PurgeDisabledTypes(enabled_types, new_enabled_types); 3047 sync_manager_.PurgeDisabledTypes(enabled_types, new_enabled_types);
3061 EXPECT_TRUE(new_enabled_types.Equals(sync_manager_.InitialSyncEndedTypes())); 3048 EXPECT_TRUE(new_enabled_types.Equals(sync_manager_.InitialSyncEndedTypes()));
3062 EXPECT_TRUE(disabled_types.Equals( 3049 EXPECT_TRUE(disabled_types.Equals(
3063 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()))); 3050 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All())));
3064 } 3051 }
3065 3052
3066 } // namespace 3053 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698