1 // Copyright (c) 2013 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 "base/files/file.h"
6 #include "base/files/file_enumerator.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/test/test_suite.h"
11 #include "third_party/leveldatabase/env_chromium.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/leveldatabase/env_idb.h"
14 #include "third_party/leveldatabase/src/include/leveldb/db.h"
16 #define FPL FILE_PATH_LITERAL
20 using leveldb::IDBEnv
;
21 using leveldb::Options
;
22 using leveldb::ReadOptions
;
24 using leveldb::Status
;
25 using leveldb::WritableFile
;
26 using leveldb::WriteOptions
;
27 using leveldb_env::ChromiumEnv
;
28 using leveldb_env::MethodID
;
30 TEST(ErrorEncoding
, OnlyAMethod
) {
31 const MethodID in_method
= leveldb_env::kSequentialFileRead
;
32 const Status s
= MakeIOError("Somefile.txt", "message", in_method
);
35 EXPECT_EQ(leveldb_env::METHOD_ONLY
,
36 ParseMethodAndError(s
.ToString().c_str(), &method
, &error
));
37 EXPECT_EQ(in_method
, method
);
38 EXPECT_EQ(-75, error
);
41 TEST(ErrorEncoding
, FileError
) {
42 const MethodID in_method
= leveldb_env::kWritableFileClose
;
43 const base::File::Error fe
= base::File::FILE_ERROR_INVALID_OPERATION
;
44 const Status s
= MakeIOError("Somefile.txt", "message", in_method
, fe
);
47 EXPECT_EQ(leveldb_env::METHOD_AND_PFE
,
48 ParseMethodAndError(s
.ToString().c_str(), &method
, &error
));
49 EXPECT_EQ(in_method
, method
);
53 TEST(ErrorEncoding
, NoEncodedMessage
) {
54 Status s
= Status::IOError("Some message", "from leveldb itself");
55 MethodID method
= leveldb_env::kRandomAccessFileRead
;
57 EXPECT_EQ(leveldb_env::NONE
,
58 ParseMethodAndError(s
.ToString().c_str(), &method
, &error
));
59 EXPECT_EQ(leveldb_env::kRandomAccessFileRead
, method
);
64 class MyEnv
: public T
{
66 MyEnv() : directory_syncs_(0) {}
67 int directory_syncs() { return directory_syncs_
; }
70 virtual void DidSyncDir(const std::string
& fname
) {
72 leveldb_env::ChromiumEnv::DidSyncDir(fname
);
80 class ChromiumEnvMultiPlatformTests
: public ::testing::Test
{
84 typedef ::testing::Types
<ChromiumEnv
> ChromiumEnvMultiPlatformTestsTypes
;
85 TYPED_TEST_CASE(ChromiumEnvMultiPlatformTests
,
86 ChromiumEnvMultiPlatformTestsTypes
);
88 TYPED_TEST(ChromiumEnvMultiPlatformTests
, DirectorySyncing
) {
91 base::ScopedTempDir dir
;
92 ASSERT_TRUE(dir
.CreateUniqueTempDir());
93 base::FilePath dir_path
= dir
.path();
94 std::string some_data
= "some data";
95 Slice data
= some_data
;
97 std::string manifest_file_name
=
98 dir_path
.Append(FILE_PATH_LITERAL("MANIFEST-001")).AsUTF8Unsafe();
99 WritableFile
* manifest_file_ptr
;
100 Status s
= env
.NewWritableFile(manifest_file_name
, &manifest_file_ptr
);
102 scoped_ptr
<WritableFile
> manifest_file(manifest_file_ptr
);
103 manifest_file
->Append(data
);
104 EXPECT_EQ(0, env
.directory_syncs());
105 manifest_file
->Append(data
);
106 EXPECT_EQ(0, env
.directory_syncs());
108 std::string sst_file_name
=
109 dir_path
.Append(FILE_PATH_LITERAL("000003.sst")).AsUTF8Unsafe();
110 WritableFile
* sst_file_ptr
;
111 s
= env
.NewWritableFile(sst_file_name
, &sst_file_ptr
);
113 scoped_ptr
<WritableFile
> sst_file(sst_file_ptr
);
114 sst_file
->Append(data
);
115 EXPECT_EQ(0, env
.directory_syncs());
117 manifest_file
->Append(data
);
118 EXPECT_EQ(1, env
.directory_syncs());
119 manifest_file
->Append(data
);
120 EXPECT_EQ(1, env
.directory_syncs());
123 int CountFilesWithExtension(const base::FilePath
& dir
,
124 const base::FilePath::StringType
& extension
) {
125 int matching_files
= 0;
126 base::FileEnumerator
dir_reader(
127 dir
, false, base::FileEnumerator::FILES
);
128 for (base::FilePath fname
= dir_reader
.Next(); !fname
.empty();
129 fname
= dir_reader
.Next()) {
130 if (fname
.MatchesExtension(extension
))
133 return matching_files
;
136 bool GetFirstLDBFile(const base::FilePath
& dir
, base::FilePath
* ldb_file
) {
137 base::FileEnumerator
dir_reader(
138 dir
, false, base::FileEnumerator::FILES
);
139 for (base::FilePath fname
= dir_reader
.Next(); !fname
.empty();
140 fname
= dir_reader
.Next()) {
141 if (fname
.MatchesExtension(FPL(".ldb"))) {
149 TEST(ChromiumEnv
, BackupTables
) {
151 options
.create_if_missing
= true;
152 options
.env
= IDBEnv();
154 base::ScopedTempDir scoped_temp_dir
;
155 ASSERT_TRUE(scoped_temp_dir
.CreateUniqueTempDir());
156 base::FilePath dir
= scoped_temp_dir
.path();
159 Status status
= DB::Open(options
, dir
.AsUTF8Unsafe(), &db
);
160 EXPECT_TRUE(status
.ok()) << status
.ToString();
161 status
= db
->Put(WriteOptions(), "key", "value");
162 EXPECT_TRUE(status
.ok()) << status
.ToString();
165 db
->CompactRange(&a
, &z
);
166 int ldb_files
= CountFilesWithExtension(dir
, FPL(".ldb"));
167 int bak_files
= CountFilesWithExtension(dir
, FPL(".bak"));
168 EXPECT_GT(ldb_files
, 0);
169 EXPECT_EQ(ldb_files
, bak_files
);
170 base::FilePath ldb_file
;
171 EXPECT_TRUE(GetFirstLDBFile(dir
, &ldb_file
));
173 EXPECT_TRUE(base::DeleteFile(ldb_file
, false));
174 EXPECT_EQ(ldb_files
- 1, CountFilesWithExtension(dir
, FPL(".ldb")));
176 // The ldb file deleted above should be restored in Open.
177 status
= leveldb::DB::Open(options
, dir
.AsUTF8Unsafe(), &db
);
178 EXPECT_TRUE(status
.ok()) << status
.ToString();
180 status
= db
->Get(ReadOptions(), "key", &value
);
181 EXPECT_TRUE(status
.ok()) << status
.ToString();
182 EXPECT_EQ("value", value
);
185 // Ensure that deleting an ldb file also deletes its backup.
186 int orig_ldb_files
= CountFilesWithExtension(dir
, FPL(".ldb"));
187 EXPECT_GT(ldb_files
, 0);
188 EXPECT_EQ(ldb_files
, bak_files
);
189 EXPECT_TRUE(GetFirstLDBFile(dir
, &ldb_file
));
190 options
.env
->DeleteFile(ldb_file
.AsUTF8Unsafe());
191 ldb_files
= CountFilesWithExtension(dir
, FPL(".ldb"));
192 bak_files
= CountFilesWithExtension(dir
, FPL(".bak"));
193 EXPECT_EQ(orig_ldb_files
- 1, ldb_files
);
194 EXPECT_EQ(bak_files
, ldb_files
);
197 TEST(ChromiumEnv
, GetChildrenEmptyDir
) {
198 base::ScopedTempDir scoped_temp_dir
;
199 ASSERT_TRUE(scoped_temp_dir
.CreateUniqueTempDir());
200 base::FilePath dir
= scoped_temp_dir
.path();
203 std::vector
<std::string
> result
;
204 leveldb::Status status
= env
->GetChildren(dir
.AsUTF8Unsafe(), &result
);
205 EXPECT_TRUE(status
.ok());
206 EXPECT_EQ(0U, result
.size());
209 TEST(ChromiumEnv
, GetChildrenPriorResults
) {
210 base::ScopedTempDir scoped_temp_dir
;
211 ASSERT_TRUE(scoped_temp_dir
.CreateUniqueTempDir());
212 base::FilePath dir
= scoped_temp_dir
.path();
214 base::FilePath new_file_dir
= dir
.Append(FPL("tmp_file"));
215 FILE* f
= fopen(new_file_dir
.AsUTF8Unsafe().c_str(), "w");
217 fputs("Temp file contents", f
);
222 std::vector
<std::string
> result
;
223 leveldb::Status status
= env
->GetChildren(dir
.AsUTF8Unsafe(), &result
);
224 EXPECT_TRUE(status
.ok());
225 EXPECT_EQ(1U, result
.size());
227 // And a second time should also return one result
228 status
= env
->GetChildren(dir
.AsUTF8Unsafe(), &result
);
229 EXPECT_TRUE(status
.ok());
230 EXPECT_EQ(1U, result
.size());
233 int main(int argc
, char** argv
) { return base::TestSuite(argc
, argv
).Run(); }