1// Copyright (c) 2011 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
7#include "base/command_line.h"
8#include "base/memory/ref_counted.h"
9#include "base/string_util.h"
10#include "base/values.h"
11#include "chrome/browser/prefs/command_line_pref_store.h"
12#include "chrome/browser/prefs/proxy_config_dictionary.h"
13#include "chrome/common/chrome_switches.h"
14#include "chrome/common/pref_names.h"
15#include "ui/base/ui_base_switches.h"
16
17namespace {
18
19class TestCommandLinePrefStore : public CommandLinePrefStore {
20 public:
21  explicit TestCommandLinePrefStore(CommandLine* cl)
22      : CommandLinePrefStore(cl) {}
23
24  bool ProxySwitchesAreValid() {
25    return ValidateProxySwitches();
26  }
27
28  void VerifyProxyMode(ProxyPrefs::ProxyMode expected_mode) {
29    const Value* value = NULL;
30    ASSERT_EQ(PrefStore::READ_OK, GetValue(prefs::kProxy, &value));
31    ASSERT_EQ(Value::TYPE_DICTIONARY, value->GetType());
32    ProxyConfigDictionary dict(static_cast<const DictionaryValue*>(value));
33    ProxyPrefs::ProxyMode actual_mode;
34    ASSERT_TRUE(dict.GetMode(&actual_mode));
35    EXPECT_EQ(expected_mode, actual_mode);
36  }
37};
38
39const char unknown_bool[] = "unknown_switch";
40const char unknown_string[] = "unknown_other_switch";
41
42}  // namespace
43
44// Tests a simple string pref on the command line.
45TEST(CommandLinePrefStoreTest, SimpleStringPref) {
46  CommandLine cl(CommandLine::NO_PROGRAM);
47  cl.AppendSwitchASCII(switches::kLang, "hi-MOM");
48  scoped_refptr<CommandLinePrefStore> store = new CommandLinePrefStore(&cl);
49
50  const Value* actual = NULL;
51  EXPECT_EQ(PrefStore::READ_OK,
52            store->GetValue(prefs::kApplicationLocale, &actual));
53  std::string result;
54  EXPECT_TRUE(actual->GetAsString(&result));
55  EXPECT_EQ("hi-MOM", result);
56}
57
58// Tests a simple boolean pref on the command line.
59TEST(CommandLinePrefStoreTest, SimpleBooleanPref) {
60  CommandLine cl(CommandLine::NO_PROGRAM);
61  cl.AppendSwitch(switches::kNoProxyServer);
62  scoped_refptr<TestCommandLinePrefStore> store =
63      new TestCommandLinePrefStore(&cl);
64
65  store->VerifyProxyMode(ProxyPrefs::MODE_DIRECT);
66}
67
68// Tests a command line with no recognized prefs.
69TEST(CommandLinePrefStoreTest, NoPrefs) {
70  CommandLine cl(CommandLine::NO_PROGRAM);
71  cl.AppendSwitch(unknown_string);
72  cl.AppendSwitchASCII(unknown_bool, "a value");
73  scoped_refptr<CommandLinePrefStore> store = new CommandLinePrefStore(&cl);
74
75  const Value* actual = NULL;
76  EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_bool, &actual));
77  EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_string, &actual));
78}
79
80// Tests a complex command line with multiple known and unknown switches.
81TEST(CommandLinePrefStoreTest, MultipleSwitches) {
82  CommandLine cl(CommandLine::NO_PROGRAM);
83  cl.AppendSwitch(unknown_string);
84  cl.AppendSwitchASCII(switches::kProxyServer, "proxy");
85  cl.AppendSwitchASCII(switches::kProxyBypassList, "list");
86  cl.AppendSwitchASCII(unknown_bool, "a value");
87  scoped_refptr<TestCommandLinePrefStore> store =
88      new TestCommandLinePrefStore(&cl);
89
90  const Value* actual = NULL;
91  EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_bool, &actual));
92  EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_string, &actual));
93
94  store->VerifyProxyMode(ProxyPrefs::MODE_FIXED_SERVERS);
95
96  const Value* value = NULL;
97  ASSERT_EQ(PrefStore::READ_OK, store->GetValue(prefs::kProxy, &value));
98  ASSERT_EQ(Value::TYPE_DICTIONARY, value->GetType());
99  ProxyConfigDictionary dict(static_cast<const DictionaryValue*>(value));
100
101  std::string string_result = "";
102
103  ASSERT_TRUE(dict.GetProxyServer(&string_result));
104  EXPECT_EQ("proxy", string_result);
105
106  ASSERT_TRUE(dict.GetBypassList(&string_result));
107  EXPECT_EQ("list", string_result);
108}
109
110// Tests proxy switch validation.
111TEST(CommandLinePrefStoreTest, ProxySwitchValidation) {
112  CommandLine cl(CommandLine::NO_PROGRAM);
113
114  // No switches.
115  scoped_refptr<TestCommandLinePrefStore> store =
116      new TestCommandLinePrefStore(&cl);
117  EXPECT_TRUE(store->ProxySwitchesAreValid());
118
119  // Only no-proxy.
120  cl.AppendSwitch(switches::kNoProxyServer);
121  scoped_refptr<TestCommandLinePrefStore> store2 =
122      new TestCommandLinePrefStore(&cl);
123  EXPECT_TRUE(store2->ProxySwitchesAreValid());
124
125  // Another proxy switch too.
126  cl.AppendSwitch(switches::kProxyAutoDetect);
127  scoped_refptr<TestCommandLinePrefStore> store3 =
128      new TestCommandLinePrefStore(&cl);
129  EXPECT_FALSE(store3->ProxySwitchesAreValid());
130
131  // All proxy switches except no-proxy.
132  CommandLine cl2(CommandLine::NO_PROGRAM);
133  cl2.AppendSwitch(switches::kProxyAutoDetect);
134  cl2.AppendSwitchASCII(switches::kProxyServer, "server");
135  cl2.AppendSwitchASCII(switches::kProxyPacUrl, "url");
136  cl2.AppendSwitchASCII(switches::kProxyBypassList, "list");
137  scoped_refptr<TestCommandLinePrefStore> store4 =
138      new TestCommandLinePrefStore(&cl2);
139  EXPECT_TRUE(store4->ProxySwitchesAreValid());
140}
141
142TEST(CommandLinePrefStoreTest, ManualProxyModeInference) {
143  CommandLine cl1(CommandLine::NO_PROGRAM);
144  cl1.AppendSwitch(unknown_string);
145  cl1.AppendSwitchASCII(switches::kProxyServer, "proxy");
146  scoped_refptr<TestCommandLinePrefStore> store1 =
147      new TestCommandLinePrefStore(&cl1);
148  store1->VerifyProxyMode(ProxyPrefs::MODE_FIXED_SERVERS);
149
150  CommandLine cl2(CommandLine::NO_PROGRAM);
151  cl2.AppendSwitchASCII(switches::kProxyPacUrl, "proxy");
152  scoped_refptr<TestCommandLinePrefStore> store2 =
153        new TestCommandLinePrefStore(&cl2);
154  store2->VerifyProxyMode(ProxyPrefs::MODE_PAC_SCRIPT);
155
156  CommandLine cl3(CommandLine::NO_PROGRAM);
157  cl3.AppendSwitchASCII(switches::kProxyServer, "");
158  scoped_refptr<TestCommandLinePrefStore> store3 =
159        new TestCommandLinePrefStore(&cl3);
160  store3->VerifyProxyMode(ProxyPrefs::MODE_DIRECT);
161}
162