crash_keys_unittest.cc 9.6 KB

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