Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 1 | // 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 Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 9 | #include <base/scoped_temp_dir.h> |
| 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 13 | #include "shill/logging.h" |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 14 | #include "shill/mock_log.h" |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 15 | |
| 16 | using testing::_; |
| 17 | |
| 18 | namespace { |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 19 | |
Christopher Wiley | f11cebb | 2012-08-08 12:22:20 -0700 | [diff] [blame] | 20 | const char kTestStr1[] = "What does Mr Wallace look like?"; |
| 21 | const char kTestStr2[] = "And now little man, I give the watch to you."; |
| 22 | const char kTestStr3[] = "This is a tasty burger!"; |
Christopher Wiley | f11cebb | 2012-08-08 12:22:20 -0700 | [diff] [blame] | 23 | |
| 24 | } // namespace |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 25 | |
| 26 | namespace shill { |
| 27 | |
| 28 | class 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 Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame^] | 41 | class MemoryLogDeathTest : public testing::Test { }; |
| 42 | |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 43 | TEST_F(MemoryLogTest, ScopedLoggerStillWorks) { |
| 44 | ScopedMockLog log; |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 45 | EXPECT_CALL(log, Log(_, _, kTestStr1)); |
| 46 | EXPECT_CALL(log, Log(_, _, kTestStr2)); |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 47 | SLOG(WiFi, 2) << "does not get through"; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 48 | ScopeLogger::GetInstance()->EnableScopesByName("+wifi"); |
| 49 | // Verbose levels are inverted. |
| 50 | ScopeLogger::GetInstance()->set_verbose_level(3); |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 51 | SLOG(WiFi, 2) << kTestStr1; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 52 | // It would be nice if the compiler didn't optimize out my conditional check. |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 53 | 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 Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | TEST_F(MemoryLogTest, NormalLoggingStillWorks) { |
| 59 | ::logging::SetMinLogLevel(logging::LOG_WARNING); |
| 60 | ScopedMockLog log; |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 61 | EXPECT_CALL(log, Log(_, _, kTestStr1)); |
| 62 | EXPECT_CALL(log, Log(_, _, kTestStr2)); |
| 63 | LOG(ERROR) << kTestStr1; |
| 64 | LOG(INFO) << "does not propagate down"; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 65 | // It would be nice if the compiler didn't optimize out my conditional check. |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 66 | LOG_IF(WARNING, strlen("two") == 3) << kTestStr2; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame^] | 69 | // Test that no matter what we did, CHECK still kills the process. |
| 70 | TEST_F(MemoryLogDeathTest, CheckLogsStillWork) { |
| 71 | EXPECT_DEATH( { CHECK(false) << "diediedie"; }, |
| 72 | "Check failed: false. diediedie"); |
| 73 | } |
| 74 | |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 75 | TEST_F(MemoryLogTest, MemoryLogIsLogging) { |
| 76 | ScopedMockLog log; |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 77 | EXPECT_CALL(log, Log(_, _, kTestStr1)); |
| 78 | EXPECT_CALL(log, Log(_, _, kTestStr2)); |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 79 | ::logging::SetMinLogLevel(logging::LOG_WARNING); |
| 80 | ASSERT_EQ(0, MemoryLog::GetInstance()->current_size_bytes()); |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 81 | LOG(WARNING) << kTestStr1; |
| 82 | LOG(WARNING) << kTestStr2; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 83 | // LT because of the prefixes prepended by the logger |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 84 | EXPECT_LT(strlen(kTestStr1) + strlen(kTestStr2), |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 85 | MemoryLog::GetInstance()->current_size_bytes()); |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 86 | EXPECT_EQ(2, MemoryLog::GetInstance()->TestGetNumberMessages()); |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 87 | MemoryLog::GetInstance()->Clear(); |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 88 | EXPECT_EQ(0, MemoryLog::GetInstance()->current_size_bytes()); |
| 89 | EXPECT_EQ(0, MemoryLog::GetInstance()->TestGetNumberMessages()); |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST_F(MemoryLogTest, MemoryLogLimitingWorks) { |
| 93 | ScopedMockLog log; |
| 94 | ::logging::SetMinLogLevel(logging::LOG_WARNING); |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 95 | LOG(INFO) << kTestStr1; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 96 | size_t old_size = MemoryLog::GetInstance()->current_size_bytes(); |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 97 | LOG(INFO) << kTestStr2; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 98 | 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 Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 111 | EXPECT_CALL(log, Log(_, _, kTestStr3)); |
| 112 | LOG(WARNING) << kTestStr3; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 113 | ASSERT_EQ(0, MemoryLog::GetInstance()->current_size_bytes()); |
| 114 | ASSERT_EQ(0, MemoryLog::GetInstance()->TestGetNumberMessages()); |
| 115 | } |
| 116 | |
| 117 | TEST_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 Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 122 | LOG(INFO) << kTestStr1; |
| 123 | LOG(INFO) << kTestStr2; |
| 124 | LOG(INFO) << kTestStr3; |
| 125 | EXPECT_EQ(3, MemoryLog::GetInstance()->TestGetNumberMessages()); |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 126 | // 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 Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 128 | size_t minimal_message_length = strlen(kTestStr1) + strlen(kTestStr2) + |
| 129 | strlen(kTestStr3); |
| 130 | EXPECT_LT(minimal_message_length, |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 131 | MemoryLog::GetInstance()->FlushToDisk(test_path.value().c_str())); |
| 132 | std::string file_contents; |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 133 | EXPECT_TRUE(file_util::ReadFileToString(test_path, &file_contents)); |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 134 | // Log should contain all three messages |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 135 | 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 Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 138 | // Preserve message order |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 139 | 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 Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame^] | 144 | // Test that most messages go through MemoryLog |
Christopher Wiley | f11cebb | 2012-08-08 12:22:20 -0700 | [diff] [blame] | 145 | TEST_F(MemoryLogTest, MemoryLogMessageInterceptorWorks) { |
| 146 | ASSERT_EQ(0, MemoryLog::GetInstance()->TestGetNumberMessages()); |
Christopher Wiley | f11cebb | 2012-08-08 12:22:20 -0700 | [diff] [blame] | 147 | // Make sure we're not double logging. |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 148 | LOG(ERROR) << kTestStr1; |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame^] | 149 | ASSERT_EQ(1, MemoryLog::GetInstance()->TestGetNumberMessages()); |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 150 | SLOG_IF(WiFi, 3, strlen("two") == 3) << kTestStr2; |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame^] | 151 | ASSERT_EQ(2, MemoryLog::GetInstance()->TestGetNumberMessages()); |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 152 | SLOG_IF(WiFi, 3, strlen("one") == 2) << "does not get through again"; |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame^] | 153 | ASSERT_EQ(2, MemoryLog::GetInstance()->TestGetNumberMessages()); |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 154 | LOG_IF(ERROR, strlen("two") == 3) << kTestStr2; |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame^] | 155 | ASSERT_EQ(3, MemoryLog::GetInstance()->TestGetNumberMessages()); |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 156 | LOG_IF(ERROR, strlen("one") == 2) << "does not get through again"; |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame^] | 157 | ASSERT_EQ(3, MemoryLog::GetInstance()->TestGetNumberMessages()); |
| 158 | NOTIMPLEMENTED(); |
Christopher Wiley | f11cebb | 2012-08-08 12:22:20 -0700 | [diff] [blame] | 159 | ASSERT_EQ(4, MemoryLog::GetInstance()->TestGetNumberMessages()); |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame^] | 160 | |
Christopher Wiley | f11cebb | 2012-08-08 12:22:20 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 163 | } // namespace shill |