blob: dab7a3f1155a4985c2437d01a4f9b638e59380f8 [file] [log] [blame]
Christopher Wiley5781aa42012-07-30 14:42:23 -07001// Copyright (c) 2012 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 <deque>
6#include <string>
7
8#include <base/file_util.h>
Christopher Wiley5781aa42012-07-30 14:42:23 -07009#include <base/scoped_temp_dir.h>
10#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
Christopher Wileyb691efd2012-08-09 13:51:51 -070013#include "shill/logging.h"
Christopher Wiley5781aa42012-07-30 14:42:23 -070014#include "shill/mock_log.h"
Christopher Wiley5781aa42012-07-30 14:42:23 -070015
16using testing::_;
17
18namespace {
Christopher Wiley5781aa42012-07-30 14:42:23 -070019
Christopher Wileyf11cebb2012-08-08 12:22:20 -070020const char kTestStr1[] = "What does Mr Wallace look like?";
21const char kTestStr2[] = "And now little man, I give the watch to you.";
22const char kTestStr3[] = "This is a tasty burger!";
Christopher Wileyf11cebb2012-08-08 12:22:20 -070023
24} // namespace
Christopher Wiley5781aa42012-07-30 14:42:23 -070025
26namespace shill {
27
28class MemoryLogTest : public testing::Test {
29 protected:
30 void TearDown() {
31 // Restore everything to defaults once more.
32 MemoryLog::GetInstance()->Clear();
33 MemoryLog::GetInstance()->SetMaximumSize(
34 MemoryLog::kDefaultMaximumMemoryLogSizeInBytes);
35 ScopeLogger::GetInstance()->set_verbose_level(0);
36 ScopeLogger::GetInstance()->EnableScopesByName("");
37 ::logging::SetMinLogLevel(logging::LOG_INFO);
38 }
39};
40
Christopher Wiley6e8c54b2012-08-15 16:12:30 -070041class MemoryLogDeathTest : public testing::Test { };
42
Christopher Wiley5781aa42012-07-30 14:42:23 -070043TEST_F(MemoryLogTest, ScopedLoggerStillWorks) {
44 ScopedMockLog log;
Christopher Wileyd7783522012-08-10 14:21:47 -070045 EXPECT_CALL(log, Log(_, _, kTestStr1));
46 EXPECT_CALL(log, Log(_, _, kTestStr2));
Christopher Wileyb691efd2012-08-09 13:51:51 -070047 SLOG(WiFi, 2) << "does not get through";
Christopher Wiley5781aa42012-07-30 14:42:23 -070048 ScopeLogger::GetInstance()->EnableScopesByName("+wifi");
49 // Verbose levels are inverted.
50 ScopeLogger::GetInstance()->set_verbose_level(3);
Christopher Wileyb691efd2012-08-09 13:51:51 -070051 SLOG(WiFi, 2) << kTestStr1;
Christopher Wiley5781aa42012-07-30 14:42:23 -070052 // It would be nice if the compiler didn't optimize out my conditional check.
Christopher Wileyb691efd2012-08-09 13:51:51 -070053 SLOG_IF(WiFi, 3, strlen("two") == 3) << kTestStr2;
54 SLOG_IF(WiFi, 3, strlen("one") == 2) << "does not get through again";
55 SLOG(WiFi, 4) << "spanish inquisition";
Christopher Wiley5781aa42012-07-30 14:42:23 -070056}
57
58TEST_F(MemoryLogTest, NormalLoggingStillWorks) {
59 ::logging::SetMinLogLevel(logging::LOG_WARNING);
60 ScopedMockLog log;
Christopher Wileyd7783522012-08-10 14:21:47 -070061 EXPECT_CALL(log, Log(_, _, kTestStr1));
62 EXPECT_CALL(log, Log(_, _, kTestStr2));
63 LOG(ERROR) << kTestStr1;
64 LOG(INFO) << "does not propagate down";
Christopher Wiley5781aa42012-07-30 14:42:23 -070065 // It would be nice if the compiler didn't optimize out my conditional check.
Christopher Wileyd7783522012-08-10 14:21:47 -070066 LOG_IF(WARNING, strlen("two") == 3) << kTestStr2;
Christopher Wiley5781aa42012-07-30 14:42:23 -070067}
68
Christopher Wiley6e8c54b2012-08-15 16:12:30 -070069// Test that no matter what we did, CHECK still kills the process.
70TEST_F(MemoryLogDeathTest, CheckLogsStillWork) {
71 EXPECT_DEATH( { CHECK(false) << "diediedie"; },
72 "Check failed: false. diediedie");
73}
74
Christopher Wiley5781aa42012-07-30 14:42:23 -070075TEST_F(MemoryLogTest, MemoryLogIsLogging) {
76 ScopedMockLog log;
Christopher Wileyd7783522012-08-10 14:21:47 -070077 EXPECT_CALL(log, Log(_, _, kTestStr1));
78 EXPECT_CALL(log, Log(_, _, kTestStr2));
Christopher Wiley5781aa42012-07-30 14:42:23 -070079 ::logging::SetMinLogLevel(logging::LOG_WARNING);
80 ASSERT_EQ(0, MemoryLog::GetInstance()->current_size_bytes());
Christopher Wileyd7783522012-08-10 14:21:47 -070081 LOG(WARNING) << kTestStr1;
82 LOG(WARNING) << kTestStr2;
Christopher Wiley5781aa42012-07-30 14:42:23 -070083 // LT because of the prefixes prepended by the logger
Christopher Wileyd7783522012-08-10 14:21:47 -070084 EXPECT_LT(strlen(kTestStr1) + strlen(kTestStr2),
Christopher Wiley5781aa42012-07-30 14:42:23 -070085 MemoryLog::GetInstance()->current_size_bytes());
Christopher Wileyd7783522012-08-10 14:21:47 -070086 EXPECT_EQ(2, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wiley5781aa42012-07-30 14:42:23 -070087 MemoryLog::GetInstance()->Clear();
Christopher Wileyd7783522012-08-10 14:21:47 -070088 EXPECT_EQ(0, MemoryLog::GetInstance()->current_size_bytes());
89 EXPECT_EQ(0, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wiley5781aa42012-07-30 14:42:23 -070090}
91
92TEST_F(MemoryLogTest, MemoryLogLimitingWorks) {
93 ScopedMockLog log;
94 ::logging::SetMinLogLevel(logging::LOG_WARNING);
Christopher Wileyd7783522012-08-10 14:21:47 -070095 LOG(INFO) << kTestStr1;
Christopher Wiley5781aa42012-07-30 14:42:23 -070096 size_t old_size = MemoryLog::GetInstance()->current_size_bytes();
Christopher Wileyd7783522012-08-10 14:21:47 -070097 LOG(INFO) << kTestStr2;
Christopher Wiley5781aa42012-07-30 14:42:23 -070098 size_t new_size = MemoryLog::GetInstance()->current_size_bytes();
99 // Setting the size just above the current size shouldn't affect anything.
100 MemoryLog::GetInstance()->SetMaximumSize(new_size + 1);
101 ASSERT_EQ(new_size, MemoryLog::GetInstance()->current_size_bytes());
102 // Force MemoryLog to discard the earliest message
103 MemoryLog::GetInstance()->SetMaximumSize(new_size - 1);
104 // Should be just last message in the buffer.
105 ASSERT_EQ(new_size - old_size,
106 MemoryLog::GetInstance()->current_size_bytes());
107 // Now force it to discard the most recent message.
108 MemoryLog::GetInstance()->SetMaximumSize(0);
109 ASSERT_EQ(0, MemoryLog::GetInstance()->current_size_bytes());
110 // Can't log if we don't have room, but the messages should still get to LOG
Christopher Wileyd7783522012-08-10 14:21:47 -0700111 EXPECT_CALL(log, Log(_, _, kTestStr3));
112 LOG(WARNING) << kTestStr3;
Christopher Wiley5781aa42012-07-30 14:42:23 -0700113 ASSERT_EQ(0, MemoryLog::GetInstance()->current_size_bytes());
114 ASSERT_EQ(0, MemoryLog::GetInstance()->TestGetNumberMessages());
115}
116
117TEST_F(MemoryLogTest, MemoryLogFlushToDiskWorks) {
118 ScopedTempDir temp_dir;
119 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
120 FilePath test_path = temp_dir.path().Append("somelogfile");
121 ::logging::SetMinLogLevel(logging::LOG_WARNING);
Christopher Wileyd7783522012-08-10 14:21:47 -0700122 LOG(INFO) << kTestStr1;
123 LOG(INFO) << kTestStr2;
124 LOG(INFO) << kTestStr3;
125 EXPECT_EQ(3, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wiley5781aa42012-07-30 14:42:23 -0700126 // Because of all the prefixed metadata on each log message, the stuff sent to
127 // disk should be bigger than the original strings put together.
Christopher Wileyd7783522012-08-10 14:21:47 -0700128 size_t minimal_message_length = strlen(kTestStr1) + strlen(kTestStr2) +
129 strlen(kTestStr3);
130 EXPECT_LT(minimal_message_length,
Christopher Wiley5781aa42012-07-30 14:42:23 -0700131 MemoryLog::GetInstance()->FlushToDisk(test_path.value().c_str()));
132 std::string file_contents;
Christopher Wileyd7783522012-08-10 14:21:47 -0700133 EXPECT_TRUE(file_util::ReadFileToString(test_path, &file_contents));
Christopher Wiley5781aa42012-07-30 14:42:23 -0700134 // Log should contain all three messages
Christopher Wileyd7783522012-08-10 14:21:47 -0700135 EXPECT_NE(file_contents.find(kTestStr1), std::string::npos);
136 EXPECT_NE(file_contents.find(kTestStr2), std::string::npos);
137 EXPECT_NE(file_contents.find(kTestStr3), std::string::npos);
Christopher Wiley5781aa42012-07-30 14:42:23 -0700138 // Preserve message order
Christopher Wileyd7783522012-08-10 14:21:47 -0700139 EXPECT_LT(file_contents.find(kTestStr1), file_contents.find(kTestStr2));
140 EXPECT_LT(file_contents.find(kTestStr2), file_contents.find(kTestStr3));
141 EXPECT_TRUE(temp_dir.Delete());
Christopher Wiley5781aa42012-07-30 14:42:23 -0700142}
143
Christopher Wiley6e8c54b2012-08-15 16:12:30 -0700144// Test that most messages go through MemoryLog
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700145TEST_F(MemoryLogTest, MemoryLogMessageInterceptorWorks) {
146 ASSERT_EQ(0, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700147 // Make sure we're not double logging.
Christopher Wileyd7783522012-08-10 14:21:47 -0700148 LOG(ERROR) << kTestStr1;
Christopher Wiley6e8c54b2012-08-15 16:12:30 -0700149 ASSERT_EQ(1, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wileyb691efd2012-08-09 13:51:51 -0700150 SLOG_IF(WiFi, 3, strlen("two") == 3) << kTestStr2;
Christopher Wiley6e8c54b2012-08-15 16:12:30 -0700151 ASSERT_EQ(2, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wileyb691efd2012-08-09 13:51:51 -0700152 SLOG_IF(WiFi, 3, strlen("one") == 2) << "does not get through again";
Christopher Wiley6e8c54b2012-08-15 16:12:30 -0700153 ASSERT_EQ(2, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wileyd7783522012-08-10 14:21:47 -0700154 LOG_IF(ERROR, strlen("two") == 3) << kTestStr2;
Christopher Wiley6e8c54b2012-08-15 16:12:30 -0700155 ASSERT_EQ(3, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wileyd7783522012-08-10 14:21:47 -0700156 LOG_IF(ERROR, strlen("one") == 2) << "does not get through again";
Christopher Wiley6e8c54b2012-08-15 16:12:30 -0700157 ASSERT_EQ(3, MemoryLog::GetInstance()->TestGetNumberMessages());
158 NOTIMPLEMENTED();
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700159 ASSERT_EQ(4, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wiley6e8c54b2012-08-15 16:12:30 -0700160
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700161}
162
Christopher Wiley5781aa42012-07-30 14:42:23 -0700163} // namespace shill