Chromium Code Reviews| 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/product_install_details.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 = { | |
| 31 sizeof(InstallConstants), 0, L"", nullptr, nullptr, | |
|
grt (UTC plus 2)
2016/11/22 11:37:09
barf. i just learned about:
// clang-format off
scottmg
2016/11/22 17:16:07
I don't think this is bad enough to warrant that.
| |
| 32 ChannelStrategy::UNSUPPORTED, true, true}; | |
| 33 const InstallDetails::Payload kFakePayload = {sizeof(InstallDetails::Payload), | |
| 34 nullptr, &kFakeInstallConstants}; | |
| 35 | |
| 36 class PartialInstallDetails : public InstallDetails { | |
| 37 public: | |
| 38 PartialInstallDetails() : InstallDetails(&kFakePayload) {} | |
| 39 PartialInstallDetails(const PartialInstallDetails&) = delete; | |
| 40 PartialInstallDetails& operator=(const PartialInstallDetails&) = delete; | |
| 41 }; | |
| 42 | |
| 43 const PartialInstallDetails kPartialInstallDetails; | |
| 44 | |
| 45 TEST(UserDataDir, EmptyResultsInDefault) { | |
| 46 std::wstring result, invalid; | |
| 47 | |
| 48 install_static::GetUserDataDirectory(L"", kPartialInstallDetails, &result, | |
| 49 &invalid); | |
| 50 EXPECT_TRUE(EndsWith(result, L"\\Google\\Chrome\\User Data")); | |
| 51 EXPECT_EQ(std::wstring(), invalid); | |
| 52 } | |
| 53 | |
| 54 TEST(UserDataDir, InvalidResultsInDefault) { | |
| 55 std::wstring result, invalid; | |
| 56 | |
| 57 install_static::GetUserDataDirectory(L"<>|:", kPartialInstallDetails, &result, | |
| 58 &invalid); | |
| 59 EXPECT_TRUE(EndsWith(result, L"\\Google\\Chrome\\User Data")); | |
| 60 EXPECT_EQ(L"<>|:", invalid); | |
| 61 } | |
| 62 | |
| 63 TEST(UserDataDir, RegistrySettingsInHKLMOverrides) { | |
| 64 std::wstring result, invalid; | |
| 65 | |
| 66 // Override the registry to say one value in HKLM, and confirm it takes | |
| 67 // precedence over the command line in both implementations. | |
| 68 registry_util::RegistryOverrideManager override_manager; | |
| 69 base::string16 temp; | |
| 70 override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); | |
| 71 ASSERT_TRUE(nt::SetTestingOverride(nt::HKLM, temp)); | |
|
grt (UTC plus 2)
2016/11/22 11:37:09
this needs to be paired with SetTestingOverride(nt
scottmg
2016/11/22 17:16:07
Yucky, done. I made it a scoped class here rather
| |
| 72 | |
| 73 base::win::RegKey key(HKEY_LOCAL_MACHINE, kPolicyRegistryKey, KEY_WRITE); | |
| 74 LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"yyy"); | |
| 75 ASSERT_EQ(rv, ERROR_SUCCESS); | |
| 76 | |
| 77 install_static::GetUserDataDirectory(L"xxx", kPartialInstallDetails, &result, | |
| 78 &invalid); | |
| 79 | |
| 80 EXPECT_TRUE(EndsWith(result, L"\\yyy")); | |
| 81 EXPECT_EQ(std::wstring(), invalid); | |
| 82 } | |
| 83 | |
| 84 TEST(UserDataDir, RegistrySettingsInHKCUOverrides) { | |
| 85 std::wstring result, invalid; | |
| 86 | |
| 87 // Override the registry to say one value in HKCU, and confirm it takes | |
| 88 // precedence over the command line in both implementations. | |
| 89 registry_util::RegistryOverrideManager override_manager; | |
| 90 base::string16 temp; | |
| 91 override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp); | |
| 92 ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); | |
| 93 | |
| 94 base::win::RegKey key(HKEY_CURRENT_USER, kPolicyRegistryKey, KEY_WRITE); | |
| 95 LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"yyy"); | |
| 96 ASSERT_EQ(rv, ERROR_SUCCESS); | |
| 97 | |
| 98 install_static::GetUserDataDirectory(L"xxx", kPartialInstallDetails, &result, | |
| 99 &invalid); | |
| 100 | |
| 101 EXPECT_TRUE(EndsWith(result, L"\\yyy")); | |
| 102 EXPECT_EQ(std::wstring(), invalid); | |
| 103 } | |
| 104 | |
| 105 TEST(UserDataDir, RegistrySettingsInHKLMTakesPrecedenceOverHKCU) { | |
| 106 std::wstring result, invalid; | |
| 107 | |
| 108 // Override the registry in both HKLM and HKCU, and confirm HKLM takes | |
| 109 // precedence. | |
| 110 registry_util::RegistryOverrideManager override_manager; | |
| 111 base::string16 temp; | |
| 112 override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); | |
| 113 ASSERT_TRUE(nt::SetTestingOverride(nt::HKLM, temp)); | |
| 114 LONG rv; | |
| 115 base::win::RegKey key1(HKEY_LOCAL_MACHINE, kPolicyRegistryKey, KEY_WRITE); | |
| 116 rv = key1.WriteValue(kUserDataDirRegistryKey, L"111"); | |
| 117 ASSERT_EQ(rv, ERROR_SUCCESS); | |
| 118 | |
| 119 override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp); | |
| 120 ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); | |
| 121 base::win::RegKey key2(HKEY_CURRENT_USER, kPolicyRegistryKey, KEY_WRITE); | |
| 122 rv = key2.WriteValue(kUserDataDirRegistryKey, L"222"); | |
| 123 ASSERT_EQ(rv, ERROR_SUCCESS); | |
| 124 | |
| 125 install_static::GetUserDataDirectory(L"xxx", kPartialInstallDetails, &result, | |
| 126 &invalid); | |
| 127 | |
| 128 EXPECT_TRUE(EndsWith(result, L"\\111")); | |
| 129 EXPECT_EQ(std::wstring(), invalid); | |
| 130 } | |
| 131 | |
| 132 TEST(UserDataDir, RegistrySettingWithPathExpansionHKLM) { | |
| 133 std::wstring result, invalid; | |
| 134 | |
| 135 registry_util::RegistryOverrideManager override_manager; | |
| 136 base::string16 temp; | |
| 137 override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); | |
| 138 ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); | |
| 139 base::win::RegKey key(HKEY_LOCAL_MACHINE, kPolicyRegistryKey, KEY_WRITE); | |
| 140 LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"${windows}"); | |
| 141 ASSERT_EQ(rv, ERROR_SUCCESS); | |
| 142 | |
| 143 install_static::GetUserDataDirectory(L"xxx", kPartialInstallDetails, &result, | |
| 144 &invalid); | |
| 145 | |
| 146 EXPECT_EQ(L"C:\\WINDOWS", result); | |
| 147 EXPECT_EQ(strlen("X:\\WINDOWS"), result.size()); | |
| 148 EXPECT_EQ(std::wstring::npos, result.find(L"${windows}")); | |
| 149 std::wstring upper; | |
| 150 std::transform(result.begin(), result.end(), std::back_inserter(upper), | |
| 151 toupper); | |
| 152 EXPECT_TRUE(EndsWith(upper, L"\\WINDOWS")); | |
| 153 EXPECT_EQ(std::wstring(), invalid); | |
| 154 } | |
| 155 | |
| 156 TEST(UserDataDir, RegistrySettingWithPathExpansionHKCU) { | |
| 157 std::wstring result, invalid; | |
| 158 | |
| 159 registry_util::RegistryOverrideManager override_manager; | |
| 160 base::string16 temp; | |
| 161 override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp); | |
| 162 ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); | |
| 163 base::win::RegKey key(HKEY_CURRENT_USER, kPolicyRegistryKey, KEY_WRITE); | |
| 164 LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"${windows}"); | |
| 165 ASSERT_EQ(rv, ERROR_SUCCESS); | |
| 166 | |
| 167 install_static::GetUserDataDirectory(L"xxx", kPartialInstallDetails, &result, | |
| 168 &invalid); | |
| 169 | |
| 170 EXPECT_EQ(L"C:\\WINDOWS", result); | |
| 171 EXPECT_EQ(strlen("X:\\WINDOWS"), result.size()); | |
| 172 EXPECT_EQ(std::wstring::npos, result.find(L"${windows}")); | |
| 173 std::wstring upper; | |
| 174 std::transform(result.begin(), result.end(), std::back_inserter(upper), | |
| 175 toupper); | |
| 176 EXPECT_TRUE(EndsWith(upper, L"\\WINDOWS")); | |
| 177 EXPECT_EQ(std::wstring(), invalid); | |
| 178 } | |
| 179 | |
| 180 } // namespace | |
| 181 } // namespace install_static | |
| OLD | NEW |