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