1 /*
2 * Copyright (C) 2021 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
16 #define private public
17 #define protected public
18 #include "time_system_ability.h"
19 #undef private
20 #undef protected
21
22 #include <cstdint>
23 #include <gtest/gtest.h>
24 #include <string>
25 #include <sys/time.h>
26 #include <unistd.h>
27
28 #include "accesstoken_kit.h"
29 #include "nativetoken_kit.h"
30 #include "securec.h"
31 #include "time_service_test.h"
32 #include "timer_info_test.h"
33 #include "token_setproc.h"
34
35 namespace OHOS {
36 namespace MiscServices {
37 using namespace testing::ext;
38 using namespace OHOS;
39 using namespace OHOS::MiscServices;
40 using namespace OHOS::Security::AccessToken;
41
42 constexpr const uint16_t EACH_LINE_LENGTH = 100;
43 constexpr const char *CMD = "hidumper -s 3702 -a";
44
45 class TimeDfxTest : public testing::Test {
46 public:
47 static void SetUpTestCase(void);
48 static void TearDownTestCase(void);
49 static bool ExecuteCmd(const std::string &cmd, std::string &result);
50 void SetUp();
51 void TearDown();
52 };
53
SetUpTestCase(void)54 void TimeDfxTest::SetUpTestCase(void)
55 {
56 }
57
TearDownTestCase(void)58 void TimeDfxTest::TearDownTestCase(void)
59 {
60 }
61
SetUp(void)62 void TimeDfxTest::SetUp(void)
63 {
64 }
65
TearDown(void)66 void TimeDfxTest::TearDown(void)
67 {
68 }
69
ExecuteCmd(const std::string & cmd,std::string & result)70 bool TimeDfxTest::ExecuteCmd(const std::string &cmd, std::string &result)
71 {
72 char buff[EACH_LINE_LENGTH] = { 0x00 };
73 FILE *ptr = popen(cmd.c_str(), "r");
74 if (ptr != nullptr) {
75 while (fgets(buff, sizeof(buff), ptr) != nullptr) {
76 result.append(std::string(buff));
77 }
78 pclose(ptr);
79 ptr = nullptr;
80 } else {
81 return false;
82 }
83 return true;
84 }
85
86 /**
87 * @tc.name: DumpAllTimeInfo001
88 * @tc.desc: dump all time info
89 * @tc.type: FUNC
90 */
91 HWTEST_F(TimeDfxTest, DumpAllTimeInfo001, TestSize.Level0)
92 {
93 std::string result;
94 auto ret = TimeDfxTest::ExecuteCmd(std::string(CMD).append(" -time").c_str(), result);
95 EXPECT_TRUE(ret);
96 EXPECT_NE(result.find("dump all time info"), std::string::npos);
97 EXPECT_NE(result.find("dump the time Zone"), std::string::npos);
98 }
99
100 /**
101 * @tc.name: DumpTimerInfo001
102 * @tc.desc: dump timer info
103 * @tc.type: FUNC
104 */
105 HWTEST_F(TimeDfxTest, DumpTimerInfo001, TestSize.Level0)
106 {
107 std::string result;
108 auto ret = TimeDfxTest::ExecuteCmd(std::string(CMD).append(" \"-timer -a\"").c_str(), result);
109 EXPECT_TRUE(ret);
110 EXPECT_NE(result.find("dump all timer info"), std::string::npos);
111 }
112
113 /**
114 * @tc.name: DumpTimerInfoById001
115 * @tc.desc: dump timer info by id
116 * @tc.type: FUNC
117 */
118 HWTEST_F(TimeDfxTest, DumpTimerInfoById001, TestSize.Level0)
119 {
120 auto timerInfo = std::make_shared<TimerInfoTest>();
121 timerInfo->SetType(timerInfo->TIMER_TYPE_REALTIME);
122 timerInfo->SetRepeat(false);
123 timerInfo->SetInterval(0);
124 timerInfo->SetWantAgent(nullptr);
125 timerInfo->SetCallbackInfo(TimeOutCallback1);
126 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
127 EXPECT_GT(timerId, 0);
128 std::string result;
129 auto CMD1 = std::string(CMD).append(" \"-timer -i ").append(std::to_string(timerId)).append(" \"");
130 auto ret = TimeDfxTest::ExecuteCmd(CMD1.c_str(), result);
131 EXPECT_TRUE(ret);
132 EXPECT_NE(result.find("timer id"), std::string::npos);
133 EXPECT_NE(result.find("timer type"), std::string::npos);
134 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
135 EXPECT_TRUE(ret);
136 }
137
138 /**
139 * @tc.name: DumpTimerTriggerById001
140 * @tc.desc: dump trigger by id
141 * @tc.type: FUNC
142 */
143 HWTEST_F(TimeDfxTest, DumpTimerTriggerById001, TestSize.Level0)
144 {
145 auto timerInfo = std::make_shared<TimerInfoTest>();
146 timerInfo->SetType(timerInfo->TIMER_TYPE_REALTIME);
147 timerInfo->SetRepeat(false);
148 timerInfo->SetInterval(5);
149 timerInfo->SetWantAgent(nullptr);
150 timerInfo->SetCallbackInfo(TimeOutCallback1);
151 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
152 EXPECT_GT(timerId, 0);
153 std::string result;
154 auto CMD1 = std::string(CMD).append(" \"-timer -s ").append(std::to_string(timerId)).append(" \"");
155 auto ret = TimeDfxTest::ExecuteCmd(CMD1.c_str(), result);
156 EXPECT_TRUE(ret);
157 EXPECT_NE(result.find("timer id"), std::string::npos);
158 EXPECT_NE(result.find("timer trigger"), std::string::npos);
159 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
160 EXPECT_TRUE(ret);
161 }
162
163 /**
164 * @tc.name: DumpShowHelp001
165 * @tc.desc: dump show help
166 * @tc.type: FUNC
167 */
168 HWTEST_F(TimeDfxTest, DumpShowHelp001, TestSize.Level0)
169 {
170 std::string result;
171 auto ret = TimeDfxTest::ExecuteCmd(std::string(CMD).append(" -h"), result);
172 EXPECT_TRUE(ret);
173 EXPECT_NE(result.find("dump current time info,include localtime,timezone info"), std::string::npos);
174 EXPECT_NE(result.find("dump all timer info"), std::string::npos);
175 EXPECT_NE(result.find("dump the timer info with timer id"), std::string::npos);
176 }
177
178 /**
179 * @tc.name: DumpIdleTimer001
180 * @tc.desc: dump idle timer when working
181 * @tc.type: FUNC
182 */
183 HWTEST_F(TimeDfxTest, DumpIdleTimer001, TestSize.Level0)
184 {
185 std::string result;
186 auto ret = TimeDfxTest::ExecuteCmd(std::string(CMD).append(" '-idle -a'"), result);
187 EXPECT_TRUE(ret);
188 EXPECT_NE(result.find("dump idle state = 0"), std::string::npos);
189 }
190
191 /**
192 * @tc.name: DumpIdleTimer001
193 * @tc.desc: dump idle timer when sleep
194 * @tc.type: FUNC
195 */
196 HWTEST_F(TimeDfxTest, DumpIdleTimer002, TestSize.Level0)
197 {
198 auto timerInfo = std::make_shared<TimerInfoTest>();
199 timerInfo->SetType(timerInfo->TIMER_TYPE_IDLE);
200 timerInfo->SetRepeat(false);
201 uint64_t timerId = 0;
202 TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
203 struct timeval currentTime {};
204 gettimeofday(¤tTime, nullptr);
205 int64_t time = (currentTime.tv_sec + 1000) * 1000 + currentTime.tv_usec / 1000;
206 TimeServiceClient::GetInstance()->StartTimerV9(timerId, time + 3000);
207
208 std::string result;
209 auto ret = TimeDfxTest::ExecuteCmd(std::string(CMD).append(" '-idle -a'"), result);
210 EXPECT_TRUE(ret);
211 EXPECT_NE(result.find("timer whenElapsed"), std::string::npos);
212
213 TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
214 }
215
216 /**
217 * @tc.name: DumpUidTimerMapInfo001
218 * @tc.desc: dump uid timer map info
219 * @tc.type: FUNC
220 */
221 HWTEST_F(TimeDfxTest, DumpUidTimerMapInfo001, TestSize.Level0)
222 {
223 std::string result;
224 auto CMD1 = std::string(CMD).append(" \"-UidTimer -l ").append(" \"");
225
226 auto timerInfo = std::make_shared<TimerInfoTest>();
227 timerInfo->SetType(timerInfo->TIMER_TYPE_REALTIME);
228 timerInfo->SetRepeat(false);
229 timerInfo->SetInterval(5);
230 timerInfo->SetWantAgent(nullptr);
231 timerInfo->SetCallbackInfo(TimeOutCallback1);
232 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
233 EXPECT_GT(timerId, 0);
234 struct timeval currentTime {};
235 gettimeofday(¤tTime, nullptr);
236 int64_t time = (currentTime.tv_sec + 1000) * 1000 + currentTime.tv_usec / 1000;
237 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, time + 3000);
238 EXPECT_TRUE(ret);
239
240 ret = TimeDfxTest::ExecuteCmd(CMD1.c_str(), result);
241 EXPECT_TRUE(ret);
242 EXPECT_NE(result.find("* timer id"), std::string::npos);
243 EXPECT_NE(result.find("* timer whenElapsed"), std::string::npos);
244 TIME_HILOGD(TIME_MODULE_SERVICE, "-UidTimer -l: %{public}s", result.c_str());
245 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
246 EXPECT_TRUE(ret);
247 }
248
249 /**
250 * @tc.name: DumpProxyPidTimerMapInfo001
251 * @tc.desc: dump pid proxy timer map info
252 * @tc.type: FUNC
253 */
254 HWTEST_F(TimeDfxTest, DumpPidTimerMapInfo001, TestSize.Level0)
255 {
256 std::string result;
257 auto CMD1 = std::string(CMD).append(" \"-PidTimer -l ").append(" \"");
258
259 auto timerInfo = std::make_shared<TimerInfoTest>();
260 timerInfo->SetType(timerInfo->TIMER_TYPE_REALTIME);
261 timerInfo->SetRepeat(false);
262 timerInfo->SetInterval(5);
263 timerInfo->SetWantAgent(nullptr);
264 timerInfo->SetCallbackInfo(TimeOutCallback1);
265 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
266 EXPECT_GT(timerId, 0);
267 struct timeval currentTime {};
268 gettimeofday(¤tTime, nullptr);
269 int64_t time = (currentTime.tv_sec + 1000) * 1000 + currentTime.tv_usec / 1000;
270 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, time + 3000);
271 EXPECT_TRUE(ret);
272
273 ret = TimeDfxTest::ExecuteCmd(CMD1.c_str(), result);
274 EXPECT_NE(result.find("* timer id"), std::string::npos);
275 EXPECT_NE(result.find("* timer whenElapsed"), std::string::npos);
276 TIME_HILOGD(TIME_MODULE_SERVICE, "-PidTimer -l: %{public}s", result.c_str());
277 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
278 EXPECT_TRUE(ret);
279 }
280
281 /**
282 * @tc.name: DumpPeoxyTimerMapInfo001
283 * @tc.desc: dump proxy timer map info
284 * @tc.type: FUNC
285 */
286 HWTEST_F(TimeDfxTest, DumpPeoxyTimerMapInfo001, TestSize.Level0)
287 {
288 std::string result;
289 auto CMD1 = std::string(CMD).append(" \"-ProxyTimer -l ").append(" \"");
290
291 auto timerInfo = std::make_shared<TimerInfoTest>();
292 timerInfo->SetType(timerInfo->TIMER_TYPE_REALTIME);
293 timerInfo->SetRepeat(false);
294 timerInfo->SetInterval(5);
295 timerInfo->SetWantAgent(nullptr);
296 timerInfo->SetCallbackInfo(TimeOutCallback1);
297 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
298 EXPECT_GT(timerId, 0);
299 struct timeval currentTime {};
300 gettimeofday(¤tTime, nullptr);
301 int64_t time = (currentTime.tv_sec + 1000) * 1000 + currentTime.tv_usec / 1000;
302 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, time + 3000);
303 EXPECT_TRUE(ret);
304
305 ret = TimeDfxTest::ExecuteCmd(CMD1.c_str(), result);
306 EXPECT_TRUE(ret);
307 EXPECT_NE(result.find("dump proxy map"), std::string::npos);
308 TIME_HILOGD(TIME_MODULE_SERVICE, "-ProxyTimer -l: %{public}s", result.c_str());
309 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
310 EXPECT_TRUE(ret);
311 }
312
313 /**
314 * @tc.name: DumpProxyDelayTime001
315 * @tc.desc: dump proxy delay time
316 * @tc.type: FUNC
317 */
318 HWTEST_F(TimeDfxTest, DumpProxyDelayTime001, TestSize.Level0)
319 {
320 std::string result;
321 auto CMD1 = std::string(CMD).append(" \"-ProxyDelayTime -l ").append(" \"");
322 auto ret = TimeDfxTest::ExecuteCmd(CMD1.c_str(), result);
323 EXPECT_TRUE(ret);
324 EXPECT_NE(result.find("259200000"), std::string::npos);
325 TIME_HILOGD(TIME_MODULE_SERVICE, "-ProxyDelayTime -l: %{public}s", result.c_str());
326 }
327
328 /**
329 * @tc.name: DumpPeoxyTimerMapInfo001
330 * @tc.desc: dump proxy timer map info
331 * @tc.type: FUNC
332 */
333 HWTEST_F(TimeDfxTest, DumpAdjustTime001, TestSize.Level0)
334 {
335 std::string result;
336 auto CMD1 = std::string(CMD).append(" \"-adjust -a ").append(" \"");
337 auto ret = TimeDfxTest::ExecuteCmd(CMD1.c_str(), result);
338 EXPECT_TRUE(ret);
339 EXPECT_NE(result.find("dump adjust time"), std::string::npos);
340 TIME_HILOGD(TIME_MODULE_SERVICE, "-adjust -a: %{public}s", result.c_str());
341 }
342 } // namespace MiscServices
343 } // namespace OHOS