[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 1 | // Copyright 2013 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 "chrome/common/crash_keys.h" |
| 6 | |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 7 | #include <set> |
[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 8 | #include <string> |
| 9 | |
| 10 | #include "base/command_line.h" |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 11 | #include "base/strings/strcat.h" |
[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 12 | #include "base/strings/stringprintf.h" |
avi | 2729e44 | 2015-12-26 05:27:45 | [diff] [blame] | 13 | #include "build/build_config.h" |
Ian Barkley-Yeung | dcd6c07 | 2021-07-23 17:53:22 | [diff] [blame] | 14 | #include "build/buildflag.h" |
Yuta Hijikata | 2bac128 | 2020-11-24 03:51:27 | [diff] [blame] | 15 | #include "build/chromeos_buildflags.h" |
Robert Sesek | d89a0620 | 2017-11-20 22:54:05 | [diff] [blame] | 16 | #include "components/crash/core/common/crash_key.h" |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 17 | #include "gpu/config/gpu_switches.h" |
| 18 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
Robert Sesek | abcdd84 | 2017-12-19 02:34:14 | [diff] [blame] | 21 | using crash_reporter::GetCrashKeyValue; |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 22 | using ::testing::IsEmpty; |
Robert Sesek | abcdd84 | 2017-12-19 02:34:14 | [diff] [blame] | 23 | |
[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 24 | class CrashKeysTest : public testing::Test { |
| 25 | public: |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 26 | void SetUp() override { crash_reporter::InitializeCrashKeys(); } |
[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 27 | |
dcheng | d002275 | 2014-10-28 00:09:06 | [diff] [blame] | 28 | void TearDown() override { |
Ian Barkley-Yeung | dcd6c07 | 2021-07-23 17:53:22 | [diff] [blame] | 29 | // Breakpad doesn't properly support ResetCrashKeysForTesting() and usually |
| 30 | // CHECK fails after it is called. |
| 31 | #if BUILDFLAG(USE_CRASHPAD_ANNOTATION) |
| 32 | crash_reporter::ResetCrashKeysForTesting(); |
| 33 | #endif |
[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 34 | } |
[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 35 | }; |
| 36 | |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 37 | TEST_F(CrashKeysTest, Extensions) { |
| 38 | // Set three extensions. |
| 39 | { |
| 40 | std::set<std::string> extensions; |
| 41 | extensions.insert("ext.1"); |
| 42 | extensions.insert("ext.2"); |
| 43 | extensions.insert("ext.3"); |
| 44 | |
| 45 | crash_keys::SetActiveExtensions(extensions); |
| 46 | |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 47 | extensions.erase(GetCrashKeyValue("extension-1")); |
| 48 | extensions.erase(GetCrashKeyValue("extension-2")); |
| 49 | extensions.erase(GetCrashKeyValue("extension-3")); |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 50 | EXPECT_EQ(0u, extensions.size()); |
| 51 | |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 52 | EXPECT_EQ("3", GetCrashKeyValue("num-extensions")); |
| 53 | EXPECT_TRUE(GetCrashKeyValue("extension-4").empty()); |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Set more than the max switches. |
| 57 | { |
| 58 | std::set<std::string> extensions; |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 59 | const int kMax = 12; |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 60 | for (int i = 1; i <= kMax; ++i) |
| 61 | extensions.insert(base::StringPrintf("ext.%d", i)); |
| 62 | crash_keys::SetActiveExtensions(extensions); |
| 63 | |
| 64 | for (int i = 1; i <= kMax; ++i) { |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 65 | extensions.erase(GetCrashKeyValue(base::StringPrintf("extension-%d", i))); |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 66 | } |
| 67 | EXPECT_EQ(2u, extensions.size()); |
| 68 | |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 69 | EXPECT_EQ("12", GetCrashKeyValue("num-extensions")); |
| 70 | EXPECT_TRUE(GetCrashKeyValue("extension-13").empty()); |
| 71 | EXPECT_TRUE(GetCrashKeyValue("extension-14").empty()); |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Set fewer to ensure that old ones are erased. |
| 75 | { |
| 76 | std::set<std::string> extensions; |
| 77 | for (int i = 1; i <= 5; ++i) |
| 78 | extensions.insert(base::StringPrintf("ext.%d", i)); |
| 79 | crash_keys::SetActiveExtensions(extensions); |
| 80 | |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 81 | extensions.erase(GetCrashKeyValue("extension-1")); |
| 82 | extensions.erase(GetCrashKeyValue("extension-2")); |
| 83 | extensions.erase(GetCrashKeyValue("extension-3")); |
| 84 | extensions.erase(GetCrashKeyValue("extension-4")); |
| 85 | extensions.erase(GetCrashKeyValue("extension-5")); |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 86 | EXPECT_EQ(0u, extensions.size()); |
| 87 | |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 88 | EXPECT_EQ("5", GetCrashKeyValue("num-extensions")); |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 89 | for (int i = 6; i < 20; ++i) { |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 90 | std::string key = base::StringPrintf("extension-%d", i); |
| 91 | EXPECT_TRUE(GetCrashKeyValue(key).empty()) << key; |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 92 | } |
[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 93 | } |
| 94 | } |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 95 | |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 96 | TEST_F(CrashKeysTest, ShouldIgnoreBoringFlags) { |
avi | 79bf913 | 2014-12-25 17:48:05 | [diff] [blame] | 97 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 98 | std::string expected_num_switches = "7"; |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 99 | command_line.AppendSwitch("--enable-logging"); |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 100 | command_line.AppendSwitch("--v=1"); |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 101 | |
| 102 | command_line.AppendSwitch("--vv=1"); |
| 103 | command_line.AppendSwitch("--vvv"); |
| 104 | command_line.AppendSwitch("--enable-multi-profiles"); |
| 105 | command_line.AppendSwitch("--device-management-url=https://foo/bar"); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 106 | command_line.AppendSwitch( |
| 107 | base::StrCat({"--", switches::kGpuPreferences, "=ABC123"})); |
Yuta Hijikata | 2bac128 | 2020-11-24 03:51:27 | [diff] [blame] | 108 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
joenotcharles | 0edd36d | 2015-12-01 16:57:32 | [diff] [blame] | 109 | command_line.AppendSwitch("--user-data-dir=/tmp"); |
| 110 | command_line.AppendSwitch("--default-wallpaper-small=test.png"); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 111 | expected_num_switches = "9"; |
joenotcharles | 0edd36d | 2015-12-01 16:57:32 | [diff] [blame] | 112 | #endif |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 113 | |
joenotcharles | 0edd36d | 2015-12-01 16:57:32 | [diff] [blame] | 114 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 115 | |
Robert Sesek | abcdd84 | 2017-12-19 02:34:14 | [diff] [blame] | 116 | EXPECT_EQ("--vv=1", GetCrashKeyValue("switch-1")); |
| 117 | EXPECT_EQ("--vvv", GetCrashKeyValue("switch-2")); |
| 118 | EXPECT_EQ("--enable-multi-profiles", GetCrashKeyValue("switch-3")); |
| 119 | EXPECT_EQ("--device-management-url=https://foo/bar", |
| 120 | GetCrashKeyValue("switch-4")); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 121 | EXPECT_EQ(expected_num_switches, GetCrashKeyValue("num-switches")); |
Robert Sesek | abcdd84 | 2017-12-19 02:34:14 | [diff] [blame] | 122 | EXPECT_TRUE(GetCrashKeyValue("switch-5").empty()); |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 123 | } |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 124 | |
Xiaohan Wang | 4d5c504 | 2022-01-18 21:54:37 | [diff] [blame] | 125 | #if BUILDFLAG(IS_CHROMEOS) |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 126 | TEST_F(CrashKeysTest, EnabledDisabledFeaturesFlags) { |
| 127 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 128 | command_line.InitFromArgv( |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 129 | {"program_name", "--example", "--enable-features=AEnabledFeatureFlag", |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 130 | "--unrelated-flag=23", "--disable-features=ADisabledFeaturesString", |
| 131 | "--more-example=yes"}); |
| 132 | |
| 133 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
| 134 | |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 135 | EXPECT_EQ("AEnabledFeatureFlag", |
| 136 | GetCrashKeyValue("commandline-enabled-feature-1")); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 137 | EXPECT_EQ("ADisabledFeaturesString", |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 138 | GetCrashKeyValue("commandline-disabled-feature-1")); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 139 | |
| 140 | // Unrelated flags are not affected by the enable-features extraction. |
| 141 | EXPECT_EQ("--example", GetCrashKeyValue("switch-1")); |
| 142 | EXPECT_EQ("--unrelated-flag=23", GetCrashKeyValue("switch-2")); |
| 143 | EXPECT_EQ("--more-example=yes", GetCrashKeyValue("switch-3")); |
| 144 | // --enable-features and --disable-features are still counted in num-switches. |
| 145 | EXPECT_EQ("5", GetCrashKeyValue("num-switches")); |
| 146 | } |
| 147 | |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 148 | TEST_F(CrashKeysTest, ShouldCreateCrashKeyForEachEnabledFeature) { |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 149 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 150 | command_line.InitFromArgv({"program_name", "--example", |
| 151 | "--enable-features=FirstFeature,SecondFeature", |
| 152 | "--unrelated-flag=23", "--more-example=yes"}); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 153 | |
| 154 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
| 155 | |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 156 | EXPECT_EQ("FirstFeature", GetCrashKeyValue("commandline-enabled-feature-1")); |
| 157 | EXPECT_EQ("SecondFeature", GetCrashKeyValue("commandline-enabled-feature-2")); |
| 158 | |
| 159 | EXPECT_EQ(GetCrashKeyValue("commandline-enabled-feature-3"), ""); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 160 | } |
| 161 | |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 162 | TEST_F(CrashKeysTest, ShouldCreateCrashKeyForEachDisabledFeature) { |
| 163 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 164 | command_line.InitFromArgv({"program_name", "--example", |
| 165 | "--disable-features=FirstFeature,SecondFeature", |
| 166 | "--unrelated-flag=23", "--more-example=yes"}); |
| 167 | |
| 168 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
| 169 | |
| 170 | EXPECT_EQ("FirstFeature", GetCrashKeyValue("commandline-disabled-feature-1")); |
| 171 | EXPECT_EQ("SecondFeature", |
| 172 | GetCrashKeyValue("commandline-disabled-feature-2")); |
| 173 | |
| 174 | EXPECT_EQ(GetCrashKeyValue("commandline-disabled-feature-3"), ""); |
| 175 | } |
| 176 | |
| 177 | TEST_F(CrashKeysTest, |
| 178 | EnabledDisabledFeatures_LastCommandArgumentShouldBeRetained) { |
| 179 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 180 | command_line.InitFromArgv( |
| 181 | {"program_name", "--enable-features=FeatureEnabledInFirstArgument", |
| 182 | "--disable-features=FeatureDisabledInFirstArgument", |
| 183 | "--enable-features=FeatureEnabledInSecondArgument", |
| 184 | "--disable-features=FeatureDisabledInSecondArgument"}); |
| 185 | |
| 186 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
| 187 | |
| 188 | EXPECT_EQ("FeatureEnabledInSecondArgument", |
| 189 | GetCrashKeyValue("commandline-enabled-feature-1")); |
| 190 | EXPECT_EQ("FeatureDisabledInSecondArgument", |
| 191 | GetCrashKeyValue("commandline-disabled-feature-1")); |
| 192 | } |
| 193 | |
| 194 | TEST_F( |
| 195 | CrashKeysTest, |
| 196 | EnabledDisabledFeatures_ShouldClearPreviousCrashKeysIfCommandLineIsReparsed) { |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 197 | { |
| 198 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 199 | command_line.InitFromArgv( |
| 200 | {"program_name", "--enable-features=OriginalEnable_1, OriginalEnable_2", |
| 201 | "--disable-features=OriginalDisable_1, OriginalDisable_2"}); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 202 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 203 | |
| 204 | EXPECT_EQ("OriginalEnable_1", |
| 205 | GetCrashKeyValue("commandline-enabled-feature-1")); |
| 206 | EXPECT_EQ("OriginalEnable_2", |
| 207 | GetCrashKeyValue("commandline-enabled-feature-2")); |
| 208 | |
| 209 | EXPECT_EQ("OriginalDisable_1", |
| 210 | GetCrashKeyValue("commandline-disabled-feature-1")); |
| 211 | EXPECT_EQ("OriginalDisable_2", |
| 212 | GetCrashKeyValue("commandline-disabled-feature-2")); |
| 213 | } |
| 214 | |
| 215 | // Parse a command line with only a single value in each flag. |
| 216 | { |
| 217 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 218 | command_line.InitFromArgv({"program_name", "--enable-features=NewEnable", |
| 219 | "--disable-features=NewDisable"}); |
| 220 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
| 221 | |
| 222 | EXPECT_EQ("NewEnable", GetCrashKeyValue("commandline-enabled-feature-1")); |
| 223 | EXPECT_EQ(GetCrashKeyValue("commandline-enabled-feature-2"), ""); |
| 224 | |
| 225 | EXPECT_EQ("NewDisable", GetCrashKeyValue("commandline-disabled-feature-1")); |
| 226 | EXPECT_EQ(GetCrashKeyValue("commandline-disabled-feature-2"), ""); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // Parse a command line with no enable-features or disable-features flags. |
| 230 | { |
| 231 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 232 | command_line.InitFromArgv( |
| 233 | {"program_name", "--enable-logging", "--type=renderer"}); |
| 234 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 235 | EXPECT_EQ(GetCrashKeyValue("commandline-enabled-feature-1"), ""); |
| 236 | EXPECT_EQ(GetCrashKeyValue("commandline-enabled-feature-2"), ""); |
| 237 | EXPECT_EQ(GetCrashKeyValue("commandline-disabled-feature-1"), ""); |
| 238 | EXPECT_EQ(GetCrashKeyValue("commandline-disabled-feature-2"), ""); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Parse a new command line with enable-features or disable-features flags. |
| 242 | { |
| 243 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 244 | command_line.InitFromArgv({"program_name", "--enable-features=NewEnable", |
| 245 | "--disable-features=NewDisable"}); |
| 246 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
Jeroen Dhollander | b93a8d17 | 2022-05-10 12:13:13 | [diff] [blame] | 247 | EXPECT_EQ("NewEnable", GetCrashKeyValue("commandline-enabled-feature-1")); |
| 248 | EXPECT_EQ("NewDisable", GetCrashKeyValue("commandline-disabled-feature-1")); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Xiaohan Wang | 4d5c504 | 2022-01-18 21:54:37 | [diff] [blame] | 252 | #endif // BUILDFLAG(IS_CHROMEOS) |