OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/install_static/install_util.h" | 5 #include "chrome/install_static/install_util.h" |
6 | 6 |
| 7 #include <tuple> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/test/test_reg_util_win.h" |
| 12 #include "chrome/install_static/install_details.h" |
| 13 #include "chrome/install_static/install_modes.h" |
| 14 #include "chrome/install_static/product_install_details.h" |
| 15 #include "chrome_elf/nt_registry/nt_registry.h" |
7 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
9 | 18 |
10 using ::testing::ElementsAre; | 19 using ::testing::ElementsAre; |
11 | 20 |
12 namespace install_static { | 21 namespace install_static { |
13 | 22 |
14 // Tests the MatchPattern function in the install_static library. | 23 // Tests the MatchPattern function in the install_static library. |
15 TEST(InstallStaticTest, MatchPattern) { | 24 TEST(InstallStaticTest, MatchPattern) { |
16 EXPECT_TRUE(MatchPattern(L"", L"")); | 25 EXPECT_TRUE(MatchPattern(L"", L"")); |
17 EXPECT_TRUE(MatchPattern(L"", L"*")); | 26 EXPECT_TRUE(MatchPattern(L"", L"*")); |
18 EXPECT_FALSE(MatchPattern(L"", L"*a")); | 27 EXPECT_FALSE(MatchPattern(L"", L"*a")); |
19 EXPECT_FALSE(MatchPattern(L"", L"abc")); | 28 EXPECT_FALSE(MatchPattern(L"", L"abc")); |
20 EXPECT_TRUE(MatchPattern(L"Hello1234", L"He??o*1*")); | 29 EXPECT_TRUE(MatchPattern(L"Hello1234", L"He??o*1*")); |
21 EXPECT_TRUE(MatchPattern(L"Foo", L"F*?")); | 30 EXPECT_TRUE(MatchPattern(L"Foo", L"F*?")); |
22 EXPECT_TRUE(MatchPattern(L"Foo", L"F*")); | 31 EXPECT_TRUE(MatchPattern(L"Foo", L"F*")); |
23 EXPECT_FALSE(MatchPattern(L"Foo", L"F*b")); | 32 EXPECT_FALSE(MatchPattern(L"Foo", L"F*b")); |
24 EXPECT_TRUE(MatchPattern(L"abcd", L"*c*d")); | 33 EXPECT_TRUE(MatchPattern(L"abcd", L"*c*d")); |
25 EXPECT_TRUE(MatchPattern(L"abcd", L"*?c*d")); | 34 EXPECT_TRUE(MatchPattern(L"abcd", L"*?c*d")); |
26 EXPECT_FALSE(MatchPattern(L"abcd", L"abcd*efgh")); | 35 EXPECT_FALSE(MatchPattern(L"abcd", L"abcd*efgh")); |
27 EXPECT_TRUE(MatchPattern(L"foobarabc", L"*bar*")); | 36 EXPECT_TRUE(MatchPattern(L"foobarabc", L"*bar*")); |
28 } | 37 } |
29 | 38 |
30 // Tests the TokenizeString function in the install_static library. | |
31 TEST(InstallStaticTest, TokenizeString) { | |
32 // Test if the string is tokenized correctly with all tokens stripped of | |
33 // leading and trailing spaces. | |
34 std::vector<std::string> results = | |
35 TokenizeString("un |deux\t|trois\n|quatre", '|', true); | |
36 ASSERT_EQ(4u, results.size()); | |
37 EXPECT_THAT(results, ElementsAre("un", "deux", "trois", "quatre")); | |
38 | |
39 // Test if the string is tokenized correctly with all tokens having | |
40 // leading and trailing spaces intact. | |
41 results = TokenizeString("un |deux\t|trois\n|quatre", '|', false); | |
42 ASSERT_EQ(4u, results.size()); | |
43 EXPECT_THAT(results, ElementsAre("un ", "deux\t", "trois\n", "quatre")); | |
44 | |
45 // Test if tokenize returns the original string if a string containing only | |
46 // one token and no delimiters is passed. | |
47 results = TokenizeString("un |deux\t|trois\n|quatre", '!', false); | |
48 ASSERT_EQ(1u, results.size()); | |
49 ASSERT_EQ(results[0], "un |deux\t|trois\n|quatre"); | |
50 | |
51 // Test if tokenize handles a space character as delimiter. | |
52 results = TokenizeString("foo bar bleh blah boo", ' ', false); | |
53 ASSERT_EQ(5u, results.size()); | |
54 EXPECT_THAT(results, ElementsAre("foo", "bar", "bleh", "blah", "boo")); | |
55 | |
56 // Test string with only delimiters. | |
57 results = TokenizeString("||||", '|', false); | |
58 ASSERT_EQ(4u, results.size()); | |
59 EXPECT_THAT(results, ElementsAre("", "", "", "")); | |
60 | |
61 // Test string with spaces separated by delimiters. | |
62 results = TokenizeString(" | | | |", '|', false); | |
63 ASSERT_EQ(4u, results.size()); | |
64 EXPECT_THAT(results, ElementsAre(" ", " ", " ", " ")); | |
65 | |
66 results = TokenizeString("one|two||four", '|', false); | |
67 ASSERT_EQ(4u, results.size()); | |
68 EXPECT_THAT(results, ElementsAre("one", "two", "", "four")); | |
69 | |
70 // TokenizeString16 tests. | |
71 // Test if the string is tokenized correctly with all tokens stripped of | |
72 // leading and trailing spaces. | |
73 std::vector<std::wstring> results16 = | |
74 TokenizeString16(L"un |deux\t|trois\n|quatre", L'|', true); | |
75 ASSERT_EQ(4u, results16.size()); | |
76 EXPECT_THAT(results16, ElementsAre(L"un", L"deux", L"trois", L"quatre")); | |
77 | |
78 // Test string with spaces separated by delimiters. | |
79 results16 = TokenizeString16(L"one|two||four", L'|', false); | |
80 ASSERT_EQ(4u, results16.size()); | |
81 EXPECT_THAT(results16, ElementsAre(L"one", L"two", L"", L"four")); | |
82 } | |
83 | |
84 // Tests the CompareVersionString function in the install_static library. | |
85 TEST(InstallStaticTest, CompareVersions) { | |
86 // Case 1. Invalid versions. | |
87 int result = 0; | |
88 EXPECT_FALSE(CompareVersionStrings("", "", &result)); | |
89 EXPECT_FALSE(CompareVersionStrings("0.0.0.0", "A.B.C.D", &result)); | |
90 EXPECT_FALSE(CompareVersionStrings("A.0.0.0", "0.0.0.0", &result)); | |
91 | |
92 // Case 2. Equal versions. | |
93 EXPECT_TRUE(CompareVersionStrings("0.0.0.0", "0.0.0.0", &result)); | |
94 EXPECT_EQ(0, result); | |
95 | |
96 // Case 3. Version1 > Version 2. | |
97 EXPECT_TRUE(CompareVersionStrings("1.0.0.0", "0.0.0.0", &result)); | |
98 EXPECT_EQ(1, result); | |
99 | |
100 // Case 4. Version1 < Version 2. | |
101 EXPECT_TRUE(CompareVersionStrings("0.0.0.0", "1.0.0.0", &result)); | |
102 EXPECT_EQ(-1, result); | |
103 | |
104 // Case 5. Version1 < Version 2. | |
105 EXPECT_TRUE(CompareVersionStrings("0.0", "0.0.1.0", &result)); | |
106 EXPECT_EQ(-1, result); | |
107 | |
108 // Case 6. Version1 > Version 2. | |
109 EXPECT_TRUE(CompareVersionStrings("0.0.0.2", "0.0", &result)); | |
110 EXPECT_EQ(1, result); | |
111 | |
112 // Case 7. Version1 > Version 2. | |
113 EXPECT_TRUE(CompareVersionStrings("1.1.1.2", "1.1.1.1", &result)); | |
114 EXPECT_EQ(1, result); | |
115 | |
116 // Case 8. Version1 < Version2 | |
117 EXPECT_TRUE(CompareVersionStrings("0.0.0.2", "0.0.0.3", &result)); | |
118 EXPECT_EQ(-1, result); | |
119 | |
120 // Case 9. Version1 > Version2 | |
121 EXPECT_TRUE(CompareVersionStrings("0.0.0.4", "0.0.0.3", &result)); | |
122 EXPECT_EQ(1, result); | |
123 | |
124 // Case 10. Version1 > Version2. Multiple digit numbers. | |
125 EXPECT_TRUE(CompareVersionStrings("0.0.12.1", "0.0.10.3", &result)); | |
126 EXPECT_EQ(1, result); | |
127 | |
128 // Case 11. Version1 < Version2. Multiple digit number. | |
129 EXPECT_TRUE(CompareVersionStrings("10.11.12.13", "12.11.12.13", &result)); | |
130 EXPECT_EQ(-1, result); | |
131 } | |
132 | |
133 // Tests the install_static::GetSwitchValueFromCommandLine function. | 39 // Tests the install_static::GetSwitchValueFromCommandLine function. |
134 TEST(InstallStaticTest, GetSwitchValueFromCommandLineTest) { | 40 TEST(InstallStaticTest, GetSwitchValueFromCommandLineTest) { |
135 // Simple case with one switch. | 41 // Simple case with one switch. |
136 std::string value = | 42 std::string value = |
137 GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe --type=bar", "type"); | 43 GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe --type=bar", "type"); |
138 EXPECT_EQ("bar", value); | 44 EXPECT_EQ("bar", value); |
139 | 45 |
140 // Multiple switches with trailing spaces between them. | 46 // Multiple switches with trailing spaces between them. |
141 value = GetSwitchValueFromCommandLine( | 47 value = GetSwitchValueFromCommandLine( |
142 "c:\\temp\\bleh.exe --type=bar --abc=def bleh", "abc"); | 48 "c:\\temp\\bleh.exe --type=bar --abc=def bleh", "abc"); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 value = GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe --type= bar", | 93 value = GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe --type= bar", |
188 "type"); | 94 "type"); |
189 EXPECT_EQ("bar", value); | 95 EXPECT_EQ("bar", value); |
190 | 96 |
191 // Tabs after the "=" before the value. | 97 // Tabs after the "=" before the value. |
192 value = GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe --type=\t\t\tbar", | 98 value = GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe --type=\t\t\tbar", |
193 "type"); | 99 "type"); |
194 EXPECT_EQ(value, "bar"); | 100 EXPECT_EQ(value, "bar"); |
195 } | 101 } |
196 | 102 |
| 103 TEST(InstallStaticTest, BrowserProcessTest) { |
| 104 EXPECT_EQ(ProcessType::UNINITIALIZED, g_process_type); |
| 105 InitializeProcessType(); |
| 106 EXPECT_FALSE(IsNonBrowserProcess()); |
| 107 } |
| 108 |
| 109 class InstallStaticUtilTest |
| 110 : public ::testing::TestWithParam< |
| 111 std::tuple<InstallConstantIndex, const char*, const char*>> { |
| 112 protected: |
| 113 InstallStaticUtilTest() { |
| 114 InstallConstantIndex mode_index; |
| 115 const char* level; |
| 116 const char* mode; |
| 117 |
| 118 std::tie(mode_index, level, mode) = GetParam(); |
| 119 |
| 120 mode_ = &kInstallModes[mode_index]; |
| 121 system_level_ = std::string(level) != "user"; |
| 122 EXPECT_TRUE(!system_level_ || mode_->supports_system_level); |
| 123 multi_install_ = std::string(mode) != "single"; |
| 124 EXPECT_TRUE(!multi_install_ || mode_->supports_multi_install); |
| 125 root_key_ = system_level_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
| 126 nt_root_key_ = system_level_ ? nt::HKLM : nt::HKCU; |
| 127 |
| 128 std::unique_ptr<PrimaryInstallDetails> details = |
| 129 base::MakeUnique<PrimaryInstallDetails>(); |
| 130 details->set_mode(mode_); |
| 131 details->set_channel(mode_->default_channel_name); |
| 132 details->set_system_level(system_level_); |
| 133 details->set_multi_install(multi_install_); |
| 134 InstallDetails::SetForProcess(std::move(details)); |
| 135 |
| 136 base::string16 path; |
| 137 override_manager_.OverrideRegistry(root_key_, &path); |
| 138 nt::SetTestingOverride(nt_root_key_, path); |
| 139 } |
| 140 |
| 141 ~InstallStaticUtilTest() { |
| 142 InstallDetails::SetForProcess(nullptr); |
| 143 nt::SetTestingOverride(nt_root_key_, base::string16()); |
| 144 } |
| 145 |
| 146 bool system_level() const { return system_level_; } |
| 147 |
| 148 bool multi_install() const { return multi_install_; } |
| 149 |
| 150 const wchar_t* default_channel() const { return mode_->default_channel_name; } |
| 151 |
| 152 void SetUsageStat(DWORD value, bool medium) { |
| 153 ASSERT_TRUE(!medium || system_level_); |
| 154 ASSERT_EQ(ERROR_SUCCESS, |
| 155 base::win::RegKey(root_key_, GetUsageStatsKeyPath(medium).c_str(), |
| 156 KEY_SET_VALUE | KEY_WOW64_32KEY) |
| 157 .WriteValue(L"usagestats", value)); |
| 158 } |
| 159 |
| 160 void SetMetricsReportingPolicy(DWORD value) { |
| 161 #if defined(GOOGLE_CHROME_BUILD) |
| 162 static constexpr wchar_t kPolicyKey[] = |
| 163 L"Software\\Policies\\Google\\Chrome"; |
| 164 #else |
| 165 static constexpr wchar_t kPolicyKey[] = L"Software\\Policies\\Chromium"; |
| 166 #endif |
| 167 |
| 168 ASSERT_EQ(ERROR_SUCCESS, |
| 169 base::win::RegKey(root_key_, kPolicyKey, KEY_SET_VALUE) |
| 170 .WriteValue(L"MetricsReportingEnabled", value)); |
| 171 } |
| 172 |
| 173 private: |
| 174 // Returns the registry path for the key holding the product's usagestats |
| 175 // value. |medium| = true returns the path for ClientStateMedium. |
| 176 std::wstring GetUsageStatsKeyPath(bool medium) { |
| 177 EXPECT_TRUE(!medium || system_level_); |
| 178 |
| 179 std::wstring result(L"Software\\"); |
| 180 if (kUseGoogleUpdateIntegration) { |
| 181 result.append(L"Google\\Update\\ClientState"); |
| 182 if (medium) |
| 183 result.append(L"Medium"); |
| 184 result.push_back(L'\\'); |
| 185 if (multi_install_) |
| 186 result.append(kBinariesAppGuid); |
| 187 else |
| 188 result.append(mode_->app_guid); |
| 189 } else if (multi_install_) { |
| 190 result.append(kBinariesPathName); |
| 191 } else { |
| 192 result.append(kProductPathName); |
| 193 } |
| 194 return result; |
| 195 } |
| 196 |
| 197 registry_util::RegistryOverrideManager override_manager_; |
| 198 HKEY root_key_ = nullptr; |
| 199 nt::ROOT_KEY nt_root_key_ = nt::AUTO; |
| 200 const InstallConstants* mode_ = nullptr; |
| 201 bool system_level_ = false; |
| 202 bool multi_install_ = false; |
| 203 |
| 204 DISALLOW_COPY_AND_ASSIGN(InstallStaticUtilTest); |
| 205 }; |
| 206 |
| 207 TEST_P(InstallStaticUtilTest, UsageStatsAbsent) { |
| 208 EXPECT_FALSE(GetCollectStatsConsent()); |
| 209 } |
| 210 |
| 211 TEST_P(InstallStaticUtilTest, UsageStatsZero) { |
| 212 SetUsageStat(0, false); |
| 213 EXPECT_FALSE(GetCollectStatsConsent()); |
| 214 } |
| 215 |
| 216 TEST_P(InstallStaticUtilTest, UsageStatsZeroMedium) { |
| 217 if (!system_level()) |
| 218 return; |
| 219 SetUsageStat(0, true); |
| 220 EXPECT_FALSE(GetCollectStatsConsent()); |
| 221 } |
| 222 |
| 223 TEST_P(InstallStaticUtilTest, UsageStatsOne) { |
| 224 SetUsageStat(1, false); |
| 225 EXPECT_TRUE(GetCollectStatsConsent()); |
| 226 } |
| 227 |
| 228 TEST_P(InstallStaticUtilTest, UsageStatsOneMedium) { |
| 229 if (!system_level()) |
| 230 return; |
| 231 SetUsageStat(1, true); |
| 232 EXPECT_TRUE(GetCollectStatsConsent()); |
| 233 } |
| 234 |
| 235 TEST_P(InstallStaticUtilTest, ReportingIsEnforcedByPolicy) { |
| 236 bool reporting_enabled = false; |
| 237 EXPECT_FALSE(ReportingIsEnforcedByPolicy(&reporting_enabled)); |
| 238 |
| 239 SetMetricsReportingPolicy(0); |
| 240 EXPECT_TRUE(ReportingIsEnforcedByPolicy(&reporting_enabled)); |
| 241 EXPECT_FALSE(reporting_enabled); |
| 242 |
| 243 SetMetricsReportingPolicy(1); |
| 244 EXPECT_TRUE(ReportingIsEnforcedByPolicy(&reporting_enabled)); |
| 245 EXPECT_TRUE(reporting_enabled); |
| 246 } |
| 247 |
| 248 TEST_P(InstallStaticUtilTest, UsageStatsPolicy) { |
| 249 // Policy alone. |
| 250 SetMetricsReportingPolicy(0); |
| 251 EXPECT_FALSE(GetCollectStatsConsent()); |
| 252 |
| 253 SetMetricsReportingPolicy(1); |
| 254 EXPECT_TRUE(GetCollectStatsConsent()); |
| 255 |
| 256 // Policy trumps usagestats. |
| 257 SetMetricsReportingPolicy(1); |
| 258 SetUsageStat(0, false); |
| 259 EXPECT_TRUE(GetCollectStatsConsent()); |
| 260 |
| 261 SetMetricsReportingPolicy(0); |
| 262 SetUsageStat(1, false); |
| 263 EXPECT_FALSE(GetCollectStatsConsent()); |
| 264 } |
| 265 |
| 266 TEST_P(InstallStaticUtilTest, GetChromeChannelName) { |
| 267 EXPECT_EQ(default_channel(), GetChromeChannelName(false)); |
| 268 std::wstring expected = default_channel(); |
| 269 if (multi_install()) { |
| 270 if (expected.empty()) |
| 271 expected = L"m"; |
| 272 else |
| 273 expected += L"-m"; |
| 274 } |
| 275 EXPECT_EQ(expected, GetChromeChannelName(true)); |
| 276 } |
| 277 |
| 278 TEST_P(InstallStaticUtilTest, GetDefaultUserDataDirectory) { |
| 279 std::wstring user_data_directory; |
| 280 ASSERT_TRUE(GetDefaultUserDataDirectory(&user_data_directory)); |
| 281 } |
| 282 |
| 283 #if defined(GOOGLE_CHROME_BUILD) |
| 284 // Stable supports multi-install at user and system levels. |
| 285 INSTANTIATE_TEST_CASE_P(Stable, |
| 286 InstallStaticUtilTest, |
| 287 testing::Combine(testing::Values(STABLE_INDEX), |
| 288 testing::Values("user", "system"), |
| 289 testing::Values("single", "multi"))); |
| 290 // Canary is single-only at user level. |
| 291 INSTANTIATE_TEST_CASE_P(Canary, |
| 292 InstallStaticUtilTest, |
| 293 testing::Combine(testing::Values(CANARY_INDEX), |
| 294 testing::Values("user"), |
| 295 testing::Values("single"))); |
| 296 #else // GOOGLE_CHROME_BUILD |
| 297 // Chromium supports multi-install at user and system levels. |
| 298 INSTANTIATE_TEST_CASE_P(Chromium, |
| 299 InstallStaticUtilTest, |
| 300 testing::Combine(testing::Values(CHROMIUM_INDEX), |
| 301 testing::Values("user", "system"), |
| 302 testing::Values("single", "multi"))); |
| 303 #endif // !GOOGLE_CHROME_BUILD |
| 304 |
197 } // namespace install_static | 305 } // namespace install_static |
OLD | NEW |