Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(720)

Side by Side Diff: chrome/install_static/user_data_dir_win_unittest.cc

Issue 2487783002: Make Crashpad use the user data dir, rather than always default location (Closed)
Patch Set: install_static for swarming Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/install_static/user_data_dir.cc ('k') | chrome_elf/chrome_elf.def » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 const wchar_t kUserDataDirNameSuffix[] = L"\\Google\\Chrome\\User Data";
25 #else
26 const wchar_t kPolicyRegistryKey[] = L"SOFTWARE\\Policies\\Chromium";
27 const wchar_t kUserDataDirNameSuffix[] = L"\\Chromium\\User Data";
28 #endif
29
30 const wchar_t kUserDataDirRegistryKey[] = L"UserDataDir";
31
32 const InstallConstants kFakeInstallConstants = {sizeof(InstallConstants), 0,
33 L""};
34
35 class ScopedNTRegistryTestingOverride {
36 public:
37 ScopedNTRegistryTestingOverride(nt::ROOT_KEY root, const std::wstring& path)
38 : root_(root) {
39 EXPECT_TRUE(nt::SetTestingOverride(root_, path));
40 }
41 ~ScopedNTRegistryTestingOverride() {
42 nt::SetTestingOverride(root_, base::string16());
43 }
44
45 private:
46 nt::ROOT_KEY root_;
47 };
48
49 TEST(UserDataDir, EmptyResultsInDefault) {
50 std::wstring result, invalid;
51
52 install_static::GetUserDataDirectoryImpl(L"", kFakeInstallConstants, &result,
53 &invalid);
54 EXPECT_TRUE(EndsWith(result, kUserDataDirNameSuffix));
55 EXPECT_EQ(std::wstring(), invalid);
56 }
57
58 TEST(UserDataDir, InvalidResultsInDefault) {
59 std::wstring result, invalid;
60
61 install_static::GetUserDataDirectoryImpl(L"<>|:", kFakeInstallConstants,
62 &result, &invalid);
63 EXPECT_TRUE(EndsWith(result, kUserDataDirNameSuffix));
64 EXPECT_EQ(L"<>|:", invalid);
65 }
66
67 TEST(UserDataDir, RegistrySettingsInHKLMOverrides) {
68 std::wstring result, invalid;
69
70 // Override the registry to say one value in HKLM, and confirm it takes
71 // precedence over the command line in both implementations.
72 registry_util::RegistryOverrideManager override_manager;
73 base::string16 temp;
74 override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp);
75 ScopedNTRegistryTestingOverride nt_override(nt::HKLM, temp);
76
77 base::win::RegKey key(HKEY_LOCAL_MACHINE, kPolicyRegistryKey, KEY_WRITE);
78 LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"yyy");
79 ASSERT_EQ(rv, ERROR_SUCCESS);
80
81 install_static::GetUserDataDirectoryImpl(L"xxx", kFakeInstallConstants,
82 &result, &invalid);
83
84 EXPECT_TRUE(EndsWith(result, L"\\yyy"));
85 EXPECT_EQ(std::wstring(), invalid);
86 }
87
88 TEST(UserDataDir, RegistrySettingsInHKCUOverrides) {
89 std::wstring result, invalid;
90
91 // Override the registry to say one value in HKCU, and confirm it takes
92 // precedence over the command line in both implementations.
93 registry_util::RegistryOverrideManager override_manager;
94 base::string16 temp;
95 override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp);
96 ScopedNTRegistryTestingOverride nt_override(nt::HKCU, temp);
97
98 base::win::RegKey key(HKEY_CURRENT_USER, kPolicyRegistryKey, KEY_WRITE);
99 LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"yyy");
100 ASSERT_EQ(rv, ERROR_SUCCESS);
101
102 install_static::GetUserDataDirectoryImpl(L"xxx", kFakeInstallConstants,
103 &result, &invalid);
104
105 EXPECT_TRUE(EndsWith(result, L"\\yyy"));
106 EXPECT_EQ(std::wstring(), invalid);
107 }
108
109 TEST(UserDataDir, RegistrySettingsInHKLMTakesPrecedenceOverHKCU) {
110 std::wstring result, invalid;
111
112 // Override the registry in both HKLM and HKCU, and confirm HKLM takes
113 // precedence.
114 registry_util::RegistryOverrideManager override_manager;
115 base::string16 temp;
116 override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp);
117 ScopedNTRegistryTestingOverride nt_override(nt::HKLM, temp);
118 LONG rv;
119 base::win::RegKey key1(HKEY_LOCAL_MACHINE, kPolicyRegistryKey, KEY_WRITE);
120 rv = key1.WriteValue(kUserDataDirRegistryKey, L"111");
tapted 2017/02/05 23:59:46 Some tests have started failing with output like
121 ASSERT_EQ(rv, ERROR_SUCCESS);
122
123 override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp);
124 ScopedNTRegistryTestingOverride nt_override2(nt::HKCU, temp);
125 base::win::RegKey key2(HKEY_CURRENT_USER, kPolicyRegistryKey, KEY_WRITE);
126 rv = key2.WriteValue(kUserDataDirRegistryKey, L"222");
127 ASSERT_EQ(rv, ERROR_SUCCESS);
128
129 install_static::GetUserDataDirectoryImpl(L"xxx", kFakeInstallConstants,
130 &result, &invalid);
131
132 EXPECT_TRUE(EndsWith(result, L"\\111"));
133 EXPECT_EQ(std::wstring(), invalid);
134 }
135
136 TEST(UserDataDir, RegistrySettingWithPathExpansionHKCU) {
137 std::wstring result, invalid;
138
139 registry_util::RegistryOverrideManager override_manager;
140 base::string16 temp;
141 override_manager.OverrideRegistry(HKEY_CURRENT_USER, &temp);
142 ScopedNTRegistryTestingOverride nt_override(nt::HKCU, temp);
143 base::win::RegKey key(HKEY_CURRENT_USER, kPolicyRegistryKey, KEY_WRITE);
144 LONG rv = key.WriteValue(kUserDataDirRegistryKey, L"${windows}");
145 ASSERT_EQ(rv, ERROR_SUCCESS);
146
147 install_static::GetUserDataDirectoryImpl(L"xxx", kFakeInstallConstants,
148 &result, &invalid);
149
150 EXPECT_EQ(strlen("X:\\WINDOWS"), result.size());
151 EXPECT_EQ(std::wstring::npos, result.find(L"${windows}"));
152 std::wstring upper;
153 std::transform(result.begin(), result.end(), std::back_inserter(upper),
154 toupper);
155 EXPECT_TRUE(EndsWith(upper, L"\\WINDOWS"));
156 EXPECT_EQ(std::wstring(), invalid);
157 }
158
159 } // namespace
160 } // namespace install_static
OLDNEW
« no previous file with comments | « chrome/install_static/user_data_dir.cc ('k') | chrome_elf/chrome_elf.def » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698