| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 // Syncer unit tests. Unfortunately a lot of these tests | 5 // Syncer unit tests. Unfortunately a lot of these tests |
| 6 // are outdated and need to be reworked and updated. | 6 // are outdated and need to be reworked and updated. |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 using std::set; | 42 using std::set; |
| 43 using std::string; | 43 using std::string; |
| 44 | 44 |
| 45 namespace browser_sync { | 45 namespace browser_sync { |
| 46 | 46 |
| 47 using syncable::BaseTransaction; | 47 using syncable::BaseTransaction; |
| 48 using syncable::Blob; | 48 using syncable::Blob; |
| 49 using syncable::CountEntriesWithName; | 49 using syncable::CountEntriesWithName; |
| 50 using syncable::Directory; | 50 using syncable::Directory; |
| 51 using syncable::Entry; | 51 using syncable::Entry; |
| 52 using syncable::ExtendedAttribute; | |
| 53 using syncable::ExtendedAttributeKey; | |
| 54 using syncable::GetFirstEntryWithName; | 52 using syncable::GetFirstEntryWithName; |
| 55 using syncable::GetOnlyEntryWithName; | 53 using syncable::GetOnlyEntryWithName; |
| 56 using syncable::Id; | 54 using syncable::Id; |
| 57 using syncable::MutableEntry; | 55 using syncable::MutableEntry; |
| 58 using syncable::MutableExtendedAttribute; | |
| 59 using syncable::ReadTransaction; | 56 using syncable::ReadTransaction; |
| 60 using syncable::ScopedDirLookup; | 57 using syncable::ScopedDirLookup; |
| 61 using syncable::WriteTransaction; | 58 using syncable::WriteTransaction; |
| 62 | 59 |
| 63 using syncable::BASE_VERSION; | 60 using syncable::BASE_VERSION; |
| 64 using syncable::CREATE; | 61 using syncable::CREATE; |
| 65 using syncable::CREATE_NEW_UPDATE_ITEM; | 62 using syncable::CREATE_NEW_UPDATE_ITEM; |
| 66 using syncable::GET_BY_HANDLE; | 63 using syncable::GET_BY_HANDLE; |
| 67 using syncable::GET_BY_ID; | 64 using syncable::GET_BY_ID; |
| 68 using syncable::GET_BY_CLIENT_TAG; | 65 using syncable::GET_BY_CLIENT_TAG; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 88 using syncable::UNIQUE_SERVER_TAG; | 85 using syncable::UNIQUE_SERVER_TAG; |
| 89 using syncable::SPECIFICS; | 86 using syncable::SPECIFICS; |
| 90 using syncable::UNITTEST; | 87 using syncable::UNITTEST; |
| 91 | 88 |
| 92 using sessions::ConflictProgress; | 89 using sessions::ConflictProgress; |
| 93 using sessions::ScopedSetSessionWriteTransaction; | 90 using sessions::ScopedSetSessionWriteTransaction; |
| 94 using sessions::StatusController; | 91 using sessions::StatusController; |
| 95 using sessions::SyncSessionContext; | 92 using sessions::SyncSessionContext; |
| 96 using sessions::SyncSession; | 93 using sessions::SyncSession; |
| 97 | 94 |
| 98 namespace { | |
| 99 const char* kTestData = "Hello World!"; | |
| 100 const int kTestDataLen = 12; | |
| 101 const int64 kTestLogRequestTimestamp = 123456; | |
| 102 } // namespace | |
| 103 | |
| 104 class SyncerTest : public testing::Test, | 95 class SyncerTest : public testing::Test, |
| 105 public SyncSession::Delegate, | 96 public SyncSession::Delegate, |
| 106 public ModelSafeWorkerRegistrar, | 97 public ModelSafeWorkerRegistrar, |
| 107 public ChannelEventHandler<SyncerEvent> { | 98 public ChannelEventHandler<SyncerEvent> { |
| 108 protected: | 99 protected: |
| 109 SyncerTest() : syncer_(NULL) {} | 100 SyncerTest() : syncer_(NULL) {} |
| 110 | 101 |
| 111 // SyncSession::Delegate implementation. | 102 // SyncSession::Delegate implementation. |
| 112 virtual void OnSilencedUntil(const base::TimeTicks& silenced_until) { | 103 virtual void OnSilencedUntil(const base::TimeTicks& silenced_until) { |
| 113 FAIL() << "Should not get silenced."; | 104 FAIL() << "Should not get silenced."; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 virtual void TearDown() { | 195 virtual void TearDown() { |
| 205 mock_server_.reset(); | 196 mock_server_.reset(); |
| 206 hookup_.reset(); | 197 hookup_.reset(); |
| 207 delete syncer_; | 198 delete syncer_; |
| 208 syncer_ = NULL; | 199 syncer_ = NULL; |
| 209 syncdb_.TearDown(); | 200 syncdb_.TearDown(); |
| 210 } | 201 } |
| 211 void WriteTestDataToEntry(WriteTransaction* trans, MutableEntry* entry) { | 202 void WriteTestDataToEntry(WriteTransaction* trans, MutableEntry* entry) { |
| 212 EXPECT_FALSE(entry->Get(IS_DIR)); | 203 EXPECT_FALSE(entry->Get(IS_DIR)); |
| 213 EXPECT_FALSE(entry->Get(IS_DEL)); | 204 EXPECT_FALSE(entry->Get(IS_DEL)); |
| 214 Blob test_value(kTestData, kTestData + kTestDataLen); | |
| 215 ExtendedAttributeKey key(entry->Get(META_HANDLE), "DATA"); | |
| 216 MutableExtendedAttribute attr(trans, CREATE, key); | |
| 217 attr.mutable_value()->swap(test_value); | |
| 218 sync_pb::EntitySpecifics specifics; | 205 sync_pb::EntitySpecifics specifics; |
| 219 specifics.MutableExtension(sync_pb::bookmark)->set_url("http://demo/"); | 206 specifics.MutableExtension(sync_pb::bookmark)->set_url("http://demo/"); |
| 220 specifics.MutableExtension(sync_pb::bookmark)->set_favicon("PNG"); | 207 specifics.MutableExtension(sync_pb::bookmark)->set_favicon("PNG"); |
| 221 entry->Put(syncable::SPECIFICS, specifics); | 208 entry->Put(syncable::SPECIFICS, specifics); |
| 222 entry->Put(syncable::IS_UNSYNCED, true); | 209 entry->Put(syncable::IS_UNSYNCED, true); |
| 223 } | 210 } |
| 224 void VerifyTestDataInEntry(BaseTransaction* trans, Entry* entry) { | 211 void VerifyTestDataInEntry(BaseTransaction* trans, Entry* entry) { |
| 225 EXPECT_FALSE(entry->Get(IS_DIR)); | 212 EXPECT_FALSE(entry->Get(IS_DIR)); |
| 226 EXPECT_FALSE(entry->Get(IS_DEL)); | 213 EXPECT_FALSE(entry->Get(IS_DEL)); |
| 227 Blob test_value(kTestData, kTestData + kTestDataLen); | |
| 228 ExtendedAttributeKey key(entry->Get(META_HANDLE), "DATA"); | |
| 229 ExtendedAttribute attr(trans, GET_BY_HANDLE, key); | |
| 230 EXPECT_FALSE(attr.is_deleted()); | |
| 231 EXPECT_TRUE(test_value == attr.value()); | |
| 232 VerifyTestBookmarkDataInEntry(entry); | 214 VerifyTestBookmarkDataInEntry(entry); |
| 233 } | 215 } |
| 234 void VerifyTestBookmarkDataInEntry(Entry* entry) { | 216 void VerifyTestBookmarkDataInEntry(Entry* entry) { |
| 235 const sync_pb::EntitySpecifics& specifics = entry->Get(syncable::SPECIFICS); | 217 const sync_pb::EntitySpecifics& specifics = entry->Get(syncable::SPECIFICS); |
| 236 EXPECT_TRUE(specifics.HasExtension(sync_pb::bookmark)); | 218 EXPECT_TRUE(specifics.HasExtension(sync_pb::bookmark)); |
| 237 EXPECT_EQ("PNG", specifics.GetExtension(sync_pb::bookmark).favicon()); | 219 EXPECT_EQ("PNG", specifics.GetExtension(sync_pb::bookmark).favicon()); |
| 238 EXPECT_EQ("http://demo/", specifics.GetExtension(sync_pb::bookmark).url()); | 220 EXPECT_EQ("http://demo/", specifics.GetExtension(sync_pb::bookmark).url()); |
| 239 } | 221 } |
| 240 | 222 |
| 241 void SyncRepeatedlyToTriggerConflictResolution(SyncSession* session) { | 223 void SyncRepeatedlyToTriggerConflictResolution(SyncSession* session) { |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 EXPECT_TRUE(iterator.Current() == session_metahandles[2]); | 517 EXPECT_TRUE(iterator.Current() == session_metahandles[2]); |
| 536 EXPECT_FALSE(iterator.Increment()); | 518 EXPECT_FALSE(iterator.Increment()); |
| 537 | 519 |
| 538 EXPECT_FALSE(iterator.Valid()); | 520 EXPECT_FALSE(iterator.Valid()); |
| 539 } | 521 } |
| 540 } | 522 } |
| 541 | 523 |
| 542 TEST_F(SyncerTest, TestGetUnsyncedAndSimpleCommit) { | 524 TEST_F(SyncerTest, TestGetUnsyncedAndSimpleCommit) { |
| 543 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); | 525 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); |
| 544 ASSERT_TRUE(dir.good()); | 526 ASSERT_TRUE(dir.good()); |
| 545 string xattr_key = "key"; | |
| 546 { | 527 { |
| 547 WriteTransaction wtrans(dir, UNITTEST, __FILE__, __LINE__); | 528 WriteTransaction wtrans(dir, UNITTEST, __FILE__, __LINE__); |
| 548 MutableEntry parent(&wtrans, syncable::CREATE, wtrans.root_id(), | 529 MutableEntry parent(&wtrans, syncable::CREATE, wtrans.root_id(), |
| 549 "Pete"); | 530 "Pete"); |
| 550 ASSERT_TRUE(parent.good()); | 531 ASSERT_TRUE(parent.good()); |
| 551 parent.Put(syncable::IS_UNSYNCED, true); | 532 parent.Put(syncable::IS_UNSYNCED, true); |
| 552 parent.Put(syncable::IS_DIR, true); | 533 parent.Put(syncable::IS_DIR, true); |
| 553 parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics()); | 534 parent.Put(syncable::SPECIFICS, DefaultBookmarkSpecifics()); |
| 554 parent.Put(syncable::BASE_VERSION, 1); | 535 parent.Put(syncable::BASE_VERSION, 1); |
| 555 parent.Put(syncable::ID, parent_id_); | 536 parent.Put(syncable::ID, parent_id_); |
| (...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1060 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); | 1041 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); |
| 1061 ASSERT_TRUE(dir.good()); | 1042 ASSERT_TRUE(dir.good()); |
| 1062 mock_server_->AddUpdateDirectory(1, 0, "foo:", 1, 10); | 1043 mock_server_->AddUpdateDirectory(1, 0, "foo:", 1, 10); |
| 1063 syncer_->SyncShare(this); | 1044 syncer_->SyncShare(this); |
| 1064 mock_server_->AddUpdateDirectory(2, 0, "foo:", 1, 20); | 1045 mock_server_->AddUpdateDirectory(2, 0, "foo:", 1, 20); |
| 1065 SyncRepeatedlyToTriggerStuckSignal(session_.get()); | 1046 SyncRepeatedlyToTriggerStuckSignal(session_.get()); |
| 1066 EXPECT_FALSE(session_->status_controller()->syncer_status().syncer_stuck); | 1047 EXPECT_FALSE(session_->status_controller()->syncer_status().syncer_stuck); |
| 1067 syncer_events_.clear(); | 1048 syncer_events_.clear(); |
| 1068 } | 1049 } |
| 1069 | 1050 |
| 1070 TEST_F(SyncerTest, ExtendedAttributeWithNullCharacter) { | |
| 1071 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); | |
| 1072 ASSERT_TRUE(dir.good()); | |
| 1073 size_t xattr_count = 2; | |
| 1074 string xattr_keys[] = { "key", "key2" }; | |
| 1075 syncable::Blob xattr_values[2]; | |
| 1076 const char* value[] = { "value", "val\0ue" }; | |
| 1077 int value_length[] = { 5, 6 }; | |
| 1078 for (size_t i = 0; i < xattr_count; i++) { | |
| 1079 for (int j = 0; j < value_length[i]; j++) | |
| 1080 xattr_values[i].push_back(value[i][j]); | |
| 1081 } | |
| 1082 sync_pb::SyncEntity* ent = | |
| 1083 mock_server_->AddUpdateBookmark(1, 0, "bob", 1, 10); | |
| 1084 mock_server_->AddUpdateExtendedAttributes( | |
| 1085 ent, xattr_keys, xattr_values, xattr_count); | |
| 1086 | |
| 1087 // Add some other items. | |
| 1088 mock_server_->AddUpdateBookmark(2, 0, "fred", 2, 10); | |
| 1089 mock_server_->AddUpdateBookmark(3, 0, "sue", 15, 10); | |
| 1090 | |
| 1091 syncer_->SyncShare(this); | |
| 1092 ReadTransaction trans(dir, __FILE__, __LINE__); | |
| 1093 Entry entry1(&trans, syncable::GET_BY_ID, ids_.FromNumber(1)); | |
| 1094 ASSERT_TRUE(entry1.good()); | |
| 1095 EXPECT_TRUE(1 == entry1.Get(syncable::BASE_VERSION)); | |
| 1096 EXPECT_TRUE(1 == entry1.Get(syncable::SERVER_VERSION)); | |
| 1097 set<ExtendedAttribute> client_extended_attributes; | |
| 1098 entry1.GetAllExtendedAttributes(&trans, &client_extended_attributes); | |
| 1099 EXPECT_TRUE(xattr_count == client_extended_attributes.size()); | |
| 1100 for (size_t i = 0; i < xattr_count; i++) { | |
| 1101 ExtendedAttributeKey key(entry1.Get(syncable::META_HANDLE), xattr_keys[i]); | |
| 1102 ExtendedAttribute expected_xattr(&trans, syncable::GET_BY_HANDLE, key); | |
| 1103 EXPECT_TRUE(expected_xattr.good()); | |
| 1104 for (int j = 0; j < value_length[i]; ++j) { | |
| 1105 EXPECT_TRUE(xattr_values[i][j] == | |
| 1106 static_cast<char>(expected_xattr.value().at(j))); | |
| 1107 } | |
| 1108 } | |
| 1109 Entry entry2(&trans, syncable::GET_BY_ID, ids_.FromNumber(2)); | |
| 1110 ASSERT_TRUE(entry2.good()); | |
| 1111 Entry entry3(&trans, syncable::GET_BY_ID, ids_.FromNumber(3)); | |
| 1112 ASSERT_TRUE(entry3.good()); | |
| 1113 } | |
| 1114 | |
| 1115 TEST_F(SyncerTest, TestBasicUpdate) { | 1051 TEST_F(SyncerTest, TestBasicUpdate) { |
| 1116 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); | 1052 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); |
| 1117 ASSERT_TRUE(dir.good()); | 1053 ASSERT_TRUE(dir.good()); |
| 1118 string id = "some_id"; | 1054 string id = "some_id"; |
| 1119 string parent_id = "0"; | 1055 string parent_id = "0"; |
| 1120 string name = "in_root"; | 1056 string name = "in_root"; |
| 1121 int64 version = 10; | 1057 int64 version = 10; |
| 1122 int64 timestamp = 10; | 1058 int64 timestamp = 10; |
| 1123 mock_server_->AddUpdateDirectory(id, parent_id, name, version, timestamp); | 1059 mock_server_->AddUpdateDirectory(id, parent_id, name, version, timestamp); |
| 1124 | 1060 |
| (...skipping 3291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4416 Add(low_id_); | 4352 Add(low_id_); |
| 4417 Add(high_id_); | 4353 Add(high_id_); |
| 4418 syncer_->SyncShare(this); | 4354 syncer_->SyncShare(this); |
| 4419 ExpectLocalOrderIsByServerId(); | 4355 ExpectLocalOrderIsByServerId(); |
| 4420 } | 4356 } |
| 4421 | 4357 |
| 4422 const SyncerTest::CommitOrderingTest | 4358 const SyncerTest::CommitOrderingTest |
| 4423 SyncerTest::CommitOrderingTest::LAST_COMMIT_ITEM = {-1, TestIdFactory::root()}; | 4359 SyncerTest::CommitOrderingTest::LAST_COMMIT_ITEM = {-1, TestIdFactory::root()}; |
| 4424 | 4360 |
| 4425 } // namespace browser_sync | 4361 } // namespace browser_sync |
| OLD | NEW |