| Index: chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prompt_config_unittest.cc
|
| diff --git a/chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prompt_config_unittest.cc b/chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prompt_config_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c9af807740b167b14df737cc85390967023657b7
|
| --- /dev/null
|
| +++ b/chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prompt_config_unittest.cc
|
| @@ -0,0 +1,147 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prompt_config.h"
|
| +
|
| +#include <map>
|
| +#include <set>
|
| +
|
| +#include "base/strings/stringprintf.h"
|
| +#include "base/test/scoped_feature_list.h"
|
| +#include "components/variations/variations_params_manager.h"
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "url/gurl.h"
|
| +
|
| +namespace safe_browsing {
|
| +
|
| +const char kTrialName[] = "trial";
|
| +// A SHA256 hash for "mydomain.com".
|
| +const char kDomainHash[] =
|
| + "0a79eaf6adb7b1e60d3fa548aa63105f525a00448efbb59ee965b9351a90ac31";
|
| +
|
| +// Test class that initializes a ScopedFeatureList so that all tests
|
| +// start off with all features disabled.
|
| +class SettingsResetPromptConfigTest : public ::testing::Test {
|
| + protected:
|
| + typedef std::map<std::string, std::string> Parameters;
|
| +
|
| + // Sets the settings reset prompt feature parameters, which has the
|
| + // side-effect of also enabling the feature.
|
| + void SetFeatureParams(const Parameters& params) {
|
| + static std::set<std::string> features = {kSettingsResetPrompt.name};
|
| +
|
| + params_manager_.ClearAllVariationParams();
|
| + params_manager_.SetVariationParamsWithFeatureAssociations(kTrialName,
|
| + params, features);
|
| + }
|
| +
|
| + void SetDefaultFeatureParams() {
|
| + Parameters default_params = {
|
| + {"domain_hashes", base::StringPrintf("{\"%s\": \"1\"}", kDomainHash)}};
|
| + SetFeatureParams(default_params);
|
| + }
|
| +
|
| + variations::testing::VariationParamsManager params_manager_;
|
| + base::test::ScopedFeatureList scoped_feature_list_;
|
| +};
|
| +
|
| +TEST_F(SettingsResetPromptConfigTest, IsPromptEnabled) {
|
| + EXPECT_FALSE(SettingsResetPromptConfig::IsPromptEnabled());
|
| +
|
| + SetDefaultFeatureParams();
|
| + EXPECT_TRUE(SettingsResetPromptConfig::IsPromptEnabled());
|
| +}
|
| +
|
| +TEST_F(SettingsResetPromptConfigTest, Create) {
|
| + // Should return nullptr when feature is not enabled.
|
| + EXPECT_FALSE(SettingsResetPromptConfig::Create());
|
| +
|
| + scoped_feature_list_.InitAndEnableFeature(kSettingsResetPrompt);
|
| +
|
| + // Check cases where |Create()| should return nullptr because of bad
|
| + // domain_hashes parameter.
|
| +
|
| + // Parameter is missing.
|
| + EXPECT_FALSE(SettingsResetPromptConfig::Create());
|
| + SetFeatureParams({});
|
| + EXPECT_FALSE(SettingsResetPromptConfig::Create());
|
| +
|
| + // Parameter is an empty string.
|
| + SetFeatureParams(Parameters({{"domain_hashes", ""}}));
|
| + EXPECT_FALSE(SettingsResetPromptConfig::Create());
|
| +
|
| + // Invalid JSON.
|
| + SetFeatureParams(Parameters({{"domain_hashes", "bad json"}}));
|
| + EXPECT_FALSE(SettingsResetPromptConfig::Create());
|
| +
|
| + // Parameter is not a JSON dictionary.
|
| + SetFeatureParams(Parameters({{"domain_hashes", "[3]"}}));
|
| + EXPECT_FALSE(SettingsResetPromptConfig::Create());
|
| +
|
| + // Bad dictionary key.
|
| + SetFeatureParams(Parameters({{"domain_hashes", "\"bad key\": \"1\""}}));
|
| + EXPECT_FALSE(SettingsResetPromptConfig::Create());
|
| +
|
| + // Dictionary key is too short.
|
| + SetFeatureParams(Parameters({{"domain_hashes", "\"1234abc\": \"1\""}}));
|
| + EXPECT_FALSE(SettingsResetPromptConfig::Create());
|
| +
|
| + // Dictionary key has correct length, but is not a hex string.
|
| + std::string non_hex_key(64, 'x');
|
| + SetFeatureParams(
|
| + Parameters({{"domain_hashes", base::StringPrintf("{\"%s\": \"1\"}",
|
| + non_hex_key.c_str())}}));
|
| + EXPECT_FALSE(SettingsResetPromptConfig::Create());
|
| +
|
| + // Correct key but non-integer value.
|
| + SetFeatureParams(Parameters(
|
| + {{"domain_hashes",
|
| + base::StringPrintf("{\"%s\": \"not integer\"}", kDomainHash)}}));
|
| + EXPECT_FALSE(SettingsResetPromptConfig::Create());
|
| +
|
| + // Correct key but integer value that is too big.
|
| + std::string too_big_int(99, '1');
|
| + SetFeatureParams(Parameters(
|
| + {{"domain_hashes", base::StringPrintf("{\"%s\": \"%s\"}", kDomainHash,
|
| + too_big_int.c_str())}}));
|
| + EXPECT_FALSE(SettingsResetPromptConfig::Create());
|
| +
|
| + // Correct key but negative integer value.
|
| + SetFeatureParams(
|
| + Parameters({{"domain_hashes",
|
| + base::StringPrintf("{\"%s\": \"-2\"}", kDomainHash)}}));
|
| + EXPECT_FALSE(SettingsResetPromptConfig::Create());
|
| +
|
| + // Should return non-nullptr with a correct set of parameters.
|
| + SetDefaultFeatureParams();
|
| + EXPECT_TRUE(SettingsResetPromptConfig::Create());
|
| +}
|
| +
|
| +TEST_F(SettingsResetPromptConfigTest, UrlToResetDomainId) {
|
| + SetDefaultFeatureParams();
|
| + auto config = SettingsResetPromptConfig::Create();
|
| + ASSERT_TRUE(config);
|
| +
|
| + // Should return negative value for URL with no match in the config.
|
| + EXPECT_LT(config->UrlToResetDomainId(GURL("http://www.hello.com")), 0);
|
| +
|
| + // Should return 1, which is "mydomain.com"'s ID.
|
| + EXPECT_EQ(config->UrlToResetDomainId(GURL("http://www.sub.mydomain.com")), 1);
|
| + EXPECT_EQ(config->UrlToResetDomainId(GURL("http://www.mydomain.com")), 1);
|
| + EXPECT_EQ(config->UrlToResetDomainId(GURL("http://mydomain.com")), 1);
|
| +
|
| + // These URLs should not match "mydomain.com".
|
| + EXPECT_LT(config->UrlToResetDomainId(GURL("http://mydomain")), 0);
|
| + EXPECT_LT(config->UrlToResetDomainId(GURL("http://mydomain.org")), 0);
|
| + EXPECT_LT(config->UrlToResetDomainId(GURL("http://prefixmydomain.com")), 0);
|
| + EXPECT_LT(config->UrlToResetDomainId(GURL("http://mydomain.com.com")), 0);
|
| + EXPECT_LT(config->UrlToResetDomainId(GURL("http://www.mydomain.com.com")), 0);
|
| +
|
| + // Should return negative value for invalid URLs.
|
| + EXPECT_LT(config->UrlToResetDomainId(GURL("htp://mydomain.com")), 0);
|
| + EXPECT_LT(config->UrlToResetDomainId(GURL("http://mydomain com")), 0);
|
| +}
|
| +
|
| +} // namespace safe_browsing.
|
|
|