| 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::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 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("", "Unable to read " + path); | 48 callback(IOBuffer(), "Unable to read " + path); |
| 49 return; | 49 return; |
| 50 } | 50 } |
| 51 callback(std::string(contentToRead), ""); | 51 callback(IOBuffer(contentToRead), ""); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void Write(const std::string& path, const std::string& 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 = "foo"; | 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("", 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("bar", mockFileSystem->lastWrittenContent); | 176 ASSERT_EQ((AdblockPlus::IFileSystem::IOBuffer{'b', 'a', 'r'}), |
| 176 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); | 177 mockFileSystem->lastWrittenContent); |
| 178 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
| 177 } | 179 } |
| 178 | 180 |
| 179 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) | 181 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) |
| 180 { | 182 { |
| 181 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); | 183 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); |
| 182 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); | 184 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); |
| 183 } | 185 } |
| 184 | 186 |
| 185 TEST_F(FileSystemJsObjectTest, WriteError) | 187 TEST_F(FileSystemJsObjectTest, WriteError) |
| 186 { | 188 { |
| 187 mockFileSystem->success = false; | 189 mockFileSystem->success = false; |
| 188 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; | 190 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; |
| 189 AdblockPlus::Sleep(50); | 191 AdblockPlus::Sleep(50); |
| 190 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); | 192 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); |
| 191 } | 193 } |
| 192 | 194 |
| 193 TEST_F(FileSystemJsObjectTest, Move) | 195 TEST_F(FileSystemJsObjectTest, Move) |
| 194 { | 196 { |
| 195 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 197 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
| 196 AdblockPlus::Sleep(50); | 198 AdblockPlus::Sleep(50); |
| 197 ASSERT_EQ("foo", mockFileSystem->movedFrom); | 199 ASSERT_EQ("foo", mockFileSystem->movedFrom); |
| 198 ASSERT_EQ("bar", mockFileSystem->movedTo); | 200 ASSERT_EQ("bar", mockFileSystem->movedTo); |
| 199 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); | 201 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
| 200 } | 202 } |
| 201 | 203 |
| 202 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) | 204 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) |
| 203 { | 205 { |
| 204 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); | 206 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); |
| 205 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); | 207 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); |
| 206 } | 208 } |
| 207 | 209 |
| 208 TEST_F(FileSystemJsObjectTest, MoveError) | 210 TEST_F(FileSystemJsObjectTest, MoveError) |
| 209 { | 211 { |
| 210 mockFileSystem->success = false; | 212 mockFileSystem->success = false; |
| 211 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 213 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
| 212 AdblockPlus::Sleep(50); | 214 AdblockPlus::Sleep(50); |
| 213 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); | 215 ASSERT_FALSE(jsEngine->Evaluate("error").IsUndefined()); |
| 214 } | 216 } |
| 215 | 217 |
| 216 TEST_F(FileSystemJsObjectTest, Remove) | 218 TEST_F(FileSystemJsObjectTest, Remove) |
| 217 { | 219 { |
| 218 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 220 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
| 219 AdblockPlus::Sleep(50); | 221 AdblockPlus::Sleep(50); |
| 220 ASSERT_EQ("foo", mockFileSystem->removedPath); | 222 ASSERT_EQ("foo", mockFileSystem->removedPath); |
| 221 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); | 223 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
| 222 } | 224 } |
| 223 | 225 |
| 224 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) | 226 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) |
| 225 { | 227 { |
| 226 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); | 228 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); |
| 227 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); | 229 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); |
| 228 } | 230 } |
| 229 | 231 |
| 230 TEST_F(FileSystemJsObjectTest, RemoveError) | 232 TEST_F(FileSystemJsObjectTest, RemoveError) |
| 231 { | 233 { |
| 232 mockFileSystem->success = false; | 234 mockFileSystem->success = false; |
| 233 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 235 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
| 234 AdblockPlus::Sleep(50); | 236 AdblockPlus::Sleep(50); |
| 235 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); | 237 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); |
| 236 } | 238 } |
| 237 | 239 |
| 238 TEST_F(FileSystemJsObjectTest, Stat) | 240 TEST_F(FileSystemJsObjectTest, Stat) |
| 239 { | 241 { |
| 240 mockFileSystem->statExists = true; | 242 mockFileSystem->statExists = true; |
| 241 mockFileSystem->statIsDirectory= false; | 243 mockFileSystem->statIsDirectory= false; |
| 242 mockFileSystem->statIsFile = true; | 244 mockFileSystem->statIsFile = true; |
| 243 mockFileSystem->statLastModified = 1337; | 245 mockFileSystem->statLastModified = 1337; |
| 244 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 246 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
| 245 AdblockPlus::Sleep(50); | 247 AdblockPlus::Sleep(50); |
| 246 ASSERT_EQ("foo", mockFileSystem->statPath); | 248 ASSERT_EQ("foo", mockFileSystem->statPath); |
| 247 ASSERT_EQ("", jsEngine->Evaluate("result.error").AsString()); | 249 ASSERT_TRUE(jsEngine->Evaluate("result.error").IsUndefined()); |
| 248 ASSERT_TRUE(jsEngine->Evaluate("result.exists").AsBool()); | 250 ASSERT_TRUE(jsEngine->Evaluate("result.exists").AsBool()); |
| 249 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory").AsBool()); | 251 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory").AsBool()); |
| 250 ASSERT_TRUE(jsEngine->Evaluate("result.isFile").AsBool()); | 252 ASSERT_TRUE(jsEngine->Evaluate("result.isFile").AsBool()); |
| 251 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified").AsInt()); | 253 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified").AsInt()); |
| 252 } | 254 } |
| 253 | 255 |
| 254 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) | 256 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) |
| 255 { | 257 { |
| 256 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); | 258 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); |
| 257 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); | 259 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); |
| 258 } | 260 } |
| 259 | 261 |
| 260 TEST_F(FileSystemJsObjectTest, StatError) | 262 TEST_F(FileSystemJsObjectTest, StatError) |
| 261 { | 263 { |
| 262 mockFileSystem->success = false; | 264 mockFileSystem->success = false; |
| 263 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 265 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
| 264 AdblockPlus::Sleep(50); | 266 AdblockPlus::Sleep(50); |
| 265 ASSERT_NE("", jsEngine->Evaluate("result.error").AsString()); | 267 ASSERT_FALSE(jsEngine->Evaluate("result.error").IsUndefined()); |
| 266 } | 268 } |
| LEFT | RIGHT |