15ba71b47Sopenharmony_ci/* 25ba71b47Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 35ba71b47Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45ba71b47Sopenharmony_ci * you may not use this file except in compliance with the License. 55ba71b47Sopenharmony_ci * You may obtain a copy of the License at 65ba71b47Sopenharmony_ci * 75ba71b47Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85ba71b47Sopenharmony_ci * 95ba71b47Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105ba71b47Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115ba71b47Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125ba71b47Sopenharmony_ci * See the License for the specific language governing permissions and 135ba71b47Sopenharmony_ci * limitations under the License. 145ba71b47Sopenharmony_ci */ 155ba71b47Sopenharmony_ci 165ba71b47Sopenharmony_ci#include "fuzztest_utils.h" 175ba71b47Sopenharmony_ci 185ba71b47Sopenharmony_ci#include "if_system_ability_manager.h" 195ba71b47Sopenharmony_ci#include "sam_mock_permission.h" 205ba71b47Sopenharmony_ci#include "system_ability_manager.h" 215ba71b47Sopenharmony_ci#include "iservice_registry.h" 225ba71b47Sopenharmony_ci#include "hisysevent_adapter.h" 235ba71b47Sopenharmony_ci 245ba71b47Sopenharmony_ci#include <cinttypes> 255ba71b47Sopenharmony_ci#include <unistd.h> 265ba71b47Sopenharmony_ci#include <cstdlib> 275ba71b47Sopenharmony_ci#include <fcntl.h> 285ba71b47Sopenharmony_ci 295ba71b47Sopenharmony_cinamespace OHOS { 305ba71b47Sopenharmony_cinamespace Samgr { 315ba71b47Sopenharmony_ci 325ba71b47Sopenharmony_cinamespace { 335ba71b47Sopenharmony_ci constexpr int32_t INIT_TIME = 3; 345ba71b47Sopenharmony_ci constexpr int32_t RETRY_TIME_OUT_NUMBER = 10; 355ba71b47Sopenharmony_ci constexpr int32_t SLEEP_INTERVAL_TIME = 200000; 365ba71b47Sopenharmony_ci constexpr int32_t DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID = 4802; 375ba71b47Sopenharmony_ci unsigned int g_dumpLevel = 0; 385ba71b47Sopenharmony_ci const std::u16string SAMGR_INTERFACE_TOKEN = u"ohos.samgr.accessToken"; 395ba71b47Sopenharmony_ci bool g_flag = false; 405ba71b47Sopenharmony_ci} 415ba71b47Sopenharmony_ci 425ba71b47Sopenharmony_cibool FuzzTestUtils::IsDmReady() 435ba71b47Sopenharmony_ci{ 445ba71b47Sopenharmony_ci auto dmProxy = SystemAbilityManager::GetInstance()->CheckSystemAbility( 455ba71b47Sopenharmony_ci DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); 465ba71b47Sopenharmony_ci if (dmProxy != nullptr) { 475ba71b47Sopenharmony_ci IPCObjectProxy* proxy = reinterpret_cast<IPCObjectProxy*>(dmProxy.GetRefPtr()); 485ba71b47Sopenharmony_ci if (proxy != nullptr && !proxy->IsObjectDead()) { 495ba71b47Sopenharmony_ci return true; 505ba71b47Sopenharmony_ci } 515ba71b47Sopenharmony_ci } 525ba71b47Sopenharmony_ci HILOGE("samgrFuzz:DM isn't ready"); 535ba71b47Sopenharmony_ci return false; 545ba71b47Sopenharmony_ci} 555ba71b47Sopenharmony_ci 565ba71b47Sopenharmony_civoid FuzzTestUtils::AddDeviceManager() 575ba71b47Sopenharmony_ci{ 585ba71b47Sopenharmony_ci if (IsDmReady()) { 595ba71b47Sopenharmony_ci return; 605ba71b47Sopenharmony_ci } 615ba71b47Sopenharmony_ci sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 625ba71b47Sopenharmony_ci if (sm == nullptr) { 635ba71b47Sopenharmony_ci HILOGE("samgrFuzz:GetSystemAbilityManager fail"); 645ba71b47Sopenharmony_ci return; 655ba71b47Sopenharmony_ci } 665ba71b47Sopenharmony_ci int32_t timeout = RETRY_TIME_OUT_NUMBER; 675ba71b47Sopenharmony_ci int64_t begin = OHOS::GetTickCount(); 685ba71b47Sopenharmony_ci sptr<IRemoteObject> dmAbility = nullptr; 695ba71b47Sopenharmony_ci do { 705ba71b47Sopenharmony_ci dmAbility = sm->CheckSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); 715ba71b47Sopenharmony_ci if (dmAbility != nullptr) { 725ba71b47Sopenharmony_ci break; 735ba71b47Sopenharmony_ci } 745ba71b47Sopenharmony_ci usleep(SLEEP_INTERVAL_TIME); 755ba71b47Sopenharmony_ci } while (timeout--); 765ba71b47Sopenharmony_ci HILOGI("samgrFuzz:Add DM spend %{public}" PRId64 " ms", OHOS::GetTickCount() - begin); 775ba71b47Sopenharmony_ci if (dmAbility == nullptr) { 785ba71b47Sopenharmony_ci HILOGE("samgrFuzz:dmAbility is null"); 795ba71b47Sopenharmony_ci return; 805ba71b47Sopenharmony_ci } 815ba71b47Sopenharmony_ci sptr<SystemAbilityManager> fuzzSAManager = SystemAbilityManager::GetInstance(); 825ba71b47Sopenharmony_ci ISystemAbilityManager::SAExtraProp saExtra(false, g_dumpLevel, u"", u""); 835ba71b47Sopenharmony_ci int32_t ret = fuzzSAManager->AddSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID, dmAbility, saExtra); 845ba71b47Sopenharmony_ci if (ret == ERR_OK) { 855ba71b47Sopenharmony_ci HILOGI("samgrFuzz:Add DM sucess"); 865ba71b47Sopenharmony_ci return; 875ba71b47Sopenharmony_ci } 885ba71b47Sopenharmony_ci HILOGE("samgrFuzz:Add DM fail"); 895ba71b47Sopenharmony_ci} 905ba71b47Sopenharmony_ci 915ba71b47Sopenharmony_civoid FuzzTestUtils::FuzzTestRemoteRequest(const uint8_t *rawData, size_t size, uint32_t code) 925ba71b47Sopenharmony_ci{ 935ba71b47Sopenharmony_ci SamMockPermission::MockPermission(); 945ba71b47Sopenharmony_ci MessageParcel data; 955ba71b47Sopenharmony_ci data.WriteInterfaceToken(SAMGR_INTERFACE_TOKEN); 965ba71b47Sopenharmony_ci data.WriteBuffer(rawData, size); 975ba71b47Sopenharmony_ci data.RewindRead(0); 985ba71b47Sopenharmony_ci MessageParcel reply; 995ba71b47Sopenharmony_ci MessageOption option; 1005ba71b47Sopenharmony_ci sptr<SystemAbilityManager> manager = SystemAbilityManager::GetInstance(); 1015ba71b47Sopenharmony_ci if (!g_flag) { 1025ba71b47Sopenharmony_ci HILOGI("TestRequest=%{public}u:Init", code); 1035ba71b47Sopenharmony_ci manager->Init(); 1045ba71b47Sopenharmony_ci g_flag = true; 1055ba71b47Sopenharmony_ci HILOGI("TestRequest=%{public}u:Init AddDeviceManager", code); 1065ba71b47Sopenharmony_ci AddDeviceManager(); 1075ba71b47Sopenharmony_ci sleep(INIT_TIME); 1085ba71b47Sopenharmony_ci if (!IsDmReady()) { 1095ba71b47Sopenharmony_ci HILOGE("TestRequest=%{public}u:Init CleanFfrt", code); 1105ba71b47Sopenharmony_ci manager->CleanFfrt(); 1115ba71b47Sopenharmony_ci return; 1125ba71b47Sopenharmony_ci } 1135ba71b47Sopenharmony_ci } else { 1145ba71b47Sopenharmony_ci HILOGI("TestRequest=%{public}u:AddDeviceManager", code); 1155ba71b47Sopenharmony_ci AddDeviceManager(); 1165ba71b47Sopenharmony_ci if (!IsDmReady()) { 1175ba71b47Sopenharmony_ci HILOGE("TestRequest=%{public}u:dm no ready,return", code); 1185ba71b47Sopenharmony_ci return; 1195ba71b47Sopenharmony_ci } 1205ba71b47Sopenharmony_ci HILOGI("TestRequest=%{public}u:SetFfrt", code); 1215ba71b47Sopenharmony_ci manager->SetFfrt(); 1225ba71b47Sopenharmony_ci } 1235ba71b47Sopenharmony_ci int32_t ret = manager->OnRemoteRequest(code, data, reply, option); 1245ba71b47Sopenharmony_ci HILOGI("TestRequest=%{public}u: ret=%{public}u", code, ret); 1255ba71b47Sopenharmony_ci manager->CleanFfrt(); 1265ba71b47Sopenharmony_ci} 1275ba71b47Sopenharmony_ci 1285ba71b47Sopenharmony_civoid FuzzTestUtils::FuzzTestRemoteRequest(MessageParcel& data, uint32_t code) 1295ba71b47Sopenharmony_ci{ 1305ba71b47Sopenharmony_ci SamMockPermission::MockPermission(); 1315ba71b47Sopenharmony_ci MessageParcel reply; 1325ba71b47Sopenharmony_ci MessageOption option; 1335ba71b47Sopenharmony_ci sptr<SystemAbilityManager> manager = SystemAbilityManager::GetInstance(); 1345ba71b47Sopenharmony_ci if (!g_flag) { 1355ba71b47Sopenharmony_ci HILOGI("FuzzTestRequest=%{public}u:Init", code); 1365ba71b47Sopenharmony_ci manager->Init(); 1375ba71b47Sopenharmony_ci g_flag = true; 1385ba71b47Sopenharmony_ci HILOGI("FuzzTestRequest=%{public}u:Init AddDeviceManager", code); 1395ba71b47Sopenharmony_ci AddDeviceManager(); 1405ba71b47Sopenharmony_ci sleep(INIT_TIME); 1415ba71b47Sopenharmony_ci if (!IsDmReady()) { 1425ba71b47Sopenharmony_ci HILOGE("FuzzTestRequest=%{public}u:Init CleanFfrt", code); 1435ba71b47Sopenharmony_ci manager->CleanFfrt(); 1445ba71b47Sopenharmony_ci return; 1455ba71b47Sopenharmony_ci } 1465ba71b47Sopenharmony_ci } else { 1475ba71b47Sopenharmony_ci HILOGI("FuzzTestRequest=%{public}u:AddDeviceManager", code); 1485ba71b47Sopenharmony_ci AddDeviceManager(); 1495ba71b47Sopenharmony_ci if (!IsDmReady()) { 1505ba71b47Sopenharmony_ci HILOGE("FuzzTestRequest=%{public}u:dm no ready,return", code); 1515ba71b47Sopenharmony_ci return; 1525ba71b47Sopenharmony_ci } 1535ba71b47Sopenharmony_ci HILOGI("FuzzTestRequest=%{public}u:SetFfrt", code); 1545ba71b47Sopenharmony_ci manager->SetFfrt(); 1555ba71b47Sopenharmony_ci } 1565ba71b47Sopenharmony_ci int32_t ret = manager->OnRemoteRequest(code, data, reply, option); 1575ba71b47Sopenharmony_ci HILOGI("FuzzTestRequest=%{public}u: ret=%{public}u", code, ret); 1585ba71b47Sopenharmony_ci manager->CleanFfrt(); 1595ba71b47Sopenharmony_ci} 1605ba71b47Sopenharmony_ci} 1615ba71b47Sopenharmony_ci} // namespace OHOS 162