1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "storage/browser/quota/quota_settings.h"
#include <memory>
#include <optional>
#include <utility>
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/numerics/safe_conversions.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "storage/browser/quota/quota_device_info_helper.h"
#include "storage/browser/quota/quota_features.h"
#include "testing/gmock/include/gmock/gmock.h"
using ::testing::_;
namespace {
constexpr uint64_t kLowPhysicalMemory = 1024 * 1024;
constexpr uint64_t kHighPhysicalMemory = 65536 * kLowPhysicalMemory;
} // namespace
namespace storage {
class MockQuotaDeviceInfoHelper : public QuotaDeviceInfoHelper {
public:
MockQuotaDeviceInfoHelper() = default;
MOCK_CONST_METHOD1(AmountOfTotalDiskSpace, int64_t(const base::FilePath&));
MOCK_CONST_METHOD0(AmountOfPhysicalMemory, uint64_t());
};
class QuotaSettingsTest : public testing::Test {
public:
QuotaSettingsTest() = default;
void SetUp() override { ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); }
// Synchronous proxy to GetNominalDynamicSettings().
std::optional<QuotaSettings> GetSettings(
bool is_incognito,
QuotaDeviceInfoHelper* device_info_helper) {
std::optional<QuotaSettings> quota_settings;
base::RunLoop run_loop;
GetNominalDynamicSettings(
profile_path(), is_incognito, device_info_helper,
base::BindLambdaForTesting([&](std::optional<QuotaSettings> settings) {
quota_settings = std::move(settings);
run_loop.Quit();
}));
run_loop.Run();
return quota_settings;
}
const base::FilePath& profile_path() const { return data_dir_.GetPath(); }
protected:
base::test::ScopedFeatureList scoped_feature_list_;
base::ScopedTempDir data_dir_;
base::test::TaskEnvironment task_environment_;
};
class QuotaSettingsIncognitoTest : public QuotaSettingsTest {
public:
QuotaSettingsIncognitoTest() = default;
protected:
void SetUpDeviceInfoHelper(const int expected_calls,
const uint64_t physical_memory_amount) {
ON_CALL(device_info_helper_, AmountOfPhysicalMemory())
.WillByDefault(::testing::Return(physical_memory_amount));
EXPECT_CALL(device_info_helper_, AmountOfPhysicalMemory())
.Times(expected_calls);
}
void GetAndTestSettings(const uint64_t physical_memory_amount) {
std::optional<QuotaSettings> settings =
GetSettings(true, &device_info_helper_);
ASSERT_TRUE(settings.has_value());
const uint64_t pool_size =
base::checked_cast<uint64_t>(settings->pool_size);
EXPECT_LE(
physical_memory_amount * GetIncognitoQuotaRatioLowerBound_ForTesting(),
pool_size);
EXPECT_GE(
physical_memory_amount * GetIncognitoQuotaRatioUpperBound_ForTesting(),
pool_size);
}
private:
MockQuotaDeviceInfoHelper device_info_helper_;
};
TEST_F(QuotaSettingsTest, Default) {
MockQuotaDeviceInfoHelper device_info_helper;
ON_CALL(device_info_helper, AmountOfTotalDiskSpace(_))
.WillByDefault(::testing::Return(2000));
std::optional<QuotaSettings> settings =
GetSettings(false, &device_info_helper);
ASSERT_TRUE(settings.has_value());
// 1600 = 2000 * default PoolSizeRatio (0.8)
EXPECT_EQ(settings->pool_size, 1600);
// 1200 = 1600 * default PerStorageKeyRatio (.75)
EXPECT_EQ(settings->per_storage_key_quota, 1200);
}
TEST_F(QuotaSettingsTest, FeatureParamsWithLargeFixedQuota) {
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kStorageQuotaSettings, {{"MustRemainAvailableBytes", "500"},
{"MustRemainAvailableRatio", "0.01"},
{"PoolSizeBytes", "2000"},
{"PoolSizeRatio", "0.8"},
{"ShouldRemainAvailableBytes", "600"},
{"ShouldRemainAvailableRatio", "0.1"}});
MockQuotaDeviceInfoHelper device_info_helper;
ON_CALL(device_info_helper, AmountOfTotalDiskSpace(_))
.WillByDefault(::testing::Return(2000));
std::optional<QuotaSettings> settings =
GetSettings(false, &device_info_helper);
ASSERT_TRUE(settings.has_value());
EXPECT_EQ(settings->pool_size, 1600);
EXPECT_EQ(settings->must_remain_available, 20);
EXPECT_EQ(settings->should_remain_available, 200);
}
TEST_F(QuotaSettingsTest, FeatureParamsWithSmallFixedQuota) {
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kStorageQuotaSettings, {{"MustRemainAvailableBytes", "5"},
{"MustRemainAvailableRatio", "0.01"},
{"PoolSizeBytes", "20"},
{"PoolSizeRatio", "0.8"},
{"ShouldRemainAvailableBytes", "60"},
{"ShouldRemainAvailableRatio", "0.1"}});
MockQuotaDeviceInfoHelper device_info_helper;
ON_CALL(device_info_helper, AmountOfTotalDiskSpace(_))
.WillByDefault(::testing::Return(2000));
std::optional<QuotaSettings> settings =
GetSettings(false, &device_info_helper);
ASSERT_TRUE(settings.has_value());
EXPECT_EQ(settings->pool_size, 20);
EXPECT_EQ(settings->must_remain_available, 5);
EXPECT_EQ(settings->should_remain_available, 60);
}
TEST_F(QuotaSettingsTest, FeatureParamsWithoutFixedQuota) {
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kStorageQuotaSettings, {{"MustRemainAvailableRatio", "0.01"},
{"PoolSizeRatio", "0.8"},
{"ShouldRemainAvailableRatio", "0.1"}});
MockQuotaDeviceInfoHelper device_info_helper;
ON_CALL(device_info_helper, AmountOfTotalDiskSpace(_))
.WillByDefault(::testing::Return(2000));
std::optional<QuotaSettings> settings =
GetSettings(false, &device_info_helper);
ASSERT_TRUE(settings.has_value());
EXPECT_EQ(settings->pool_size, 1600);
EXPECT_EQ(settings->must_remain_available, 20);
EXPECT_EQ(settings->should_remain_available, 200);
}
TEST_F(QuotaSettingsIncognitoTest, IncognitoDynamicQuota_LowPhysicalMemory) {
const int expected_device_info_calls = 1;
SetUpDeviceInfoHelper(expected_device_info_calls, kLowPhysicalMemory);
GetAndTestSettings(kLowPhysicalMemory);
}
TEST_F(QuotaSettingsIncognitoTest, IncognitoDynamicQuota_HighPhysicalMemory) {
const int expected_device_info_calls = 1;
SetUpDeviceInfoHelper(expected_device_info_calls, kHighPhysicalMemory);
GetAndTestSettings(kHighPhysicalMemory);
}
} // namespace storage
|