| 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::IFileSystem | 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 lastWrittenFile; | 
| 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 removedFile; | 
| 34     mutable std::string statPath; | 34     mutable std::string statFile; | 
| 35     bool statExists; | 35     bool statExists; | 
| 36     bool statIsDirectory; |  | 
| 37     bool statIsFile; |  | 
| 38     int statLastModified; | 36     int statLastModified; | 
| 39 | 37 | 
| 40     MockFileSystem() : success(true) | 38     MockFileSystem() : success(true) | 
| 41     { | 39     { | 
| 42     } | 40     } | 
| 43 | 41 | 
| 44     void Read(const std::string& path, const ReadCallback& callback) const overr
     ide | 42     void Read(const std::string& fileName, const ReadCallback& callback) const o
     verride | 
| 45     { | 43     { | 
| 46       if (!success) | 44       if (!success) | 
| 47       { | 45       { | 
| 48         callback(IOBuffer(), "Unable to read " + path); | 46         callback(IOBuffer(), "Unable to read " + fileName); | 
| 49         return; | 47         return; | 
| 50       } | 48       } | 
| 51       callback(IOBuffer(contentToRead), ""); | 49       callback(IOBuffer(contentToRead), ""); | 
| 52     } | 50     } | 
| 53 | 51 | 
| 54     void Write(const std::string& path, const IOBuffer& data, | 52     void Write(const std::string& fileName, const IOBuffer& data, | 
| 55                const Callback& callback) override | 53                const Callback& callback) override | 
| 56     { | 54     { | 
| 57       if (!success) | 55       if (!success) | 
| 58       { | 56       { | 
| 59         callback("Unable to write to " + path); | 57         callback("Unable to write to " + fileName); | 
| 60         return; | 58         return; | 
| 61       } | 59       } | 
| 62       lastWrittenPath = path; | 60       lastWrittenFile = fileName; | 
| 63 | 61 | 
| 64       lastWrittenContent = data; | 62       lastWrittenContent = data; | 
| 65       callback(""); | 63       callback(""); | 
| 66     } | 64     } | 
| 67 | 65 | 
| 68     void Move(const std::string& fromPath, const std::string& toPath, | 66     void Move(const std::string& fromFileName, const std::string& toFileName, | 
| 69               const Callback& callback) override | 67               const Callback& callback) override | 
| 70     { | 68     { | 
| 71       if (!success) | 69       if (!success) | 
| 72       { | 70       { | 
| 73         callback("Unable to move " + fromPath + " to " + toPath); | 71         callback("Unable to move " + fromFileName + " to " + toFileName); | 
| 74         return; | 72         return; | 
| 75       } | 73       } | 
| 76       movedFrom = fromPath; | 74       movedFrom = fromFileName; | 
| 77       movedTo = toPath; | 75       movedTo = toFileName; | 
| 78       callback(""); | 76       callback(""); | 
| 79     } | 77     } | 
| 80 | 78 | 
| 81     void Remove(const std::string& path, const Callback& callback) override | 79     void Remove(const std::string& fileName, const Callback& callback) override | 
| 82     { | 80     { | 
| 83       if (!success) | 81       if (!success) | 
| 84       { | 82       { | 
| 85         callback("Unable to remove " + path); | 83         callback("Unable to remove " + fileName); | 
| 86         return; | 84         return; | 
| 87       } | 85       } | 
| 88       removedPath = path; | 86       removedFile = fileName; | 
| 89       callback(""); | 87       callback(""); | 
| 90     } | 88     } | 
| 91 | 89 | 
| 92     void Stat(const std::string& path, const StatCallback& callback) const overr
     ide | 90     void Stat(const std::string& fileName, const StatCallback& callback) const o
     verride | 
| 93     { | 91     { | 
| 94       StatResult result; | 92       StatResult result; | 
| 95       std::string error; | 93       std::string error; | 
| 96       if (!success) | 94       if (!success) | 
| 97         error = "Unable to stat " + path; | 95         error = "Unable to stat " + fileName; | 
| 98       else | 96       else | 
| 99       { | 97       { | 
| 100         statPath = path; | 98         statFile = fileName; | 
| 101         result.exists = statExists; | 99         result.exists = statExists; | 
| 102         result.isDirectory = statIsDirectory; |  | 
| 103         result.isFile = statIsFile; |  | 
| 104         result.lastModified = statLastModified; | 100         result.lastModified = statLastModified; | 
| 105       } | 101       } | 
| 106       callback(result, error); | 102       callback(result, error); | 
| 107     } | 103     } | 
| 108 |  | 
| 109     std::string Resolve(const std::string& path) const override |  | 
| 110     { |  | 
| 111       if (!success) |  | 
| 112         throw std::runtime_error("Unable to stat " + path); |  | 
| 113       return path; |  | 
| 114     } |  | 
| 115   }; | 104   }; | 
| 116 | 105 | 
| 117   void ReadFile(AdblockPlus::JsEngine& jsEngine, std::string& content, | 106   void ReadFile(AdblockPlus::JsEngine& jsEngine, std::string& content, | 
| 118                 std::string& error) | 107                 std::string& error) | 
| 119   { | 108   { | 
| 120     jsEngine.Evaluate("_fileSystem.read('', function(r) {result = r})"); | 109     jsEngine.Evaluate("_fileSystem.read('', function(r) {result = r})"); | 
| 121     content = jsEngine.Evaluate("result.content").AsString(); | 110     content = jsEngine.Evaluate("result.content").AsString(); | 
| 122     error = jsEngine.Evaluate("result.error").AsString(); | 111     error = jsEngine.Evaluate("result.error").AsString(); | 
| 123   } | 112   } | 
| 124 | 113 | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 162   std::string content; | 151   std::string content; | 
| 163   std::string error; | 152   std::string error; | 
| 164   ReadFile(GetJsEngine(), content, error); | 153   ReadFile(GetJsEngine(), content, error); | 
| 165   ASSERT_NE("", error); | 154   ASSERT_NE("", error); | 
| 166   ASSERT_EQ("", content); | 155   ASSERT_EQ("", content); | 
| 167 } | 156 } | 
| 168 | 157 | 
| 169 TEST_F(FileSystemJsObjectTest, Write) | 158 TEST_F(FileSystemJsObjectTest, Write) | 
| 170 { | 159 { | 
| 171   GetJsEngine().Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e
     })"); | 160   GetJsEngine().Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e
     })"); | 
| 172   ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); | 161   ASSERT_EQ("foo", mockFileSystem->lastWrittenFile); | 
| 173   ASSERT_EQ((AdblockPlus::IFileSystem::IOBuffer{'b', 'a', 'r'}), | 162   ASSERT_EQ((AdblockPlus::IFileSystem::IOBuffer{'b', 'a', 'r'}), | 
| 174             mockFileSystem->lastWrittenContent); | 163             mockFileSystem->lastWrittenContent); | 
| 175   ASSERT_TRUE(GetJsEngine().Evaluate("error").IsUndefined()); | 164   ASSERT_TRUE(GetJsEngine().Evaluate("error").IsUndefined()); | 
| 176 } | 165 } | 
| 177 | 166 | 
| 178 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) | 167 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) | 
| 179 { | 168 { | 
| 180   ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.write()")); | 169   ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.write()")); | 
| 181   ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.write('', '', '')")); | 170   ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.write('', '', '')")); | 
| 182 } | 171 } | 
| (...skipping 22 matching lines...) Expand all  Loading... | 
| 205 TEST_F(FileSystemJsObjectTest, MoveError) | 194 TEST_F(FileSystemJsObjectTest, MoveError) | 
| 206 { | 195 { | 
| 207   mockFileSystem->success = false; | 196   mockFileSystem->success = false; | 
| 208   GetJsEngine().Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e}
     )"); | 197   GetJsEngine().Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e}
     )"); | 
| 209   ASSERT_FALSE(GetJsEngine().Evaluate("error").IsUndefined()); | 198   ASSERT_FALSE(GetJsEngine().Evaluate("error").IsUndefined()); | 
| 210 } | 199 } | 
| 211 | 200 | 
| 212 TEST_F(FileSystemJsObjectTest, Remove) | 201 TEST_F(FileSystemJsObjectTest, Remove) | 
| 213 { | 202 { | 
| 214   GetJsEngine().Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 203   GetJsEngine().Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 
| 215   ASSERT_EQ("foo", mockFileSystem->removedPath); | 204   ASSERT_EQ("foo", mockFileSystem->removedFile); | 
| 216   ASSERT_TRUE(GetJsEngine().Evaluate("error").IsUndefined()); | 205   ASSERT_TRUE(GetJsEngine().Evaluate("error").IsUndefined()); | 
| 217 } | 206 } | 
| 218 | 207 | 
| 219 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) | 208 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) | 
| 220 { | 209 { | 
| 221   ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.remove()")); | 210   ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.remove()")); | 
| 222   ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.remove('', '')")); | 211   ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.remove('', '')")); | 
| 223 } | 212 } | 
| 224 | 213 | 
| 225 TEST_F(FileSystemJsObjectTest, RemoveError) | 214 TEST_F(FileSystemJsObjectTest, RemoveError) | 
| 226 { | 215 { | 
| 227   mockFileSystem->success = false; | 216   mockFileSystem->success = false; | 
| 228   GetJsEngine().Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 217   GetJsEngine().Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 
| 229   ASSERT_NE("", GetJsEngine().Evaluate("error").AsString()); | 218   ASSERT_NE("", GetJsEngine().Evaluate("error").AsString()); | 
| 230 } | 219 } | 
| 231 | 220 | 
| 232 TEST_F(FileSystemJsObjectTest, Stat) | 221 TEST_F(FileSystemJsObjectTest, Stat) | 
| 233 { | 222 { | 
| 234   mockFileSystem->statExists = true; | 223   mockFileSystem->statExists = true; | 
| 235   mockFileSystem->statIsDirectory= false; |  | 
| 236   mockFileSystem->statIsFile = true; |  | 
| 237   mockFileSystem->statLastModified = 1337; | 224   mockFileSystem->statLastModified = 1337; | 
| 238   GetJsEngine().Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 225   GetJsEngine().Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 
| 239   ASSERT_EQ("foo", mockFileSystem->statPath); | 226   ASSERT_EQ("foo", mockFileSystem->statFile); | 
| 240   ASSERT_TRUE(GetJsEngine().Evaluate("result.error").IsUndefined()); | 227   ASSERT_TRUE(GetJsEngine().Evaluate("result.error").IsUndefined()); | 
| 241   ASSERT_TRUE(GetJsEngine().Evaluate("result.exists").AsBool()); | 228   ASSERT_TRUE(GetJsEngine().Evaluate("result.exists").AsBool()); | 
| 242   ASSERT_FALSE(GetJsEngine().Evaluate("result.isDirectory").AsBool()); |  | 
| 243   ASSERT_TRUE(GetJsEngine().Evaluate("result.isFile").AsBool()); |  | 
| 244   ASSERT_EQ(1337, GetJsEngine().Evaluate("result.lastModified").AsInt()); | 229   ASSERT_EQ(1337, GetJsEngine().Evaluate("result.lastModified").AsInt()); | 
| 245 } | 230 } | 
| 246 | 231 | 
| 247 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) | 232 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) | 
| 248 { | 233 { | 
| 249   ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.stat()")); | 234   ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.stat()")); | 
| 250   ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.stat('', '')")); | 235   ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.stat('', '')")); | 
| 251 } | 236 } | 
| 252 | 237 | 
| 253 TEST_F(FileSystemJsObjectTest, StatError) | 238 TEST_F(FileSystemJsObjectTest, StatError) | 
| 254 { | 239 { | 
| 255   mockFileSystem->success = false; | 240   mockFileSystem->success = false; | 
| 256   GetJsEngine().Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 241   GetJsEngine().Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 
| 257   ASSERT_FALSE(GetJsEngine().Evaluate("result.error").IsUndefined()); | 242   ASSERT_FALSE(GetJsEngine().Evaluate("result.error").IsUndefined()); | 
| 258 } | 243 } | 
| OLD | NEW | 
|---|