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 "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 IOBuffer contentToRead; | 28 IOBuffer contentToRead; |
29 std::string lastWrittenPath; | 29 std::string lastWrittenPath; |
30 IOBuffer 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 IOBuffer 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 return contentToRead; | 48 callback(IOBuffer(), "Unable to read " + path); |
| 49 return; |
| 50 } |
| 51 callback(IOBuffer(contentToRead), ""); |
49 } | 52 } |
50 | 53 |
51 void Write(const std::string& path, const IOBuffer& data) | 54 void Write(const std::string& path, const IOBuffer& data, |
| 55 const Callback& callback) |
52 { | 56 { |
53 if (!success) | 57 if (!success) |
54 throw std::runtime_error("Unable to write to " + path); | 58 { |
| 59 callback("Unable to write to " + path); |
| 60 return; |
| 61 } |
55 lastWrittenPath = path; | 62 lastWrittenPath = path; |
| 63 |
56 lastWrittenContent = data; | 64 lastWrittenContent = data; |
| 65 callback(""); |
57 } | 66 } |
58 | 67 |
59 void Move(const std::string& fromPath, const std::string& toPath) | 68 void Move(const std::string& fromPath, const std::string& toPath, |
| 69 const Callback& callback) |
60 { | 70 { |
61 if (!success) | 71 if (!success) |
62 throw std::runtime_error("Unable to move " + fromPath + " to " | 72 { |
63 + toPath); | 73 callback("Unable to move " + fromPath + " to " + toPath); |
| 74 return; |
| 75 } |
64 movedFrom = fromPath; | 76 movedFrom = fromPath; |
65 movedTo = toPath; | 77 movedTo = toPath; |
| 78 callback(""); |
66 } | 79 } |
67 | 80 |
68 void Remove(const std::string& path) | 81 void Remove(const std::string& path, const Callback& callback) |
69 { | 82 { |
70 if (!success) | 83 if (!success) |
71 throw std::runtime_error("Unable to remove " + path); | 84 { |
| 85 callback("Unable to remove " + path); |
| 86 return; |
| 87 } |
72 removedPath = path; | 88 removedPath = path; |
| 89 callback(""); |
73 } | 90 } |
74 | 91 |
75 StatResult Stat(const std::string& path) const | 92 void Stat(const std::string& path, |
| 93 const StatCallback& callback) const |
76 { | 94 { |
| 95 StatResult result; |
| 96 std::string error; |
77 if (!success) | 97 if (!success) |
78 throw std::runtime_error("Unable to stat " + path); | 98 error = "Unable to stat " + path; |
79 statPath = path; | 99 else |
80 StatResult result; | 100 { |
81 result.exists = statExists; | 101 statPath = path; |
82 result.isDirectory = statIsDirectory; | 102 result.exists = statExists; |
83 result.isFile = statIsFile; | 103 result.isDirectory = statIsDirectory; |
84 result.lastModified = statLastModified; | 104 result.isFile = statIsFile; |
85 return result; | 105 result.lastModified = statLastModified; |
| 106 } |
| 107 callback(result, error); |
86 } | 108 } |
87 | 109 |
88 std::string Resolve(const std::string& path) const | 110 std::string Resolve(const std::string& path) const |
89 { | 111 { |
90 if (!success) | 112 if (!success) |
91 throw std::runtime_error("Unable to stat " + path); | 113 throw std::runtime_error("Unable to stat " + path); |
92 return path; | 114 return path; |
93 } | 115 } |
94 }; | 116 }; |
95 | 117 |
96 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, | 118 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, |
97 std::string& error) | 119 std::string& error) |
98 { | 120 { |
99 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); | 121 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); |
100 AdblockPlus::Sleep(50); | 122 AdblockPlus::Sleep(50); |
101 content = jsEngine->Evaluate("result.content").AsString(); | 123 content = jsEngine->Evaluate("result.content").AsString(); |
102 error = jsEngine->Evaluate("result.error").AsString(); | 124 error = jsEngine->Evaluate("result.error").AsString(); |
103 } | 125 } |
104 | 126 |
105 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; | 127 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; |
106 | 128 |
107 class FileSystemJsObjectTest : public BaseJsTest | 129 class FileSystemJsObjectTest : public BaseJsTest |
108 { | 130 { |
109 protected: | 131 protected: |
110 MockFileSystemPtr mockFileSystem; | 132 MockFileSystemPtr mockFileSystem; |
111 | 133 |
112 void SetUp() | 134 void SetUp() |
113 { | 135 { |
114 BaseJsTest::SetUp(); | |
115 mockFileSystem = MockFileSystemPtr(new MockFileSystem); | 136 mockFileSystem = MockFileSystemPtr(new MockFileSystem); |
116 jsEngine->SetFileSystem(mockFileSystem); | 137 JsEngineCreationParameters params; |
| 138 params.fileSystem = mockFileSystem; |
| 139 jsEngine = CreateJsEngine(std::move(params)); |
117 } | 140 } |
118 }; | 141 }; |
119 } | 142 } |
120 | 143 |
121 TEST_F(FileSystemJsObjectTest, Read) | 144 TEST_F(FileSystemJsObjectTest, Read) |
122 { | 145 { |
123 mockFileSystem->contentToRead = | 146 mockFileSystem->contentToRead = |
124 AdblockPlus::FileSystem::IOBuffer{'f', 'o', 'o'}; | 147 AdblockPlus::IFileSystem::IOBuffer{'f', 'o', 'o'}; |
125 std::string content; | 148 std::string content; |
126 std::string error; | 149 std::string error; |
127 ReadFile(jsEngine, content, error); | 150 ReadFile(jsEngine, content, error); |
128 ASSERT_EQ("foo", content); | 151 ASSERT_EQ("foo", content); |
129 ASSERT_EQ("", error); | 152 ASSERT_EQ("undefined", error); |
130 } | 153 } |
131 | 154 |
132 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments) | 155 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments) |
133 { | 156 { |
134 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); | 157 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); |
135 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); | 158 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); |
136 } | 159 } |
137 | 160 |
138 TEST_F(FileSystemJsObjectTest, ReadError) | 161 TEST_F(FileSystemJsObjectTest, ReadError) |
139 { | 162 { |
140 mockFileSystem->success = false; | 163 mockFileSystem->success = false; |
141 std::string content; | 164 std::string content; |
142 std::string error; | 165 std::string error; |
143 ReadFile(jsEngine, content, error); | 166 ReadFile(jsEngine, content, error); |
144 ASSERT_NE("", error); | 167 ASSERT_NE("", error); |
145 ASSERT_EQ("", content); | 168 ASSERT_EQ("", content); |
146 } | 169 } |
147 | 170 |
148 TEST_F(FileSystemJsObjectTest, Write) | 171 TEST_F(FileSystemJsObjectTest, Write) |
149 { | 172 { |
150 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; | 173 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; |
151 AdblockPlus::Sleep(50); | 174 AdblockPlus::Sleep(50); |
152 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); | 175 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); |
153 ASSERT_EQ((AdblockPlus::FileSystem::IOBuffer{'b', 'a', 'r'}), | 176 ASSERT_EQ((AdblockPlus::IFileSystem::IOBuffer{'b', 'a', 'r'}), |
154 mockFileSystem->lastWrittenContent); | 177 mockFileSystem->lastWrittenContent); |
155 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); | 178 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
156 } | 179 } |
157 | 180 |
158 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) | 181 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) |
159 { | 182 { |
160 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); | 183 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); |
161 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); | 184 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); |
162 } | 185 } |
163 | 186 |
164 TEST_F(FileSystemJsObjectTest, WriteError) | 187 TEST_F(FileSystemJsObjectTest, WriteError) |
165 { | 188 { |
166 mockFileSystem->success = false; | 189 mockFileSystem->success = false; |
167 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; | 190 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; |
168 AdblockPlus::Sleep(50); | 191 AdblockPlus::Sleep(50); |
169 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); | 192 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); |
170 } | 193 } |
171 | 194 |
172 TEST_F(FileSystemJsObjectTest, Move) | 195 TEST_F(FileSystemJsObjectTest, Move) |
173 { | 196 { |
174 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 197 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
175 AdblockPlus::Sleep(50); | 198 AdblockPlus::Sleep(50); |
176 ASSERT_EQ("foo", mockFileSystem->movedFrom); | 199 ASSERT_EQ("foo", mockFileSystem->movedFrom); |
177 ASSERT_EQ("bar", mockFileSystem->movedTo); | 200 ASSERT_EQ("bar", mockFileSystem->movedTo); |
178 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); | 201 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
179 } | 202 } |
180 | 203 |
181 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) | 204 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) |
182 { | 205 { |
183 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); | 206 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); |
184 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); | 207 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); |
185 } | 208 } |
186 | 209 |
187 TEST_F(FileSystemJsObjectTest, MoveError) | 210 TEST_F(FileSystemJsObjectTest, MoveError) |
188 { | 211 { |
189 mockFileSystem->success = false; | 212 mockFileSystem->success = false; |
190 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 213 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
191 AdblockPlus::Sleep(50); | 214 AdblockPlus::Sleep(50); |
192 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); | 215 ASSERT_FALSE(jsEngine->Evaluate("error").IsUndefined()); |
193 } | 216 } |
194 | 217 |
195 TEST_F(FileSystemJsObjectTest, Remove) | 218 TEST_F(FileSystemJsObjectTest, Remove) |
196 { | 219 { |
197 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 220 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
198 AdblockPlus::Sleep(50); | 221 AdblockPlus::Sleep(50); |
199 ASSERT_EQ("foo", mockFileSystem->removedPath); | 222 ASSERT_EQ("foo", mockFileSystem->removedPath); |
200 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); | 223 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
201 } | 224 } |
202 | 225 |
203 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) | 226 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) |
204 { | 227 { |
205 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); | 228 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); |
206 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); | 229 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); |
207 } | 230 } |
208 | 231 |
209 TEST_F(FileSystemJsObjectTest, RemoveError) | 232 TEST_F(FileSystemJsObjectTest, RemoveError) |
210 { | 233 { |
211 mockFileSystem->success = false; | 234 mockFileSystem->success = false; |
212 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 235 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
213 AdblockPlus::Sleep(50); | 236 AdblockPlus::Sleep(50); |
214 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); | 237 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); |
215 } | 238 } |
216 | 239 |
217 TEST_F(FileSystemJsObjectTest, Stat) | 240 TEST_F(FileSystemJsObjectTest, Stat) |
218 { | 241 { |
219 mockFileSystem->statExists = true; | 242 mockFileSystem->statExists = true; |
220 mockFileSystem->statIsDirectory= false; | 243 mockFileSystem->statIsDirectory= false; |
221 mockFileSystem->statIsFile = true; | 244 mockFileSystem->statIsFile = true; |
222 mockFileSystem->statLastModified = 1337; | 245 mockFileSystem->statLastModified = 1337; |
223 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 246 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
224 AdblockPlus::Sleep(50); | 247 AdblockPlus::Sleep(50); |
225 ASSERT_EQ("foo", mockFileSystem->statPath); | 248 ASSERT_EQ("foo", mockFileSystem->statPath); |
226 ASSERT_EQ("", jsEngine->Evaluate("result.error").AsString()); | 249 ASSERT_TRUE(jsEngine->Evaluate("result.error").IsUndefined()); |
227 ASSERT_TRUE(jsEngine->Evaluate("result.exists").AsBool()); | 250 ASSERT_TRUE(jsEngine->Evaluate("result.exists").AsBool()); |
228 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory").AsBool()); | 251 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory").AsBool()); |
229 ASSERT_TRUE(jsEngine->Evaluate("result.isFile").AsBool()); | 252 ASSERT_TRUE(jsEngine->Evaluate("result.isFile").AsBool()); |
230 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified").AsInt()); | 253 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified").AsInt()); |
231 } | 254 } |
232 | 255 |
233 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) | 256 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) |
234 { | 257 { |
235 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); | 258 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); |
236 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); | 259 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); |
237 } | 260 } |
238 | 261 |
239 TEST_F(FileSystemJsObjectTest, StatError) | 262 TEST_F(FileSystemJsObjectTest, StatError) |
240 { | 263 { |
241 mockFileSystem->success = false; | 264 mockFileSystem->success = false; |
242 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 265 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
243 AdblockPlus::Sleep(50); | 266 AdblockPlus::Sleep(50); |
244 ASSERT_NE("", jsEngine->Evaluate("result.error").AsString()); | 267 ASSERT_FALSE(jsEngine->Evaluate("result.error").IsUndefined()); |
245 } | 268 } |
OLD | NEW |