diff --git a/services/bundlemgr_lite/BUILD.gn b/services/bundlemgr_lite/BUILD.gn index 96be3f231afa0c11aacd0ea3549b26453b7e9d09..d8004cf9c864611581bd068be9da72a796c29fbf 100644 --- a/services/bundlemgr_lite/BUILD.gn +++ b/services/bundlemgr_lite/BUILD.gn @@ -95,5 +95,6 @@ lite_component("appexecfwk_services_lite") { ":bundlems", "tools:bm", "bundle_daemon:bundle_daemon", + "unittest:bundle_mgr_test", ] } diff --git a/services/bundlemgr_lite/unittest/BUILD.gn b/services/bundlemgr_lite/unittest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..78e4f0420e3cbcffe49374375241db850452e321 --- /dev/null +++ b/services/bundlemgr_lite/unittest/BUILD.gn @@ -0,0 +1,19 @@ +# Copyright (c) 2021 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. + +import("//build/lite/config/component/lite_component.gni") +import("//build/lite/config/test.gni") + +group("bundle_mgr_test") { + deps = [ "camera/bms_info_get_test:bundle_mgr_test_bmsGetInfoTest_group" ] +} diff --git a/services/bundlemgr_lite/unittest/camera/bms_info_get_test/BUILD.gn b/services/bundlemgr_lite/unittest/camera/bms_info_get_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..7d2ba3084546c554f4e4c3254c878868ae1e9a18 --- /dev/null +++ b/services/bundlemgr_lite/unittest/camera/bms_info_get_test/BUILD.gn @@ -0,0 +1,63 @@ +# Copyright (c) 2021 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. + +import("//build/lite/config/component/lite_component.gni") +import("//build/lite/config/test.gni") + +unittest("bundle_mgr_test_bmsGetInfoTest") { + output_extension = "bin" + output_dir = "$root_out_dir/test/unittest/BmsGetInfoTest" + + ldflags = [ + "-lstdc++", + "-lpthread", + "-lm", + ] + + include_dirs = [ + "${aafwk_lite_path}/interfaces/kits/want_lite", + "${aafwk_lite_path}/interfaces/innerkits/abilitymgr_lite", + "${aafwk_lite_path}/services/abilitymgr_lite/include", + "${appexecfwk_lite_path}/utils/bundle_lite", + "${appexecfwk_lite_path}/interfaces/kits/bundle_lite/", + "${appexecfwk_lite_path}/interfaces/innerkits/bundlemgr_lite/", + "${appexecfwk_lite_path}/kits/appkit_lite/appkit_utils/include", + "${appexecfwk_lite_path}/services/bundlemgr_lite/include", + "//foundation/distributedschedule/samgr_lite/interfaces/innerkits/samgr", + "//foundation/distributedschedule/samgr_lite/interfaces/innerkits/registry", + "//kernel/liteos_a/kernel/include", + "//kernel/liteos_a/kernel/common", + "//utils/native/lite/include", + "//third_party/cJSON", + "//third_party/googletest/googletest/include", + "//third_party/googletest/googletest/src", + ] + + sources = [ "bms_info_get_test.cpp" ] + + deps = [ + "${appexecfwk_lite_path}/frameworks/bundle_lite:bundle", + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", + "//build/lite/config/component/cJSON:cjson_shared", + "//foundation/communication/ipc_lite:liteipc_adapter", + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + + # "//test/xts/tools/hcpptest:hcpptest_main", + ] + + defines = [ "OHOS_APPEXECFWK_BMS_BUNDLEMANAGER" ] +} + +group("bundle_mgr_test_bmsGetInfoTest_group") { + deps = [ ":bundle_mgr_test_bmsGetInfoTest" ] +} diff --git a/services/bundlemgr_lite/unittest/camera/bms_info_get_test/bms_info_get_test.cpp b/services/bundlemgr_lite/unittest/camera/bms_info_get_test/bms_info_get_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a95f4c7f7884030adb270e06f40e0555c46c86c --- /dev/null +++ b/services/bundlemgr_lite/unittest/camera/bms_info_get_test/bms_info_get_test.cpp @@ -0,0 +1,242 @@ +/* + * Copyright (c) 2021 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 +#include + +#include + +#include "adapter.h" +#include "appexecfwk_errors.h" +#include "bundle_manager.h" +#include "element_name.h" +#include "securec.h" + +using namespace testing::ext; + +namespace OHOS { + const std::string HAP_PATH = "/nfs/bundle/camera_signed.hap"; + const std::string BUNDLE_NAME = "com.huawei.camera"; + const std::string MODULE_TYPE = "entry"; + const std::string METADATA_KEY = "camera"; + const std::string INVALID_METADATA_KEY = "hello"; + const std::string ABILITY_NAME = ".MainAbility"; + const char INVALID_BUNDLE_NAME[] = "com.huawei.testcase"; + const std::string MAX_INVALID_BUNDLE_NAME = std::string("com.example.testcasecom.example.testcasecom.example") + + ".testcasecom.example.testcasecom.example.testcasecom.example.testcasecom.example"; + const uint32_t WAIT_TIMEOUT = 30; + const int32_t BASE_UID = 10000; + const int32_t MAX_UID = 10100; + static sem_t g_sem; + + class BmsInfoGetTest : public testing::Test { + public: + static void SetUpTestCase(); + static void TearDownTestCase(); + static void InstallBundle(); + static void UninstallBundle(); + }; + + static void UninstallCallback(const uint8_t resultCode, const void *resultMessage) + { + std::string strMessage = reinterpret_cast(resultMessage); + if (!strMessage.empty()) { + printf("uninstall resultMessage is %s\n", strMessage.c_str()); + } + sem_post(&g_sem); + } + + static void InstallCallback(const uint8_t resultCode, const void *resultMessage) + { + std::string strMessage = reinterpret_cast(resultMessage); + if (!strMessage.empty()) { + printf("install resultMessage is %s\n", strMessage.c_str()); + } + sem_post(&g_sem); + } + + void BmsInfoGetTest::InstallBundle() + { + sem_init(&g_sem, 0, 0); + InstallParam installParam = { + .installLocation = 1, + .keepData = false + }; + Install(HAP_PATH.c_str(), &installParam, InstallCallback); + struct timespec ts = {}; + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += WAIT_TIMEOUT; + sem_timedwait(&g_sem, &ts); + } + + void BmsInfoGetTest::UninstallBundle() + { + sem_init(&g_sem, 0, 0); + InstallParam installParam = { + .installLocation = 1, + .keepData = false + }; + Uninstall(BUNDLE_NAME.c_str(), &installParam, UninstallCallback); + struct timespec ts = {}; + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += WAIT_TIMEOUT; + sem_timedwait(&g_sem, &ts); + } + + void BmsInfoGetTest::SetUpTestCase() + { + // need to set debug mode + InstallBundle(); + } + + void BmsInfoGetTest::TearDownTestCase() + { + UninstallBundle(); + } + + /** + * @tc.name: GetBundleInfo001 + * @tc.desc: get bundleInfo when bundle is exists, flag is 0 + * @tc.type: FUNC + */ + HWTEST_F(BmsInfoGetTest, GetBundleInfo001, TestSize.Level0) { + printf("begin to excute GetBundleInfo001\n"); + BundleInfo bundleInfo; + (void) memset_s(&bundleInfo, sizeof(BundleInfo), 0, sizeof(BundleInfo)); + uint8_t ret = GetBundleInfo(BUNDLE_NAME.c_str(), 0, &bundleInfo); + ASSERT_EQ(ret, 0); + EXPECT_EQ(bundleInfo.bundleName, BUNDLE_NAME); + EXPECT_EQ(bundleInfo.numOfAbility, 0); + ClearBundleInfo(&bundleInfo); + printf("end to excute GetBundleInfo001\n"); + } + + /** + * @tc.name: GetBundleInfo002 + * @tc.desc: get bundleInfo when bundle is exists, flag is 1 + * @tc.type: FUNC + */ + HWTEST_F(BmsInfoGetTest, GetBundleInfo002, TestSize.Level0) { + printf("begin to excute GetBundleInfo002\n"); + BundleInfo bundleInfo; + (void) memset_s(&bundleInfo, sizeof(BundleInfo), 0, sizeof(BundleInfo)); + uint8_t ret = GetBundleInfo(BUNDLE_NAME.c_str(), 1, &bundleInfo); + ASSERT_EQ(ret, 0); + EXPECT_EQ(bundleInfo.bundleName, BUNDLE_NAME); + ClearBundleInfo(&bundleInfo); + printf("end to excute GetBundleInfo002\n"); + } + + /** + * @tc.name: GetBundleInfo003 + * @tc.desc: get bundleInfo when param is invalid + * @tc.type: FUNC + */ + HWTEST_F(BmsInfoGetTest, GetBundleInfo003, TestSize.Level0) { + printf("begin to excute GetBundleInfo003\n"); + BundleInfo bundleInfo; + (void) memset_s(&bundleInfo, sizeof(BundleInfo), 0, sizeof(BundleInfo)); + uint8_t ret = GetBundleInfo(nullptr, 1, &bundleInfo); + EXPECT_EQ(ret, ERR_APPEXECFWK_OBJECT_NULL); + ret = GetBundleInfo(BUNDLE_NAME.c_str(), 1, nullptr); + EXPECT_EQ(ret, ERR_APPEXECFWK_OBJECT_NULL); + ret = GetBundleInfo(BUNDLE_NAME.c_str(), 2, &bundleInfo); + EXPECT_EQ(ret, ERR_APPEXECFWK_QUERY_PARAMETER_ERROR); + ret = GetBundleInfo(MAX_INVALID_BUNDLE_NAME.c_str(), 1, &bundleInfo); + EXPECT_EQ(ret, ERR_APPEXECFWK_QUERY_PARAMETER_ERROR); + printf("end to excute GetBundleInfo003\n"); + } + + /** + * @tc.name: GetBundleInfo004 + * @tc.desc: get bundleInfo when bundle is not exists + * @tc.type: FUNC + */ + HWTEST_F(BmsInfoGetTest, GetBundleInfo004, TestSize.Level0) { + printf("begin to excute GetBundleInfo004\n"); + BundleInfo bundleInfo; + (void) memset_s(&bundleInfo, sizeof(BundleInfo), 0, sizeof(BundleInfo)); + uint8_t ret = GetBundleInfo(INVALID_BUNDLE_NAME, 1, &bundleInfo); + EXPECT_EQ(ret, ERR_APPEXECFWK_QUERY_NO_INFOS); + printf("end to excute GetBundleInfo004\n"); + } + + /** + * @tc.name: GetBundleInfos001 + * @tc.desc: get bundleInfos when bundle is exists, flag is 0 + * @tc.type: FUNC + */ + HWTEST_F(BmsInfoGetTest, GetBundleInfos001, TestSize.Level0) { + printf("begin to excute GetBundleInfos001\n"); + BundleInfo *bundleInfo = nullptr; + int32_t len = -1; + uint8_t ret = GetBundleInfos(0, &bundleInfo, &len); + ASSERT_EQ(ret, 0); + EXPECT_GE(len, 1); + for (int32_t i = 0; i < len; i++) { + if (bundleInfo[i].bundleName != nullptr && strcmp(bundleInfo[i].bundleName, BUNDLE_NAME.c_str())) { + EXPECT_EQ(bundleInfo[i].numOfModule, 1); + EXPECT_EQ(bundleInfo[i].moduleInfos[0].moduleType, MODULE_TYPE); + EXPECT_EQ(bundleInfo[i].numOfAbility, 0); + } + ClearBundleInfo(bundleInfo + i); + } + AdapterFree(bundleInfo); + printf("end to excute GetBundleInfos001\n"); + } + + /** + * @tc.name: GetBundleInfos002 + * @tc.desc: get bundleInfos when bundle is exists, flag is 1 + * @tc.type: FUNC + */ + HWTEST_F(BmsInfoGetTest, GetBundleInfos002, TestSize.Level0) { + printf("begin to excute GetBundleInfos002\n"); + BundleInfo *bundleInfo = nullptr; + int32_t len = -1; + uint8_t ret = GetBundleInfos(1, &bundleInfo, &len); + ASSERT_EQ(ret, 0); + EXPECT_GE(len, 1); + for (int32_t i = 0; i < len; i++) { + if (bundleInfo[i].bundleName != nullptr && strcmp(bundleInfo[i].bundleName, BUNDLE_NAME.c_str())) { + EXPECT_EQ(bundleInfo[i].numOfModule, 1); + EXPECT_EQ(bundleInfo[i].moduleInfos[0].moduleType, MODULE_TYPE); + } + ClearBundleInfo(bundleInfo + i); + } + AdapterFree(bundleInfo); + printf("end to excute GetBundleInfos002\n"); + } + + /** + * @tc.name: GetBundleInfos003 + * @tc.desc: get bundleInfos when no bundle exists + * @tc.type: FUNC + */ + HWTEST_F(BmsInfoGetTest, GetBundleInfos003, TestSize.Level0) { + printf("begin to excute GetBundleInfos003\n"); + int32_t len = -1; + BundleInfo *bundleInfo = nullptr; + uint8_t ret = GetBundleInfos(1, nullptr, &len); + EXPECT_EQ(ret, ERR_APPEXECFWK_OBJECT_NULL); + ret = GetBundleInfos(1, &bundleInfo, nullptr); + EXPECT_EQ(ret, ERR_APPEXECFWK_OBJECT_NULL); + printf("end to excute GetBundleInfos003\n"); + } +} \ No newline at end of file