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 FilePath& db_path, |
| 19 leveldb::FileType type, |
| 20 ptrdiff_t offset, |
| 21 size_t size) { |
| 22 file_util::FileEnumerator file_enum( |
| 23 db_path, false /* recursive */, |
| 24 static_cast<file_util::FileEnumerator::FileType>( |
| 25 file_util::FileEnumerator::DIRECTORIES | |
| 26 file_util::FileEnumerator::FILES)); |
| 27 FilePath file_path; |
| 28 FilePath picked_file_path; |
| 29 uint64 picked_file_number = kuint64max; |
| 30 |
| 31 while (!(file_path = file_enum.Next()).empty()) { |
| 32 uint64 number = kuint64max; |
| 33 leveldb::FileType file_type; |
| 34 EXPECT_TRUE(leveldb::ParseFileName(FilePathToString(file_path.BaseName()), |
| 35 &number, &file_type)); |
| 36 if (file_type == type && |
| 37 (picked_file_number == kuint64max || picked_file_number < number)) { |
| 38 picked_file_path = file_path; |
| 39 picked_file_number = number; |
| 40 } |
| 41 } |
| 42 |
| 43 EXPECT_FALSE(picked_file_path.empty()); |
| 44 EXPECT_NE(kuint64max, picked_file_number); |
| 45 |
| 46 bool created = true; |
| 47 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; |
| 48 base::PlatformFile file = |
| 49 CreatePlatformFile(picked_file_path, |
| 50 base::PLATFORM_FILE_OPEN | |
| 51 base::PLATFORM_FILE_READ | |
| 52 base::PLATFORM_FILE_WRITE, |
| 53 &created, &error); |
| 54 EXPECT_EQ(base::PLATFORM_FILE_OK, error); |
| 55 EXPECT_FALSE(created); |
| 56 |
| 57 base::PlatformFileInfo file_info; |
| 58 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info)); |
| 59 if (offset < 0) |
| 60 offset += file_info.size; |
| 61 EXPECT_GE(offset, 0); |
| 62 EXPECT_LE(offset, file_info.size); |
| 63 |
| 64 size = std::min(size, static_cast<size_t>(file_info.size - offset)); |
| 65 |
| 66 std::vector<char> buf(size); |
| 67 int read_size = base::ReadPlatformFile(file, offset, |
| 68 vector_as_array(&buf), buf.size()); |
| 69 EXPECT_LT(0, read_size); |
| 70 EXPECT_GE(buf.size(), static_cast<size_t>(read_size)); |
| 71 buf.resize(read_size); |
| 72 |
| 73 std::transform(buf.begin(), buf.end(), buf.begin(), |
| 74 std::logical_not<char>()); |
| 75 |
| 76 int written_size = base::WritePlatformFile(file, offset, |
| 77 vector_as_array(&buf), buf.size()); |
| 78 EXPECT_GT(written_size, 0); |
| 79 EXPECT_EQ(buf.size(), static_cast<size_t>(written_size)); |
| 80 |
| 81 base::ClosePlatformFile(file); |
| 82 } |
| 83 |
| 84 } // namespace fileapi |
OLD | NEW |