Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prompt_config_unittest.cc

Issue 2614773008: Add a config class for the settings reset prompt. (Closed)
Patch Set: Addressed csharp's comments. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_config.h"
6
7 #include <map>
8 #include <set>
9
10 #include "base/strings/stringprintf.h"
11 #include "base/test/scoped_feature_list.h"
12 #include "components/variations/variations_params_manager.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "url/gurl.h"
16
17 namespace safe_browsing {
18
19 const char kTrialName[] = "trial";
20 // A SHA256 hash for "mydomain.com".
21 const char kDomainHash[] =
22 "0a79eaf6adb7b1e60d3fa548aa63105f525a00448efbb59ee965b9351a90ac31";
23
24 // Test class that initializes a ScopedFeatureList so that all tests
25 // start off with all features disabled.
26 class SettingsResetPromptConfigTest : public ::testing::Test {
27 protected:
28 typedef std::map<std::string, std::string> Parameters;
29
30 // Sets the settings reset prompt feature parameters, which has the
31 // side-effect of also enabling the feature.
32 void SetFeatureParams(const Parameters& params) {
33 static std::set<std::string> features = {kSettingsResetPrompt.name};
34
35 params_manager_.ClearAllVariationParams();
36 params_manager_.SetVariationParamsWithFeatureAssociations(kTrialName,
37 params, features);
38 }
39
40 void SetDefaultFeatureParams() {
41 Parameters default_params = {
42 {"domain_hashes", base::StringPrintf("{\"%s\": \"1\"}", kDomainHash)}};
43 SetFeatureParams(default_params);
44 }
45
46 variations::testing::VariationParamsManager params_manager_;
47 base::test::ScopedFeatureList scoped_feature_list_;
48 };
49
50 TEST_F(SettingsResetPromptConfigTest, IsPromptEnabled) {
51 EXPECT_FALSE(SettingsResetPromptConfig::IsPromptEnabled());
52
53 SetDefaultFeatureParams();
54 EXPECT_TRUE(SettingsResetPromptConfig::IsPromptEnabled());
55 }
56
57 TEST_F(SettingsResetPromptConfigTest, Create) {
58 // Should return nullptr when feature is not enabled.
59 EXPECT_FALSE(SettingsResetPromptConfig::Create());
60
61 scoped_feature_list_.InitAndEnableFeature(kSettingsResetPrompt);
62
63 // Check cases where |Create()| should return nullptr because of bad
64 // domain_hashes parameter.
65
66 // Parameter is missing.
67 EXPECT_FALSE(SettingsResetPromptConfig::Create());
68 SetFeatureParams({});
69 EXPECT_FALSE(SettingsResetPromptConfig::Create());
70
71 // Parameter is an empty string.
72 SetFeatureParams(Parameters({{"domain_hashes", ""}}));
73 EXPECT_FALSE(SettingsResetPromptConfig::Create());
74
75 // Invalid JSON.
76 SetFeatureParams(Parameters({{"domain_hashes", "bad json"}}));
77 EXPECT_FALSE(SettingsResetPromptConfig::Create());
78
79 // Parameter is not a JSON dictionary.
80 SetFeatureParams(Parameters({{"domain_hashes", "[3]"}}));
81 EXPECT_FALSE(SettingsResetPromptConfig::Create());
82
83 // Bad dictionary key.
84 SetFeatureParams(Parameters({{"domain_hashes", "\"bad key\": \"1\""}}));
85 EXPECT_FALSE(SettingsResetPromptConfig::Create());
86
87 // Dictionary key is too short.
88 SetFeatureParams(Parameters({{"domain_hashes", "\"1234abc\": \"1\""}}));
89 EXPECT_FALSE(SettingsResetPromptConfig::Create());
90
91 // Dictionary key has correct length, but is not a hex string.
92 std::string non_hex_key(64, 'x');
93 SetFeatureParams(
94 Parameters({{"domain_hashes", base::StringPrintf("{\"%s\": \"1\"}",
95 non_hex_key.c_str())}}));
96 EXPECT_FALSE(SettingsResetPromptConfig::Create());
97
98 // Correct key but non-integer value.
99 SetFeatureParams(Parameters(
100 {{"domain_hashes",
101 base::StringPrintf("{\"%s\": \"not integer\"}", kDomainHash)}}));
102 EXPECT_FALSE(SettingsResetPromptConfig::Create());
103
104 // Correct key but integer value that is too big.
105 std::string too_big_int(99, '1');
106 SetFeatureParams(Parameters(
107 {{"domain_hashes", base::StringPrintf("{\"%s\": \"%s\"}", kDomainHash,
108 too_big_int.c_str())}}));
109 EXPECT_FALSE(SettingsResetPromptConfig::Create());
110
111 // Correct key but negative integer value.
112 SetFeatureParams(
113 Parameters({{"domain_hashes",
114 base::StringPrintf("{\"%s\": \"-2\"}", kDomainHash)}}));
115 EXPECT_FALSE(SettingsResetPromptConfig::Create());
116
117 // Should return non-nullptr with a correct set of parameters.
118 SetDefaultFeatureParams();
119 EXPECT_TRUE(SettingsResetPromptConfig::Create());
120 }
121
122 TEST_F(SettingsResetPromptConfigTest, UrlToResetDomainId) {
123 SetDefaultFeatureParams();
124 auto config = SettingsResetPromptConfig::Create();
125 ASSERT_TRUE(config);
126
127 // Should return negative value for URL with no match in the config.
128 EXPECT_LT(config->UrlToResetDomainId(GURL("http://www.hello.com")), 0);
129
130 // Should return 1, which is "mydomain.com"'s ID.
131 EXPECT_EQ(config->UrlToResetDomainId(GURL("http://www.sub.mydomain.com")), 1);
132 EXPECT_EQ(config->UrlToResetDomainId(GURL("http://www.mydomain.com")), 1);
133 EXPECT_EQ(config->UrlToResetDomainId(GURL("http://mydomain.com")), 1);
134
135 // These URLs should not match "mydomain.com".
136 EXPECT_LT(config->UrlToResetDomainId(GURL("http://mydomain")), 0);
137 EXPECT_LT(config->UrlToResetDomainId(GURL("http://mydomain.org")), 0);
138 EXPECT_LT(config->UrlToResetDomainId(GURL("http://prefixmydomain.com")), 0);
139 EXPECT_LT(config->UrlToResetDomainId(GURL("http://mydomain.com.com")), 0);
140 EXPECT_LT(config->UrlToResetDomainId(GURL("http://www.mydomain.com.com")), 0);
141
142 // Should return negative value for invalid URLs.
143 EXPECT_LT(config->UrlToResetDomainId(GURL("htp://mydomain.com")), 0);
144 EXPECT_LT(config->UrlToResetDomainId(GURL("http://mydomain com")), 0);
145 }
146
147 } // namespace safe_browsing.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698