1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "hilog_print_test.h"
16 #include "hilog/log.h"
17 #include <log_utils.h>
18
19 using namespace std;
20 using namespace testing::ext;
21 using namespace OHOS;
22 using namespace OHOS::HiviewDFX;
23
24 namespace {
25 const HiLogLabel LABEL = { LOG_CORE, 0xD002D00, "HILOGTEST_C" };
26 const int LOGINDEX = 42 + strlen("HILOGTEST_C");
27 constexpr uint32_t QUERY_INTERVAL = 1; // sleep 1s
28 const HiLogLabel KMSG_LABEL = { LOG_KMSG, 0xD002D00, "HILOGTEST_C" };
29 const std::string PRIV_STR = "<private>";
30
GetCmdResultFromPopen(const std::string & cmd)31 std::string GetCmdResultFromPopen(const std::string& cmd)
32 {
33 if (cmd.empty()) {
34 return "";
35 }
36 FILE* fp = popen(cmd.c_str(), "r");
37 if (fp == nullptr) {
38 return "";
39 }
40 std::string ret = "";
41 char* buffer = nullptr;
42 size_t len = 0;
43 while (getline(&buffer, &len, fp) != -1) {
44 std::string line = buffer;
45 ret += line;
46 }
47 if (buffer != nullptr) {
48 free(buffer);
49 buffer = nullptr;
50 }
51 pclose(fp);
52 return ret;
53 }
54
IsExistInCmdResult(const std::string & cmd,const std::string & str)55 bool IsExistInCmdResult(const std::string &cmd, const std::string &str)
56 {
57 if (cmd.empty()) {
58 return false;
59 }
60 FILE* fp = popen(cmd.c_str(), "r");
61 if (fp == nullptr) {
62 return false;
63 }
64 bool ret = false;
65 char* buffer = nullptr;
66 size_t len = 0;
67 while (getline(&buffer, &len, fp) != -1) {
68 std::string line = buffer;
69 if (line.find(str) != string::npos) {
70 ret = true;
71 break;
72 }
73 }
74 if (buffer != nullptr) {
75 free(buffer);
76 buffer = nullptr;
77 }
78 pclose(fp);
79 return ret;
80 }
81 }
82
SetUpTestCase()83 void HilogPrintTest::SetUpTestCase()
84 {
85 (void)GetCmdResultFromPopen("hilog -b X");
86 (void)GetCmdResultFromPopen("hilog -b I -D d002d00");
87 }
88
TearDownTestCase()89 void HilogPrintTest::TearDownTestCase()
90 {
91 (void)GetCmdResultFromPopen("hilog -b I");
92 }
93
SetUp()94 void HilogPrintTest::SetUp()
95 {
96 (void)GetCmdResultFromPopen("hilog -r");
97 }
98
99 namespace {
100 /**
101 * @tc.name: Dfx_HilogPrintTest_HilogTypeTest
102 * @tc.desc: HilogTypeTest.
103 * @tc.type: FUNC
104 */
105 HWTEST_F(HilogPrintTest, HilogTypeTest, TestSize.Level1)
106 {
107 const vector<string> typeVec = {
108 {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()'+-/,.-~:;<=>?_[]{}|\\\""},
109 {"123"},
110 {"173"},
111 {"123"},
112 {"0x7b, 0x7B"},
113 {"0.000123, 0.000123"},
114 {"1.230000e-04, 1.230000E-04"},
115 {"0.000123, 0.123"},
116 {"0.000123, 0.123"},
117 {"A"},
118 };
119
120 GTEST_LOG_(INFO) << "HilogTypeTest: start.";
121 HiLog::Info(LABEL, "%{public}s",
122 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()'+-/,.-~:;<=>?_[]{}|\\\"");
123 HiLog::Info(LABEL, "%{public}i", 123);
124 HiLog::Info(LABEL, "%{public}o", 123);
125 HiLog::Info(LABEL, "%{public}u", 123);
126 HiLog::Info(LABEL, "0x%{public}x, 0x%{public}X", 123, 123);
127 HiLog::Info(LABEL, "%{public}.6f, %{public}.6lf", 0.000123, 0.000123);
128 HiLog::Info(LABEL, "%{public}e, %{public}E", 0.000123, 0.000123);
129 HiLog::Info(LABEL, "%{public}g, %{public}g", 0.000123, 0.123);
130 HiLog::Info(LABEL, "%{public}G, %{public}G", 0.000123, 0.123);
131 HiLog::Info(LABEL, "%{public}c", 65);
132
133 std::string res = GetCmdResultFromPopen("hilog -T HILOGTEST_C -x");
134 vector<string> vec;
135 std::string log = "";
136 Split(res, vec, "\n");
137 for (unsigned int i = 0; i < vec.size(); i++) {
138 log = vec[i].substr(LOGINDEX);
139 EXPECT_EQ(log, typeVec[i]);
140 }
141 }
142
143 /**
144 * @tc.name: Dfx_HilogPrintTest_HilogFlagTest
145 * @tc.desc: HilogFlagTest.
146 * @tc.type: FUNC
147 */
148 HWTEST_F(HilogPrintTest, HilogFlagTest, TestSize.Level1)
149 {
150 const vector<string> FlagVec = {
151 {" 1000"},
152 {"1000 "},
153 {"+1000, -1000"},
154 {" 1000, -1000"},
155 {"3e8, 0x3e8"},
156 {"1000, 1000."},
157 {"1000, 1000.00"},
158 {"01000"},
159 };
160
161 GTEST_LOG_(INFO) << "HilogFlagTest: start.";
162 HiLog::Info(LABEL, "%{public}5d", 1000);
163 HiLog::Info(LABEL, "%{public}-5d", 1000);
164 HiLog::Info(LABEL, "%{public}+d, %{public}+d", 1000, -1000);
165 HiLog::Info(LABEL, "%{public} d, %{public} d", 1000, -1000);
166 HiLog::Info(LABEL, "%{public}x, %{public}#x", 1000, 1000);
167 HiLog::Info(LABEL, "%{public}.0f, %{public}#.0f", 1000.0, 1000.0);
168 HiLog::Info(LABEL, "%{public}g, %{public}#g", 1000.0, 1000.0);
169 HiLog::Info(LABEL, "%{public}05d", 1000);
170
171 std::string res = GetCmdResultFromPopen("hilog -T HILOGTEST_C -x");
172 vector<string> vec;
173 std::string log = "";
174 Split(res, vec, "\n");
175 for (unsigned int i = 0; i < vec.size(); i++) {
176 log = vec[i].substr(LOGINDEX);
177 EXPECT_EQ(log, FlagVec[i]);
178 }
179 }
180
181 /**
182 * @tc.name: Dfx_HilogPrintTest_HilogWidthTest
183 * @tc.desc: HilogWidthTest.
184 * @tc.type: FUNC
185 */
186 HWTEST_F(HilogPrintTest, HilogWidthTest, TestSize.Level1)
187 {
188 const vector<string> WidthVec = {
189 {"001000"},
190 {"001000"},
191 };
192
193 GTEST_LOG_(INFO) << "HilogWidthTest: start.";
194 HiLog::Info(LABEL, "%{public}06d", 1000);
195 HiLog::Info(LABEL, "%{public}0*d", 6, 1000);
196
197 std::string res = GetCmdResultFromPopen("hilog -T HILOGTEST_C -x");
198 vector<string> vec;
199 std::string log = "";
200 Split(res, vec, "\n");
201 for (unsigned int i = 0; i < vec.size(); i++) {
202 log = vec[i].substr(LOGINDEX);
203 EXPECT_EQ(log, WidthVec[i]);
204 }
205 }
206
207 /**
208 * @tc.name: Dfx_HilogPrintTest_HilogPrecisionTest
209 * @tc.desc: HilogPrecisionTest.
210 * @tc.type: FUNC
211 */
212 HWTEST_F(HilogPrintTest, HilogPrecisionTest, TestSize.Level1)
213 {
214 const vector<string> PrecisionVec = {
215 {"00001000"},
216 {"1000.12345679"},
217 {"1000.12345600"},
218 {"1000.1235"},
219 {"abcdefgh"},
220 };
221
222 GTEST_LOG_(INFO) << "HilogPrecisionTest: start.";
223 HiLog::Info(LABEL, "%{public}.8d", 1000);
224 HiLog::Info(LABEL, "%{public}.8f", 1000.123456789);
225 HiLog::Info(LABEL, "%{public}.8f", 1000.123456);
226 HiLog::Info(LABEL, "%{public}.8g", 1000.123456);
227 HiLog::Info(LABEL, "%{public}.8s", "abcdefghij");
228
229 std::string res = GetCmdResultFromPopen("hilog -T HILOGTEST_C -x");
230 vector<string> vec;
231 std::string log = "";
232 Split(res, vec, "\n");
233 for (unsigned int i = 0; i < vec.size(); i++) {
234 log = vec[i].substr(LOGINDEX);
235 EXPECT_EQ(log, PrecisionVec[i]);
236 }
237 }
238
239 /**
240 * @tc.name: Dfx_HilogPrintTest_HilogKmsgPrivacyTest
241 * @tc.desc: HilogKmsgPrivacyTest.
242 * @tc.type: FUNC
243 */
244 HWTEST_F(HilogPrintTest, HilogKmsgPrivacyTest, TestSize.Level1)
245 {
246 GTEST_LOG_(INFO) << "HilogKmsgPrivacyTest: start.";
247 std::string msg = "HilogPrintTest:HilogKmsgPrivacyTest";
248 HiLog::Info(KMSG_LABEL, "%s", msg.c_str());
249 sleep(QUERY_INTERVAL);
250 EXPECT_TRUE(IsExistInCmdResult("hilog -t kmsg -x |grep HILOGTEST_C", PRIV_STR));
251
252 (void)GetCmdResultFromPopen("hilog -r");
253 (void)GetCmdResultFromPopen("hilog -p off");
254 HiLog::Info(KMSG_LABEL, "%s", msg.c_str());
255 sleep(QUERY_INTERVAL);
256 EXPECT_TRUE(IsExistInCmdResult("hilog -t kmsg -x |grep HILOGTEST_C", msg));
257 (void)GetCmdResultFromPopen("hilog -p on");
258 }
259 } // namespace