1 /*
2 * Copyright (C) 2018 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 <gtest/gtest.h>
18
19 #include "netdutils/Log.h"
20
globalFunctionName()21 android::netdutils::LogEntry globalFunctionName() {
22 return android::netdutils::LogEntry().function(__FUNCTION__);
23 }
24
globalPrettyFunctionName()25 android::netdutils::LogEntry globalPrettyFunctionName() {
26 return android::netdutils::LogEntry().prettyFunction(__PRETTY_FUNCTION__);
27 }
28
29 namespace android {
30 namespace netdutils {
31
32 namespace {
33
functionName()34 LogEntry functionName() {
35 return LogEntry().function(__FUNCTION__);
36 }
37
prettyFunctionName()38 LogEntry prettyFunctionName() {
39 return LogEntry().prettyFunction(__PRETTY_FUNCTION__);
40 }
41
42 } // namespace
43
44 class AAA {
45 public:
46 AAA() = default;
47
functionName()48 LogEntry functionName() {
49 return LogEntry().function(__FUNCTION__);
50 }
51
prettyFunctionName()52 LogEntry prettyFunctionName() {
53 return LogEntry().prettyFunction(__PRETTY_FUNCTION__);
54 }
55
56 class BBB {
57 public:
58 BBB() = default;
59
functionName()60 LogEntry functionName() {
61 return LogEntry().function(__FUNCTION__);
62 }
63
prettyFunctionName()64 LogEntry prettyFunctionName() {
65 return LogEntry().prettyFunction(__PRETTY_FUNCTION__);
66 }
67 };
68 };
69
TEST(LogEntryTest,Empty)70 TEST(LogEntryTest, Empty) {
71 LogEntry empty;
72 EXPECT_EQ("", empty.toString());
73 }
74
TEST(LogEntryTest,GlobalFunction)75 TEST(LogEntryTest, GlobalFunction) {
76 EXPECT_EQ("globalFunctionName()", ::globalFunctionName().toString());
77 }
78
TEST(LogEntryTest,GlobalPrettyFunction)79 TEST(LogEntryTest, GlobalPrettyFunction) {
80 EXPECT_EQ("globalPrettyFunctionName()", ::globalPrettyFunctionName().toString());
81 }
82
TEST(LogEntryTest,UnnamedNamespaceFunction)83 TEST(LogEntryTest, UnnamedNamespaceFunction) {
84 const LogEntry entry = functionName();
85 EXPECT_EQ("functionName()", entry.toString());
86 }
87
TEST(LogEntryTest,UnnamedNamespacePrettyFunction)88 TEST(LogEntryTest, UnnamedNamespacePrettyFunction) {
89 const LogEntry entry = prettyFunctionName();
90 EXPECT_EQ("prettyFunctionName()", entry.toString());
91 }
92
TEST(LogEntryTest,ClassFunction)93 TEST(LogEntryTest, ClassFunction) {
94 const LogEntry entry = AAA().functionName();
95 EXPECT_EQ("functionName()", entry.toString());
96 }
97
TEST(LogEntryTest,ClassPrettyFunction)98 TEST(LogEntryTest, ClassPrettyFunction) {
99 const LogEntry entry = AAA().prettyFunctionName();
100 EXPECT_EQ("AAA::prettyFunctionName()", entry.toString());
101 }
102
TEST(LogEntryTest,InnerClassFunction)103 TEST(LogEntryTest, InnerClassFunction) {
104 const LogEntry entry = AAA::BBB().functionName();
105 EXPECT_EQ("functionName()", entry.toString());
106 }
107
TEST(LogEntryTest,InnerClassPrettyFunction)108 TEST(LogEntryTest, InnerClassPrettyFunction) {
109 const LogEntry entry = AAA::BBB().prettyFunctionName();
110 EXPECT_EQ("BBB::prettyFunctionName()", entry.toString());
111 }
112
TEST(LogEntryTest,PrintChainedArguments)113 TEST(LogEntryTest, PrintChainedArguments) {
114 const LogEntry entry = LogEntry()
115 .function("testFunc")
116 .arg("hello")
117 .arg(42)
118 .arg(true);
119 EXPECT_EQ("testFunc(hello, 42, true)", entry.toString());
120 }
121
TEST(LogEntryTest,PrintIntegralTypes)122 TEST(LogEntryTest, PrintIntegralTypes) {
123 const LogEntry entry = LogEntry()
124 .function("testFunc")
125 .arg('A')
126 .arg(100U)
127 .arg(-1000LL);
128 EXPECT_EQ("testFunc(65, 100, -1000)", entry.toString());
129 }
130
TEST(LogEntryTest,PrintHex)131 TEST(LogEntryTest, PrintHex) {
132 const std::vector<uint8_t> buf{0xDE, 0xAD, 0xBE, 0xEF};
133 const LogEntry entry = LogEntry().function("testFunc").arg(buf);
134 EXPECT_EQ("testFunc({deadbeef})", entry.toString());
135 }
136
TEST(LogEntryTest,PrintArgumentPack)137 TEST(LogEntryTest, PrintArgumentPack) {
138 const LogEntry entry = LogEntry().function("testFunc").args("hello", 42, false);
139 EXPECT_EQ("testFunc(hello, 42, false)", entry.toString());
140 }
141
142 } // namespace netdutils
143 } // namespace android
144