1 /*
2 * Copyright 2023 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 #define LOG_TAG "test"
18
19 #include <gtest/gtest.h>
20 #include <log/log.h>
21
22 #include "bluetooth/log.h"
23 #include "truncating_buffer.h"
24
25 /// Captures the latest message generated by the android vlog
26 /// implementation.
27 static std::optional<__android_log_message> androidLogMessage;
28
29 /// Mask the implementation from liblog.
__android_log_is_loggable(int,const char *,int)30 int __android_log_is_loggable(int /*prio*/, const char* /*tag*/, int /*default_prio*/) {
31 return true;
32 }
33
34 /// Mask the implementation from liblog.
__android_log_write_log_message(struct __android_log_message * log_message)35 void __android_log_write_log_message(struct __android_log_message* log_message) {
36 if (log_message != nullptr) {
37 log_message->message = strdup(log_message->message);
38 androidLogMessage.emplace(*log_message);
39 }
40 }
41
42 using namespace bluetooth;
43
TEST(BluetoothLogTest,verbose)44 TEST(BluetoothLogTest, verbose) {
45 androidLogMessage.reset();
46
47 log::verbose("verbose test");
48
49 ASSERT_TRUE(androidLogMessage.has_value());
50 EXPECT_EQ(androidLogMessage->priority, ANDROID_LOG_VERBOSE);
51 EXPECT_STREQ(androidLogMessage->tag, LOG_TAG);
52 EXPECT_EQ(androidLogMessage->file, nullptr);
53 EXPECT_EQ(androidLogMessage->line, 0);
54 EXPECT_STREQ(androidLogMessage->message, "system/log/src/vlog_test.cc:47 TestBody: verbose test");
55 }
56
TEST(BluetoothLogTest,debug)57 TEST(BluetoothLogTest, debug) {
58 androidLogMessage.reset();
59
60 log::debug("debug test");
61
62 ASSERT_TRUE(androidLogMessage.has_value());
63 EXPECT_EQ(androidLogMessage->priority, ANDROID_LOG_DEBUG);
64 EXPECT_STREQ(androidLogMessage->tag, LOG_TAG);
65 EXPECT_STREQ(androidLogMessage->file, nullptr);
66 EXPECT_EQ(androidLogMessage->line, 0);
67 EXPECT_STREQ(androidLogMessage->message, "system/log/src/vlog_test.cc:60 TestBody: debug test");
68 }
69
TEST(BluetoothLogTest,info)70 TEST(BluetoothLogTest, info) {
71 androidLogMessage.reset();
72
73 log::info("info test");
74
75 ASSERT_TRUE(androidLogMessage.has_value());
76 EXPECT_EQ(androidLogMessage->priority, ANDROID_LOG_INFO);
77 EXPECT_STREQ(androidLogMessage->tag, LOG_TAG);
78 EXPECT_STREQ(androidLogMessage->file, nullptr);
79 EXPECT_EQ(androidLogMessage->line, 0);
80 EXPECT_STREQ(androidLogMessage->message, "system/log/src/vlog_test.cc:73 TestBody: info test");
81 }
82
TEST(BluetoothLogTest,warn)83 TEST(BluetoothLogTest, warn) {
84 androidLogMessage.reset();
85
86 log::warn("warn test");
87
88 ASSERT_TRUE(androidLogMessage.has_value());
89 EXPECT_EQ(androidLogMessage->priority, ANDROID_LOG_WARN);
90 EXPECT_STREQ(androidLogMessage->tag, LOG_TAG);
91 EXPECT_STREQ(androidLogMessage->file, nullptr);
92 EXPECT_EQ(androidLogMessage->line, 0);
93 EXPECT_STREQ(androidLogMessage->message, "system/log/src/vlog_test.cc:86 TestBody: warn test");
94 }
95
TEST(BluetoothLogTest,error)96 TEST(BluetoothLogTest, error) {
97 androidLogMessage.reset();
98
99 log::error("error test");
100
101 ASSERT_TRUE(androidLogMessage.has_value());
102 EXPECT_EQ(androidLogMessage->priority, ANDROID_LOG_ERROR);
103 EXPECT_STREQ(androidLogMessage->tag, LOG_TAG);
104 EXPECT_STREQ(androidLogMessage->file, nullptr);
105 EXPECT_EQ(androidLogMessage->line, 0);
106 EXPECT_STREQ(androidLogMessage->message, "system/log/src/vlog_test.cc:99 TestBody: error test");
107 }
108
TEST(BluetoothLogDeathTest,fatal)109 TEST(BluetoothLogDeathTest, fatal) {
110 androidLogMessage.reset();
111
112 ASSERT_DEATH(
113 {
114 log::fatal("fatal test");
115 // Validate that the compiler is correctly handling log::fatal as
116 // [[noreturn]] by attempting to invoke an undefined function.
117 // This test will fail linking if this check fails.
118 void undefined_function();
119 undefined_function();
120 },
121 "fatal test");
122
123 ASSERT_DEATH(
124 {
125 log::fatal("fatal test {}", "2");
126 void undefined_function();
127 undefined_function();
128 },
129 "fatal test 2");
130
131 ASSERT_DEATH(
132 {
133 log::fatal("fatal test {}, {}", 2, 3);
134 void undefined_function();
135 undefined_function();
136 },
137 "fatal test 2, 3");
138 }
139
TEST(BluetoothLogDeathTest,assert_that)140 TEST(BluetoothLogDeathTest, assert_that) {
141 androidLogMessage.reset();
142
143 log::assert_that(true, "assert_that test true");
144 log::assert_that(true, "assert_that test {}", "true");
145
146 ASSERT_DEATH({ log::assert_that(false, "assert_that test false"); }, "assert_that test false");
147 }
148
TEST(BluetoothLogTest,null_string_parameter)149 TEST(BluetoothLogTest, null_string_parameter) {
150 androidLogMessage.reset();
151
152 char const* const_null_str = nullptr;
153 log::info("input: {}", const_null_str);
154 EXPECT_STREQ(androidLogMessage->message,
155 "system/log/src/vlog_test.cc:153 TestBody: input: (nullptr)");
156
157 androidLogMessage.reset();
158
159 char* null_str = nullptr;
160 log::info("input: {}", null_str);
161 EXPECT_STREQ(androidLogMessage->message,
162 "system/log/src/vlog_test.cc:160 TestBody: input: (nullptr)");
163
164 androidLogMessage.reset();
165
166 char const* nonnull_str = "hello world";
167 log::info("input: {}", nonnull_str);
168 EXPECT_STREQ(androidLogMessage->message,
169 "system/log/src/vlog_test.cc:167 TestBody: input: hello world");
170 }
171