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.
5 #include "webkit/browser/fileapi/sandbox_database_test_helper.h"
11 #include "base/file_util.h"
12 #include "base/files/file_enumerator.h"
13 #include "base/stl_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "webkit/common/fileapi/file_system_util.h"
19 void CorruptDatabase(const base::FilePath
& db_path
,
20 leveldb::FileType type
,
23 base::FileEnumerator
file_enum(db_path
, false /* not recursive */,
24 base::FileEnumerator::DIRECTORIES
| base::FileEnumerator::FILES
);
25 base::FilePath file_path
;
26 base::FilePath picked_file_path
;
27 uint64 picked_file_number
= kuint64max
;
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
;
41 EXPECT_FALSE(picked_file_path
.empty());
42 EXPECT_NE(kuint64max
, picked_file_number
);
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
,
52 EXPECT_EQ(base::PLATFORM_FILE_OK
, error
);
53 EXPECT_FALSE(created
);
55 base::PlatformFileInfo file_info
;
56 EXPECT_TRUE(base::GetPlatformFileInfo(file
, &file_info
));
58 offset
+= file_info
.size
;
60 EXPECT_LE(offset
, file_info
.size
);
62 size
= std::min(size
, static_cast<size_t>(file_info
.size
- offset
));
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
);
71 std::transform(buf
.begin(), buf
.end(), buf
.begin(),
72 std::logical_not
<char>());
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
));
79 base::ClosePlatformFile(file
);
82 void DeleteDatabaseFile(const base::FilePath
& db_path
,
83 leveldb::FileType type
) {
84 base::FileEnumerator
file_enum(db_path
, false /* not recursive */,
85 base::FileEnumerator::DIRECTORIES
| base::FileEnumerator::FILES
);
86 base::FilePath file_path
;
87 while (!(file_path
= file_enum
.Next()).empty()) {
88 uint64 number
= kuint64max
;
89 leveldb::FileType file_type
;
90 EXPECT_TRUE(leveldb::ParseFileName(FilePathToString(file_path
.BaseName()),
91 &number
, &file_type
));
92 if (file_type
== type
) {
93 base::DeleteFile(file_path
, false);
94 // We may have multiple files for the same type, so don't break here.
99 } // namespace fileapi