diff --git a/BUILD.gn b/BUILD.gn index 1d7cc5cdf6fa0f713e3e387ee994035e87ae47ef..da01e997332026566518f290820bbedfb48fb0d7 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -29,6 +29,7 @@ group("accesstoken_build_module_test") { "interfaces/innerkits/token_setproc/test:unittest", "services/accesstokenmanager/test:unittest", "services/common/database/test:unittest", + "services/common/json_parse/test:unittest" ] if (ability_base_enable == true) { deps += [ diff --git a/services/common/json_parse/test/BUILD.gn b/services/common/json_parse/test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..bf583367baf5edfd9a803dd2168bb7eaf47c857d --- /dev/null +++ b/services/common/json_parse/test/BUILD.gn @@ -0,0 +1,74 @@ +# Copyright (c) 2025 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/test.gni") +import("../../../../access_token.gni") + +config("accesstoken_json_parse_config") { + visibility = [ ":*" ] + include_dirs = [ "include" ] +} + +ohos_unittest("libjsonparse_test") { + subsystem_name = "accesscontrol" + part_name = "access_token" + module_out_path = part_name + "/" + part_name + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + branch_protector_ret = "pac_ret" + + include_dirs = [ + "${access_token_path}/frameworks/common/include", + "${access_token_path}/interfaces/innerkits/accesstoken/include", + "${access_token_path}/services/accesstokenmanager/main/cpp/include/token", + "${access_token_path}/services/common/json_parse/include", + ] + + sources = [ + "${access_token_path}/services/common/json_parse/src/cjson_utils.cpp", + "${access_token_path}/services/common/json_parse/src/json_parse_loader.cpp", + "unittest/cjson_utils_test.cpp", + "unittest/json_parse_loader_test.cpp", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + configs = [ + "${access_token_path}/config:access_token_compile_flags", + "${access_token_path}/config:coverage_flags", + ] + public_configs = [ ":accesstoken_json_parse_config" ] + + deps = [ + "${access_token_path}/frameworks/common:accesstoken_common_cxx", + ] + + external_deps = [ + "cJSON:cjson", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_single", + ] + + if (customization_config_policy_enable) { + cflags_cc += [ "-DCUSTOMIZATION_CONFIG_POLICY_ENABLE" ] + external_deps += [ "config_policy:configpolicy_util" ] + } +} + +group("unittest") { + testonly = true + deps = [ ":libjsonparse_test" ] +} diff --git a/services/common/json_parse/test/unittest/cjson_utils_test.cpp b/services/common/json_parse/test/unittest/cjson_utils_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b877876883c96791547195d33e47e03940a1a269 --- /dev/null +++ b/services/common/json_parse/test/unittest/cjson_utils_test.cpp @@ -0,0 +1,315 @@ +/* + * Copyright (c) 2025 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 "cjson_utils.h" + +using namespace testing::ext; + +namespace OHOS { +namespace Security { +namespace AccessToken { + +class CJsonUtilsTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + + void SetUp(); + void TearDown(); +}; + +void CJsonUtilsTest::SetUpTestCase() {} +void CJsonUtilsTest::TearDownTestCase() {} +void CJsonUtilsTest::SetUp() {} +void CJsonUtilsTest::TearDown() {} + +/* + * @tc.name: CreateJsonFromString + * @tc.desc: CreateJsonFromString + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, CreateJsonFromStringTest001, TestSize.Level1) +{ + std::string test; + EXPECT_EQ(nullptr, CreateJsonFromString(test)); +} + +/* + * @tc.name: PackJsonToString + * @tc.desc: PackJsonToString + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, PackJsonToStringTest001, TestSize.Level1) +{ + std::string res = PackJsonToString(nullptr); + EXPECT_EQ(res.size(), 0); + + FreeJsonString(nullptr); +} + +/* + * @tc.name: GetObjFromJson + * @tc.desc: GetObjFromJson + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, GetObjFromJsonTest001, TestSize.Level1) +{ + std::string test; + EXPECT_EQ(nullptr, GetObjFromJson(nullptr, test)); + + test = "test1"; + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(true, AddStringToJson(jsonInner, "test0", "0")); + EXPECT_EQ(nullptr, GetObjFromJson(jsonInner, test)); + + test = "test0"; + EXPECT_EQ(nullptr, GetObjFromJson(jsonInner, test)); +} + +/* + * @tc.name: GetArrayFromJson + * @tc.desc: GetArrayFromJson + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, GetArrayFromJsonTest001, TestSize.Level1) +{ + std::string test; + EXPECT_EQ(nullptr, GetArrayFromJson(nullptr, test)); + + test = "test1"; + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(true, AddStringToJson(jsonInner, "test0", "0")); + EXPECT_EQ(nullptr, GetArrayFromJson(jsonInner, test)); + + test = "test0"; + EXPECT_EQ(nullptr, GetArrayFromJson(jsonInner, test)); +} + +/* + * @tc.name: GetStringFromJson + * @tc.desc: GetStringFromJson + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, GetStringFromJsonTest001, TestSize.Level1) +{ + std::string test; + std::string res; + EXPECT_EQ(false, GetStringFromJson(nullptr, test, res)); + + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(true, AddStringToJson(jsonInner, "test0", "0")); + EXPECT_EQ(false, GetStringFromJson(jsonInner.get(), test, res)); + + test = "test1"; + EXPECT_EQ(false, GetStringFromJson(jsonInner.get(), test, res)); + + CJsonUnique jsonArray = CreateJsonArray(); + ASSERT_EQ(true, AddObjToJson(jsonArray, "test1", jsonInner)); + EXPECT_EQ(false, GetStringFromJson(jsonArray.get(), test, res)); +} + +/* + * @tc.name: GetIntFromJson + * @tc.desc: GetIntFromJson + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, GetIntFromJsonTest001, TestSize.Level1) +{ + std::string test; + int32_t res; + EXPECT_EQ(false, GetIntFromJson(nullptr, test, res)); + + test = "test1"; + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(true, AddStringToJson(jsonInner, "test0", "abc")); + EXPECT_EQ(false, GetIntFromJson(jsonInner, test, res)); + + test = "test0"; + EXPECT_EQ(false, GetIntFromJson(jsonInner, test, res)); +} + +/* + * @tc.name: GetUnsignedIntFromJson + * @tc.desc: GetUnsignedIntFromJson + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, GetUnsignedIntFromJsonTest001, TestSize.Level1) +{ + std::string test; + uint32_t res; + EXPECT_EQ(false, GetUnsignedIntFromJson(nullptr, test, res)); + + test = "test1"; + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(true, AddStringToJson(jsonInner, "test0", "abc")); + EXPECT_EQ(false, GetUnsignedIntFromJson(jsonInner, test, res)); + + test = "test0"; + EXPECT_EQ(false, GetUnsignedIntFromJson(jsonInner, test, res)); +} + +/* + * @tc.name: GetBoolFromJson + * @tc.desc: GetBoolFromJson + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, GetBoolFromJsonTest001, TestSize.Level1) +{ + std::string test; + bool res; + EXPECT_EQ(false, GetBoolFromJson(nullptr, test, res)); + + test = "test1"; + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(true, AddStringToJson(jsonInner, "test0", "0")); + EXPECT_EQ(false, GetBoolFromJson(jsonInner, test, res)); + + test = "test0"; + EXPECT_EQ(false, GetBoolFromJson(jsonInner, test, res)); +} + +/* + * @tc.name: GetBoolFromJson + * @tc.desc: GetBoolFromJson + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, GetBoolFromJsonTest002, TestSize.Level1) +{ + std::string test = "test1"; + bool res; + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(true, AddBoolToJson(jsonInner, test, true)); + + EXPECT_EQ(true, GetBoolFromJson(jsonInner, test, res)); + EXPECT_EQ(res, true); +} + +/* + * @tc.name: AddObjToJson + * @tc.desc: AddObjToJson + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, AddObjToJsonTest001, TestSize.Level1) +{ + ASSERT_EQ(false, AddObjToJson(nullptr, "", nullptr)); + std::string test = "test1"; + ASSERT_EQ(false, AddObjToJson(nullptr, test, nullptr)); + + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(true, AddStringToJson(jsonInner, "test0", "0")); + ASSERT_EQ(true, AddStringToJson(jsonInner, "test1", "1")); + + CJsonUnique jsonArray = CreateJsonArray(); + ASSERT_EQ(true, AddObjToJson(jsonArray, "test1", jsonInner)); + ASSERT_EQ(true, AddObjToJson(jsonArray, "test1", jsonInner)); +} + +/* + * @tc.name: AddObjToArray + * @tc.desc: AddObjToArray + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, AddObjToArrayTest001, TestSize.Level1) +{ + ASSERT_EQ(false, AddObjToArray(nullptr, nullptr)); + + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(false, AddObjToArray(nullptr, jsonInner.get())); +} + +/* + * @tc.name: AddStringToJson + * @tc.desc: AddStringToJson + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, AddStringToJsonTest001, TestSize.Level1) +{ + ASSERT_EQ(false, AddStringToJson(nullptr, "", "")); + ASSERT_EQ(false, AddStringToJson(nullptr, "test0", "test0")); + + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(true, AddStringToJson(jsonInner, "test0", "test0")); + // twice + ASSERT_EQ(true, AddStringToJson(jsonInner, "test0", "test0")); +} + +/* + * @tc.name: AddBoolToJson + * @tc.desc: AddBoolToJson + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, AddBoolToJsonTest001, TestSize.Level1) +{ + ASSERT_EQ(false, AddBoolToJson(nullptr, "", true)); + ASSERT_EQ(false, AddBoolToJson(nullptr, "test0", true)); + + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(true, AddBoolToJson(jsonInner, "test0", true)); + // twice + ASSERT_EQ(true, AddBoolToJson(jsonInner, "test0", true)); +} + +/* + * @tc.name: AddIntToJson + * @tc.desc: AddIntToJson + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, AddIntToJsonTest001, TestSize.Level1) +{ + ASSERT_EQ(false, AddIntToJson(nullptr, "", 0)); + ASSERT_EQ(false, AddIntToJson(nullptr, "test0", 0)); + + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(true, AddIntToJson(jsonInner, "test0", 0)); + // twice + ASSERT_EQ(true, AddIntToJson(jsonInner, "test0", 0)); +} + +/* + * @tc.name: AddUnsignedIntToJson + * @tc.desc: AddUnsignedIntToJson + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(CJsonUtilsTest, AddUnsignedIntToJsonTest001, TestSize.Level1) +{ + ASSERT_EQ(false, AddUnsignedIntToJson(nullptr, "", 0)); + ASSERT_EQ(false, AddUnsignedIntToJson(nullptr, "test0", 0)); + + CJsonUnique jsonInner = CreateJson(); + ASSERT_EQ(true, AddUnsignedIntToJson(jsonInner, "test0", 0)); + // twice + ASSERT_EQ(true, AddUnsignedIntToJson(jsonInner, "test0", 0)); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/common/json_parse/test/unittest/json_parse_loader_test.cpp b/services/common/json_parse/test/unittest/json_parse_loader_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f90945fb1655af77a2498a393f20bca27a42cce --- /dev/null +++ b/services/common/json_parse/test/unittest/json_parse_loader_test.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2025 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 + +#define private public +#include "json_parse_loader.h" +#undef private + +using namespace testing::ext; + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +constexpr const char* TEST_FILE_PATH = "/data/test/abcdefg.txt"; +} + +class JsonParseLoaderTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + + void SetUp(); + void TearDown(); +}; + +void JsonParseLoaderTest::SetUpTestCase() {} +void JsonParseLoaderTest::TearDownTestCase() {} +void JsonParseLoaderTest::SetUp() {} +void JsonParseLoaderTest::TearDown() {} + +/* + * @tc.name: IsDirExsit + * @tc.desc: IsDirExsit + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(JsonParseLoaderTest, IsDirExsitTest001, TestSize.Level1) +{ + ConfigPolicLoader loader; + EXPECT_FALSE(loader.IsDirExsit("")); + int32_t fd = open(TEST_FILE_PATH, O_RDWR | O_CREAT); + EXPECT_NE(-1, fd); + + EXPECT_FALSE(loader.IsDirExsit(TEST_FILE_PATH)); +} + +#ifdef CUSTOMIZATION_CONFIG_POLICY_ENABLE +/* + * @tc.name: GetConfigValueFromFile + * @tc.desc: GetConfigValueFromFile + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(JsonParseLoaderTest, GetConfigValueFromFileTest001, TestSize.Level1) +{ + ConfigPolicLoader loader; + AccessTokenConfigValue config; + EXPECT_FALSE(loader.GetConfigValueFromFile(ServiceType::ACCESSTOKEN_SERVICE, "", config)); +} +#endif // CUSTOMIZATION_CONFIG_POLICY_ENABLE + +/* + * @tc.name: ParserNativeRawData + * @tc.desc: ParserNativeRawData + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(JsonParseLoaderTest, ParserNativeRawDataTest001, TestSize.Level1) +{ + ConfigPolicLoader loader; + std::vector tokenInfos; + EXPECT_FALSE(loader.ParserNativeRawData("", tokenInfos)); +} + +/* + * @tc.name: ParserDlpPermsRawData + * @tc.desc: ParserDlpPermsRawData + * @tc.type: FUNC + * @tc.require: TDD coverage + */ +HWTEST_F(JsonParseLoaderTest, ParserDlpPermsRawDataTest001, TestSize.Level1) +{ + ConfigPolicLoader loader; + std::vector dlpPerms; + EXPECT_FALSE(loader.ParserDlpPermsRawData("", dlpPerms)); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/common/json_parse/unittest/json_parse_test.cpp b/services/common/json_parse/test/unittest/json_parse_test.cpp similarity index 100% rename from services/common/json_parse/unittest/json_parse_test.cpp rename to services/common/json_parse/test/unittest/json_parse_test.cpp diff --git a/services/privacymanager/test/coverage/permission_record_manager_coverage_test.cpp b/services/privacymanager/test/coverage/permission_record_manager_coverage_test.cpp index 883515ea2b7e98dec3de579c3e28d5dde354aa97..b23457fedf6a1c1ca021e07ba41aca9c4d1110c0 100644 --- a/services/privacymanager/test/coverage/permission_record_manager_coverage_test.cpp +++ b/services/privacymanager/test/coverage/permission_record_manager_coverage_test.cpp @@ -35,6 +35,7 @@ #include "state_change_callback.h" #include "time_util.h" #include "token_setproc.h" +#include "on_permission_used_record_callback_stub.h" using namespace testing; using namespace testing::ext; @@ -56,6 +57,7 @@ static constexpr int32_t PERMISSION_USED_TYPE_VALUE = 1; static constexpr int32_t PERMISSION_USED_TYPE_WITH_PICKER_TYPE_VALUE = 3; static constexpr uint32_t RANDOM_TOKENID = 123; static constexpr int32_t TEST_USER_ID_11 = 11; +static constexpr int32_t INVALID_CODE = 9999; static PermissionStateFull g_testState1 = { .permissionName = "ohos.permission.CAMERA", .isGeneral = true, @@ -476,6 +478,9 @@ HWTEST_F(PermissionRecordManagerTest, OnApplicationStateChanged001, TestSize.Lev appStateData.accessTokenId = tokenId; observer.OnAppStopped(appStateData); + appStateData.state = INVALID_CODE; + observer.OnAppStopped(appStateData); + usleep(500000); // 500000us = 0.5s ASSERT_EQ(PERM_ACTIVE_IN_BACKGROUND, callbackPtr->type_); @@ -857,6 +862,130 @@ HWTEST_F(PermissionRecordManagerTest, GetRecords004, TestSize.Level1) EXPECT_EQ(ERR_PARAM_INVALID, PermissionRecordManager::GetInstance().GetPermissionUsedRecords(request, result)); } +/** + * @tc.name: GetRecords005 + * @tc.desc: test ERR_PARAM_INVALID + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PermissionRecordManagerTest, GetRecords005, TestSize.Level1) +{ + PermissionRecordManager::GetInstance().UpdatePermRecImmediately(); + + AddPermParamInfo info; + + PermissionRecord result; + EXPECT_EQ(ERR_PARAM_INVALID, PermissionRecordManager::GetInstance().GetPermissionRecord(info, result)); + + PermissionRecordManager::GetInstance().ExecuteDeletePermissionRecordTask(); +} + +/** + * @tc.name: SetPermissionUsedRecordToggleStatus001 + * @tc.desc: test ERR_PARAM_INVALID + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PermissionRecordManagerTest, SetPermissionUsedRecordToggleStatus001, TestSize.Level1) +{ + MockNativeToken mock("privacy_service"); + + EXPECT_EQ(Constant::SUCCESS, PermissionRecordManager::GetInstance().SetPermissionUsedRecordToggleStatus(0, false)); + EXPECT_EQ(Constant::SUCCESS, PermissionRecordManager::GetInstance().SetPermissionUsedRecordToggleStatus(0, true)); + EXPECT_EQ(Constant::SUCCESS, PermissionRecordManager::GetInstance().SetPermissionUsedRecordToggleStatus(0, true)); + + AccessTokenIDEx tokenIdEx = PrivacyTestCommon::GetHapTokenIdFromBundle( + g_InfoParms1.userID, g_InfoParms1.bundleName, g_InfoParms1.instIndex); + AccessTokenID tokenID = tokenIdEx.tokenIdExStruct.tokenID; + ASSERT_NE(INVALID_TOKENID, tokenID); + + GeneratePermissionRecord(tokenID); + + EXPECT_EQ(Constant::SUCCESS, PermissionRecordManager::GetInstance().SetPermissionUsedRecordToggleStatus(1, false)); + EXPECT_EQ(Constant::SUCCESS, PermissionRecordManager::GetInstance().SetPermissionUsedRecordToggleStatus(1, true)); + EXPECT_EQ(Constant::SUCCESS, + PermissionRecordManager::GetInstance().SetPermissionUsedRecordToggleStatus(105, false)); + EXPECT_EQ(Constant::SUCCESS, PermissionRecordManager::GetInstance().SetPermissionUsedRecordToggleStatus(105, true)); +} + +/** + * @tc.name: GetPermissionUsedRecordToggleStatus001 + * @tc.desc: test ERR_PARAM_INVALID + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PermissionRecordManagerTest, GetPermissionUsedRecordToggleStatus001, TestSize.Level1) +{ + bool res = true; + EXPECT_EQ(Constant::SUCCESS, PermissionRecordManager::GetInstance().GetPermissionUsedRecordToggleStatus(0, res)); + EXPECT_EQ(Constant::SUCCESS, PermissionRecordManager::GetInstance().GetPermissionUsedRecordToggleStatus(0, res)); + EXPECT_EQ(Constant::SUCCESS, PermissionRecordManager::GetInstance().SetPermissionUsedRecordToggleStatus(106, res)); + EXPECT_EQ(Constant::SUCCESS, PermissionRecordManager::GetInstance().SetPermissionUsedRecordToggleStatus(106, res)); + + PermissionRecordManager::GetInstance().UpdatePermUsedRecToggleStatusMapFromDb(); +} + +class TestUsedRecordCallback : public OnPermissionUsedRecordCallbackStub { +public: + TestUsedRecordCallback() = default; + virtual ~TestUsedRecordCallback() = default; + void OnQueried(ErrCode code, PermissionUsedResult& result) {} +}; + +/** + * @tc.name: GetPermissionUsedRecordsAsync001 + * @tc.desc: test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PermissionRecordManagerTest, GetPermissionUsedRecordsAsync001, TestSize.Level1) +{ + AccessTokenIDEx tokenIdEx = PrivacyTestCommon::GetHapTokenIdFromBundle( + g_InfoParms1.userID, g_InfoParms1.bundleName, g_InfoParms1.instIndex); + AccessTokenID tokenID = tokenIdEx.tokenIdExStruct.tokenID; + ASSERT_NE(INVALID_TOKENID, tokenID); + + PermissionUsedRequest request; + request.tokenId = tokenID; + request.isRemote = false; + request.flag = PermissionUsageFlag::FLAG_PERMISSION_USAGE_SUMMARY_IN_SCREEN_LOCKED; + + OHOS::sptr cb(new TestUsedRecordCallback()); + EXPECT_EQ(Constant::SUCCESS, PermissionRecordManager::GetInstance().GetPermissionUsedRecordsAsync(request, cb)); +} + +/** + * @tc.name: GetLockScreenStatus001 + * @tc.desc: test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PermissionRecordManagerTest, GetLockScreenStatus001, TestSize.Level1) +{ + EXPECT_NE(PrivacyError::ERR_PARAM_INVALID, PermissionRecordManager::GetInstance().GetLockScreenStatus(true)); +} + +/** + * @tc.name: SetHapWithFGReminder001 + * @tc.desc: test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PermissionRecordManagerTest, SetHapWithFGReminder001, TestSize.Level1) +{ + EXPECT_NE(PrivacyError::ERR_PARAM_INVALID, PermissionRecordManager::GetInstance().SetHapWithFGReminder(123, true)); + + AccessTokenIDEx tokenIdEx = PrivacyTestCommon::GetHapTokenIdFromBundle( + g_InfoParms1.userID, g_InfoParms1.bundleName, g_InfoParms1.instIndex); + AccessTokenID tokenID = tokenIdEx.tokenIdExStruct.tokenID; + ASSERT_NE(INVALID_TOKENID, tokenID); + + EXPECT_NE(PrivacyError::ERR_PARAM_INVALID, + PermissionRecordManager::GetInstance().SetHapWithFGReminder(tokenID, true)); + EXPECT_NE(PrivacyError::ERR_PARAM_INVALID, + PermissionRecordManager::GetInstance().SetHapWithFGReminder(tokenID, false)); +} + /** * @tc.name: GetRecordsFromLocalDBTest001 * @tc.desc: test GetRecordsFromLocalDB: token = 0 diff --git a/services/privacymanager/test/unittest/privacy_manager_service_test.cpp b/services/privacymanager/test/unittest/privacy_manager_service_test.cpp index a0839f50f9090615f9f5fc4843218681a0d02cf3..24060de25061a43b0b9792abd19f967869fd1a3f 100644 --- a/services/privacymanager/test/unittest/privacy_manager_service_test.cpp +++ b/services/privacymanager/test/unittest/privacy_manager_service_test.cpp @@ -22,12 +22,12 @@ #include "on_permission_used_record_callback_stub.h" #define private public #include "permission_record_manager.h" +#include "privacy_manager_service.h" #undef private #include "perm_active_status_change_callback_stub.h" #include "perm_active_status_change_callback.h" #include "privacy_error.h" #include "privacy_field_const.h" -#include "privacy_manager_service.h" #include "privacy_test_common.h" #include "proxy_death_callback_stub.h" #include "state_change_callback.h" @@ -46,6 +46,9 @@ constexpr const char* CAMERA_PERMISSION_NAME = "ohos.permission.CAMERA"; constexpr const char* MICROPHONE_PERMISSION_NAME = "ohos.permission.MICROPHONE"; constexpr const char* LOCATION_PERMISSION_NAME = "ohos.permission.LOCATION"; static const uint32_t PERM_LIST_SIZE_MAX = 1024; +static constexpr int32_t COMMON_EVENT_SERVICE_ID = 3299; +static constexpr int32_t SCREENLOCK_SERVICE_ID = 3704; +static constexpr int32_t INVALID_CODE = 999; static PermissionStateFull g_testState = { .permissionName = "ohos.permission.CAMERA", .isGeneral = true, @@ -471,6 +474,24 @@ HWTEST_F(PrivacyManagerServiceTest, SetPermissionUsedRecordToggleStatusInner002, ASSERT_EQ(PrivacyError::ERR_NOT_SYSTEM_APP, ret); } +/** + * @tc.name: SetPermissionUsedRecordToggleStatusInner003 + * @tc.desc: SetPermissionUsedRecordToggleStatusInner test. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PrivacyManagerServiceTest, SetPermissionUsedRecordToggleStatusInner003, TestSize.Level1) +{ + int32_t userID = 1; + bool status = true; + + std::vector reqPerm; + MockHapToken mock("SetPermissionUsedRecordToggleStatusInner003", reqPerm, true); // set self tokenID to system app + + int32_t ret = privacyManagerService_->SetPermissionUsedRecordToggleStatus(userID, status); + ASSERT_EQ(RET_SUCCESS, ret); +} + /** * @tc.name: GetPermissionUsedRecordToggleStatusInner001 * @tc.desc: GetPermissionUsedRecordToggleStatusInner test. @@ -504,6 +525,24 @@ HWTEST_F(PrivacyManagerServiceTest, GetPermissionUsedRecordToggleStatusInner002, ASSERT_EQ(PrivacyError::ERR_NOT_SYSTEM_APP, ret); } +/** + * @tc.name: GetPermissionUsedRecordToggleStatusInner003 + * @tc.desc: GetPermissionUsedRecordToggleStatusInner test. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PrivacyManagerServiceTest, GetPermissionUsedRecordToggleStatusInner003, TestSize.Level1) +{ + int32_t userID = 1; + bool status = true; + + std::vector reqPerm; + MockHapToken mock("GetPermissionUsedRecordToggleStatusInner003", reqPerm, true); // set self tokenID to system app + + int32_t ret = privacyManagerService_->GetPermissionUsedRecordToggleStatus(userID, status); + ASSERT_EQ(RET_SUCCESS, ret); +} + /** * @tc.name: StartUsingPermissionInner001 * @tc.desc: StartUsingPermissionInner test. @@ -749,6 +788,21 @@ HWTEST_F(PrivacyManagerServiceTest, RemovePermissionUsedRecordsInner003, TestSiz ASSERT_EQ(PrivacyError::ERR_NOT_SYSTEM_APP, privacyManagerService_->RemovePermissionUsedRecords(tokenID)); } +/** + * @tc.name: RemovePermissionUsedRecordsInner004 + * @tc.desc: RemovePermissionUsedRecordsInner test. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PrivacyManagerServiceTest, RemovePermissionUsedRecordsInner004, TestSize.Level1) +{ + AccessTokenID tokenID = 123; // 123 is invalid tokenID + + std::vector reqPerm = {"ohos.permission.PERMISSION_USED_STATS"}; + MockHapToken mock("RemovePermissionUsedRecordsInner004", reqPerm, true); // set self tokenID to system app + ASSERT_EQ(RET_SUCCESS, privacyManagerService_->RemovePermissionUsedRecords(tokenID)); +} + /** * @tc.name: GetPermissionUsedRecordsInner001 * @tc.desc: GetPermissionUsedRecordsInner test. @@ -1110,6 +1164,25 @@ HWTEST_F(PrivacyManagerServiceTest, SetMutePolicyInner001, TestSize.Level1) EXPECT_EQ(PrivacyError::ERR_PERMISSION_DENIED, ret); } +/** + * @tc.name: SetMutePolicyInner002 + * @tc.desc: SetMutePolicyInner test. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PrivacyManagerServiceTest, SetMutePolicyInner002, TestSize.Level1) +{ + AccessTokenID tokenID = 123; // 123 is invalid tokenID + uint32_t policyType = 0; + uint32_t callerType = 0; + bool isMute = false; + + MockNativeToken mock("camera_service"); + + int32_t ret = privacyManagerService_->SetMutePolicy(policyType, callerType, isMute, tokenID); + EXPECT_NE(PrivacyError::ERR_PERMISSION_DENIED, ret); +} + /** * @tc.name: SetHapWithFGReminderInner001 * @tc.desc: SetHapWithFGReminderInner test. @@ -1128,6 +1201,24 @@ HWTEST_F(PrivacyManagerServiceTest, SetHapWithFGReminderInner001, TestSize.Level int32_t ret = privacyManagerService_->SetHapWithFGReminder(tokenID, isAllowed); EXPECT_EQ(PrivacyError::ERR_PERMISSION_DENIED, ret); } + +/** + * @tc.name: GetProxyDeathHandle001 + * @tc.desc: GetProxyDeathHandle test. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PrivacyManagerServiceTest, GetProxyDeathHandle001, TestSize.Level1) +{ + auto handler1 = privacyManagerService_->GetProxyDeathHandler(); + ASSERT_NE(nullptr, handler1); + auto handler2 = privacyManagerService_->GetProxyDeathHandler(); + ASSERT_NE(nullptr, handler2); + + privacyManagerService_->OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, "123"); + privacyManagerService_->OnAddSystemAbility(SCREENLOCK_SERVICE_ID, "123"); + privacyManagerService_->OnAddSystemAbility(INVALID_CODE, "123"); +} } // namespace AccessToken } // namespace Security } // namespace OHOS