Add ICU message format support
[chromium-blink-merge.git] / chromecast / crash / linux / minidump_writer_unittest.cc
blobc359e313e0069801f6c94b5392dab65685c9d659
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.
5 #include <fstream>
7 #include "base/base_paths.h"
8 #include "base/bind.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 {
20 namespace {
22 const char kDumplogFile[] = "dumplog";
23 const char kLockfileName[] = "lockfile";
24 const char kMinidumpSubdir[] = "minidumps";
26 class FakeMinidumpGenerator : public MinidumpGenerator {
27 public:
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) {
36 return 0;
39 } // namespace
41 class MinidumpWriterTest : public testing::Test {
42 protected:
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_));
57 base::File lockfile(
58 lockfile_path_,
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_;
72 private:
73 scoped_ptr<base::ScopedPathOverride> home_;
75 DISALLOW_COPY_AND_ASSIGN(MinidumpWriterTest);
78 TEST_F(MinidumpWriterTest, Write_FailsWithIncorrectMinidumpPath) {
79 MinidumpWriter writer(&fake_generator_,
80 "/path/to/wrong/dir",
81 MinidumpParams(),
82 base::Bind(&FakeDumpState));
84 ASSERT_EQ(-1, writer.Write());
87 TEST_F(MinidumpWriterTest, Write_FailsWithMultiLevelRelativeMinidumpPath) {
88 MinidumpWriter writer(&fake_generator_,
89 "subdir/dumplog",
90 MinidumpParams(),
91 base::Bind(&FakeDumpState));
93 ASSERT_EQ(-1, writer.Write());
96 TEST_F(MinidumpWriterTest, Write_SucceedsWithSimpleFilename) {
97 MinidumpWriter writer(&fake_generator_,
98 "dumplog",
99 MinidumpParams(),
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(),
108 MinidumpParams(),
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(),
117 MinidumpParams(),
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(),
125 MinidumpParams(),
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(
133 "\"name\": \"p\","
134 "\"dump_time\" : \"2012-01-01 01:02:03\","
135 "\"dump\": \"dump_string\","
136 "\"uptime\": \"123456789\","
137 "\"logfile\": \"logfile.log\""
138 "}"));
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(),
149 MinidumpParams(),
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(),
184 MinidumpParams(),
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(
193 "\"name\": \"p\","
194 "\"dump_time\" : \"2012-01-01 01:02:03\","
195 "\"dump\": \"dump_string\","
196 "\"uptime\": \"123456789\","
197 "\"logfile\": \"logfile.log\""
198 "}"));
199 ASSERT_TRUE(info->valid());
200 ASSERT_TRUE(AppendLockFile(*info));
201 ASSERT_EQ(0, writer.Write());
204 } // namespace chromecast