OLD | NEW |
(Empty) | |
| 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 "components/crash/core/common/crash_keys.h" |
| 6 |
| 7 #include <map> |
| 8 #include <string> |
| 9 |
| 10 #include "base/command_line.h" |
| 11 #include "base/compiler_specific.h" |
| 12 #include "base/debug/crash_logging.h" |
| 13 #include "base/format_macros.h" |
| 14 #include "base/strings/string_piece.h" |
| 15 #include "base/strings/stringprintf.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 class CrashKeysTest : public testing::Test { |
| 19 public: |
| 20 void SetUp() override { |
| 21 self_ = this; |
| 22 base::debug::SetCrashKeyReportingFunctions( |
| 23 &SetCrashKeyValue, &ClearCrashKey); |
| 24 |
| 25 std::vector<base::debug::CrashKey> keys; |
| 26 crash_keys::GetCrashKeysForCommandLineSwitches(&keys); |
| 27 base::debug::InitCrashKeys(keys.data(), keys.size(), |
| 28 crash_keys::kChunkMaxLength); |
| 29 ASSERT_FALSE(keys.empty()); |
| 30 } |
| 31 |
| 32 void TearDown() override { |
| 33 base::debug::ResetCrashLoggingForTesting(); |
| 34 self_ = NULL; |
| 35 } |
| 36 |
| 37 bool HasCrashKey(const std::string& key) { |
| 38 return keys_.find(key) != keys_.end(); |
| 39 } |
| 40 |
| 41 std::string GetKeyValue(const std::string& key) { |
| 42 std::map<std::string, std::string>::const_iterator it = keys_.find(key); |
| 43 if (it == keys_.end()) |
| 44 return std::string(); |
| 45 return it->second; |
| 46 } |
| 47 |
| 48 private: |
| 49 static void SetCrashKeyValue(const base::StringPiece& key, |
| 50 const base::StringPiece& value) { |
| 51 self_->keys_[key.as_string()] = value.as_string(); |
| 52 } |
| 53 |
| 54 static void ClearCrashKey(const base::StringPiece& key) { |
| 55 self_->keys_.erase(key.as_string()); |
| 56 } |
| 57 |
| 58 static CrashKeysTest* self_; |
| 59 |
| 60 std::map<std::string, std::string> keys_; |
| 61 }; |
| 62 |
| 63 CrashKeysTest* CrashKeysTest::self_ = NULL; |
| 64 |
| 65 TEST_F(CrashKeysTest, Switches) { |
| 66 // Set three switches. |
| 67 { |
| 68 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 69 for (size_t i = 1; i <= 3; ++i) |
| 70 command_line.AppendSwitch(base::StringPrintf("--flag-%" PRIuS, i)); |
| 71 crash_keys::SetSwitchesFromCommandLine(command_line, nullptr); |
| 72 EXPECT_EQ("--flag-1", GetKeyValue("switch-1")); |
| 73 EXPECT_EQ("--flag-2", GetKeyValue("switch-2")); |
| 74 EXPECT_EQ("--flag-3", GetKeyValue("switch-3")); |
| 75 EXPECT_FALSE(HasCrashKey("switch-4")); |
| 76 } |
| 77 |
| 78 // Set more than the max switches. |
| 79 { |
| 80 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 81 const size_t kMax = crash_keys::kSwitchesMaxCount + 2; |
| 82 EXPECT_GT(kMax, static_cast<size_t>(15)); |
| 83 for (size_t i = 1; i <= kMax; ++i) |
| 84 command_line.AppendSwitch(base::StringPrintf("--many-%" PRIuS, i)); |
| 85 crash_keys::SetSwitchesFromCommandLine(command_line, nullptr); |
| 86 EXPECT_EQ("--many-1", GetKeyValue("switch-1")); |
| 87 EXPECT_EQ("--many-9", GetKeyValue("switch-9")); |
| 88 EXPECT_EQ("--many-15", GetKeyValue("switch-15")); |
| 89 EXPECT_FALSE(HasCrashKey("switch-16")); |
| 90 EXPECT_FALSE(HasCrashKey("switch-17")); |
| 91 } |
| 92 |
| 93 // Set fewer to ensure that old ones are erased. |
| 94 { |
| 95 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 96 for (size_t i = 1; i <= 5; ++i) |
| 97 command_line.AppendSwitch(base::StringPrintf("--fewer-%" PRIuS, i)); |
| 98 crash_keys::SetSwitchesFromCommandLine(command_line, nullptr); |
| 99 EXPECT_EQ("--fewer-1", GetKeyValue("switch-1")); |
| 100 EXPECT_EQ("--fewer-2", GetKeyValue("switch-2")); |
| 101 EXPECT_EQ("--fewer-3", GetKeyValue("switch-3")); |
| 102 EXPECT_EQ("--fewer-4", GetKeyValue("switch-4")); |
| 103 EXPECT_EQ("--fewer-5", GetKeyValue("switch-5")); |
| 104 for (size_t i = 6; i < 20; ++i) |
| 105 EXPECT_FALSE(HasCrashKey(base::StringPrintf(crash_keys::kSwitchFormat, |
| 106 i))); |
| 107 } |
| 108 } |
| 109 |
| 110 namespace { |
| 111 |
| 112 bool IsBoringFlag(const std::string& flag) { |
| 113 return flag.compare("--boring") == 0; |
| 114 } |
| 115 |
| 116 } // namespace |
| 117 |
| 118 TEST_F(CrashKeysTest, FilterFlags) { |
| 119 using crash_keys::kSwitchesMaxCount; |
| 120 |
| 121 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 122 command_line.AppendSwitch("--not-boring-1"); |
| 123 command_line.AppendSwitch("--boring"); |
| 124 |
| 125 // Include the max number of non-boring switches, to make sure that only the |
| 126 // switches actually included in the crash keys are counted. |
| 127 for (size_t i = 2; i <= kSwitchesMaxCount; ++i) |
| 128 command_line.AppendSwitch(base::StringPrintf("--not-boring-%" PRIuS, i)); |
| 129 |
| 130 crash_keys::SetSwitchesFromCommandLine(command_line, &IsBoringFlag); |
| 131 |
| 132 // If the boring keys are filtered out, every single key should now be |
| 133 // not-boring. |
| 134 for (size_t i = 1; i <= kSwitchesMaxCount; ++i) { |
| 135 std::string switch_name = base::StringPrintf(crash_keys::kSwitchFormat, i); |
| 136 std::string switch_value = base::StringPrintf("--not-boring-%" PRIuS, i); |
| 137 EXPECT_EQ(switch_value, GetKeyValue(switch_name)) << "switch_name is " << |
| 138 switch_name; |
| 139 } |
| 140 } |
OLD | NEW |