Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / prefs / command_line_pref_store_unittest.cc
blob2799e29a00de1742be7daeb636f030665778867b
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
3 // found in the LICENSE file.
5 #include <gtest/gtest.h>
7 #include "base/command_line.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/strings/string_util.h"
10 #include "base/values.h"
11 #include "chrome/browser/prefs/command_line_pref_store.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/proxy_config/proxy_config_dictionary.h"
15 #include "components/proxy_config/proxy_config_pref_names.h"
16 #include "ui/base/ui_base_switches.h"
18 namespace {
20 const char unknown_bool[] = "unknown_switch";
21 const char unknown_string[] = "unknown_other_switch";
23 } // namespace
25 class TestCommandLinePrefStore : public CommandLinePrefStore {
26 public:
27 explicit TestCommandLinePrefStore(base::CommandLine* cl)
28 : CommandLinePrefStore(cl) {}
30 bool ProxySwitchesAreValid() {
31 return ValidateProxySwitches();
34 void VerifyProxyMode(ProxyPrefs::ProxyMode expected_mode) {
35 const base::Value* value = NULL;
36 ASSERT_TRUE(GetValue(proxy_config::prefs::kProxy, &value));
37 ASSERT_EQ(base::Value::TYPE_DICTIONARY, value->GetType());
38 ProxyConfigDictionary dict(
39 static_cast<const base::DictionaryValue*>(value));
40 ProxyPrefs::ProxyMode actual_mode;
41 ASSERT_TRUE(dict.GetMode(&actual_mode));
42 EXPECT_EQ(expected_mode, actual_mode);
45 void VerifySSLCipherSuites(const char* const* ciphers,
46 size_t cipher_count) {
47 const base::Value* value = NULL;
48 ASSERT_TRUE(GetValue(prefs::kCipherSuiteBlacklist, &value));
49 ASSERT_EQ(base::Value::TYPE_LIST, value->GetType());
50 const base::ListValue* list_value =
51 static_cast<const base::ListValue*>(value);
52 ASSERT_EQ(cipher_count, list_value->GetSize());
54 std::string cipher_string;
55 for (base::ListValue::const_iterator it = list_value->begin();
56 it != list_value->end(); ++it, ++ciphers) {
57 ASSERT_TRUE((*it)->GetAsString(&cipher_string));
58 EXPECT_EQ(*ciphers, cipher_string);
62 private:
63 ~TestCommandLinePrefStore() override {}
66 // Tests a simple string pref on the command line.
67 TEST(CommandLinePrefStoreTest, SimpleStringPref) {
68 base::CommandLine cl(base::CommandLine::NO_PROGRAM);
69 cl.AppendSwitchASCII(switches::kLang, "hi-MOM");
70 scoped_refptr<CommandLinePrefStore> store = new CommandLinePrefStore(&cl);
72 const base::Value* actual = NULL;
73 EXPECT_TRUE(store->GetValue(prefs::kApplicationLocale, &actual));
74 std::string result;
75 EXPECT_TRUE(actual->GetAsString(&result));
76 EXPECT_EQ("hi-MOM", result);
79 // Tests a simple boolean pref on the command line.
80 TEST(CommandLinePrefStoreTest, SimpleBooleanPref) {
81 base::CommandLine cl(base::CommandLine::NO_PROGRAM);
82 cl.AppendSwitch(switches::kNoProxyServer);
83 scoped_refptr<TestCommandLinePrefStore> store =
84 new TestCommandLinePrefStore(&cl);
86 store->VerifyProxyMode(ProxyPrefs::MODE_DIRECT);
89 // Tests a command line with no recognized prefs.
90 TEST(CommandLinePrefStoreTest, NoPrefs) {
91 base::CommandLine cl(base::CommandLine::NO_PROGRAM);
92 cl.AppendSwitch(unknown_string);
93 cl.AppendSwitchASCII(unknown_bool, "a value");
94 scoped_refptr<CommandLinePrefStore> store = new CommandLinePrefStore(&cl);
96 const base::Value* actual = NULL;
97 EXPECT_FALSE(store->GetValue(unknown_bool, &actual));
98 EXPECT_FALSE(store->GetValue(unknown_string, &actual));
101 // Tests a complex command line with multiple known and unknown switches.
102 TEST(CommandLinePrefStoreTest, MultipleSwitches) {
103 base::CommandLine cl(base::CommandLine::NO_PROGRAM);
104 cl.AppendSwitch(unknown_string);
105 cl.AppendSwitchASCII(switches::kProxyServer, "proxy");
106 cl.AppendSwitchASCII(switches::kProxyBypassList, "list");
107 cl.AppendSwitchASCII(unknown_bool, "a value");
108 scoped_refptr<TestCommandLinePrefStore> store =
109 new TestCommandLinePrefStore(&cl);
111 const base::Value* actual = NULL;
112 EXPECT_FALSE(store->GetValue(unknown_bool, &actual));
113 EXPECT_FALSE(store->GetValue(unknown_string, &actual));
115 store->VerifyProxyMode(ProxyPrefs::MODE_FIXED_SERVERS);
117 const base::Value* value = NULL;
118 ASSERT_TRUE(store->GetValue(proxy_config::prefs::kProxy, &value));
119 ASSERT_EQ(base::Value::TYPE_DICTIONARY, value->GetType());
120 ProxyConfigDictionary dict(static_cast<const base::DictionaryValue*>(value));
122 std::string string_result;
124 ASSERT_TRUE(dict.GetProxyServer(&string_result));
125 EXPECT_EQ("proxy", string_result);
127 ASSERT_TRUE(dict.GetBypassList(&string_result));
128 EXPECT_EQ("list", string_result);
131 // Tests proxy switch validation.
132 TEST(CommandLinePrefStoreTest, ProxySwitchValidation) {
133 base::CommandLine cl(base::CommandLine::NO_PROGRAM);
135 // No switches.
136 scoped_refptr<TestCommandLinePrefStore> store =
137 new TestCommandLinePrefStore(&cl);
138 EXPECT_TRUE(store->ProxySwitchesAreValid());
140 // Only no-proxy.
141 cl.AppendSwitch(switches::kNoProxyServer);
142 scoped_refptr<TestCommandLinePrefStore> store2 =
143 new TestCommandLinePrefStore(&cl);
144 EXPECT_TRUE(store2->ProxySwitchesAreValid());
146 // Another proxy switch too.
147 cl.AppendSwitch(switches::kProxyAutoDetect);
148 scoped_refptr<TestCommandLinePrefStore> store3 =
149 new TestCommandLinePrefStore(&cl);
150 EXPECT_FALSE(store3->ProxySwitchesAreValid());
152 // All proxy switches except no-proxy.
153 base::CommandLine cl2(base::CommandLine::NO_PROGRAM);
154 cl2.AppendSwitch(switches::kProxyAutoDetect);
155 cl2.AppendSwitchASCII(switches::kProxyServer, "server");
156 cl2.AppendSwitchASCII(switches::kProxyPacUrl, "url");
157 cl2.AppendSwitchASCII(switches::kProxyBypassList, "list");
158 scoped_refptr<TestCommandLinePrefStore> store4 =
159 new TestCommandLinePrefStore(&cl2);
160 EXPECT_TRUE(store4->ProxySwitchesAreValid());
163 TEST(CommandLinePrefStoreTest, ManualProxyModeInference) {
164 base::CommandLine cl1(base::CommandLine::NO_PROGRAM);
165 cl1.AppendSwitch(unknown_string);
166 cl1.AppendSwitchASCII(switches::kProxyServer, "proxy");
167 scoped_refptr<TestCommandLinePrefStore> store1 =
168 new TestCommandLinePrefStore(&cl1);
169 store1->VerifyProxyMode(ProxyPrefs::MODE_FIXED_SERVERS);
171 base::CommandLine cl2(base::CommandLine::NO_PROGRAM);
172 cl2.AppendSwitchASCII(switches::kProxyPacUrl, "proxy");
173 scoped_refptr<TestCommandLinePrefStore> store2 =
174 new TestCommandLinePrefStore(&cl2);
175 store2->VerifyProxyMode(ProxyPrefs::MODE_PAC_SCRIPT);
177 base::CommandLine cl3(base::CommandLine::NO_PROGRAM);
178 cl3.AppendSwitchASCII(switches::kProxyServer, std::string());
179 scoped_refptr<TestCommandLinePrefStore> store3 =
180 new TestCommandLinePrefStore(&cl3);
181 store3->VerifyProxyMode(ProxyPrefs::MODE_DIRECT);
184 TEST(CommandLinePrefStoreTest, DisableSSLCipherSuites) {
185 base::CommandLine cl1(base::CommandLine::NO_PROGRAM);
186 cl1.AppendSwitchASCII(switches::kCipherSuiteBlacklist,
187 "0x0004,0x0005");
188 scoped_refptr<TestCommandLinePrefStore> store1 =
189 new TestCommandLinePrefStore(&cl1);
190 const char* const expected_ciphers1[] = {
191 "0x0004",
192 "0x0005",
194 store1->VerifySSLCipherSuites(expected_ciphers1,
195 arraysize(expected_ciphers1));
197 base::CommandLine cl2(base::CommandLine::NO_PROGRAM);
198 cl2.AppendSwitchASCII(switches::kCipherSuiteBlacklist,
199 "0x0004, WHITESPACE_IGNORED TEST , 0x0005");
200 scoped_refptr<TestCommandLinePrefStore> store2 =
201 new TestCommandLinePrefStore(&cl2);
202 const char* const expected_ciphers2[] = {
203 "0x0004",
204 "WHITESPACE_IGNORED TEST",
205 "0x0005",
207 store2->VerifySSLCipherSuites(expected_ciphers2,
208 arraysize(expected_ciphers2));
210 base::CommandLine cl3(base::CommandLine::NO_PROGRAM);
211 cl3.AppendSwitchASCII(switches::kCipherSuiteBlacklist,
212 "0x0004;MOAR;0x0005");
213 scoped_refptr<TestCommandLinePrefStore> store3 =
214 new TestCommandLinePrefStore(&cl3);
215 const char* const expected_ciphers3[] = {
216 "0x0004;MOAR;0x0005"
218 store3->VerifySSLCipherSuites(expected_ciphers3,
219 arraysize(expected_ciphers3));