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 <cstdint>
16 #include <ctime>
17 #include <gtest/gtest.h>
18 #include <string>
19 #include <sys/time.h>
20 #include <unistd.h>
21
22 #include "dfx_types.h"
23 #include "hilog_wrapper.h"
24 #include "securec.h"
25 #include "statistic_reporter.h"
26
27 using namespace testing::ext;
28 using namespace OHOS::MiscServices;
29 namespace OHOS {
30 namespace WallpaperMgrService {
31 constexpr const uint16_t EACH_LINE_LENGTH = 100;
32 constexpr const uint16_t TOTAL_LENGTH = 1000;
33 constexpr const char *CMD1 = "hidumper -s 3705";
34 constexpr const char *CMD2 = "hidumper -s 3705 -a -h";
35 constexpr const char *CMD3 = "hidumper -s 3705 -a -all";
36 constexpr const int USER_ID = 1000;
37 constexpr const char *BUNDLE_NAME = "WALLPAPER_SERVICE";
38 class WallpaperDfxTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 static bool ExecuteCmd(const std::string &cmd, std::string &result);
43 void SetUp();
44 void TearDown();
45 };
46
SetUpTestCase(void)47 void WallpaperDfxTest::SetUpTestCase(void)
48 {
49 HILOG_INFO("WallpaperDfxTest::SetUpTestCase");
50 }
51
TearDownTestCase(void)52 void WallpaperDfxTest::TearDownTestCase(void)
53 {
54 HILOG_INFO("WallpaperDfxTest::TearDownTestCase");
55 }
56
SetUp(void)57 void WallpaperDfxTest::SetUp(void)
58 {
59 HILOG_INFO("WallpaperDfxTest::SetUp");
60 }
61
TearDown(void)62 void WallpaperDfxTest::TearDown(void)
63 {
64 HILOG_INFO("WallpaperDfxTest::TearDown");
65 }
66
ExecuteCmd(const std::string & cmd,std::string & result)67 bool WallpaperDfxTest::ExecuteCmd(const std::string &cmd, std::string &result)
68 {
69 char buff[EACH_LINE_LENGTH] = { 0x00 };
70 char output[TOTAL_LENGTH] = { 0x00 };
71 FILE *ptr = popen(cmd.c_str(), "r");
72 if (ptr != nullptr) {
73 while (fgets(buff, sizeof(buff), ptr) != nullptr) {
74 if (strcat_s(output, sizeof(output), buff) != 0) {
75 pclose(ptr);
76 ptr = nullptr;
77 return false;
78 }
79 }
80 pclose(ptr);
81 ptr = nullptr;
82 } else {
83 return false;
84 }
85 result = std::string(output);
86 return true;
87 }
88
89 /**
90 * @tc.name: WallpaperDfxTest_DumpMethod_001
91 * @tc.desc: DumpAllMethod
92 * @tc.type: FUNC
93 * @tc.require:
94 * @tc.author:
95 */
96 HWTEST_F(WallpaperDfxTest, WallpaperDfxTest_DumpMethod_001, TestSize.Level0)
97 {
98 std::string result;
99 auto ret = WallpaperDfxTest::ExecuteCmd(CMD1, result);
100 EXPECT_TRUE(ret);
101 EXPECT_NE(result.find("Description"), std::string::npos);
102 EXPECT_NE(result.find("Show all"), std::string::npos);
103 }
104
105 /**
106 * @tc.name: WallpaperDfxTest_Dump_ShowHelp_001
107 * @tc.desc: Dump ShowHelp.
108 * @tc.type: FUNC
109 * @tc.require:
110 * @tc.author:
111 */
112 HWTEST_F(WallpaperDfxTest, WallpaperDfxTest_Dump_ShowHelp_001, TestSize.Level0)
113 {
114 std::string result;
115 auto ret = WallpaperDfxTest::ExecuteCmd(CMD2, result);
116 EXPECT_TRUE(ret);
117 EXPECT_NE(result.find("Description"), std::string::npos);
118 EXPECT_NE(result.find("Show all"), std::string::npos);
119 }
120
121 /**
122 * @tc.name: WallpaperDfxTest_DumpAllMethod_001
123 * @tc.desc: Dump ShowIllegalInformation.
124 * @tc.type: FUNC
125 * @tc.require:
126 * @tc.author:
127 */
128 HWTEST_F(WallpaperDfxTest, WallpaperDfxTest_DumpAllMethod_001, TestSize.Level0)
129 {
130 std::string result;
131 auto ret = WallpaperDfxTest::ExecuteCmd(CMD3, result);
132 EXPECT_TRUE(ret);
133 EXPECT_NE(result.find("WallpaperExtensionAbility"), std::string::npos);
134 }
135
136 /**
137 * @tc.name: WallpaperDfxTest_StatisticReporter_001
138 * @tc.desc: Statistic Reporter .
139 * @tc.type: FUNC
140 * @tc.require:
141 * @tc.author:
142 */
143 HWTEST_F(WallpaperDfxTest, WallpaperDfxTest_StatisticReporter_001, TestSize.Level0)
144 {
145 StatisticReporter::StartTimerThread();
146 UsageTimeStat timeStat;
147 timeStat.packagesName = BUNDLE_NAME;
148 timeStat.startTime = time(nullptr);
149 StatisticReporter::ReportUsageTimeStatistic(USER_ID, timeStat);
150 time_t current = time(nullptr);
151 auto status = StatisticReporter::InvokeUsageTime(current);
152 EXPECT_EQ(status, ReportStatus::SUCCESS);
153 }
154 } // namespace WallpaperMgrService
155 } // namespace OHOS