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 19using namespace std; 20using namespace testing::ext; 21using namespace OHOS; 22using namespace OHOS::HiviewDFX; 23 24namespace { 25const HiLogLabel LABEL = { LOG_CORE, 0xD002D00, "HILOGTEST_C" }; 26const int LOGINDEX = 42 + strlen("HILOGTEST_C"); 27 28std::string GetCmdResultFromPopen(const std::string& cmd) 29{ 30 if (cmd.empty()) { 31 return ""; 32 } 33 FILE* fp = popen(cmd.c_str(), "r"); 34 if (fp == nullptr) { 35 return ""; 36 } 37 std::string ret = ""; 38 char* buffer = nullptr; 39 size_t len = 0; 40 while (getline(&buffer, &len, fp) != -1) { 41 std::string line = buffer; 42 ret += line; 43 } 44 if (buffer != nullptr) { 45 free(buffer); 46 buffer = nullptr; 47 } 48 pclose(fp); 49 return ret; 50} 51} 52 53void HilogPrintTest::SetUpTestCase() 54{ 55 (void)GetCmdResultFromPopen("hilog -b X"); 56 (void)GetCmdResultFromPopen("hilog -b I -D d002d00"); 57} 58 59void HilogPrintTest::TearDownTestCase() 60{ 61 (void)GetCmdResultFromPopen("hilog -b I"); 62} 63 64void HilogPrintTest::SetUp() 65{ 66 (void)GetCmdResultFromPopen("hilog -r"); 67} 68 69namespace { 70/** 71 * @tc.name: Dfx_HilogPrintTest_HilogTypeTest 72 * @tc.desc: HilogTypeTest. 73 * @tc.type: FUNC 74 */ 75HWTEST_F(HilogPrintTest, HilogTypeTest, TestSize.Level1) 76{ 77const vector<string> typeVec = { 78 {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()'+-/,.-~:;<=>?_[]{}|\\\""}, 79 {"123"}, 80 {"173"}, 81 {"123"}, 82 {"0x7b, 0x7B"}, 83 {"0.000123, 0.000123"}, 84 {"1.230000e-04, 1.230000E-04"}, 85 {"0.000123, 0.123"}, 86 {"0.000123, 0.123"}, 87 {"A"}, 88}; 89 90 GTEST_LOG_(INFO) << "HilogTypeTest: start."; 91 HiLog::Info(LABEL, "%{public}s", 92 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()'+-/,.-~:;<=>?_[]{}|\\\""); 93 HiLog::Info(LABEL, "%{public}i", 123); 94 HiLog::Info(LABEL, "%{public}o", 123); 95 HiLog::Info(LABEL, "%{public}u", 123); 96 HiLog::Info(LABEL, "0x%{public}x, 0x%{public}X", 123, 123); 97 HiLog::Info(LABEL, "%{public}.6f, %{public}.6lf", 0.000123, 0.000123); 98 HiLog::Info(LABEL, "%{public}e, %{public}E", 0.000123, 0.000123); 99 HiLog::Info(LABEL, "%{public}g, %{public}g", 0.000123, 0.123); 100 HiLog::Info(LABEL, "%{public}G, %{public}G", 0.000123, 0.123); 101 HiLog::Info(LABEL, "%{public}c", 65); 102 103 std::string res = GetCmdResultFromPopen("hilog -T HILOGTEST_C -x"); 104 vector<string> vec; 105 std::string log = ""; 106 Split(res, vec, "\n"); 107 for (unsigned int i = 0; i < vec.size(); i++) { 108 log = vec[i].substr(LOGINDEX); 109 EXPECT_EQ(log, typeVec[i]); 110 } 111} 112 113/** 114 * @tc.name: Dfx_HilogPrintTest_HilogFlagTest 115 * @tc.desc: HilogFlagTest. 116 * @tc.type: FUNC 117 */ 118HWTEST_F(HilogPrintTest, HilogFlagTest, TestSize.Level1) 119{ 120const vector<string> FlagVec = { 121 {" 1000"}, 122 {"1000 "}, 123 {"+1000, -1000"}, 124 {" 1000, -1000"}, 125 {"3e8, 0x3e8"}, 126 {"1000, 1000."}, 127 {"1000, 1000.00"}, 128 {"01000"}, 129}; 130 131 GTEST_LOG_(INFO) << "HilogFlagTest: start."; 132 HiLog::Info(LABEL, "%{public}5d", 1000); 133 HiLog::Info(LABEL, "%{public}-5d", 1000); 134 HiLog::Info(LABEL, "%{public}+d, %{public}+d", 1000, -1000); 135 HiLog::Info(LABEL, "%{public} d, %{public} d", 1000, -1000); 136 HiLog::Info(LABEL, "%{public}x, %{public}#x", 1000, 1000); 137 HiLog::Info(LABEL, "%{public}.0f, %{public}#.0f", 1000.0, 1000.0); 138 HiLog::Info(LABEL, "%{public}g, %{public}#g", 1000.0, 1000.0); 139 HiLog::Info(LABEL, "%{public}05d", 1000); 140 141 std::string res = GetCmdResultFromPopen("hilog -T HILOGTEST_C -x"); 142 vector<string> vec; 143 std::string log = ""; 144 Split(res, vec, "\n"); 145 for (unsigned int i = 0; i < vec.size(); i++) { 146 log = vec[i].substr(LOGINDEX); 147 EXPECT_EQ(log, FlagVec[i]); 148 } 149} 150 151/** 152 * @tc.name: Dfx_HilogPrintTest_HilogWidthTest 153 * @tc.desc: HilogWidthTest. 154 * @tc.type: FUNC 155 */ 156HWTEST_F(HilogPrintTest, HilogWidthTest, TestSize.Level1) 157{ 158const vector<string> WidthVec = { 159 {"001000"}, 160 {"001000"}, 161}; 162 163 GTEST_LOG_(INFO) << "HilogWidthTest: start."; 164 HiLog::Info(LABEL, "%{public}06d", 1000); 165 HiLog::Info(LABEL, "%{public}0*d", 6, 1000); 166 167 std::string res = GetCmdResultFromPopen("hilog -T HILOGTEST_C -x"); 168 vector<string> vec; 169 std::string log = ""; 170 Split(res, vec, "\n"); 171 for (unsigned int i = 0; i < vec.size(); i++) { 172 log = vec[i].substr(LOGINDEX); 173 EXPECT_EQ(log, WidthVec[i]); 174 } 175} 176 177/** 178 * @tc.name: Dfx_HilogPrintTest_HilogPrecisionTest 179 * @tc.desc: HilogPrecisionTest. 180 * @tc.type: FUNC 181 */ 182HWTEST_F(HilogPrintTest, HilogPrecisionTest, TestSize.Level1) 183{ 184const vector<string> PrecisionVec = { 185 {"00001000"}, 186 {"1000.12345679"}, 187 {"1000.12345600"}, 188 {"1000.1235"}, 189 {"abcdefgh"}, 190}; 191 192 GTEST_LOG_(INFO) << "HilogPrecisionTest: start."; 193 HiLog::Info(LABEL, "%{public}.8d", 1000); 194 HiLog::Info(LABEL, "%{public}.8f", 1000.123456789); 195 HiLog::Info(LABEL, "%{public}.8f", 1000.123456); 196 HiLog::Info(LABEL, "%{public}.8g", 1000.123456); 197 HiLog::Info(LABEL, "%{public}.8s", "abcdefghij"); 198 199 std::string res = GetCmdResultFromPopen("hilog -T HILOGTEST_C -x"); 200 vector<string> vec; 201 std::string log = ""; 202 Split(res, vec, "\n"); 203 for (unsigned int i = 0; i < vec.size(); i++) { 204 log = vec[i].substr(LOGINDEX); 205 EXPECT_EQ(log, PrecisionVec[i]); 206 } 207} 208} // namespace