1 /*
2 * Copyright (C) 2019 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 "MainFileTag"
18
19 #include <android-base/logging.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "LegacyUtils.h"
24
25 namespace log_tag_test {
26
27 // In an extra file with different log tag
28 void generateLogOtherTag();
29
generateLog()30 bool generateLog() {
31 NN_RET_CHECK_FAIL() << "Forcing failure to validate log tag";
32 }
33
34 class LogTagTest : public ::testing::Test {
35 protected:
~LogTagTest()36 ~LogTagTest() override { ::android::base::SetLogger(::android::base::LogdLogger()); }
37 };
38
TEST_F(LogTagTest,NnRetCheckFailMacroReturnsFalse)39 TEST_F(LogTagTest, NnRetCheckFailMacroReturnsFalse) {
40 EXPECT_FALSE(generateLog());
41 }
42
TEST_F(LogTagTest,EachFileLogTagIsCaptured)43 TEST_F(LogTagTest, EachFileLogTagIsCaptured) {
44 android::base::SetLogger([](android::base::LogId /* logId */,
45 android::base::LogSeverity /* logSeverity */, const char* tag,
46 const char* /*file*/, unsigned int /*line*/,
47 const char* /*message*/) {
48 EXPECT_STREQ(tag, "MainFileTag") << "Tag for this file has not been used";
49 });
50 generateLog();
51
52 android::base::SetLogger([](android::base::LogId /* logId */,
53 android::base::LogSeverity /* logSeverity */, const char* tag,
54 const char* /*file*/, unsigned int /*line*/,
55 const char* /*message*/) {
56 EXPECT_STREQ(tag, "SecondFileTag") << "Tag for the second test file has not been used";
57 });
58 generateLogOtherTag();
59 }
60
TEST_F(LogTagTest,LogIsAtErrorLevel)61 TEST_F(LogTagTest, LogIsAtErrorLevel) {
62 android::base::SetLogger(
63 [](android::base::LogId /* logId */, android::base::LogSeverity logSeverity,
64 const char* /* tag */, const char* /* file */, unsigned int /*line*/,
65 const char* /* message */) { EXPECT_EQ(logSeverity, ::android::base::ERROR); });
66
67 generateLog();
68 }
69
TEST_F(LogTagTest,LogContainsCommonMessage)70 TEST_F(LogTagTest, LogContainsCommonMessage) {
71 android::base::SetLogger([](android::base::LogId /* logId */,
72 android::base::LogSeverity /* logSeverity */, const char* /* tag */,
73 const char* /* file */, unsigned int /*line*/,
74 const char* message) {
75 EXPECT_THAT(message, testing::MatchesRegex("NN_RET_CHECK failed.+"));
76 });
77
78 generateLog();
79 }
80
TEST_F(LogTagTest,ErrnoIsRestoredAfterLogging)81 TEST_F(LogTagTest, ErrnoIsRestoredAfterLogging) {
82 android::base::SetLogger([](android::base::LogId, android::base::LogSeverity,
83 const char* /*tag*/, const char* /*file*/, unsigned int /*line*/,
84 const char* /*message*/) { errno = -1; });
85
86 const int kTestErrno = 56;
87 errno = kTestErrno;
88 generateLog();
89 EXPECT_EQ(errno, kTestErrno);
90 }
91 } // namespace log_tag_test
92