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