Chromium Code Reviews| 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 <algorithm> | |
| 6 #include <map> | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/file_path.h" | |
| 12 #include "base/file_util.h" | |
| 13 #include "base/json/json_reader.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/stl_util.h" | |
| 16 #include "base/values.h" | |
| 17 #include "chrome/browser/browser_process.h" | |
| 18 #include "chrome/browser/policy/browser_policy_connector.h" | |
| 19 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | |
| 20 #include "chrome/browser/policy/policy_map.h" | |
| 21 #include "chrome/browser/prefs/pref_service.h" | |
| 22 #include "chrome/browser/profiles/profile.h" | |
| 23 #include "chrome/browser/ui/browser.h" | |
| 24 #include "chrome/test/base/in_process_browser_test.h" | |
| 25 #include "chrome/test/base/ui_test_utils.h" | |
| 26 #include "googleurl/src/gurl.h" | |
| 27 #include "policy/policy_constants.h" | |
| 28 #include "testing/gmock/include/gmock/gmock.h" | |
| 29 #include "testing/gtest/include/gtest/gtest.h" | |
| 30 | |
| 31 #if defined(OS_MACOSX) | |
| 32 #include "base/base_paths.h" | |
| 33 #include "base/mac/foundation_util.h" | |
| 34 #include "base/path_service.h" | |
| 35 #include "chrome/common/chrome_constants.h" | |
| 36 #endif | |
| 37 | |
| 38 using testing::Return; | |
| 39 | |
| 40 namespace policy { | |
| 41 | |
| 42 namespace { | |
| 43 | |
| 44 // Contains the testing details for a single policy, loaded from | |
| 45 // chrome/test/data/policy/policy_test_cases.json. | |
| 46 class PolicyTestCase { | |
| 47 public: | |
| 48 explicit PolicyTestCase(const std::string& name) | |
| 49 : name_(name), | |
| 50 is_local_state_(false), | |
| 51 official_only_(false) {} | |
| 52 ~PolicyTestCase() {} | |
| 53 | |
| 54 const std::string& name() const { return name_; } | |
| 55 | |
| 56 void set_pref(const std::string& pref) { pref_ = pref; } | |
| 57 const std::string& pref() const { return pref_; } | |
| 58 const char* pref_name() const { return pref_.c_str(); } | |
| 59 | |
| 60 const PolicyMap& test_policy() const { return test_policy_; } | |
| 61 void set_test_policy(const PolicyMap& policy) { | |
| 62 test_policy_.CopyFrom(policy); | |
| 63 } | |
| 64 | |
| 65 const std::vector<GURL>& settings_pages() const { return settings_pages_; } | |
| 66 void add_settings_page(const GURL& url) { settings_pages_.push_back(url); } | |
|
jam
2012/09/11 23:18:41
nit: AddSettingsPage, unix_hacker style is only fo
Joao da Silva
2012/09/12 12:13:57
Done.
| |
| 67 | |
| 68 bool is_os_supported() const { | |
|
jam
2012/09/11 23:18:41
ditto, IsOSSupported
Joao da Silva
2012/09/12 12:13:57
Done.
| |
| 69 #if defined(OS_WIN) | |
| 70 const std::string os("win"); | |
| 71 #elif defined(OS_MACOSX) | |
| 72 const std::string os("mac"); | |
| 73 #elif defined(OS_CHROMEOS) | |
| 74 const std::string os("chromeos"); | |
| 75 #elif defined(OS_LINUX) | |
| 76 const std::string os("linux"); | |
| 77 #else | |
| 78 #error "Unknown platform" | |
| 79 #endif | |
| 80 return std::find(supported_os_.begin(), supported_os_.end(), os) != | |
| 81 supported_os_.end(); | |
| 82 } | |
| 83 | |
| 84 void add_supported_os(const std::string& os) { supported_os_.push_back(os); } | |
|
jam
2012/09/11 23:18:41
AddSupportedOS
Joao da Silva
2012/09/12 12:13:57
Done.
| |
| 85 | |
| 86 bool is_local_state() const { return is_local_state_; } | |
| 87 void set_local_state(bool flag) { is_local_state_ = flag; } | |
| 88 | |
| 89 bool is_official_only() const { return official_only_; } | |
| 90 void set_official_only(bool flag) { official_only_ = flag; } | |
| 91 | |
| 92 bool is_supported() const { | |
|
jam
2012/09/11 23:18:41
IsSupported
Joao da Silva
2012/09/12 12:13:57
Done.
| |
| 93 #if !defined(OFFICIAL_BUILD) | |
| 94 if (is_official_only()) | |
| 95 return false; | |
| 96 #endif | |
| 97 return is_os_supported(); | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 std::string name_; | |
| 102 std::string pref_; | |
| 103 PolicyMap test_policy_; | |
| 104 std::vector<GURL> settings_pages_; | |
| 105 std::vector<std::string> supported_os_; | |
| 106 bool is_local_state_; | |
| 107 bool official_only_; | |
| 108 | |
| 109 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); | |
| 110 }; | |
| 111 | |
| 112 } // namespace | |
| 113 | |
| 114 class PolicyPrefsTest : public InProcessBrowserTest { | |
| 115 protected: | |
| 116 PolicyPrefsTest() {} | |
| 117 virtual ~PolicyPrefsTest() {} | |
| 118 | |
| 119 // Loads policy_test_cases.json and builds a map of test cases. | |
| 120 static void SetUpTestCase() { | |
| 121 #if defined(OS_MACOSX) | |
| 122 // Ugly hack to work around http://crbug.com/63183, since this uses the | |
| 123 // PathService from GetTestFilePath() above before BrowserTestBase() and | |
| 124 // InProcessBrowserTest() are invoked. Those ctors include similar hacks. | |
| 125 base::mac::SetOverrideAmIBundled(true); | |
| 126 FilePath chrome_path; | |
| 127 CHECK(PathService::Get(base::FILE_EXE, &chrome_path)); | |
| 128 FilePath fixed_chrome_path = | |
| 129 chrome_path.DirName().Append(chrome::kBrowserProcessExecutablePath); | |
| 130 CHECK(PathService::Override(base::FILE_EXE, fixed_chrome_path)); | |
|
Joao da Silva
2012/09/11 22:17:39
I'd like to avoid this hack if you have a better i
jam
2012/09/11 23:18:41
I don't know enough about this area, I would check
| |
| 131 #endif | |
| 132 | |
| 133 FilePath path = ui_test_utils::GetTestFilePath( | |
| 134 FilePath(FILE_PATH_LITERAL("policy")), | |
| 135 FilePath(FILE_PATH_LITERAL("policy_test_cases.json"))); | |
| 136 std::string json; | |
| 137 ASSERT_TRUE(file_util::ReadFileToString(path, &json)); | |
| 138 int error_code = -1; | |
| 139 std::string error_string; | |
| 140 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | |
| 141 json, base::JSON_ALLOW_TRAILING_COMMAS, &error_code, &error_string)); | |
| 142 ASSERT_TRUE(value.get()) | |
| 143 << "Error parsing policy_test_cases.json: " << error_string; | |
| 144 base::DictionaryValue* dict = NULL; | |
| 145 ASSERT_TRUE(value->GetAsDictionary(&dict)); | |
| 146 policy_test_cases_ = new std::map<std::string, PolicyTestCase*>(); | |
| 147 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); | |
| 148 for (const PolicyDefinitionList::Entry* policy = list->begin; | |
| 149 policy != list->end; ++policy) { | |
| 150 PolicyTestCase* test_case = GetTestCase(dict, policy->name); | |
| 151 if (test_case) | |
| 152 (*policy_test_cases_)[policy->name] = test_case; | |
| 153 } | |
| 154 | |
| 155 #if defined(OS_MACOSX) | |
| 156 // Restore |chrome_path| so that the fix in InProcessBrowserTest() works. | |
|
jam
2012/09/11 23:18:41
you mean SetUpTestCase?
Joao da Silva
2012/09/12 12:13:57
InProcessBrowserTest() has code similar to the fix
| |
| 157 CHECK(PathService::Override(base::FILE_EXE, chrome_path)); | |
| 158 #endif | |
| 159 } | |
| 160 | |
| 161 static void TearDownTestCase() { | |
| 162 STLDeleteValues(policy_test_cases_); | |
| 163 delete policy_test_cases_; | |
| 164 } | |
| 165 | |
| 166 static PolicyTestCase* GetTestCase(const base::DictionaryValue* tests, | |
| 167 const std::string& name) { | |
| 168 const base::DictionaryValue* dict = NULL; | |
| 169 if (!tests->GetDictionary(name, &dict)) | |
| 170 return NULL; | |
| 171 PolicyTestCase* test_case = new PolicyTestCase(name); | |
| 172 std::string pref; | |
| 173 if (dict->GetString("pref", &pref)) | |
| 174 test_case->set_pref(pref); | |
| 175 const base::DictionaryValue* policy_dict = NULL; | |
| 176 if (dict->GetDictionary("test_policy", &policy_dict)) { | |
| 177 PolicyMap policies; | |
| 178 policies.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | |
| 179 test_case->set_test_policy(policies); | |
| 180 } | |
| 181 const base::ListValue* settings_pages = NULL; | |
| 182 if (dict->GetList("settings_pages", &settings_pages)) { | |
| 183 for (size_t i = 0; i < settings_pages->GetSize(); ++i) { | |
| 184 std::string page; | |
| 185 if (settings_pages->GetString(i, &page)) | |
| 186 test_case->add_settings_page(GURL(page)); | |
| 187 } | |
| 188 } | |
| 189 const base::ListValue* supported_os = NULL; | |
| 190 if (dict->GetList("os", &supported_os)) { | |
| 191 for (size_t i = 0; i < supported_os->GetSize(); ++i) { | |
| 192 std::string os; | |
| 193 if (supported_os->GetString(i, &os)) | |
| 194 test_case->add_supported_os(os); | |
| 195 } | |
| 196 } | |
| 197 bool flag = false; | |
| 198 if (dict->GetBoolean("local_state", &flag)) | |
| 199 test_case->set_local_state(flag); | |
| 200 if (dict->GetBoolean("official_only", &flag)) | |
| 201 test_case->set_official_only(flag); | |
| 202 return test_case; | |
| 203 } | |
| 204 | |
| 205 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
| 206 EXPECT_CALL(provider_, IsInitializationComplete()) | |
| 207 .WillRepeatedly(Return(true)); | |
| 208 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | |
| 209 } | |
| 210 | |
| 211 static std::map<std::string, PolicyTestCase*>* policy_test_cases_; | |
| 212 MockConfigurationPolicyProvider provider_; | |
| 213 }; | |
| 214 | |
| 215 std::map<std::string, PolicyTestCase*>* PolicyPrefsTest::policy_test_cases_ = 0; | |
| 216 | |
| 217 IN_PROC_BROWSER_TEST_F(PolicyPrefsTest, AllPoliciesHaveATestCase) { | |
| 218 // Verifies that all known policies have a test case in the JSON file. | |
| 219 // This test fails when a policy is added to | |
| 220 // chrome/app/policy/policy_templates.json but a test case is not added to | |
| 221 // chrome/test/data/policy/policy_test_cases.json. | |
| 222 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); | |
| 223 for (const PolicyDefinitionList::Entry* policy = list->begin; | |
| 224 policy != list->end; ++policy) { | |
| 225 EXPECT_TRUE(ContainsKey(*policy_test_cases_, policy->name)) | |
| 226 << "Missing policy test case for: " << policy->name; | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 IN_PROC_BROWSER_TEST_F(PolicyPrefsTest, PolicyToPrefsMapping) { | |
| 231 // Verifies that policies make their corresponding preferences become managed, | |
| 232 // and that the user can't override that setting. | |
| 233 const PolicyMap kNoPolicies; | |
| 234 PrefService* profile_prefs = browser()->profile()->GetPrefs(); | |
| 235 PrefService* local_state = g_browser_process->local_state(); | |
| 236 std::map<std::string, PolicyTestCase*>::iterator it; | |
| 237 for (it = policy_test_cases_->begin(); | |
| 238 it != policy_test_cases_->end(); ++it) { | |
| 239 PolicyTestCase* test_case = it->second; | |
| 240 if (!test_case->is_supported() || test_case->pref().empty()) | |
| 241 continue; | |
| 242 LOG(INFO) << "Testing policy: " << test_case->name(); | |
| 243 // Clear policies. | |
| 244 provider_.UpdateChromePolicy(kNoPolicies); | |
| 245 | |
| 246 PrefService* prefs = | |
| 247 test_case->is_local_state() ? local_state : profile_prefs; | |
| 248 // The preference must have been registered. | |
| 249 const PrefService::Preference* pref = | |
| 250 prefs->FindPreference(test_case->pref_name()); | |
| 251 ASSERT_TRUE(pref); | |
| 252 prefs->ClearPref(test_case->pref_name()); | |
| 253 | |
| 254 // Verify that setting the policy overrides the pref. | |
| 255 EXPECT_TRUE(pref->IsDefaultValue()); | |
| 256 EXPECT_TRUE(pref->IsUserModifiable()); | |
| 257 EXPECT_FALSE(pref->IsUserControlled()); | |
| 258 EXPECT_FALSE(pref->IsManaged()); | |
| 259 | |
| 260 provider_.UpdateChromePolicy(test_case->test_policy()); | |
| 261 EXPECT_FALSE(pref->IsDefaultValue()); | |
| 262 EXPECT_FALSE(pref->IsUserModifiable()); | |
| 263 EXPECT_FALSE(pref->IsUserControlled()); | |
| 264 EXPECT_TRUE(pref->IsManaged()); | |
| 265 } | |
| 266 } | |
| 267 | |
| 268 } // namespace policy | |
| OLD | NEW |