OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 7 #include "base/test/test_reg_util_win.h" |
| 8 #include "chrome/install_static/install_details.h" |
| 9 #include "chrome/install_static/user_data_dir.h" |
| 10 #include "chrome_elf/nt_registry/nt_registry.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace install_static { |
| 14 namespace { |
| 15 |
| 16 inline bool EndsWith(const std::wstring& value, const std::wstring& ending) { |
| 17 if (ending.size() > value.size()) |
| 18 return false; |
| 19 return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); |
| 20 } |
| 21 |
| 22 #if defined(GOOGLE_CHROME_BUILD) |
| 23 const wchar_t kPolicyRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; |
| 24 #else |
| 25 const wchar_t kPolicyRegistryKey[] = L"SOFTWARE\\Policies\\Chromium"; |
| 26 #endif |
| 27 |
| 28 const wchar_t kUserDataDirRegistryKey[] = L"UserDataDir"; |
| 29 |
| 30 const InstallConstants kFakeInstallConstants = {sizeof(InstallConstants), 0, |
| 31 L""}; |
| 32 |
| 33 class ScopedNTRegistryTestingOverride { |
| 34 public: |
| 35 ScopedNTRegistryTestingOverride(nt::ROOT_KEY root, const std::wstring& path) |
| 36 : root_(root) { |
| 37 EXPECT_TRUE(nt::SetTestingOverride(root_, path)); |
| 38 } |
| 39 ~ScopedNTRegistryTestingOverride() { |
| 40 nt::SetTestingOverride(root_, base::string16()); |
| 41 } |
| 42 |
| 43 private: |
| 44 nt::ROOT_KEY root_; |
| 45 }; |
| 46 |
| 47 TEST(UserDataDir, EmptyResultsInDefault) { |
| 48 std::wstring result, invalid; |
| 49 |
| 50 install_static::GetUserDataDirectoryImpl(L"", kFakeInstallConstants, &result, |
| 51 &invalid); |
| 52 EXPECT_TRUE(EndsWith(result, L"\\Google\\Chrome\\User Data")); |
| 53 EXPECT_EQ(std::wstring(), invalid); |
| 54 } |
| 55 |
| 56 TEST(UserDataDir, InvalidResultsInDefault) { |
| 57 std::wstring result, invalid; |
| 58 |
| 59 install_static::GetUserDataDirectoryImpl(L"<>|:", kFakeInstallConstants, |
| 60 &result, &invalid); |
| 61 EXPECT_TRUE(EndsWith(result, L"\\Google\\Chrome\\User Data")); |
| 62 EXPECT_EQ(L"<>|:", invalid); |
| 63 } |
| 64 |
| 65 TEST(UserDataDir, RegistrySettingsInHKLMOverrides) { |
| 66 std::wstring result, invalid; |
| 67 |
| 68 // Override the registry to say one value in HKLM, and confirm it takes |
| 69 // precedence over the command line in both implementations. |
| 70 registry_util::RegistryOverrideManager override_manager; |
| 71 base::string16 temp; |
| 72 override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); |
| 73 ScopedNTRegistryTestingOverride nt_override(nt::HKLM, temp); |
| 74 |
| 75 base::win::RegKey key(HKEY_LOCAL_MACHINE, kPolicyRegistryKey, KEY_WRITE); |
| 76 LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"yyy"); |
| 77 ASSERT_EQ(rv, ERROR_SUCCESS); |
| 78 |
| 79 install_static::GetUserDataDirectoryImpl(L"xxx", kFakeInstallConstants, |
| 80 &result, &invalid); |
| 81 |
| 82 EXPECT_TRUE(EndsWith(result, L"\\yyy")); |
| 83 EXPECT_EQ(std::wstring(), invalid); |
| 84 } |
| 85 |
| 86 TEST(UserDataDir, RegistrySettingsInHKCUOverrides) { |
| 87 std::wstring result, invalid; |
| 88 |
| 89 // Override the registry to say one value in HKCU, and confirm it takes |
| 90 // precedence over the command line in both implementations. |
| 91 registry_util::RegistryOverrideManager override_manager; |
| 92 base::string16 temp; |
| 93 override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp); |
| 94 ScopedNTRegistryTestingOverride nt_override(nt::HKCU, temp); |
| 95 |
| 96 base::win::RegKey key(HKEY_CURRENT_USER, kPolicyRegistryKey, KEY_WRITE); |
| 97 LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"yyy"); |
| 98 ASSERT_EQ(rv, ERROR_SUCCESS); |
| 99 |
| 100 install_static::GetUserDataDirectoryImpl(L"xxx", kFakeInstallConstants, |
| 101 &result, &invalid); |
| 102 |
| 103 EXPECT_TRUE(EndsWith(result, L"\\yyy")); |
| 104 EXPECT_EQ(std::wstring(), invalid); |
| 105 } |
| 106 |
| 107 TEST(UserDataDir, RegistrySettingsInHKLMTakesPrecedenceOverHKCU) { |
| 108 std::wstring result, invalid; |
| 109 |
| 110 // Override the registry in both HKLM and HKCU, and confirm HKLM takes |
| 111 // precedence. |
| 112 registry_util::RegistryOverrideManager override_manager; |
| 113 base::string16 temp; |
| 114 override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); |
| 115 ScopedNTRegistryTestingOverride nt_override(nt::HKLM, temp); |
| 116 LONG rv; |
| 117 base::win::RegKey key1(HKEY_LOCAL_MACHINE, kPolicyRegistryKey, KEY_WRITE); |
| 118 rv = key1.WriteValue(kUserDataDirRegistryKey, L"111"); |
| 119 ASSERT_EQ(rv, ERROR_SUCCESS); |
| 120 |
| 121 override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp); |
| 122 ScopedNTRegistryTestingOverride nt_override2(nt::HKCU, temp); |
| 123 base::win::RegKey key2(HKEY_CURRENT_USER, kPolicyRegistryKey, KEY_WRITE); |
| 124 rv = key2.WriteValue(kUserDataDirRegistryKey, L"222"); |
| 125 ASSERT_EQ(rv, ERROR_SUCCESS); |
| 126 |
| 127 install_static::GetUserDataDirectoryImpl(L"xxx", kFakeInstallConstants, |
| 128 &result, &invalid); |
| 129 |
| 130 EXPECT_TRUE(EndsWith(result, L"\\111")); |
| 131 EXPECT_EQ(std::wstring(), invalid); |
| 132 } |
| 133 |
| 134 TEST(UserDataDir, RegistrySettingWithPathExpansionHKLM) { |
| 135 std::wstring result, invalid; |
| 136 |
| 137 registry_util::RegistryOverrideManager override_manager; |
| 138 base::string16 temp; |
| 139 override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); |
| 140 ScopedNTRegistryTestingOverride nt_override(nt::HKCU, temp); |
| 141 base::win::RegKey key(HKEY_LOCAL_MACHINE, kPolicyRegistryKey, KEY_WRITE); |
| 142 LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"${windows}"); |
| 143 ASSERT_EQ(rv, ERROR_SUCCESS); |
| 144 |
| 145 install_static::GetUserDataDirectoryImpl(L"xxx", kFakeInstallConstants, |
| 146 &result, &invalid); |
| 147 |
| 148 EXPECT_EQ(L"C:\\WINDOWS", result); |
| 149 EXPECT_EQ(strlen("X:\\WINDOWS"), result.size()); |
| 150 EXPECT_EQ(std::wstring::npos, result.find(L"${windows}")); |
| 151 std::wstring upper; |
| 152 std::transform(result.begin(), result.end(), std::back_inserter(upper), |
| 153 toupper); |
| 154 EXPECT_TRUE(EndsWith(upper, L"\\WINDOWS")); |
| 155 EXPECT_EQ(std::wstring(), invalid); |
| 156 } |
| 157 |
| 158 TEST(UserDataDir, RegistrySettingWithPathExpansionHKCU) { |
| 159 std::wstring result, invalid; |
| 160 |
| 161 registry_util::RegistryOverrideManager override_manager; |
| 162 base::string16 temp; |
| 163 override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp); |
| 164 ScopedNTRegistryTestingOverride nt_override(nt::HKCU, temp); |
| 165 base::win::RegKey key(HKEY_CURRENT_USER, kPolicyRegistryKey, KEY_WRITE); |
| 166 LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"${windows}"); |
| 167 ASSERT_EQ(rv, ERROR_SUCCESS); |
| 168 |
| 169 install_static::GetUserDataDirectoryImpl(L"xxx", kFakeInstallConstants, |
| 170 &result, &invalid); |
| 171 |
| 172 EXPECT_EQ(L"C:\\WINDOWS", result); |
| 173 EXPECT_EQ(strlen("X:\\WINDOWS"), result.size()); |
| 174 EXPECT_EQ(std::wstring::npos, result.find(L"${windows}")); |
| 175 std::wstring upper; |
| 176 std::transform(result.begin(), result.end(), std::back_inserter(upper), |
| 177 toupper); |
| 178 EXPECT_TRUE(EndsWith(upper, L"\\WINDOWS")); |
| 179 EXPECT_EQ(std::wstring(), invalid); |
| 180 } |
| 181 |
| 182 } // namespace |
| 183 } // namespace install_static |
OLD | NEW |