• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetCmdResultFromPopen(const std::string & cmd)28 std::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 
SetUpTestCase()53 void HilogPrintTest::SetUpTestCase()
54 {
55     (void)GetCmdResultFromPopen("hilog -b X");
56     (void)GetCmdResultFromPopen("hilog -b I -D d002d00");
57 }
58 
TearDownTestCase()59 void HilogPrintTest::TearDownTestCase()
60 {
61     (void)GetCmdResultFromPopen("hilog -b I");
62 }
63 
SetUp()64 void HilogPrintTest::SetUp()
65 {
66     (void)GetCmdResultFromPopen("hilog -r");
67 }
68 
69 namespace {
70 /**
71  * @tc.name: Dfx_HilogPrintTest_HilogTypeTest
72  * @tc.desc: HilogTypeTest.
73  * @tc.type: FUNC
74  */
75 HWTEST_F(HilogPrintTest, HilogTypeTest, TestSize.Level1)
76 {
77 const 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", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()'+-/,.-~:;<=>?_[]{}|\\\"");
92     HiLog::Info(LABEL, "%{public}i", 123);
93     HiLog::Info(LABEL, "%{public}o", 123);
94     HiLog::Info(LABEL, "%{public}u", 123);
95     HiLog::Info(LABEL, "0x%{public}x, 0x%{public}X", 123, 123);
96     HiLog::Info(LABEL, "%{public}.6f, %{public}.6lf", 0.000123, 0.000123);
97     HiLog::Info(LABEL, "%{public}e, %{public}E", 0.000123, 0.000123);
98     HiLog::Info(LABEL, "%{public}g, %{public}g", 0.000123, 0.123);
99     HiLog::Info(LABEL, "%{public}G, %{public}G", 0.000123, 0.123);
100     HiLog::Info(LABEL, "%{public}c", 65);
101 
102     std::string res = GetCmdResultFromPopen("hilog -T HILOGTEST_C -x");
103     vector<string> vec;
104     std::string log = "";
105     Split(res, vec, "\n");
106     for (unsigned int i = 0; i < vec.size(); i++) {
107         log = vec[i].substr(LOGINDEX);
108         EXPECT_EQ(log, typeVec[i]);
109     }
110 }
111 
112 /**
113  * @tc.name: Dfx_HilogPrintTest_HilogFlagTest
114  * @tc.desc: HilogFlagTest.
115  * @tc.type: FUNC
116  */
117 HWTEST_F(HilogPrintTest, HilogFlagTest, TestSize.Level1)
118 {
119 const vector<string> FlagVec = {
120     {" 1000"},
121     {"1000 "},
122     {"+1000, -1000"},
123     {" 1000, -1000"},
124     {"3e8, 0x3e8"},
125     {"1000, 1000."},
126     {"1000, 1000.00"},
127     {"01000"},
128 };
129 
130     GTEST_LOG_(INFO) << "HilogFlagTest: start.";
131     HiLog::Info(LABEL, "%{public}5d", 1000);
132     HiLog::Info(LABEL, "%{public}-5d", 1000);
133     HiLog::Info(LABEL, "%{public}+d, %{public}+d", 1000, -1000);
134     HiLog::Info(LABEL, "%{public} d, %{public} d", 1000, -1000);
135     HiLog::Info(LABEL, "%{public}x, %{public}#x", 1000, 1000);
136     HiLog::Info(LABEL, "%{public}.0f, %{public}#.0f", 1000.0, 1000.0);
137     HiLog::Info(LABEL, "%{public}g, %{public}#g", 1000.0, 1000.0);
138     HiLog::Info(LABEL, "%{public}05d", 1000);
139 
140     std::string res = GetCmdResultFromPopen("hilog -T HILOGTEST_C -x");
141     vector<string> vec;
142     std::string log = "";
143     Split(res, vec, "\n");
144     for (unsigned int i = 0; i < vec.size(); i++) {
145         log = vec[i].substr(LOGINDEX);
146         EXPECT_EQ(log, FlagVec[i]);
147     }
148 }
149 
150 /**
151  * @tc.name: Dfx_HilogPrintTest_HilogWidthTest
152  * @tc.desc: HilogWidthTest.
153  * @tc.type: FUNC
154  */
155 HWTEST_F(HilogPrintTest, HilogWidthTest, TestSize.Level1)
156 {
157 const vector<string> WidthVec = {
158     {"001000"},
159     {"001000"},
160 };
161 
162     GTEST_LOG_(INFO) << "HilogWidthTest: start.";
163     HiLog::Info(LABEL, "%{public}06d", 1000);
164     HiLog::Info(LABEL, "%{public}0*d", 6, 1000);
165 
166     std::string res = GetCmdResultFromPopen("hilog -T HILOGTEST_C -x");
167     vector<string> vec;
168     std::string log = "";
169     Split(res, vec, "\n");
170     for (unsigned int i = 0; i < vec.size(); i++) {
171         log = vec[i].substr(LOGINDEX);
172         EXPECT_EQ(log, WidthVec[i]);
173     }
174 }
175 
176 /**
177  * @tc.name: Dfx_HilogPrintTest_HilogPrecisionTest
178  * @tc.desc: HilogPrecisionTest.
179  * @tc.type: FUNC
180  */
181 HWTEST_F(HilogPrintTest, HilogPrecisionTest, TestSize.Level1)
182 {
183 const vector<string> PrecisionVec = {
184     {"00001000"},
185     {"1000.12345679"},
186     {"1000.12345600"},
187     {"1000.1235"},
188     {"abcdefgh"},
189 };
190 
191     GTEST_LOG_(INFO) << "HilogPrecisionTest: start.";
192     HiLog::Info(LABEL, "%{public}.8d", 1000);
193     HiLog::Info(LABEL, "%{public}.8f", 1000.123456789);
194     HiLog::Info(LABEL, "%{public}.8f", 1000.123456);
195     HiLog::Info(LABEL, "%{public}.8g", 1000.123456);
196     HiLog::Info(LABEL, "%{public}.8s", "abcdefghij");
197 
198     std::string res = GetCmdResultFromPopen("hilog -T HILOGTEST_C -x");
199     vector<string> vec;
200     std::string log = "";
201     Split(res, vec, "\n");
202     for (unsigned int i = 0; i < vec.size(); i++) {
203         log = vec[i].substr(LOGINDEX);
204         EXPECT_EQ(log, PrecisionVec[i]);
205     }
206 }
207 } // namespace