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 <gtest/gtest.h>
16 #include <sys/time.h>
17 #include <unistd.h>
18 
19 #include <cstdint>
20 #include <ctime>
21 #include <string>
22 
23 #include "dfx_types.h"
24 #include "hilog_wrapper.h"
25 #include "securec.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 class WallpaperDfxTest : public testing::Test {
37 public:
38     static void SetUpTestCase(void);
39     static void TearDownTestCase(void);
40     static bool ExecuteCmd(const std::string &cmd, std::string &result);
41     void SetUp();
42     void TearDown();
43 };
44 
SetUpTestCase(void)45 void WallpaperDfxTest::SetUpTestCase(void)
46 {
47     HILOG_INFO("WallpaperDfxTest::SetUpTestCase");
48 }
49 
TearDownTestCase(void)50 void WallpaperDfxTest::TearDownTestCase(void)
51 {
52     HILOG_INFO("WallpaperDfxTest::TearDownTestCase");
53 }
54 
SetUp(void)55 void WallpaperDfxTest::SetUp(void)
56 {
57     HILOG_INFO("WallpaperDfxTest::SetUp");
58 }
59 
TearDown(void)60 void WallpaperDfxTest::TearDown(void)
61 {
62     HILOG_INFO("WallpaperDfxTest::TearDown");
63 }
64 
ExecuteCmd(const std::string & cmd,std::string & result)65 bool WallpaperDfxTest::ExecuteCmd(const std::string &cmd, std::string &result)
66 {
67     char buff[EACH_LINE_LENGTH] = { 0x00 };
68     char output[TOTAL_LENGTH] = { 0x00 };
69     FILE *ptr = popen(cmd.c_str(), "r");
70     if (ptr != nullptr) {
71         while (fgets(buff, sizeof(buff), ptr) != nullptr) {
72             if (strcat_s(output, sizeof(output), buff) != 0) {
73                 pclose(ptr);
74                 ptr = nullptr;
75                 return false;
76             }
77         }
78         pclose(ptr);
79         ptr = nullptr;
80     } else {
81         return false;
82     }
83     result = std::string(output);
84     return true;
85 }
86 
87 /**
88 * @tc.name: WallpaperDfxTest_DumpMethod_001
89 * @tc.desc: DumpAllMethod
90 * @tc.type: FUNC
91 * @tc.require:
92 * @tc.author:
93 */
94 HWTEST_F(WallpaperDfxTest, WallpaperDfxTest_DumpMethod_001, TestSize.Level0)
95 {
96     std::string result;
97     auto ret = WallpaperDfxTest::ExecuteCmd(CMD1, result);
98     EXPECT_TRUE(ret);
99     EXPECT_NE(result.find("Description"), std::string::npos);
100     EXPECT_NE(result.find("Show all"), std::string::npos);
101 }
102 
103 /**
104 * @tc.name: WallpaperDfxTest_Dump_ShowHelp_001
105 * @tc.desc: Dump ShowHelp.
106 * @tc.type: FUNC
107 * @tc.require:
108 * @tc.author:
109 */
110 HWTEST_F(WallpaperDfxTest, WallpaperDfxTest_Dump_ShowHelp_001, TestSize.Level0)
111 {
112     std::string result;
113     auto ret = WallpaperDfxTest::ExecuteCmd(CMD2, result);
114     EXPECT_TRUE(ret);
115     EXPECT_NE(result.find("Description"), std::string::npos);
116     EXPECT_NE(result.find("Show all"), std::string::npos);
117 }
118 
119 /**
120 * @tc.name: WallpaperDfxTest_DumpAllMethod_001
121 * @tc.desc: Dump ShowIllegalInformation.
122 * @tc.type: FUNC
123 * @tc.require:
124 * @tc.author:
125 */
126 HWTEST_F(WallpaperDfxTest, WallpaperDfxTest_DumpAllMethod_001, TestSize.Level0)
127 {
128     std::string result;
129     auto ret = WallpaperDfxTest::ExecuteCmd(CMD3, result);
130     EXPECT_TRUE(ret);
131     EXPECT_NE(result.find("WallpaperExtensionAbility"), std::string::npos);
132 }
133 } // namespace WallpaperMgrService
134 } // namespace OHOS