blob: ed8fff7565fae308047c60904e923abce1b88899 [file] [log] [blame]
Ken Mixter03403162010-08-18 15:23:16 -07001// Copyright (c) 2010 The Chromium OS 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.
4
5#include <unistd.h>
6
7#include "base/file_util.h"
Ken Mixter04ec10f2010-08-26 16:02:02 -07008#include "base/string_util.h"
Ken Mixter03403162010-08-18 15:23:16 -07009#include "crash-reporter/crash_collector.h"
10#include "crash-reporter/system_logging_mock.h"
11#include "gflags/gflags.h"
12#include "gtest/gtest.h"
13
14void CountCrash() {
15 ADD_FAILURE();
16}
17
18bool IsMetrics() {
19 ADD_FAILURE();
20 return false;
21}
22
23class CrashCollectorTest : public ::testing::Test {
Ken Mixter04ec10f2010-08-26 16:02:02 -070024 public:
Ken Mixter03403162010-08-18 15:23:16 -070025 void SetUp() {
26 collector_.Initialize(CountCrash,
27 IsMetrics,
28 &logging_);
Ken Mixter04ec10f2010-08-26 16:02:02 -070029 test_dir_ = FilePath("test");
30 file_util::CreateDirectory(test_dir_);
Ken Mixter03403162010-08-18 15:23:16 -070031 }
Ken Mixter04ec10f2010-08-26 16:02:02 -070032
33 void TearDown() {
34 file_util::Delete(test_dir_, true);
35 }
36
37 bool CheckHasCapacity();
38
Ken Mixter03403162010-08-18 15:23:16 -070039 protected:
40 SystemLoggingMock logging_;
41 CrashCollector collector_;
Ken Mixter04ec10f2010-08-26 16:02:02 -070042 FilePath test_dir_;
Ken Mixter03403162010-08-18 15:23:16 -070043};
44
45TEST_F(CrashCollectorTest, Initialize) {
46 ASSERT_TRUE(CountCrash == collector_.count_crash_function_);
47 ASSERT_TRUE(IsMetrics == collector_.is_feedback_allowed_function_);
48 ASSERT_TRUE(&logging_ == collector_.logger_);
49}
50
51TEST_F(CrashCollectorTest, GetCrashDirectoryInfo) {
52 FilePath path;
53 const int kRootUid = 0;
54 const int kRootGid = 0;
55 const int kNtpUid = 5;
56 const int kChronosUid = 1000;
57 const int kChronosGid = 1001;
58 const mode_t kExpectedSystemMode = 01755;
59 const mode_t kExpectedUserMode = 0755;
60
61 mode_t directory_mode;
62 uid_t directory_owner;
63 gid_t directory_group;
64
65 path = collector_.GetCrashDirectoryInfo(kRootUid,
66 kChronosUid,
67 kChronosGid,
68 &directory_mode,
69 &directory_owner,
70 &directory_group);
71 EXPECT_EQ("/var/spool/crash", path.value());
72 EXPECT_EQ(kExpectedSystemMode, directory_mode);
73 EXPECT_EQ(kRootUid, directory_owner);
74 EXPECT_EQ(kRootGid, directory_group);
75
76 path = collector_.GetCrashDirectoryInfo(kNtpUid,
77 kChronosUid,
78 kChronosGid,
79 &directory_mode,
80 &directory_owner,
81 &directory_group);
82 EXPECT_EQ("/var/spool/crash", path.value());
83 EXPECT_EQ(kExpectedSystemMode, directory_mode);
84 EXPECT_EQ(kRootUid, directory_owner);
85 EXPECT_EQ(kRootGid, directory_group);
86
87 path = collector_.GetCrashDirectoryInfo(kChronosUid,
88 kChronosUid,
89 kChronosGid,
90 &directory_mode,
91 &directory_owner,
92 &directory_group);
93 EXPECT_EQ("/home/chronos/user/crash", path.value());
94 EXPECT_EQ(kExpectedUserMode, directory_mode);
95 EXPECT_EQ(kChronosUid, directory_owner);
96 EXPECT_EQ(kChronosGid, directory_group);
97}
98
99TEST_F(CrashCollectorTest, FormatDumpBasename) {
100 struct tm tm = {0};
101 tm.tm_sec = 15;
102 tm.tm_min = 50;
103 tm.tm_hour = 13;
104 tm.tm_mday = 23;
105 tm.tm_mon = 4;
106 tm.tm_year = 110;
107 tm.tm_isdst = -1;
108 std::string basename =
109 collector_.FormatDumpBasename("foo", mktime(&tm), 100);
110 ASSERT_EQ("foo.20100523.135015.100", basename);
111}
112
Ken Mixter04ec10f2010-08-26 16:02:02 -0700113bool CrashCollectorTest::CheckHasCapacity() {
114 static const char kFullMessage[] = "Crash directory test already full";
115 bool has_capacity = collector_.CheckHasCapacity(test_dir_);
116 bool has_message = (logging_.log().find(kFullMessage) != std::string::npos);
117 EXPECT_EQ(has_message, !has_capacity);
118 return has_capacity;
119}
120
121TEST_F(CrashCollectorTest, CheckHasCapacityOverNonCore) {
122 // Test up to kMaxCrashDirectorySize-1 non-core files can be added.
123 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 1; ++i) {
124 EXPECT_TRUE(CheckHasCapacity());
125 file_util::WriteFile(test_dir_.Append(StringPrintf("file%d", i)), "", 0);
126 }
127
128 // Test an additional kMaxCrashDirectorySize - 1 core files fit.
129 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 1; ++i) {
130 EXPECT_TRUE(CheckHasCapacity());
131 file_util::WriteFile(test_dir_.Append(StringPrintf("file%d.core", i)),
132 "", 0);
133 }
134
135 // Test an additional kMaxCrashDirectorySize non-core files don't fit.
136 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize; ++i) {
137 file_util::WriteFile(test_dir_.Append(StringPrintf("overage%d", i)), "", 0);
138 EXPECT_FALSE(CheckHasCapacity());
139 }
140}
141
142TEST_F(CrashCollectorTest, CheckHasCapacityOverCore) {
143 // Set up kMaxCrashDirectorySize - 1 core files.
144 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 1; ++i) {
145 file_util::WriteFile(test_dir_.Append(StringPrintf("file%d.core", i)),
146 "", 0);
147 }
148
149 EXPECT_TRUE(CheckHasCapacity());
150
151 // Test an additional core file does not fit.
152 file_util::WriteFile(test_dir_.Append("overage.core"), "", 0);
153 EXPECT_FALSE(CheckHasCapacity());
154}
155
Ken Mixter03403162010-08-18 15:23:16 -0700156int main(int argc, char **argv) {
157 ::testing::InitGoogleTest(&argc, argv);
158 return RUN_ALL_TESTS();
159}