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