| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "content/browser/fileapi/sandbox_database_test_helper.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <algorithm> | |
| 10 #include <functional> | |
| 11 #include <limits> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/files/file.h" | |
| 15 #include "base/files/file_enumerator.h" | |
| 16 #include "base/files/file_util.h" | |
| 17 #include "storage/common/fileapi/file_system_util.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 using storage::FilePathToString; | |
| 21 | |
| 22 namespace content { | |
| 23 | |
| 24 void CorruptDatabase(const base::FilePath& db_path, | |
| 25 leveldb::FileType type, | |
| 26 ptrdiff_t offset, | |
| 27 size_t size) { | |
| 28 base::FileEnumerator file_enum(db_path, false /* not recursive */, | |
| 29 base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES); | |
| 30 base::FilePath file_path; | |
| 31 base::FilePath picked_file_path; | |
| 32 uint64_t picked_file_number = std::numeric_limits<uint64_t>::max(); | |
| 33 | |
| 34 while (!(file_path = file_enum.Next()).empty()) { | |
| 35 uint64_t number = std::numeric_limits<uint64_t>::max(); | |
| 36 leveldb::FileType file_type; | |
| 37 EXPECT_TRUE(leveldb::ParseFileName(FilePathToString(file_path.BaseName()), | |
| 38 &number, &file_type)); | |
| 39 if (file_type == type && | |
| 40 (picked_file_number == std::numeric_limits<uint64_t>::max() || | |
| 41 picked_file_number < number)) { | |
| 42 picked_file_path = file_path; | |
| 43 picked_file_number = number; | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 EXPECT_FALSE(picked_file_path.empty()); | |
| 48 EXPECT_NE(std::numeric_limits<uint64_t>::max(), picked_file_number); | |
| 49 | |
| 50 base::File file(picked_file_path, | |
| 51 base::File::FLAG_OPEN | base::File::FLAG_READ | | |
| 52 base::File::FLAG_WRITE); | |
| 53 ASSERT_TRUE(file.IsValid()); | |
| 54 EXPECT_FALSE(file.created()); | |
| 55 | |
| 56 base::File::Info file_info; | |
| 57 EXPECT_TRUE(file.GetInfo(&file_info)); | |
| 58 if (offset < 0) | |
| 59 offset += file_info.size; | |
| 60 EXPECT_GE(offset, 0); | |
| 61 EXPECT_LE(offset, file_info.size); | |
| 62 | |
| 63 size = std::min(size, static_cast<size_t>(file_info.size - offset)); | |
| 64 | |
| 65 std::vector<char> buf(size); | |
| 66 int read_size = file.Read(offset, buf.data(), 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 = file.Write(offset, buf.data(), buf.size()); | |
| 75 EXPECT_GT(written_size, 0); | |
| 76 EXPECT_EQ(buf.size(), static_cast<size_t>(written_size)); | |
| 77 } | |
| 78 | |
| 79 void DeleteDatabaseFile(const base::FilePath& db_path, | |
| 80 leveldb::FileType type) { | |
| 81 base::FileEnumerator file_enum(db_path, false /* not recursive */, | |
| 82 base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES); | |
| 83 base::FilePath file_path; | |
| 84 while (!(file_path = file_enum.Next()).empty()) { | |
| 85 uint64_t number = std::numeric_limits<uint64_t>::max(); | |
| 86 leveldb::FileType file_type; | |
| 87 EXPECT_TRUE(leveldb::ParseFileName(FilePathToString(file_path.BaseName()), | |
| 88 &number, &file_type)); | |
| 89 if (file_type == type) { | |
| 90 base::DeleteFile(file_path, false); | |
| 91 // We may have multiple files for the same type, so don't break here. | |
| 92 } | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 } // namespace content | |
| OLD | NEW |