Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: base/debug/crash_logging_unittest.cc

Issue 11761030: Create the crash key registration system and register some Mac-specific keys. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "base/debug/crash_logging.h"
6
7 #include <map>
8 #include <string>
9
10 #include "base/bind.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace {
14
15 std::map<std::string, std::string>* key_values_ = NULL;
16
17 } // namespace
18
19 class CrashLoggingTest : public testing::Test {
20 public:
21 virtual void SetUp() {
22 base::debug::SetCrashKeyReportingFunctions(
23 &CrashLoggingTest::SetKeyValue,
24 &CrashLoggingTest::ClearKeyValue);
25 key_values_ = new std::map<std::string, std::string>;
Scott Hess - ex-Googler 2013/01/09 22:34:20 Set this before setting the reporting functions, j
Robert Sesek 2013/01/09 23:06:30 Done.
26 }
27
28 virtual void TearDown() {
29 base::debug::ResetCrashLoggingForTesting();
30
31 delete key_values_;
32 key_values_ = NULL;
33 }
34
35 private:
36 static void SetKeyValue(const base::StringPiece& key,
37 const base::StringPiece& value) {
38 (*key_values_)[key.as_string()] = value.as_string();
39 }
40
41 static void ClearKeyValue(const base::StringPiece& key) {
42 key_values_->erase(key.as_string());
43 }
44 };
45
46 TEST_F(CrashLoggingTest, SetClearSingle) {
47 const char* kTestKey = "test-key";
48 base::debug::CrashKey keys[1] = { { kTestKey, 1, 255 } };
49 base::debug::InitCrashKeys(keys, 1, NULL);
50
51 base::debug::SetCrashKeyValue(kTestKey, "value");
52 EXPECT_EQ("value", (*key_values_)[kTestKey]);
53
54 base::debug::ClearCrashKey(kTestKey);
55 EXPECT_EQ(key_values_->end(), key_values_->find(kTestKey));
56 }
57
58 TEST_F(CrashLoggingTest, SetChunked) {
59 const char* kTestKey = "chunky";
60 const char* kChunk1 = "chunky-1";
61 const char* kChunk2 = "chunky-2";
62 const char* kChunk3 = "chunky-3";
63 base::debug::CrashKey keys[] = { { kTestKey, 3, 5 } };
64 base::debug::InitCrashKeys(keys, arraysize(keys), NULL);
65
66 std::map<std::string, std::string>& values = *key_values_;
67
68 // Fill only the first chunk.
69 base::debug::SetCrashKeyValue(kTestKey, "foo");
70 EXPECT_EQ(1u, values.size());
71 EXPECT_EQ("foo", values[kChunk1]);
72 EXPECT_EQ(values.end(), values.find(kChunk2));
73 EXPECT_EQ(values.end(), values.find(kChunk3));
74
75 // Fill all three chunks with truncation.
76 base::debug::SetCrashKeyValue(kTestKey, "five four three two");
77 EXPECT_EQ(3u, values.size());
78 EXPECT_EQ("five ", values[kChunk1]);
79 EXPECT_EQ("four ", values[kChunk2]);
80 EXPECT_EQ("three", values[kChunk3]);
81
82 // Fill only two chunks.
83 base::debug::SetCrashKeyValue(kTestKey, "allays");
84 EXPECT_EQ(2u, values.size());
85 EXPECT_EQ("allay", values[kChunk1]);
86 EXPECT_EQ("s", values[kChunk2]);
87 EXPECT_EQ(values.end(), values.find(kChunk3));
88
89 // Clear everything.
90 base::debug::ClearCrashKey(kTestKey);
91 EXPECT_EQ(0u, values.size());
92 EXPECT_EQ(values.end(), values.find(kChunk1));
93 EXPECT_EQ(values.end(), values.find(kChunk2));
94 EXPECT_EQ(values.end(), values.find(kChunk3));
Scott Hess - ex-Googler 2013/01/09 22:34:20 It might make sense to have this up by the all-thr
Robert Sesek 2013/01/09 23:06:30 I want to test that under-filling it clears the la
Scott Hess - ex-Googler 2013/01/09 23:40:18 Yeah, I agree that that needs tested. What I mean
Robert Sesek 2013/01/10 16:52:50 Got it. Done.
95 }
96
97 TEST_F(CrashLoggingTest, ScopedCrashKey) {
98 const char* kTestKey = "test-key";
99 base::debug::CrashKey keys[] = { { kTestKey, 1, 255 } };
100 base::debug::InitCrashKeys(keys, arraysize(keys), NULL);
101
102 EXPECT_EQ(key_values_->end(), key_values_->find(kTestKey));
103 {
104 base::debug::ScopedCrashKey scoped_crash_key(kTestKey, "value");
105 EXPECT_EQ("value", (*key_values_)[kTestKey]);
106 }
107 EXPECT_EQ(key_values_->end(), key_values_->find(kTestKey));
Scott Hess - ex-Googler 2013/01/09 22:34:20 Assertions about key_values_->size() might be reas
Robert Sesek 2013/01/09 23:06:30 Done.
108 }
109
110 TEST_F(CrashLoggingTest, InitSize) {
111 base::debug::CrashKey keys[] = {
112 { "chunked-3", 3, 255 },
113 { "single", 1, 10 },
114 { "chunked-6", 6, 200 },
Scott Hess - ex-Googler 2013/01/09 22:34:20 I'd have been perfectly happy forbidding -N from t
Robert Sesek 2013/01/09 23:06:30 Seems like it's more trouble than it's worth.
115 };
116
117 size_t num_keys;
118 base::debug::InitCrashKeys(keys, arraysize(keys), &num_keys);
119
120 EXPECT_EQ(10u, num_keys);
121
122 EXPECT_TRUE(base::debug::LookupCrashKey("chunked-3"));
123 EXPECT_TRUE(base::debug::LookupCrashKey("single"));
124 EXPECT_TRUE(base::debug::LookupCrashKey("chunked-6"));
125 EXPECT_FALSE(base::debug::LookupCrashKey("chunked-6-4"));
126 }
127
128 TEST_F(CrashLoggingTest, ChunkValue) {
129 using base::debug::ChunkCrashKeyValue;
130
131 // Test truncation.
132 base::debug::CrashKey key = { "chunky", 1, 10 };
133 std::vector<std::string> results = ChunkCrashKeyValue(key, "hello world");
134 ASSERT_EQ(1u, results.size());
135 EXPECT_EQ("hello worl", results[0]);
136
137 // Test short string.
138 results = ChunkCrashKeyValue(key, "hi");
139 ASSERT_EQ(1u, results.size());
140 EXPECT_EQ("hi", results[0]);
141
142 // Test chunk pair.
143 key.num_chunks = 2;
144 key.max_length = 3;
145 results = ChunkCrashKeyValue(key, "foobar");
146 ASSERT_EQ(2u, results.size());
147 EXPECT_EQ("foo", results[0]);
148 EXPECT_EQ("bar", results[1]);
149
150 // Test chunk pair truncation.
151 results = ChunkCrashKeyValue(key, "foobared");
152 ASSERT_EQ(2u, results.size());
153 EXPECT_EQ("foo", results[0]);
154 EXPECT_EQ("bar", results[1]);
155
156 // Test extra chunks.
157 key.num_chunks = 100;
158 results = ChunkCrashKeyValue(key, "hello world");
159 ASSERT_EQ(4u, results.size());
160 EXPECT_EQ("hel", results[0]);
161 EXPECT_EQ("lo ", results[1]);
162 EXPECT_EQ("wor", results[2]);
163 EXPECT_EQ("ld", results[3]);
164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698