OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #include <sstream> | 18 #include <sstream> |
19 #include <AdblockPlus.h> | 19 #include <AdblockPlus.h> |
20 #include <gtest/gtest.h> | 20 #include <gtest/gtest.h> |
21 | 21 |
22 #include "BaseJsTest.h" | 22 #include "BaseJsTest.h" |
23 | 23 |
24 using AdblockPlus::IFileSystem; | 24 using AdblockPlus::IFileSystem; |
25 using AdblockPlus::Sync; | 25 using AdblockPlus::FileSystemPtr; |
| 26 using AdblockPlus::SchedulerTask; |
26 | 27 |
27 namespace | 28 namespace |
28 { | 29 { |
29 const std::string testPath = "libadblockplus-t\xc3\xa4st-file"; | 30 const std::string testPath = "libadblockplus-t\xc3\xa4st-file"; |
30 | 31 |
31 void WriteString(const AdblockPlus::FileSystemPtr& fileSystem, | 32 class DefaultFileSystemTest : public ::testing::Test |
32 const std::string& content) | |
33 { | 33 { |
34 Sync sync; | 34 public: |
| 35 void SetUp() override |
| 36 { |
| 37 fileSystem = AdblockPlus::CreateDefaultFileSystem([this](const SchedulerTa
sk& task) |
| 38 { |
| 39 fileSystemTasks.emplace_back(task); |
| 40 }); |
| 41 } |
| 42 protected: |
| 43 void WriteString(const std::string& content) |
| 44 { |
| 45 bool hasRun = false; |
| 46 fileSystem->Write(testPath, |
| 47 IFileSystem::IOBuffer(content.cbegin(), content.cend()), |
| 48 [&hasRun](const std::string& error) |
| 49 { |
| 50 EXPECT_TRUE(error.empty()) << error; |
| 51 hasRun = true; |
| 52 }); |
| 53 EXPECT_FALSE(hasRun); |
| 54 PumpTask(); |
| 55 EXPECT_TRUE(hasRun); |
| 56 } |
35 | 57 |
36 fileSystem->Write(testPath, | 58 void PumpTask() |
37 IFileSystem::IOBuffer(content.cbegin(), content.cend()), | 59 { |
38 [&sync](const std::string& error) | 60 ASSERT_EQ(1u, fileSystemTasks.size()); |
39 { | 61 (*fileSystemTasks.begin())(); |
40 EXPECT_TRUE(error.empty()); | 62 fileSystemTasks.pop_front(); |
| 63 } |
41 | 64 |
42 sync.Set(); | 65 std::list<SchedulerTask> fileSystemTasks; |
43 }); | 66 FileSystemPtr fileSystem; |
44 | 67 }; |
45 sync.WaitFor(); | |
46 } | |
47 } | 68 } |
48 | 69 |
49 TEST(DefaultFileSystemTest, WriteReadRemove) | 70 TEST_F(DefaultFileSystemTest, WriteReadRemove) |
50 { | 71 { |
51 Sync sync; | 72 WriteString("foo"); |
52 AdblockPlus::FileSystemPtr fileSystem = AdblockPlus::CreateDefaultFileSystem()
; | 73 |
53 WriteString(fileSystem, "foo"); | 74 bool hasReadRun = false; |
54 fileSystem->Read(testPath, | 75 fileSystem->Read(testPath, |
55 [fileSystem, &sync](IFileSystem::IOBuffer&& content, const std::string& erro
r) | 76 [this, &hasReadRun](IFileSystem::IOBuffer&& content, const std::string& erro
r) |
56 { | 77 { |
57 EXPECT_TRUE(error.empty()); | 78 EXPECT_TRUE(error.empty()); |
58 EXPECT_EQ("foo", std::string(content.cbegin(), content.cend())); | 79 EXPECT_EQ("foo", std::string(content.cbegin(), content.cend())); |
| 80 hasReadRun = true; |
| 81 }); |
| 82 EXPECT_FALSE(hasReadRun); |
| 83 PumpTask(); |
| 84 EXPECT_TRUE(hasReadRun); |
59 | 85 |
60 fileSystem->Remove(testPath, [&sync](const std::string& error) | 86 bool hasRemoveRun = false; |
61 { | 87 fileSystem->Remove(testPath, [&hasRemoveRun](const std::string& error) |
62 EXPECT_TRUE(error.empty()); | 88 { |
63 sync.Set(); | 89 EXPECT_TRUE(error.empty()); |
64 }); | 90 hasRemoveRun = true; |
65 }); | 91 }); |
66 | 92 EXPECT_FALSE(hasRemoveRun); |
67 EXPECT_TRUE(sync.WaitFor()); | 93 PumpTask(); |
| 94 EXPECT_TRUE(hasRemoveRun); |
68 } | 95 } |
69 | 96 |
70 TEST(DefaultFileSystemTest, StatWorkingDirectory) | 97 TEST_F(DefaultFileSystemTest, StatWorkingDirectory) |
71 { | 98 { |
72 Sync sync; | 99 bool hasStatRun = false; |
73 AdblockPlus::FileSystemPtr fileSystem = AdblockPlus::CreateDefaultFileSystem()
; | |
74 fileSystem->Stat(".", | 100 fileSystem->Stat(".", |
75 [fileSystem, &sync](const IFileSystem::StatResult result, const std::string&
error) | 101 [&hasStatRun](const IFileSystem::StatResult result, const std::string& error
) |
76 { | 102 { |
77 EXPECT_TRUE(error.empty()); | 103 EXPECT_TRUE(error.empty()); |
78 ASSERT_TRUE(result.exists); | 104 ASSERT_TRUE(result.exists); |
79 ASSERT_TRUE(result.isDirectory); | 105 ASSERT_TRUE(result.isDirectory); |
80 ASSERT_FALSE(result.isFile); | 106 ASSERT_FALSE(result.isFile); |
81 ASSERT_NE(0, result.lastModified); | 107 ASSERT_NE(0, result.lastModified); |
82 sync.Set(); | 108 hasStatRun = true; |
83 }); | 109 }); |
84 | 110 EXPECT_FALSE(hasStatRun); |
85 EXPECT_TRUE(sync.WaitFor()); | 111 PumpTask(); |
| 112 EXPECT_TRUE(hasStatRun); |
86 } | 113 } |
87 | 114 |
88 TEST(DefaultFileSystemTest, WriteMoveStatRemove) | 115 TEST_F(DefaultFileSystemTest, WriteMoveStatRemove) |
89 { | 116 { |
90 Sync sync; | 117 WriteString("foo"); |
91 AdblockPlus::FileSystemPtr fileSystem = AdblockPlus::CreateDefaultFileSystem()
; | |
92 WriteString(fileSystem, "foo"); | |
93 | 118 |
| 119 bool hasStatOrigFileExistsRun = false; |
94 fileSystem->Stat(testPath, | 120 fileSystem->Stat(testPath, |
95 [fileSystem, &sync](const IFileSystem::StatResult& result, const std::string
& error) | 121 [&hasStatOrigFileExistsRun](const IFileSystem::StatResult& result, const std
::string& error) |
96 { | 122 { |
97 EXPECT_TRUE(error.empty()); | 123 EXPECT_TRUE(error.empty()); |
98 ASSERT_TRUE(result.exists); | 124 ASSERT_TRUE(result.exists); |
99 ASSERT_TRUE(result.isFile); | 125 ASSERT_TRUE(result.isFile); |
100 ASSERT_FALSE(result.isDirectory); | 126 ASSERT_FALSE(result.isDirectory); |
101 ASSERT_NE(0, result.lastModified); | 127 ASSERT_NE(0, result.lastModified); |
102 const std::string newTestPath = testPath + "-new"; | 128 hasStatOrigFileExistsRun = true; |
103 fileSystem->Move(testPath, newTestPath, [fileSystem, &sync, newTestPath](c
onst std::string& error) | |
104 { | |
105 EXPECT_TRUE(error.empty()); | |
106 fileSystem->Stat(testPath, [fileSystem, &sync, newTestPath](const IFileS
ystem::StatResult& result, const std::string& error) | |
107 { | |
108 EXPECT_TRUE(error.empty()); | |
109 ASSERT_FALSE(result.exists); | |
110 fileSystem->Stat(newTestPath, [fileSystem, &sync, newTestPath](const I
FileSystem::StatResult& result, const std::string& error) | |
111 { | |
112 EXPECT_TRUE(error.empty()); | |
113 ASSERT_TRUE(result.exists); | |
114 fileSystem->Remove(newTestPath, [fileSystem, &sync, newTestPath](con
st std::string& error) | |
115 { | |
116 EXPECT_TRUE(error.empty()); | |
117 fileSystem->Stat(newTestPath, [fileSystem, &sync, newTestPath](con
st IFileSystem::StatResult& result, const std::string& error) | |
118 { | |
119 EXPECT_TRUE(error.empty()); | |
120 ASSERT_FALSE(result.exists); | |
121 sync.Set(); | |
122 }); | |
123 }); | |
124 }); | |
125 }); | |
126 }); | |
127 }); | 129 }); |
| 130 EXPECT_FALSE(hasStatOrigFileExistsRun); |
| 131 PumpTask(); |
| 132 EXPECT_TRUE(hasStatOrigFileExistsRun); |
128 | 133 |
129 EXPECT_TRUE(sync.WaitFor()); | 134 const std::string newTestPath = testPath + "-new"; |
| 135 bool hasMoveRun = false; |
| 136 fileSystem->Move(testPath, newTestPath, [&hasMoveRun, newTestPath](const std::
string& error) |
| 137 { |
| 138 EXPECT_TRUE(error.empty()); |
| 139 hasMoveRun = true; |
| 140 }); |
| 141 EXPECT_FALSE(hasMoveRun); |
| 142 PumpTask(); |
| 143 EXPECT_TRUE(hasMoveRun); |
| 144 |
| 145 bool hasStatOrigFileDontExistsRun = false; |
| 146 fileSystem->Stat(testPath, [&hasStatOrigFileDontExistsRun](const IFileSystem::
StatResult& result, const std::string& error) |
| 147 { |
| 148 EXPECT_TRUE(error.empty()); |
| 149 ASSERT_FALSE(result.exists); |
| 150 hasStatOrigFileDontExistsRun = true; |
| 151 }); |
| 152 EXPECT_FALSE(hasStatOrigFileDontExistsRun); |
| 153 PumpTask(); |
| 154 EXPECT_TRUE(hasStatOrigFileDontExistsRun); |
| 155 |
| 156 bool hasStatNewFileExistsRun = false; |
| 157 fileSystem->Stat(newTestPath, [&hasStatNewFileExistsRun](const IFileSystem::St
atResult& result, const std::string& error) |
| 158 { |
| 159 EXPECT_TRUE(error.empty()); |
| 160 ASSERT_TRUE(result.exists); |
| 161 hasStatNewFileExistsRun = true; |
| 162 }); |
| 163 EXPECT_FALSE(hasStatNewFileExistsRun); |
| 164 PumpTask(); |
| 165 EXPECT_TRUE(hasStatNewFileExistsRun); |
| 166 |
| 167 bool hasRemoveRun = false; |
| 168 fileSystem->Remove(newTestPath, [&hasRemoveRun](const std::string& error) |
| 169 { |
| 170 EXPECT_TRUE(error.empty()); |
| 171 hasRemoveRun = true; |
| 172 }); |
| 173 EXPECT_FALSE(hasRemoveRun); |
| 174 PumpTask(); |
| 175 EXPECT_TRUE(hasRemoveRun); |
| 176 |
| 177 bool hasStatRemovedFileRun = false; |
| 178 fileSystem->Stat(newTestPath, [&hasStatRemovedFileRun](const IFileSystem::Stat
Result& result, const std::string& error) |
| 179 { |
| 180 EXPECT_TRUE(error.empty()); |
| 181 ASSERT_FALSE(result.exists); |
| 182 hasStatRemovedFileRun = true; |
| 183 }); |
| 184 EXPECT_FALSE(hasStatRemovedFileRun); |
| 185 PumpTask(); |
| 186 EXPECT_TRUE(hasStatRemovedFileRun); |
130 } | 187 } |
OLD | NEW |