1 // Copyright 2014 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.
5 #include "chrome/browser/sync_file_system/drive_backend/metadata_database_index.h"
7 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.h"
8 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_test_util.h"
9 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h"
10 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
11 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
14 #include "third_party/leveldatabase/src/include/leveldb/db.h"
15 #include "third_party/leveldatabase/src/include/leveldb/env.h"
17 namespace sync_file_system
{
18 namespace drive_backend
{
22 const int64 kSyncRootTrackerID
= 1;
23 const int64 kAppRootTrackerID
= 2;
24 const int64 kFileTrackerID
= 3;
25 const int64 kPlaceholderTrackerID
= 4;
27 scoped_ptr
<DatabaseContents
> CreateTestDatabaseContents() {
28 scoped_ptr
<DatabaseContents
> contents(new DatabaseContents
);
30 scoped_ptr
<FileMetadata
> sync_root_metadata
=
31 test_util::CreateFolderMetadata("sync_root_folder_id",
32 "Chrome Syncable FileSystem");
33 scoped_ptr
<FileTracker
> sync_root_tracker
=
34 test_util::CreateTracker(*sync_root_metadata
, kSyncRootTrackerID
,
37 scoped_ptr
<FileMetadata
> app_root_metadata
=
38 test_util::CreateFolderMetadata("app_root_folder_id", "app_id");
39 scoped_ptr
<FileTracker
> app_root_tracker
=
40 test_util::CreateTracker(*app_root_metadata
, kAppRootTrackerID
,
41 sync_root_tracker
.get());
42 app_root_tracker
->set_app_id("app_id");
43 app_root_tracker
->set_tracker_kind(TRACKER_KIND_APP_ROOT
);
45 scoped_ptr
<FileMetadata
> file_metadata
=
46 test_util::CreateFileMetadata("file_id", "file", "file_md5");
47 scoped_ptr
<FileTracker
> file_tracker
=
48 test_util::CreateTracker(*file_metadata
, kFileTrackerID
,
49 app_root_tracker
.get());
51 scoped_ptr
<FileTracker
> placeholder_tracker
=
52 test_util::CreatePlaceholderTracker("unsynced_file_id",
53 kPlaceholderTrackerID
,
54 app_root_tracker
.get());
56 contents
->file_metadata
.push_back(sync_root_metadata
.release());
57 contents
->file_trackers
.push_back(sync_root_tracker
.release());
58 contents
->file_metadata
.push_back(app_root_metadata
.release());
59 contents
->file_trackers
.push_back(app_root_tracker
.release());
60 contents
->file_metadata
.push_back(file_metadata
.release());
61 contents
->file_trackers
.push_back(file_tracker
.release());
62 contents
->file_trackers
.push_back(placeholder_tracker
.release());
63 return contents
.Pass();
68 class MetadataDatabaseIndexTest
: public testing::Test
{
70 void SetUp() override
{
71 in_memory_env_
.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
74 contents_
= CreateTestDatabaseContents();
75 index_
= MetadataDatabaseIndex::CreateForTesting(contents_
.get(),
79 MetadataDatabaseIndex
* index() { return index_
.get(); }
82 void InitializeLevelDB() {
83 leveldb::DB
* db
= nullptr;
84 leveldb::Options options
;
85 options
.create_if_missing
= true;
86 options
.max_open_files
= 0; // Use minimum.
87 options
.env
= in_memory_env_
.get();
88 leveldb::Status status
= leveldb::DB::Open(options
, "", &db
);
89 ASSERT_TRUE(status
.ok());
91 db_
.reset(new LevelDBWrapper(make_scoped_ptr(db
)));
94 scoped_ptr
<DatabaseContents
> contents_
;
95 scoped_ptr
<MetadataDatabaseIndex
> index_
;
97 scoped_ptr
<leveldb::Env
> in_memory_env_
;
98 scoped_ptr
<LevelDBWrapper
> db_
;
101 TEST_F(MetadataDatabaseIndexTest
, GetEntryTest
) {
103 EXPECT_FALSE(index()->GetFileTracker(kInvalidTrackerID
, nullptr));
104 ASSERT_TRUE(index()->GetFileTracker(kFileTrackerID
, &tracker
));
105 EXPECT_EQ(kFileTrackerID
, tracker
.tracker_id());
106 EXPECT_EQ("file_id", tracker
.file_id());
108 FileMetadata metadata
;
109 EXPECT_FALSE(index()->GetFileMetadata(std::string(), nullptr));
110 ASSERT_TRUE(index()->GetFileMetadata("file_id", &metadata
));
111 EXPECT_EQ("file_id", metadata
.file_id());
114 TEST_F(MetadataDatabaseIndexTest
, IndexLookUpTest
) {
115 TrackerIDSet trackers
= index()->GetFileTrackerIDsByFileID("file_id");
116 EXPECT_EQ(1u, trackers
.size());
117 EXPECT_TRUE(trackers
.has_active());
118 EXPECT_EQ(kFileTrackerID
, trackers
.active_tracker());
120 int64 app_root_tracker_id
= index()->GetAppRootTracker("app_id");
121 EXPECT_EQ(kAppRootTrackerID
, app_root_tracker_id
);
123 trackers
= index()->GetFileTrackerIDsByParentAndTitle(
124 app_root_tracker_id
, "file");
125 EXPECT_EQ(1u, trackers
.size());
126 EXPECT_TRUE(trackers
.has_active());
127 EXPECT_EQ(kFileTrackerID
, trackers
.active_tracker());
129 EXPECT_TRUE(index()->PickMultiTrackerFileID().empty());
130 EXPECT_EQ(kInvalidTrackerID
,
131 index()->PickMultiBackingFilePath().parent_id
);
132 EXPECT_EQ(kPlaceholderTrackerID
, index()->PickDirtyTracker());
135 TEST_F(MetadataDatabaseIndexTest
, UpdateTest
) {
136 EXPECT_FALSE(index()->IsDemotedDirtyTracker(kPlaceholderTrackerID
));
137 index()->DemoteDirtyTracker(kPlaceholderTrackerID
);
138 EXPECT_TRUE(index()->IsDemotedDirtyTracker(kPlaceholderTrackerID
));
139 EXPECT_EQ(kInvalidTrackerID
, index()->PickDirtyTracker());
140 index()->PromoteDemotedDirtyTrackers();
141 EXPECT_EQ(kPlaceholderTrackerID
, index()->PickDirtyTracker());
143 FileMetadata metadata
;
144 ASSERT_TRUE(index()->GetFileMetadata("file_id", &metadata
));
145 FileTracker app_root_tracker
;
146 ASSERT_TRUE(index()->GetFileTracker(kAppRootTrackerID
, &app_root_tracker
));
148 int64 new_tracker_id
= 100;
149 scoped_ptr
<FileTracker
> new_tracker
=
150 test_util::CreateTracker(metadata
, new_tracker_id
, &app_root_tracker
);
151 new_tracker
->set_active(false);
152 index()->StoreFileTracker(new_tracker
.Pass());
154 EXPECT_EQ("file_id", index()->PickMultiTrackerFileID());
155 EXPECT_EQ(ParentIDAndTitle(kAppRootTrackerID
, std::string("file")),
156 index()->PickMultiBackingFilePath());
158 index()->RemoveFileMetadata("file_id");
159 index()->RemoveFileTracker(kFileTrackerID
);
161 EXPECT_FALSE(index()->GetFileMetadata("file_id", nullptr));
162 EXPECT_FALSE(index()->GetFileTracker(kFileTrackerID
, nullptr));
165 } // namespace drive_backend
166 } // namespace sync_file_system