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

Side by Side Diff: chromecast/app/linux/cast_crash_reporter_client_unittest.cc

Issue 1484713003: [Chromecast] Use ScopedTemp[File|Dir] in tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Fix bug in test fixture, add CHECK to ScopedTempFile::Read Created 5 years 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <fstream> 5 #include <fstream>
6 6
7 #include "base/base_paths.h" 7 #include "base/base_paths.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/files/file.h" 9 #include "base/files/file.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_vector.h" 12 #include "base/memory/scoped_vector.h"
12 #include "base/test/scoped_path_override.h" 13 #include "base/test/scoped_path_override.h"
13 #include "base/threading/thread_restrictions.h" 14 #include "base/threading/thread_restrictions.h"
14 #include "base/values.h" 15 #include "base/values.h"
15 #include "chromecast/app/linux/cast_crash_reporter_client.h" 16 #include "chromecast/app/linux/cast_crash_reporter_client.h"
17 #include "chromecast/base/scoped_temp_file.h"
16 #include "chromecast/crash/app_state_tracker.h" 18 #include "chromecast/crash/app_state_tracker.h"
17 #include "chromecast/crash/linux/crash_testing_utils.h" 19 #include "chromecast/crash/linux/crash_testing_utils.h"
18 #include "chromecast/crash/linux/crash_util.h" 20 #include "chromecast/crash/linux/crash_util.h"
19 #include "chromecast/crash/linux/dump_info.h" 21 #include "chromecast/crash/linux/dump_info.h"
20 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
21 23
22 namespace chromecast { 24 namespace chromecast {
23 namespace { 25 namespace {
24 26
25 const char kFakeDumpstateContents[] = "Dumpstate Contents\nDumpdumpdumpdump\n"; 27 const char kFakeDumpstateContents[] = "Dumpstate Contents\nDumpdumpdumpdump\n";
(...skipping 15 matching lines...) Expand all
41 CastCrashReporterClientTest() {} 43 CastCrashReporterClientTest() {}
42 ~CastCrashReporterClientTest() override {} 44 ~CastCrashReporterClientTest() override {}
43 45
44 static void SetUpTestCase() { 46 static void SetUpTestCase() {
45 // Set a callback to be used in place of the |dumpstate| executable. 47 // Set a callback to be used in place of the |dumpstate| executable.
46 CrashUtil::SetDumpStateCbForTest(base::Bind(&WriteFakeDumpStateFile)); 48 CrashUtil::SetDumpStateCbForTest(base::Bind(&WriteFakeDumpStateFile));
47 } 49 }
48 50
49 // testing::Test implementation: 51 // testing::Test implementation:
50 void SetUp() override { 52 void SetUp() override {
51 // Set up a temporary directory which will be used as our fake home dir. 53 // Override the $HOME path.
52 ASSERT_TRUE(base::CreateNewTempDirectory("", &fake_home_dir_)); 54 ASSERT_TRUE(fake_home_dir_.CreateUniqueTempDir());
53 home_override_.reset( 55 home_override_.reset(
54 new base::ScopedPathOverride(base::DIR_HOME, fake_home_dir_)); 56 new base::ScopedPathOverride(base::DIR_HOME, home_path()));
55 57
56 // "Launch" YouTube. 58 // "Launch" YouTube.
57 AppStateTracker::SetLastLaunchedApp("youtube"); 59 AppStateTracker::SetLastLaunchedApp("youtube");
58 AppStateTracker::SetCurrentApp("youtube"); 60 AppStateTracker::SetCurrentApp("youtube");
59 61
60 // "Launch" and switch to Pandora. 62 // "Launch" and switch to Pandora.
61 AppStateTracker::SetLastLaunchedApp("pandora"); 63 AppStateTracker::SetLastLaunchedApp("pandora");
62 AppStateTracker::SetCurrentApp("pandora"); 64 AppStateTracker::SetCurrentApp("pandora");
63 65
64 // "Launch" Netflix. 66 // "Launch" Netflix.
65 AppStateTracker::SetLastLaunchedApp("netflix"); 67 AppStateTracker::SetLastLaunchedApp("netflix");
66 // Netflix crashed. 68 // Netflix crashed.
67 69
68 // A minidump file is created. 70 // A minidump file is written.
69 base::CreateTemporaryFile(&minidump_path_); 71 minidump_.Write(kFakeMinidumpContents);
70 base::File minidump(minidump_path_,
71 base::File::FLAG_OPEN | base::File::FLAG_APPEND);
72 minidump.Write(0, kFakeMinidumpContents, sizeof(kFakeMinidumpContents) - 1);
73 minidump.Close();
74 } 72 }
75 73
76 void TearDown() override { 74 void TearDown() override {
77 // Remove IO restrictions in order to examine the state of the filesystem. 75 // Remove IO restrictions in order to examine the state of the filesystem.
78 base::ThreadRestrictions::SetIOAllowed(true); 76 base::ThreadRestrictions::SetIOAllowed(true);
79 77
80 // Assert that the original file has been moved. 78 // Assert that the original file has been moved.
81 ASSERT_FALSE(base::PathExists(minidump_path_)); 79 ASSERT_FALSE(base::PathExists(minidump_path()));
82 80
83 // Assert that the file has been moved to "minidumps", with the expected 81 // Assert that the file has been moved to "minidumps", with the expected
84 // contents. 82 // contents.
85 std::string contents; 83 std::string contents;
86 base::FilePath new_minidump = 84 base::FilePath new_minidump =
87 fake_home_dir_.Append("minidumps").Append(minidump_path_.BaseName()); 85 home_path().Append("minidumps").Append(minidump_path().BaseName());
88 ASSERT_TRUE(base::PathExists(new_minidump)); 86 ASSERT_TRUE(base::PathExists(new_minidump));
89 ASSERT_TRUE(base::ReadFileToString(new_minidump, &contents)); 87 ASSERT_TRUE(base::ReadFileToString(new_minidump, &contents));
90 ASSERT_EQ(kFakeMinidumpContents, contents); 88 ASSERT_EQ(kFakeMinidumpContents, contents);
91 89
92 // Assert that the dumpstate file has been written with the expected 90 // Assert that the dumpstate file has been written with the expected
93 // contents. 91 // contents.
94 base::FilePath dumpstate = new_minidump.AddExtension(".txt.gz"); 92 base::FilePath dumpstate = new_minidump.AddExtension(".txt.gz");
95 ASSERT_TRUE(base::PathExists(dumpstate)); 93 ASSERT_TRUE(base::PathExists(dumpstate));
96 ASSERT_TRUE(base::ReadFileToString(dumpstate, &contents)); 94 ASSERT_TRUE(base::ReadFileToString(dumpstate, &contents));
97 ASSERT_EQ(kFakeDumpstateContents, contents); 95 ASSERT_EQ(kFakeDumpstateContents, contents);
98 96
99 // Assert that the lockfile has logged the correct information. 97 // Assert that the lockfile has logged the correct information.
100 base::FilePath lockfile = 98 base::FilePath lockfile =
101 fake_home_dir_.Append("minidumps").Append("lockfile"); 99 home_path().Append("minidumps").Append("lockfile");
102 ASSERT_TRUE(base::PathExists(lockfile)); 100 ASSERT_TRUE(base::PathExists(lockfile));
103 ScopedVector<DumpInfo> dumps; 101 ScopedVector<DumpInfo> dumps;
104 ASSERT_TRUE(FetchDumps(lockfile.value(), &dumps)); 102 ASSERT_TRUE(FetchDumps(lockfile.value(), &dumps));
105 ASSERT_EQ(1u, dumps.size()); 103 ASSERT_EQ(1u, dumps.size());
106 104
107 const DumpInfo& dump_info = *(dumps[0]); 105 const DumpInfo& dump_info = *(dumps[0]);
108 ASSERT_TRUE(dump_info.valid()); 106 ASSERT_TRUE(dump_info.valid());
109 EXPECT_EQ(new_minidump.value(), dump_info.crashed_process_dump()); 107 EXPECT_EQ(new_minidump.value(), dump_info.crashed_process_dump());
110 EXPECT_EQ(dumpstate.value(), dump_info.logfile()); 108 EXPECT_EQ(dumpstate.value(), dump_info.logfile());
111 EXPECT_EQ("youtube", dump_info.params().previous_app_name); 109 EXPECT_EQ("youtube", dump_info.params().previous_app_name);
112 EXPECT_EQ("pandora", dump_info.params().current_app_name); 110 EXPECT_EQ("pandora", dump_info.params().current_app_name);
113 EXPECT_EQ("netflix", dump_info.params().last_app_name); 111 EXPECT_EQ("netflix", dump_info.params().last_app_name);
114 } 112 }
115 113
116 const base::FilePath& minidump_path() { return minidump_path_; } 114 base::FilePath minidump_path() { return minidump_.path(); }
115 base::FilePath home_path() { return fake_home_dir_.path(); }
117 116
118 private: 117 private:
119 base::FilePath fake_home_dir_; 118 base::ScopedTempDir fake_home_dir_;
120 base::FilePath minidump_path_; 119 ScopedTempFile minidump_;
121 scoped_ptr<base::ScopedPathOverride> home_override_; 120 scoped_ptr<base::ScopedPathOverride> home_override_;
121
122 DISALLOW_COPY_AND_ASSIGN(CastCrashReporterClientTest);
122 }; 123 };
123 124
124 #if ENABLE_THREAD_RESTRICTIONS 125 #if ENABLE_THREAD_RESTRICTIONS
125 // This test shall only be run when thread restricitons are enabled. Otherwise, 126 // This test shall only be run when thread restricitons are enabled. Otherwise,
126 // the thread will not actually be IO-restricted, and the final ASSERT will 127 // the thread will not actually be IO-restricted, and the final ASSERT will
127 // fail. 128 // fail.
128 TEST_F(CastCrashReporterClientTest, EndToEndTestOnIORestrictedThread) { 129 TEST_F(CastCrashReporterClientTest, EndToEndTestOnIORestrictedThread) {
129 // Handle a "crash" on an IO restricted thread. 130 // Handle a "crash" on an IO restricted thread.
130 base::ThreadRestrictions::SetIOAllowed(false); 131 base::ThreadRestrictions::SetIOAllowed(false);
131 CastCrashReporterClient client; 132 CastCrashReporterClient client;
(...skipping 10 matching lines...) Expand all
142 base::ThreadRestrictions::SetIOAllowed(true); 143 base::ThreadRestrictions::SetIOAllowed(true);
143 CastCrashReporterClient client; 144 CastCrashReporterClient client;
144 ASSERT_TRUE(client.HandleCrashDump(minidump_path().value().c_str())); 145 ASSERT_TRUE(client.HandleCrashDump(minidump_path().value().c_str()));
145 146
146 // Assert that the thread is not IO restricted when the function exits. 147 // Assert that the thread is not IO restricted when the function exits.
147 // Note that SetIOAllowed returns the previous value. 148 // Note that SetIOAllowed returns the previous value.
148 ASSERT_TRUE(base::ThreadRestrictions::SetIOAllowed(true)); 149 ASSERT_TRUE(base::ThreadRestrictions::SetIOAllowed(true));
149 } 150 }
150 151
151 } // namespace chromecast 152 } // namespace chromecast
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698