Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | #include "ChattyLogBuffer.h" |
| 25 | #include "LogReaderList.h" |
| 26 | #include "LogStatistics.h" |
| 27 | #include "LogTags.h" |
Tom Cherry | 0efb410 | 2020-06-16 10:14:09 -0700 | [diff] [blame] | 28 | #include "PruneList.h" |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 29 | #include "SerializedLogBuffer.h" |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 30 | #include "SimpleLogBuffer.h" |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 31 | |
| 32 | struct LogMessage { |
| 33 | logger_entry entry; |
| 34 | std::string message; |
| 35 | bool regex_compare = false; // Only set for expected messages, when true 'message' should be |
| 36 | // interpretted as a regex. |
| 37 | }; |
| 38 | |
| 39 | // Compares the ordered list of expected and result, causing a test failure with appropriate |
| 40 | // information on failure. |
| 41 | void CompareLogMessages(const std::vector<LogMessage>& expected, |
| 42 | const std::vector<LogMessage>& result); |
| 43 | // Sets hdr_size and len parameters appropriately. |
| 44 | void FixupMessages(std::vector<LogMessage>* messages); |
| 45 | |
| 46 | class TestWriter : public LogWriter { |
| 47 | public: |
| 48 | TestWriter(std::vector<LogMessage>* msgs, bool* released) |
Tom Cherry | eb49b04 | 2020-05-28 21:03:43 -0700 | [diff] [blame] | 49 | : LogWriter(0, true), msgs_(msgs), released_(released) {} |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 50 | bool Write(const logger_entry& entry, const char* message) override { |
| 51 | msgs_->emplace_back(LogMessage{entry, std::string(message, entry.len), false}); |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | void Release() { |
| 56 | if (released_) *released_ = true; |
| 57 | } |
| 58 | |
| 59 | std::string name() const override { return "test_writer"; } |
| 60 | |
| 61 | private: |
| 62 | std::vector<LogMessage>* msgs_; |
| 63 | bool* released_; |
| 64 | }; |
| 65 | |
| 66 | class LogBufferTest : public testing::TestWithParam<std::string> { |
| 67 | protected: |
| 68 | void SetUp() override { |
| 69 | if (GetParam() == "chatty") { |
| 70 | log_buffer_.reset(new ChattyLogBuffer(&reader_list_, &tags_, &prune_, &stats_)); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 71 | } else if (GetParam() == "serialized") { |
| 72 | log_buffer_.reset(new SerializedLogBuffer(&reader_list_, &tags_, &stats_)); |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 73 | } else if (GetParam() == "simple") { |
| 74 | log_buffer_.reset(new SimpleLogBuffer(&reader_list_, &tags_, &stats_)); |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 75 | } else { |
| 76 | FAIL() << "Unknown buffer type selected for test"; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void LogMessages(const std::vector<LogMessage>& messages) { |
| 81 | for (auto& [entry, message, _] : messages) { |
| 82 | log_buffer_->Log(static_cast<log_id_t>(entry.lid), log_time(entry.sec, entry.nsec), |
| 83 | entry.uid, entry.pid, entry.tid, message.c_str(), message.size()); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | LogReaderList reader_list_; |
| 88 | LogTags tags_; |
| 89 | PruneList prune_; |
Tom Cherry | f74503d | 2020-06-19 12:21:21 -0700 | [diff] [blame] | 90 | LogStatistics stats_{false, true}; |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 91 | std::unique_ptr<LogBuffer> log_buffer_; |
| 92 | }; |