Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame^] | 1 | // 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 <sys/file.h> |
| 6 | |
| 7 | #include <base/eintr_wrapper.h> |
| 8 | #include <base/file_util.h> |
| 9 | #include <base/logging.h> |
| 10 | #include <base/string_util.h> |
| 11 | #include <gmock/gmock.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | |
| 14 | #include "counter.h" |
| 15 | |
| 16 | using ::testing::_; |
| 17 | using ::testing::MockFunction; |
| 18 | using ::testing::StrictMock; |
| 19 | |
| 20 | namespace chromeos_metrics { |
| 21 | |
| 22 | static const char kTestRecordFile[] = "record-file"; |
| 23 | static const char kDoesNotExistFile[] = "/does/not/exist"; |
| 24 | |
| 25 | class RecordTest : public testing::Test { |
| 26 | protected: |
| 27 | virtual void SetUp() { |
| 28 | EXPECT_EQ(0, record_.tag()); |
| 29 | EXPECT_EQ(0, record_.count()); |
| 30 | } |
| 31 | |
| 32 | // The record under test. |
| 33 | TaggedCounter::Record record_; |
| 34 | }; |
| 35 | |
| 36 | class TaggedCounterTest : public testing::Test { |
| 37 | protected: |
| 38 | virtual void SetUp() { |
| 39 | EXPECT_EQ(NULL, counter_.filename_); |
| 40 | EXPECT_TRUE(NULL == counter_.reporter_); |
| 41 | EXPECT_EQ(NULL, counter_.reporter_handle_); |
| 42 | EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_); |
| 43 | |
| 44 | counter_.Init(kTestRecordFile, &Reporter, this); |
| 45 | EXPECT_TRUE(AssertNoOrEmptyRecordFile()); |
| 46 | EXPECT_EQ(kTestRecordFile, counter_.filename_); |
| 47 | EXPECT_TRUE(Reporter == counter_.reporter_); |
| 48 | EXPECT_EQ(this, counter_.reporter_handle_); |
| 49 | EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_); |
| 50 | |
| 51 | // The test fixture object will be used by the log message handler. |
| 52 | test_ = this; |
| 53 | logging::SetLogMessageHandler(HandleLogMessages); |
| 54 | } |
| 55 | |
| 56 | virtual void TearDown() { |
| 57 | logging::SetLogMessageHandler(NULL); |
| 58 | test_ = NULL; |
| 59 | file_util::Delete(FilePath(kTestRecordFile), false); |
| 60 | } |
| 61 | |
| 62 | // Asserts that the record file contains the specified contents. |
| 63 | testing::AssertionResult AssertRecord(const char* expr_tag, |
| 64 | const char* expr_count, |
| 65 | int expected_tag, |
| 66 | int expected_count) { |
| 67 | int fd = HANDLE_EINTR(open(kTestRecordFile, O_RDONLY)); |
| 68 | if (fd < 0) { |
| 69 | testing::Message msg; |
| 70 | msg << "Unable to open " << kTestRecordFile; |
| 71 | return testing::AssertionFailure(msg); |
| 72 | } |
| 73 | |
| 74 | TaggedCounter::Record record; |
| 75 | if (!file_util::ReadFromFD(fd, reinterpret_cast<char*>(&record), |
| 76 | sizeof(record))) { |
| 77 | testing::Message msg; |
| 78 | msg << "Unable to read " << sizeof(record) << " bytes from " |
| 79 | << kTestRecordFile; |
| 80 | HANDLE_EINTR(close(fd)); |
| 81 | return testing::AssertionFailure(msg); |
| 82 | } |
| 83 | |
| 84 | if (record.tag() != expected_tag || record.count() != expected_count) { |
| 85 | testing::Message msg; |
| 86 | msg << "actual record (" << record.tag() << ", " << record.count() |
| 87 | << ") expected (" << expected_tag << ", " << expected_count << ")"; |
| 88 | HANDLE_EINTR(close(fd)); |
| 89 | return testing::AssertionFailure(msg); |
| 90 | } |
| 91 | |
| 92 | HANDLE_EINTR(close(fd)); |
| 93 | return testing::AssertionSuccess(); |
| 94 | } |
| 95 | |
| 96 | // Returns true if the persistent record file does not exist or is |
| 97 | // empty, false otherwise. |
| 98 | bool AssertNoOrEmptyRecordFile() { |
| 99 | FilePath record_file(counter_.filename_); |
| 100 | int64 record_file_size; |
| 101 | return !file_util::PathExists(record_file) || |
| 102 | (file_util::GetFileSize(record_file, &record_file_size) && |
| 103 | record_file_size == 0); |
| 104 | } |
| 105 | |
| 106 | // Adds a reporter call expectation that the specified tag/count |
| 107 | // callback will be generated. |
| 108 | void ExpectReporterCall(int tag, int count) { |
| 109 | EXPECT_CALL(reporter_, Call(_, tag, count)) |
| 110 | .Times(1) |
| 111 | .RetiresOnSaturation(); |
| 112 | } |
| 113 | |
| 114 | // The reporter callback forwards the call to the reporter mock so |
| 115 | // that we can set call expectations. |
| 116 | static void Reporter(void* handle, int tag, int count) { |
| 117 | TaggedCounterTest* test = static_cast<TaggedCounterTest*>(handle); |
| 118 | ASSERT_FALSE(NULL == test); |
| 119 | test->reporter_.Call(handle, tag, count); |
| 120 | } |
| 121 | |
| 122 | // Collects log messages in the |log_| member string so that they |
| 123 | // can be analyzed for errors and expected behavior. |
| 124 | static bool HandleLogMessages(int severity, const std::string& str) { |
| 125 | test_->log_.append(str); |
| 126 | test_->log_.append("\n"); |
| 127 | |
| 128 | // Returning true would mute the log. |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | // Returns true if the counter log contains |pattern|, false otherwise. |
| 133 | bool LogContains(const std::string& pattern) { |
| 134 | return log_.find(pattern) != std::string::npos; |
| 135 | } |
| 136 | |
| 137 | // The TaggedCounter object under test. |
| 138 | TaggedCounter counter_; |
| 139 | |
| 140 | // The accumulated counter log. |
| 141 | std::string log_; |
| 142 | |
| 143 | // Reporter mock to set callback expectations on. |
| 144 | StrictMock<MockFunction<void(void* handle, int tag, int count)> > reporter_; |
| 145 | |
| 146 | // Pointer to the current test fixture. |
| 147 | static TaggedCounterTest* test_; |
| 148 | }; |
| 149 | |
| 150 | // static |
| 151 | TaggedCounterTest* TaggedCounterTest::test_ = NULL; |
| 152 | |
| 153 | TEST_F(RecordTest, Init) { |
| 154 | record_.Init(/* tag */ 5, /* count */ -1); |
| 155 | EXPECT_EQ(5, record_.tag()); |
| 156 | EXPECT_EQ(0, record_.count()); |
| 157 | |
| 158 | record_.Init(/* tag */ -2, /* count */ 10); |
| 159 | EXPECT_EQ(-2, record_.tag()); |
| 160 | EXPECT_EQ(10, record_.count()); |
| 161 | } |
| 162 | |
| 163 | TEST_F(RecordTest, Add) { |
| 164 | record_.Add(/* count */ -1); |
| 165 | EXPECT_EQ(0, record_.count()); |
| 166 | |
| 167 | record_.Add(/* count */ 5); |
| 168 | EXPECT_EQ(5, record_.count()); |
| 169 | |
| 170 | record_.Add(/* count */ 10); |
| 171 | EXPECT_EQ(15, record_.count()); |
| 172 | |
| 173 | record_.Add(/* count */ -2); |
| 174 | EXPECT_EQ(15, record_.count()); |
| 175 | |
| 176 | record_.Add(/* count */ INT_MAX); |
| 177 | EXPECT_EQ(INT_MAX, record_.count()); |
| 178 | |
| 179 | record_.Add(/* count */ 1); |
| 180 | EXPECT_EQ(INT_MAX, record_.count()); |
| 181 | } |
| 182 | |
| 183 | TEST_F(TaggedCounterTest, BadFileLocation) { |
| 184 | // Checks that the counter doesn't die badly if the file can't be |
| 185 | // created. |
| 186 | counter_.Init(kDoesNotExistFile, |
| 187 | /* reporter */ NULL, /* reporter_handle */ NULL); |
| 188 | counter_.Update(/* tag */ 10, /* count */ 20); |
| 189 | EXPECT_TRUE(LogContains("Unable to open the persistent counter file: " |
| 190 | "No such file or directory")); |
| 191 | EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_); |
| 192 | file_util::Delete(FilePath(kDoesNotExistFile), false); |
| 193 | } |
| 194 | |
| 195 | TEST_F(TaggedCounterTest, Flush) { |
| 196 | counter_.Flush(); |
| 197 | EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_); |
| 198 | |
| 199 | counter_.Update(/* tag */ 40, /* count */ 60); |
| 200 | ExpectReporterCall(/* tag */ 40, /* count */ 60); |
| 201 | counter_.Flush(); |
| 202 | EXPECT_TRUE(AssertNoOrEmptyRecordFile()); |
| 203 | EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_); |
| 204 | |
| 205 | counter_.Update(/* tag */ 41, /* count */ 70); |
| 206 | counter_.record_.Init(/* tag */ 0, /* count */ 0); |
| 207 | counter_.record_state_ = TaggedCounter::kRecordInvalid; |
| 208 | ExpectReporterCall(/* tag */ 41, /* count */ 70); |
| 209 | counter_.Flush(); |
| 210 | EXPECT_TRUE(AssertNoOrEmptyRecordFile()); |
| 211 | EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_); |
| 212 | } |
| 213 | |
| 214 | TEST_F(TaggedCounterTest, InitFromFile) { |
| 215 | counter_.Update(/* tag */ 30, /* count */ 50); |
| 216 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 30, /* seconds */ 50); |
| 217 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
| 218 | |
| 219 | counter_.Init(kTestRecordFile, &Reporter, this); |
| 220 | counter_.Update(/* tag */ 30, /* count */ 40); |
| 221 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 30, /* seconds */ 90); |
| 222 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
| 223 | |
| 224 | counter_.Init(kTestRecordFile, &Reporter, this); |
| 225 | ExpectReporterCall(/* tag */ 30, /* count */ 90); |
| 226 | counter_.Update(/* tag */ 31, /* count */ 60); |
| 227 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 31, /* seconds */ 60); |
| 228 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
| 229 | |
| 230 | ExpectReporterCall(/* tag */ 31, /* count */ 60); |
| 231 | counter_.Init(kTestRecordFile, &Reporter, this); |
| 232 | counter_.Update(/* tag */ 32, /* count */ 0); |
| 233 | EXPECT_TRUE(AssertNoOrEmptyRecordFile()); |
| 234 | EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_); |
| 235 | } |
| 236 | |
| 237 | TEST_F(TaggedCounterTest, Update) { |
| 238 | counter_.Update(/* tag */ 20, /* count */ 30); |
| 239 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 20, /* seconds */ 30); |
| 240 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
| 241 | |
| 242 | counter_.Update(/* tag */ 20, /* count */ 40); |
| 243 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 20, /* seconds */ 70); |
| 244 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
| 245 | |
| 246 | ExpectReporterCall(/* tag */ 20, /* count */ 70); |
| 247 | counter_.Update(/* tag */ 21, /* count */ 15); |
| 248 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 21, /* seconds */ 15); |
| 249 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
| 250 | |
| 251 | ExpectReporterCall(/* tag */ 21, /* count */ 15); |
| 252 | counter_.Update(/* tag */ 22, /* count */ 0); |
| 253 | EXPECT_TRUE(AssertNoOrEmptyRecordFile()); |
| 254 | EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_); |
| 255 | } |
| 256 | |
| 257 | } // namespace chromeos_metrics |
| 258 | |
| 259 | int main(int argc, char** argv) { |
| 260 | testing::InitGoogleTest(&argc, argv); |
| 261 | return RUN_ALL_TESTS(); |
| 262 | } |