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::IFileSystem | 24 class MockFileSystem : public AdblockPlus::IFileSystem |
25 { | 25 { |
26 public: | 26 public: |
27 bool success; | 27 bool success; |
28 std::vector<char> contentToRead; | 28 IOBuffer contentToRead; |
29 std::string lastWrittenPath; | 29 std::string lastWrittenPath; |
30 std::vector<char> 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 void Read(const std::string& path, const ReadCallback& callback) const | 44 void Read(const std::string& path, const ReadCallback& callback) const |
45 { | 45 { |
46 if (!success) | 46 if (!success) |
47 { | 47 { |
48 callback(std::vector<char>(), "Unable to read " + path); | 48 callback(IOBuffer(), "Unable to read " + path); |
49 return; | 49 return; |
50 } | 50 } |
51 callback(std::vector<char>(contentToRead), ""); | 51 callback(IOBuffer(contentToRead), ""); |
52 } | 52 } |
53 | 53 |
54 void Write(const std::string& path, const std::vector<char>& data, | 54 void Write(const std::string& path, const IOBuffer& data, |
55 const Callback& callback) | 55 const Callback& callback) |
56 { | 56 { |
57 if (!success) | 57 if (!success) |
58 { | 58 { |
59 callback("Unable to write to " + path); | 59 callback("Unable to write to " + path); |
60 return; | 60 return; |
61 } | 61 } |
62 lastWrittenPath = path; | 62 lastWrittenPath = path; |
63 | 63 |
64 lastWrittenContent = data; | 64 lastWrittenContent = data; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 mockFileSystem = MockFileSystemPtr(new MockFileSystem); | 136 mockFileSystem = MockFileSystemPtr(new MockFileSystem); |
137 JsEngineCreationParameters params; | 137 JsEngineCreationParameters params; |
138 params.fileSystem = mockFileSystem; | 138 params.fileSystem = mockFileSystem; |
139 jsEngine = CreateJsEngine(std::move(params)); | 139 jsEngine = CreateJsEngine(std::move(params)); |
140 } | 140 } |
141 }; | 141 }; |
142 } | 142 } |
143 | 143 |
144 TEST_F(FileSystemJsObjectTest, Read) | 144 TEST_F(FileSystemJsObjectTest, Read) |
145 { | 145 { |
146 mockFileSystem->contentToRead = std::vector<char>{'f', 'o', 'o'}; | 146 mockFileSystem->contentToRead = |
| 147 AdblockPlus::IFileSystem::IOBuffer{'f', 'o', 'o'}; |
147 std::string content; | 148 std::string content; |
148 std::string error; | 149 std::string error; |
149 ReadFile(jsEngine, content, error); | 150 ReadFile(jsEngine, content, error); |
150 ASSERT_EQ("foo", content); | 151 ASSERT_EQ("foo", content); |
151 ASSERT_EQ("undefined", error); | 152 ASSERT_EQ("undefined", error); |
152 } | 153 } |
153 | 154 |
154 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments) | 155 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments) |
155 { | 156 { |
156 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); | 157 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); |
157 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); | 158 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); |
158 } | 159 } |
159 | 160 |
160 TEST_F(FileSystemJsObjectTest, ReadError) | 161 TEST_F(FileSystemJsObjectTest, ReadError) |
161 { | 162 { |
162 mockFileSystem->success = false; | 163 mockFileSystem->success = false; |
163 std::string content; | 164 std::string content; |
164 std::string error; | 165 std::string error; |
165 ReadFile(jsEngine, content, error); | 166 ReadFile(jsEngine, content, error); |
166 ASSERT_NE("", error); | 167 ASSERT_NE("", error); |
167 ASSERT_EQ("", content); | 168 ASSERT_EQ("", content); |
168 } | 169 } |
169 | 170 |
170 TEST_F(FileSystemJsObjectTest, Write) | 171 TEST_F(FileSystemJsObjectTest, Write) |
171 { | 172 { |
172 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; | 173 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; |
173 AdblockPlus::Sleep(50); | 174 AdblockPlus::Sleep(50); |
174 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); | 175 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); |
175 ASSERT_EQ((std::vector<char>{'b', 'a', 'r'}), | 176 ASSERT_EQ((AdblockPlus::IFileSystem::IOBuffer{'b', 'a', 'r'}), |
176 mockFileSystem->lastWrittenContent); | 177 mockFileSystem->lastWrittenContent); |
177 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); | 178 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
178 } | 179 } |
179 | 180 |
180 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) | 181 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) |
181 { | 182 { |
182 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); | 183 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); |
183 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); | 184 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); |
184 } | 185 } |
185 | 186 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); | 259 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); |
259 } | 260 } |
260 | 261 |
261 TEST_F(FileSystemJsObjectTest, StatError) | 262 TEST_F(FileSystemJsObjectTest, StatError) |
262 { | 263 { |
263 mockFileSystem->success = false; | 264 mockFileSystem->success = false; |
264 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 265 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
265 AdblockPlus::Sleep(50); | 266 AdblockPlus::Sleep(50); |
266 ASSERT_FALSE(jsEngine->Evaluate("result.error").IsUndefined()); | 267 ASSERT_FALSE(jsEngine->Evaluate("result.error").IsUndefined()); |
267 } | 268 } |
LEFT | RIGHT |