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