Index: chrome/install_static/user_data_dir_win_unittest.cc |
diff --git a/chrome/install_static/user_data_dir_win_unittest.cc b/chrome/install_static/user_data_dir_win_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c6d5724c2041fc489b3ac8894e1a48a9239abd29 |
--- /dev/null |
+++ b/chrome/install_static/user_data_dir_win_unittest.cc |
@@ -0,0 +1,181 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <algorithm> |
+ |
+#include "base/test/test_reg_util_win.h" |
+#include "chrome/install_static/install_details.h" |
+#include "chrome/install_static/product_install_details.h" |
+#include "chrome_elf/nt_registry/nt_registry.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace install_static { |
+namespace { |
+ |
+inline bool EndsWith(const std::wstring& value, const std::wstring& ending) { |
+ if (ending.size() > value.size()) |
+ return false; |
+ return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); |
+} |
+ |
+#if defined(GOOGLE_CHROME_BUILD) |
+const wchar_t kPolicyRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; |
+#else |
+const wchar_t kPolicyRegistryKey[] = L"SOFTWARE\\Policies\\Chromium"; |
+#endif |
+ |
+const wchar_t kUserDataDirRegistryKey[] = L"UserDataDir"; |
+ |
+const InstallConstants kFakeInstallConstants = { |
+ 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.
|
+ ChannelStrategy::UNSUPPORTED, true, true}; |
+const InstallDetails::Payload kFakePayload = {sizeof(InstallDetails::Payload), |
+ nullptr, &kFakeInstallConstants}; |
+ |
+class PartialInstallDetails : public InstallDetails { |
+ public: |
+ PartialInstallDetails() : InstallDetails(&kFakePayload) {} |
+ PartialInstallDetails(const PartialInstallDetails&) = delete; |
+ PartialInstallDetails& operator=(const PartialInstallDetails&) = delete; |
+}; |
+ |
+const PartialInstallDetails kPartialInstallDetails; |
+ |
+TEST(UserDataDir, EmptyResultsInDefault) { |
+ std::wstring result, invalid; |
+ |
+ install_static::GetUserDataDirectory(L"", kPartialInstallDetails, &result, |
+ &invalid); |
+ EXPECT_TRUE(EndsWith(result, L"\\Google\\Chrome\\User Data")); |
+ EXPECT_EQ(std::wstring(), invalid); |
+} |
+ |
+TEST(UserDataDir, InvalidResultsInDefault) { |
+ std::wstring result, invalid; |
+ |
+ install_static::GetUserDataDirectory(L"<>|:", kPartialInstallDetails, &result, |
+ &invalid); |
+ EXPECT_TRUE(EndsWith(result, L"\\Google\\Chrome\\User Data")); |
+ EXPECT_EQ(L"<>|:", invalid); |
+} |
+ |
+TEST(UserDataDir, RegistrySettingsInHKLMOverrides) { |
+ std::wstring result, invalid; |
+ |
+ // Override the registry to say one value in HKLM, and confirm it takes |
+ // precedence over the command line in both implementations. |
+ registry_util::RegistryOverrideManager override_manager; |
+ base::string16 temp; |
+ override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); |
+ 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
|
+ |
+ base::win::RegKey key(HKEY_LOCAL_MACHINE, kPolicyRegistryKey, KEY_WRITE); |
+ LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"yyy"); |
+ ASSERT_EQ(rv, ERROR_SUCCESS); |
+ |
+ install_static::GetUserDataDirectory(L"xxx", kPartialInstallDetails, &result, |
+ &invalid); |
+ |
+ EXPECT_TRUE(EndsWith(result, L"\\yyy")); |
+ EXPECT_EQ(std::wstring(), invalid); |
+} |
+ |
+TEST(UserDataDir, RegistrySettingsInHKCUOverrides) { |
+ std::wstring result, invalid; |
+ |
+ // Override the registry to say one value in HKCU, and confirm it takes |
+ // precedence over the command line in both implementations. |
+ registry_util::RegistryOverrideManager override_manager; |
+ base::string16 temp; |
+ override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp); |
+ ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); |
+ |
+ base::win::RegKey key(HKEY_CURRENT_USER, kPolicyRegistryKey, KEY_WRITE); |
+ LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"yyy"); |
+ ASSERT_EQ(rv, ERROR_SUCCESS); |
+ |
+ install_static::GetUserDataDirectory(L"xxx", kPartialInstallDetails, &result, |
+ &invalid); |
+ |
+ EXPECT_TRUE(EndsWith(result, L"\\yyy")); |
+ EXPECT_EQ(std::wstring(), invalid); |
+} |
+ |
+TEST(UserDataDir, RegistrySettingsInHKLMTakesPrecedenceOverHKCU) { |
+ std::wstring result, invalid; |
+ |
+ // Override the registry in both HKLM and HKCU, and confirm HKLM takes |
+ // precedence. |
+ registry_util::RegistryOverrideManager override_manager; |
+ base::string16 temp; |
+ override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); |
+ ASSERT_TRUE(nt::SetTestingOverride(nt::HKLM, temp)); |
+ LONG rv; |
+ base::win::RegKey key1(HKEY_LOCAL_MACHINE, kPolicyRegistryKey, KEY_WRITE); |
+ rv = key1.WriteValue(kUserDataDirRegistryKey, L"111"); |
+ ASSERT_EQ(rv, ERROR_SUCCESS); |
+ |
+ override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp); |
+ ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); |
+ base::win::RegKey key2(HKEY_CURRENT_USER, kPolicyRegistryKey, KEY_WRITE); |
+ rv = key2.WriteValue(kUserDataDirRegistryKey, L"222"); |
+ ASSERT_EQ(rv, ERROR_SUCCESS); |
+ |
+ install_static::GetUserDataDirectory(L"xxx", kPartialInstallDetails, &result, |
+ &invalid); |
+ |
+ EXPECT_TRUE(EndsWith(result, L"\\111")); |
+ EXPECT_EQ(std::wstring(), invalid); |
+} |
+ |
+TEST(UserDataDir, RegistrySettingWithPathExpansionHKLM) { |
+ std::wstring result, invalid; |
+ |
+ registry_util::RegistryOverrideManager override_manager; |
+ base::string16 temp; |
+ override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); |
+ ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); |
+ base::win::RegKey key(HKEY_LOCAL_MACHINE, kPolicyRegistryKey, KEY_WRITE); |
+ LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"${windows}"); |
+ ASSERT_EQ(rv, ERROR_SUCCESS); |
+ |
+ install_static::GetUserDataDirectory(L"xxx", kPartialInstallDetails, &result, |
+ &invalid); |
+ |
+ EXPECT_EQ(L"C:\\WINDOWS", result); |
+ EXPECT_EQ(strlen("X:\\WINDOWS"), result.size()); |
+ EXPECT_EQ(std::wstring::npos, result.find(L"${windows}")); |
+ std::wstring upper; |
+ std::transform(result.begin(), result.end(), std::back_inserter(upper), |
+ toupper); |
+ EXPECT_TRUE(EndsWith(upper, L"\\WINDOWS")); |
+ EXPECT_EQ(std::wstring(), invalid); |
+} |
+ |
+TEST(UserDataDir, RegistrySettingWithPathExpansionHKCU) { |
+ std::wstring result, invalid; |
+ |
+ registry_util::RegistryOverrideManager override_manager; |
+ base::string16 temp; |
+ override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp); |
+ ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); |
+ base::win::RegKey key(HKEY_CURRENT_USER, kPolicyRegistryKey, KEY_WRITE); |
+ LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"${windows}"); |
+ ASSERT_EQ(rv, ERROR_SUCCESS); |
+ |
+ install_static::GetUserDataDirectory(L"xxx", kPartialInstallDetails, &result, |
+ &invalid); |
+ |
+ EXPECT_EQ(L"C:\\WINDOWS", result); |
+ EXPECT_EQ(strlen("X:\\WINDOWS"), result.size()); |
+ EXPECT_EQ(std::wstring::npos, result.find(L"${windows}")); |
+ std::wstring upper; |
+ std::transform(result.begin(), result.end(), std::back_inserter(upper), |
+ toupper); |
+ EXPECT_TRUE(EndsWith(upper, L"\\WINDOWS")); |
+ EXPECT_EQ(std::wstring(), invalid); |
+} |
+ |
+} // namespace |
+} // namespace install_static |