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 // Unit tests for master preferences related methods. 6 7 #include "base/file_util.h" 8 #include "base/memory/scoped_ptr.h" 9 #include "base/path_service.h" 10 #include "base/strings/stringprintf.h" 11 #include "base/values.h" 12 #include "chrome/common/chrome_paths.h" 13 #include "chrome/installer/util/master_preferences.h" 14 #include "chrome/installer/util/master_preferences_constants.h" 15 #include "chrome/installer/util/util_constants.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 18 namespace { 19 class MasterPreferencesTest : public testing::Test { 20 protected: 21 virtual void SetUp() { 22 ASSERT_TRUE(file_util::CreateTemporaryFile(&prefs_file_)); 23 } 24 25 virtual void TearDown() { 26 EXPECT_TRUE(base::DeleteFile(prefs_file_, false)); 27 } 28 29 const base::FilePath& prefs_file() const { return prefs_file_; } 30 31 private: 32 base::FilePath prefs_file_; 33 }; 34 35 // Used to specify an expected value for a set boolean preference variable. 36 struct ExpectedBooleans { 37 const char* name; 38 bool expected_value; 39 }; 40 41 } // namespace 42 43 TEST_F(MasterPreferencesTest, NoFileToParse) { 44 EXPECT_TRUE(base::DeleteFile(prefs_file(), false)); 45 installer::MasterPreferences prefs(prefs_file()); 46 EXPECT_FALSE(prefs.read_from_file()); 47 } 48 49 TEST_F(MasterPreferencesTest, ParseDistroParams) { 50 const char text[] = 51 "{ \n" 52 " \"distribution\": { \n" 53 " \"show_welcome_page\": true,\n" 54 " \"import_search_engine\": true,\n" 55 " \"import_history\": true,\n" 56 " \"import_bookmarks\": true,\n" 57 " \"import_bookmarks_from_file\": \"c:\\\\foo\",\n" 58 " \"import_home_page\": true,\n" 59 " \"do_not_create_any_shortcuts\": true,\n" 60 " \"do_not_create_desktop_shortcut\": true,\n" 61 " \"do_not_create_quick_launch_shortcut\": true,\n" 62 " \"do_not_create_taskbar_shortcut\": true,\n" 63 " \"do_not_launch_chrome\": true,\n" 64 " \"make_chrome_default\": true,\n" 65 " \"make_chrome_default_for_user\": true,\n" 66 " \"system_level\": true,\n" 67 " \"verbose_logging\": true,\n" 68 " \"require_eula\": true,\n" 69 " \"alternate_shortcut_text\": true,\n" 70 " \"chrome_shortcut_icon_index\": 1,\n" 71 " \"ping_delay\": 40\n" 72 " },\n" 73 " \"blah\": {\n" 74 " \"import_history\": false\n" 75 " }\n" 76 "} \n"; 77 78 EXPECT_TRUE(file_util::WriteFile(prefs_file(), text, strlen(text))); 79 installer::MasterPreferences prefs(prefs_file()); 80 EXPECT_TRUE(prefs.read_from_file()); 81 82 const char* expected_true[] = { 83 installer::master_preferences::kDistroImportSearchPref, 84 installer::master_preferences::kDistroImportHistoryPref, 85 installer::master_preferences::kDistroImportBookmarksPref, 86 installer::master_preferences::kDistroImportHomePagePref, 87 installer::master_preferences::kDoNotCreateAnyShortcuts, 88 installer::master_preferences::kDoNotCreateDesktopShortcut, 89 installer::master_preferences::kDoNotCreateQuickLaunchShortcut, 90 installer::master_preferences::kDoNotCreateTaskbarShortcut, 91 installer::master_preferences::kDoNotLaunchChrome, 92 installer::master_preferences::kMakeChromeDefault, 93 installer::master_preferences::kMakeChromeDefaultForUser, 94 installer::master_preferences::kSystemLevel, 95 installer::master_preferences::kVerboseLogging, 96 installer::master_preferences::kRequireEula, 97 installer::master_preferences::kAltShortcutText, 98 }; 99 100 for (int i = 0; i < arraysize(expected_true); ++i) { 101 bool value = false; 102 EXPECT_TRUE(prefs.GetBool(expected_true[i], &value)); 103 EXPECT_TRUE(value) << expected_true[i]; 104 } 105 106 std::string str_value; 107 EXPECT_TRUE(prefs.GetString( 108 installer::master_preferences::kDistroImportBookmarksFromFilePref, 109 &str_value)); 110 EXPECT_STREQ("c:\\foo", str_value.c_str()); 111 112 int icon_index = 0; 113 EXPECT_TRUE(prefs.GetInt( 114 installer::master_preferences::kChromeShortcutIconIndex, 115 &icon_index)); 116 EXPECT_EQ(icon_index, 1); 117 int ping_delay = 90; 118 EXPECT_TRUE(prefs.GetInt(installer::master_preferences::kDistroPingDelay, 119 &ping_delay)); 120 EXPECT_EQ(ping_delay, 40); 121 } 122 123 TEST_F(MasterPreferencesTest, ParseMissingDistroParams) { 124 const char text[] = 125 "{ \n" 126 " \"distribution\": { \n" 127 " \"import_search_engine\": true,\n" 128 " \"import_bookmarks\": false,\n" 129 " \"import_bookmarks_from_file\": \"\",\n" 130 " \"do_not_create_desktop_shortcut\": true,\n" 131 " \"do_not_create_quick_launch_shortcut\": true,\n" 132 " \"do_not_launch_chrome\": true,\n" 133 " \"chrome_shortcut_icon_index\": \"bac\"\n" 134 " }\n" 135 "} \n"; 136 137 EXPECT_TRUE(file_util::WriteFile(prefs_file(), text, strlen(text))); 138 installer::MasterPreferences prefs(prefs_file()); 139 EXPECT_TRUE(prefs.read_from_file()); 140 141 ExpectedBooleans expected_bool[] = { 142 { installer::master_preferences::kDistroImportSearchPref, true }, 143 { installer::master_preferences::kDistroImportBookmarksPref, false }, 144 { installer::master_preferences::kDoNotCreateDesktopShortcut, true }, 145 { installer::master_preferences::kDoNotCreateQuickLaunchShortcut, true }, 146 { installer::master_preferences::kDoNotLaunchChrome, true }, 147 }; 148 149 bool value = false; 150 for (int i = 0; i < arraysize(expected_bool); ++i) { 151 EXPECT_TRUE(prefs.GetBool(expected_bool[i].name, &value)); 152 EXPECT_EQ(value, expected_bool[i].expected_value) << expected_bool[i].name; 153 } 154 155 const char* missing_bools[] = { 156 installer::master_preferences::kDistroImportHomePagePref, 157 installer::master_preferences::kDoNotRegisterForUpdateLaunch, 158 installer::master_preferences::kMakeChromeDefault, 159 installer::master_preferences::kMakeChromeDefaultForUser, 160 }; 161 162 for (int i = 0; i < arraysize(missing_bools); ++i) { 163 EXPECT_FALSE(prefs.GetBool(missing_bools[i], &value)) << missing_bools[i]; 164 } 165 166 std::string str_value; 167 EXPECT_FALSE(prefs.GetString( 168 installer::master_preferences::kDistroImportBookmarksFromFilePref, 169 &str_value)); 170 171 int icon_index = 0; 172 EXPECT_FALSE(prefs.GetInt( 173 installer::master_preferences::kChromeShortcutIconIndex, 174 &icon_index)); 175 EXPECT_EQ(icon_index, 0); 176 177 int ping_delay = 90; 178 EXPECT_FALSE(prefs.GetInt( 179 installer::master_preferences::kDistroPingDelay, &ping_delay)); 180 EXPECT_EQ(ping_delay, 90); 181 } 182 183 TEST_F(MasterPreferencesTest, FirstRunTabs) { 184 const char text[] = 185 "{ \n" 186 " \"distribution\": { \n" 187 " \"something here\": true\n" 188 " },\n" 189 " \"first_run_tabs\": [\n" 190 " \"http://google.com/f1\",\n" 191 " \"https://google.com/f2\",\n" 192 " \"new_tab_page\"\n" 193 " ]\n" 194 "} \n"; 195 196 EXPECT_TRUE(file_util::WriteFile(prefs_file(), text, strlen(text))); 197 installer::MasterPreferences prefs(prefs_file()); 198 typedef std::vector<std::string> TabsVector; 199 TabsVector tabs = prefs.GetFirstRunTabs(); 200 ASSERT_EQ(3, tabs.size()); 201 EXPECT_EQ("http://google.com/f1", tabs[0]); 202 EXPECT_EQ("https://google.com/f2", tabs[1]); 203 EXPECT_EQ("new_tab_page", tabs[2]); 204 } 205 206 // In this test instead of using our synthetic json file, we use an 207 // actual test case from the extensions unittest. The hope here is that if 208 // they change something in the manifest this test will break, but in 209 // general it is expected the extension format to be backwards compatible. 210 TEST(MasterPrefsExtension, ValidateExtensionJSON) { 211 base::FilePath prefs_path; 212 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &prefs_path)); 213 prefs_path = prefs_path.AppendASCII("extensions") 214 .AppendASCII("good").AppendASCII("Preferences"); 215 216 installer::MasterPreferences prefs(prefs_path); 217 DictionaryValue* extensions = NULL; 218 EXPECT_TRUE(prefs.GetExtensionsBlock(&extensions)); 219 int location = 0; 220 EXPECT_TRUE(extensions->GetInteger( 221 "behllobkkfkfnphdnhnkndlbkcpglgmj.location", &location)); 222 int state = 0; 223 EXPECT_TRUE(extensions->GetInteger( 224 "behllobkkfkfnphdnhnkndlbkcpglgmj.state", &state)); 225 std::string path; 226 EXPECT_TRUE(extensions->GetString( 227 "behllobkkfkfnphdnhnkndlbkcpglgmj.path", &path)); 228 std::string key; 229 EXPECT_TRUE(extensions->GetString( 230 "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.key", &key)); 231 std::string name; 232 EXPECT_TRUE(extensions->GetString( 233 "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.name", &name)); 234 std::string version; 235 EXPECT_TRUE(extensions->GetString( 236 "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.version", &version)); 237 } 238 239 // Test that we are parsing master preferences correctly. 240 TEST_F(MasterPreferencesTest, GetInstallPreferencesTest) { 241 // Create a temporary prefs file. 242 base::FilePath prefs_file; 243 ASSERT_TRUE(file_util::CreateTemporaryFile(&prefs_file)); 244 const char text[] = 245 "{ \n" 246 " \"distribution\": { \n" 247 " \"do_not_create_desktop_shortcut\": false,\n" 248 " \"do_not_create_quick_launch_shortcut\": false,\n" 249 " \"do_not_launch_chrome\": true,\n" 250 " \"system_level\": true,\n" 251 " \"verbose_logging\": false\n" 252 " }\n" 253 "} \n"; 254 EXPECT_TRUE(file_util::WriteFile(prefs_file, text, strlen(text))); 255 256 // Make sure command line values override the values in master preferences. 257 std::wstring cmd_str( 258 L"setup.exe --installerdata=\"" + prefs_file.value() + L"\""); 259 cmd_str.append(L" --do-not-launch-chrome"); 260 CommandLine cmd_line = CommandLine::FromString(cmd_str); 261 installer::MasterPreferences prefs(cmd_line); 262 263 // Check prefs that do not have any equivalent command line option. 264 ExpectedBooleans expected_bool[] = { 265 { installer::master_preferences::kDoNotLaunchChrome, true }, 266 { installer::master_preferences::kSystemLevel, true }, 267 { installer::master_preferences::kVerboseLogging, false }, 268 }; 269 270 // Now check that prefs got merged correctly. 271 bool value = false; 272 for (int i = 0; i < arraysize(expected_bool); ++i) { 273 EXPECT_TRUE(prefs.GetBool(expected_bool[i].name, &value)); 274 EXPECT_EQ(value, expected_bool[i].expected_value) << expected_bool[i].name; 275 } 276 277 // Delete temporary prefs file. 278 EXPECT_TRUE(base::DeleteFile(prefs_file, false)); 279 280 // Check that if master prefs doesn't exist, we can still parse the common 281 // prefs. 282 cmd_str = L"setup.exe --do-not-launch-chrome"; 283 cmd_line.ParseFromString(cmd_str); 284 installer::MasterPreferences prefs2(cmd_line); 285 ExpectedBooleans expected_bool2[] = { 286 { installer::master_preferences::kDoNotLaunchChrome, true }, 287 }; 288 289 for (int i = 0; i < arraysize(expected_bool2); ++i) { 290 EXPECT_TRUE(prefs2.GetBool(expected_bool2[i].name, &value)); 291 EXPECT_EQ(value, expected_bool2[i].expected_value) 292 << expected_bool2[i].name; 293 } 294 295 EXPECT_FALSE(prefs2.GetBool( 296 installer::master_preferences::kSystemLevel, &value)); 297 EXPECT_FALSE(prefs2.GetBool( 298 installer::master_preferences::kVerboseLogging, &value)); 299 } 300 301 TEST_F(MasterPreferencesTest, TestDefaultInstallConfig) { 302 std::wstringstream chrome_cmd, cf_cmd; 303 chrome_cmd << "setup.exe"; 304 cf_cmd << "setup.exe --" << installer::switches::kChromeFrame; 305 306 CommandLine chrome_install(CommandLine::FromString(chrome_cmd.str())); 307 CommandLine cf_install(CommandLine::FromString(cf_cmd.str())); 308 309 installer::MasterPreferences pref_chrome(chrome_install); 310 installer::MasterPreferences pref_cf(cf_install); 311 312 EXPECT_FALSE(pref_chrome.is_multi_install()); 313 EXPECT_TRUE(pref_chrome.install_chrome()); 314 EXPECT_FALSE(pref_chrome.install_chrome_frame()); 315 316 EXPECT_FALSE(pref_cf.is_multi_install()); 317 EXPECT_FALSE(pref_cf.install_chrome()); 318 EXPECT_TRUE(pref_cf.install_chrome_frame()); 319 } 320 321 TEST_F(MasterPreferencesTest, TestMultiInstallConfig) { 322 using installer::switches::kMultiInstall; 323 using installer::switches::kChrome; 324 using installer::switches::kChromeFrame; 325 326 std::wstringstream chrome_cmd, cf_cmd, chrome_cf_cmd; 327 chrome_cmd << "setup.exe --" << kMultiInstall << " --" << kChrome; 328 cf_cmd << "setup.exe --" << kMultiInstall << " --" << kChromeFrame; 329 chrome_cf_cmd << "setup.exe --" << kMultiInstall << " --" << kChrome << 330 " --" << kChromeFrame; 331 332 CommandLine chrome_install(CommandLine::FromString(chrome_cmd.str())); 333 CommandLine cf_install(CommandLine::FromString(cf_cmd.str())); 334 CommandLine chrome_cf_install(CommandLine::FromString(chrome_cf_cmd.str())); 335 336 installer::MasterPreferences pref_chrome(chrome_install); 337 installer::MasterPreferences pref_cf(cf_install); 338 installer::MasterPreferences pref_chrome_cf(chrome_cf_install); 339 340 EXPECT_TRUE(pref_chrome.is_multi_install()); 341 EXPECT_TRUE(pref_chrome.install_chrome()); 342 EXPECT_FALSE(pref_chrome.install_chrome_frame()); 343 344 EXPECT_TRUE(pref_cf.is_multi_install()); 345 EXPECT_FALSE(pref_cf.install_chrome()); 346 EXPECT_TRUE(pref_cf.install_chrome_frame()); 347 348 EXPECT_TRUE(pref_chrome_cf.is_multi_install()); 349 EXPECT_TRUE(pref_chrome_cf.install_chrome()); 350 EXPECT_TRUE(pref_chrome_cf.install_chrome_frame()); 351 } 352 353 TEST_F(MasterPreferencesTest, EnforceLegacyCreateAllShortcutsFalse) { 354 static const char kCreateAllShortcutsFalsePrefs[] = 355 "{" 356 " \"distribution\": {" 357 " \"create_all_shortcuts\": false" 358 " }" 359 "}"; 360 361 installer::MasterPreferences prefs(kCreateAllShortcutsFalsePrefs); 362 363 bool do_not_create_desktop_shortcut = false; 364 bool do_not_create_quick_launch_shortcut = false; 365 bool do_not_create_taskbar_shortcut = false; 366 prefs.GetBool( 367 installer::master_preferences::kDoNotCreateDesktopShortcut, 368 &do_not_create_desktop_shortcut); 369 prefs.GetBool( 370 installer::master_preferences::kDoNotCreateQuickLaunchShortcut, 371 &do_not_create_quick_launch_shortcut); 372 prefs.GetBool( 373 installer::master_preferences::kDoNotCreateTaskbarShortcut, 374 &do_not_create_taskbar_shortcut); 375 // create_all_shortcuts is a legacy preference that should only enforce 376 // do_not_create_desktop_shortcut and do_not_create_quick_launch_shortcut 377 // when set to false. 378 EXPECT_TRUE(do_not_create_desktop_shortcut); 379 EXPECT_TRUE(do_not_create_quick_launch_shortcut); 380 EXPECT_FALSE(do_not_create_taskbar_shortcut); 381 } 382 383 TEST_F(MasterPreferencesTest, DontEnforceLegacyCreateAllShortcutsTrue) { 384 static const char kCreateAllShortcutsFalsePrefs[] = 385 "{" 386 " \"distribution\": {" 387 " \"create_all_shortcuts\": true" 388 " }" 389 "}"; 390 391 installer::MasterPreferences prefs(kCreateAllShortcutsFalsePrefs); 392 393 bool do_not_create_desktop_shortcut = false; 394 bool do_not_create_quick_launch_shortcut = false; 395 bool do_not_create_taskbar_shortcut = false; 396 prefs.GetBool( 397 installer::master_preferences::kDoNotCreateDesktopShortcut, 398 &do_not_create_desktop_shortcut); 399 prefs.GetBool( 400 installer::master_preferences::kDoNotCreateQuickLaunchShortcut, 401 &do_not_create_quick_launch_shortcut); 402 prefs.GetBool( 403 installer::master_preferences::kDoNotCreateTaskbarShortcut, 404 &do_not_create_taskbar_shortcut); 405 EXPECT_FALSE(do_not_create_desktop_shortcut); 406 EXPECT_FALSE(do_not_create_quick_launch_shortcut); 407 EXPECT_FALSE(do_not_create_taskbar_shortcut); 408 } 409 410 TEST_F(MasterPreferencesTest, DontEnforceLegacyCreateAllShortcutsNotSpecified) { 411 static const char kCreateAllShortcutsFalsePrefs[] = 412 "{" 413 " \"distribution\": {" 414 " \"some_other_pref\": true" 415 " }" 416 "}"; 417 418 installer::MasterPreferences prefs(kCreateAllShortcutsFalsePrefs); 419 420 bool do_not_create_desktop_shortcut = false; 421 bool do_not_create_quick_launch_shortcut = false; 422 bool do_not_create_taskbar_shortcut = false; 423 prefs.GetBool( 424 installer::master_preferences::kDoNotCreateDesktopShortcut, 425 &do_not_create_desktop_shortcut); 426 prefs.GetBool( 427 installer::master_preferences::kDoNotCreateQuickLaunchShortcut, 428 &do_not_create_quick_launch_shortcut); 429 prefs.GetBool( 430 installer::master_preferences::kDoNotCreateTaskbarShortcut, 431 &do_not_create_taskbar_shortcut); 432 EXPECT_FALSE(do_not_create_desktop_shortcut); 433 EXPECT_FALSE(do_not_create_quick_launch_shortcut); 434 EXPECT_FALSE(do_not_create_taskbar_shortcut); 435 } 436