• 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 
65 // b/260917913
TEST_F(LogCaptureTest,DISABLED_truncate)66 TEST_F(LogCaptureTest, DISABLED_truncate) {
67   std::unique_ptr<LogCapture> log_capture = std::make_unique<LogCapture>();
68 
69   CalibrateOneLine(kLogError);
70   size_t size = log_capture->Size();
71   ASSERT_TRUE(size > 0);
72 
73   log_capture->Reset();
74   ASSERT_EQ(0UL, log_capture->Size());
75 
76   CalibrateOneLine(kLogError);
77   ASSERT_EQ(size, log_capture->Size());
78 }
79 
80 // b/260917913
TEST_F(LogCaptureTest,DISABLED_log_size)81 TEST_F(LogCaptureTest, DISABLED_log_size) {
82   std::unique_ptr<LogCapture> log_capture = std::make_unique<LogCapture>();
83 
84   CalibrateOneLine(kEmptyLine);
85   size_t empty_line_size = log_capture->Size();
86   log_capture->Reset();
87 
88   std::vector<std::string> log_lines = {
89       kLogError,
90       kLogWarn,
91       kLogInfo,
92   };
93 
94   size_t msg_size{0};
95   for (auto& log_line : log_lines) {
96     msg_size += CalibrateOneLine(log_line.c_str());
97   }
98 
99   ASSERT_EQ(empty_line_size * log_lines.size() + msg_size, log_capture->Size());
100 
101   ASSERT_TRUE(log_capture->Rewind()->Find(kLogError));
102   ASSERT_TRUE(log_capture->Rewind()->Find(kLogWarn));
103   ASSERT_TRUE(log_capture->Rewind()->Find(kLogInfo));
104 }
105 
106 // b/260917913
TEST_F(LogCaptureTest,DISABLED_typical)107 TEST_F(LogCaptureTest, DISABLED_typical) {
108   bluetooth::common::InitFlags::Load(nullptr);
109   std::unique_ptr<LogCapture> log_capture = std::make_unique<LogCapture>();
110 
111   LOG_ERROR("%s", kLogError);
112   LOG_WARN("%s", kLogWarn);
113   LOG_INFO("%s", kLogInfo);
114   LOG_DEBUG("%s", kLogDebug);
115   LOG_VERBOSE("%s", kLogVerbose);
116 
117   ASSERT_TRUE(log_capture->Rewind()->Find(kLogError));
118   ASSERT_TRUE(log_capture->Rewind()->Find(kLogWarn));
119   ASSERT_TRUE(log_capture->Rewind()->Find(kLogInfo));
120   ASSERT_FALSE(log_capture->Rewind()->Find(kLogDebug));
121   ASSERT_FALSE(log_capture->Rewind()->Find(kLogVerbose));
122 }
123 
124 // b/260917913
TEST_F(LogCaptureTest,DISABLED_with_logging_debug_enabled_for_all)125 TEST_F(LogCaptureTest, DISABLED_with_logging_debug_enabled_for_all) {
126   bluetooth::common::InitFlags::Load(test_flags);
127   std::unique_ptr<LogCapture> log_capture = std::make_unique<LogCapture>();
128 
129   LOG_ERROR("%s", kLogError);
130   LOG_WARN("%s", kLogWarn);
131   LOG_INFO("%s", kLogInfo);
132   LOG_DEBUG("%s", kLogDebug);
133   LOG_VERBOSE("%s", kLogVerbose);
134 
135   ASSERT_TRUE(log_capture->Rewind()->Find(kLogError));
136   ASSERT_TRUE(log_capture->Rewind()->Find(kLogWarn));
137   ASSERT_TRUE(log_capture->Rewind()->Find(kLogInfo));
138   ASSERT_TRUE(log_capture->Rewind()->Find(kLogDebug));
139   ASSERT_TRUE(log_capture->Rewind()->Find(kLogVerbose));
140   bluetooth::common::InitFlags::Load(nullptr);
141 }
142 
143 // b/260917913
TEST_F(LogCaptureTest,DISABLED_wait_until_log_contains)144 TEST_F(LogCaptureTest, DISABLED_wait_until_log_contains) {
145   bluetooth::common::InitFlags::Load(test_flags);
146   std::unique_ptr<LogCapture> log_capture = std::make_unique<LogCapture>();
147 
148   LOG_DEBUG("%s", kLogDebug);
149   std::promise<void> promise;
150   log_capture->WaitUntilLogContains(&promise, kLogDebug);
151   bluetooth::common::InitFlags::Load(nullptr);
152 }
153 
154 }  // namespace testing
155 }  // namespace bluetooth
156