OLD | NEW |
---|---|
(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 TEST_F(SettingsResetPromptConfigTest, UrlToResetDomainIdTLDs) { | |
148 // Ensure that we do not match top level or registry domains even in the | |
149 // presence of faulty config data that would match those. | |
150 | |
151 // Hash for "com". | |
152 const char kTLDHash1[] = | |
153 "71b4f3a3748cd6843c01e293e701fce769f52381821e21daf2ff4fe9ea57a6f3"; | |
154 // Hash for "co.uk". | |
155 const char kTLDHash2[] = | |
156 "ad4fad2f5e5fb480ff7f9f648c6b20bbd5e44362d86821e29d30e65e626299b0"; | |
157 // Hash for "com.br". | |
158 const char kTLDHash3[] = | |
159 "1d9c4ffc5429a9b4529abf6fbe9f20b52b7401c8f0fed46c7ed67b1e3153932c"; | |
160 // Hash for private registry domain "appspot.com". | |
161 const char kTLDHash4[] = | |
162 "bffd48c8162466106a84f42945bfbbcfe501c9f0931219e02ce46e275f05ba51"; | |
robertshield
2017/01/11 20:40:12
Could you also add a test for ".uk" here? I reckon
alito
2017/01/11 20:55:30
Done.
| |
163 | |
164 SetFeatureParams(Parameters( | |
165 {{"domain_hashes", | |
166 base::StringPrintf( | |
167 "{\"%s\": \"1\", \"%s\": \"2\", \"%s\": \"3\", \"%s\": \"4\"}", | |
168 kTLDHash1, kTLDHash2, kTLDHash3, kTLDHash4)}})); | |
169 auto config = SettingsResetPromptConfig::Create(); | |
170 ASSERT_TRUE(config); | |
171 | |
172 EXPECT_LT(config->UrlToResetDomainId(GURL("http://something.com")), 0); | |
173 EXPECT_LT(config->UrlToResetDomainId(GURL("http://something.co.uk")), 0); | |
174 EXPECT_LT(config->UrlToResetDomainId(GURL("http://something.com.br")), 0); | |
175 EXPECT_LT(config->UrlToResetDomainId(GURL("http://something.appspot.com")), | |
176 0); | |
177 EXPECT_LT(config->UrlToResetDomainId(GURL("http://com")), 0); | |
178 EXPECT_LT(config->UrlToResetDomainId(GURL("http://co.uk")), 0); | |
179 EXPECT_LT(config->UrlToResetDomainId(GURL("http://com.br")), 0); | |
180 EXPECT_LT(config->UrlToResetDomainId(GURL("http://appspot.com")), 0); | |
181 } | |
182 | |
183 } // namespace safe_browsing | |
OLD | NEW |