| 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 <AdblockPlus.h> |   19 #include <AdblockPlus.h> | 
|   20 #include <gtest/gtest.h> |   20 #include <gtest/gtest.h> | 
|   21  |   21  | 
|   22 #include "BaseJsTest.h" |   22 #include "BaseJsTest.h" | 
|   23  |   23  | 
|   24 using AdblockPlus::IFileSystem; |   24 using AdblockPlus::IFileSystem; | 
|   25 using AdblockPlus::FileSystemPtr; |   25 using AdblockPlus::FileSystemPtr; | 
|   26 using AdblockPlus::SchedulerTask; |   26 using AdblockPlus::SchedulerTask; | 
|   27  |   27  | 
|   28 namespace |   28 namespace | 
|   29 { |   29 { | 
|   30   const std::string testPath = "libadblockplus-t\xc3\xa4st-file"; |   30   const std::string testFileName = "libadblockplus-t\xc3\xa4st-file"; | 
|   31  |   31  | 
|   32   class DefaultFileSystemTest : public ::testing::Test |   32   class DefaultFileSystemTest : public ::testing::Test | 
|   33   { |   33   { | 
|   34   public: |   34   public: | 
|   35     void SetUp() override |   35     void SetUp() override | 
|   36     { |   36     { | 
|   37       fileSystem = AdblockPlus::CreateDefaultFileSystem([this](const SchedulerTa
     sk& task) |   37       fileSystem = AdblockPlus::CreateDefaultFileSystem([this](const SchedulerTa
     sk& task) | 
|   38       { |   38       { | 
|   39         fileSystemTasks.emplace_back(task); |   39         fileSystemTasks.emplace_back(task); | 
|   40       }); |   40       }); | 
|   41     } |   41     } | 
|   42   protected: |   42   protected: | 
|   43     void WriteString(const std::string& content) |   43     void WriteString(const std::string& content) | 
|   44     { |   44     { | 
|   45       bool hasRun = false; |   45       bool hasRun = false; | 
|   46       fileSystem->Write(testPath, |   46       fileSystem->Write(testFileName, | 
|   47         IFileSystem::IOBuffer(content.cbegin(), content.cend()), |   47         IFileSystem::IOBuffer(content.cbegin(), content.cend()), | 
|   48         [&hasRun](const std::string& error) |   48         [&hasRun](const std::string& error) | 
|   49       { |   49       { | 
|   50         EXPECT_TRUE(error.empty()) << error; |   50         EXPECT_TRUE(error.empty()) << error; | 
|   51         hasRun = true; |   51         hasRun = true; | 
|   52       }); |   52       }); | 
|   53       EXPECT_FALSE(hasRun); |   53       EXPECT_FALSE(hasRun); | 
|   54       PumpTask(); |   54       PumpTask(); | 
|   55       EXPECT_TRUE(hasRun); |   55       EXPECT_TRUE(hasRun); | 
|   56     } |   56     } | 
|   57  |   57  | 
|   58     void PumpTask() |   58     void PumpTask() | 
|   59     { |   59     { | 
|   60       ASSERT_EQ(1u, fileSystemTasks.size()); |   60       ASSERT_EQ(1u, fileSystemTasks.size()); | 
|   61       (*fileSystemTasks.begin())(); |   61       (*fileSystemTasks.begin())(); | 
|   62       fileSystemTasks.pop_front(); |   62       fileSystemTasks.pop_front(); | 
|   63     } |   63     } | 
|   64  |   64  | 
|   65     std::list<SchedulerTask> fileSystemTasks; |   65     std::list<SchedulerTask> fileSystemTasks; | 
|   66     FileSystemPtr fileSystem; |   66     FileSystemPtr fileSystem; | 
|   67   }; |   67   }; | 
|   68 } |   68 } | 
|   69  |   69  | 
|   70 TEST_F(DefaultFileSystemTest, WriteReadRemove) |   70 TEST_F(DefaultFileSystemTest, WriteReadRemove) | 
|   71 { |   71 { | 
|   72   WriteString("foo"); |   72   WriteString("foo"); | 
|   73  |   73  | 
|   74   bool hasReadRun = false; |   74   bool hasReadRun = false; | 
|   75   fileSystem->Read(testPath, |   75   fileSystem->Read(testFileName, | 
|   76     [this, &hasReadRun](IFileSystem::IOBuffer&& content, const std::string& erro
     r) |   76     [this, &hasReadRun](IFileSystem::IOBuffer&& content, const std::string& erro
     r) | 
|   77   { |   77   { | 
|   78     EXPECT_TRUE(error.empty()); |   78     EXPECT_TRUE(error.empty()); | 
|   79     EXPECT_EQ("foo", std::string(content.cbegin(), content.cend())); |   79     EXPECT_EQ("foo", std::string(content.cbegin(), content.cend())); | 
|   80     hasReadRun = true; |   80     hasReadRun = true; | 
|   81   }); |   81   }); | 
|   82   EXPECT_FALSE(hasReadRun); |   82   EXPECT_FALSE(hasReadRun); | 
|   83   PumpTask(); |   83   PumpTask(); | 
|   84   EXPECT_TRUE(hasReadRun); |   84   EXPECT_TRUE(hasReadRun); | 
|   85  |   85  | 
|   86   bool hasRemoveRun = false; |   86   bool hasRemoveRun = false; | 
|   87   fileSystem->Remove(testPath, [&hasRemoveRun](const std::string& error) |   87   fileSystem->Remove(testFileName, [&hasRemoveRun](const std::string& error) | 
|   88   { |   88   { | 
|   89     EXPECT_TRUE(error.empty()); |   89     EXPECT_TRUE(error.empty()); | 
|   90     hasRemoveRun = true; |   90     hasRemoveRun = true; | 
|   91   }); |   91   }); | 
|   92   EXPECT_FALSE(hasRemoveRun); |   92   EXPECT_FALSE(hasRemoveRun); | 
|   93   PumpTask(); |   93   PumpTask(); | 
|   94   EXPECT_TRUE(hasRemoveRun); |   94   EXPECT_TRUE(hasRemoveRun); | 
|   95 } |   95 } | 
|   96  |   96  | 
|   97 TEST_F(DefaultFileSystemTest, StatWorkingDirectory) |   97 TEST_F(DefaultFileSystemTest, StatWorkingDirectory) | 
|   98 { |   98 { | 
|   99   bool hasStatRun = false; |   99   bool hasStatRun = false; | 
|  100   fileSystem->Stat(".", |  100   fileSystem->Stat(".", | 
|  101     [&hasStatRun](const IFileSystem::StatResult result, const std::string& error
     ) |  101     [&hasStatRun](const IFileSystem::StatResult result, const std::string& error
     ) | 
|  102     { |  102     { | 
|  103       EXPECT_TRUE(error.empty()); |  103       EXPECT_TRUE(error.empty()); | 
|  104       ASSERT_TRUE(result.exists); |  104       ASSERT_TRUE(result.exists); | 
|  105       ASSERT_TRUE(result.isDirectory); |  | 
|  106       ASSERT_FALSE(result.isFile); |  | 
|  107       ASSERT_NE(0, result.lastModified); |  105       ASSERT_NE(0, result.lastModified); | 
|  108       hasStatRun = true; |  106       hasStatRun = true; | 
|  109     }); |  107     }); | 
|  110   EXPECT_FALSE(hasStatRun); |  108   EXPECT_FALSE(hasStatRun); | 
|  111   PumpTask(); |  109   PumpTask(); | 
|  112   EXPECT_TRUE(hasStatRun); |  110   EXPECT_TRUE(hasStatRun); | 
|  113 } |  111 } | 
|  114  |  112  | 
|  115 TEST_F(DefaultFileSystemTest, WriteMoveStatRemove) |  113 TEST_F(DefaultFileSystemTest, WriteMoveStatRemove) | 
|  116 { |  114 { | 
|  117   WriteString("foo"); |  115   WriteString("foo"); | 
|  118  |  116  | 
|  119   bool hasStatOrigFileExistsRun = false; |  117   bool hasStatOrigFileExistsRun = false; | 
|  120   fileSystem->Stat(testPath, |  118   fileSystem->Stat(testFileName, | 
|  121     [&hasStatOrigFileExistsRun](const IFileSystem::StatResult& result, const std
     ::string& error) |  119     [&hasStatOrigFileExistsRun](const IFileSystem::StatResult& result, const std
     ::string& error) | 
|  122     { |  120     { | 
|  123       EXPECT_TRUE(error.empty()); |  121       EXPECT_TRUE(error.empty()); | 
|  124       ASSERT_TRUE(result.exists); |  122       ASSERT_TRUE(result.exists); | 
|  125       ASSERT_TRUE(result.isFile); |  | 
|  126       ASSERT_FALSE(result.isDirectory); |  | 
|  127       ASSERT_NE(0, result.lastModified); |  123       ASSERT_NE(0, result.lastModified); | 
|  128       hasStatOrigFileExistsRun = true; |  124       hasStatOrigFileExistsRun = true; | 
|  129     }); |  125     }); | 
|  130   EXPECT_FALSE(hasStatOrigFileExistsRun); |  126   EXPECT_FALSE(hasStatOrigFileExistsRun); | 
|  131   PumpTask(); |  127   PumpTask(); | 
|  132   EXPECT_TRUE(hasStatOrigFileExistsRun); |  128   EXPECT_TRUE(hasStatOrigFileExistsRun); | 
|  133  |  129  | 
|  134   const std::string newTestPath = testPath + "-new"; |  130   const std::string newTestFileName = testFileName + "-new"; | 
|  135   bool hasMoveRun = false; |  131   bool hasMoveRun = false; | 
|  136   fileSystem->Move(testPath, newTestPath, [&hasMoveRun, newTestPath](const std::
     string& error) |  132   fileSystem->Move(testFileName, newTestFileName, [&hasMoveRun, newTestFileName]
     (const std::string& error) | 
|  137   { |  133   { | 
|  138     EXPECT_TRUE(error.empty()); |  134     EXPECT_TRUE(error.empty()); | 
|  139     hasMoveRun = true; |  135     hasMoveRun = true; | 
|  140   }); |  136   }); | 
|  141   EXPECT_FALSE(hasMoveRun); |  137   EXPECT_FALSE(hasMoveRun); | 
|  142   PumpTask(); |  138   PumpTask(); | 
|  143   EXPECT_TRUE(hasMoveRun); |  139   EXPECT_TRUE(hasMoveRun); | 
|  144  |  140  | 
|  145   bool hasStatOrigFileDontExistsRun = false; |  141   bool hasStatOrigFileDontExistsRun = false; | 
|  146   fileSystem->Stat(testPath, [&hasStatOrigFileDontExistsRun](const IFileSystem::
     StatResult& result, const std::string& error) |  142   fileSystem->Stat(testFileName, [&hasStatOrigFileDontExistsRun](const IFileSyst
     em::StatResult& result, const std::string& error) | 
|  147   { |  143   { | 
|  148     EXPECT_TRUE(error.empty()); |  144     EXPECT_TRUE(error.empty()); | 
|  149     ASSERT_FALSE(result.exists); |  145     ASSERT_FALSE(result.exists); | 
|  150     hasStatOrigFileDontExistsRun = true; |  146     hasStatOrigFileDontExistsRun = true; | 
|  151   }); |  147   }); | 
|  152   EXPECT_FALSE(hasStatOrigFileDontExistsRun); |  148   EXPECT_FALSE(hasStatOrigFileDontExistsRun); | 
|  153   PumpTask(); |  149   PumpTask(); | 
|  154   EXPECT_TRUE(hasStatOrigFileDontExistsRun); |  150   EXPECT_TRUE(hasStatOrigFileDontExistsRun); | 
|  155  |  151  | 
|  156   bool hasStatNewFileExistsRun = false; |  152   bool hasStatNewFileExistsRun = false; | 
|  157   fileSystem->Stat(newTestPath, [&hasStatNewFileExistsRun](const IFileSystem::St
     atResult& result, const std::string& error) |  153   fileSystem->Stat(newTestFileName, [&hasStatNewFileExistsRun](const IFileSystem
     ::StatResult& result, const std::string& error) | 
|  158   { |  154   { | 
|  159     EXPECT_TRUE(error.empty()); |  155     EXPECT_TRUE(error.empty()); | 
|  160     ASSERT_TRUE(result.exists); |  156     ASSERT_TRUE(result.exists); | 
|  161     hasStatNewFileExistsRun = true; |  157     hasStatNewFileExistsRun = true; | 
|  162   }); |  158   }); | 
|  163   EXPECT_FALSE(hasStatNewFileExistsRun); |  159   EXPECT_FALSE(hasStatNewFileExistsRun); | 
|  164   PumpTask(); |  160   PumpTask(); | 
|  165   EXPECT_TRUE(hasStatNewFileExistsRun); |  161   EXPECT_TRUE(hasStatNewFileExistsRun); | 
|  166  |  162  | 
|  167   bool hasRemoveRun = false; |  163   bool hasRemoveRun = false; | 
|  168   fileSystem->Remove(newTestPath, [&hasRemoveRun](const std::string& error) |  164   fileSystem->Remove(newTestFileName, [&hasRemoveRun](const std::string& error) | 
|  169   { |  165   { | 
|  170     EXPECT_TRUE(error.empty()); |  166     EXPECT_TRUE(error.empty()); | 
|  171     hasRemoveRun = true; |  167     hasRemoveRun = true; | 
|  172   }); |  168   }); | 
|  173   EXPECT_FALSE(hasRemoveRun); |  169   EXPECT_FALSE(hasRemoveRun); | 
|  174   PumpTask(); |  170   PumpTask(); | 
|  175   EXPECT_TRUE(hasRemoveRun); |  171   EXPECT_TRUE(hasRemoveRun); | 
|  176  |  172  | 
|  177   bool hasStatRemovedFileRun = false; |  173   bool hasStatRemovedFileRun = false; | 
|  178   fileSystem->Stat(newTestPath, [&hasStatRemovedFileRun](const IFileSystem::Stat
     Result& result, const std::string& error) |  174   fileSystem->Stat(newTestFileName, [&hasStatRemovedFileRun](const IFileSystem::
     StatResult& result, const std::string& error) | 
|  179   { |  175   { | 
|  180     EXPECT_TRUE(error.empty()); |  176     EXPECT_TRUE(error.empty()); | 
|  181     ASSERT_FALSE(result.exists); |  177     ASSERT_FALSE(result.exists); | 
|  182     hasStatRemovedFileRun = true; |  178     hasStatRemovedFileRun = true; | 
|  183   }); |  179   }); | 
|  184   EXPECT_FALSE(hasStatRemovedFileRun); |  180   EXPECT_FALSE(hasStatRemovedFileRun); | 
|  185   PumpTask(); |  181   PumpTask(); | 
|  186   EXPECT_TRUE(hasStatRemovedFileRun); |  182   EXPECT_TRUE(hasStatRemovedFileRun); | 
|  187 } |  183 } | 
| OLD | NEW |