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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: base/debug/crash_logging_unittest.cc
diff --git a/base/debug/crash_logging_unittest.cc b/base/debug/crash_logging_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5209f3fdcf0c11f2542ce6cf4b1e7f58b2a6dcec
--- /dev/null
+++ b/base/debug/crash_logging_unittest.cc
@@ -0,0 +1,164 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/debug/crash_logging.h"
+
+#include <map>
+#include <string>
+
+#include "base/bind.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+std::map<std::string, std::string>* key_values_ = NULL;
+
+} // namespace
+
+class CrashLoggingTest : public testing::Test {
+ public:
+ virtual void SetUp() {
+ base::debug::SetCrashKeyReportingFunctions(
+ &CrashLoggingTest::SetKeyValue,
+ &CrashLoggingTest::ClearKeyValue);
+ 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.
+ }
+
+ virtual void TearDown() {
+ base::debug::ResetCrashLoggingForTesting();
+
+ delete key_values_;
+ key_values_ = NULL;
+ }
+
+ private:
+ static void SetKeyValue(const base::StringPiece& key,
+ const base::StringPiece& value) {
+ (*key_values_)[key.as_string()] = value.as_string();
+ }
+
+ static void ClearKeyValue(const base::StringPiece& key) {
+ key_values_->erase(key.as_string());
+ }
+};
+
+TEST_F(CrashLoggingTest, SetClearSingle) {
+ const char* kTestKey = "test-key";
+ base::debug::CrashKey keys[1] = { { kTestKey, 1, 255 } };
+ base::debug::InitCrashKeys(keys, 1, NULL);
+
+ base::debug::SetCrashKeyValue(kTestKey, "value");
+ EXPECT_EQ("value", (*key_values_)[kTestKey]);
+
+ base::debug::ClearCrashKey(kTestKey);
+ EXPECT_EQ(key_values_->end(), key_values_->find(kTestKey));
+}
+
+TEST_F(CrashLoggingTest, SetChunked) {
+ const char* kTestKey = "chunky";
+ const char* kChunk1 = "chunky-1";
+ const char* kChunk2 = "chunky-2";
+ const char* kChunk3 = "chunky-3";
+ base::debug::CrashKey keys[] = { { kTestKey, 3, 5 } };
+ base::debug::InitCrashKeys(keys, arraysize(keys), NULL);
+
+ std::map<std::string, std::string>& values = *key_values_;
+
+ // Fill only the first chunk.
+ base::debug::SetCrashKeyValue(kTestKey, "foo");
+ EXPECT_EQ(1u, values.size());
+ EXPECT_EQ("foo", values[kChunk1]);
+ EXPECT_EQ(values.end(), values.find(kChunk2));
+ EXPECT_EQ(values.end(), values.find(kChunk3));
+
+ // Fill all three chunks with truncation.
+ base::debug::SetCrashKeyValue(kTestKey, "five four three two");
+ EXPECT_EQ(3u, values.size());
+ EXPECT_EQ("five ", values[kChunk1]);
+ EXPECT_EQ("four ", values[kChunk2]);
+ EXPECT_EQ("three", values[kChunk3]);
+
+ // Fill only two chunks.
+ base::debug::SetCrashKeyValue(kTestKey, "allays");
+ EXPECT_EQ(2u, values.size());
+ EXPECT_EQ("allay", values[kChunk1]);
+ EXPECT_EQ("s", values[kChunk2]);
+ EXPECT_EQ(values.end(), values.find(kChunk3));
+
+ // Clear everything.
+ base::debug::ClearCrashKey(kTestKey);
+ EXPECT_EQ(0u, values.size());
+ EXPECT_EQ(values.end(), values.find(kChunk1));
+ EXPECT_EQ(values.end(), values.find(kChunk2));
+ 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.
+}
+
+TEST_F(CrashLoggingTest, ScopedCrashKey) {
+ const char* kTestKey = "test-key";
+ base::debug::CrashKey keys[] = { { kTestKey, 1, 255 } };
+ base::debug::InitCrashKeys(keys, arraysize(keys), NULL);
+
+ EXPECT_EQ(key_values_->end(), key_values_->find(kTestKey));
+ {
+ base::debug::ScopedCrashKey scoped_crash_key(kTestKey, "value");
+ EXPECT_EQ("value", (*key_values_)[kTestKey]);
+ }
+ 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.
+}
+
+TEST_F(CrashLoggingTest, InitSize) {
+ base::debug::CrashKey keys[] = {
+ { "chunked-3", 3, 255 },
+ { "single", 1, 10 },
+ { "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.
+ };
+
+ size_t num_keys;
+ base::debug::InitCrashKeys(keys, arraysize(keys), &num_keys);
+
+ EXPECT_EQ(10u, num_keys);
+
+ EXPECT_TRUE(base::debug::LookupCrashKey("chunked-3"));
+ EXPECT_TRUE(base::debug::LookupCrashKey("single"));
+ EXPECT_TRUE(base::debug::LookupCrashKey("chunked-6"));
+ EXPECT_FALSE(base::debug::LookupCrashKey("chunked-6-4"));
+}
+
+TEST_F(CrashLoggingTest, ChunkValue) {
+ using base::debug::ChunkCrashKeyValue;
+
+ // Test truncation.
+ base::debug::CrashKey key = { "chunky", 1, 10 };
+ std::vector<std::string> results = ChunkCrashKeyValue(key, "hello world");
+ ASSERT_EQ(1u, results.size());
+ EXPECT_EQ("hello worl", results[0]);
+
+ // Test short string.
+ results = ChunkCrashKeyValue(key, "hi");
+ ASSERT_EQ(1u, results.size());
+ EXPECT_EQ("hi", results[0]);
+
+ // Test chunk pair.
+ key.num_chunks = 2;
+ key.max_length = 3;
+ results = ChunkCrashKeyValue(key, "foobar");
+ ASSERT_EQ(2u, results.size());
+ EXPECT_EQ("foo", results[0]);
+ EXPECT_EQ("bar", results[1]);
+
+ // Test chunk pair truncation.
+ results = ChunkCrashKeyValue(key, "foobared");
+ ASSERT_EQ(2u, results.size());
+ EXPECT_EQ("foo", results[0]);
+ EXPECT_EQ("bar", results[1]);
+
+ // Test extra chunks.
+ key.num_chunks = 100;
+ results = ChunkCrashKeyValue(key, "hello world");
+ ASSERT_EQ(4u, results.size());
+ EXPECT_EQ("hel", results[0]);
+ EXPECT_EQ("lo ", results[1]);
+ EXPECT_EQ("wor", results[2]);
+ EXPECT_EQ("ld", results[3]);
+}

Powered by Google App Engine
This is Rietveld 408576698