1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
// 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 <fstream>
#include "base/base_paths.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/test/scoped_path_override.h"
#include "chromecast/base/serializers.h"
#include "chromecast/crash/linux/dump_info.h"
#include "chromecast/crash/linux/minidump_generator.h"
#include "chromecast/crash/linux/minidump_writer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromecast {
namespace {
const char kDumplogFile[] = "dumplog";
const char kLockfileName[] = "lockfile";
const char kMinidumpSubdir[] = "minidumps";
const char kDumpsKey[] = "dumps";
class FakeMinidumpGenerator : public MinidumpGenerator {
public:
FakeMinidumpGenerator() {}
~FakeMinidumpGenerator() override {}
// MinidumpGenerator implementation:
bool Generate(const std::string& minidump_path) override { return true; }
};
int FakeDumpState(const std::string& minidump_path) {
return 0;
}
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()));
}
} // namespace
class MinidumpWriterTest : public testing::Test {
protected:
MinidumpWriterTest() {}
~MinidumpWriterTest() override {}
void SetUp() override {
// Set up a temporary directory which will be used as our fake home dir.
base::FilePath fake_home_dir;
ASSERT_TRUE(base::CreateNewTempDirectory("", &fake_home_dir));
home_.reset(new base::ScopedPathOverride(base::DIR_HOME, fake_home_dir));
minidump_dir_ = fake_home_dir.Append(kMinidumpSubdir);
dumplog_file_ = minidump_dir_.Append(kDumplogFile);
lockfile_path_ = minidump_dir_.Append(kLockfileName);
// Create the minidump directory and lockfile.
ASSERT_TRUE(base::CreateDirectory(minidump_dir_));
base::File lockfile(
lockfile_path_,
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
ASSERT_TRUE(lockfile.IsValid());
}
int AppendLockFile(const DumpInfo& dump) {
scoped_ptr<base::Value> contents(DeserializeJsonFromFile(lockfile_path_));
if (!contents) {
base::DictionaryValue* dict = new base::DictionaryValue();
contents = make_scoped_ptr(dict);
dict->Set(kDumpsKey, make_scoped_ptr(new base::ListValue()));
}
base::DictionaryValue* dict;
base::ListValue* dump_list;
if (!contents || !contents->GetAsDictionary(&dict) ||
!dict->GetList(kDumpsKey, &dump_list) || !dump_list) {
return -1;
}
dump_list->Append(dump.GetAsValue());
return SerializeJsonToFile(lockfile_path_, *contents) ? 0 : -1;
}
FakeMinidumpGenerator fake_generator_;
base::FilePath minidump_dir_;
base::FilePath dumplog_file_;
base::FilePath lockfile_path_;
private:
scoped_ptr<base::ScopedPathOverride> home_;
DISALLOW_COPY_AND_ASSIGN(MinidumpWriterTest);
};
TEST_F(MinidumpWriterTest, Write_FailsWithIncorrectMinidumpPath) {
MinidumpWriter writer(&fake_generator_,
"/path/to/wrong/dir",
MinidumpParams(),
base::Bind(&FakeDumpState));
ASSERT_EQ(-1, writer.Write());
}
TEST_F(MinidumpWriterTest, Write_FailsWithMultiLevelRelativeMinidumpPath) {
MinidumpWriter writer(&fake_generator_,
"subdir/dumplog",
MinidumpParams(),
base::Bind(&FakeDumpState));
ASSERT_EQ(-1, writer.Write());
}
TEST_F(MinidumpWriterTest, Write_SucceedsWithSimpleFilename) {
MinidumpWriter writer(&fake_generator_,
"dumplog",
MinidumpParams(),
base::Bind(&FakeDumpState));
ASSERT_EQ(0, writer.Write());
}
TEST_F(MinidumpWriterTest, Write_SucceedsWithCorrectMinidumpPath) {
MinidumpWriter writer(&fake_generator_,
dumplog_file_.value(),
MinidumpParams(),
base::Bind(&FakeDumpState));
ASSERT_EQ(0, writer.Write());
}
TEST_F(MinidumpWriterTest, Write_FailsWithSubdirInCorrectPath) {
MinidumpWriter writer(&fake_generator_,
dumplog_file_.Append("subdir/logfile").value(),
MinidumpParams(),
base::Bind(&FakeDumpState));
ASSERT_EQ(-1, writer.Write());
}
TEST_F(MinidumpWriterTest, Write_FailsWhenTooManyDumpsPresent) {
MinidumpWriter writer(&fake_generator_,
dumplog_file_.value(),
MinidumpParams(),
base::Bind(&FakeDumpState));
// Write dump logs to the lockfile.
size_t too_many_dumps = writer.max_dumps() + 1;
for (size_t i = 0; i < too_many_dumps; ++i) {
scoped_ptr<DumpInfo> info(CreateDumpInfo(
"{"
"\"name\": \"p\","
"\"dump_time\" : \"2012-01-01 01:02:03\","
"\"dump\": \"dump_string\","
"\"uptime\": \"123456789\","
"\"logfile\": \"logfile.log\""
"}"));
ASSERT_TRUE(info->valid());
ASSERT_EQ(0, AppendLockFile(*info));
}
ASSERT_EQ(-1, writer.Write());
}
TEST_F(MinidumpWriterTest, Write_FailsWhenTooManyRecentDumpsPresent) {
MinidumpWriter writer(&fake_generator_,
dumplog_file_.value(),
MinidumpParams(),
base::Bind(&FakeDumpState));
// Write dump logs to the lockfile.
size_t too_many_recent_dumps = writer.max_recent_dumps() + 1;
for (size_t i = 0; i < too_many_recent_dumps; ++i) {
MinidumpParams params;
DumpInfo info("dump", "/dump/path", time(nullptr), params);
ASSERT_EQ(0, AppendLockFile(info));
}
ASSERT_EQ(-1, writer.Write());
}
TEST_F(MinidumpWriterTest, Write_SucceedsWhenDumpLimitsNotExceeded) {
MinidumpWriter writer(&fake_generator_,
dumplog_file_.value(),
MinidumpParams(),
base::Bind(&FakeDumpState));
ASSERT_GT(writer.max_dumps(), 1);
ASSERT_GT(writer.max_recent_dumps(), 0);
// Write an old dump logs to the lockfile.
scoped_ptr<DumpInfo> info(CreateDumpInfo(
"{"
"\"name\": \"p\","
"\"dump_time\" : \"2012-01-01 01:02:03\","
"\"dump\": \"dump_string\","
"\"uptime\": \"123456789\","
"\"logfile\": \"logfile.log\""
"}"));
ASSERT_TRUE(info->valid());
ASSERT_EQ(0, AppendLockFile(*info));
ASSERT_EQ(0, writer.Write());
}
} // namespace chromecast
|