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_utils_test.h"
16 #include "hilog_common.h"
17 #include <log_utils.h>
18 #include <hilog/log_c.h>
19 #include <list>
20
21 using namespace std;
22 using namespace testing::ext;
23 using namespace OHOS;
24 using namespace OHOS::HiviewDFX;
25
GetCmdResultFromPopen(const std::string & cmd)26 static std::string GetCmdResultFromPopen(const std::string& cmd)
27 {
28 if (cmd.empty()) {
29 return "";
30 }
31 FILE* fp = popen(cmd.c_str(), "r");
32 if (fp == nullptr) {
33 return "";
34 }
35 std::string ret = "";
36 char* buffer = nullptr;
37 size_t len = 0;
38 while (getline(&buffer, &len, fp) != -1) {
39 std::string line = buffer;
40 ret += line;
41 }
42 if (buffer != nullptr) {
43 free(buffer);
44 buffer = nullptr;
45 }
46 pclose(fp);
47 return ret;
48 }
49
50 namespace {
51 /**
52 * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_001
53 * @tc.desc: Size2Str & Str2Size.
54 * @tc.type: FUNC
55 */
56 HWTEST_F(HilogUtilsTest, HilogUtilsTest_001, TestSize.Level1)
57 {
58 GTEST_LOG_(INFO) << "HilogUtilsTest_001: start.";
59 const std::list<pair<uint64_t, string>> sizeStrList = {
60 /* size, unit */
61 {1, "B"},
62 {1ULL << 10, "K"},
63 {1ULL << 20, "M"},
64 {1ULL << 30, "G"},
65 {1ULL << 40, "T"},
66 };
67 for (auto &it : sizeStrList) {
68 EXPECT_EQ(Size2Str(it.first), "1.0" + it.second);
69 EXPECT_EQ(Str2Size("1" + it.second), it.first);
70 }
71
72 // valid str reg [0-9]+[BKMGT]?
73 EXPECT_EQ(Str2Size("1.2A"), 0);
74 }
75
76 /**
77 * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_002
78 * @tc.desc: LogType2Str & Str2LogType.
79 * @tc.type: FUNC
80 */
81 HWTEST_F(HilogUtilsTest, HilogUtilsTest_002, TestSize.Level1)
82 {
83 GTEST_LOG_(INFO) << "HilogUtilsTest_002: start.";
84 const std::list<pair<LogType, string>> logTypesList = {
85 {LOG_INIT, "init"},
86 {LOG_CORE, "core"},
87 {LOG_APP, "app"},
88 {LOG_KMSG, "kmsg"},
89 {LOG_TYPE_MAX, "invalid"},
90 };
91 for (auto &it : logTypesList) {
92 EXPECT_EQ(LogType2Str(it.first), it.second);
93 EXPECT_EQ(Str2LogType(it.second), it.first);
94 }
95 }
96
97 /**
98 * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_003
99 * @tc.desc: ComboLogType2Str & Str2ComboLogType.
100 * @tc.type: FUNC
101 */
102 HWTEST_F(HilogUtilsTest, HilogUtilsTest_003, TestSize.Level1)
103 {
104 GTEST_LOG_(INFO) << "HilogUtilsTest_003: start.";
105 const std::list<pair<uint16_t, string>> logTypesList = {
106 /* ComboLogType, str */
107 {1 << LOG_APP, "app"},
108 {1 << LOG_INIT, "init"},
109 {1 << LOG_CORE, "core"},
110 {1 << LOG_KMSG, "kmsg"},
111 {(1 << LOG_APP) + (1 << LOG_INIT) + (1 << LOG_CORE) + (1 << LOG_KMSG), "init,core,app,kmsg"},
112 };
113 for (auto &it : logTypesList) {
114 EXPECT_EQ(ComboLogType2Str(it.first), it.second);
115 EXPECT_EQ(Str2ComboLogType(it.second), it.first);
116 }
117
118 EXPECT_EQ(Str2ComboLogType(""), (1 << LOG_APP) + (1 << LOG_CORE));
119 EXPECT_EQ(Str2ComboLogType("invalid"), 0);
120 }
121
122 /**
123 * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_004
124 * @tc.desc: LogLevel2Str & Str2LogLevel.
125 * @tc.type: FUNC
126 */
127 HWTEST_F(HilogUtilsTest, HilogUtilsTest_004, TestSize.Level1)
128 {
129 GTEST_LOG_(INFO) << "HilogUtilsTest_004: start.";
130 struct LogLevelEntry {
131 const LogLevel logLevel;
132 const std::string str;
133 const std::string shortStr;
134 const int comboLogLevel;
135 };
136
137 LogLevelEntry logLevelEntries[] = {
138 {LOG_LEVEL_MIN, "INVALID", "V", 0},
139 {LOG_DEBUG, "DEBUG", "D", 1 << LOG_DEBUG},
140 {LOG_INFO, "INFO", "I", 1 << LOG_INFO},
141 {LOG_WARN, "WARN", "W", 1 << LOG_WARN},
142 {LOG_ERROR, "ERROR", "E", 1 << LOG_ERROR},
143 {LOG_FATAL, "FATAL", "F", 1 << LOG_FATAL, },
144 {LOG_LEVEL_MAX, "X", "X", 0},
145 };
146
147 constexpr int logLevelEntryCnt = sizeof(logLevelEntries) / sizeof(LogLevelEntry);
148
149 for (int i = 0; i < logLevelEntryCnt; i++) {
150 EXPECT_EQ(LogLevel2Str(logLevelEntries[i].logLevel), logLevelEntries[i].str);
151 EXPECT_EQ(Str2LogLevel(logLevelEntries[i].str), logLevelEntries[i].logLevel);
152 EXPECT_EQ(LogLevel2ShortStr(logLevelEntries[i].logLevel), logLevelEntries[i].shortStr);
153 EXPECT_EQ(ShortStr2LogLevel(logLevelEntries[i].shortStr), logLevelEntries[i].logLevel);
154 if (logLevelEntries[i].comboLogLevel != 0) {
155 EXPECT_EQ(ComboLogLevel2Str(logLevelEntries[i].comboLogLevel), logLevelEntries[i].str);
156 }
157 EXPECT_EQ(Str2ComboLogLevel(logLevelEntries[i].str), logLevelEntries[i].comboLogLevel);
158 }
159
160 EXPECT_EQ(Str2ComboLogLevel(""), 0xFFFF);
161 }
162
163 /**
164 * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_005
165 * @tc.desc: GetBitsCount & GetBitPos.
166 * @tc.type: FUNC
167 */
168 HWTEST_F(HilogUtilsTest, HilogUtilsTest_005, TestSize.Level1)
169 {
170 GTEST_LOG_(INFO) << "HelperTest_005: start.";
171 uint64_t num1 = 1 << 4;
172 uint64_t num2 = (1 << 2) + (1 << 3) + (1 << 4);
173 EXPECT_EQ(GetBitPos(num1), 4);
174 // only accpet the number which is power of 2
175 EXPECT_EQ(GetBitPos(num2), 0);
176 EXPECT_EQ(GetBitsCount(num2), 3);
177 }
178
179 /**
180 * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_006
181 * @tc.desc: Uint2DecStr DecStr2Uint Uint2HexStr & HexStr2Uint.
182 * @tc.type: FUNC
183 */
184 HWTEST_F(HilogUtilsTest, HilogUtilsTest_006, TestSize.Level1)
185 {
186 GTEST_LOG_(INFO) << "HilogUtilsTest_006: start.";
187 uint32_t decNum = 1250;
188 uint32_t hexNum = 0xd002d00;
189 std::string decStr = "1250";
190 std::string hexStr = "d002d00";
191 EXPECT_EQ(Uint2DecStr(decNum), decStr);
192 EXPECT_EQ(DecStr2Uint(decStr), decNum);
193 EXPECT_EQ(Uint2HexStr(hexNum), hexStr);
194 EXPECT_EQ(HexStr2Uint(hexStr), hexNum);
195 }
196
197 /**
198 * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_007
199 * @tc.desc: GetAllLogTypes.
200 * @tc.type: FUNC
201 */
202 HWTEST_F(HilogUtilsTest, HilogUtilsTest_007, TestSize.Level1)
203 {
204 GTEST_LOG_(INFO) << "HilogUtilsTest_007: start.";
205 vector<uint16_t> vec = GetAllLogTypes();
206 sort(vec.begin(), vec.end());
207 vector<uint16_t> allTypes {0, 1, 3, 4};
208 EXPECT_TRUE(vec == allTypes);
209 }
210
211 /**
212 * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_008
213 * @tc.desc: GetPPidByPid.
214 * @tc.type: FUNC
215 */
216 HWTEST_F(HilogUtilsTest, HilogUtilsTest_008, TestSize.Level1)
217 {
218 GTEST_LOG_(INFO) << "HilogUtilsTest_008: start.";
219 uint32_t pid = stoi(GetCmdResultFromPopen("pidof hilogd"));
220 EXPECT_EQ(GetPPidByPid(pid), 1);
221
222 uint32_t invalidPid = 999999;
223 EXPECT_EQ(GetPPidByPid(invalidPid), 0);
224 }
225
226 /**
227 * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_009
228 * @tc.desc: WaitingToDo.
229 * @tc.type: FUNC
230 */
231 HWTEST_F(HilogUtilsTest, HilogUtilsTest_009, TestSize.Level1)
232 {
233 GTEST_LOG_(INFO) << "HilogUtilsTest_009: start.";
__anon217a301d0202(const string &path) 234 int ret = WaitingToDo(WAITING_DATA_MS, "/data/log", [](const string &path) {
235 if (!access(path.c_str(), F_OK)) {
236 return RET_SUCCESS;
237 }
238 return RET_FAIL;
239 });
240 EXPECT_EQ(ret, RET_SUCCESS);
241
__anon217a301d0302(const string &path) 242 ret = WaitingToDo(WAITING_DATA_MS, "/test/ttt", [](const string &path) {
243 if (!access(path.c_str(), F_OK)) {
244 return RET_SUCCESS;
245 }
246 return RET_FAIL;
247 });
248 PrintErrorno(errno);
249 EXPECT_EQ(ret, RET_FAIL);
250 }
251 } // namespace
252