1 /*
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 // LOG_TAG must be unset for android-base's logging to use a default tag.
18 #undef LOG_TAG
19 
20 #include <stdlib.h>
21 
22 #include <android-base/file.h>
23 #include <android-base/logging.h>
24 #include <android-base/properties.h>
25 #include <android-base/scopeguard.h>
26 #include <android/log.h>
27 
28 #include <gtest/gtest.h>
29 
30 #ifndef __ANDROID__
getprogname()31 static const char* getprogname() {
32   return program_invocation_short_name;
33 }
34 #endif
35 
TEST(liblog_default_tag,no_default_tag_libbase_write_first)36 TEST(liblog_default_tag, no_default_tag_libbase_write_first) {
37   using namespace android::base;
38   bool message_seen = false;
39   std::string expected_tag = "";
40   SetLogger([&](LogId, LogSeverity, const char* tag, const char*, unsigned int, const char*) {
41     message_seen = true;
42     EXPECT_EQ(expected_tag, tag);
43   });
44 
45   expected_tag = getprogname();
46   LOG(WARNING) << "message";
47   EXPECT_TRUE(message_seen);
48   message_seen = false;
49 
50   __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_WARN, nullptr, "message");
51   EXPECT_TRUE(message_seen);
52 }
53 
TEST(liblog_default_tag,no_default_tag_liblog_write_first)54 TEST(liblog_default_tag, no_default_tag_liblog_write_first) {
55   using namespace android::base;
56   bool message_seen = false;
57   std::string expected_tag = "";
58   SetLogger([&](LogId, LogSeverity, const char* tag, const char*, unsigned int, const char*) {
59     message_seen = true;
60     EXPECT_EQ(expected_tag, tag);
61   });
62 
63   expected_tag = getprogname();
64   __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_WARN, nullptr, "message");
65   EXPECT_TRUE(message_seen);
66   message_seen = false;
67 
68   LOG(WARNING) << "message";
69   EXPECT_TRUE(message_seen);
70 }
71 
TEST(liblog_default_tag,libbase_sets_default_tag)72 TEST(liblog_default_tag, libbase_sets_default_tag) {
73   using namespace android::base;
74   bool message_seen = false;
75   std::string expected_tag = "libbase_test_tag";
76   SetLogger([&](LogId, LogSeverity, const char* tag, const char*, unsigned int, const char*) {
77     message_seen = true;
78     EXPECT_EQ(expected_tag, tag);
79   });
80   SetDefaultTag(expected_tag);
81 
82   __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_WARN, nullptr, "message");
83   EXPECT_TRUE(message_seen);
84   message_seen = false;
85 
86   LOG(WARNING) << "message";
87   EXPECT_TRUE(message_seen);
88 }
89 
TEST(liblog_default_tag,liblog_sets_default_tag)90 TEST(liblog_default_tag, liblog_sets_default_tag) {
91   using namespace android::base;
92   bool message_seen = false;
93   std::string expected_tag = "liblog_test_tag";
94   SetLogger([&](LogId, LogSeverity, const char* tag, const char*, unsigned int, const char*) {
95     message_seen = true;
96     EXPECT_EQ(expected_tag, tag);
97   });
98   __android_log_set_default_tag(expected_tag.c_str());
99 
100   __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_WARN, nullptr, "message");
101   EXPECT_TRUE(message_seen);
102   message_seen = false;
103 
104   LOG(WARNING) << "message";
105   EXPECT_TRUE(message_seen);
106 }
107 
TEST(liblog_default_tag,default_tag_plus_log_severity)108 TEST(liblog_default_tag, default_tag_plus_log_severity) {
109 #ifdef __ANDROID__
110   using namespace android::base;
111   bool message_seen = false;
112   std::string expected_tag = "liblog_test_tag";
113   SetLogger([&](LogId, LogSeverity, const char* tag, const char*, unsigned int, const char*) {
114     message_seen = true;
115     EXPECT_EQ(expected_tag, tag);
116   });
117   __android_log_set_default_tag(expected_tag.c_str());
118 
119   auto log_tag_property = "log.tag." + expected_tag;
120   SetProperty(log_tag_property, "V");
121   auto reset_tag_property_guard = make_scope_guard([=] { SetProperty(log_tag_property, ""); });
122 
123   __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_VERBOSE, nullptr, "message");
124   EXPECT_TRUE(message_seen);
125   message_seen = false;
126 
127   LOG(VERBOSE) << "message";
128   EXPECT_TRUE(message_seen);
129 #else
130   GTEST_SKIP() << "No log tag properties on host";
131 #endif
132 }
133 
TEST(liblog_default_tag,generated_default_tag_plus_log_severity)134 TEST(liblog_default_tag, generated_default_tag_plus_log_severity) {
135 #ifdef __ANDROID__
136   using namespace android::base;
137   bool message_seen = false;
138   std::string expected_tag = getprogname();
139   SetLogger([&](LogId, LogSeverity, const char* tag, const char*, unsigned int, const char*) {
140     message_seen = true;
141     EXPECT_EQ(expected_tag, tag);
142   });
143 
144   // Even without any calls to SetDefaultTag(), the first message that attempts to log, will
145   // generate a default tag from getprogname() and check log.tag.<default tag> for loggability. This
146   // case checks that we can log a Verbose message when log.tag.<getprogname()> is set to 'V'.
147   auto log_tag_property = "log.tag." + expected_tag;
148   SetProperty(log_tag_property, "V");
149   auto reset_tag_property_guard = make_scope_guard([=] { SetProperty(log_tag_property, ""); });
150 
151   __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_VERBOSE, nullptr, "message");
152   EXPECT_TRUE(message_seen);
153   message_seen = false;
154 
155   LOG(VERBOSE) << "message";
156   EXPECT_TRUE(message_seen);
157 #else
158   GTEST_SKIP() << "No log tag properties on host";
159 #endif
160 }