summaryrefslogtreecommitdiffstats
path: root/chromecast
diff options
context:
space:
mode:
authorbcf <bcf@google.com>2015-07-29 11:23:52 -0700
committerCommit bot <commit-bot@chromium.org>2015-07-29 18:24:22 +0000
commit62f6287af58fa80a2b830e436ea4f8dd20b8e1d5 (patch)
tree8353d322eaa3a88704b2ac1b7da7b8b795d2e192 /chromecast
parent3c100cee848bd29360dad2cba9a7013910dacafb (diff)
downloadchromium_src-62f6287af58fa80a2b830e436ea4f8dd20b8e1d5.zip
chromium_src-62f6287af58fa80a2b830e436ea4f8dd20b8e1d5.tar.gz
chromium_src-62f6287af58fa80a2b830e436ea4f8dd20b8e1d5.tar.bz2
[Chromecast] Consolidate duplicated crash test functions.
TEST=Modified tests build and pass under gyp and gn. BUG= Review URL: https://codereview.chromium.org/1254113004 Cr-Commit-Position: refs/heads/master@{#340924}
Diffstat (limited to 'chromecast')
-rw-r--r--chromecast/app/BUILD.gn1
-rw-r--r--chromecast/app/linux/cast_crash_reporter_client_unittest.cc31
-rw-r--r--chromecast/chromecast_tests.gypi14
-rw-r--r--chromecast/crash/BUILD.gn13
-rw-r--r--chromecast/crash/linux/crash_testing_utils.cc93
-rw-r--r--chromecast/crash/linux/crash_testing_utils.h35
-rw-r--r--chromecast/crash/linux/dump_info_unittest.cc13
-rw-r--r--chromecast/crash/linux/minidump_writer_unittest.cc34
-rw-r--r--chromecast/crash/linux/synchronized_minidump_manager_unittest.cc42
9 files changed, 166 insertions, 110 deletions
diff --git a/chromecast/app/BUILD.gn b/chromecast/app/BUILD.gn
index 51cf158..6515e11 100644
--- a/chromecast/app/BUILD.gn
+++ b/chromecast/app/BUILD.gn
@@ -36,6 +36,7 @@ test("cast_shell_unittests") {
"//base/test:run_all_unittests",
"//base/test:test_support",
"//chromecast/crash",
+ "//chromecast/crash:test_support",
"//testing/gtest",
]
}
diff --git a/chromecast/app/linux/cast_crash_reporter_client_unittest.cc b/chromecast/app/linux/cast_crash_reporter_client_unittest.cc
index e37e4df..adcfd26 100644
--- a/chromecast/app/linux/cast_crash_reporter_client_unittest.cc
+++ b/chromecast/app/linux/cast_crash_reporter_client_unittest.cc
@@ -13,8 +13,8 @@
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "chromecast/app/linux/cast_crash_reporter_client.h"
-#include "chromecast/base/serializers.h"
#include "chromecast/crash/app_state_tracker.h"
+#include "chromecast/crash/linux/crash_testing_utils.h"
#include "chromecast/crash/linux/crash_util.h"
#include "chromecast/crash/linux/dump_info.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -24,7 +24,6 @@ namespace {
const char kFakeDumpstateContents[] = "Dumpstate Contents\nDumpdumpdumpdump\n";
const char kFakeMinidumpContents[] = "Minidump Contents\nLine1\nLine2\n";
-const char kDumpsKey[] = "dumps";
int WriteFakeDumpStateFile(const std::string& path) {
// Append the correct extension and write the data to file.
@@ -35,34 +34,6 @@ int WriteFakeDumpStateFile(const std::string& path) {
return 0;
}
-bool FetchDumps(const std::string& lockfile_path,
- ScopedVector<DumpInfo>* dumps) {
- if (!dumps) {
- return false;
- }
- dumps->clear();
-
- base::FilePath path(lockfile_path);
- scoped_ptr<base::Value> contents(DeserializeJsonFromFile(path));
-
- base::DictionaryValue* dict;
- base::ListValue* dump_list;
- if (!contents || !contents->GetAsDictionary(&dict) ||
- !dict->GetList(kDumpsKey, &dump_list) || !dump_list) {
- return false;
- }
-
- for (base::Value* elem : *dump_list) {
- scoped_ptr<DumpInfo> dump = make_scoped_ptr(new DumpInfo(elem));
- if (!dump->valid()) {
- return false;
- }
- dumps->push_back(dump.Pass());
- }
-
- return true;
-}
-
} // namespace
class CastCrashReporterClientTest : public testing::Test {
diff --git a/chromecast/chromecast_tests.gypi b/chromecast/chromecast_tests.gypi
index 96897e2..075734a 100644
--- a/chromecast/chromecast_tests.gypi
+++ b/chromecast/chromecast_tests.gypi
@@ -30,9 +30,22 @@
],
}, # end of cast_base_unittests
{
+ 'target_name': 'cast_crash_test_support',
+ 'type': '<(component)',
+ 'dependencies': [
+ 'chromecast.gyp:cast_crash',
+ 'cast_base',
+ ],
+ 'sources': [
+ 'crash/linux/crash_testing_utils.cc',
+ 'crash/linux/crash_testing_utils.h',
+ ],
+ }, # end of target 'cast_crash_test_support'
+ {
'target_name': 'cast_crash_unittests',
'type': '<(gtest_target_type)',
'dependencies': [
+ 'cast_crash_test_support',
'chromecast.gyp:cast_crash',
'../base/base.gyp:run_all_unittests',
'../testing/gmock.gyp:gmock',
@@ -330,6 +343,7 @@
'target_name': 'cast_shell_unittests',
'type': '<(gtest_target_type)',
'dependencies': [
+ 'cast_crash_test_support',
'chromecast.gyp:cast_crash_client',
'../base/base.gyp:run_all_unittests',
'../testing/gtest.gyp:gtest',
diff --git a/chromecast/crash/BUILD.gn b/chromecast/crash/BUILD.gn
index d085b35..900b95c 100644
--- a/chromecast/crash/BUILD.gn
+++ b/chromecast/crash/BUILD.gn
@@ -37,6 +37,18 @@ source_set("crash") {
]
}
+source_set("test_support") {
+ sources = [
+ "linux/crash_testing_utils.cc",
+ "linux/crash_testing_utils.h",
+ ]
+
+ deps = [
+ ":crash",
+ "//base",
+ ]
+}
+
test("cast_crash_unittests") {
sources = [
"cast_crashdump_uploader_unittest.cc",
@@ -48,6 +60,7 @@ test("cast_crash_unittests") {
deps = [
":crash",
+ ":test_support",
"//base",
"//base/test:run_all_unittests",
"//base/test:test_support",
diff --git a/chromecast/crash/linux/crash_testing_utils.cc b/chromecast/crash/linux/crash_testing_utils.cc
new file mode 100644
index 0000000..61b9ea1
--- /dev/null
+++ b/chromecast/crash/linux/crash_testing_utils.cc
@@ -0,0 +1,93 @@
+// Copyright 2015 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 "chromecast/crash/linux/crash_testing_utils.h"
+
+#include "base/files/file_util.h"
+#include "base/values.h"
+#include "chromecast/base/serializers.h"
+#include "chromecast/crash/linux/dump_info.h"
+
+namespace chromecast {
+namespace {
+
+const char kDumpsKey[] = "dumps";
+
+// Gets the list of deserialized DumpInfo given a deserialized |lockfile|.
+base::ListValue* GetDumpList(base::Value* lockfile) {
+ base::DictionaryValue* dict;
+ base::ListValue* dump_list;
+ if (!lockfile || !lockfile->GetAsDictionary(&dict) ||
+ !dict->GetList(kDumpsKey, &dump_list)) {
+ return nullptr;
+ }
+
+ return dump_list;
+}
+
+} // namespcae
+
+scoped_ptr<DumpInfo> CreateDumpInfo(const std::string& json_string) {
+ scoped_ptr<base::Value> value(DeserializeFromJson(json_string));
+ return make_scoped_ptr(new DumpInfo(value.get()));
+}
+
+bool FetchDumps(const std::string& lockfile_path,
+ ScopedVector<DumpInfo>* dumps) {
+ if (!dumps) {
+ return false;
+ }
+ dumps->clear();
+
+ base::FilePath path(lockfile_path);
+ scoped_ptr<base::Value> contents(DeserializeJsonFromFile(path));
+ base::ListValue* dump_list = GetDumpList(contents.get());
+ if (!dump_list) {
+ return false;
+ }
+
+ for (base::Value* elem : *dump_list) {
+ scoped_ptr<DumpInfo> dump = make_scoped_ptr(new DumpInfo(elem));
+ if (!dump->valid()) {
+ return false;
+ }
+ dumps->push_back(dump.Pass());
+ }
+
+ return true;
+}
+
+bool CreateLockFile(const std::string& lockfile_path) {
+ scoped_ptr<base::DictionaryValue> output =
+ make_scoped_ptr(new base::DictionaryValue());
+ output->Set(kDumpsKey, make_scoped_ptr(new base::ListValue()));
+
+ base::FilePath path(lockfile_path);
+
+ const base::Value* val = output.get();
+ return SerializeJsonToFile(path, *val);
+}
+
+bool AppendLockFile(const std::string& lockfile_path, const DumpInfo& dump) {
+ base::FilePath path(lockfile_path);
+
+ scoped_ptr<base::Value> contents(DeserializeJsonFromFile(path));
+ if (!contents) {
+ CreateLockFile(lockfile_path);
+ if (!(contents = DeserializeJsonFromFile(path))) {
+ return false;
+ }
+ }
+
+ base::ListValue* dump_list = GetDumpList(contents.get());
+ if (!dump_list) {
+ return false;
+ }
+
+ dump_list->Append(dump.GetAsValue());
+
+ return SerializeJsonToFile(path, *contents);
+}
+
+} // namespace chromecast
diff --git a/chromecast/crash/linux/crash_testing_utils.h b/chromecast/crash/linux/crash_testing_utils.h
new file mode 100644
index 0000000..be23b77
--- /dev/null
+++ b/chromecast/crash/linux/crash_testing_utils.h
@@ -0,0 +1,35 @@
+// Copyright 2015 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.
+
+#ifndef CHROMECAST_CRASH_LINUX_CRASH_TESTING_UTILS_H_
+#define CHROMECAST_CRASH_LINUX_CRASH_TESTING_UTILS_H_
+
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/scoped_vector.h"
+
+namespace chromecast {
+
+class DumpInfo;
+
+// Creates a DumpInfo object corresponding to the deserialization of
+// |json_string|. Returned DumpInfo object maybe invalid if |json_string|
+// doesn't correspond to a valid DumpInfo object.
+scoped_ptr<DumpInfo> CreateDumpInfo(const std::string& json_string);
+
+// Populates |dumps| with all the DumpInfo entries serialized in the lockfile at
+// |lockfile_path|. Returns true on success, false on error.
+bool FetchDumps(const std::string& lockfile_path,
+ ScopedVector<DumpInfo>* dumps);
+
+// Creates an empty lockfile at |lockfile_path|. Returns true on success, false
+// on error.
+bool CreateLockFile(const std::string& lockfile_path);
+
+// Appends serialization of |dump| onto the lockfile at |lockfile_path|.
+// Returns true on success, false on error.
+bool AppendLockFile(const std::string& lockfile_path, const DumpInfo& dump);
+
+} // namespace chromecast
+
+#endif // CHROMECAST_CRASH_LINUX_CRASH_TESTING_UTILS_H_
diff --git a/chromecast/crash/linux/dump_info_unittest.cc b/chromecast/crash/linux/dump_info_unittest.cc
index fcbd646..c567e8e 100644
--- a/chromecast/crash/linux/dump_info_unittest.cc
+++ b/chromecast/crash/linux/dump_info_unittest.cc
@@ -5,21 +5,12 @@
#include <time.h>
#include "base/values.h"
-#include "chromecast/base/serializers.h"
+#include "chromecast/crash/linux/crash_testing_utils.h"
#include "chromecast/crash/linux/dump_info.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromecast {
-namespace {
-
-scoped_ptr<DumpInfo> CreateDumpInfo(const std::string& json_string) {
- scoped_ptr<base::Value> value(DeserializeFromJson(json_string));
- return make_scoped_ptr(new DumpInfo(value.get()));
-}
-
-} // namespace
-
TEST(DumpInfoTest, EmptyStringIsNotValid) {
scoped_ptr<DumpInfo> dump_info(CreateDumpInfo(""));
ASSERT_FALSE(dump_info->valid());
@@ -202,4 +193,4 @@ TEST(DumpInfoTest, TooManyFieldsIsNotValid) {
ASSERT_FALSE(info->valid());
}
-} // chromecast
+} // namespace chromecast
diff --git a/chromecast/crash/linux/minidump_writer_unittest.cc b/chromecast/crash/linux/minidump_writer_unittest.cc
index 41b4905..d623085 100644
--- a/chromecast/crash/linux/minidump_writer_unittest.cc
+++ b/chromecast/crash/linux/minidump_writer_unittest.cc
@@ -10,7 +10,7 @@
#include "base/files/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/test/scoped_path_override.h"
-#include "chromecast/base/serializers.h"
+#include "chromecast/crash/linux/crash_testing_utils.h"
#include "chromecast/crash/linux/dump_info.h"
#include "chromecast/crash/linux/minidump_generator.h"
#include "chromecast/crash/linux/minidump_writer.h"
@@ -22,7 +22,6 @@ namespace {
const char kDumplogFile[] = "dumplog";
const char kLockfileName[] = "lockfile";
const char kMinidumpSubdir[] = "minidumps";
-const char kDumpsKey[] = "dumps";
class FakeMinidumpGenerator : public MinidumpGenerator {
public:
@@ -37,11 +36,6 @@ int FakeDumpState(const std::string& minidump_path) {
return 0;
}
-scoped_ptr<DumpInfo> CreateDumpInfo(const std::string& json_string) {
- scoped_ptr<base::Value> value(DeserializeFromJson(json_string));
- return make_scoped_ptr(new DumpInfo(value.get()));
-}
-
} // namespace
class MinidumpWriterTest : public testing::Test {
@@ -66,24 +60,8 @@ class MinidumpWriterTest : public testing::Test {
ASSERT_TRUE(lockfile.IsValid());
}
- int AppendLockFile(const DumpInfo& dump) {
- scoped_ptr<base::Value> contents(DeserializeJsonFromFile(lockfile_path_));
- if (!contents) {
- base::DictionaryValue* dict = new base::DictionaryValue();
- contents = make_scoped_ptr(dict);
- dict->Set(kDumpsKey, make_scoped_ptr(new base::ListValue()));
- }
-
- base::DictionaryValue* dict;
- base::ListValue* dump_list;
- if (!contents || !contents->GetAsDictionary(&dict) ||
- !dict->GetList(kDumpsKey, &dump_list) || !dump_list) {
- return -1;
- }
-
- dump_list->Append(dump.GetAsValue());
-
- return SerializeJsonToFile(lockfile_path_, *contents) ? 0 : -1;
+ bool AppendLockFile(const DumpInfo& dump) {
+ return chromecast::AppendLockFile(lockfile_path_.value(), dump);
}
FakeMinidumpGenerator fake_generator_;
@@ -159,7 +137,7 @@ TEST_F(MinidumpWriterTest, Write_FailsWhenTooManyDumpsPresent) {
"\"logfile\": \"logfile.log\""
"}"));
ASSERT_TRUE(info->valid());
- ASSERT_EQ(0, AppendLockFile(*info));
+ ASSERT_TRUE(AppendLockFile(*info));
}
ASSERT_EQ(-1, writer.Write());
@@ -176,7 +154,7 @@ TEST_F(MinidumpWriterTest, Write_FailsWhenTooManyRecentDumpsPresent) {
for (size_t i = 0; i < too_many_recent_dumps; ++i) {
MinidumpParams params;
DumpInfo info("dump", "/dump/path", time(nullptr), params);
- ASSERT_EQ(0, AppendLockFile(info));
+ ASSERT_TRUE(AppendLockFile(info));
}
ASSERT_EQ(-1, writer.Write());
@@ -201,7 +179,7 @@ TEST_F(MinidumpWriterTest, Write_SucceedsWhenDumpLimitsNotExceeded) {
"\"logfile\": \"logfile.log\""
"}"));
ASSERT_TRUE(info->valid());
- ASSERT_EQ(0, AppendLockFile(*info));
+ ASSERT_TRUE(AppendLockFile(*info));
ASSERT_EQ(0, writer.Write());
}
diff --git a/chromecast/crash/linux/synchronized_minidump_manager_unittest.cc b/chromecast/crash/linux/synchronized_minidump_manager_unittest.cc
index 45b507a..41a84f0 100644
--- a/chromecast/crash/linux/synchronized_minidump_manager_unittest.cc
+++ b/chromecast/crash/linux/synchronized_minidump_manager_unittest.cc
@@ -22,7 +22,7 @@
#include "base/test/scoped_path_override.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h"
-#include "chromecast/base/serializers.h"
+#include "chromecast/crash/linux/crash_testing_utils.h"
#include "chromecast/crash/linux/dump_info.h"
#include "chromecast/crash/linux/synchronized_minidump_manager.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -32,7 +32,6 @@ namespace {
const char kLockfileName[] = "lockfile";
const char kMinidumpSubdir[] = "minidumps";
-const char kDumpsKey[] = "dumps";
// A trivial implementation of SynchronizedMinidumpManager, which does no work
// to the
@@ -73,45 +72,6 @@ class SynchronizedMinidumpManagerSimple : public SynchronizedMinidumpManager {
scoped_ptr<DumpInfo> dump_info_;
};
-bool FetchDumps(const std::string& lockfile_path,
- ScopedVector<DumpInfo>* dumps) {
- if (!dumps) {
- return false;
- }
- dumps->clear();
-
- base::FilePath path(lockfile_path);
- scoped_ptr<base::Value> contents(DeserializeJsonFromFile(path));
-
- base::DictionaryValue* dict;
- base::ListValue* dump_list;
- if (!contents || !contents->GetAsDictionary(&dict) ||
- !dict->GetList(kDumpsKey, &dump_list) || !dump_list) {
- return false;
- }
-
- for (base::Value* elem : *dump_list) {
- scoped_ptr<DumpInfo> dump = make_scoped_ptr(new DumpInfo(elem));
- if (!dump->valid()) {
- return false;
- }
- dumps->push_back(dump.Pass());
- }
-
- return true;
-}
-
-bool CreateLockFile(const std::string& lockfile_path) {
- scoped_ptr<base::DictionaryValue> output =
- make_scoped_ptr(new base::DictionaryValue());
- output->Set("dumps", make_scoped_ptr(new base::ListValue()));
-
- base::FilePath path(lockfile_path);
-
- const base::Value* val = output.get();
- return SerializeJsonToFile(path, *val);
-}
-
void DoWorkLockedTask(SynchronizedMinidumpManagerSimple* manager) {
manager->DoWorkLocked();
}