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
);
34 base::File::Error error
= base::File::FILE_ERROR_MAX
;
35 EXPECT_EQ(leveldb_env::METHOD_ONLY
, ParseMethodAndError(s
, &method
, &error
));
36 EXPECT_EQ(in_method
, method
);
37 EXPECT_EQ(base::File::FILE_ERROR_MAX
, error
);
40 TEST(ErrorEncoding
, FileError
) {
41 const MethodID in_method
= leveldb_env::kWritableFileClose
;
42 const base::File::Error fe
= base::File::FILE_ERROR_INVALID_OPERATION
;
43 const Status s
= MakeIOError("Somefile.txt", "message", in_method
, fe
);
45 base::File::Error error
;
46 EXPECT_EQ(leveldb_env::METHOD_AND_BFE
,
47 ParseMethodAndError(s
, &method
, &error
));
48 EXPECT_EQ(in_method
, method
);
52 TEST(ErrorEncoding
, NoEncodedMessage
) {
53 Status s
= Status::IOError("Some message", "from leveldb itself");
54 MethodID method
= leveldb_env::kRandomAccessFileRead
;
55 base::File::Error error
= base::File::FILE_ERROR_MAX
;
56 EXPECT_EQ(leveldb_env::NONE
, ParseMethodAndError(s
, &method
, &error
));
57 EXPECT_EQ(leveldb_env::kRandomAccessFileRead
, method
);
58 EXPECT_EQ(base::File::FILE_ERROR_MAX
, error
);
62 class ChromiumEnvMultiPlatformTests
: public ::testing::Test
{
66 typedef ::testing::Types
<ChromiumEnv
> ChromiumEnvMultiPlatformTestsTypes
;
67 TYPED_TEST_CASE(ChromiumEnvMultiPlatformTests
,
68 ChromiumEnvMultiPlatformTestsTypes
);
70 int CountFilesWithExtension(const base::FilePath
& dir
,
71 const base::FilePath::StringType
& extension
) {
72 int matching_files
= 0;
73 base::FileEnumerator
dir_reader(
74 dir
, false, base::FileEnumerator::FILES
);
75 for (base::FilePath fname
= dir_reader
.Next(); !fname
.empty();
76 fname
= dir_reader
.Next()) {
77 if (fname
.MatchesExtension(extension
))
80 return matching_files
;
83 bool GetFirstLDBFile(const base::FilePath
& dir
, base::FilePath
* ldb_file
) {
84 base::FileEnumerator
dir_reader(
85 dir
, false, base::FileEnumerator::FILES
);
86 for (base::FilePath fname
= dir_reader
.Next(); !fname
.empty();
87 fname
= dir_reader
.Next()) {
88 if (fname
.MatchesExtension(FPL(".ldb"))) {
96 TEST(ChromiumEnv
, BackupTables
) {
98 options
.create_if_missing
= true;
99 options
.env
= IDBEnv();
101 base::ScopedTempDir scoped_temp_dir
;
102 ASSERT_TRUE(scoped_temp_dir
.CreateUniqueTempDir());
103 base::FilePath dir
= scoped_temp_dir
.path();
106 Status status
= DB::Open(options
, dir
.AsUTF8Unsafe(), &db
);
107 EXPECT_TRUE(status
.ok()) << status
.ToString();
108 status
= db
->Put(WriteOptions(), "key", "value");
109 EXPECT_TRUE(status
.ok()) << status
.ToString();
112 db
->CompactRange(&a
, &z
);
113 int ldb_files
= CountFilesWithExtension(dir
, FPL(".ldb"));
114 int bak_files
= CountFilesWithExtension(dir
, FPL(".bak"));
115 EXPECT_GT(ldb_files
, 0);
116 EXPECT_EQ(ldb_files
, bak_files
);
117 base::FilePath ldb_file
;
118 EXPECT_TRUE(GetFirstLDBFile(dir
, &ldb_file
));
120 EXPECT_TRUE(base::DeleteFile(ldb_file
, false));
121 EXPECT_EQ(ldb_files
- 1, CountFilesWithExtension(dir
, FPL(".ldb")));
123 // The ldb file deleted above should be restored in Open.
124 status
= leveldb::DB::Open(options
, dir
.AsUTF8Unsafe(), &db
);
125 EXPECT_TRUE(status
.ok()) << status
.ToString();
127 status
= db
->Get(ReadOptions(), "key", &value
);
128 EXPECT_TRUE(status
.ok()) << status
.ToString();
129 EXPECT_EQ("value", value
);
132 // Ensure that deleting an ldb file also deletes its backup.
133 int orig_ldb_files
= CountFilesWithExtension(dir
, FPL(".ldb"));
134 EXPECT_GT(ldb_files
, 0);
135 EXPECT_EQ(ldb_files
, bak_files
);
136 EXPECT_TRUE(GetFirstLDBFile(dir
, &ldb_file
));
137 options
.env
->DeleteFile(ldb_file
.AsUTF8Unsafe());
138 ldb_files
= CountFilesWithExtension(dir
, FPL(".ldb"));
139 bak_files
= CountFilesWithExtension(dir
, FPL(".bak"));
140 EXPECT_EQ(orig_ldb_files
- 1, ldb_files
);
141 EXPECT_EQ(bak_files
, ldb_files
);
144 TEST(ChromiumEnv
, GetChildrenEmptyDir
) {
145 base::ScopedTempDir scoped_temp_dir
;
146 ASSERT_TRUE(scoped_temp_dir
.CreateUniqueTempDir());
147 base::FilePath dir
= scoped_temp_dir
.path();
150 std::vector
<std::string
> result
;
151 leveldb::Status status
= env
->GetChildren(dir
.AsUTF8Unsafe(), &result
);
152 EXPECT_TRUE(status
.ok());
153 EXPECT_EQ(0U, result
.size());
156 TEST(ChromiumEnv
, GetChildrenPriorResults
) {
157 base::ScopedTempDir scoped_temp_dir
;
158 ASSERT_TRUE(scoped_temp_dir
.CreateUniqueTempDir());
159 base::FilePath dir
= scoped_temp_dir
.path();
161 base::FilePath new_file_dir
= dir
.Append(FPL("tmp_file"));
162 FILE* f
= fopen(new_file_dir
.AsUTF8Unsafe().c_str(), "w");
164 fputs("Temp file contents", f
);
169 std::vector
<std::string
> result
;
170 leveldb::Status status
= env
->GetChildren(dir
.AsUTF8Unsafe(), &result
);
171 EXPECT_TRUE(status
.ok());
172 EXPECT_EQ(1U, result
.size());
174 // And a second time should also return one result
175 status
= env
->GetChildren(dir
.AsUTF8Unsafe(), &result
);
176 EXPECT_TRUE(status
.ok());
177 EXPECT_EQ(1U, result
.size());
180 int main(int argc
, char** argv
) { return base::TestSuite(argc
, argv
).Run(); }