LEFT | RIGHT |
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 "BaseJsTest.h" | 19 #include "BaseJsTest.h" |
20 #include "../src/Thread.h" | 20 #include "../src/Thread.h" |
21 | 21 |
22 namespace | 22 namespace |
23 { | 23 { |
24 class MockFileSystem : public AdblockPlus::FileSystem | 24 class MockFileSystem : public AdblockPlus::IFileSystem |
25 { | 25 { |
26 public: | 26 public: |
27 bool success; | 27 bool success; |
28 std::string contentToRead; | 28 IOBuffer contentToRead; |
29 std::string lastWrittenPath; | 29 std::string lastWrittenPath; |
30 std::string lastWrittenContent; | 30 IOBuffer lastWrittenContent; |
31 std::string movedFrom; | 31 std::string movedFrom; |
32 std::string movedTo; | 32 std::string movedTo; |
33 std::string removedPath; | 33 std::string removedPath; |
34 mutable std::string statPath; | 34 mutable std::string statPath; |
35 bool statExists; | 35 bool statExists; |
36 bool statIsDirectory; | 36 bool statIsDirectory; |
37 bool statIsFile; | 37 bool statIsFile; |
38 int statLastModified; | 38 int statLastModified; |
39 | 39 |
40 MockFileSystem() : success(true) | 40 MockFileSystem() : success(true) |
41 { | 41 { |
42 } | 42 } |
43 | 43 |
44 std::shared_ptr<std::istream> Read(const std::string& path) const | 44 void Read(const std::string& path, const ReadCallback& callback) const |
45 { | 45 { |
46 if (!success) | 46 if (!success) |
47 throw std::runtime_error("Unable to read " + path); | 47 { |
48 std::stringstream* const stream = new std::stringstream; | 48 callback(IOBuffer(), "Unable to read " + path); |
49 *stream << contentToRead; | 49 return; |
50 return std::shared_ptr<std::istream>(stream); | 50 } |
51 } | 51 callback(IOBuffer(contentToRead), ""); |
52 | 52 } |
53 void Read(const std::string& path, | 53 |
54 const ReadCallback& callback) const | 54 void Write(const std::string& path, const IOBuffer& data, |
55 { | 55 const Callback& callback) |
56 try | 56 { |
57 { | 57 if (!success) |
58 auto result = Read(path); | 58 { |
59 callback(result); | 59 callback("Unable to write to " + path); |
60 } | 60 return; |
61 catch (...) | 61 } |
62 { | |
63 } | |
64 } | |
65 | |
66 void Write(const std::string& path, std::istream& data) | |
67 { | |
68 if (!success) | |
69 throw std::runtime_error("Unable to write to " + path); | |
70 lastWrittenPath = path; | 62 lastWrittenPath = path; |
71 | 63 |
72 std::stringstream content; | 64 lastWrittenContent = data; |
73 content << data.rdbuf(); | 65 callback(""); |
74 lastWrittenContent = content.str(); | 66 } |
75 } | 67 |
76 | 68 void Move(const std::string& fromPath, const std::string& toPath, |
77 void Write(const std::string& path, | 69 const Callback& callback) |
78 std::istream& data, | 70 { |
79 const Callback& callback) | 71 if (!success) |
80 { | 72 { |
81 try | 73 callback("Unable to move " + fromPath + " to " + toPath); |
82 { | 74 return; |
83 Write(path, data); | 75 } |
84 callback(); | |
85 } | |
86 catch (...) | |
87 { | |
88 } | |
89 } | |
90 | |
91 void Move(const std::string& fromPath, const std::string& toPath) | |
92 { | |
93 if (!success) | |
94 throw std::runtime_error("Unable to move " + fromPath + " to " | |
95 + toPath); | |
96 movedFrom = fromPath; | 76 movedFrom = fromPath; |
97 movedTo = toPath; | 77 movedTo = toPath; |
98 } | 78 callback(""); |
99 | 79 } |
100 void Move(const std::string& fromPath, | 80 |
101 const std::string& toPath, | 81 void Remove(const std::string& path, const Callback& callback) |
102 const Callback& callback) | 82 { |
103 { | 83 if (!success) |
104 try | 84 { |
105 { | 85 callback("Unable to remove " + path); |
106 Move(fromPath, toPath); | 86 return; |
107 callback(); | 87 } |
108 } | |
109 catch (...) | |
110 { | |
111 } | |
112 } | |
113 | |
114 void Remove(const std::string& path) | |
115 { | |
116 if (!success) | |
117 throw std::runtime_error("Unable to remove " + path); | |
118 removedPath = path; | 88 removedPath = path; |
119 } | 89 callback(""); |
120 | |
121 void Remove(const std::string& path, const Callback& callback) | |
122 { | |
123 try | |
124 { | |
125 Remove(path); | |
126 callback(); | |
127 } | |
128 catch (...) | |
129 { | |
130 } | |
131 } | |
132 | |
133 StatResult Stat(const std::string& path) const | |
134 { | |
135 if (!success) | |
136 throw std::runtime_error("Unable to stat " + path); | |
137 statPath = path; | |
138 StatResult result; | |
139 result.exists = statExists; | |
140 result.isDirectory = statIsDirectory; | |
141 result.isFile = statIsFile; | |
142 result.lastModified = statLastModified; | |
143 return result; | |
144 } | 90 } |
145 | 91 |
146 void Stat(const std::string& path, | 92 void Stat(const std::string& path, |
147 const StatCallback& callback) const | 93 const StatCallback& callback) const |
148 { | 94 { |
149 try | 95 StatResult result; |
150 { | 96 std::string error; |
151 auto result = Stat(path); | 97 if (!success) |
152 callback(result); | 98 error = "Unable to stat " + path; |
153 } | 99 else |
154 catch (...) | 100 { |
155 { | 101 statPath = path; |
156 } | 102 result.exists = statExists; |
| 103 result.isDirectory = statIsDirectory; |
| 104 result.isFile = statIsFile; |
| 105 result.lastModified = statLastModified; |
| 106 } |
| 107 callback(result, error); |
157 } | 108 } |
158 | 109 |
159 std::string Resolve(const std::string& path) const | 110 std::string Resolve(const std::string& path) const |
160 { | 111 { |
161 if (!success) | 112 if (!success) |
162 throw std::runtime_error("Unable to stat " + path); | 113 throw std::runtime_error("Unable to stat " + path); |
163 return path; | 114 return path; |
164 } | |
165 | |
166 void Resolve(const std::string& path, | |
167 const ResolveCallback& callback) const | |
168 { | |
169 try | |
170 { | |
171 auto result = Resolve(path); | |
172 callback(result); | |
173 } | |
174 catch (...) | |
175 { | |
176 } | |
177 } | 115 } |
178 }; | 116 }; |
179 | 117 |
180 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, | 118 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, |
181 std::string& error) | 119 std::string& error) |
182 { | 120 { |
183 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); | 121 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); |
184 AdblockPlus::Sleep(50); | 122 AdblockPlus::Sleep(50); |
185 content = jsEngine->Evaluate("result.content").AsString(); | 123 content = jsEngine->Evaluate("result.content").AsString(); |
186 error = jsEngine->Evaluate("result.error").AsString(); | 124 error = jsEngine->Evaluate("result.error").AsString(); |
187 } | 125 } |
188 | 126 |
189 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; | 127 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; |
190 | 128 |
191 class FileSystemJsObjectTest : public BaseJsTest | 129 class FileSystemJsObjectTest : public BaseJsTest |
192 { | 130 { |
193 protected: | 131 protected: |
194 MockFileSystemPtr mockFileSystem; | 132 MockFileSystemPtr mockFileSystem; |
195 | 133 |
196 void SetUp() | 134 void SetUp() |
197 { | 135 { |
198 BaseJsTest::SetUp(); | |
199 mockFileSystem = MockFileSystemPtr(new MockFileSystem); | 136 mockFileSystem = MockFileSystemPtr(new MockFileSystem); |
200 jsEngine->SetFileSystem(mockFileSystem); | 137 JsEngineCreationParameters params; |
| 138 params.fileSystem = mockFileSystem; |
| 139 jsEngine = CreateJsEngine(std::move(params)); |
201 } | 140 } |
202 }; | 141 }; |
203 } | 142 } |
204 | 143 |
205 TEST_F(FileSystemJsObjectTest, Read) | 144 TEST_F(FileSystemJsObjectTest, Read) |
206 { | 145 { |
207 mockFileSystem->contentToRead = "foo"; | 146 mockFileSystem->contentToRead = |
| 147 AdblockPlus::IFileSystem::IOBuffer{'f', 'o', 'o'}; |
208 std::string content; | 148 std::string content; |
209 std::string error; | 149 std::string error; |
210 ReadFile(jsEngine, content, error); | 150 ReadFile(jsEngine, content, error); |
211 ASSERT_EQ("foo", content); | 151 ASSERT_EQ("foo", content); |
212 ASSERT_EQ("", error); | 152 ASSERT_EQ("undefined", error); |
213 } | 153 } |
214 | 154 |
215 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments) | 155 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments) |
216 { | 156 { |
217 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); | 157 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); |
218 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); | 158 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); |
219 } | 159 } |
220 | 160 |
221 TEST_F(FileSystemJsObjectTest, ReadError) | 161 TEST_F(FileSystemJsObjectTest, ReadError) |
222 { | 162 { |
223 mockFileSystem->success = false; | 163 mockFileSystem->success = false; |
224 std::string content; | 164 std::string content; |
225 std::string error; | 165 std::string error; |
226 ReadFile(jsEngine, content, error); | 166 ReadFile(jsEngine, content, error); |
227 ASSERT_NE("", error); | 167 ASSERT_NE("", error); |
228 ASSERT_EQ("", content); | 168 ASSERT_EQ("", content); |
229 } | 169 } |
230 | 170 |
231 TEST_F(FileSystemJsObjectTest, Write) | 171 TEST_F(FileSystemJsObjectTest, Write) |
232 { | 172 { |
233 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; | 173 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; |
234 AdblockPlus::Sleep(50); | 174 AdblockPlus::Sleep(50); |
235 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); | 175 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); |
236 ASSERT_EQ("bar", mockFileSystem->lastWrittenContent); | 176 ASSERT_EQ((AdblockPlus::IFileSystem::IOBuffer{'b', 'a', 'r'}), |
237 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); | 177 mockFileSystem->lastWrittenContent); |
| 178 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
238 } | 179 } |
239 | 180 |
240 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) | 181 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) |
241 { | 182 { |
242 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); | 183 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); |
243 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); | 184 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); |
244 } | 185 } |
245 | 186 |
246 TEST_F(FileSystemJsObjectTest, WriteError) | 187 TEST_F(FileSystemJsObjectTest, WriteError) |
247 { | 188 { |
248 mockFileSystem->success = false; | 189 mockFileSystem->success = false; |
249 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; | 190 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; |
250 AdblockPlus::Sleep(50); | 191 AdblockPlus::Sleep(50); |
251 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); | 192 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); |
252 } | 193 } |
253 | 194 |
254 TEST_F(FileSystemJsObjectTest, Move) | 195 TEST_F(FileSystemJsObjectTest, Move) |
255 { | 196 { |
256 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 197 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
257 AdblockPlus::Sleep(50); | 198 AdblockPlus::Sleep(50); |
258 ASSERT_EQ("foo", mockFileSystem->movedFrom); | 199 ASSERT_EQ("foo", mockFileSystem->movedFrom); |
259 ASSERT_EQ("bar", mockFileSystem->movedTo); | 200 ASSERT_EQ("bar", mockFileSystem->movedTo); |
260 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); | 201 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
261 } | 202 } |
262 | 203 |
263 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) | 204 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) |
264 { | 205 { |
265 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); | 206 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); |
266 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); | 207 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); |
267 } | 208 } |
268 | 209 |
269 TEST_F(FileSystemJsObjectTest, MoveError) | 210 TEST_F(FileSystemJsObjectTest, MoveError) |
270 { | 211 { |
271 mockFileSystem->success = false; | 212 mockFileSystem->success = false; |
272 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 213 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
273 AdblockPlus::Sleep(50); | 214 AdblockPlus::Sleep(50); |
274 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); | 215 ASSERT_FALSE(jsEngine->Evaluate("error").IsUndefined()); |
275 } | 216 } |
276 | 217 |
277 TEST_F(FileSystemJsObjectTest, Remove) | 218 TEST_F(FileSystemJsObjectTest, Remove) |
278 { | 219 { |
279 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 220 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
280 AdblockPlus::Sleep(50); | 221 AdblockPlus::Sleep(50); |
281 ASSERT_EQ("foo", mockFileSystem->removedPath); | 222 ASSERT_EQ("foo", mockFileSystem->removedPath); |
282 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); | 223 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
283 } | 224 } |
284 | 225 |
285 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) | 226 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) |
286 { | 227 { |
287 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); | 228 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); |
288 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); | 229 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); |
289 } | 230 } |
290 | 231 |
291 TEST_F(FileSystemJsObjectTest, RemoveError) | 232 TEST_F(FileSystemJsObjectTest, RemoveError) |
292 { | 233 { |
293 mockFileSystem->success = false; | 234 mockFileSystem->success = false; |
294 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 235 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
295 AdblockPlus::Sleep(50); | 236 AdblockPlus::Sleep(50); |
296 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); | 237 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); |
297 } | 238 } |
298 | 239 |
299 TEST_F(FileSystemJsObjectTest, Stat) | 240 TEST_F(FileSystemJsObjectTest, Stat) |
300 { | 241 { |
301 mockFileSystem->statExists = true; | 242 mockFileSystem->statExists = true; |
302 mockFileSystem->statIsDirectory= false; | 243 mockFileSystem->statIsDirectory= false; |
303 mockFileSystem->statIsFile = true; | 244 mockFileSystem->statIsFile = true; |
304 mockFileSystem->statLastModified = 1337; | 245 mockFileSystem->statLastModified = 1337; |
305 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 246 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
306 AdblockPlus::Sleep(50); | 247 AdblockPlus::Sleep(50); |
307 ASSERT_EQ("foo", mockFileSystem->statPath); | 248 ASSERT_EQ("foo", mockFileSystem->statPath); |
308 ASSERT_EQ("", jsEngine->Evaluate("result.error").AsString()); | 249 ASSERT_TRUE(jsEngine->Evaluate("result.error").IsUndefined()); |
309 ASSERT_TRUE(jsEngine->Evaluate("result.exists").AsBool()); | 250 ASSERT_TRUE(jsEngine->Evaluate("result.exists").AsBool()); |
310 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory").AsBool()); | 251 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory").AsBool()); |
311 ASSERT_TRUE(jsEngine->Evaluate("result.isFile").AsBool()); | 252 ASSERT_TRUE(jsEngine->Evaluate("result.isFile").AsBool()); |
312 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified").AsInt()); | 253 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified").AsInt()); |
313 } | 254 } |
314 | 255 |
315 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) | 256 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) |
316 { | 257 { |
317 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); | 258 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); |
318 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); | 259 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); |
319 } | 260 } |
320 | 261 |
321 TEST_F(FileSystemJsObjectTest, StatError) | 262 TEST_F(FileSystemJsObjectTest, StatError) |
322 { | 263 { |
323 mockFileSystem->success = false; | 264 mockFileSystem->success = false; |
324 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 265 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
325 AdblockPlus::Sleep(50); | 266 AdblockPlus::Sleep(50); |
326 ASSERT_NE("", jsEngine->Evaluate("result.error").AsString()); | 267 ASSERT_FALSE(jsEngine->Evaluate("result.error").IsUndefined()); |
327 } | 268 } |
LEFT | RIGHT |