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