OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
13 #include "base/scoped_temp_dir.h" | 13 #include "base/scoped_temp_dir.h" |
14 #include "base/stl_util.h" | 14 #include "webkit/fileapi/file_system_database_test_helper.h" |
15 #include "third_party/leveldatabase/src/db/filename.h" | |
16 #include "third_party/leveldatabase/src/include/leveldb/db.h" | |
17 #include "webkit/fileapi/file_system_origin_database.h" | 15 #include "webkit/fileapi/file_system_origin_database.h" |
18 #include "webkit/fileapi/file_system_util.h" | |
19 | 16 |
20 namespace fileapi { | 17 namespace fileapi { |
21 | 18 |
22 namespace { | 19 namespace { |
23 const FilePath::CharType kFileSystemDirName[] = | 20 const FilePath::CharType kFileSystemDirName[] = |
24 FILE_PATH_LITERAL("File System"); | 21 FILE_PATH_LITERAL("File System"); |
25 const FilePath::CharType kOriginDatabaseName[] = FILE_PATH_LITERAL("Origins"); | 22 const FilePath::CharType kOriginDatabaseName[] = FILE_PATH_LITERAL("Origins"); |
26 | |
27 void CorruptDatabase(const FilePath& db_path, | |
28 leveldb::FileType type, | |
29 ptrdiff_t offset, | |
30 size_t size) { | |
31 file_util::FileEnumerator file_enum( | |
32 db_path, false /* recursive */, | |
33 static_cast<file_util::FileEnumerator::FileType>( | |
34 file_util::FileEnumerator::DIRECTORIES | | |
35 file_util::FileEnumerator::FILES)); | |
36 FilePath file_path; | |
37 FilePath picked_file_path; | |
38 uint64 picked_file_number = kuint64max; | |
39 | |
40 while (!(file_path = file_enum.Next()).empty()) { | |
41 uint64 number = kuint64max; | |
42 leveldb::FileType file_type; | |
43 EXPECT_TRUE(leveldb::ParseFileName(FilePathToString(file_path.BaseName()), | |
44 &number, &file_type)); | |
45 if (file_type == type && | |
46 (picked_file_number == kuint64max || picked_file_number < number)) { | |
47 picked_file_path = file_path; | |
48 picked_file_number = number; | |
49 } | |
50 } | |
51 | |
52 EXPECT_FALSE(picked_file_path.empty()); | |
53 EXPECT_NE(kuint64max, picked_file_number); | |
54 | |
55 bool created = true; | |
56 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
57 base::PlatformFile file = | |
58 CreatePlatformFile(picked_file_path, | |
59 base::PLATFORM_FILE_OPEN | | |
60 base::PLATFORM_FILE_READ | | |
61 base::PLATFORM_FILE_WRITE, | |
62 &created, &error); | |
63 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
64 EXPECT_FALSE(created); | |
65 | |
66 base::PlatformFileInfo file_info; | |
67 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info)); | |
68 if (offset < 0) | |
69 offset += file_info.size; | |
70 EXPECT_GE(offset, 0); | |
71 EXPECT_LE(offset, file_info.size); | |
72 | |
73 size = std::min(size, static_cast<size_t>(file_info.size - offset)); | |
74 | |
75 std::vector<char> buf(size); | |
76 int read_size = base::ReadPlatformFile(file, offset, | |
77 vector_as_array(&buf), buf.size()); | |
78 EXPECT_LT(0, read_size); | |
79 EXPECT_GE(buf.size(), static_cast<size_t>(read_size)); | |
80 buf.resize(read_size); | |
81 | |
82 std::transform(buf.begin(), buf.end(), buf.begin(), | |
83 std::logical_not<char>()); | |
84 | |
85 int written_size = base::WritePlatformFile(file, offset, | |
86 vector_as_array(&buf), buf.size()); | |
87 EXPECT_GT(written_size, 0); | |
88 EXPECT_EQ(buf.size(), static_cast<size_t>(written_size)); | |
89 | |
90 base::ClosePlatformFile(file); | |
91 } | |
92 | |
93 } | 23 } |
94 | 24 |
95 TEST(FileSystemOriginDatabaseTest, BasicTest) { | 25 TEST(FileSystemOriginDatabaseTest, BasicTest) { |
96 ScopedTempDir dir; | 26 ScopedTempDir dir; |
97 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 27 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
98 const FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 28 const FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
99 EXPECT_FALSE(file_util::PathExists(kFSDir)); | 29 EXPECT_FALSE(file_util::PathExists(kFSDir)); |
100 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 30 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); |
101 | 31 |
102 FileSystemOriginDatabase database(kFSDir); | 32 FileSystemOriginDatabase database(kFSDir); |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 EXPECT_FALSE(database->HasOriginPath(kOrigin)); | 247 EXPECT_FALSE(database->HasOriginPath(kOrigin)); |
318 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); | 248 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); |
319 EXPECT_FALSE(path.empty()); | 249 EXPECT_FALSE(path.empty()); |
320 EXPECT_TRUE(database->HasOriginPath(kOrigin)); | 250 EXPECT_TRUE(database->HasOriginPath(kOrigin)); |
321 | 251 |
322 EXPECT_FALSE(file_util::PathExists(kGarbageFile)); | 252 EXPECT_FALSE(file_util::PathExists(kGarbageFile)); |
323 EXPECT_FALSE(file_util::PathExists(kGarbageDir)); | 253 EXPECT_FALSE(file_util::PathExists(kGarbageDir)); |
324 } | 254 } |
325 | 255 |
326 } // namespace fileapi | 256 } // namespace fileapi |
OLD | NEW |