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.
7 #include "base/base_paths.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/test/scoped_path_override.h"
13 #include "chromecast/crash/linux/crash_testing_utils.h"
14 #include "chromecast/crash/linux/dump_info.h"
15 #include "chromecast/crash/linux/minidump_generator.h"
16 #include "chromecast/crash/linux/minidump_writer.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace chromecast
{
22 const char kDumplogFile
[] = "dumplog";
23 const char kLockfileName
[] = "lockfile";
24 const char kMinidumpSubdir
[] = "minidumps";
26 class FakeMinidumpGenerator
: public MinidumpGenerator
{
28 FakeMinidumpGenerator() {}
29 ~FakeMinidumpGenerator() override
{}
31 // MinidumpGenerator implementation:
32 bool Generate(const std::string
& minidump_path
) override
{ return true; }
35 int FakeDumpState(const std::string
& minidump_path
) {
41 class MinidumpWriterTest
: public testing::Test
{
43 MinidumpWriterTest() {}
44 ~MinidumpWriterTest() override
{}
46 void SetUp() override
{
47 // Set up a temporary directory which will be used as our fake home dir.
48 base::FilePath fake_home_dir
;
49 ASSERT_TRUE(base::CreateNewTempDirectory("", &fake_home_dir
));
50 home_
.reset(new base::ScopedPathOverride(base::DIR_HOME
, fake_home_dir
));
51 minidump_dir_
= fake_home_dir
.Append(kMinidumpSubdir
);
52 dumplog_file_
= minidump_dir_
.Append(kDumplogFile
);
53 lockfile_path_
= minidump_dir_
.Append(kLockfileName
);
55 // Create the minidump directory and lockfile.
56 ASSERT_TRUE(base::CreateDirectory(minidump_dir_
));
59 base::File::FLAG_CREATE_ALWAYS
| base::File::FLAG_WRITE
);
60 ASSERT_TRUE(lockfile
.IsValid());
63 bool AppendLockFile(const DumpInfo
& dump
) {
64 return chromecast::AppendLockFile(lockfile_path_
.value(), dump
);
67 FakeMinidumpGenerator fake_generator_
;
68 base::FilePath minidump_dir_
;
69 base::FilePath dumplog_file_
;
70 base::FilePath lockfile_path_
;
73 scoped_ptr
<base::ScopedPathOverride
> home_
;
75 DISALLOW_COPY_AND_ASSIGN(MinidumpWriterTest
);
78 TEST_F(MinidumpWriterTest
, Write_FailsWithIncorrectMinidumpPath
) {
79 MinidumpWriter
writer(&fake_generator_
,
82 base::Bind(&FakeDumpState
));
84 ASSERT_EQ(-1, writer
.Write());
87 TEST_F(MinidumpWriterTest
, Write_FailsWithMultiLevelRelativeMinidumpPath
) {
88 MinidumpWriter
writer(&fake_generator_
,
91 base::Bind(&FakeDumpState
));
93 ASSERT_EQ(-1, writer
.Write());
96 TEST_F(MinidumpWriterTest
, Write_SucceedsWithSimpleFilename
) {
97 MinidumpWriter
writer(&fake_generator_
,
100 base::Bind(&FakeDumpState
));
102 ASSERT_EQ(0, writer
.Write());
105 TEST_F(MinidumpWriterTest
, Write_SucceedsWithCorrectMinidumpPath
) {
106 MinidumpWriter
writer(&fake_generator_
,
107 dumplog_file_
.value(),
109 base::Bind(&FakeDumpState
));
111 ASSERT_EQ(0, writer
.Write());
114 TEST_F(MinidumpWriterTest
, Write_FailsWithSubdirInCorrectPath
) {
115 MinidumpWriter
writer(&fake_generator_
,
116 dumplog_file_
.Append("subdir/logfile").value(),
118 base::Bind(&FakeDumpState
));
119 ASSERT_EQ(-1, writer
.Write());
122 TEST_F(MinidumpWriterTest
, Write_FailsWhenTooManyDumpsPresent
) {
123 MinidumpWriter
writer(&fake_generator_
,
124 dumplog_file_
.value(),
126 base::Bind(&FakeDumpState
));
128 // Write dump logs to the lockfile.
129 size_t too_many_dumps
= SynchronizedMinidumpManager::kMaxLockfileDumps
+ 1;
130 for (size_t i
= 0; i
< too_many_dumps
; ++i
) {
131 scoped_ptr
<DumpInfo
> info(CreateDumpInfo(
134 "\"dump_time\" : \"2012-01-01 01:02:03\","
135 "\"dump\": \"dump_string\","
136 "\"uptime\": \"123456789\","
137 "\"logfile\": \"logfile.log\""
139 ASSERT_TRUE(info
->valid());
140 ASSERT_TRUE(AppendLockFile(*info
));
143 ASSERT_EQ(-1, writer
.Write());
146 TEST_F(MinidumpWriterTest
, Write_FailsWhenTooManyRecentDumpsPresent
) {
147 MinidumpWriter
writer(&fake_generator_
,
148 dumplog_file_
.value(),
150 base::Bind(&FakeDumpState
));
151 // Multiple iters to make sure period resets work correctly
152 for (int iter
= 0; iter
< 3; ++iter
) {
153 time_t now
= time(nullptr);
155 // Write dump logs to the lockfile.
156 size_t too_many_recent_dumps
=
157 SynchronizedMinidumpManager::kRatelimitPeriodMaxDumps
;
158 for (size_t i
= 0; i
< too_many_recent_dumps
; ++i
) {
159 ASSERT_EQ(0, writer
.Write());
161 // Clear dumps so we don't reach max dumps in lockfile
162 ASSERT_TRUE(ClearDumps(lockfile_path_
.value()));
165 // Should fail with too many dumps
166 ASSERT_EQ(-1, writer
.Write());
168 int64 period
= SynchronizedMinidumpManager::kRatelimitPeriodSeconds
;
170 // Half period shouldn't trigger reset
171 SetRatelimitPeriodStart(lockfile_path_
.value(), now
- period
/ 2);
172 ASSERT_EQ(-1, writer
.Write());
174 // Set period starting time to trigger a reset
175 SetRatelimitPeriodStart(lockfile_path_
.value(), now
- period
);
178 ASSERT_EQ(0, writer
.Write());
181 TEST_F(MinidumpWriterTest
, Write_SucceedsWhenDumpLimitsNotExceeded
) {
182 MinidumpWriter
writer(&fake_generator_
,
183 dumplog_file_
.value(),
185 base::Bind(&FakeDumpState
));
187 ASSERT_GT(SynchronizedMinidumpManager::kMaxLockfileDumps
, 1);
188 ASSERT_GT(SynchronizedMinidumpManager::kRatelimitPeriodMaxDumps
, 0);
190 // Write an old dump logs to the lockfile.
191 scoped_ptr
<DumpInfo
> info(CreateDumpInfo(
194 "\"dump_time\" : \"2012-01-01 01:02:03\","
195 "\"dump\": \"dump_string\","
196 "\"uptime\": \"123456789\","
197 "\"logfile\": \"logfile.log\""
199 ASSERT_TRUE(info
->valid());
200 ASSERT_TRUE(AppendLockFile(*info
));
201 ASSERT_EQ(0, writer
.Write());
204 } // namespace chromecast