Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / content / browser / fileapi / sandbox_database_test_helper.cc
blob205b653e37089dadb70d1f82283b63ad000d7b56
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.
5 #include "content/browser/fileapi/sandbox_database_test_helper.h"
7 #include <algorithm>
8 #include <functional>
9 #include <vector>
11 #include "base/files/file.h"
12 #include "base/files/file_enumerator.h"
13 #include "base/files/file_util.h"
14 #include "base/stl_util.h"
15 #include "storage/common/fileapi/file_system_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using storage::FilePathToString;
20 namespace content {
22 void CorruptDatabase(const base::FilePath& db_path,
23 leveldb::FileType type,
24 ptrdiff_t offset,
25 size_t size) {
26 base::FileEnumerator file_enum(db_path, false /* not recursive */,
27 base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES);
28 base::FilePath file_path;
29 base::FilePath picked_file_path;
30 uint64 picked_file_number = kuint64max;
32 while (!(file_path = file_enum.Next()).empty()) {
33 uint64 number = kuint64max;
34 leveldb::FileType file_type;
35 EXPECT_TRUE(leveldb::ParseFileName(FilePathToString(file_path.BaseName()),
36 &number, &file_type));
37 if (file_type == type &&
38 (picked_file_number == kuint64max || picked_file_number < number)) {
39 picked_file_path = file_path;
40 picked_file_number = number;
44 EXPECT_FALSE(picked_file_path.empty());
45 EXPECT_NE(kuint64max, picked_file_number);
47 base::File file(picked_file_path,
48 base::File::FLAG_OPEN | base::File::FLAG_READ |
49 base::File::FLAG_WRITE);
50 ASSERT_TRUE(file.IsValid());
51 EXPECT_FALSE(file.created());
53 base::File::Info file_info;
54 EXPECT_TRUE(file.GetInfo(&file_info));
55 if (offset < 0)
56 offset += file_info.size;
57 EXPECT_GE(offset, 0);
58 EXPECT_LE(offset, file_info.size);
60 size = std::min(size, static_cast<size_t>(file_info.size - offset));
62 std::vector<char> buf(size);
63 int read_size = file.Read(offset, vector_as_array(&buf), buf.size());
64 EXPECT_LT(0, read_size);
65 EXPECT_GE(buf.size(), static_cast<size_t>(read_size));
66 buf.resize(read_size);
68 std::transform(buf.begin(), buf.end(), buf.begin(),
69 std::logical_not<char>());
71 int written_size = file.Write(offset, vector_as_array(&buf), buf.size());
72 EXPECT_GT(written_size, 0);
73 EXPECT_EQ(buf.size(), static_cast<size_t>(written_size));
76 void DeleteDatabaseFile(const base::FilePath& db_path,
77 leveldb::FileType type) {
78 base::FileEnumerator file_enum(db_path, false /* not recursive */,
79 base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES);
80 base::FilePath file_path;
81 while (!(file_path = file_enum.Next()).empty()) {
82 uint64 number = kuint64max;
83 leveldb::FileType file_type;
84 EXPECT_TRUE(leveldb::ParseFileName(FilePathToString(file_path.BaseName()),
85 &number, &file_type));
86 if (file_type == type) {
87 base::DeleteFile(file_path, false);
88 // We may have multiple files for the same type, so don't break here.
93 } // namespace content