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