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_path.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/test/test_suite.h"
8 #include "env_chromium.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using namespace leveldb_env
;
12 using namespace leveldb
;
14 TEST(ErrorEncoding
, OnlyAMethod
) {
15 const MethodID in_method
= kSequentialFileRead
;
16 const Status s
= MakeIOError("Somefile.txt", "message", in_method
);
19 EXPECT_EQ(METHOD_ONLY
,
20 ParseMethodAndError(s
.ToString().c_str(), &method
, &error
));
21 EXPECT_EQ(in_method
, method
);
22 EXPECT_EQ(-75, error
);
25 TEST(ErrorEncoding
, PlatformFileError
) {
26 const MethodID in_method
= kWritableFileClose
;
27 const base::PlatformFileError pfe
=
28 base::PLATFORM_FILE_ERROR_INVALID_OPERATION
;
29 const Status s
= MakeIOError("Somefile.txt", "message", in_method
, pfe
);
32 EXPECT_EQ(METHOD_AND_PFE
,
33 ParseMethodAndError(s
.ToString().c_str(), &method
, &error
));
34 EXPECT_EQ(in_method
, method
);
35 EXPECT_EQ(pfe
, error
);
38 TEST(ErrorEncoding
, Errno
) {
39 const MethodID in_method
= kWritableFileFlush
;
40 const int some_errno
= ENOENT
;
42 MakeIOError("Somefile.txt", "message", in_method
, some_errno
);
45 EXPECT_EQ(METHOD_AND_ERRNO
,
46 ParseMethodAndError(s
.ToString().c_str(), &method
, &error
));
47 EXPECT_EQ(in_method
, method
);
48 EXPECT_EQ(some_errno
, error
);
51 TEST(ErrorEncoding
, NoEncodedMessage
) {
52 Status s
= Status::IOError("Some message", "from leveldb itself");
53 MethodID method
= kRandomAccessFileRead
;
55 EXPECT_EQ(NONE
, ParseMethodAndError(s
.ToString().c_str(), &method
, &error
));
56 EXPECT_EQ(kRandomAccessFileRead
, method
);
60 class MyEnv
: public ChromiumEnv
{
62 MyEnv() : directory_syncs_(0) {}
63 int directory_syncs() { return directory_syncs_
; }
66 virtual void DidSyncDir(const std::string
& fname
) {
68 ChromiumEnv::DidSyncDir(fname
);
75 TEST(ChromiumEnv
, DirectorySyncing
) {
77 base::ScopedTempDir dir
;
78 dir
.CreateUniqueTempDir();
79 base::FilePath dir_path
= dir
.path();
80 std::string some_data
= "some data";
81 Slice data
= some_data
;
83 std::string manifest_file_name
=
84 FilePathToString(dir_path
.Append(FILE_PATH_LITERAL("MANIFEST-001")));
85 WritableFile
* manifest_file
;
86 Status s
= env
.NewWritableFile(manifest_file_name
, &manifest_file
);
88 manifest_file
->Append(data
);
89 EXPECT_EQ(0, env
.directory_syncs());
90 manifest_file
->Append(data
);
91 EXPECT_EQ(0, env
.directory_syncs());
93 std::string sst_file_name
=
94 FilePathToString(dir_path
.Append(FILE_PATH_LITERAL("000003.sst")));
95 WritableFile
* sst_file
;
96 s
= env
.NewWritableFile(sst_file_name
, &sst_file
);
98 sst_file
->Append(data
);
99 EXPECT_EQ(0, env
.directory_syncs());
101 manifest_file
->Append(data
);
102 EXPECT_EQ(1, env
.directory_syncs());
103 manifest_file
->Append(data
);
104 EXPECT_EQ(1, env
.directory_syncs());
107 int main(int argc
, char** argv
) { return base::TestSuite(argc
, argv
).Run(); }