OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 <fstream> |
| 6 |
| 7 #include "base/base_paths.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/files/file.h" |
| 10 #include "base/files/file_util.h" |
| 11 #include "base/memory/scoped_vector.h" |
| 12 #include "base/test/scoped_path_override.h" |
| 13 #include "chromecast/app/linux/cast_crash_reporter_client.h" |
| 14 #include "chromecast/crash/app_state_tracker.h" |
| 15 #include "chromecast/crash/linux/crash_util.h" |
| 16 #include "chromecast/crash/linux/dump_info.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace chromecast { |
| 20 namespace { |
| 21 |
| 22 const char kFakeDumpstateContents[] = "Dumpstate Contents\nDumpdumpdumpdump\n"; |
| 23 const char kFakeMinidumpContents[] = "Minidump Contents\nLine1\nLine2\n"; |
| 24 |
| 25 int WriteFakeDumpStateFile(const std::string& path) { |
| 26 // Append the correct extension and write the data to file. |
| 27 base::File dumpstate(base::FilePath(path).AddExtension(".txt.gz"), |
| 28 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 29 dumpstate.Write( |
| 30 0, kFakeDumpstateContents, sizeof(kFakeDumpstateContents) - 1); |
| 31 return 0; |
| 32 } |
| 33 |
| 34 ScopedVector<DumpInfo> GetCurrentDumps(const std::string& logfile_path) { |
| 35 ScopedVector<DumpInfo> dumps; |
| 36 std::string entry; |
| 37 |
| 38 std::ifstream in(logfile_path); |
| 39 DCHECK(in.is_open()); |
| 40 while (std::getline(in, entry)) { |
| 41 scoped_ptr<DumpInfo> info(new DumpInfo(entry)); |
| 42 dumps.push_back(info.Pass()); |
| 43 } |
| 44 return dumps.Pass(); |
| 45 } |
| 46 |
| 47 } // namespace |
| 48 |
| 49 TEST(CastCrashReporterClientTest, EndToEnd) { |
| 50 // Set up a temporary directory which will be used as our fake home dir. |
| 51 base::FilePath fake_home_dir; |
| 52 ASSERT_TRUE(base::CreateNewTempDirectory("", &fake_home_dir)); |
| 53 base::ScopedPathOverride home(base::DIR_HOME, fake_home_dir); |
| 54 |
| 55 // Set a callback to be used in place of the |dumpstate| executable. |
| 56 CrashUtil::SetDumpStateCbForTest(base::Bind(&WriteFakeDumpStateFile)); |
| 57 |
| 58 // "Launch" YouTube. |
| 59 AppStateTracker::SetLastLaunchedApp("youtube"); |
| 60 AppStateTracker::SetCurrentApp("youtube"); |
| 61 |
| 62 // "Launch" and switch to Pandora. |
| 63 AppStateTracker::SetLastLaunchedApp("pandora"); |
| 64 AppStateTracker::SetCurrentApp("pandora"); |
| 65 |
| 66 // "Launch" Netflix. |
| 67 AppStateTracker::SetLastLaunchedApp("netflix"); |
| 68 // Netflix crashed. |
| 69 |
| 70 // A minidump file is created. |
| 71 base::FilePath minidump_path; |
| 72 base::CreateTemporaryFile(&minidump_path); |
| 73 base::File minidump(minidump_path, |
| 74 base::File::FLAG_OPEN | base::File::FLAG_APPEND); |
| 75 minidump.Write(0, kFakeMinidumpContents, sizeof(kFakeMinidumpContents) - 1); |
| 76 minidump.Close(); |
| 77 |
| 78 // Handle the crash. |
| 79 CastCrashReporterClient client; |
| 80 ASSERT_TRUE(client.HandleCrashDump(minidump_path.value().c_str())); |
| 81 |
| 82 // Assert that the original file has been moved. |
| 83 ASSERT_FALSE(base::PathExists(minidump_path)); |
| 84 |
| 85 // Assert that the file has been moved to "minidumps", with the expected |
| 86 // contents. |
| 87 std::string contents; |
| 88 base::FilePath new_minidump = |
| 89 fake_home_dir.Append("minidumps").Append(minidump_path.BaseName()); |
| 90 ASSERT_TRUE(base::PathExists(new_minidump)); |
| 91 ASSERT_TRUE(base::ReadFileToString(new_minidump, &contents)); |
| 92 ASSERT_EQ(kFakeMinidumpContents, contents); |
| 93 |
| 94 // Assert that the dumpstate file has been written with the expected contents. |
| 95 base::FilePath dumpstate = new_minidump.AddExtension(".txt.gz"); |
| 96 ASSERT_TRUE(base::PathExists(dumpstate)); |
| 97 ASSERT_TRUE(base::ReadFileToString(dumpstate, &contents)); |
| 98 ASSERT_EQ(kFakeDumpstateContents, contents); |
| 99 |
| 100 // Assert that the lockfile has logged the correct information. |
| 101 base::FilePath lockfile = |
| 102 fake_home_dir.Append("minidumps").Append("lockfile"); |
| 103 ASSERT_TRUE(base::PathExists(lockfile)); |
| 104 ScopedVector<DumpInfo> dumps = GetCurrentDumps(lockfile.value()); |
| 105 ASSERT_EQ(1u, dumps.size()); |
| 106 |
| 107 const DumpInfo& dump_info = *(dumps[0]); |
| 108 ASSERT_TRUE(dump_info.valid()); |
| 109 EXPECT_EQ(new_minidump.value(), dump_info.crashed_process_dump()); |
| 110 EXPECT_EQ(dumpstate.value(), dump_info.logfile()); |
| 111 EXPECT_EQ("youtube", dump_info.params().previous_app_name); |
| 112 EXPECT_EQ("pandora", dump_info.params().current_app_name); |
| 113 EXPECT_EQ("netflix", dump_info.params().last_app_name); |
| 114 } |
| 115 |
| 116 } // namespace chromecast |
OLD | NEW |