OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 |
| 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 virtual void SetUp() OVERRIDE { |
| 20 self_ = this; |
| 21 base::debug::SetCrashKeyReportingFunctions( |
| 22 &SetCrashKeyValue, &ClearCrashKey); |
| 23 crash_keys::RegisterChromeCrashKeys(); |
| 24 } |
| 25 |
| 26 virtual void TearDown() OVERRIDE { |
| 27 base::debug::ResetCrashLoggingForTesting(); |
| 28 self_ = NULL; |
| 29 } |
| 30 |
| 31 bool HasCrashKey(const std::string& key) { |
| 32 return keys_.find(key) != keys_.end(); |
| 33 } |
| 34 |
| 35 std::string GetKeyValue(const std::string& key) { |
| 36 return keys_[key]; |
| 37 } |
| 38 |
| 39 private: |
| 40 static void SetCrashKeyValue(const base::StringPiece& key, |
| 41 const base::StringPiece& value) { |
| 42 self_->keys_[key.as_string()] = value.as_string(); |
| 43 } |
| 44 |
| 45 static void ClearCrashKey(const base::StringPiece& key) { |
| 46 self_->keys_.erase(key.as_string()); |
| 47 } |
| 48 |
| 49 static CrashKeysTest* self_; |
| 50 |
| 51 std::map<std::string, std::string> keys_; |
| 52 }; |
| 53 |
| 54 CrashKeysTest* CrashKeysTest::self_ = NULL; |
| 55 |
| 56 TEST_F(CrashKeysTest, Switches) { |
| 57 // Set three switches. |
| 58 { |
| 59 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 60 for (int i = 1; i <= 3; ++i) |
| 61 command_line.AppendSwitch(base::StringPrintf("--flag-%d", i)); |
| 62 crash_keys::SetSwitchesFromCommandLine(&command_line); |
| 63 EXPECT_EQ("--flag-1", GetKeyValue("switch-1")); |
| 64 EXPECT_EQ("--flag-2", GetKeyValue("switch-2")); |
| 65 EXPECT_EQ("--flag-3", GetKeyValue("switch-3")); |
| 66 EXPECT_FALSE(HasCrashKey("switch-4")); |
| 67 } |
| 68 |
| 69 // Set more than the max switches. |
| 70 { |
| 71 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 72 const int kMax = crash_keys::kSwitchesMaxCount + 2; |
| 73 EXPECT_GT(kMax, 15); |
| 74 for (int i = 1; i <= kMax; ++i) |
| 75 command_line.AppendSwitch(base::StringPrintf("--many-%d", i)); |
| 76 crash_keys::SetSwitchesFromCommandLine(&command_line); |
| 77 EXPECT_EQ("--many-1", GetKeyValue("switch-1")); |
| 78 EXPECT_EQ("--many-9", GetKeyValue("switch-9")); |
| 79 EXPECT_EQ("--many-15", GetKeyValue("switch-15")); |
| 80 EXPECT_FALSE(HasCrashKey("switch-16")); |
| 81 EXPECT_FALSE(HasCrashKey("switch-17")); |
| 82 } |
| 83 |
| 84 // Set fewer to ensure that old ones are erased. |
| 85 { |
| 86 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 87 const char kFormat[] = "--fewer-%d"; |
| 88 for (int i = 1; i <= 5; ++i) |
| 89 command_line.AppendSwitch(base::StringPrintf(kFormat, i)); |
| 90 crash_keys::SetSwitchesFromCommandLine(&command_line); |
| 91 EXPECT_EQ("--fewer-1", GetKeyValue("switch-1")); |
| 92 EXPECT_EQ("--fewer-2", GetKeyValue("switch-2")); |
| 93 EXPECT_EQ("--fewer-3", GetKeyValue("switch-3")); |
| 94 EXPECT_EQ("--fewer-4", GetKeyValue("switch-4")); |
| 95 EXPECT_EQ("--fewer-5", GetKeyValue("switch-5")); |
| 96 for (int i = 6; i < 20; ++i) |
| 97 EXPECT_FALSE(HasCrashKey(base::StringPrintf(kFormat, i))); |
| 98 } |
| 99 } |
OLD | NEW |