2015-10-07 17:01:47 -04:00
|
|
|
// Copyright 2015 The Crashpad Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "client/prune_crash_reports.h"
|
|
|
|
|
2017-04-19 20:10:33 -04:00
|
|
|
#include <stddef.h>
|
2015-10-07 17:01:47 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-04-19 20:10:33 -04:00
|
|
|
#include "base/numerics/safe_conversions.h"
|
2015-10-07 17:01:47 -04:00
|
|
|
#include "base/rand_util.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "test/scoped_temp_dir.h"
|
|
|
|
#include "util/file/file_io.h"
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
namespace test {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MockDatabase : public CrashReportDatabase {
|
|
|
|
public:
|
|
|
|
// CrashReportDatabase:
|
|
|
|
MOCK_METHOD0(GetSettings, Settings*());
|
2018-02-13 17:28:11 -08:00
|
|
|
MOCK_METHOD1(PrepareNewCrashReport,
|
|
|
|
OperationStatus(std::unique_ptr<NewReport>*));
|
2015-10-07 17:01:47 -04:00
|
|
|
MOCK_METHOD2(LookUpCrashReport, OperationStatus(const UUID&, Report*));
|
|
|
|
MOCK_METHOD1(GetPendingReports, OperationStatus(std::vector<Report>*));
|
|
|
|
MOCK_METHOD1(GetCompletedReports, OperationStatus(std::vector<Report>*));
|
2018-07-24 09:13:49 -07:00
|
|
|
MOCK_METHOD3(GetReportForUploading,
|
2018-02-15 08:17:12 -08:00
|
|
|
OperationStatus(const UUID&,
|
2018-07-24 09:13:49 -07:00
|
|
|
std::unique_ptr<const UploadReport>*,
|
|
|
|
bool report_metrics));
|
2015-10-07 17:01:47 -04:00
|
|
|
MOCK_METHOD3(RecordUploadAttempt,
|
2018-02-15 08:17:12 -08:00
|
|
|
OperationStatus(UploadReport*, bool, const std::string&));
|
2016-09-26 15:06:19 -07:00
|
|
|
MOCK_METHOD2(SkipReportUpload,
|
|
|
|
OperationStatus(const UUID&, Metrics::CrashSkippedReason));
|
2015-10-07 17:01:47 -04:00
|
|
|
MOCK_METHOD1(DeleteReport, OperationStatus(const UUID&));
|
2016-08-24 15:37:46 -04:00
|
|
|
MOCK_METHOD1(RequestUpload, OperationStatus(const UUID&));
|
2018-02-13 17:28:11 -08:00
|
|
|
|
|
|
|
// gmock doesn't support mocking methods with non-copyable types such as
|
|
|
|
// unique_ptr.
|
|
|
|
OperationStatus FinishedWritingCrashReport(std::unique_ptr<NewReport> report,
|
|
|
|
UUID* uuid) override {
|
|
|
|
return kNoError;
|
|
|
|
}
|
2015-10-07 17:01:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
time_t NDaysAgo(int num_days) {
|
|
|
|
return time(nullptr) - (num_days * 60 * 60 * 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PruneCrashReports, AgeCondition) {
|
|
|
|
CrashReportDatabase::Report report_80_days;
|
|
|
|
report_80_days.creation_time = NDaysAgo(80);
|
|
|
|
|
|
|
|
CrashReportDatabase::Report report_10_days;
|
|
|
|
report_10_days.creation_time = NDaysAgo(10);
|
|
|
|
|
|
|
|
CrashReportDatabase::Report report_30_days;
|
|
|
|
report_30_days.creation_time = NDaysAgo(30);
|
|
|
|
|
|
|
|
AgePruneCondition condition(30);
|
|
|
|
EXPECT_TRUE(condition.ShouldPruneReport(report_80_days));
|
|
|
|
EXPECT_FALSE(condition.ShouldPruneReport(report_10_days));
|
|
|
|
EXPECT_FALSE(condition.ShouldPruneReport(report_30_days));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PruneCrashReports, SizeCondition) {
|
|
|
|
ScopedTempDir temp_dir;
|
|
|
|
|
|
|
|
CrashReportDatabase::Report report_1k;
|
|
|
|
report_1k.file_path = temp_dir.path().Append(FILE_PATH_LITERAL("file1024"));
|
|
|
|
CrashReportDatabase::Report report_3k;
|
|
|
|
report_3k.file_path = temp_dir.path().Append(FILE_PATH_LITERAL("file3072"));
|
|
|
|
|
|
|
|
{
|
|
|
|
ScopedFileHandle scoped_file_1k(
|
|
|
|
LoggingOpenFileForWrite(report_1k.file_path,
|
|
|
|
FileWriteMode::kCreateOrFail,
|
|
|
|
FilePermissions::kOwnerOnly));
|
|
|
|
ASSERT_TRUE(scoped_file_1k.is_valid());
|
|
|
|
|
|
|
|
std::string string;
|
|
|
|
for (int i = 0; i < 128; ++i)
|
|
|
|
string.push_back(static_cast<char>(i));
|
|
|
|
|
2015-10-07 17:31:27 -04:00
|
|
|
for (size_t i = 0; i < 1024; i += string.size()) {
|
2015-10-07 17:01:47 -04:00
|
|
|
ASSERT_TRUE(LoggingWriteFile(scoped_file_1k.get(),
|
|
|
|
string.c_str(), string.length()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedFileHandle scoped_file_3k(
|
|
|
|
LoggingOpenFileForWrite(report_3k.file_path,
|
|
|
|
FileWriteMode::kCreateOrFail,
|
|
|
|
FilePermissions::kOwnerOnly));
|
|
|
|
ASSERT_TRUE(scoped_file_3k.is_valid());
|
|
|
|
|
2015-10-07 17:31:27 -04:00
|
|
|
for (size_t i = 0; i < 3072; i += string.size()) {
|
2015-10-07 17:01:47 -04:00
|
|
|
ASSERT_TRUE(LoggingWriteFile(scoped_file_3k.get(),
|
|
|
|
string.c_str(), string.length()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DatabaseSizePruneCondition condition(1);
|
|
|
|
EXPECT_FALSE(condition.ShouldPruneReport(report_1k));
|
|
|
|
EXPECT_TRUE(condition.ShouldPruneReport(report_1k));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DatabaseSizePruneCondition condition(1);
|
|
|
|
EXPECT_TRUE(condition.ShouldPruneReport(report_3k));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DatabaseSizePruneCondition condition(6);
|
|
|
|
EXPECT_FALSE(condition.ShouldPruneReport(report_3k));
|
|
|
|
EXPECT_FALSE(condition.ShouldPruneReport(report_3k));
|
|
|
|
EXPECT_TRUE(condition.ShouldPruneReport(report_1k));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class StaticCondition final : public PruneCondition {
|
|
|
|
public:
|
|
|
|
explicit StaticCondition(bool value) : value_(value), did_execute_(false) {}
|
|
|
|
~StaticCondition() {}
|
|
|
|
|
|
|
|
bool ShouldPruneReport(const CrashReportDatabase::Report& report) override {
|
|
|
|
did_execute_ = true;
|
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool did_execute() const { return did_execute_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const bool value_;
|
|
|
|
bool did_execute_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(StaticCondition);
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(PruneCrashReports, BinaryCondition) {
|
2017-07-25 13:34:04 -04:00
|
|
|
static constexpr struct {
|
2015-10-07 17:01:47 -04:00
|
|
|
const char* name;
|
|
|
|
BinaryPruneCondition::Operator op;
|
|
|
|
bool lhs_value;
|
|
|
|
bool rhs_value;
|
|
|
|
bool cond_result;
|
|
|
|
bool lhs_executed;
|
|
|
|
bool rhs_executed;
|
|
|
|
} kTests[] = {
|
|
|
|
{"false && false",
|
|
|
|
BinaryPruneCondition::AND, false, false,
|
|
|
|
false, true, false},
|
|
|
|
{"false && true",
|
|
|
|
BinaryPruneCondition::AND, false, true,
|
|
|
|
false, true, false},
|
|
|
|
{"true && false",
|
|
|
|
BinaryPruneCondition::AND, true, false,
|
|
|
|
false, true, true},
|
|
|
|
{"true && true",
|
|
|
|
BinaryPruneCondition::AND, true, true,
|
|
|
|
true, true, true},
|
|
|
|
{"false || false",
|
|
|
|
BinaryPruneCondition::OR, false, false,
|
|
|
|
false, true, true},
|
|
|
|
{"false || true",
|
|
|
|
BinaryPruneCondition::OR, false, true,
|
|
|
|
true, true, true},
|
|
|
|
{"true || false",
|
|
|
|
BinaryPruneCondition::OR, true, false,
|
|
|
|
true, true, false},
|
|
|
|
{"true || true",
|
|
|
|
BinaryPruneCondition::OR, true, true,
|
|
|
|
true, true, false},
|
|
|
|
};
|
|
|
|
for (const auto& test : kTests) {
|
|
|
|
SCOPED_TRACE(test.name);
|
|
|
|
auto lhs = new StaticCondition(test.lhs_value);
|
|
|
|
auto rhs = new StaticCondition(test.rhs_value);
|
|
|
|
BinaryPruneCondition condition(test.op, lhs, rhs);
|
|
|
|
CrashReportDatabase::Report report;
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
EXPECT_EQ(condition.ShouldPruneReport(report), test.cond_result);
|
|
|
|
EXPECT_EQ(lhs->did_execute(), test.lhs_executed);
|
|
|
|
EXPECT_EQ(rhs->did_execute(), test.rhs_executed);
|
2015-10-07 17:01:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MATCHER_P(TestUUID, data_1, "") {
|
|
|
|
return arg.data_1 == data_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PruneCrashReports, PruneOrder) {
|
|
|
|
using ::testing::_;
|
|
|
|
using ::testing::DoAll;
|
|
|
|
using ::testing::Return;
|
|
|
|
using ::testing::SetArgPointee;
|
|
|
|
|
|
|
|
std::vector<CrashReportDatabase::Report> reports;
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
CrashReportDatabase::Report temp;
|
|
|
|
temp.uuid.data_1 = i;
|
|
|
|
temp.creation_time = NDaysAgo(i * 10);
|
|
|
|
reports.push_back(temp);
|
|
|
|
}
|
|
|
|
// The randomness from std::rand() is not, so use a better rand() instead.
|
2017-04-19 20:10:33 -04:00
|
|
|
const auto random_generator = [](ptrdiff_t rand_max) {
|
|
|
|
return base::RandInt(0, base::checked_cast<int>(rand_max) - 1);
|
2015-10-14 16:56:04 -07:00
|
|
|
};
|
|
|
|
std::random_shuffle(reports.begin(), reports.end(), random_generator);
|
2015-10-07 17:01:47 -04:00
|
|
|
std::vector<CrashReportDatabase::Report> pending_reports(
|
|
|
|
reports.begin(), reports.begin() + 5);
|
|
|
|
std::vector<CrashReportDatabase::Report> completed_reports(
|
|
|
|
reports.begin() + 5, reports.end());
|
|
|
|
|
|
|
|
MockDatabase db;
|
|
|
|
EXPECT_CALL(db, GetPendingReports(_)).WillOnce(DoAll(
|
|
|
|
SetArgPointee<0>(pending_reports),
|
|
|
|
Return(CrashReportDatabase::kNoError)));
|
|
|
|
EXPECT_CALL(db, GetCompletedReports(_)).WillOnce(DoAll(
|
|
|
|
SetArgPointee<0>(completed_reports),
|
|
|
|
Return(CrashReportDatabase::kNoError)));
|
|
|
|
for (size_t i = 0; i < reports.size(); ++i) {
|
|
|
|
EXPECT_CALL(db, DeleteReport(TestUUID(i)))
|
|
|
|
.WillOnce(Return(CrashReportDatabase::kNoError));
|
|
|
|
}
|
|
|
|
|
|
|
|
StaticCondition delete_all(true);
|
|
|
|
PruneCrashReportDatabase(&db, &delete_all);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace test
|
|
|
|
} // namespace crashpad
|