| 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     std::string contentToRead; | 28     std::string contentToRead; | 
| 29     std::string lastWrittenPath; | 29     std::string lastWrittenPath; | 
| 30     std::string lastWrittenContent; | 30     std::string 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("", "Unable to read " + path); | 
| 49       *stream << contentToRead; | 49         return; | 
| 50       return std::shared_ptr<std::istream>(stream); | 50       } | 
|  | 51       callback(std::string(contentToRead), ""); | 
| 51     } | 52     } | 
| 52 | 53 | 
| 53     void Write(const std::string& path, std::istream& data) | 54     void Write(const std::string& path, const std::string& data, | 
|  | 55                const Callback& callback) | 
| 54     { | 56     { | 
| 55       if (!success) | 57       if (!success) | 
| 56         throw std::runtime_error("Unable to write to " + path); | 58       { | 
|  | 59         callback("Unable to write to " + path); | 
|  | 60         return; | 
|  | 61       } | 
| 57       lastWrittenPath = path; | 62       lastWrittenPath = path; | 
| 58 | 63 | 
| 59       std::stringstream content; | 64       lastWrittenContent = data; | 
| 60       content << data.rdbuf(); | 65       callback(""); | 
| 61       lastWrittenContent = content.str(); |  | 
| 62     } | 66     } | 
| 63 | 67 | 
| 64     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) | 
| 65     { | 70     { | 
| 66       if (!success) | 71       if (!success) | 
| 67         throw std::runtime_error("Unable to move " + fromPath + " to " | 72       { | 
| 68                                  + toPath); | 73         callback("Unable to move " + fromPath + " to " + toPath); | 
|  | 74         return; | 
|  | 75       } | 
| 69       movedFrom = fromPath; | 76       movedFrom = fromPath; | 
| 70       movedTo = toPath; | 77       movedTo = toPath; | 
|  | 78       callback(""); | 
| 71     } | 79     } | 
| 72 | 80 | 
| 73     void Remove(const std::string& path) | 81     void Remove(const std::string& path, const Callback& callback) | 
| 74     { | 82     { | 
| 75       if (!success) | 83       if (!success) | 
| 76         throw std::runtime_error("Unable to remove " + path); | 84       { | 
|  | 85         callback("Unable to remove " + path); | 
|  | 86         return; | 
|  | 87       } | 
| 77       removedPath = path; | 88       removedPath = path; | 
|  | 89       callback(""); | 
| 78     } | 90     } | 
| 79 | 91 | 
| 80     StatResult Stat(const std::string& path) const | 92     void Stat(const std::string& path, | 
|  | 93               const StatCallback& callback) const | 
| 81     { | 94     { | 
|  | 95       StatResult result; | 
|  | 96       std::string error; | 
| 82       if (!success) | 97       if (!success) | 
| 83         throw std::runtime_error("Unable to stat " + path); | 98         error = "Unable to stat " + path; | 
| 84       statPath = path; | 99       else | 
| 85       StatResult result; | 100       { | 
| 86       result.exists = statExists; | 101         statPath = path; | 
| 87       result.isDirectory = statIsDirectory; | 102         result.exists = statExists; | 
| 88       result.isFile = statIsFile; | 103         result.isDirectory = statIsDirectory; | 
| 89       result.lastModified = statLastModified; | 104         result.isFile = statIsFile; | 
| 90       return result; | 105         result.lastModified = statLastModified; | 
|  | 106       } | 
|  | 107       callback(result, error); | 
| 91     } | 108     } | 
| 92 | 109 | 
| 93     std::string Resolve(const std::string& path) const | 110     std::string Resolve(const std::string& path) const | 
| 94     { | 111     { | 
| 95       if (!success) | 112       if (!success) | 
| 96         throw std::runtime_error("Unable to stat " + path); | 113         throw std::runtime_error("Unable to stat " + path); | 
| 97       return path; | 114       return path; | 
| 98     } | 115     } | 
| 99   }; | 116   }; | 
| 100 | 117 | 
| 101   void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, | 118   void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, | 
| 102                 std::string& error) | 119                 std::string& error) | 
| 103   { | 120   { | 
| 104     jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); | 121     jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); | 
| 105     AdblockPlus::Sleep(50); | 122     AdblockPlus::Sleep(50); | 
| 106     content = jsEngine->Evaluate("result.content").AsString(); | 123     content = jsEngine->Evaluate("result.content").AsString(); | 
| 107     error = jsEngine->Evaluate("result.error").AsString(); | 124     error = jsEngine->Evaluate("result.error").AsString(); | 
| 108   } | 125   } | 
| 109 | 126 | 
| 110   typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; | 127   typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; | 
| 111 | 128 | 
| 112   class FileSystemJsObjectTest : public BaseJsTest | 129   class FileSystemJsObjectTest : public BaseJsTest | 
| 113   { | 130   { | 
| 114   protected: | 131   protected: | 
| 115     MockFileSystemPtr mockFileSystem; | 132     MockFileSystemPtr mockFileSystem; | 
| 116 | 133 | 
| 117     void SetUp() | 134     void SetUp() | 
| 118     { | 135     { | 
| 119       BaseJsTest::SetUp(); |  | 
| 120       mockFileSystem = MockFileSystemPtr(new MockFileSystem); | 136       mockFileSystem = MockFileSystemPtr(new MockFileSystem); | 
| 121       jsEngine->SetFileSystem(mockFileSystem); | 137       JsEngineCreationParameters params; | 
|  | 138       params.fileSystem = mockFileSystem; | 
|  | 139       jsEngine = CreateJsEngine(std::move(params)); | 
| 122     } | 140     } | 
| 123   }; | 141   }; | 
| 124 } | 142 } | 
| 125 | 143 | 
| 126 TEST_F(FileSystemJsObjectTest, Read) | 144 TEST_F(FileSystemJsObjectTest, Read) | 
| 127 { | 145 { | 
| 128   mockFileSystem->contentToRead = "foo"; | 146   mockFileSystem->contentToRead = "foo"; | 
| 129   std::string content; | 147   std::string content; | 
| 130   std::string error; | 148   std::string error; | 
| 131   ReadFile(jsEngine, content, error); | 149   ReadFile(jsEngine, content, error); | 
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 239   ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); | 257   ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); | 
| 240 } | 258 } | 
| 241 | 259 | 
| 242 TEST_F(FileSystemJsObjectTest, StatError) | 260 TEST_F(FileSystemJsObjectTest, StatError) | 
| 243 { | 261 { | 
| 244   mockFileSystem->success = false; | 262   mockFileSystem->success = false; | 
| 245   jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 263   jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 
| 246   AdblockPlus::Sleep(50); | 264   AdblockPlus::Sleep(50); | 
| 247   ASSERT_NE("", jsEngine->Evaluate("result.error").AsString()); | 265   ASSERT_NE("", jsEngine->Evaluate("result.error").AsString()); | 
| 248 } | 266 } | 
| OLD | NEW | 
|---|