Commit 1f02b90f authored by Jarryd's avatar Jarryd Committed by Commit Bot

Quota: Support mocking physical memory in quota tests.

Rename QuotaDiskInfoHelper to QuotaDeviceInfoHelper and add another
method to it, AmountOfPhysicalMemory. This allows mocking physical
memory in unit tests, particularly useful for testing code having to
do with incognito mode. This change also removes a specialized
function in QuotaSettings that was used exclusively for testing.

Follow up to https://crrev.com/c/1890055

Bug: 1017120
Change-Id: If28105bba658cdff1e3f58340b6dc7bda8285289
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1895074Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Reviewed-by: default avatarRamin Halavati <rhalavati@chromium.org>
Reviewed-by: default avatarVictor Costan <pwnall@chromium.org>
Reviewed-by: default avatarSean Topping <seantopping@chromium.org>
Commit-Queue: Jarryd Goodman <jarrydg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713309}
parent 2627076a
......@@ -503,7 +503,7 @@ void AwContentBrowserClient::GetQuotaSettings(
storage::OptionalQuotaSettingsCallback callback) {
storage::GetNominalDynamicSettings(
partition->GetPath(), context->IsOffTheRecord(),
storage::GetDefaultDiskInfoHelper(), std::move(callback));
storage::GetDefaultDeviceInfoHelper(), std::move(callback));
}
content::GeneratedCodeCacheSettings
......
......@@ -2631,7 +2631,7 @@ void ChromeContentBrowserClient::GetQuotaSettings(
}
storage::GetNominalDynamicSettings(
partition->GetPath(), context->IsOffTheRecord(),
storage::GetDefaultDiskInfoHelper(), std::move(callback));
storage::GetDefaultDeviceInfoHelper(), std::move(callback));
}
content::GeneratedCodeCacheSettings
......
......@@ -547,7 +547,7 @@ void CastContentBrowserClient::GetQuotaSettings(
storage::OptionalQuotaSettingsCallback callback) {
storage::GetNominalDynamicSettings(
partition->GetPath(), context->IsOffTheRecord(),
storage::GetDefaultDiskInfoHelper(), std::move(callback));
storage::GetDefaultDeviceInfoHelper(), std::move(callback));
}
void CastContentBrowserClient::AllowCertificateError(
......
......@@ -138,7 +138,7 @@ void ShellContentBrowserClient::GetQuotaSettings(
storage::OptionalQuotaSettingsCallback callback) {
storage::GetNominalDynamicSettings(
partition->GetPath(), context->IsOffTheRecord(),
storage::GetDefaultDiskInfoHelper(), std::move(callback));
storage::GetDefaultDeviceInfoHelper(), std::move(callback));
}
bool ShellContentBrowserClient::IsHandledURL(const GURL& url) {
......
......@@ -154,7 +154,7 @@ void HeadlessContentBrowserClient::GetQuotaSettings(
::storage::OptionalQuotaSettingsCallback callback) {
::storage::GetNominalDynamicSettings(
partition->GetPath(), context->IsOffTheRecord(),
::storage::GetDefaultDiskInfoHelper(), std::move(callback));
::storage::GetDefaultDeviceInfoHelper(), std::move(callback));
}
content::GeneratedCodeCacheSettings
......
......@@ -197,8 +197,8 @@ jumbo_component("browser") {
"quota/quota_client.h",
"quota/quota_database.cc",
"quota/quota_database.h",
"quota/quota_disk_info_helper.cc",
"quota/quota_disk_info_helper.h",
"quota/quota_device_info_helper.cc",
"quota/quota_device_info_helper.h",
"quota/quota_features.cc",
"quota/quota_features.h",
"quota/quota_limit_type.h",
......
......@@ -2,15 +2,19 @@
// 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_disk_info_helper.h"
#include "storage/browser/quota/quota_device_info_helper.h"
namespace storage {
QuotaDiskInfoHelper::~QuotaDiskInfoHelper() = default;
QuotaDeviceInfoHelper::~QuotaDeviceInfoHelper() = default;
int64_t QuotaDiskInfoHelper::AmountOfTotalDiskSpace(
int64_t QuotaDeviceInfoHelper::AmountOfTotalDiskSpace(
const base::FilePath& path) const {
return base::SysInfo::AmountOfTotalDiskSpace(path);
}
int64_t QuotaDeviceInfoHelper::AmountOfPhysicalMemory() const {
return base::SysInfo::AmountOfPhysicalMemory();
}
} // namespace storage
......@@ -6,26 +6,28 @@
#include "base/no_destructor.h"
#include "base/system/sys_info.h"
#ifndef STORAGE_BROWSER_QUOTA_QUOTA_DISK_INFO_HELPER_H_
#define STORAGE_BROWSER_QUOTA_QUOTA_DISK_INFO_HELPER_H_
#ifndef STORAGE_BROWSER_QUOTA_QUOTA_DEVICE_INFO_HELPER_H_
#define STORAGE_BROWSER_QUOTA_QUOTA_DEVICE_INFO_HELPER_H_
namespace storage {
// Interface used by the quota system to gather disk space information.
// Can be overridden in tests.
// Subclasses must be thread-safe.
// QuotaSettings instances own a singleton instance of QuotaDiskInfoHelper.
class COMPONENT_EXPORT(STORAGE_BROWSER) QuotaDiskInfoHelper {
// QuotaSettings instances own a singleton instance of QuotaDeviceInfoHelper.
class COMPONENT_EXPORT(STORAGE_BROWSER) QuotaDeviceInfoHelper {
public:
QuotaDiskInfoHelper() = default;
virtual ~QuotaDiskInfoHelper();
QuotaDeviceInfoHelper() = default;
virtual ~QuotaDeviceInfoHelper();
virtual int64_t AmountOfTotalDiskSpace(const base::FilePath& path) const;
virtual int64_t AmountOfPhysicalMemory() const;
private:
DISALLOW_COPY_AND_ASSIGN(QuotaDiskInfoHelper);
}; // class QuotaDiskInfoHelper
DISALLOW_COPY_AND_ASSIGN(QuotaDeviceInfoHelper);
}; // class QuotaDeviceInfoHelper
} // namespace storage
#endif // STORAGE_BROWSER_QUOTA_QUOTA_DISK_INFO_HELPER_H_
#endif // STORAGE_BROWSER_QUOTA_QUOTA_DEVICE_INFO_HELPER_H_
......@@ -13,7 +13,7 @@
#include "base/task/post_task.h"
#include "base/threading/scoped_blocking_call.h"
#include "build/build_config.h"
#include "storage/browser/quota/quota_disk_info_helper.h"
#include "storage/browser/quota/quota_device_info_helper.h"
#include "storage/browser/quota/quota_features.h"
#include "storage/browser/quota/quota_macros.h"
......@@ -60,13 +60,13 @@ storage::QuotaSettings CalculateIncognitoDynamicSettings(
base::Optional<storage::QuotaSettings> CalculateNominalDynamicSettings(
const base::FilePath& partition_path,
bool is_incognito,
QuotaDiskInfoHelper* disk_info_helper) {
QuotaDeviceInfoHelper* device_info_helper) {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
if (is_incognito) {
return CalculateIncognitoDynamicSettings(
base::SysInfo::AmountOfPhysicalMemory());
device_info_helper->AmountOfPhysicalMemory());
}
// The fraction of the device's storage the browser is willing to
......@@ -124,7 +124,7 @@ base::Optional<storage::QuotaSettings> CalculateNominalDynamicSettings(
storage::QuotaSettings settings;
int64_t total = disk_info_helper->AmountOfTotalDiskSpace(partition_path);
int64_t total = device_info_helper->AmountOfTotalDiskSpace(partition_path);
if (total == -1) {
LOG(ERROR) << "Unable to compute QuotaSettings.";
return base::nullopt;
......@@ -150,25 +150,21 @@ base::Optional<storage::QuotaSettings> CalculateNominalDynamicSettings(
} // namespace
int64_t GetIncognitoPoolSizeForTesting(int64_t physical_memory_amount) {
return CalculateIncognitoDynamicSettings(physical_memory_amount).pool_size;
}
void GetNominalDynamicSettings(const base::FilePath& partition_path,
bool is_incognito,
QuotaDiskInfoHelper* disk_info_helper,
QuotaDeviceInfoHelper* device_info_helper,
OptionalQuotaSettingsCallback callback) {
base::PostTaskAndReplyWithResult(
FROM_HERE,
{base::ThreadPool(), base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&CalculateNominalDynamicSettings, partition_path,
is_incognito, base::Unretained(disk_info_helper)),
is_incognito, base::Unretained(device_info_helper)),
std::move(callback));
}
QuotaDiskInfoHelper* GetDefaultDiskInfoHelper() {
static base::NoDestructor<QuotaDiskInfoHelper> singleton;
QuotaDeviceInfoHelper* GetDefaultDeviceInfoHelper() {
static base::NoDestructor<QuotaDeviceInfoHelper> singleton;
return singleton.get();
}
......
......@@ -13,7 +13,7 @@
#include "base/files/file_path.h"
#include "base/optional.h"
#include "base/time/time.h"
#include "storage/browser/quota/quota_disk_info_helper.h"
#include "storage/browser/quota/quota_device_info_helper.h"
namespace storage {
......@@ -79,13 +79,10 @@ using GetQuotaSettingsFunc =
COMPONENT_EXPORT(STORAGE_BROWSER)
void GetNominalDynamicSettings(const base::FilePath& partition_path,
bool is_incognito,
QuotaDiskInfoHelper* diskInfoHelper,
QuotaDeviceInfoHelper* deviceInfoHelper,
OptionalQuotaSettingsCallback callback);
COMPONENT_EXPORT(STORAGE_BROWSER)
// TODO(https://crbug.com/1017120): Add memory size mocking support to
// QuotaDiskInfoHelper and remove this function.
int64_t GetIncognitoPoolSizeForTesting(int64_t physical_memory_amount);
// Returns settings with a poolsize of zero and no per host quota.
inline QuotaSettings GetNoQuotaSettings() {
......@@ -102,7 +99,7 @@ inline QuotaSettings GetHardCodedSettings(int64_t per_host_quota) {
// Returns object that can fetch actual total disk space; instance lives
// as long as the process is a live.
COMPONENT_EXPORT(STORAGE_BROWSER)
QuotaDiskInfoHelper* GetDefaultDiskInfoHelper();
QuotaDeviceInfoHelper* GetDefaultDeviceInfoHelper();
} // namespace storage
#endif // STORAGE_BROWSER_QUOTA_QUOTA_SETTINGS_H_
......@@ -11,7 +11,7 @@
#include "base/test/bind_test_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "storage/browser/quota/quota_disk_info_helper.h"
#include "storage/browser/quota/quota_device_info_helper.h"
#include "storage/browser/quota/quota_features.h"
#include "storage/browser/quota/quota_settings.h"
#include "testing/gmock/include/gmock/gmock.h"
......@@ -20,10 +20,11 @@ using ::testing::_;
namespace storage {
class MockQuotaDiskInfoHelper : public QuotaDiskInfoHelper {
class MockQuotaDeviceInfoHelper : public QuotaDeviceInfoHelper {
public:
MockQuotaDiskInfoHelper() = default;
MockQuotaDeviceInfoHelper() = default;
MOCK_CONST_METHOD1(AmountOfTotalDiskSpace, int64_t(const base::FilePath&));
MOCK_CONST_METHOD0(AmountOfPhysicalMemory, int64_t());
};
class QuotaSettingsTest : public testing::Test {
......@@ -43,13 +44,13 @@ class QuotaSettingsTest : public testing::Test {
};
TEST_F(QuotaSettingsTest, Default) {
MockQuotaDiskInfoHelper disk_info_helper;
ON_CALL(disk_info_helper, AmountOfTotalDiskSpace(_))
MockQuotaDeviceInfoHelper device_info_helper;
ON_CALL(device_info_helper, AmountOfTotalDiskSpace(_))
.WillByDefault(::testing::Return(2000));
bool callback_executed = false;
GetNominalDynamicSettings(
profile_path(), false, &disk_info_helper,
profile_path(), false, &device_info_helper,
base::BindLambdaForTesting([&](base::Optional<QuotaSettings> settings) {
callback_executed = true;
ASSERT_NE(settings, base::nullopt);
......@@ -63,8 +64,8 @@ TEST_F(QuotaSettingsTest, Default) {
}
TEST_F(QuotaSettingsTest, ExpandedTempPool) {
MockQuotaDiskInfoHelper disk_info_helper;
ON_CALL(disk_info_helper, AmountOfTotalDiskSpace(_))
MockQuotaDeviceInfoHelper device_info_helper;
ON_CALL(device_info_helper, AmountOfTotalDiskSpace(_))
.WillByDefault(::testing::Return(2000));
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kQuotaExpandPoolSize,
......@@ -72,7 +73,7 @@ TEST_F(QuotaSettingsTest, ExpandedTempPool) {
bool callback_executed = false;
GetNominalDynamicSettings(
profile_path(), false, &disk_info_helper,
profile_path(), false, &device_info_helper,
base::BindLambdaForTesting([&](base::Optional<QuotaSettings> settings) {
callback_executed = true;
ASSERT_NE(settings, base::nullopt);
......@@ -86,14 +87,14 @@ TEST_F(QuotaSettingsTest, ExpandedTempPool) {
}
TEST_F(QuotaSettingsTest, UnlimitedTempPool) {
MockQuotaDiskInfoHelper disk_info_helper;
ON_CALL(disk_info_helper, AmountOfTotalDiskSpace(_))
MockQuotaDeviceInfoHelper device_info_helper;
ON_CALL(device_info_helper, AmountOfTotalDiskSpace(_))
.WillByDefault(::testing::Return(2000));
scoped_feature_list_.InitAndEnableFeature(features::kQuotaUnlimitedPoolSize);
bool callback_executed = false;
GetNominalDynamicSettings(
profile_path(), false, &disk_info_helper,
profile_path(), false, &device_info_helper,
base::BindLambdaForTesting([&](base::Optional<QuotaSettings> settings) {
callback_executed = true;
ASSERT_NE(settings, base::nullopt);
......@@ -108,30 +109,124 @@ TEST_F(QuotaSettingsTest, IncognitoQuotaCapped) {
const int64_t kMBytes = 1024 * 1024;
const int64_t kMaxIncognitoPoolSize = 330 * kMBytes; // 300 MB + 10%
MockQuotaDeviceInfoHelper device_info_helper;
ON_CALL(device_info_helper, AmountOfPhysicalMemory())
.WillByDefault(::testing::Return(kMaxIncognitoPoolSize));
scoped_feature_list_.InitAndDisableFeature(features::kIncognitoDynamicQuota);
EXPECT_GE(kMaxIncognitoPoolSize,
GetIncognitoPoolSizeForTesting(kMaxIncognitoPoolSize * 1000));
bool callback_executed = false;
GetNominalDynamicSettings(
profile_path(), true, &device_info_helper,
base::BindLambdaForTesting([&](base::Optional<QuotaSettings> settings) {
callback_executed = true;
EXPECT_GE(kMaxIncognitoPoolSize, settings->pool_size);
}));
task_environment_.RunUntilIdle();
EXPECT_TRUE(callback_executed);
}
TEST_F(QuotaSettingsTest, IncognitoQuotaDynamic) {
TEST_F(QuotaSettingsTest, IncognitoDynamicQuota1) {
const int64_t kMBytes = 1024 * 1024;
const int64_t kMaxIncognitoPoolSize = 330 * kMBytes; // 300 MB + 10%
const int64_t test_cases[] = {
kMaxIncognitoPoolSize / 10, kMaxIncognitoPoolSize,
kMaxIncognitoPoolSize * 100, kMaxIncognitoPoolSize * 1000};
const int64_t physical_memory_amount = kMaxIncognitoPoolSize / 10;
MockQuotaDeviceInfoHelper device_info_helper;
ON_CALL(device_info_helper, AmountOfPhysicalMemory())
.WillByDefault(::testing::Return(physical_memory_amount));
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kIncognitoDynamicQuota,
{{"IncognitoQuotaRatioLowerBound", "0.1"},
{"IncognitoQuotaRatioLowerBound", "0.2"}});
for (const int64_t physical_memory_amount : test_cases) {
EXPECT_LE(physical_memory_amount / 10,
GetIncognitoPoolSizeForTesting(physical_memory_amount))
<< physical_memory_amount;
EXPECT_GE(physical_memory_amount / 5,
GetIncognitoPoolSizeForTesting(physical_memory_amount))
<< physical_memory_amount;
}
{"IncognitoQuotaRatioUpperBound", "0.2"}});
bool callback_executed = false;
GetNominalDynamicSettings(
profile_path(), true, &device_info_helper,
base::BindLambdaForTesting([&](base::Optional<QuotaSettings> settings) {
callback_executed = true;
EXPECT_LE(physical_memory_amount / 10, settings->pool_size);
EXPECT_GE(physical_memory_amount / 5, settings->pool_size);
}));
task_environment_.RunUntilIdle();
EXPECT_TRUE(callback_executed);
}
TEST_F(QuotaSettingsTest, IncognitoDynamicQuota2) {
const int64_t kMBytes = 1024 * 1024;
const int64_t kMaxIncognitoPoolSize = 330 * kMBytes; // 300 MB + 10%
const int64_t physical_memory_amount = kMaxIncognitoPoolSize;
MockQuotaDeviceInfoHelper device_info_helper;
ON_CALL(device_info_helper, AmountOfPhysicalMemory())
.WillByDefault(::testing::Return(physical_memory_amount));
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kIncognitoDynamicQuota,
{{"IncognitoQuotaRatioLowerBound", "0.1"},
{"IncognitoQuotaRatioUpperBound", "0.2"}});
bool callback_executed = false;
GetNominalDynamicSettings(
profile_path(), true, &device_info_helper,
base::BindLambdaForTesting([&](base::Optional<QuotaSettings> settings) {
callback_executed = true;
EXPECT_LE(physical_memory_amount / 10, settings->pool_size);
EXPECT_GE(physical_memory_amount / 5, settings->pool_size);
}));
task_environment_.RunUntilIdle();
EXPECT_TRUE(callback_executed);
}
TEST_F(QuotaSettingsTest, IncognitoDynamicQuota3) {
const int64_t kMBytes = 1024 * 1024;
const int64_t kMaxIncognitoPoolSize = 330 * kMBytes; // 300 MB + 10%
const int64_t physical_memory_amount = kMaxIncognitoPoolSize * 100;
MockQuotaDeviceInfoHelper device_info_helper;
ON_CALL(device_info_helper, AmountOfPhysicalMemory())
.WillByDefault(::testing::Return(physical_memory_amount));
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kIncognitoDynamicQuota,
{{"IncognitoQuotaRatioLowerBound", "0.1"},
{"IncognitoQuotaRatioUpperBound", "0.2"}});
bool callback_executed = false;
GetNominalDynamicSettings(
profile_path(), true, &device_info_helper,
base::BindLambdaForTesting([&](base::Optional<QuotaSettings> settings) {
callback_executed = true;
EXPECT_LE(physical_memory_amount / 10, settings->pool_size);
EXPECT_GE(physical_memory_amount / 5, settings->pool_size);
}));
task_environment_.RunUntilIdle();
EXPECT_TRUE(callback_executed);
}
TEST_F(QuotaSettingsTest, IncognitoDynamicQuota4) {
const int64_t kMBytes = 1024 * 1024;
const int64_t kMaxIncognitoPoolSize = 330 * kMBytes; // 300 MB + 10%
const int64_t physical_memory_amount = kMaxIncognitoPoolSize * 1000;
MockQuotaDeviceInfoHelper device_info_helper;
ON_CALL(device_info_helper, AmountOfPhysicalMemory())
.WillByDefault(::testing::Return(physical_memory_amount));
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kIncognitoDynamicQuota,
{{"IncognitoQuotaRatioLowerBound", "0.1"},
{"IncognitoQuotaRatioUpperBound", "0.2"}});
bool callback_executed = false;
GetNominalDynamicSettings(
profile_path(), true, &device_info_helper,
base::BindLambdaForTesting([&](base::Optional<QuotaSettings> settings) {
callback_executed = true;
EXPECT_LE(physical_memory_amount / 10, settings->pool_size);
EXPECT_GE(physical_memory_amount / 5, settings->pool_size);
}));
task_environment_.RunUntilIdle();
EXPECT_TRUE(callback_executed);
}
} // namespace storage
......@@ -30,7 +30,7 @@ void ViewsContentBrowserClient::GetQuotaSettings(
storage::OptionalQuotaSettingsCallback callback) {
storage::GetNominalDynamicSettings(
partition->GetPath(), context->IsOffTheRecord(),
storage::GetDefaultDiskInfoHelper(), std::move(callback));
storage::GetDefaultDeviceInfoHelper(), std::move(callback));
}
} // namespace ui
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment