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

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: Fix broken DCHECKs 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 EXPECT_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 EXPECT_TRUE(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 EXPECT_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id));
154 WriteNode node(&trans);
155 EXPECT_TRUE(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 838 matching lines...) Expand 10 before | Expand all | Expand 10 after
1533 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 1521 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1534 1522
1535 // Create some unencrypted unsynced data. 1523 // Create some unencrypted unsynced data.
1536 int64 folder = MakeFolderWithParent(sync_manager_.GetUserShare(), 1524 int64 folder = MakeFolderWithParent(sync_manager_.GetUserShare(),
1537 BOOKMARKS, 1525 BOOKMARKS,
1538 GetIdForDataType(BOOKMARKS), 1526 GetIdForDataType(BOOKMARKS),
1539 NULL); 1527 NULL);
1540 // First batch_size nodes are children of folder. 1528 // First batch_size nodes are children of folder.
1541 size_t i; 1529 size_t i;
1542 for (i = 0; i < batch_size; ++i) { 1530 for (i = 0; i < batch_size; ++i) {
1543 MakeNodeWithParent(sync_manager_.GetUserShare(), BOOKMARKS, 1531 MakeBookmarkWithParent(sync_manager_.GetUserShare(), folder, NULL);
1544 base::StringPrintf("%"PRIuS"", i), folder);
1545 } 1532 }
1546 // Next batch_size nodes are a different type and on their own. 1533 // Next batch_size nodes are a different type and on their own.
1547 for (; i < 2*batch_size; ++i) { 1534 for (; i < 2*batch_size; ++i) {
1548 MakeNodeWithParent(sync_manager_.GetUserShare(), SESSIONS, 1535 MakeNode(sync_manager_.GetUserShare(), SESSIONS,
1549 base::StringPrintf("%"PRIuS"", i), 1536 base::StringPrintf("%"PRIuS"", i));
1550 GetIdForDataType(SESSIONS));
1551 } 1537 }
1552 // Last batch_size nodes are a third type that will not need encryption. 1538 // Last batch_size nodes are a third type that will not need encryption.
1553 for (; i < 3*batch_size; ++i) { 1539 for (; i < 3*batch_size; ++i) {
1554 MakeNodeWithParent(sync_manager_.GetUserShare(), THEMES, 1540 MakeNode(sync_manager_.GetUserShare(), THEMES,
1555 base::StringPrintf("%"PRIuS"", i), 1541 base::StringPrintf("%"PRIuS"", i));
1556 GetIdForDataType(THEMES));
1557 } 1542 }
1558 1543
1559 { 1544 {
1560 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1545 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1561 EXPECT_TRUE(GetEncryptedTypesWithTrans(&trans).Equals( 1546 EXPECT_TRUE(GetEncryptedTypesWithTrans(&trans).Equals(
1562 SyncEncryptionHandler::SensitiveTypes())); 1547 SyncEncryptionHandler::SensitiveTypes()));
1563 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1548 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1564 trans.GetWrappedTrans(), 1549 trans.GetWrappedTrans(),
1565 BOOKMARKS, 1550 BOOKMARKS,
1566 false /* not encrypted */)); 1551 false /* not encrypted */));
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after
2143 } 2128 }
2144 2129
2145 // Create a bookmark and set the title/url, then verify the data was properly 2130 // Create a bookmark and set the title/url, then verify the data was properly
2146 // set. This replicates the unique way bookmarks have of creating sync nodes. 2131 // set. This replicates the unique way bookmarks have of creating sync nodes.
2147 // See BookmarkChangeProcessor::PlaceSyncNode(..). 2132 // See BookmarkChangeProcessor::PlaceSyncNode(..).
2148 TEST_F(SyncManagerTest, CreateLocalBookmark) { 2133 TEST_F(SyncManagerTest, CreateLocalBookmark) {
2149 std::string title = "title"; 2134 std::string title = "title";
2150 std::string url = "url"; 2135 std::string url = "url";
2151 { 2136 {
2152 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2137 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2153 ReadNode root_node(&trans); 2138 ReadNode bookmark_root(&trans);
2154 root_node.InitByRootLookup(); 2139 ASSERT_EQ(BaseNode::INIT_OK,
2140 bookmark_root.InitByTagLookup(ModelTypeToRootTag(BOOKMARKS)));
2155 WriteNode node(&trans); 2141 WriteNode node(&trans);
2156 ASSERT_TRUE(node.InitByCreation(BOOKMARKS, root_node, NULL)); 2142 ASSERT_TRUE(node.InitBookmarkByCreation(bookmark_root, NULL));
2157 node.SetIsFolder(false); 2143 node.SetIsFolder(false);
2158 node.SetTitle(UTF8ToWide(title)); 2144 node.SetTitle(UTF8ToWide(title));
2159 2145
2160 sync_pb::BookmarkSpecifics bookmark_specifics(node.GetBookmarkSpecifics()); 2146 sync_pb::BookmarkSpecifics bookmark_specifics(node.GetBookmarkSpecifics());
2161 bookmark_specifics.set_url(url); 2147 bookmark_specifics.set_url(url);
2162 node.SetBookmarkSpecifics(bookmark_specifics); 2148 node.SetBookmarkSpecifics(bookmark_specifics);
2163 } 2149 }
2164 { 2150 {
2165 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2151 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2166 ReadNode root_node(&trans); 2152 ReadNode bookmark_root(&trans);
2167 root_node.InitByRootLookup(); 2153 ASSERT_EQ(BaseNode::INIT_OK,
2168 int64 child_id = root_node.GetFirstChildId(); 2154 bookmark_root.InitByTagLookup(ModelTypeToRootTag(BOOKMARKS)));
2155 int64 child_id = bookmark_root.GetFirstChildId();
2169 2156
2170 ReadNode node(&trans); 2157 ReadNode node(&trans);
2171 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id)); 2158 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id));
2172 EXPECT_FALSE(node.GetIsFolder()); 2159 EXPECT_FALSE(node.GetIsFolder());
2173 EXPECT_EQ(title, node.GetTitle()); 2160 EXPECT_EQ(title, node.GetTitle());
2174 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); 2161 EXPECT_EQ(url, node.GetBookmarkSpecifics().url());
2175 } 2162 }
2176 } 2163 }
2177 2164
2178 // Verifies WriteNode::UpdateEntryWithEncryption does not make unnecessary 2165 // Verifies WriteNode::UpdateEntryWithEncryption does not make unnecessary
(...skipping 879 matching lines...) Expand 10 before | Expand all | Expand 10 after
3058 Difference(ModelTypeSet::All(), disabled_types); 3045 Difference(ModelTypeSet::All(), disabled_types);
3059 3046
3060 // Verify only the non-disabled types remain after cleanup. 3047 // Verify only the non-disabled types remain after cleanup.
3061 sync_manager_.PurgeDisabledTypes(enabled_types, new_enabled_types); 3048 sync_manager_.PurgeDisabledTypes(enabled_types, new_enabled_types);
3062 EXPECT_TRUE(new_enabled_types.Equals(sync_manager_.InitialSyncEndedTypes())); 3049 EXPECT_TRUE(new_enabled_types.Equals(sync_manager_.InitialSyncEndedTypes()));
3063 EXPECT_TRUE(disabled_types.Equals( 3050 EXPECT_TRUE(disabled_types.Equals(
3064 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()))); 3051 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All())));
3065 } 3052 }
3066 3053
3067 } // namespace 3054 } // namespace
OLDNEW
« no previous file with comments | « sync/internal_api/sync_encryption_handler_impl_unittest.cc ('k') | sync/internal_api/test/test_entry_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698