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.
5 #include "chrome/common/crash_keys.h"
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"
17 class CrashKeysTest
: public testing::Test
{
19 virtual void SetUp() OVERRIDE
{
21 base::debug::SetCrashKeyReportingFunctions(
22 &SetCrashKeyValue
, &ClearCrashKey
);
23 crash_keys::RegisterChromeCrashKeys();
26 virtual void TearDown() OVERRIDE
{
27 base::debug::ResetCrashLoggingForTesting();
31 bool HasCrashKey(const std::string
& key
) {
32 return keys_
.find(key
) != keys_
.end();
35 std::string
GetKeyValue(const std::string
& key
) {
40 static void SetCrashKeyValue(const base::StringPiece
& key
,
41 const base::StringPiece
& value
) {
42 self_
->keys_
[key
.as_string()] = value
.as_string();
45 static void ClearCrashKey(const base::StringPiece
& key
) {
46 self_
->keys_
.erase(key
.as_string());
49 static CrashKeysTest
* self_
;
51 std::map
<std::string
, std::string
> keys_
;
54 CrashKeysTest
* CrashKeysTest::self_
= NULL
;
56 TEST_F(CrashKeysTest
, Switches
) {
57 // Set three switches.
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"));
69 // Set more than the max switches.
71 CommandLine
command_line(CommandLine::NO_PROGRAM
);
72 const int kMax
= crash_keys::kSwitchesMaxCount
+ 2;
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"));
84 // Set fewer to ensure that old ones are erased.
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
)));