| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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. | |
| 4 | |
| 5 #include "webkit/fileapi/file_system_database_test_helper.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <functional> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/file_util.h" | |
| 12 #include "base/stl_util.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 #include "webkit/fileapi/file_system_util.h" | |
| 15 | |
| 16 namespace fileapi { | |
| 17 | |
| 18 void CorruptDatabase(const base::FilePath& db_path, | |
| 19 leveldb::FileType type, | |
| 20 ptrdiff_t offset, | |
| 21 size_t size) { | |
| 22 file_util::FileEnumerator file_enum(db_path, false /* not recursive */, | |
| 23 file_util::FileEnumerator::DIRECTORIES | | |
| 24 file_util::FileEnumerator::FILES); | |
| 25 base::FilePath file_path; | |
| 26 base::FilePath picked_file_path; | |
| 27 uint64 picked_file_number = kuint64max; | |
| 28 | |
| 29 while (!(file_path = file_enum.Next()).empty()) { | |
| 30 uint64 number = kuint64max; | |
| 31 leveldb::FileType file_type; | |
| 32 EXPECT_TRUE(leveldb::ParseFileName(FilePathToString(file_path.BaseName()), | |
| 33 &number, &file_type)); | |
| 34 if (file_type == type && | |
| 35 (picked_file_number == kuint64max || picked_file_number < number)) { | |
| 36 picked_file_path = file_path; | |
| 37 picked_file_number = number; | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 EXPECT_FALSE(picked_file_path.empty()); | |
| 42 EXPECT_NE(kuint64max, picked_file_number); | |
| 43 | |
| 44 bool created = true; | |
| 45 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 46 base::PlatformFile file = | |
| 47 CreatePlatformFile(picked_file_path, | |
| 48 base::PLATFORM_FILE_OPEN | | |
| 49 base::PLATFORM_FILE_READ | | |
| 50 base::PLATFORM_FILE_WRITE, | |
| 51 &created, &error); | |
| 52 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
| 53 EXPECT_FALSE(created); | |
| 54 | |
| 55 base::PlatformFileInfo file_info; | |
| 56 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info)); | |
| 57 if (offset < 0) | |
| 58 offset += file_info.size; | |
| 59 EXPECT_GE(offset, 0); | |
| 60 EXPECT_LE(offset, file_info.size); | |
| 61 | |
| 62 size = std::min(size, static_cast<size_t>(file_info.size - offset)); | |
| 63 | |
| 64 std::vector<char> buf(size); | |
| 65 int read_size = base::ReadPlatformFile(file, offset, | |
| 66 vector_as_array(&buf), buf.size()); | |
| 67 EXPECT_LT(0, read_size); | |
| 68 EXPECT_GE(buf.size(), static_cast<size_t>(read_size)); | |
| 69 buf.resize(read_size); | |
| 70 | |
| 71 std::transform(buf.begin(), buf.end(), buf.begin(), | |
| 72 std::logical_not<char>()); | |
| 73 | |
| 74 int written_size = base::WritePlatformFile(file, offset, | |
| 75 vector_as_array(&buf), buf.size()); | |
| 76 EXPECT_GT(written_size, 0); | |
| 77 EXPECT_EQ(buf.size(), static_cast<size_t>(written_size)); | |
| 78 | |
| 79 base::ClosePlatformFile(file); | |
| 80 } | |
| 81 | |
| 82 void DeleteDatabaseFile(const base::FilePath& db_path, | |
| 83 leveldb::FileType type) { | |
| 84 file_util::FileEnumerator file_enum(db_path, false /* not recursive */, | |
| 85 file_util::FileEnumerator::DIRECTORIES | | |
| 86 file_util::FileEnumerator::FILES); | |
| 87 base::FilePath file_path; | |
| 88 while (!(file_path = file_enum.Next()).empty()) { | |
| 89 uint64 number = kuint64max; | |
| 90 leveldb::FileType file_type; | |
| 91 EXPECT_TRUE(leveldb::ParseFileName(FilePathToString(file_path.BaseName()), | |
| 92 &number, &file_type)); | |
| 93 if (file_type == type) { | |
| 94 file_util::Delete(file_path, false); | |
| 95 // We may have multiple files for the same type, so don't break here. | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 } | |
| 100 | |
| 101 } // namespace fileapi | |
| OLD | NEW |