blob: f91a1b544254afdb32d88dc5cca1d0a70d2f5098 [file] [log] [blame]
Tom Cherrye18346d2020-05-21 12:13:20 -07001/*
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"
28#include "LogWhiteBlackList.h"
Tom Cherry8f613462020-05-12 12:46:43 -070029#include "SimpleLogBuffer.h"
Tom Cherrye18346d2020-05-21 12:13:20 -070030
31struct LogMessage {
32 logger_entry entry;
33 std::string message;
34 bool regex_compare = false; // Only set for expected messages, when true 'message' should be
35 // interpretted as a regex.
36};
37
38// Compares the ordered list of expected and result, causing a test failure with appropriate
39// information on failure.
40void CompareLogMessages(const std::vector<LogMessage>& expected,
41 const std::vector<LogMessage>& result);
42// Sets hdr_size and len parameters appropriately.
43void FixupMessages(std::vector<LogMessage>* messages);
44
45class TestWriter : public LogWriter {
46 public:
47 TestWriter(std::vector<LogMessage>* msgs, bool* released)
Tom Cherryeb49b042020-05-28 21:03:43 -070048 : LogWriter(0, true), msgs_(msgs), released_(released) {}
Tom Cherrye18346d2020-05-21 12:13:20 -070049 bool Write(const logger_entry& entry, const char* message) override {
50 msgs_->emplace_back(LogMessage{entry, std::string(message, entry.len), false});
51 return true;
52 }
53
54 void Release() {
55 if (released_) *released_ = true;
56 }
57
58 std::string name() const override { return "test_writer"; }
59
60 private:
61 std::vector<LogMessage>* msgs_;
62 bool* released_;
63};
64
65class LogBufferTest : public testing::TestWithParam<std::string> {
66 protected:
67 void SetUp() override {
68 if (GetParam() == "chatty") {
69 log_buffer_.reset(new ChattyLogBuffer(&reader_list_, &tags_, &prune_, &stats_));
Tom Cherry8f613462020-05-12 12:46:43 -070070 } else if (GetParam() == "simple") {
71 log_buffer_.reset(new SimpleLogBuffer(&reader_list_, &tags_, &stats_));
Tom Cherrye18346d2020-05-21 12:13:20 -070072 } else {
73 FAIL() << "Unknown buffer type selected for test";
74 }
75 }
76
77 void LogMessages(const std::vector<LogMessage>& messages) {
78 for (auto& [entry, message, _] : messages) {
79 log_buffer_->Log(static_cast<log_id_t>(entry.lid), log_time(entry.sec, entry.nsec),
80 entry.uid, entry.pid, entry.tid, message.c_str(), message.size());
81 }
82 }
83
84 LogReaderList reader_list_;
85 LogTags tags_;
86 PruneList prune_;
87 LogStatistics stats_{false};
88 std::unique_ptr<LogBuffer> log_buffer_;
89};