Skip to content

Commit

Permalink
Add unit test for the settings migration from PLUGIN + ResourceIdenti…
Browse files Browse the repository at this point in the history
…fier

Since higher level APIs that would work with the ResourceIdentifier are
now gone, this unit tests is implemented at a lower level, that is, by
manually creating the relevant dictionaries with their values in the
user preferences, perform the migration from BravePrefProvider and
finally manually (again) inspecting the result user preferences.
  • Loading branch information
mariospr committed Dec 22, 2020
1 parent 1456884 commit 7943ccb
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class BravePrefProvider : public PrefProvider,
FRIEND_TEST_ALL_PREFIXES(BravePrefProviderTest, TestShieldsSettingsMigration);
FRIEND_TEST_ALL_PREFIXES(BravePrefProviderTest,
TestShieldsSettingsMigrationVersion);
FRIEND_TEST_ALL_PREFIXES(BravePrefProviderTest,
TestShieldsSettingsMigrationFromResourceIDs);
void MigrateShieldsSettings(bool incognito);
void MigrateShieldsSettingsFromResourceIds();
void MigrateShieldsSettingsFromResourceIdsForOneType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

#include "base/macros.h"
#include "base/optional.h"
#include "base/values.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "brave/components/content_settings/core/browser/brave_content_settings_pref_provider.h"
#include "brave/components/content_settings/core/browser/brave_content_settings_utils.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/content_settings_registry.h"
#include "components/content_settings/core/common/content_settings.h"
Expand All @@ -20,13 +22,24 @@
#include "components/content_settings/core/test/content_settings_test_utils.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "services/preferences/public/cpp/dictionary_value_update.h"
#include "services/preferences/public/cpp/scoped_pref_update.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace content_settings {

namespace {

constexpr char kUserProfilePluginsPath[] =
"profile.content_settings.exceptions.plugins";

const char kExpirationPath[] = "expiration";
const char kLastModifiedPath[] = "last_modified";
const char kSessionModelPath[] = "model";
const char kSettingPath[] = "setting";
const char kPerResourcePath[] = "per_resource";

using GURLSourcePair = std::pair<GURL, ContentSettingsType>;

ContentSettingsPattern SecondaryUrlToPattern(const GURL& gurl) {
Expand All @@ -37,6 +50,71 @@ ContentSettingsPattern SecondaryUrlToPattern(const GURL& gurl) {
return ContentSettingsPattern::FromString("https://firstParty/*");
}

void InitializeAllShieldSettingsInDictionary(
prefs::DictionaryValueUpdate* dictionary,
const base::Time& last_modified_time,
const int& value) {
const uint64_t last_modified_time_in_ms =
last_modified_time.ToDeltaSinceWindowsEpoch().InMicroseconds();

dictionary->SetInteger(kExpirationPath, 0);
dictionary->SetString(
kLastModifiedPath, base::NumberToString(last_modified_time_in_ms));
dictionary->SetInteger(kSessionModelPath,
static_cast<int>(content_settings::SessionModel::Durable));

std::unique_ptr<prefs::DictionaryValueUpdate> per_resource_dict =
dictionary->SetDictionaryWithoutPathExpansion(
kPerResourcePath, std::make_unique<base::DictionaryValue>());

per_resource_dict->SetInteger(brave_shields::kAds, value);
per_resource_dict->SetInteger(brave_shields::kCookies, value);
per_resource_dict->SetInteger(brave_shields::kCosmeticFiltering, value);
per_resource_dict->SetInteger(brave_shields::kFingerprintingV2, value);
per_resource_dict->SetInteger(brave_shields::kHTTPUpgradableResources, value);
per_resource_dict->SetInteger(brave_shields::kReferrers, value);
per_resource_dict->SetInteger(brave_shields::kTrackers, value);
}

void InitializeBraveShieldsSettingInDictionary(
prefs::DictionaryValueUpdate* dictionary,
const base::Time& last_modified_time,
const int& value) {
const uint64_t last_modified_time_in_ms =
last_modified_time.ToDeltaSinceWindowsEpoch().InMicroseconds();

dictionary->SetInteger(kExpirationPath, 0);
dictionary->SetString(
kLastModifiedPath, base::NumberToString(last_modified_time_in_ms));
dictionary->SetInteger(kSessionModelPath,
static_cast<int>(content_settings::SessionModel::Durable));

std::unique_ptr<prefs::DictionaryValueUpdate> brave_per_resource_dict =
dictionary->SetDictionaryWithoutPathExpansion(
kPerResourcePath, std::make_unique<base::DictionaryValue>());

brave_per_resource_dict->SetInteger(brave_shields::kBraveShields, value);
}

void CheckMigrationFromResourceIdentifierForDictionary(
const base::DictionaryValue* dictionary,
const std::string& patterns_string,
const base::Time& expected_last_modified,
const int& expected_setting_value) {
const base::DictionaryValue* settings_dict = nullptr;
dictionary->GetDictionaryWithoutPathExpansion(patterns_string,
&settings_dict);
EXPECT_NE(settings_dict, nullptr);

int actual_value;
settings_dict->GetInteger(kSettingPath, &actual_value);
EXPECT_EQ(GetTimeStampFromDictionary(settings_dict, kLastModifiedPath),
expected_last_modified);
EXPECT_EQ(GetSessionModelFromDictionary(settings_dict, kSessionModelPath),
content_settings::SessionModel::Durable);
EXPECT_EQ(actual_value, expected_setting_value);
}

class ShieldsSetting {
public:
ShieldsSetting(BravePrefProvider* provider,
Expand Down Expand Up @@ -302,7 +380,7 @@ TEST_F(BravePrefProviderTest, TestShieldsSettingsMigrationVersion) {
true /* store_last_modified */,
false /* restore_session */);

// Should have migrated when constrcuted (with profile).
// Should have migrated when constructed (with profile).
EXPECT_EQ(2, prefs->GetInteger(kBraveShieldsSettingsVersion));

// Reset and check that migration runs.
Expand All @@ -318,4 +396,78 @@ TEST_F(BravePrefProviderTest, TestShieldsSettingsMigrationVersion) {
provider.ShutdownOnUIThread();
}

TEST_F(BravePrefProviderTest, TestShieldsSettingsMigrationFromResourceIDs) {
PrefService* pref_service = testing_profile()->GetPrefs();
BravePrefProvider provider(
pref_service, false /* incognito */, true /* store_last_modified */,
false /* restore_session */);

// Manually write settings under the PLUGINS type using the no longer existing
// ResourceIdentifier names, and then perform the migration.
prefs::ScopedDictionaryPrefUpdate plugins_pref_update(
pref_service, kUserProfilePluginsPath);
std::unique_ptr<prefs::DictionaryValueUpdate> plugins_dictionary =
plugins_pref_update.Get();
EXPECT_NE(plugins_dictionary, nullptr);

base::Time expected_last_modified = base::Time::Now();

// Seed global shield settings with non-default values.
std::unique_ptr<prefs::DictionaryValueUpdate> global_settings_dict =
plugins_dictionary->SetDictionaryWithoutPathExpansion(
"*,*", std::make_unique<base::DictionaryValue>());

const int expected_global_settings_value = 1;
InitializeAllShieldSettingsInDictionary(global_settings_dict.get(),
expected_last_modified,
expected_global_settings_value);

// Change all of those global settings for www.example.com.
std::unique_ptr<prefs::DictionaryValueUpdate> example_settings_dict =
plugins_dictionary->SetDictionaryWithoutPathExpansion(
"www.example.com,*", std::make_unique<base::DictionaryValue>());

const int expected_example_com_settings_value = 1;
InitializeAllShieldSettingsInDictionary(example_settings_dict.get(),
expected_last_modified,
expected_example_com_settings_value);

// Disable Brave Shields for www.brave.com.
std::unique_ptr<prefs::DictionaryValueUpdate> brave_settings_dict =
plugins_dictionary->SetDictionaryWithoutPathExpansion(
"www.brave.com,*", std::make_unique<base::DictionaryValue>());

const int expected_brave_com_settings_value = 1;
InitializeBraveShieldsSettingInDictionary(brave_settings_dict.get(),
expected_last_modified,
expected_brave_com_settings_value);

provider.MigrateShieldsSettingsFromResourceIds();

// Check migration for all the settings has been properly done.
for (auto content_type : GetShieldsContentSettingsTypes()) {
const base::DictionaryValue* brave_shields_dict =
pref_service->GetDictionary(GetShieldsSettingUserPrefsPath(
GetShieldsContentTypeName(content_type)));
EXPECT_NE(brave_shields_dict, nullptr);

if (content_type == ContentSettingsType::BRAVE_SHIELDS) {
// We only changed the value of BRAVE_SHIELDS in www.brave.com.
CheckMigrationFromResourceIdentifierForDictionary(
brave_shields_dict, "www.brave.com,*", expected_last_modified,
expected_brave_com_settings_value);
} else {
// All the other settings we changed them globally and in www.example.com.
CheckMigrationFromResourceIdentifierForDictionary(
brave_shields_dict, "*,*", expected_last_modified,
expected_global_settings_value);
CheckMigrationFromResourceIdentifierForDictionary(
brave_shields_dict, "www.example.com,*", expected_last_modified,
expected_example_com_settings_value);
}
}

provider.ShutdownOnUIThread();
}

} // namespace content_settings

0 comments on commit 7943ccb

Please sign in to comment.