diff --git a/services/samgr/native/source/main.cpp b/services/samgr/native/source/main.cpp index 820b6b8d67f9ca3e14cc37ab1e3796a463a58f02..e1ca2ce2f4c6da38111fdb10feeac7ba1832a751 100644 --- a/services/samgr/native/source/main.cpp +++ b/services/samgr/native/source/main.cpp @@ -29,7 +29,9 @@ int main(int argc, char *argv[]) OHOS::sptr manager = OHOS::SystemAbilityManager::GetInstance(); manager->Init(); OHOS::sptr serv = manager->AsObject(); - IPCSkeleton::SetContextObject(serv); + if (!IPCSkeleton::SetContextObject(serv)) { + HILOGE("SetContextObject fail"); + } HILOGI("start System Ability Manager Loop"); OHOS::IPCSkeleton::JoinWorkThread(); diff --git a/services/samgr/native/test/unittest/BUILD.gn b/services/samgr/native/test/unittest/BUILD.gn index 4050c38a98ac5f12ded0901ddeefa788e45aa590..abf2d233fbd5fb73e1bb4b1ec7f18c9fc49dad05 100644 --- a/services/samgr/native/test/unittest/BUILD.gn +++ b/services/samgr/native/test/unittest/BUILD.gn @@ -70,6 +70,7 @@ ohos_unittest("SystemAbilityMgrTest") { } ohos_executable("ondemand") { + #module_out_path = module_output_path sources = [ "./src/ondemand_helper.cpp" ] configs = [ ":sam_test_config", @@ -95,10 +96,60 @@ ohos_executable("ondemand") { subsystem_name = "distributedschedule" } +ohos_executable("TestTool") { + #module_out_path = module_output_path + sources = [ + "${samgr_dir}/utils/native/source/tools.cpp", + "${samgr_services_dir}/source/ability_death_recipient.cpp", + "${samgr_services_dir}/source/rpc_callback_imp.cpp", + "${samgr_services_dir}/source/system_ability_load_callback_proxy.cpp", + "${samgr_services_dir}/source/system_ability_manager.cpp", + "${samgr_services_dir}/source/system_ability_manager_stub.cpp", + "${samgr_services_dir}/source/system_ability_status_change_proxy.cpp", + "${samgr_services_dir}/test/unittest/src/itest_transaction_service.cpp", + "./src/system_ability_test_tool.cpp", + ] + + configs = [ + ":sam_test_config", + "${samgr_dir}/interfaces/innerkits/lsamgr:config_samgr", + "${samgr_dir}/interfaces/innerkits/samgr_proxy:samgr_proxy_config", + "${samgr_dir}/services/samgr/native:sam_config", + "${samgr_dir}/services/samgr/native:distributed_store_config", + ] + deps = [ + "${samgr_dir}/interfaces/innerkits/common:samgr_common", + "${samgr_dir}/interfaces/innerkits/samgr_proxy:samgr_proxy", + ] + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog_base" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + external_deps += [ + "access_token:libaccesstoken_sdk", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "hiviewdfx_hilog_native:libhilog_base", + "init:libbegetutil", + "ipc:ipc_core", + "ipc:libdbinder", + "utils_base:utils", + ] + part_name = "samgr_standard" + subsystem_name = "distributedschedule" +} + group("unittest") { testonly = true deps = [ ":SystemAbilityMgrTest", + ":TestTool", ":ondemand", ] } diff --git a/services/samgr/native/test/unittest/src/system_ability_test_tool.cpp b/services/samgr/native/test/unittest/src/system_ability_test_tool.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d77245bdddf0f525d09e7277e0f3595e3427e4a7 --- /dev/null +++ b/services/samgr/native/test/unittest/src/system_ability_test_tool.cpp @@ -0,0 +1,262 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "datetime_ex.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "itest_transaction_service.h" +#include "string_ex.h" +#include "system_ability_definition.h" +#include "system_ability_load_callback_stub.h" +#include "system_ability_status_change_stub.h" +#include "sam_log.h" + +#define private public +#include "system_ability_manager.h" + +using namespace OHOS; +using namespace std; + +namespace { + constexpr int32_t PARSE_WAIT_TIME = 1000 * 50; // us + constexpr int32_t DEFAULT_SA_ID = 1401; + + const string HELP_CONTENT = "usage: samgr test tool \n" + "These are common samgr test tool commands list:\n" + " h list available commands\n" + " ls list all said\n" + " parse list all sa xml in profile\n" + " get get loacl system ability with given said\n" + " getrmt get remote system ability with given said and deviceid\n" + " add add mock system ability\n" + " remove remove mock system ability\n" + " load load local system ability\n" + " subscribe subscribe device info with options\n"; + + class MockLoadCallback : public SystemAbilityLoadCallbackStub { + public: + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject) override; + void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; + }; + + void MockLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const sptr& remoteObject) + { + cout << "OnLoadSystemAbilitySuccess systemAbilityId:" << systemAbilityId << " IRemoteObject result:" << + ((remoteObject != nullptr) ? "succeed" : "failed") << endl; + } + + void MockLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) + { + cout << "OnLoadSystemAbilityFail systemAbilityId:" << systemAbilityId << endl; + } + + class MockSaStatusChange : public SystemAbilityStatusChangeStub { + public: + void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + }; + + void MockSaStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) + { + cout << "OnAddSystemAbility said : " << systemAbilityId << endl; + } + + void MockSaStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) + { + cout << "OnRemoveSystemAbility said : " << systemAbilityId << endl; + } + + void DoHelp() + { + cout << HELP_CONTENT << endl; + } + + void DoList() + { + cout << "system ability list" << endl; + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + cout << "------------------samgr is unavailable-----------------------------------------" << endl; + return; + } + vector abilities = sm->ListSystemAbilities(); + cout << "total sa : " << abilities.size() << endl; + for (u16string said : abilities) { + cout << Str16ToStr8(said)<< endl; + } + } + + void DoPasre() + { + cout << "parse system ability profile" << endl; + sptr manager = SystemAbilityManager::GetInstance(); + if (manager == nullptr) { + cout << "------------------manager is unavailable-----------------------------------------" << endl; + return; + } + manager->Init(); + usleep(PARSE_WAIT_TIME); + cout << "init system ability profile size " << manager->saProfileMap_.size() << endl; + for (const auto& [said, saProfile] : manager->saProfileMap_) { + cout << "system ability profile said " << said << " process " << Str16ToStr8(saProfile.process) << endl; + } + } + + + void DoGet(int32_t said) + { + cout << "get system ability " << said < sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + cout << "------------------samgr is unavailable-----------------------------------------" << endl; + return; + } + + auto ability = sm->GetSystemAbility(said); + if (ability != nullptr) { + cout << "----------------- find sa : "<< said << "-----------------------------------------" << endl; + } else { + cout << "----------------- not find sa : "<< said << "--------------------------------------" << endl; + } + } + + void DoGetRmt(int32_t said, const string& deviceid) + { + cout << "get remote system ability " << said < sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + cout << "------------------samgr is unavailable-----------------------------------------" << endl; + return; + } + + auto ability = sm->GetSystemAbility(said, deviceid); + if (ability != nullptr) { + cout << "----------------- find remote sa : "<< said << "-----------------------------------------" << endl; + } else { + cout << "----------------- not find remote sa : "<< said << "--------------------------------------" << endl; + } + } + + void DoAdd() + { + cout << "add mock system ability : " << DISTRIBUTED_SCHED_TEST_TT_ID << endl; + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + cout << "------------------samgr is unavailable-----------------------------------------" << endl; + return; + } + sptr testAbility = new TestTransactionService(); + int32_t res = sm->AddSystemAbility(DISTRIBUTED_SCHED_TEST_TT_ID, testAbility); + cout << "add system ability result : " << ((res == 0) ? "succeed" : "failed") << endl; + } + + void DoRemove() + { + cout << "remove mock system ability" << endl; + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + cout << "------------------samgr is unavailable-----------------------------------------" << endl; + return; + } + sptr testAbility = new TestTransactionService(); + int32_t res = sm->AddSystemAbility(DISTRIBUTED_SCHED_TEST_TT_ID, testAbility); + res = sm->RemoveSystemAbility(DISTRIBUTED_SCHED_TEST_TT_ID); + cout << "remove system ability result : " << ((res == 0) ? "succeed" : "failed") << endl; + } + + void DoLoad(int32_t said) + { + cout << "load system ability " << said << endl; + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + cout << "------------------samgr is unavailable-----------------------------------------" << endl; + return; + } + + sptr callback = new MockLoadCallback(); + int32_t res = sm->LoadSystemAbility(said, callback); + cout << "load system ability result : " << ((res == 0) ? "succeed" : "failed") << endl; + } + + void DoSubscribe(int32_t said) + { + cout << "subscribe system ability" << endl; + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + cout << "------------------samgr is unavailable-----------------------------------------" << endl; + return; + } + + sptr listen = new MockSaStatusChange(); + int32_t res = sm->SubscribeSystemAbility(said, listen); + cout << "subscribe system ability result : " << ((res == 0) ? "succeed" : "failed") << endl; + } +} + +int main(int argc, char* argv[]) +{ + if (argc == 2 && strcmp(argv[1], "h") == 0) { + DoHelp(); + return 0; + } + if (argc == 2 && strcmp(argv[1], "ls") == 0) { + DoList(); + return 0; + } + if (argc == 2 && strcmp(argv[1], "parse") == 0) { + DoPasre(); + return 0; + } + if (argc > 2 && strcmp(argv[1], "get") == 0) { + int32_t said = DEFAULT_SA_ID; + StrToInt(argv[2], said); + DoGet(said); + return 0; + } + if (argc > 3 && strcmp(argv[1], "getrmt") == 0) { + int32_t said = DEFAULT_SA_ID; + StrToInt(argv[2], said); + string deviceid = argv[3]; + DoGetRmt(said, deviceid); + return 0; + } + if (argc == 2 && strcmp(argv[1], "add") == 0) { + DoAdd(); + return 0; + } + if (argc == 2 && strcmp(argv[1], "remove") == 0) { + DoRemove(); + return 0; + } + if (argc > 2 && strcmp(argv[1], "load") == 0) { + int32_t said = DEFAULT_SA_ID; + StrToInt(argv[2], said); + DoLoad(said); + return 0; + } + if (argc > 2 && strcmp(argv[1], "subscribe") == 0) { + int32_t said = DEFAULT_SA_ID; + StrToInt(argv[2], said); + DoSubscribe(said); + return 0; + } + cout << "invalid command, get help with arg 'h'"<< endl; + return 0; +} \ No newline at end of file diff --git a/test/resource/ohos_test.xml b/test/resource/ohos_test.xml index 3b843f584925d74a635c266be880dcb5f055b639..94cfb9d7378547d240407b5465f79d0dc0b4b18a 100644 --- a/test/resource/ohos_test.xml +++ b/test/resource/ohos_test.xml @@ -25,6 +25,8 @@