• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "securec.h"
29 #include "time_service_test.h"
30 #include "timer_info_test.h"
31 
32 namespace OHOS {
33 namespace MiscServices {
34 using namespace testing::ext;
35 using namespace OHOS;
36 using namespace OHOS::MiscServices;
37 
38 constexpr const uint16_t EACH_LINE_LENGTH = 100;
39 constexpr const char *CMD = "hidumper -s 3702 -a";
40 
41 class TimeDfxTest : public testing::Test {
42 public:
43     static void SetUpTestCase(void);
44     static void TearDownTestCase(void);
45     static bool ExecuteCmd(const std::string &cmd, std::string &result);
46     void SetUp();
47     void TearDown();
48 };
49 
SetUpTestCase(void)50 void TimeDfxTest::SetUpTestCase(void)
51 {
52 }
53 
TearDownTestCase(void)54 void TimeDfxTest::TearDownTestCase(void)
55 {
56 }
57 
SetUp(void)58 void TimeDfxTest::SetUp(void)
59 {
60 }
61 
TearDown(void)62 void TimeDfxTest::TearDown(void)
63 {
64 }
65 
ExecuteCmd(const std::string & cmd,std::string & result)66 bool TimeDfxTest::ExecuteCmd(const std::string &cmd, std::string &result)
67 {
68     char buff[EACH_LINE_LENGTH] = { 0x00 };
69     FILE *ptr = popen(cmd.c_str(), "r");
70     if (ptr != nullptr) {
71         while (fgets(buff, sizeof(buff), ptr) != nullptr) {
72             result.append(std::string(buff));
73         }
74         pclose(ptr);
75         ptr = nullptr;
76     } else {
77         return false;
78     }
79     return true;
80 }
81 
82 /**
83 * @tc.name: DumpAllTimeInfo001
84 * @tc.desc: dump all time info
85 * @tc.type: FUNC
86 */
87 HWTEST_F(TimeDfxTest, DumpAllTimeInfo001, TestSize.Level0)
88 {
89     std::string result;
90     auto ret = TimeDfxTest::ExecuteCmd(std::string(CMD).append(" -time").c_str(), result);
91     EXPECT_TRUE(ret);
92     EXPECT_NE(result.find("dump all time info"), std::string::npos);
93     EXPECT_NE(result.find("dump the time Zone"), std::string::npos);
94 }
95 
96 /**
97 * @tc.name: DumpTimerInfo001
98 * @tc.desc: dump timer info
99 * @tc.type: FUNC
100 */
101 HWTEST_F(TimeDfxTest, DumpTimerInfo001, TestSize.Level0)
102 {
103     std::string result;
104     TimeSystemAbility::GetInstance()->timerManagerHandler_ = nullptr;
105     auto ret = TimeDfxTest::ExecuteCmd(std::string(CMD).append(" \"-timer -a\"").c_str(), result);
106     EXPECT_TRUE(ret);
107     EXPECT_NE(result.find("dump all timer info"), std::string::npos);
108 }
109 
110 /**
111 * @tc.name: DumpTimerInfoById001
112 * @tc.desc: dump timer info by id
113 * @tc.type: FUNC
114 */
115 HWTEST_F(TimeDfxTest, DumpTimerInfoById001, TestSize.Level0)
116 {
117     auto timerInfo = std::make_shared<TimerInfoTest>();
118     timerInfo->SetType(1);
119     timerInfo->SetRepeat(false);
120     timerInfo->SetInterval(0);
121     timerInfo->SetWantAgent(nullptr);
122     timerInfo->SetCallbackInfo(TimeOutCallback1);
123     auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
124     EXPECT_TRUE(timerId1 > 0);
125     std::string result;
126     TimeSystemAbility::GetInstance()->timerManagerHandler_ = nullptr;
127     auto CMD1 = std::string(CMD).append(" \"-timer -i ").append(std::to_string(timerId1)).append(" \"");
128     auto ret = TimeDfxTest::ExecuteCmd(CMD1.c_str(), result);
129     EXPECT_TRUE(ret);
130     EXPECT_NE(result.find("timer id"), std::string::npos);
131     EXPECT_NE(result.find("timer type"), std::string::npos);
132     ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId1);
133     EXPECT_TRUE(ret);
134 }
135 
136 /**
137 * @tc.name: DumpTimerTriggerById001
138 * @tc.desc: dump trigger by id
139 * @tc.type: FUNC
140 */
141 HWTEST_F(TimeDfxTest, DumpTimerTriggerById001, TestSize.Level0)
142 {
143     auto timerInfo = std::make_shared<TimerInfoTest>();
144     timerInfo->SetType(1);
145     timerInfo->SetRepeat(false);
146     timerInfo->SetInterval(5);
147     timerInfo->SetWantAgent(nullptr);
148     timerInfo->SetCallbackInfo(TimeOutCallback1);
149     auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
150     EXPECT_TRUE(timerId1 > 0);
151     std::string result;
152     TimeSystemAbility::GetInstance()->timerManagerHandler_ = nullptr;
153     auto CMD1 = std::string(CMD).append(" \"-timer -s ").append(std::to_string(timerId1)).append(" \"");
154     auto ret = TimeDfxTest::ExecuteCmd(CMD1.c_str(), result);
155     EXPECT_TRUE(ret);
156     EXPECT_NE(result.find("timer id"), std::string::npos);
157     EXPECT_NE(result.find("timer trigger"), std::string::npos);
158     ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId1);
159     EXPECT_TRUE(ret);
160 }
161 
162 /**
163 * @tc.name: DumpShowHelp001
164 * @tc.desc: dump show help
165 * @tc.type: FUNC
166 */
167 HWTEST_F(TimeDfxTest, DumpShowHelp001, TestSize.Level0)
168 {
169     std::string result;
170     auto ret = TimeDfxTest::ExecuteCmd(std::string(CMD).append(" -h"), result);
171     EXPECT_TRUE(ret);
172     EXPECT_NE(result.find("dump current time info,include localtime,timezone info"), std::string::npos);
173     EXPECT_NE(result.find("dump all timer info"), std::string::npos);
174     EXPECT_NE(result.find("dump the timer info with timer id"), std::string::npos);
175 }
176 } // namespace MiscServices
177 } // namespace OHOS