| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/common/crash_keys.h" | 5 #include "components/crash/core/common/crash_keys.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | |
| 9 #include <string> | 8 #include <string> |
| 10 | 9 |
| 11 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 12 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 13 #include "base/debug/crash_logging.h" | 12 #include "base/debug/crash_logging.h" |
| 14 #include "base/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
| 15 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 16 |
| 18 class CrashKeysTest : public testing::Test { | 17 class CrashKeysTest : public testing::Test { |
| 19 public: | 18 public: |
| 20 void SetUp() override { | 19 void SetUp() override { |
| 21 self_ = this; | 20 self_ = this; |
| 22 base::debug::SetCrashKeyReportingFunctions( | 21 base::debug::SetCrashKeyReportingFunctions( |
| 23 &SetCrashKeyValue, &ClearCrashKey); | 22 &SetCrashKeyValue, &ClearCrashKey); |
| 24 crash_keys::RegisterChromeCrashKeys(); | 23 |
| 24 std::vector<base::debug::CrashKey> keys; |
| 25 crash_keys::GetCrashKeysForCommandLineSwitches(&keys); |
| 26 base::debug::InitCrashKeys(keys.data(), keys.size(), |
| 27 crash_keys::kChunkMaxLength); |
| 25 } | 28 } |
| 26 | 29 |
| 27 void TearDown() override { | 30 void TearDown() override { |
| 28 base::debug::ResetCrashLoggingForTesting(); | 31 base::debug::ResetCrashLoggingForTesting(); |
| 29 self_ = NULL; | 32 self_ = NULL; |
| 30 } | 33 } |
| 31 | 34 |
| 32 bool HasCrashKey(const std::string& key) { | 35 bool HasCrashKey(const std::string& key) { |
| 33 return keys_.find(key) != keys_.end(); | 36 return keys_.find(key) != keys_.end(); |
| 34 } | 37 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 56 }; | 59 }; |
| 57 | 60 |
| 58 CrashKeysTest* CrashKeysTest::self_ = NULL; | 61 CrashKeysTest* CrashKeysTest::self_ = NULL; |
| 59 | 62 |
| 60 TEST_F(CrashKeysTest, Switches) { | 63 TEST_F(CrashKeysTest, Switches) { |
| 61 // Set three switches. | 64 // Set three switches. |
| 62 { | 65 { |
| 63 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | 66 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 64 for (int i = 1; i <= 3; ++i) | 67 for (int i = 1; i <= 3; ++i) |
| 65 command_line.AppendSwitch(base::StringPrintf("--flag-%d", i)); | 68 command_line.AppendSwitch(base::StringPrintf("--flag-%d", i)); |
| 66 crash_keys::SetSwitchesFromCommandLine(&command_line); | 69 crash_keys::SetSwitchesFromCommandLine(command_line); |
| 67 EXPECT_EQ("--flag-1", GetKeyValue("switch-1")); | 70 EXPECT_EQ("--flag-1", GetKeyValue("switch-1")); |
| 68 EXPECT_EQ("--flag-2", GetKeyValue("switch-2")); | 71 EXPECT_EQ("--flag-2", GetKeyValue("switch-2")); |
| 69 EXPECT_EQ("--flag-3", GetKeyValue("switch-3")); | 72 EXPECT_EQ("--flag-3", GetKeyValue("switch-3")); |
| 70 EXPECT_FALSE(HasCrashKey("switch-4")); | 73 EXPECT_FALSE(HasCrashKey("switch-4")); |
| 71 } | 74 } |
| 72 | 75 |
| 73 // Set more than the max switches. | 76 // Set more than the max switches. |
| 74 { | 77 { |
| 75 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | 78 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 76 const int kMax = crash_keys::kSwitchesMaxCount + 2; | 79 const int kMax = crash_keys::kSwitchesMaxCount + 2; |
| 77 EXPECT_GT(kMax, 15); | 80 EXPECT_GT(kMax, 15); |
| 78 for (int i = 1; i <= kMax; ++i) | 81 for (int i = 1; i <= kMax; ++i) |
| 79 command_line.AppendSwitch(base::StringPrintf("--many-%d", i)); | 82 command_line.AppendSwitch(base::StringPrintf("--many-%d", i)); |
| 80 crash_keys::SetSwitchesFromCommandLine(&command_line); | 83 crash_keys::SetSwitchesFromCommandLine(command_line); |
| 81 EXPECT_EQ("--many-1", GetKeyValue("switch-1")); | 84 EXPECT_EQ("--many-1", GetKeyValue("switch-1")); |
| 82 EXPECT_EQ("--many-9", GetKeyValue("switch-9")); | 85 EXPECT_EQ("--many-9", GetKeyValue("switch-9")); |
| 83 EXPECT_EQ("--many-15", GetKeyValue("switch-15")); | 86 EXPECT_EQ("--many-15", GetKeyValue("switch-15")); |
| 84 EXPECT_FALSE(HasCrashKey("switch-16")); | 87 EXPECT_FALSE(HasCrashKey("switch-16")); |
| 85 EXPECT_FALSE(HasCrashKey("switch-17")); | 88 EXPECT_FALSE(HasCrashKey("switch-17")); |
| 86 } | 89 } |
| 87 | 90 |
| 88 // Set fewer to ensure that old ones are erased. | 91 // Set fewer to ensure that old ones are erased. |
| 89 { | 92 { |
| 90 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | 93 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 91 for (int i = 1; i <= 5; ++i) | 94 for (int i = 1; i <= 5; ++i) |
| 92 command_line.AppendSwitch(base::StringPrintf("--fewer-%d", i)); | 95 command_line.AppendSwitch(base::StringPrintf("--fewer-%d", i)); |
| 93 crash_keys::SetSwitchesFromCommandLine(&command_line); | 96 crash_keys::SetSwitchesFromCommandLine(command_line); |
| 94 EXPECT_EQ("--fewer-1", GetKeyValue("switch-1")); | 97 EXPECT_EQ("--fewer-1", GetKeyValue("switch-1")); |
| 95 EXPECT_EQ("--fewer-2", GetKeyValue("switch-2")); | 98 EXPECT_EQ("--fewer-2", GetKeyValue("switch-2")); |
| 96 EXPECT_EQ("--fewer-3", GetKeyValue("switch-3")); | 99 EXPECT_EQ("--fewer-3", GetKeyValue("switch-3")); |
| 97 EXPECT_EQ("--fewer-4", GetKeyValue("switch-4")); | 100 EXPECT_EQ("--fewer-4", GetKeyValue("switch-4")); |
| 98 EXPECT_EQ("--fewer-5", GetKeyValue("switch-5")); | 101 EXPECT_EQ("--fewer-5", GetKeyValue("switch-5")); |
| 99 for (int i = 6; i < 20; ++i) | 102 for (int i = 6; i < 20; ++i) |
| 100 EXPECT_FALSE(HasCrashKey(base::StringPrintf(crash_keys::kSwitch, i))); | 103 EXPECT_FALSE(HasCrashKey(base::StringPrintf(crash_keys::kSwitchFormat, |
| 104 i))); |
| 101 } | 105 } |
| 102 } | 106 } |
| 103 | 107 |
| 104 TEST_F(CrashKeysTest, Extensions) { | 108 namespace { |
| 105 // Set three extensions. | |
| 106 { | |
| 107 std::set<std::string> extensions; | |
| 108 extensions.insert("ext.1"); | |
| 109 extensions.insert("ext.2"); | |
| 110 extensions.insert("ext.3"); | |
| 111 | 109 |
| 112 crash_keys::SetActiveExtensions(extensions); | 110 bool IsBoringFlag(const std::string& flag) { |
| 111 return flag.compare("--boring") == 0; |
| 112 } |
| 113 | 113 |
| 114 extensions.erase(GetKeyValue("extension-1")); | 114 } // namespace |
| 115 extensions.erase(GetKeyValue("extension-2")); | |
| 116 extensions.erase(GetKeyValue("extension-3")); | |
| 117 EXPECT_EQ(0u, extensions.size()); | |
| 118 | 115 |
| 119 EXPECT_EQ("3", GetKeyValue("num-extensions")); | 116 TEST_F(CrashKeysTest, FilterFlags) { |
| 120 EXPECT_FALSE(HasCrashKey("extension-4")); | 117 using crash_keys::kSwitchesMaxCount; |
| 121 } | |
| 122 | 118 |
| 123 // Set more than the max switches. | 119 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 124 { | 120 command_line.AppendSwitch("--not-boring-1"); |
| 125 std::set<std::string> extensions; | 121 command_line.AppendSwitch("--boring"); |
| 126 const int kMax = crash_keys::kExtensionIDMaxCount + 2; | |
| 127 EXPECT_GT(kMax, 10); | |
| 128 for (int i = 1; i <= kMax; ++i) | |
| 129 extensions.insert(base::StringPrintf("ext.%d", i)); | |
| 130 crash_keys::SetActiveExtensions(extensions); | |
| 131 | 122 |
| 132 for (int i = 1; i <= kMax; ++i) { | 123 // Include the max number of non-boring switches, to make sure that only the |
| 133 extensions.erase( | 124 // switches actually included in the crash keys are counted. |
| 134 GetKeyValue(base::StringPrintf(crash_keys::kExtensionID, i))); | 125 for (int i = 2; i <= kSwitchesMaxCount; ++i) |
| 135 } | 126 command_line.AppendSwitch(base::StringPrintf("--not-boring-%d", i)); |
| 136 EXPECT_EQ(2u, extensions.size()); | |
| 137 | 127 |
| 138 EXPECT_EQ("12", GetKeyValue("num-extensions")); | 128 crash_keys::SetSwitchesFromCommandLine(command_line, &IsBoringFlag); |
| 139 EXPECT_FALSE(HasCrashKey("extension-13")); | |
| 140 EXPECT_FALSE(HasCrashKey("extension-14")); | |
| 141 } | |
| 142 | 129 |
| 143 // Set fewer to ensure that old ones are erased. | 130 // If the boring keys are filtered out, every single key should now be |
| 144 { | 131 // not-boring. |
| 145 std::set<std::string> extensions; | 132 for (int i = 1; i <= kSwitchesMaxCount; ++i) { |
| 146 for (int i = 1; i <= 5; ++i) | 133 std::string switch_name = base::StringPrintf(crash_keys::kSwitchFormat, i); |
| 147 extensions.insert(base::StringPrintf("ext.%d", i)); | 134 std::string switch_value = base::StringPrintf("--not-boring-%d", i); |
| 148 crash_keys::SetActiveExtensions(extensions); | 135 EXPECT_EQ(switch_value, GetKeyValue(switch_name)) << "switch_name is " << |
| 149 | 136 switch_name; |
| 150 extensions.erase(GetKeyValue("extension-1")); | |
| 151 extensions.erase(GetKeyValue("extension-2")); | |
| 152 extensions.erase(GetKeyValue("extension-3")); | |
| 153 extensions.erase(GetKeyValue("extension-4")); | |
| 154 extensions.erase(GetKeyValue("extension-5")); | |
| 155 EXPECT_EQ(0u, extensions.size()); | |
| 156 | |
| 157 EXPECT_EQ("5", GetKeyValue("num-extensions")); | |
| 158 for (int i = 6; i < 20; ++i) { | |
| 159 std::string key = base::StringPrintf(crash_keys::kExtensionID, i); | |
| 160 EXPECT_FALSE(HasCrashKey(key)) << key; | |
| 161 } | |
| 162 } | 137 } |
| 163 } | 138 } |
| 164 | |
| 165 #if defined(OS_CHROMEOS) | |
| 166 TEST_F(CrashKeysTest, IgnoreBoringFlags) { | |
| 167 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | |
| 168 command_line.AppendSwitch("--enable-logging"); | |
| 169 command_line.AppendSwitch("--user-data-dir=/tmp"); | |
| 170 command_line.AppendSwitch("--v=1"); | |
| 171 command_line.AppendSwitch("--default-wallpaper-small=test.png"); | |
| 172 | |
| 173 command_line.AppendSwitch("--vv=1"); | |
| 174 command_line.AppendSwitch("--vvv"); | |
| 175 command_line.AppendSwitch("--enable-multi-profiles"); | |
| 176 command_line.AppendSwitch("--device-management-url=https://foo/bar"); | |
| 177 | |
| 178 crash_keys::SetSwitchesFromCommandLine(&command_line); | |
| 179 | |
| 180 EXPECT_EQ("--vv=1", GetKeyValue("switch-1")); | |
| 181 EXPECT_EQ("--vvv", GetKeyValue("switch-2")); | |
| 182 EXPECT_EQ("--enable-multi-profiles", GetKeyValue("switch-3")); | |
| 183 EXPECT_EQ("--device-management-url=https://foo/bar", GetKeyValue("switch-4")); | |
| 184 EXPECT_FALSE(HasCrashKey("switch-5")); | |
| 185 } | |
| 186 #endif | |
| OLD | NEW |