OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Unit tests for helper functions for the Chrome Extensions Proxy Settings API. | 5 // Unit tests for helper functions for the Chrome Extensions Proxy Settings API. |
6 | 6 |
7 #include "chrome/browser/extensions/api/proxy/proxy_api_helpers.h" | 7 #include "chrome/browser/extensions/api/proxy/proxy_api_helpers.h" |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
| 12 #include "base/memory/ptr_util.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "chrome/browser/extensions/api/proxy/proxy_api_constants.h" | 14 #include "chrome/browser/extensions/api/proxy/proxy_api_constants.h" |
14 #include "components/proxy_config/proxy_config_dictionary.h" | 15 #include "components/proxy_config/proxy_config_dictionary.h" |
15 #include "components/proxy_config/proxy_prefs.h" | 16 #include "components/proxy_config/proxy_prefs.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 namespace extensions { | 19 namespace extensions { |
19 | 20 |
20 namespace keys = proxy_api_constants; | 21 namespace keys = proxy_api_constants; |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 const char kSamplePacScript[] = "test"; | 25 const char kSamplePacScript[] = "test"; |
25 const char kSamplePacScriptAsDataUrl[] = | 26 const char kSamplePacScriptAsDataUrl[] = |
26 "data:application/x-ns-proxy-autoconfig;base64,dGVzdA=="; | 27 "data:application/x-ns-proxy-autoconfig;base64,dGVzdA=="; |
27 const char kSamplePacScriptAsDataUrl2[] = | 28 const char kSamplePacScriptAsDataUrl2[] = |
28 "data:;base64,dGVzdA=="; | 29 "data:;base64,dGVzdA=="; |
29 const char kSamplePacScriptUrl[] = "http://wpad/wpad.dat"; | 30 const char kSamplePacScriptUrl[] = "http://wpad/wpad.dat"; |
30 | 31 |
31 // Helper function to create a ProxyServer dictionary as defined in the | 32 // Helper function to create a ProxyServer dictionary as defined in the |
32 // extension API. | 33 // extension API. |
33 base::DictionaryValue* CreateTestProxyServerDict(const std::string& host) { | 34 std::unique_ptr<base::DictionaryValue> CreateTestProxyServerDict( |
34 base::DictionaryValue* dict = new base::DictionaryValue; | 35 const std::string& host) { |
| 36 auto dict = base::MakeUnique<base::DictionaryValue>(); |
35 dict->SetString(keys::kProxyConfigRuleHost, host); | 37 dict->SetString(keys::kProxyConfigRuleHost, host); |
36 return dict; | 38 return dict; |
37 } | 39 } |
38 | 40 |
39 // Helper function to create a ProxyServer dictionary as defined in the | 41 // Helper function to create a ProxyServer dictionary as defined in the |
40 // extension API. | 42 // extension API. |
41 base::DictionaryValue* CreateTestProxyServerDict(const std::string& schema, | 43 std::unique_ptr<base::DictionaryValue> CreateTestProxyServerDict( |
42 const std::string& host, | 44 const std::string& schema, |
43 int port) { | 45 const std::string& host, |
44 base::DictionaryValue* dict = new base::DictionaryValue; | 46 int port) { |
| 47 auto dict = base::MakeUnique<base::DictionaryValue>(); |
45 dict->SetString(keys::kProxyConfigRuleScheme, schema); | 48 dict->SetString(keys::kProxyConfigRuleScheme, schema); |
46 dict->SetString(keys::kProxyConfigRuleHost, host); | 49 dict->SetString(keys::kProxyConfigRuleHost, host); |
47 dict->SetInteger(keys::kProxyConfigRulePort, port); | 50 dict->SetInteger(keys::kProxyConfigRulePort, port); |
48 return dict; | 51 return dict; |
49 } | 52 } |
50 | 53 |
51 } // namespace | 54 } // namespace |
52 | 55 |
53 namespace proxy_api_helpers { | 56 namespace proxy_api_helpers { |
54 | 57 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 112 |
110 // Currently we are still missing a PAC script entry. | 113 // Currently we are still missing a PAC script entry. |
111 // This is silently ignored. | 114 // This is silently ignored. |
112 ASSERT_TRUE(GetPacUrlFromExtensionPref(&proxy_config, &out, &error, | 115 ASSERT_TRUE(GetPacUrlFromExtensionPref(&proxy_config, &out, &error, |
113 &bad_message)); | 116 &bad_message)); |
114 EXPECT_EQ(std::string(), out); | 117 EXPECT_EQ(std::string(), out); |
115 EXPECT_EQ(std::string(), error); | 118 EXPECT_EQ(std::string(), error); |
116 EXPECT_FALSE(bad_message); | 119 EXPECT_FALSE(bad_message); |
117 | 120 |
118 // Set up a pac script. | 121 // Set up a pac script. |
119 base::DictionaryValue* pacScriptDict = new base::DictionaryValue; | 122 auto pacScriptDict = base::MakeUnique<base::DictionaryValue>(); |
120 pacScriptDict->SetString(keys::kProxyConfigPacScriptUrl, kSamplePacScriptUrl); | 123 pacScriptDict->SetString(keys::kProxyConfigPacScriptUrl, kSamplePacScriptUrl); |
121 proxy_config.Set(keys::kProxyConfigPacScript, pacScriptDict); | 124 proxy_config.Set(keys::kProxyConfigPacScript, std::move(pacScriptDict)); |
122 | 125 |
123 ASSERT_TRUE(GetPacUrlFromExtensionPref(&proxy_config, &out, &error, | 126 ASSERT_TRUE(GetPacUrlFromExtensionPref(&proxy_config, &out, &error, |
124 &bad_message)); | 127 &bad_message)); |
125 EXPECT_EQ(kSamplePacScriptUrl, out); | 128 EXPECT_EQ(kSamplePacScriptUrl, out); |
126 EXPECT_EQ(std::string(), error); | 129 EXPECT_EQ(std::string(), error); |
127 EXPECT_FALSE(bad_message); | 130 EXPECT_FALSE(bad_message); |
128 } | 131 } |
129 | 132 |
130 TEST(ExtensionProxyApiHelpers, GetPacDataFromExtensionPref) { | 133 TEST(ExtensionProxyApiHelpers, GetPacDataFromExtensionPref) { |
131 std::string out; | 134 std::string out; |
132 std::string error; | 135 std::string error; |
133 bool bad_message = false; | 136 bool bad_message = false; |
134 | 137 |
135 base::DictionaryValue proxy_config; | 138 base::DictionaryValue proxy_config; |
136 proxy_config.SetString( | 139 proxy_config.SetString( |
137 keys::kProxyConfigMode, | 140 keys::kProxyConfigMode, |
138 ProxyPrefs::ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT)); | 141 ProxyPrefs::ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT)); |
139 | 142 |
140 // Currently we are still missing a PAC data entry. This is silently ignored. | 143 // Currently we are still missing a PAC data entry. This is silently ignored. |
141 ASSERT_TRUE(GetPacDataFromExtensionPref(&proxy_config, &out, &error, | 144 ASSERT_TRUE(GetPacDataFromExtensionPref(&proxy_config, &out, &error, |
142 &bad_message)); | 145 &bad_message)); |
143 EXPECT_EQ(std::string(), out); | 146 EXPECT_EQ(std::string(), out); |
144 EXPECT_EQ(std::string(), error); | 147 EXPECT_EQ(std::string(), error); |
145 EXPECT_FALSE(bad_message); | 148 EXPECT_FALSE(bad_message); |
146 | 149 |
147 // Set up a PAC script. | 150 // Set up a PAC script. |
148 base::DictionaryValue* pacScriptDict = new base::DictionaryValue; | 151 auto pacScriptDict = base::MakeUnique<base::DictionaryValue>(); |
149 pacScriptDict->SetString(keys::kProxyConfigPacScriptData, kSamplePacScript); | 152 pacScriptDict->SetString(keys::kProxyConfigPacScriptData, kSamplePacScript); |
150 proxy_config.Set(keys::kProxyConfigPacScript, pacScriptDict); | 153 proxy_config.Set(keys::kProxyConfigPacScript, std::move(pacScriptDict)); |
151 | 154 |
152 ASSERT_TRUE(GetPacDataFromExtensionPref(&proxy_config, &out, &error, | 155 ASSERT_TRUE(GetPacDataFromExtensionPref(&proxy_config, &out, &error, |
153 &bad_message)); | 156 &bad_message)); |
154 EXPECT_EQ(kSamplePacScript, out); | 157 EXPECT_EQ(kSamplePacScript, out); |
155 EXPECT_EQ(std::string(), error); | 158 EXPECT_EQ(std::string(), error); |
156 EXPECT_FALSE(bad_message); | 159 EXPECT_FALSE(bad_message); |
157 } | 160 } |
158 | 161 |
159 TEST(ExtensionProxyApiHelpers, GetProxyRulesStringFromExtensionPref) { | 162 TEST(ExtensionProxyApiHelpers, GetProxyRulesStringFromExtensionPref) { |
160 std::string out; | 163 std::string out; |
161 std::string error; | 164 std::string error; |
162 bool bad_message = false; | 165 bool bad_message = false; |
163 | 166 |
164 base::DictionaryValue proxy_config; | 167 base::DictionaryValue proxy_config; |
165 proxy_config.SetString( | 168 proxy_config.SetString( |
166 keys::kProxyConfigMode, | 169 keys::kProxyConfigMode, |
167 ProxyPrefs::ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); | 170 ProxyPrefs::ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); |
168 | 171 |
169 // Currently we are still missing a proxy config entry. | 172 // Currently we are still missing a proxy config entry. |
170 // This is silently ignored. | 173 // This is silently ignored. |
171 ASSERT_TRUE( | 174 ASSERT_TRUE( |
172 GetProxyRulesStringFromExtensionPref(&proxy_config, &out, &error, | 175 GetProxyRulesStringFromExtensionPref(&proxy_config, &out, &error, |
173 &bad_message)); | 176 &bad_message)); |
174 EXPECT_EQ(std::string(), out); | 177 EXPECT_EQ(std::string(), out); |
175 EXPECT_EQ(std::string(), error); | 178 EXPECT_EQ(std::string(), error); |
176 | 179 |
177 base::DictionaryValue* proxy_rules = new base::DictionaryValue; | 180 auto proxy_rules = base::MakeUnique<base::DictionaryValue>(); |
178 proxy_rules->Set(keys::field_name[1], CreateTestProxyServerDict("proxy1")); | 181 proxy_rules->Set(keys::field_name[1], CreateTestProxyServerDict("proxy1")); |
179 proxy_rules->Set(keys::field_name[2], CreateTestProxyServerDict("proxy2")); | 182 proxy_rules->Set(keys::field_name[2], CreateTestProxyServerDict("proxy2")); |
180 proxy_config.Set(keys::kProxyConfigRules, proxy_rules); | 183 proxy_config.Set(keys::kProxyConfigRules, std::move(proxy_rules)); |
181 | 184 |
182 ASSERT_TRUE( | 185 ASSERT_TRUE( |
183 GetProxyRulesStringFromExtensionPref(&proxy_config, &out, &error, | 186 GetProxyRulesStringFromExtensionPref(&proxy_config, &out, &error, |
184 &bad_message)); | 187 &bad_message)); |
185 EXPECT_EQ("http=proxy1:80;https=proxy2:80", out); | 188 EXPECT_EQ("http=proxy1:80;https=proxy2:80", out); |
186 EXPECT_EQ(std::string(), error); | 189 EXPECT_EQ(std::string(), error); |
187 EXPECT_FALSE(bad_message); | 190 EXPECT_FALSE(bad_message); |
188 } | 191 } |
189 | 192 |
190 TEST(ExtensionProxyApiHelpers, GetBypassListFromExtensionPref) { | 193 TEST(ExtensionProxyApiHelpers, GetBypassListFromExtensionPref) { |
191 std::string out; | 194 std::string out; |
192 std::string error; | 195 std::string error; |
193 bool bad_message = false; | 196 bool bad_message = false; |
194 | 197 |
195 base::DictionaryValue proxy_config; | 198 base::DictionaryValue proxy_config; |
196 proxy_config.SetString( | 199 proxy_config.SetString( |
197 keys::kProxyConfigMode, | 200 keys::kProxyConfigMode, |
198 ProxyPrefs::ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); | 201 ProxyPrefs::ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); |
199 | 202 |
200 // Currently we are still missing a proxy config entry. | 203 // Currently we are still missing a proxy config entry. |
201 // This is silently ignored. | 204 // This is silently ignored. |
202 ASSERT_TRUE( | 205 ASSERT_TRUE( |
203 GetBypassListFromExtensionPref(&proxy_config, &out, &error, | 206 GetBypassListFromExtensionPref(&proxy_config, &out, &error, |
204 &bad_message)); | 207 &bad_message)); |
205 EXPECT_EQ(std::string(), out); | 208 EXPECT_EQ(std::string(), out); |
206 EXPECT_EQ(std::string(), error); | 209 EXPECT_EQ(std::string(), error); |
207 EXPECT_FALSE(bad_message); | 210 EXPECT_FALSE(bad_message); |
208 | 211 |
209 base::ListValue* bypass_list = new base::ListValue; | 212 auto bypass_list = base::MakeUnique<base::ListValue>(); |
210 bypass_list->AppendString("host1"); | 213 bypass_list->AppendString("host1"); |
211 bypass_list->AppendString("host2"); | 214 bypass_list->AppendString("host2"); |
212 base::DictionaryValue* proxy_rules = new base::DictionaryValue; | 215 auto proxy_rules = base::MakeUnique<base::DictionaryValue>(); |
213 proxy_rules->Set(keys::kProxyConfigBypassList, bypass_list); | 216 proxy_rules->Set(keys::kProxyConfigBypassList, std::move(bypass_list)); |
214 proxy_config.Set(keys::kProxyConfigRules, proxy_rules); | 217 proxy_config.Set(keys::kProxyConfigRules, std::move(proxy_rules)); |
215 | 218 |
216 ASSERT_TRUE( | 219 ASSERT_TRUE( |
217 GetBypassListFromExtensionPref(&proxy_config, &out, &error, | 220 GetBypassListFromExtensionPref(&proxy_config, &out, &error, |
218 &bad_message)); | 221 &bad_message)); |
219 EXPECT_EQ("host1,host2", out); | 222 EXPECT_EQ("host1,host2", out); |
220 EXPECT_EQ(std::string(), error); | 223 EXPECT_EQ(std::string(), error); |
221 EXPECT_FALSE(bad_message); | 224 EXPECT_FALSE(bad_message); |
222 } | 225 } |
223 | 226 |
224 TEST(ExtensionProxyApiHelpers, CreateProxyConfigDict) { | 227 TEST(ExtensionProxyApiHelpers, CreateProxyConfigDict) { |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 | 323 |
321 std::unique_ptr<base::DictionaryValue> expected(new base::DictionaryValue); | 324 std::unique_ptr<base::DictionaryValue> expected(new base::DictionaryValue); |
322 expected->Set("proxyForHttp", | 325 expected->Set("proxyForHttp", |
323 CreateTestProxyServerDict("http", "proxy1", 80)); | 326 CreateTestProxyServerDict("http", "proxy1", 80)); |
324 expected->Set("proxyForHttps", | 327 expected->Set("proxyForHttps", |
325 CreateTestProxyServerDict("http", "proxy2", 80)); | 328 CreateTestProxyServerDict("http", "proxy2", 80)); |
326 expected->Set("proxyForFtp", | 329 expected->Set("proxyForFtp", |
327 CreateTestProxyServerDict("http", "proxy3", 80)); | 330 CreateTestProxyServerDict("http", "proxy3", 80)); |
328 expected->Set("fallbackProxy", | 331 expected->Set("fallbackProxy", |
329 CreateTestProxyServerDict("socks4", "proxy4", 80)); | 332 CreateTestProxyServerDict("socks4", "proxy4", 80)); |
330 base::ListValue* bypass_list = new base::ListValue; | 333 auto bypass_list = base::MakeUnique<base::ListValue>(); |
331 bypass_list->AppendString("localhost"); | 334 bypass_list->AppendString("localhost"); |
332 expected->Set(keys::kProxyConfigBypassList, bypass_list); | 335 expected->Set(keys::kProxyConfigBypassList, std::move(bypass_list)); |
333 | 336 |
334 EXPECT_TRUE(base::Value::Equals(expected.get(), extension_pref.get())); | 337 EXPECT_TRUE(base::Value::Equals(expected.get(), extension_pref.get())); |
335 } | 338 } |
336 | 339 |
337 // Test multiple proxies per scheme -- expect that only the first is returned. | 340 // Test multiple proxies per scheme -- expect that only the first is returned. |
338 TEST(ExtensionProxyApiHelpers, CreateProxyRulesDictMultipleProxies) { | 341 TEST(ExtensionProxyApiHelpers, CreateProxyRulesDictMultipleProxies) { |
339 std::unique_ptr<base::DictionaryValue> browser_pref( | 342 std::unique_ptr<base::DictionaryValue> browser_pref( |
340 ProxyConfigDictionary::CreateFixedServers( | 343 ProxyConfigDictionary::CreateFixedServers( |
341 "http=proxy1:80,default://;https=proxy2:80,proxy1:80;ftp=proxy3:80," | 344 "http=proxy1:80,default://;https=proxy2:80,proxy1:80;ftp=proxy3:80," |
342 "https://proxy5:443;socks=proxy4:80,proxy1:80", | 345 "https://proxy5:443;socks=proxy4:80,proxy1:80", |
343 "localhost")); | 346 "localhost")); |
344 ProxyConfigDictionary config(std::move(browser_pref)); | 347 ProxyConfigDictionary config(std::move(browser_pref)); |
345 std::unique_ptr<base::DictionaryValue> extension_pref( | 348 std::unique_ptr<base::DictionaryValue> extension_pref( |
346 CreateProxyRulesDict(config)); | 349 CreateProxyRulesDict(config)); |
347 ASSERT_TRUE(extension_pref.get()); | 350 ASSERT_TRUE(extension_pref.get()); |
348 | 351 |
349 std::unique_ptr<base::DictionaryValue> expected(new base::DictionaryValue); | 352 std::unique_ptr<base::DictionaryValue> expected(new base::DictionaryValue); |
350 expected->Set("proxyForHttp", | 353 expected->Set("proxyForHttp", |
351 CreateTestProxyServerDict("http", "proxy1", 80)); | 354 CreateTestProxyServerDict("http", "proxy1", 80)); |
352 expected->Set("proxyForHttps", | 355 expected->Set("proxyForHttps", |
353 CreateTestProxyServerDict("http", "proxy2", 80)); | 356 CreateTestProxyServerDict("http", "proxy2", 80)); |
354 expected->Set("proxyForFtp", | 357 expected->Set("proxyForFtp", |
355 CreateTestProxyServerDict("http", "proxy3", 80)); | 358 CreateTestProxyServerDict("http", "proxy3", 80)); |
356 expected->Set("fallbackProxy", | 359 expected->Set("fallbackProxy", |
357 CreateTestProxyServerDict("socks4", "proxy4", 80)); | 360 CreateTestProxyServerDict("socks4", "proxy4", 80)); |
358 base::ListValue* bypass_list = new base::ListValue; | 361 auto bypass_list = base::MakeUnique<base::ListValue>(); |
359 bypass_list->AppendString("localhost"); | 362 bypass_list->AppendString("localhost"); |
360 expected->Set(keys::kProxyConfigBypassList, bypass_list); | 363 expected->Set(keys::kProxyConfigBypassList, std::move(bypass_list)); |
361 | 364 |
362 EXPECT_TRUE(base::Value::Equals(expected.get(), extension_pref.get())); | 365 EXPECT_TRUE(base::Value::Equals(expected.get(), extension_pref.get())); |
363 } | 366 } |
364 | 367 |
365 // Test if a PAC script URL is specified. | 368 // Test if a PAC script URL is specified. |
366 TEST(ExtensionProxyApiHelpers, CreatePacScriptDictWithUrl) { | 369 TEST(ExtensionProxyApiHelpers, CreatePacScriptDictWithUrl) { |
367 std::unique_ptr<base::DictionaryValue> browser_pref( | 370 std::unique_ptr<base::DictionaryValue> browser_pref( |
368 ProxyConfigDictionary::CreatePacScript(kSamplePacScriptUrl, false)); | 371 ProxyConfigDictionary::CreatePacScript(kSamplePacScriptUrl, false)); |
369 ProxyConfigDictionary config(std::move(browser_pref)); | 372 ProxyConfigDictionary config(std::move(browser_pref)); |
370 std::unique_ptr<base::DictionaryValue> extension_pref( | 373 std::unique_ptr<base::DictionaryValue> extension_pref( |
(...skipping 28 matching lines...) Expand all Loading... |
399 expected.AppendString("s1"); | 402 expected.AppendString("s1"); |
400 expected.AppendString("s2"); | 403 expected.AppendString("s2"); |
401 expected.AppendString("s3"); | 404 expected.AppendString("s3"); |
402 | 405 |
403 std::unique_ptr<base::ListValue> out(TokenizeToStringList("s1;s2;s3", ";")); | 406 std::unique_ptr<base::ListValue> out(TokenizeToStringList("s1;s2;s3", ";")); |
404 EXPECT_TRUE(base::Value::Equals(&expected, out.get())); | 407 EXPECT_TRUE(base::Value::Equals(&expected, out.get())); |
405 } | 408 } |
406 | 409 |
407 } // namespace proxy_api_helpers | 410 } // namespace proxy_api_helpers |
408 } // namespace extensions | 411 } // namespace extensions |
OLD | NEW |