tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

FileSystemParentTest.cpp (6868B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "FileSystemParentTest.h"
      8 
      9 #include "FileSystemParentTestHelpers.h"
     10 #include "TestHelpers.h"
     11 #include "datamodel/FileSystemDataManager.h"
     12 #include "datamodel/FileSystemDatabaseManager.h"
     13 #include "gtest/gtest.h"
     14 #include "mozilla/dom/PFileSystemManager.h"
     15 
     16 namespace mozilla::dom::fs::test {
     17 
     18 FileSystemParentTest::FileSystemParentTest() = default;
     19 
     20 FileSystemParentTest::~FileSystemParentTest() = default;
     21 
     22 // static
     23 void FileSystemParentTest::SetUpTestCase() {
     24  ASSERT_NO_FATAL_FAILURE(InitializeFixture());
     25 }
     26 
     27 // static
     28 void FileSystemParentTest::TearDownTestCase() {
     29  ASSERT_NO_FATAL_FAILURE(ShutdownFixture());
     30 }
     31 
     32 void FileSystemParentTest::TearDown() {
     33  ReleaseDataManager();
     34 
     35  ASSERT_NO_FATAL_FAILURE(ClearStoragesForOrigin(GetTestOriginMetadata()));
     36 }
     37 
     38 // static
     39 void FileSystemParentTest::InitializeTemporaryOrigin(
     40    bool aCreateIfNonExistent) {
     41  ASSERT_NO_FATAL_FAILURE(
     42      QuotaManagerDependencyFixture::InitializeTemporaryOrigin(
     43          GetTestOriginMetadata(), aCreateIfNonExistent));
     44 }
     45 
     46 //  static
     47 void FileSystemParentTest::GetOriginUsage(quota::UsageInfo& aResult) {
     48  ASSERT_NO_FATAL_FAILURE(QuotaManagerDependencyFixture::GetOriginUsage(
     49      GetTestOriginMetadata(), &aResult));
     50 }
     51 
     52 //  static
     53 void FileSystemParentTest::GetCachedOriginUsage(quota::UsageInfo& aResult) {
     54  ASSERT_NO_FATAL_FAILURE(QuotaManagerDependencyFixture::GetCachedOriginUsage(
     55      GetTestOriginMetadata(), &aResult));
     56 }
     57 
     58 // static
     59 void FileSystemParentTest::InitializeTemporaryClient() {
     60  ASSERT_NO_FATAL_FAILURE(
     61      QuotaManagerDependencyFixture::InitializeTemporaryClient(
     62          GetTestClientMetadata(), /* aCreateIfNonExistent */ true));
     63 }
     64 
     65 // static
     66 void FileSystemParentTest::GetStaticDatabaseUsage(
     67    quota::UsageInfo& aDatabaseUsage) {
     68  quota::QuotaManager* quotaManager = quota::QuotaManager::Get();
     69  ASSERT_TRUE(quotaManager);
     70 
     71  TEST_TRY_UNWRAP(
     72      auto databaseUsage,
     73      PerformOnThread(
     74          quotaManager->IOThread(), []() -> Result<quota::UsageInfo, QMResult> {
     75            QM_TRY_INSPECT(
     76                const ResultConnection& conn,
     77                data::GetStorageConnection(GetTestOriginMetadata(),
     78                                           /* aDirectoryLockId */ -1));
     79 
     80            return data::FileSystemDatabaseManager::GetUsage(
     81                conn, GetTestOriginMetadata());
     82          }));
     83 
     84  aDatabaseUsage = databaseUsage;
     85 }
     86 
     87 void FileSystemParentTest::EnsureDataManager() {
     88  PerformOnBackgroundThread([this]() {
     89    ASSERT_NO_FATAL_FAILURE(test::CreateRegisteredDataManager(
     90        GetTestOriginMetadata(), mDataManager));
     91  });
     92 }
     93 
     94 void FileSystemParentTest::ReleaseDataManager() {
     95  PerformOnBackgroundThread([this]() { mDataManager = nullptr; });
     96 }
     97 
     98 void FileSystemParentTest::LockExclusive(const EntryId& aEntryId) {
     99  ASSERT_TRUE(mDataManager);
    100 
    101  TEST_TRY_UNWRAP(FileId fileId, PerformOnBackgroundThread([this, &aEntryId]() {
    102                    return mDataManager->LockExclusive(aEntryId);
    103                  }));
    104 }
    105 
    106 void FileSystemParentTest::UnlockExclusive(const EntryId& aEntryId) {
    107  ASSERT_TRUE(mDataManager);
    108 
    109  PerformOnBackgroundThread(
    110      [this, &aEntryId]() { mDataManager->UnlockExclusive(aEntryId); });
    111 }
    112 
    113 void FileSystemParentTest::CreateNewEmptyFile(EntryId& aEntryId) {
    114  ASSERT_TRUE(mDataManager);
    115 
    116  TEST_TRY_UNWRAP(
    117      EntryId testFileId,
    118      PerformOnThread(
    119          mDataManager->MutableIOTaskQueuePtr(),
    120          [this]() -> Result<EntryId, QMResult> {
    121            data::FileSystemDatabaseManager* databaseManager =
    122                mDataManager->MutableDatabaseManagerPtr();
    123 
    124            QM_TRY_UNWRAP(const EntryId rootId,
    125                          data::GetRootHandle(GetTestOrigin()));
    126            FileSystemChildMetadata fileData(rootId, GetTestFileName());
    127 
    128            EntryId testFileId;
    129            ENSURE_NO_FATAL_FAILURE(
    130                test::CreateNewEmptyFile(databaseManager, fileData, testFileId),
    131                Err(QMResult(NS_ERROR_FAILURE)));
    132 
    133            return testFileId;
    134          }));
    135 
    136  aEntryId = testFileId;
    137 }
    138 
    139 void FileSystemParentTest::WriteDataToFile(EntryId& aEntryId,
    140                                           const nsCString& aData) {
    141  ASSERT_TRUE(mDataManager);
    142 
    143  TEST_TRY(PerformOnThread(mDataManager->MutableIOTaskQueuePtr(),
    144                           [this, &aEntryId, &aData]() -> Result<Ok, QMResult> {
    145                             data::FileSystemDatabaseManager* databaseManager =
    146                                 mDataManager->MutableDatabaseManagerPtr();
    147 
    148                             ENSURE_NO_FATAL_FAILURE(
    149                                 test::WriteDataToFile(GetTestOriginMetadata(),
    150                                                       databaseManager,
    151                                                       aEntryId, aData),
    152                                 Err(QMResult(NS_ERROR_FAILURE)));
    153 
    154                             return Ok{};
    155                           }));
    156 }
    157 
    158 void FileSystemParentTest::RemoveFile(bool& aWasRemoved) {
    159  ASSERT_TRUE(mDataManager);
    160 
    161  TEST_TRY_UNWRAP(
    162      bool wasRemoved,
    163      PerformOnThread(mDataManager->MutableIOTaskQueuePtr(),
    164                      [this]() -> Result<bool, QMResult> {
    165                        data::FileSystemDatabaseManager* databaseManager =
    166                            mDataManager->MutableDatabaseManagerPtr();
    167 
    168                        QM_TRY_UNWRAP(const EntryId rootId,
    169                                      data::GetRootHandle(GetTestOrigin()));
    170 
    171                        QM_TRY_RETURN(databaseManager->RemoveFile(
    172                            {rootId, GetTestFileName()}));
    173                      }));
    174 
    175  aWasRemoved = wasRemoved;
    176 }
    177 
    178 void FileSystemParentTest::GetDatabaseUsage(quota::UsageInfo& aDatabaseUsage) {
    179  ASSERT_TRUE(mDataManager);
    180 
    181  TEST_TRY_UNWRAP(
    182      auto databaseUsage,
    183      PerformOnThread(mDataManager->MutableIOTaskQueuePtr(), [this]() {
    184        data::FileSystemDatabaseManager* databaseManager =
    185            mDataManager->MutableDatabaseManagerPtr();
    186 
    187        QM_TRY_RETURN(databaseManager->GetUsage());
    188      }));
    189 
    190  aDatabaseUsage = databaseUsage;
    191 }
    192 
    193 void FileSystemParentTest::UpdateDatabaseUsage(const FileId& aFileId) {
    194  ASSERT_TRUE(mDataManager);
    195 
    196  TEST_TRY(PerformOnThread(
    197      mDataManager->MutableIOTaskQueuePtr(), [this, &aFileId]() {
    198        data::FileSystemDatabaseManager* databaseManager =
    199            mDataManager->MutableDatabaseManagerPtr();
    200 
    201        QM_TRY_RETURN(MOZ_TO_RESULT(databaseManager->UpdateUsage(aFileId)));
    202      }));
    203 }
    204 
    205 }  // namespace mozilla::dom::fs::test