100600bfbSopenharmony_ci/* 200600bfbSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 300600bfbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 400600bfbSopenharmony_ci * you may not use this file except in compliance with the License. 500600bfbSopenharmony_ci * You may obtain a copy of the License at 600600bfbSopenharmony_ci * 700600bfbSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 800600bfbSopenharmony_ci * 900600bfbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1000600bfbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1100600bfbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1200600bfbSopenharmony_ci * See the License for the specific language governing permissions and 1300600bfbSopenharmony_ci * limitations under the License. 1400600bfbSopenharmony_ci */ 1500600bfbSopenharmony_ci#include "hidumper_service_test.h" 1600600bfbSopenharmony_ci#include <iservice_registry.h> 1700600bfbSopenharmony_ci#include "dump_manager_client.h" 1800600bfbSopenharmony_ci#include "dump_manager_service.h" 1900600bfbSopenharmony_ci#include "inner/dump_service_id.h" 2000600bfbSopenharmony_ci#include "dump_on_demand_load.h" 2100600bfbSopenharmony_ciusing namespace std; 2200600bfbSopenharmony_ciusing namespace testing::ext; 2300600bfbSopenharmony_ciusing namespace OHOS; 2400600bfbSopenharmony_ciusing namespace OHOS::HiviewDFX; 2500600bfbSopenharmony_cinamespace OHOS { 2600600bfbSopenharmony_cinamespace HiviewDFX { 2700600bfbSopenharmony_ciconst int32_t HidumperServiceTest::LIMIT_SIZE = 5000; 2800600bfbSopenharmony_civoid HidumperServiceTest::SetUpTestCase(void) 2900600bfbSopenharmony_ci{ 3000600bfbSopenharmony_ci} 3100600bfbSopenharmony_ci 3200600bfbSopenharmony_civoid HidumperServiceTest::TearDownTestCase(void) 3300600bfbSopenharmony_ci{ 3400600bfbSopenharmony_ci} 3500600bfbSopenharmony_ci 3600600bfbSopenharmony_civoid HidumperServiceTest::SetUp(void) 3700600bfbSopenharmony_ci{ 3800600bfbSopenharmony_ci} 3900600bfbSopenharmony_ci 4000600bfbSopenharmony_civoid HidumperServiceTest::TearDown(void) 4100600bfbSopenharmony_ci{ 4200600bfbSopenharmony_ci} 4300600bfbSopenharmony_ci 4400600bfbSopenharmony_ci/** 4500600bfbSopenharmony_ci * @tc.name: HidumperServiceTest001 4600600bfbSopenharmony_ci * @tc.desc: Test DumpManagerService service ready. 4700600bfbSopenharmony_ci * @tc.type: FUNC 4800600bfbSopenharmony_ci */ 4900600bfbSopenharmony_ciHWTEST_F(HidumperServiceTest, HidumperServiceTest001, TestSize.Level3) 5000600bfbSopenharmony_ci{ 5100600bfbSopenharmony_ci sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 5200600bfbSopenharmony_ci ASSERT_TRUE(sam != nullptr) << "HidumperServiceTest001 fail to get GetSystemAbilityManager"; 5300600bfbSopenharmony_ci sptr<OnDemandLoadCallback> loadCallback = new OnDemandLoadCallback(); 5400600bfbSopenharmony_ci int32_t result = sam->LoadSystemAbility(DFX_SYS_HIDUMPER_ABILITY_ID, loadCallback); 5500600bfbSopenharmony_ci ASSERT_TRUE(result == ERR_OK) << "GetSystemAbility failed."; 5600600bfbSopenharmony_ci} 5700600bfbSopenharmony_ci 5800600bfbSopenharmony_ci/** 5900600bfbSopenharmony_ci * @tc.name: DumpManagerService002 6000600bfbSopenharmony_ci * @tc.desc: Test DumpManagerService Request. 6100600bfbSopenharmony_ci * @tc.type: FUNC 6200600bfbSopenharmony_ci */ 6300600bfbSopenharmony_ciHWTEST_F(HidumperServiceTest, DumpManagerService002, TestSize.Level3) 6400600bfbSopenharmony_ci{ 6500600bfbSopenharmony_ci auto dumpManagerService = std::make_shared<DumpManagerService>(); 6600600bfbSopenharmony_ci dumpManagerService->OnStart(); 6700600bfbSopenharmony_ci dumpManagerService->started_ = true; 6800600bfbSopenharmony_ci std::vector<std::u16string> args; 6900600bfbSopenharmony_ci int32_t ret = ERR_OK; 7000600bfbSopenharmony_ci ret = dumpManagerService->Dump(-1, args); 7100600bfbSopenharmony_ci ASSERT_TRUE(ret == ERR_OK); 7200600bfbSopenharmony_ci ret = dumpManagerService->Request(args, -1); 7300600bfbSopenharmony_ci ASSERT_TRUE(ret == ERR_OK); 7400600bfbSopenharmony_ci 7500600bfbSopenharmony_ci dumpManagerService->OnStop(); 7600600bfbSopenharmony_ci} 7700600bfbSopenharmony_ci 7800600bfbSopenharmony_ci/** 7900600bfbSopenharmony_ci * @tc.name: DumpManagerService003 8000600bfbSopenharmony_ci * @tc.desc: Test DumpManagerService CountFdNums. 8100600bfbSopenharmony_ci * @tc.type: FUNC 8200600bfbSopenharmony_ci */ 8300600bfbSopenharmony_ciHWTEST_F(HidumperServiceTest, DumpManagerService003, TestSize.Level3) 8400600bfbSopenharmony_ci{ 8500600bfbSopenharmony_ci auto dumpManagerService = std::make_shared<DumpManagerService>(); 8600600bfbSopenharmony_ci int32_t pid = 1; 8700600bfbSopenharmony_ci uint32_t fdNums = 0; 8800600bfbSopenharmony_ci std::string detailFdInfo; 8900600bfbSopenharmony_ci std::string topLeakedType; 9000600bfbSopenharmony_ci int32_t ret = dumpManagerService->CountFdNums(pid, fdNums, detailFdInfo, topLeakedType); 9100600bfbSopenharmony_ci ASSERT_TRUE(ret == 0); 9200600bfbSopenharmony_ci ASSERT_FALSE(detailFdInfo.empty()); 9300600bfbSopenharmony_ci ASSERT_FALSE(topLeakedType.empty()); 9400600bfbSopenharmony_ci} 9500600bfbSopenharmony_ci 9600600bfbSopenharmony_ci/** 9700600bfbSopenharmony_ci * @tc.name: DumpManagerService004 9800600bfbSopenharmony_ci * @tc.desc: Test DumpManagerService ScanPidOverLimit. 9900600bfbSopenharmony_ci * @tc.type: FUNC 10000600bfbSopenharmony_ci */ 10100600bfbSopenharmony_ciHWTEST_F(HidumperServiceTest, DumpManagerService004, TestSize.Level3) 10200600bfbSopenharmony_ci{ 10300600bfbSopenharmony_ci auto dumpManagerService = std::make_shared<DumpManagerService>(); 10400600bfbSopenharmony_ci std::string requestType = "fd"; 10500600bfbSopenharmony_ci std::vector<int32_t> pidList; 10600600bfbSopenharmony_ci int32_t ret = dumpManagerService->ScanPidOverLimit(requestType, LIMIT_SIZE, pidList); 10700600bfbSopenharmony_ci ASSERT_TRUE(ret == 0); 10800600bfbSopenharmony_ci 10900600bfbSopenharmony_ci ret = dumpManagerService->ScanPidOverLimit(requestType, -1, pidList); 11000600bfbSopenharmony_ci ASSERT_TRUE(ret != 0); 11100600bfbSopenharmony_ci} 11200600bfbSopenharmony_ci 11300600bfbSopenharmony_ci/** 11400600bfbSopenharmony_ci * @tc.name: DumpManagerService005 11500600bfbSopenharmony_ci * @tc.desc: Test DumpManagerService Request. 11600600bfbSopenharmony_ci * @tc.type: FUNC 11700600bfbSopenharmony_ci */ 11800600bfbSopenharmony_ciHWTEST_F(HidumperServiceTest, DumpManagerService005, TestSize.Level3) 11900600bfbSopenharmony_ci{ 12000600bfbSopenharmony_ci auto dumpManagerService = std::make_shared<DumpManagerService>(); 12100600bfbSopenharmony_ci dumpManagerService->OnStart(); 12200600bfbSopenharmony_ci dumpManagerService->started_ = true; 12300600bfbSopenharmony_ci dumpManagerService->OnStart(); 12400600bfbSopenharmony_ci 12500600bfbSopenharmony_ci std::vector<std::u16string> args; 12600600bfbSopenharmony_ci dumpManagerService->blockRequest_ = true; 12700600bfbSopenharmony_ci int32_t ret = dumpManagerService->Request(args, -1); 12800600bfbSopenharmony_ci ASSERT_TRUE(ret != 0); 12900600bfbSopenharmony_ci dumpManagerService->blockRequest_ = false; 13000600bfbSopenharmony_ci dumpManagerService->started_ = false; 13100600bfbSopenharmony_ci ret = dumpManagerService->Request(args, -1); 13200600bfbSopenharmony_ci ASSERT_TRUE(ret != 0); 13300600bfbSopenharmony_ci dumpManagerService->OnStop(); 13400600bfbSopenharmony_ci dumpManagerService->started_ = false; 13500600bfbSopenharmony_ci dumpManagerService->OnStop(); 13600600bfbSopenharmony_ci} 13700600bfbSopenharmony_ci} // namespace HiviewDFX 13800600bfbSopenharmony_ci} // namespace OHOS