1 // Copyright 2014 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 "chrome_elf/chrome_elf_util.h" 6 7 #include <tuple> 8 9 #include "base/test/test_reg_util_win.h" 10 #include "base/win/registry.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/platform_test.h" 13 14 namespace { 15 16 const wchar_t kRegPathClientState[] = L"Software\\Google\\Update\\ClientState"; 17 const wchar_t kRegPathClientStateMedium[] = 18 L"Software\\Google\\Update\\ClientStateMedium"; 19 const wchar_t kRegValueUsageStats[] = L"usagestats"; 20 const wchar_t kUninstallArgumentsField[] = L"UninstallArguments"; 21 22 const wchar_t kAppGuidCanary[] = 23 L"{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}"; 24 const wchar_t kAppGuidGoogleChrome[] = 25 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; 26 const wchar_t kAppGuidGoogleBinaries[] = 27 L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; 28 29 const wchar_t kCanaryExePath[] = 30 L"C:\\Users\\user\\AppData\\Local\\Google\\Chrome SxS\\Application" 31 L"\\chrome.exe"; 32 const wchar_t kChromeSystemExePath[] = 33 L"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"; 34 const wchar_t kChromeUserExePath[] = 35 L"C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"; 36 const wchar_t kChromiumExePath[] = 37 L"C:\\Users\\user\\AppData\\Local\\Chromium\\Application\\chrome.exe"; 38 39 40 TEST(ChromeElfUtilTest, CanaryTest) { 41 EXPECT_TRUE(IsCanary(kCanaryExePath)); 42 EXPECT_FALSE(IsCanary(kChromeUserExePath)); 43 EXPECT_FALSE(IsCanary(kChromiumExePath)); 44 } 45 46 TEST(ChromeElfUtilTest, SystemInstallTest) { 47 EXPECT_TRUE(IsSystemInstall(kChromeSystemExePath)); 48 EXPECT_FALSE(IsSystemInstall(kChromeUserExePath)); 49 } 50 51 // Parameterized test with paramters: 52 // 1: product: "canary" or "google" 53 // 2: install level: "user" or "system" 54 // 3: install mode: "single" or "multi" 55 class ChromeElfUtilTest : 56 public testing::TestWithParam<std::tuple<const char*, 57 const char*, 58 const char*> > { 59 protected: 60 virtual void SetUp() OVERRIDE { 61 override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, 62 L"chrome_elf_test_local"); 63 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, 64 L"chrome_elf_test_current"); 65 const char* app; 66 const char* level; 67 const char* mode; 68 std::tie(app, level, mode) = GetParam(); 69 is_canary_ = (std::string(app) == "canary"); 70 system_level_ = (std::string(level) != "user"); 71 multi_install_ = (std::string(mode) != "single"); 72 if (is_canary_) { 73 ASSERT_FALSE(system_level_); 74 ASSERT_FALSE(multi_install_); 75 app_guid_ = kAppGuidCanary; 76 chrome_path_ = kCanaryExePath; 77 } else { 78 app_guid_ = kAppGuidGoogleChrome; 79 chrome_path_ = (system_level_ ? kChromeSystemExePath : 80 kChromeUserExePath); 81 } 82 if (multi_install_) { 83 SetMultiInstallStateInRegistry(system_level_, true); 84 app_guid_ = kAppGuidGoogleBinaries; 85 } 86 } 87 88 base::string16 BuildKey(const wchar_t* path, const wchar_t* guid) { 89 base::string16 full_key_path(path); 90 full_key_path.append(1, L'\\'); 91 full_key_path.append(guid); 92 return full_key_path; 93 } 94 95 void SetUsageStat(DWORD value, bool state_medium) { 96 LONG result = base::win::RegKey( 97 system_level_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, 98 BuildKey(state_medium ? kRegPathClientStateMedium : kRegPathClientState, 99 app_guid_).c_str(), 100 KEY_SET_VALUE).WriteValue(kRegValueUsageStats, value); 101 ASSERT_EQ(ERROR_SUCCESS, result); 102 } 103 104 void SetMultiInstallStateInRegistry(bool system_install, bool multi) { 105 base::win::RegKey key( 106 system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, 107 BuildKey(kRegPathClientState, kAppGuidGoogleChrome).c_str(), 108 KEY_SET_VALUE); 109 LONG result; 110 if (multi) { 111 result = key.WriteValue(kUninstallArgumentsField, 112 L"yadda yadda --multi-install yadda yadda"); 113 } else { 114 result = key.DeleteValue(kUninstallArgumentsField); 115 } 116 ASSERT_EQ(ERROR_SUCCESS, result); 117 } 118 119 const wchar_t* app_guid_; 120 const wchar_t* chrome_path_; 121 bool system_level_; 122 bool multi_install_; 123 bool is_canary_; 124 registry_util::RegistryOverrideManager override_manager_; 125 }; 126 127 TEST_P(ChromeElfUtilTest, MultiInstallTest) { 128 if (is_canary_) 129 return; 130 SetMultiInstallStateInRegistry(system_level_, true); 131 EXPECT_TRUE(IsMultiInstall(system_level_)); 132 133 SetMultiInstallStateInRegistry(system_level_, false); 134 EXPECT_FALSE(IsMultiInstall(system_level_)); 135 } 136 137 TEST_P(ChromeElfUtilTest, UsageStatsAbsent) { 138 EXPECT_FALSE(AreUsageStatsEnabled(chrome_path_)); 139 } 140 141 TEST_P(ChromeElfUtilTest, UsageStatsZero) { 142 SetUsageStat(0, false); 143 EXPECT_FALSE(AreUsageStatsEnabled(chrome_path_)); 144 } 145 146 TEST_P(ChromeElfUtilTest, UsageStatsOne) { 147 SetUsageStat(1, false); 148 EXPECT_TRUE(AreUsageStatsEnabled(chrome_path_)); 149 if (is_canary_) { 150 EXPECT_FALSE(AreUsageStatsEnabled(kChromeUserExePath)); 151 EXPECT_FALSE(AreUsageStatsEnabled(kChromeSystemExePath)); 152 } else if (system_level_) { 153 EXPECT_FALSE(AreUsageStatsEnabled(kCanaryExePath)); 154 EXPECT_FALSE(AreUsageStatsEnabled(kChromeUserExePath)); 155 } else { 156 EXPECT_FALSE(AreUsageStatsEnabled(kCanaryExePath)); 157 EXPECT_FALSE(AreUsageStatsEnabled(kChromeSystemExePath)); 158 } 159 } 160 161 TEST_P(ChromeElfUtilTest, UsageStatsZeroInStateMedium) { 162 if (!system_level_) 163 return; 164 SetUsageStat(0, true); 165 EXPECT_FALSE(AreUsageStatsEnabled(chrome_path_)); 166 } 167 168 TEST_P(ChromeElfUtilTest, UsageStatsOneInStateMedium) { 169 if (!system_level_) 170 return; 171 SetUsageStat(1, true); 172 EXPECT_TRUE(AreUsageStatsEnabled(chrome_path_)); 173 EXPECT_FALSE(AreUsageStatsEnabled(kCanaryExePath)); 174 EXPECT_FALSE(AreUsageStatsEnabled(kChromeUserExePath)); 175 } 176 177 INSTANTIATE_TEST_CASE_P(Canary, ChromeElfUtilTest, 178 testing::Combine(testing::Values("canary"), 179 testing::Values("user"), 180 testing::Values("single"))); 181 INSTANTIATE_TEST_CASE_P(GoogleChrome, ChromeElfUtilTest, 182 testing::Combine(testing::Values("google"), 183 testing::Values("user", "system"), 184 testing::Values("single", "multi"))); 185 186 } // namespace 187