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 #include "hidumper_service_test.h"
16 #include <iservice_registry.h>
17 #include "dump_manager_client.h"
18 #include "dump_manager_service.h"
19 #include "inner/dump_service_id.h"
20 #include "dump_on_demand_load.h"
21 #include "executor/memory/memory_util.h"
22 #include "string_ex.h"
23
24 using namespace std;
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace OHOS::HiviewDFX;
28 namespace OHOS {
29 namespace HiviewDFX {
30 const int32_t HidumperServiceTest::LIMIT_SIZE = 5000;
SetUpTestCase(void)31 void HidumperServiceTest::SetUpTestCase(void)
32 {
33 }
34
TearDownTestCase(void)35 void HidumperServiceTest::TearDownTestCase(void)
36 {
37 }
38
SetUp(void)39 void HidumperServiceTest::SetUp(void)
40 {
41 }
42
TearDown(void)43 void HidumperServiceTest::TearDown(void)
44 {
45 }
46
47 /**
48 * @tc.name: HidumperServiceTest001
49 * @tc.desc: Test DumpManagerService service ready.
50 * @tc.type: FUNC
51 */
52 HWTEST_F(HidumperServiceTest, HidumperServiceTest001, TestSize.Level3)
53 {
54 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
55 ASSERT_TRUE(sam != nullptr) << "HidumperServiceTest001 fail to get GetSystemAbilityManager";
56 sptr<OnDemandLoadCallback> loadCallback = new OnDemandLoadCallback();
57 int32_t result = sam->LoadSystemAbility(DFX_SYS_HIDUMPER_ABILITY_ID, loadCallback);
58 ASSERT_TRUE(result == ERR_OK) << "GetSystemAbility failed.";
59 }
60
61 /**
62 * @tc.name: DumpManagerService002
63 * @tc.desc: Test DumpManagerService Request.
64 * @tc.type: FUNC
65 */
66 HWTEST_F(HidumperServiceTest, DumpManagerService002, TestSize.Level3)
67 {
68 auto dumpManagerService = std::make_shared<DumpManagerService>();
69 dumpManagerService->OnStart();
70 dumpManagerService->started_ = true;
71 std::vector<std::u16string> args;
72 int32_t ret = ERR_OK;
73 ret = dumpManagerService->Dump(-1, args);
74 ASSERT_TRUE(ret == ERR_OK);
75 ret = dumpManagerService->Request(args, -1);
76 ASSERT_TRUE(ret == ERR_OK);
77
78 dumpManagerService->OnStop();
79 }
80
81 /**
82 * @tc.name: DumpManagerService003
83 * @tc.desc: Test DumpManagerService CountFdNums.
84 * @tc.type: FUNC
85 */
86 HWTEST_F(HidumperServiceTest, DumpManagerService003, TestSize.Level3)
87 {
88 auto dumpManagerService = std::make_shared<DumpManagerService>();
89 int32_t pid = 1;
90 uint32_t fdNums = 0;
91 std::string detailFdInfo;
92 std::string topLeakedType;
93 int32_t ret = dumpManagerService->CountFdNums(pid, fdNums, detailFdInfo, topLeakedType);
94 ASSERT_TRUE(ret == 0);
95 ASSERT_FALSE(detailFdInfo.empty());
96 ASSERT_FALSE(topLeakedType.empty());
97 }
98
99 /**
100 * @tc.name: DumpManagerService004
101 * @tc.desc: Test DumpManagerService ScanPidOverLimit.
102 * @tc.type: FUNC
103 */
104 HWTEST_F(HidumperServiceTest, DumpManagerService004, TestSize.Level3)
105 {
106 auto dumpManagerService = std::make_shared<DumpManagerService>();
107 std::string requestType = "fd";
108 std::vector<int32_t> pidList;
109 int32_t ret = dumpManagerService->ScanPidOverLimit(requestType, LIMIT_SIZE, pidList);
110 ASSERT_TRUE(ret == 0);
111
112 ret = dumpManagerService->ScanPidOverLimit(requestType, -1, pidList);
113 ASSERT_TRUE(ret != 0);
114 }
115
116 /**
117 * @tc.name: DumpManagerService005
118 * @tc.desc: Test DumpManagerService Request.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(HidumperServiceTest, DumpManagerService005, TestSize.Level3)
122 {
123 auto dumpManagerService = std::make_shared<DumpManagerService>();
124 dumpManagerService->OnStart();
125 dumpManagerService->started_ = true;
126 dumpManagerService->OnStart();
127
128 std::vector<std::u16string> args;
129 dumpManagerService->blockRequest_ = true;
130 int32_t ret = dumpManagerService->Request(args, -1);
131 ASSERT_TRUE(ret != 0);
132 dumpManagerService->blockRequest_ = false;
133 dumpManagerService->started_ = false;
134 ret = dumpManagerService->Request(args, -1);
135 ASSERT_TRUE(ret != 0);
136 dumpManagerService->OnStop();
137 dumpManagerService->started_ = false;
138 dumpManagerService->OnStop();
139 }
140
141 /**
142 * @tc.name: DumpManagerService006
143 * @tc.desc: Test hidumper_service run in small cpu.
144 * @tc.type: FUNC
145 */
146 HWTEST_F(HidumperServiceTest, DumpManagerService006, TestSize.Level3)
147 {
148 std::string cmd = "hidumper -h";
149 std::vector<std::string> vec;
150 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
151 ASSERT_GT(vec.size(), 0);
152 cmd = "taskset -p `pidof hidumper_service`";
153 vec.clear();
154 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
155 ASSERT_TRUE(vec.size() > 0);
156 ASSERT_TRUE(vec[0].find(":") != std::string::npos);
157 std::string str = vec[0];
158 string::size_type typePos = str.find(":");
159 if (typePos != str.npos) {
160 string valueStr = str.substr(typePos + 2); // pid ***'s current affinity mask: f
161 ASSERT_TRUE(valueStr == "f");
162 }
163 }
164
165 /**
166 * @tc.name: DumpManagerService007
167 * @tc.desc: Test DumpManagerService error request.
168 * @tc.type: FUNC
169 */
170 HWTEST_F(HidumperServiceTest, DumpManagerService007, TestSize.Level3)
171 {
172 auto dumpManagerService = std::make_shared<DumpManagerService>();
173 std::vector<std::u16string> args;
174 int outfd = -1;
175 dumpManagerService->HandleRequestError(args, outfd, -1, "test");
176 ASSERT_TRUE(args.size() == 0);
177 args.push_back(Str8ToStr16("test"));
178 dumpManagerService->HandleRequestError(args, outfd, -1, "test");
179 ASSERT_TRUE(args.size() == 0);
180 }
181 } // namespace HiviewDFX
182 } // namespace OHOS