[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: |
dcheng | d002275 | 2014-10-28 00:09:06 | [diff] [blame] | 26 | void SetUp() override { |
Robert Sesek | d89a0620 | 2017-11-20 22:54:05 | [diff] [blame] | 27 | crash_reporter::InitializeCrashKeys(); |
[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 28 | } |
| 29 | |
dcheng | d002275 | 2014-10-28 00:09:06 | [diff] [blame] | 30 | void TearDown() override { |
Ian Barkley-Yeung | dcd6c07 | 2021-07-23 17:53:22 | [diff] [blame] | 31 | // Breakpad doesn't properly support ResetCrashKeysForTesting() and usually |
| 32 | // CHECK fails after it is called. |
| 33 | #if BUILDFLAG(USE_CRASHPAD_ANNOTATION) |
| 34 | crash_reporter::ResetCrashKeysForTesting(); |
| 35 | #endif |
[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 36 | } |
[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 37 | }; |
| 38 | |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 39 | TEST_F(CrashKeysTest, Extensions) { |
| 40 | // Set three extensions. |
| 41 | { |
| 42 | std::set<std::string> extensions; |
| 43 | extensions.insert("ext.1"); |
| 44 | extensions.insert("ext.2"); |
| 45 | extensions.insert("ext.3"); |
| 46 | |
| 47 | crash_keys::SetActiveExtensions(extensions); |
| 48 | |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 49 | extensions.erase(GetCrashKeyValue("extension-1")); |
| 50 | extensions.erase(GetCrashKeyValue("extension-2")); |
| 51 | extensions.erase(GetCrashKeyValue("extension-3")); |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 52 | EXPECT_EQ(0u, extensions.size()); |
| 53 | |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 54 | EXPECT_EQ("3", GetCrashKeyValue("num-extensions")); |
| 55 | EXPECT_TRUE(GetCrashKeyValue("extension-4").empty()); |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Set more than the max switches. |
| 59 | { |
| 60 | std::set<std::string> extensions; |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 61 | const int kMax = 12; |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 62 | for (int i = 1; i <= kMax; ++i) |
| 63 | extensions.insert(base::StringPrintf("ext.%d", i)); |
| 64 | crash_keys::SetActiveExtensions(extensions); |
| 65 | |
| 66 | for (int i = 1; i <= kMax; ++i) { |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 67 | extensions.erase(GetCrashKeyValue(base::StringPrintf("extension-%d", i))); |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 68 | } |
| 69 | EXPECT_EQ(2u, extensions.size()); |
| 70 | |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 71 | EXPECT_EQ("12", GetCrashKeyValue("num-extensions")); |
| 72 | EXPECT_TRUE(GetCrashKeyValue("extension-13").empty()); |
| 73 | EXPECT_TRUE(GetCrashKeyValue("extension-14").empty()); |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // Set fewer to ensure that old ones are erased. |
| 77 | { |
| 78 | std::set<std::string> extensions; |
| 79 | for (int i = 1; i <= 5; ++i) |
| 80 | extensions.insert(base::StringPrintf("ext.%d", i)); |
| 81 | crash_keys::SetActiveExtensions(extensions); |
| 82 | |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 83 | extensions.erase(GetCrashKeyValue("extension-1")); |
| 84 | extensions.erase(GetCrashKeyValue("extension-2")); |
| 85 | extensions.erase(GetCrashKeyValue("extension-3")); |
| 86 | extensions.erase(GetCrashKeyValue("extension-4")); |
| 87 | extensions.erase(GetCrashKeyValue("extension-5")); |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 88 | EXPECT_EQ(0u, extensions.size()); |
| 89 | |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 90 | EXPECT_EQ("5", GetCrashKeyValue("num-extensions")); |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 91 | for (int i = 6; i < 20; ++i) { |
Robert Sesek | 0f93c58 | 2017-12-19 15:13:43 | [diff] [blame] | 92 | std::string key = base::StringPrintf("extension-%d", i); |
| 93 | EXPECT_TRUE(GetCrashKeyValue(key).empty()) << key; |
[email protected] | f9d5a61 | 2014-01-21 18:40:57 | [diff] [blame] | 94 | } |
[email protected] | b8255e7a | 2013-10-02 19:11:13 | [diff] [blame] | 95 | } |
| 96 | } |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 97 | |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 98 | TEST_F(CrashKeysTest, IgnoreBoringFlags) { |
avi | 79bf913 | 2014-12-25 17:48:05 | [diff] [blame] | 99 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 100 | std::string expected_num_switches = "7"; |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 101 | command_line.AppendSwitch("--enable-logging"); |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 102 | command_line.AppendSwitch("--v=1"); |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 103 | |
| 104 | command_line.AppendSwitch("--vv=1"); |
| 105 | command_line.AppendSwitch("--vvv"); |
| 106 | command_line.AppendSwitch("--enable-multi-profiles"); |
| 107 | command_line.AppendSwitch("--device-management-url=https://foo/bar"); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 108 | command_line.AppendSwitch( |
| 109 | base::StrCat({"--", switches::kGpuPreferences, "=ABC123"})); |
Yuta Hijikata | 2bac128 | 2020-11-24 03:51:27 | [diff] [blame] | 110 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
joenotcharles | 0edd36d | 2015-12-01 16:57:32 | [diff] [blame] | 111 | command_line.AppendSwitch("--user-data-dir=/tmp"); |
| 112 | command_line.AppendSwitch("--default-wallpaper-small=test.png"); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 113 | expected_num_switches = "9"; |
joenotcharles | 0edd36d | 2015-12-01 16:57:32 | [diff] [blame] | 114 | #endif |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 115 | |
joenotcharles | 0edd36d | 2015-12-01 16:57:32 | [diff] [blame] | 116 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 117 | |
Robert Sesek | abcdd84 | 2017-12-19 02:34:14 | [diff] [blame] | 118 | EXPECT_EQ("--vv=1", GetCrashKeyValue("switch-1")); |
| 119 | EXPECT_EQ("--vvv", GetCrashKeyValue("switch-2")); |
| 120 | EXPECT_EQ("--enable-multi-profiles", GetCrashKeyValue("switch-3")); |
| 121 | EXPECT_EQ("--device-management-url=https://foo/bar", |
| 122 | GetCrashKeyValue("switch-4")); |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 123 | EXPECT_EQ(expected_num_switches, GetCrashKeyValue("num-switches")); |
Robert Sesek | abcdd84 | 2017-12-19 02:34:14 | [diff] [blame] | 124 | EXPECT_TRUE(GetCrashKeyValue("switch-5").empty()); |
[email protected] | af7c024 | 2014-04-11 07:27:29 | [diff] [blame] | 125 | } |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 126 | |
Xiaohan Wang | 4d5c504 | 2022-01-18 21:54:37 | [diff] [blame^] | 127 | #if BUILDFLAG(IS_CHROMEOS) |
Ian Barkley-Yeung | f998741 | 2021-07-21 02:36:04 | [diff] [blame] | 128 | TEST_F(CrashKeysTest, EnabledDisabledFeaturesFlags) { |
| 129 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 130 | command_line.InitFromArgv( |
| 131 | {"program_name", "--example", |
| 132 | "--enable-features=LongString,WhichDoesn't,Fit,In64Characters,ButFitsIn," |
| 133 | "120Characters,SoWePutIt,InItsOwnCrashKey", |
| 134 | "--unrelated-flag=23", "--disable-features=ADisabledFeaturesString", |
| 135 | "--more-example=yes"}); |
| 136 | |
| 137 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
| 138 | |
| 139 | EXPECT_EQ( |
| 140 | "LongString,WhichDoesn't,Fit,In64Characters,ButFitsIn," |
| 141 | "120Characters,SoWePutIt,InItsOwnCrashKey", |
| 142 | GetCrashKeyValue("commandline-enabled-features")); |
| 143 | EXPECT_EQ("ADisabledFeaturesString", |
| 144 | GetCrashKeyValue("commandline-disabled-features")); |
| 145 | |
| 146 | // Unrelated flags are not affected by the enable-features extraction. |
| 147 | EXPECT_EQ("--example", GetCrashKeyValue("switch-1")); |
| 148 | EXPECT_EQ("--unrelated-flag=23", GetCrashKeyValue("switch-2")); |
| 149 | EXPECT_EQ("--more-example=yes", GetCrashKeyValue("switch-3")); |
| 150 | // --enable-features and --disable-features are still counted in num-switches. |
| 151 | EXPECT_EQ("5", GetCrashKeyValue("num-switches")); |
| 152 | } |
| 153 | |
| 154 | TEST_F(CrashKeysTest, EnabledDisabledFeatures_MultipleFlags) { |
| 155 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 156 | command_line.InitFromArgv({"program_name", |
| 157 | "--enable-features=FeatureOne,FeatureTwo", |
| 158 | "--disable-features=FeatureThree", |
| 159 | "--enable-features=FeatureFour,FeatureFive", |
| 160 | "--disable-features=FeatureSix,FeatureSeven"}); |
| 161 | |
| 162 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
| 163 | |
| 164 | EXPECT_EQ("FeatureFour,FeatureFive", |
| 165 | GetCrashKeyValue("commandline-enabled-features")); |
| 166 | EXPECT_EQ("FeatureSix,FeatureSeven", |
| 167 | GetCrashKeyValue("commandline-disabled-features")); |
| 168 | } |
| 169 | |
| 170 | TEST_F(CrashKeysTest, EnabledDisabledFeatures_MultipleParses) { |
| 171 | { |
| 172 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 173 | command_line.InitFromArgv({"program_name", |
| 174 | "--enable-features=OriginalEnable", |
| 175 | "--disable-features=OriginalDisable"}); |
| 176 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
| 177 | EXPECT_EQ("OriginalEnable", |
| 178 | GetCrashKeyValue("commandline-enabled-features")); |
| 179 | EXPECT_EQ("OriginalDisable", |
| 180 | GetCrashKeyValue("commandline-disabled-features")); |
| 181 | } |
| 182 | |
| 183 | // Parse a command line with no enable-features or disable-features flags. |
| 184 | { |
| 185 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 186 | command_line.InitFromArgv( |
| 187 | {"program_name", "--enable-logging", "--type=renderer"}); |
| 188 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
| 189 | EXPECT_EQ("", GetCrashKeyValue("commandline-enabled-features")); |
| 190 | EXPECT_EQ("", GetCrashKeyValue("commandline-disabled-features")); |
| 191 | } |
| 192 | |
| 193 | // Parse a new command line with enable-features or disable-features flags. |
| 194 | { |
| 195 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 196 | command_line.InitFromArgv({"program_name", "--enable-features=NewEnable", |
| 197 | "--disable-features=NewDisable"}); |
| 198 | crash_keys::SetCrashKeysFromCommandLine(command_line); |
| 199 | EXPECT_EQ("NewEnable", GetCrashKeyValue("commandline-enabled-features")); |
| 200 | EXPECT_EQ("NewDisable", GetCrashKeyValue("commandline-disabled-features")); |
| 201 | } |
| 202 | } |
| 203 | |
Xiaohan Wang | 4d5c504 | 2022-01-18 21:54:37 | [diff] [blame^] | 204 | #endif // BUILDFLAG(IS_CHROMEOS) |