• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 #include "log_capture.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 
25 #include "common/init_flags.h"
26 #include "os/log.h"
27 
28 namespace {
29 const char* test_flags[] = {
30     "INIT_logging_debug_enabled_for_all=true",
31     nullptr,
32 };
33 
34 constexpr char kEmptyLine[] = "";
35 constexpr char kLogError[] = "LOG_ERROR";
36 constexpr char kLogWarn[] = "LOG_WARN";
37 constexpr char kLogInfo[] = "LOG_INFO";
38 constexpr char kLogDebug[] = "LOG_DEBUG";
39 constexpr char kLogVerbose[] = "LOG_VERBOSE";
40 
41 }  // namespace
42 
43 namespace bluetooth {
44 namespace testing {
45 
46 class LogCaptureTest : public ::testing::Test {
47  protected:
SetUp()48   void SetUp() override {}
49 
TearDown()50   void TearDown() override {}
51 
52   // The line number is part of the log output and must be factored out
CalibrateOneLine(const char * log_line)53   size_t CalibrateOneLine(const char* log_line) {
54     LOG_INFO("%s", log_line);
55     return strlen(log_line);
56   }
57 };
58 
TEST_F(LogCaptureTest,no_output)59 TEST_F(LogCaptureTest, no_output) {
60   std::unique_ptr<LogCapture> log_capture = std::make_unique<LogCapture>();
61 
62   ASSERT_TRUE(log_capture->Size() == 0);
63 }
64 
TEST_F(LogCaptureTest,truncate)65 TEST_F(LogCaptureTest, truncate) {
66   std::unique_ptr<LogCapture> log_capture = std::make_unique<LogCapture>();
67 
68   CalibrateOneLine(kLogError);
69   size_t size = log_capture->Size();
70   ASSERT_TRUE(size > 0);
71 
72   log_capture->Reset();
73   ASSERT_EQ(0UL, log_capture->Size());
74 
75   CalibrateOneLine(kLogError);
76   ASSERT_EQ(size, log_capture->Size());
77 }
78 
TEST_F(LogCaptureTest,log_size)79 TEST_F(LogCaptureTest, log_size) {
80   std::unique_ptr<LogCapture> log_capture = std::make_unique<LogCapture>();
81 
82   CalibrateOneLine(kEmptyLine);
83   size_t empty_line_size = log_capture->Size();
84   log_capture->Reset();
85 
86   std::vector<std::string> log_lines = {
87       kLogError,
88       kLogWarn,
89       kLogInfo,
90   };
91 
92   size_t msg_size{0};
93   for (auto& log_line : log_lines) {
94     msg_size += CalibrateOneLine(log_line.c_str());
95   }
96 
97   ASSERT_EQ(empty_line_size * log_lines.size() + msg_size, log_capture->Size());
98 
99   ASSERT_TRUE(log_capture->Rewind()->Find(kLogError));
100   ASSERT_TRUE(log_capture->Rewind()->Find(kLogWarn));
101   ASSERT_TRUE(log_capture->Rewind()->Find(kLogInfo));
102 }
103 
TEST_F(LogCaptureTest,typical)104 TEST_F(LogCaptureTest, typical) {
105   std::unique_ptr<LogCapture> log_capture = std::make_unique<LogCapture>();
106 
107   LOG_ERROR("%s", kLogError);
108   LOG_WARN("%s", kLogWarn);
109   LOG_INFO("%s", kLogInfo);
110   LOG_DEBUG("%s", kLogDebug);
111   LOG_VERBOSE("%s", kLogVerbose);
112 
113   ASSERT_TRUE(log_capture->Rewind()->Find(kLogError));
114   ASSERT_TRUE(log_capture->Rewind()->Find(kLogWarn));
115   ASSERT_TRUE(log_capture->Rewind()->Find(kLogInfo));
116   ASSERT_FALSE(log_capture->Rewind()->Find(kLogDebug));
117   ASSERT_FALSE(log_capture->Rewind()->Find(kLogVerbose));
118 }
119 
TEST_F(LogCaptureTest,with_logging_debug_enabled_for_all)120 TEST_F(LogCaptureTest, with_logging_debug_enabled_for_all) {
121   bluetooth::common::InitFlags::Load(test_flags);
122   std::unique_ptr<LogCapture> log_capture = std::make_unique<LogCapture>();
123 
124   LOG_ERROR("%s", kLogError);
125   LOG_WARN("%s", kLogWarn);
126   LOG_INFO("%s", kLogInfo);
127   LOG_DEBUG("%s", kLogDebug);
128   LOG_VERBOSE("%s", kLogVerbose);
129 
130   ASSERT_TRUE(log_capture->Rewind()->Find(kLogError));
131   ASSERT_TRUE(log_capture->Rewind()->Find(kLogWarn));
132   ASSERT_TRUE(log_capture->Rewind()->Find(kLogInfo));
133   ASSERT_TRUE(log_capture->Rewind()->Find(kLogDebug));
134   ASSERT_TRUE(log_capture->Rewind()->Find(kLogVerbose));
135   bluetooth::common::InitFlags::Load(nullptr);
136 }
137 
TEST_F(LogCaptureTest,wait_until_log_contains)138 TEST_F(LogCaptureTest, wait_until_log_contains) {
139   bluetooth::common::InitFlags::Load(test_flags);
140   std::unique_ptr<LogCapture> log_capture = std::make_unique<LogCapture>();
141 
142   LOG_DEBUG("%s", kLogDebug);
143   std::promise<void> promise;
144   log_capture->WaitUntilLogContains(&promise, kLogDebug);
145   bluetooth::common::InitFlags::Load(nullptr);
146 }
147 
148 }  // namespace testing
149 }  // namespace bluetooth
150