blob: 88d4afe73cd0cefaffcdd3e4d0fe14046a697d2a [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
41TEST_F(MemoryLogTest, ScopedLoggerStillWorks) {
42 ScopedMockLog log;
Christopher Wileyd7783522012-08-10 14:21:47 -070043 EXPECT_CALL(log, Log(_, _, kTestStr1));
44 EXPECT_CALL(log, Log(_, _, kTestStr2));
Christopher Wileyb691efd2012-08-09 13:51:51 -070045 SLOG(WiFi, 2) << "does not get through";
Christopher Wiley5781aa42012-07-30 14:42:23 -070046 ScopeLogger::GetInstance()->EnableScopesByName("+wifi");
47 // Verbose levels are inverted.
48 ScopeLogger::GetInstance()->set_verbose_level(3);
Christopher Wileyb691efd2012-08-09 13:51:51 -070049 SLOG(WiFi, 2) << kTestStr1;
Christopher Wiley5781aa42012-07-30 14:42:23 -070050 // It would be nice if the compiler didn't optimize out my conditional check.
Christopher Wileyb691efd2012-08-09 13:51:51 -070051 SLOG_IF(WiFi, 3, strlen("two") == 3) << kTestStr2;
52 SLOG_IF(WiFi, 3, strlen("one") == 2) << "does not get through again";
53 SLOG(WiFi, 4) << "spanish inquisition";
Christopher Wiley5781aa42012-07-30 14:42:23 -070054}
55
56TEST_F(MemoryLogTest, NormalLoggingStillWorks) {
57 ::logging::SetMinLogLevel(logging::LOG_WARNING);
58 ScopedMockLog log;
Christopher Wileyd7783522012-08-10 14:21:47 -070059 EXPECT_CALL(log, Log(_, _, kTestStr1));
60 EXPECT_CALL(log, Log(_, _, kTestStr2));
61 LOG(ERROR) << kTestStr1;
62 LOG(INFO) << "does not propagate down";
Christopher Wiley5781aa42012-07-30 14:42:23 -070063 // It would be nice if the compiler didn't optimize out my conditional check.
Christopher Wileyd7783522012-08-10 14:21:47 -070064 LOG_IF(WARNING, strlen("two") == 3) << kTestStr2;
Christopher Wiley5781aa42012-07-30 14:42:23 -070065}
66
67TEST_F(MemoryLogTest, MemoryLogIsLogging) {
68 ScopedMockLog log;
Christopher Wileyd7783522012-08-10 14:21:47 -070069 EXPECT_CALL(log, Log(_, _, kTestStr1));
70 EXPECT_CALL(log, Log(_, _, kTestStr2));
Christopher Wiley5781aa42012-07-30 14:42:23 -070071 ::logging::SetMinLogLevel(logging::LOG_WARNING);
72 ASSERT_EQ(0, MemoryLog::GetInstance()->current_size_bytes());
Christopher Wileyd7783522012-08-10 14:21:47 -070073 LOG(WARNING) << kTestStr1;
74 LOG(WARNING) << kTestStr2;
Christopher Wiley5781aa42012-07-30 14:42:23 -070075 // LT because of the prefixes prepended by the logger
Christopher Wileyd7783522012-08-10 14:21:47 -070076 EXPECT_LT(strlen(kTestStr1) + strlen(kTestStr2),
Christopher Wiley5781aa42012-07-30 14:42:23 -070077 MemoryLog::GetInstance()->current_size_bytes());
Christopher Wileyd7783522012-08-10 14:21:47 -070078 EXPECT_EQ(2, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wiley5781aa42012-07-30 14:42:23 -070079 MemoryLog::GetInstance()->Clear();
Christopher Wileyd7783522012-08-10 14:21:47 -070080 EXPECT_EQ(0, MemoryLog::GetInstance()->current_size_bytes());
81 EXPECT_EQ(0, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wiley5781aa42012-07-30 14:42:23 -070082}
83
84TEST_F(MemoryLogTest, MemoryLogLimitingWorks) {
85 ScopedMockLog log;
86 ::logging::SetMinLogLevel(logging::LOG_WARNING);
Christopher Wileyd7783522012-08-10 14:21:47 -070087 LOG(INFO) << kTestStr1;
Christopher Wiley5781aa42012-07-30 14:42:23 -070088 size_t old_size = MemoryLog::GetInstance()->current_size_bytes();
Christopher Wileyd7783522012-08-10 14:21:47 -070089 LOG(INFO) << kTestStr2;
Christopher Wiley5781aa42012-07-30 14:42:23 -070090 size_t new_size = MemoryLog::GetInstance()->current_size_bytes();
91 // Setting the size just above the current size shouldn't affect anything.
92 MemoryLog::GetInstance()->SetMaximumSize(new_size + 1);
93 ASSERT_EQ(new_size, MemoryLog::GetInstance()->current_size_bytes());
94 // Force MemoryLog to discard the earliest message
95 MemoryLog::GetInstance()->SetMaximumSize(new_size - 1);
96 // Should be just last message in the buffer.
97 ASSERT_EQ(new_size - old_size,
98 MemoryLog::GetInstance()->current_size_bytes());
99 // Now force it to discard the most recent message.
100 MemoryLog::GetInstance()->SetMaximumSize(0);
101 ASSERT_EQ(0, MemoryLog::GetInstance()->current_size_bytes());
102 // Can't log if we don't have room, but the messages should still get to LOG
Christopher Wileyd7783522012-08-10 14:21:47 -0700103 EXPECT_CALL(log, Log(_, _, kTestStr3));
104 LOG(WARNING) << kTestStr3;
Christopher Wiley5781aa42012-07-30 14:42:23 -0700105 ASSERT_EQ(0, MemoryLog::GetInstance()->current_size_bytes());
106 ASSERT_EQ(0, MemoryLog::GetInstance()->TestGetNumberMessages());
107}
108
109TEST_F(MemoryLogTest, MemoryLogFlushToDiskWorks) {
110 ScopedTempDir temp_dir;
111 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
112 FilePath test_path = temp_dir.path().Append("somelogfile");
113 ::logging::SetMinLogLevel(logging::LOG_WARNING);
Christopher Wileyd7783522012-08-10 14:21:47 -0700114 LOG(INFO) << kTestStr1;
115 LOG(INFO) << kTestStr2;
116 LOG(INFO) << kTestStr3;
117 EXPECT_EQ(3, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wiley5781aa42012-07-30 14:42:23 -0700118 // Because of all the prefixed metadata on each log message, the stuff sent to
119 // disk should be bigger than the original strings put together.
Christopher Wileyd7783522012-08-10 14:21:47 -0700120 size_t minimal_message_length = strlen(kTestStr1) + strlen(kTestStr2) +
121 strlen(kTestStr3);
122 EXPECT_LT(minimal_message_length,
Christopher Wiley5781aa42012-07-30 14:42:23 -0700123 MemoryLog::GetInstance()->FlushToDisk(test_path.value().c_str()));
124 std::string file_contents;
Christopher Wileyd7783522012-08-10 14:21:47 -0700125 EXPECT_TRUE(file_util::ReadFileToString(test_path, &file_contents));
Christopher Wiley5781aa42012-07-30 14:42:23 -0700126 // Log should contain all three messages
Christopher Wileyd7783522012-08-10 14:21:47 -0700127 EXPECT_NE(file_contents.find(kTestStr1), std::string::npos);
128 EXPECT_NE(file_contents.find(kTestStr2), std::string::npos);
129 EXPECT_NE(file_contents.find(kTestStr3), std::string::npos);
Christopher Wiley5781aa42012-07-30 14:42:23 -0700130 // Preserve message order
Christopher Wileyd7783522012-08-10 14:21:47 -0700131 EXPECT_LT(file_contents.find(kTestStr1), file_contents.find(kTestStr2));
132 EXPECT_LT(file_contents.find(kTestStr2), file_contents.find(kTestStr3));
133 EXPECT_TRUE(temp_dir.Delete());
Christopher Wiley5781aa42012-07-30 14:42:23 -0700134}
135
Christopher Wileyd7783522012-08-10 14:21:47 -0700136// Test that messages we're logging that don't go through the MessageLog still
137// end up in the message log when we have our handler installed.
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700138TEST_F(MemoryLogTest, MemoryLogMessageInterceptorWorks) {
139 ASSERT_EQ(0, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wileyd7783522012-08-10 14:21:47 -0700140 // Have to use this weird syntax because most normal macros wind up logging
141 // via LOG, thus getting sent through MemoryLog.
142 LOG_STREAM(ERROR) << "Doesn't get caught";
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700143 ASSERT_EQ(0, MemoryLog::GetInstance()->TestGetNumberMessages());
144 MemoryLog::InstallLogInterceptor();
Christopher Wileyd7783522012-08-10 14:21:47 -0700145 LOG_STREAM(ERROR) << "Caught this one!";
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700146 ASSERT_EQ(1, MemoryLog::GetInstance()->TestGetNumberMessages());
147 // Make sure we're not double logging.
Christopher Wileyd7783522012-08-10 14:21:47 -0700148 LOG(ERROR) << kTestStr1;
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700149 ASSERT_EQ(2, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wileyb691efd2012-08-09 13:51:51 -0700150 // SLOG_IF works with the intercepting handler.
151 SLOG_IF(WiFi, 3, strlen("two") == 3) << kTestStr2;
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700152 ASSERT_EQ(3, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wileyb691efd2012-08-09 13:51:51 -0700153 SLOG_IF(WiFi, 3, strlen("one") == 2) << "does not get through again";
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700154 ASSERT_EQ(3, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wileyd7783522012-08-10 14:21:47 -0700155 // Similarly, LOG_IF works with the handler.
156 LOG_IF(ERROR, strlen("two") == 3) << kTestStr2;
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700157 ASSERT_EQ(4, MemoryLog::GetInstance()->TestGetNumberMessages());
Christopher Wileyd7783522012-08-10 14:21:47 -0700158 LOG_IF(ERROR, strlen("one") == 2) << "does not get through again";
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700159 ASSERT_EQ(4, MemoryLog::GetInstance()->TestGetNumberMessages());
160 MemoryLog::UninstallLogInterceptor();
Christopher Wileyd7783522012-08-10 14:21:47 -0700161 LOG_STREAM(ERROR) << "Doesn't get caught";
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700162 ASSERT_EQ(4, MemoryLog::GetInstance()->TestGetNumberMessages());
163 // Normal log messages still get through.
Christopher Wileyd7783522012-08-10 14:21:47 -0700164 LOG(ERROR) << kTestStr2;
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700165 ASSERT_EQ(5, MemoryLog::GetInstance()->TestGetNumberMessages());
166}
167
Christopher Wiley5781aa42012-07-30 14:42:23 -0700168} // namespace shill