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