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 TimeSystemAbility::GetInstance()->timerManagerHandler_ = nullptr;
109 auto ret = TimeDfxTest::ExecuteCmd(std::string(CMD).append(" \"-timer -a\"").c_str(), result);
110 EXPECT_TRUE(ret);
111 EXPECT_NE(result.find("dump all timer info"), std::string::npos);
112 }
113
114 /**
115 * @tc.name: DumpTimerInfoById001
116 * @tc.desc: dump timer info by id
117 * @tc.type: FUNC
118 */
119 HWTEST_F(TimeDfxTest, DumpTimerInfoById001, TestSize.Level0)
120 {
121 auto timerInfo = std::make_shared<TimerInfoTest>();
122 timerInfo->SetType(1);
123 timerInfo->SetRepeat(false);
124 timerInfo->SetInterval(0);
125 timerInfo->SetWantAgent(nullptr);
126 timerInfo->SetCallbackInfo(TimeOutCallback1);
127 auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
128 EXPECT_TRUE(timerId1 > 0);
129 std::string result;
130 TimeSystemAbility::GetInstance()->timerManagerHandler_ = nullptr;
131 auto CMD1 = std::string(CMD).append(" \"-timer -i ").append(std::to_string(timerId1)).append(" \"");
132 auto ret = TimeDfxTest::ExecuteCmd(CMD1.c_str(), result);
133 EXPECT_TRUE(ret);
134 EXPECT_NE(result.find("timer id"), std::string::npos);
135 EXPECT_NE(result.find("timer type"), std::string::npos);
136 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId1);
137 EXPECT_TRUE(ret);
138 }
139
140 /**
141 * @tc.name: DumpTimerTriggerById001
142 * @tc.desc: dump trigger by id
143 * @tc.type: FUNC
144 */
145 HWTEST_F(TimeDfxTest, DumpTimerTriggerById001, TestSize.Level0)
146 {
147 auto timerInfo = std::make_shared<TimerInfoTest>();
148 timerInfo->SetType(1);
149 timerInfo->SetRepeat(false);
150 timerInfo->SetInterval(5);
151 timerInfo->SetWantAgent(nullptr);
152 timerInfo->SetCallbackInfo(TimeOutCallback1);
153 auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
154 EXPECT_TRUE(timerId1 > 0);
155 std::string result;
156 TimeSystemAbility::GetInstance()->timerManagerHandler_ = nullptr;
157 auto CMD1 = std::string(CMD).append(" \"-timer -s ").append(std::to_string(timerId1)).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(timerId1);
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 } // namespace MiscServices
219 } // namespace OHOS