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