| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/files/file_enumerator.h" | 6 #include "base/files/file_enumerator.h" |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/test/test_suite.h" | 9 #include "base/test/test_suite.h" |
| 10 #include "env_chromium.h" | 10 #if defined(OS_WIN) |
| 11 #include "env_chromium_win32.h" |
| 12 #endif |
| 13 #include "env_chromium_posix.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/leveldatabase/env_idb.h" | 15 #include "third_party/leveldatabase/env_idb.h" |
| 13 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 16 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 14 | 17 |
| 15 using namespace leveldb_env; | 18 using namespace leveldb_env; |
| 16 using namespace leveldb; | 19 using namespace leveldb; |
| 17 | 20 |
| 18 #define FPL FILE_PATH_LITERAL | 21 #define FPL FILE_PATH_LITERAL |
| 19 | 22 |
| 20 TEST(ErrorEncoding, OnlyAMethod) { | 23 TEST(ErrorEncoding, OnlyAMethod) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 47 const Status s = | 50 const Status s = |
| 48 MakeIOError("Somefile.txt", "message", in_method, some_errno); | 51 MakeIOError("Somefile.txt", "message", in_method, some_errno); |
| 49 MethodID method; | 52 MethodID method; |
| 50 int error; | 53 int error; |
| 51 EXPECT_EQ(METHOD_AND_ERRNO, | 54 EXPECT_EQ(METHOD_AND_ERRNO, |
| 52 ParseMethodAndError(s.ToString().c_str(), &method, &error)); | 55 ParseMethodAndError(s.ToString().c_str(), &method, &error)); |
| 53 EXPECT_EQ(in_method, method); | 56 EXPECT_EQ(in_method, method); |
| 54 EXPECT_EQ(some_errno, error); | 57 EXPECT_EQ(some_errno, error); |
| 55 } | 58 } |
| 56 | 59 |
| 60 #if defined(OS_WIN) |
| 61 TEST(ErrorEncoding, ErrnoWin32) { |
| 62 const MethodID in_method = kWritableFileFlush; |
| 63 const DWORD some_errno = ERROR_FILE_NOT_FOUND; |
| 64 const Status s = |
| 65 MakeIOErrorWin32("Somefile.txt", "message", in_method, some_errno); |
| 66 MethodID method; |
| 67 int error; |
| 68 EXPECT_EQ(METHOD_AND_ERRNO, |
| 69 ParseMethodAndError(s.ToString().c_str(), &method, &error)); |
| 70 EXPECT_EQ(in_method, method); |
| 71 EXPECT_EQ(some_errno, error); |
| 72 } |
| 73 #endif |
| 74 |
| 57 TEST(ErrorEncoding, NoEncodedMessage) { | 75 TEST(ErrorEncoding, NoEncodedMessage) { |
| 58 Status s = Status::IOError("Some message", "from leveldb itself"); | 76 Status s = Status::IOError("Some message", "from leveldb itself"); |
| 59 MethodID method = kRandomAccessFileRead; | 77 MethodID method = kRandomAccessFileRead; |
| 60 int error = 4; | 78 int error = 4; |
| 61 EXPECT_EQ(NONE, ParseMethodAndError(s.ToString().c_str(), &method, &error)); | 79 EXPECT_EQ(NONE, ParseMethodAndError(s.ToString().c_str(), &method, &error)); |
| 62 EXPECT_EQ(kRandomAccessFileRead, method); | 80 EXPECT_EQ(kRandomAccessFileRead, method); |
| 63 EXPECT_EQ(4, error); | 81 EXPECT_EQ(4, error); |
| 64 } | 82 } |
| 65 | 83 |
| 66 class MyEnv : public ChromiumEnv { | 84 class MyEnvDelegate : public ChromiumEnvDelegate { |
| 67 public: | 85 public: |
| 68 MyEnv() : directory_syncs_(0) {} | 86 MyEnvDelegate() : directory_syncs_(0) {} |
| 69 int directory_syncs() { return directory_syncs_; } | 87 int directory_syncs() const { return directory_syncs_; } |
| 70 | 88 |
| 71 protected: | |
| 72 virtual void DidSyncDir(const std::string& fname) { | 89 virtual void DidSyncDir(const std::string& fname) { |
| 73 ++directory_syncs_; | 90 ++directory_syncs_; |
| 74 ChromiumEnv::DidSyncDir(fname); | |
| 75 } | 91 } |
| 76 | 92 |
| 77 private: | 93 private: |
| 78 int directory_syncs_; | 94 int directory_syncs_; |
| 79 }; | 95 }; |
| 80 | 96 |
| 81 TEST(ChromiumEnv, DirectorySyncing) { | 97 template <typename T> |
| 82 MyEnv env; | 98 class ChromiumEnvMultiPlatformTests : public ::testing::Test { |
| 99 public: |
| 100 }; |
| 101 |
| 102 #if defined(OS_WIN) |
| 103 typedef ::testing::Types<ChromiumEnvPosix, ChromiumEnvWin32> ChromiumEnvMultiPla
tformTestsTypes; |
| 104 #else |
| 105 typedef ::testing::Types<ChromiumEnvPosix> ChromiumEnvMultiPlatformTestsTypes; |
| 106 #endif |
| 107 TYPED_TEST_CASE(ChromiumEnvMultiPlatformTests, ChromiumEnvMultiPlatformTestsType
s); |
| 108 |
| 109 TYPED_TEST(ChromiumEnvMultiPlatformTests, DirectorySyncing) { |
| 110 TypeParam env; |
| 111 MyEnvDelegate* delegate(new MyEnvDelegate()); |
| 112 env.SetDelegate(delegate); |
| 113 |
| 83 base::ScopedTempDir dir; | 114 base::ScopedTempDir dir; |
| 84 dir.CreateUniqueTempDir(); | 115 dir.CreateUniqueTempDir(); |
| 85 base::FilePath dir_path = dir.path(); | 116 base::FilePath dir_path = dir.path(); |
| 86 std::string some_data = "some data"; | 117 std::string some_data = "some data"; |
| 87 Slice data = some_data; | 118 Slice data = some_data; |
| 88 | 119 |
| 89 std::string manifest_file_name = | 120 std::string manifest_file_name = |
| 90 FilePathToString(dir_path.Append(FILE_PATH_LITERAL("MANIFEST-001"))); | 121 FilePathToString(dir_path.Append(FILE_PATH_LITERAL("MANIFEST-001"))); |
| 91 WritableFile* manifest_file_ptr; | 122 WritableFile* manifest_file_ptr; |
| 92 Status s = env.NewWritableFile(manifest_file_name, &manifest_file_ptr); | 123 Status s = env.NewWritableFile(manifest_file_name, &manifest_file_ptr); |
| 93 EXPECT_TRUE(s.ok()); | 124 EXPECT_TRUE(s.ok()); |
| 94 scoped_ptr<WritableFile> manifest_file(manifest_file_ptr); | 125 scoped_ptr<WritableFile> manifest_file(manifest_file_ptr); |
| 95 manifest_file->Append(data); | 126 manifest_file->Append(data); |
| 96 EXPECT_EQ(0, env.directory_syncs()); | 127 EXPECT_EQ(0, delegate->directory_syncs()); |
| 97 manifest_file->Append(data); | 128 manifest_file->Append(data); |
| 98 EXPECT_EQ(0, env.directory_syncs()); | 129 EXPECT_EQ(0, delegate->directory_syncs()); |
| 99 | 130 |
| 100 std::string sst_file_name = | 131 std::string sst_file_name = |
| 101 FilePathToString(dir_path.Append(FILE_PATH_LITERAL("000003.sst"))); | 132 FilePathToString(dir_path.Append(FILE_PATH_LITERAL("000003.sst"))); |
| 102 WritableFile* sst_file_ptr; | 133 WritableFile* sst_file_ptr; |
| 103 s = env.NewWritableFile(sst_file_name, &sst_file_ptr); | 134 s = env.NewWritableFile(sst_file_name, &sst_file_ptr); |
| 104 EXPECT_TRUE(s.ok()); | 135 EXPECT_TRUE(s.ok()); |
| 105 scoped_ptr<WritableFile> sst_file(sst_file_ptr); | 136 scoped_ptr<WritableFile> sst_file(sst_file_ptr); |
| 106 sst_file->Append(data); | 137 sst_file->Append(data); |
| 107 EXPECT_EQ(0, env.directory_syncs()); | 138 EXPECT_EQ(0, delegate->directory_syncs()); |
| 108 | 139 |
| 109 manifest_file->Append(data); | 140 manifest_file->Append(data); |
| 110 EXPECT_EQ(1, env.directory_syncs()); | 141 EXPECT_EQ(1, delegate->directory_syncs()); |
| 111 manifest_file->Append(data); | 142 manifest_file->Append(data); |
| 112 EXPECT_EQ(1, env.directory_syncs()); | 143 EXPECT_EQ(1, delegate->directory_syncs()); |
| 113 } | 144 } |
| 114 | 145 |
| 115 int CountFilesWithExtension(const base::FilePath& dir, | 146 int CountFilesWithExtension(const base::FilePath& dir, |
| 116 const base::FilePath::StringType& extension) { | 147 const base::FilePath::StringType& extension) { |
| 117 int matching_files = 0; | 148 int matching_files = 0; |
| 118 base::FileEnumerator dir_reader( | 149 base::FileEnumerator dir_reader( |
| 119 dir, false, base::FileEnumerator::FILES); | 150 dir, false, base::FileEnumerator::FILES); |
| 120 for (base::FilePath fname = dir_reader.Next(); !fname.empty(); | 151 for (base::FilePath fname = dir_reader.Next(); !fname.empty(); |
| 121 fname = dir_reader.Next()) { | 152 fname = dir_reader.Next()) { |
| 122 if (fname.MatchesExtension(extension)) | 153 if (fname.MatchesExtension(extension)) |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 leveldb::Status status = env->GetChildren(dir.AsUTF8Unsafe(), &result); | 247 leveldb::Status status = env->GetChildren(dir.AsUTF8Unsafe(), &result); |
| 217 EXPECT_TRUE(status.ok()); | 248 EXPECT_TRUE(status.ok()); |
| 218 EXPECT_EQ(1, result.size()); | 249 EXPECT_EQ(1, result.size()); |
| 219 | 250 |
| 220 // And a second time should also return one result | 251 // And a second time should also return one result |
| 221 status = env->GetChildren(dir.AsUTF8Unsafe(), &result); | 252 status = env->GetChildren(dir.AsUTF8Unsafe(), &result); |
| 222 EXPECT_TRUE(status.ok()); | 253 EXPECT_TRUE(status.ok()); |
| 223 EXPECT_EQ(1, result.size()); | 254 EXPECT_EQ(1, result.size()); |
| 224 } | 255 } |
| 225 | 256 |
| 226 int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); } | 257 int main(int argc, char** argv) { |
| 258 return base::TestSuite(argc, argv).Run(); |
| 259 } |
| OLD | NEW |