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/strings/utf_string_conversions.h" |
| 8 #include "base/test/test_reg_util_win.h" |
| 9 #include "chrome/common/chrome_paths.h" |
| 10 #include "chrome/install_static/install_util.h" |
| 11 #include "chrome_elf/nt_registry/nt_registry.h" |
| 12 #include "components/policy/policy_constants.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 inline bool EndsWith(const std::wstring& value, const std::wstring& ending) { |
| 18 if (ending.size() > value.size()) |
| 19 return false; |
| 20 return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 TEST(UserDataDir, EmptyResultsInDefault) { |
| 26 std::wstring result, invalid; |
| 27 |
| 28 install_static::GetUserDataDirectory(L"", &result, &invalid); |
| 29 EXPECT_TRUE(EndsWith(result, L"\\Google\\Chrome\\User Data")); |
| 30 EXPECT_EQ(std::wstring(), invalid); |
| 31 } |
| 32 |
| 33 TEST(UserDataDir, InvalidResultsInDefault) { |
| 34 std::wstring result, invalid; |
| 35 |
| 36 install_static::GetUserDataDirectory(L"<>|:", &result, &invalid); |
| 37 EXPECT_TRUE(EndsWith(result, L"\\Google\\Chrome\\User Data")); |
| 38 EXPECT_EQ(L"<>|:", invalid); |
| 39 } |
| 40 |
| 41 TEST(UserDataDir, RegistrySettingsInHKLMOverrides) { |
| 42 std::wstring result, invalid; |
| 43 |
| 44 // Override the registry to say one value in HKLM, and confirm it takes |
| 45 // precedence over the command line in both implementations. |
| 46 registry_util::RegistryOverrideManager override_manager; |
| 47 base::string16 temp; |
| 48 override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); |
| 49 ASSERT_TRUE(nt::SetTestingOverride(nt::HKLM, temp)); |
| 50 |
| 51 base::win::RegKey key(HKEY_LOCAL_MACHINE, policy::kRegistryChromePolicyKey, |
| 52 KEY_WRITE); |
| 53 LONG rv = key.WriteValue( |
| 54 base::UTF8ToUTF16(policy::key::kUserDataDir).c_str(), L"yyy"); |
| 55 ASSERT_EQ(rv, ERROR_SUCCESS); |
| 56 |
| 57 install_static::GetUserDataDirectory(L"xxx", &result, &invalid); |
| 58 |
| 59 EXPECT_TRUE(EndsWith(result, L"\\yyy")); |
| 60 EXPECT_EQ(std::wstring(), invalid); |
| 61 } |
| 62 |
| 63 TEST(UserDataDir, RegistrySettingsInHKCUOverrides) { |
| 64 std::wstring result, invalid; |
| 65 |
| 66 // Override the registry to say one value in HKCU, 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_CURRENT_USER, &temp); |
| 71 ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); |
| 72 |
| 73 base::win::RegKey key(HKEY_CURRENT_USER, policy::kRegistryChromePolicyKey, |
| 74 KEY_WRITE); |
| 75 LONG rv = key.WriteValue( |
| 76 base::UTF8ToUTF16(policy::key::kUserDataDir).c_str(), L"yyy"); |
| 77 ASSERT_EQ(rv, ERROR_SUCCESS); |
| 78 |
| 79 install_static::GetUserDataDirectory(L"xxx", &result, &invalid); |
| 80 |
| 81 EXPECT_TRUE(EndsWith(result, L"\\yyy")); |
| 82 EXPECT_EQ(std::wstring(), invalid); |
| 83 } |
| 84 |
| 85 TEST(UserDataDir, RegistrySettingsInHKLMTakesPrecedenceOverHKCU) { |
| 86 std::wstring result, invalid; |
| 87 |
| 88 // Override the registry in both HKLM and HKCU, and confirm HKLM takes |
| 89 // precedence. |
| 90 registry_util::RegistryOverrideManager override_manager; |
| 91 base::string16 temp; |
| 92 override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); |
| 93 ASSERT_TRUE(nt::SetTestingOverride(nt::HKLM, temp)); |
| 94 LONG rv; |
| 95 base::win::RegKey key1(HKEY_LOCAL_MACHINE, policy::kRegistryChromePolicyKey, |
| 96 KEY_WRITE); |
| 97 rv = key1.WriteValue(base::UTF8ToUTF16(policy::key::kUserDataDir).c_str(), |
| 98 L"111"); |
| 99 ASSERT_EQ(rv, ERROR_SUCCESS); |
| 100 |
| 101 override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp); |
| 102 ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); |
| 103 base::win::RegKey key2(HKEY_CURRENT_USER, policy::kRegistryChromePolicyKey, |
| 104 KEY_WRITE); |
| 105 rv = key2.WriteValue(base::UTF8ToUTF16(policy::key::kUserDataDir).c_str(), |
| 106 L"222"); |
| 107 ASSERT_EQ(rv, ERROR_SUCCESS); |
| 108 |
| 109 install_static::GetUserDataDirectory(L"xxx", &result, &invalid); |
| 110 |
| 111 EXPECT_TRUE(EndsWith(result, L"\\111")); |
| 112 EXPECT_EQ(std::wstring(), invalid); |
| 113 } |
| 114 |
| 115 TEST(UserDataDir, RegistrySettingWithPathExpansion) { |
| 116 std::wstring result, invalid; |
| 117 |
| 118 registry_util::RegistryOverrideManager override_manager; |
| 119 base::string16 temp; |
| 120 override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp); |
| 121 ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); |
| 122 base::win::RegKey key(HKEY_CURRENT_USER, policy::kRegistryChromePolicyKey, |
| 123 KEY_WRITE); |
| 124 LONG rv = key.WriteValue( |
| 125 base::UTF8ToUTF16(policy::key::kUserDataDir).c_str(), L"${windows}"); |
| 126 ASSERT_EQ(rv, ERROR_SUCCESS); |
| 127 |
| 128 install_static::GetUserDataDirectory(L"xxx", &result, &invalid); |
| 129 |
| 130 EXPECT_EQ(strlen("X:\\WINDOWS"), result.size()); |
| 131 EXPECT_EQ(std::wstring::npos, result.find(L"${windows}")); |
| 132 std::wstring upper; |
| 133 std::transform(result.begin(), result.end(), std::back_inserter(upper), |
| 134 toupper); |
| 135 EXPECT_TRUE(EndsWith(upper, L"\\WINDOWS")); |
| 136 EXPECT_EQ(std::wstring(), invalid); |
| 137 } |
OLD | NEW |