| 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 #include "chrome/browser/sync/syncable/syncable.h" | 5 #include "chrome/browser/sync/syncable/syncable.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 | 10 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 #include "third_party/sqlite/preprocessed/sqlite3.h" | 40 #include "third_party/sqlite/preprocessed/sqlite3.h" |
| 41 | 41 |
| 42 using browser_sync::TestIdFactory; | 42 using browser_sync::TestIdFactory; |
| 43 using std::cout; | 43 using std::cout; |
| 44 using std::endl; | 44 using std::endl; |
| 45 using std::string; | 45 using std::string; |
| 46 | 46 |
| 47 namespace syncable { | 47 namespace syncable { |
| 48 | 48 |
| 49 namespace { | 49 namespace { |
| 50 // A lot of these tests were written expecting to be able to read and write | 50 void PutDataAsBookmarkFavicon(WriteTransaction* wtrans, |
| 51 // object data on entries. However, the design has changed. | 51 MutableEntry* e, |
| 52 void PutDataAsExtendedAttribute(WriteTransaction* wtrans, | 52 const char* bytes, |
| 53 MutableEntry* e, | 53 size_t bytes_length) { |
| 54 const char* bytes, | 54 sync_pb::EntitySpecifics specifics; |
| 55 size_t bytes_length) { | 55 specifics.MutableExtension(sync_pb::bookmark)->set_url("http://demo/"); |
| 56 ExtendedAttributeKey key(e->Get(META_HANDLE), "DATA"); | 56 specifics.MutableExtension(sync_pb::bookmark)->set_favicon(bytes, |
| 57 MutableExtendedAttribute attr(wtrans, CREATE, key); | 57 bytes_length); |
| 58 Blob bytes_blob(bytes, bytes + bytes_length); | 58 e->Put(SPECIFICS, specifics); |
| 59 attr.mutable_value()->swap(bytes_blob); | |
| 60 } | 59 } |
| 61 | 60 |
| 62 void ExpectDataFromExtendedAttributeEquals(BaseTransaction* trans, | 61 void ExpectDataFromBookmarkFaviconEquals(BaseTransaction* trans, |
| 63 Entry* e, | 62 Entry* e, |
| 64 const char* bytes, | 63 const char* bytes, |
| 65 size_t bytes_length) { | 64 size_t bytes_length) { |
| 66 ASSERT_TRUE(e->good()); | 65 ASSERT_TRUE(e->good()); |
| 67 Blob expected_value(bytes, bytes + bytes_length); | 66 ASSERT_TRUE(e->Get(SPECIFICS).HasExtension(sync_pb::bookmark)); |
| 68 ExtendedAttributeKey key(e->Get(META_HANDLE), "DATA"); | 67 ASSERT_EQ("http://demo/", |
| 69 ExtendedAttribute attr(trans, GET_BY_HANDLE, key); | 68 e->Get(SPECIFICS).GetExtension(sync_pb::bookmark).url()); |
| 70 EXPECT_FALSE(attr.is_deleted()); | 69 ASSERT_EQ(std::string(bytes, bytes_length), |
| 71 EXPECT_EQ(expected_value, attr.value()); | 70 e->Get(SPECIFICS).GetExtension(sync_pb::bookmark).favicon()); |
| 72 } | 71 } |
| 73 } // namespace | 72 } // namespace |
| 74 | 73 |
| 75 class SyncableGeneralTest : public testing::Test { | 74 class SyncableGeneralTest : public testing::Test { |
| 76 public: | 75 public: |
| 77 virtual void SetUp() { | 76 virtual void SetUp() { |
| 78 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 77 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 79 db_path_ = temp_dir_.path().Append( | 78 db_path_ = temp_dir_.path().Append( |
| 80 FILE_PATH_LITERAL("SyncableTest.sqlite3")); | 79 FILE_PATH_LITERAL("SyncableTest.sqlite3")); |
| 81 } | 80 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 EXPECT_EQ(*i, written_metahandle); | 130 EXPECT_EQ(*i, written_metahandle); |
| 132 } | 131 } |
| 133 } | 132 } |
| 134 | 133 |
| 135 // Test writing data to an entity. Also check that GET_BY_HANDLE works. | 134 // Test writing data to an entity. Also check that GET_BY_HANDLE works. |
| 136 static const char s[] = "Hello World."; | 135 static const char s[] = "Hello World."; |
| 137 { | 136 { |
| 138 WriteTransaction trans(&dir, UNITTEST, __FILE__, __LINE__); | 137 WriteTransaction trans(&dir, UNITTEST, __FILE__, __LINE__); |
| 139 MutableEntry e(&trans, GET_BY_HANDLE, written_metahandle); | 138 MutableEntry e(&trans, GET_BY_HANDLE, written_metahandle); |
| 140 ASSERT_TRUE(e.good()); | 139 ASSERT_TRUE(e.good()); |
| 141 PutDataAsExtendedAttribute(&trans, &e, s, sizeof(s)); | 140 PutDataAsBookmarkFavicon(&trans, &e, s, sizeof(s)); |
| 142 } | 141 } |
| 143 | 142 |
| 144 // Test reading back the contents that we just wrote. | 143 // Test reading back the contents that we just wrote. |
| 145 { | 144 { |
| 146 WriteTransaction trans(&dir, UNITTEST, __FILE__, __LINE__); | 145 WriteTransaction trans(&dir, UNITTEST, __FILE__, __LINE__); |
| 147 MutableEntry e(&trans, GET_BY_HANDLE, written_metahandle); | 146 MutableEntry e(&trans, GET_BY_HANDLE, written_metahandle); |
| 148 ASSERT_TRUE(e.good()); | 147 ASSERT_TRUE(e.good()); |
| 149 ExpectDataFromExtendedAttributeEquals(&trans, &e, s, sizeof(s)); | 148 ExpectDataFromBookmarkFaviconEquals(&trans, &e, s, sizeof(s)); |
| 150 } | 149 } |
| 151 | 150 |
| 152 // Verify it exists in the folder. | 151 // Verify it exists in the folder. |
| 153 { | 152 { |
| 154 ReadTransaction rtrans(&dir, __FILE__, __LINE__); | 153 ReadTransaction rtrans(&dir, __FILE__, __LINE__); |
| 155 EXPECT_EQ(1, CountEntriesWithName(&rtrans, rtrans.root_id(), name)); | 154 EXPECT_EQ(1, CountEntriesWithName(&rtrans, rtrans.root_id(), name)); |
| 156 } | 155 } |
| 157 | 156 |
| 158 // Now delete it. | 157 // Now delete it. |
| 159 { | 158 { |
| (...skipping 1243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1403 file_util::Delete(directory_manager_->GetSyncDataDatabasePath(), | 1402 file_util::Delete(directory_manager_->GetSyncDataDatabasePath(), |
| 1404 true); | 1403 true); |
| 1405 // Test. | 1404 // Test. |
| 1406 directory_manager_->Open(dirname); | 1405 directory_manager_->Open(dirname); |
| 1407 ScopedDirLookup dir(directory_manager_, dirname); | 1406 ScopedDirLookup dir(directory_manager_, dirname); |
| 1408 CHECK(dir.good()); | 1407 CHECK(dir.good()); |
| 1409 WriteTransaction trans(dir, UNITTEST, __FILE__, __LINE__); | 1408 WriteTransaction trans(dir, UNITTEST, __FILE__, __LINE__); |
| 1410 MutableEntry me(&trans, CREATE, trans.root_id(), "Jeff"); | 1409 MutableEntry me(&trans, CREATE, trans.root_id(), "Jeff"); |
| 1411 me.Put(BASE_VERSION, 1); | 1410 me.Put(BASE_VERSION, 1); |
| 1412 me.Put(ID, jeff_id); | 1411 me.Put(ID, jeff_id); |
| 1413 PutDataAsExtendedAttribute(&trans, &me, test_bytes, | 1412 PutDataAsBookmarkFavicon(&trans, &me, test_bytes, |
| 1414 sizeof(test_bytes)); | 1413 sizeof(test_bytes)); |
| 1415 } | 1414 } |
| 1416 { | 1415 { |
| 1417 ScopedDirLookup dir(directory_manager_, dirname); | 1416 ScopedDirLookup dir(directory_manager_, dirname); |
| 1418 CHECK(dir.good()); | 1417 CHECK(dir.good()); |
| 1419 dir->SaveChanges(); | 1418 dir->SaveChanges(); |
| 1420 } | 1419 } |
| 1421 directory_manager_->Close(dirname); | 1420 directory_manager_->Close(dirname); |
| 1422 break; | 1421 break; |
| 1423 case 1: | 1422 case 1: |
| 1424 { | 1423 { |
| 1425 directory_manager_->Open(dirname); | 1424 directory_manager_->Open(dirname); |
| 1426 ScopedDirLookup dir(directory_manager_, dirname); | 1425 ScopedDirLookup dir(directory_manager_, dirname); |
| 1427 CHECK(dir.good()); | 1426 CHECK(dir.good()); |
| 1428 } | 1427 } |
| 1429 break; | 1428 break; |
| 1430 case 2: | 1429 case 2: |
| 1431 { | 1430 { |
| 1432 ScopedDirLookup dir(directory_manager_, dirname); | 1431 ScopedDirLookup dir(directory_manager_, dirname); |
| 1433 CHECK(dir.good()); | 1432 CHECK(dir.good()); |
| 1434 } | 1433 } |
| 1435 break; | 1434 break; |
| 1436 case 3: | 1435 case 3: |
| 1437 { | 1436 { |
| 1438 ScopedDirLookup dir(directory_manager_, dirname); | 1437 ScopedDirLookup dir(directory_manager_, dirname); |
| 1439 CHECK(dir.good()); | 1438 CHECK(dir.good()); |
| 1440 ReadTransaction trans(dir, __FILE__, __LINE__); | 1439 ReadTransaction trans(dir, __FILE__, __LINE__); |
| 1441 Entry e(&trans, GET_BY_ID, jeff_id); | 1440 Entry e(&trans, GET_BY_ID, jeff_id); |
| 1442 ExpectDataFromExtendedAttributeEquals(&trans, &e, test_bytes, | 1441 ExpectDataFromBookmarkFaviconEquals(&trans, &e, test_bytes, |
| 1443 sizeof(test_bytes)); | 1442 sizeof(test_bytes)); |
| 1444 } | 1443 } |
| 1445 // Same result as CloseAllDirectories, but more code coverage. | 1444 // Same result as CloseAllDirectories, but more code coverage. |
| 1446 directory_manager_->Close(dirname); | 1445 directory_manager_->Close(dirname); |
| 1447 break; | 1446 break; |
| 1448 } | 1447 } |
| 1449 step_->number += 1; | 1448 step_->number += 1; |
| 1450 step_->condvar.Signal(); | 1449 step_->condvar.Signal(); |
| 1451 } | 1450 } |
| 1452 } | 1451 } |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1570 } | 1569 } |
| 1571 } | 1570 } |
| 1572 } | 1571 } |
| 1573 | 1572 |
| 1574 void FakeSync(MutableEntry* e, const char* fake_id) { | 1573 void FakeSync(MutableEntry* e, const char* fake_id) { |
| 1575 e->Put(IS_UNSYNCED, false); | 1574 e->Put(IS_UNSYNCED, false); |
| 1576 e->Put(BASE_VERSION, 2); | 1575 e->Put(BASE_VERSION, 2); |
| 1577 e->Put(ID, Id::CreateFromServerId(fake_id)); | 1576 e->Put(ID, Id::CreateFromServerId(fake_id)); |
| 1578 } | 1577 } |
| 1579 | 1578 |
| 1580 TEST_F(SyncableDirectoryTest, Bug1509232) { | |
| 1581 const string a = "alpha"; | |
| 1582 const Id entry_id = dir_.get()->NextId(); | |
| 1583 CreateEntry(a, entry_id); | |
| 1584 { | |
| 1585 WriteTransaction trans(dir_.get(), UNITTEST, __FILE__, __LINE__); | |
| 1586 MutableEntry e(&trans, GET_BY_ID, entry_id); | |
| 1587 ASSERT_TRUE(e.good()); | |
| 1588 ExtendedAttributeKey key(e.Get(META_HANDLE), "resourcefork"); | |
| 1589 MutableExtendedAttribute ext(&trans, CREATE, key); | |
| 1590 ASSERT_TRUE(ext.good()); | |
| 1591 const char value[] = "stuff"; | |
| 1592 Blob value_blob(value, value + arraysize(value)); | |
| 1593 ext.mutable_value()->swap(value_blob); | |
| 1594 ext.delete_attribute(); | |
| 1595 } | |
| 1596 // This call to SaveChanges used to CHECK fail. | |
| 1597 dir_.get()->SaveChanges(); | |
| 1598 } | |
| 1599 | |
| 1600 class SyncableClientTagTest : public SyncableDirectoryTest { | 1579 class SyncableClientTagTest : public SyncableDirectoryTest { |
| 1601 public: | 1580 public: |
| 1602 static const int kBaseVersion = 1; | 1581 static const int kBaseVersion = 1; |
| 1603 const char* test_name_; | 1582 const char* test_name_; |
| 1604 const char* test_tag_; | 1583 const char* test_tag_; |
| 1605 | 1584 |
| 1606 SyncableClientTagTest() : test_name_("test_name"), test_tag_("dietcoke") {} | 1585 SyncableClientTagTest() : test_name_("test_name"), test_tag_("dietcoke") {} |
| 1607 | 1586 |
| 1608 bool CreateWithDefaultTag(Id id, bool deleted) { | 1587 bool CreateWithDefaultTag(Id id, bool deleted) { |
| 1609 return CreateWithTag(test_tag_, id, deleted); | 1588 return CreateWithTag(test_tag_, id, deleted); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1687 TEST_F(SyncableClientTagTest, TestClientTagIndexDuplicateServer) { | 1666 TEST_F(SyncableClientTagTest, TestClientTagIndexDuplicateServer) { |
| 1688 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true)); | 1667 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true)); |
| 1689 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true)); | 1668 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true)); |
| 1690 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false)); | 1669 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false)); |
| 1691 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false)); | 1670 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false)); |
| 1692 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true)); | 1671 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true)); |
| 1693 } | 1672 } |
| 1694 | 1673 |
| 1695 } // namespace | 1674 } // namespace |
| 1696 } // namespace syncable | 1675 } // namespace syncable |
| OLD | NEW |