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

Unified Diff: chromecast/crash/linux/crash_testing_utils.cc

Issue 1254113004: [Chromecast] Consolidate duplicated crash test functions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Updated commit message Created 5 years, 5 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: chromecast/crash/linux/crash_testing_utils.cc
diff --git a/chromecast/crash/linux/crash_testing_utils.cc b/chromecast/crash/linux/crash_testing_utils.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c188e8322b791645d35e6cf40d63e46ac6828d5e
--- /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);
+}
+
+} // chromecast
slan 2015/07/28 23:50:30 nit: namespace chromecast
bcf_google 2015/07/29 00:41:13 Done.

Powered by Google App Engine
This is Rietveld 408576698