From 8581f1972b967c520eff75f778e7fbf915f9ba0e Mon Sep 17 00:00:00 2001 From: wangyang2022 Date: Mon, 19 Sep 2022 20:12:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0DMS=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=20Signed-off-by:=20wangyan?= =?UTF-8?q?g2022=20=20Change-Id:=20Ia0ad2f46c84a5e?= =?UTF-8?q?b48996dde28315c34c111994a1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/dtbschedmgr/test/BUILD.gn | 1 + .../distributed_sched_permission_test.cpp | 208 ++++++++++++++++++ .../test/unittest/distributed_sched_util.cpp | 19 +- .../test/unittest/distributed_sched_util.h | 5 + .../test/unittest/dms_token_callback_test.cpp | 170 ++++++++++++++ .../test/unittest/dms_token_callback_test.h | 38 ++++ 6 files changed, 438 insertions(+), 3 deletions(-) create mode 100644 services/dtbschedmgr/test/unittest/dms_token_callback_test.cpp create mode 100644 services/dtbschedmgr/test/unittest/dms_token_callback_test.h diff --git a/services/dtbschedmgr/test/BUILD.gn b/services/dtbschedmgr/test/BUILD.gn index d9158091..bb8a2359 100644 --- a/services/dtbschedmgr/test/BUILD.gn +++ b/services/dtbschedmgr/test/BUILD.gn @@ -130,6 +130,7 @@ ohos_unittest("distributedschedsvrtest") { sources = [ "unittest/distributed_sched_service_test.cpp", + "unittest/dms_token_callback_test.cpp", "unittest/mock_distributed_sched.cpp", ] sources += dtbschedmgr_sources diff --git a/services/dtbschedmgr/test/unittest/distributed_sched_permission_test.cpp b/services/dtbschedmgr/test/unittest/distributed_sched_permission_test.cpp index 26f3899b..597a3242 100644 --- a/services/dtbschedmgr/test/unittest/distributed_sched_permission_test.cpp +++ b/services/dtbschedmgr/test/unittest/distributed_sched_permission_test.cpp @@ -19,6 +19,7 @@ #define private public #include "distributed_sched_permission.h" #undef private +#include "distributed_sched_util.h" #include "dtbschedmgr_log.h" #include "test_log.h" @@ -40,6 +41,18 @@ const string DEVICE_ID = "255.255.255.255"; const string INVALID_DEVICE_ID = ""; const string PERMISSION_NAME = "ohos.permission.DISTRIBUTED_DATASYNC"; const string INVALID_PERMISSION_NAME = "ohos.permission.TEST"; + +const string MOCK_FIELD_GROUP_NAME = "MockName"; +const string MOCK_FIELD_GROUP_ID = "MockId"; +const string MOCK_FIELD_GROUP_OWNER = "MockOwner"; +const int32_t MOCK_FIELD_GROUP_TYPE = 0; +const int32_t MOCK_FIELD_GROUP_VISIBILITY = 0; +const string FIELD_GROUP_NAME = "groupName"; +const string FIELD_GROUP_ID = "groupId"; +const string FIELD_GROUP_OWNER = "groupOwner"; +const string FIELD_GROUP_TYPE = "groupType"; +const string FIELD_GROUP_VISIBILITY = "groupVisibility"; +const char* FOUNDATION_PROCESS_NAME = "foundation"; } void DistributedSchedPermissionTest::SetUpTestCase() @@ -264,6 +277,159 @@ HWTEST_F(DistributedSchedPermissionTest, CheckDPermission_010, TestSize.Level1) DTEST_LOG << "DistributedSchedPermissionTest CheckDPermission_010 end result:" << ret << std::endl; } +/** + * @tc.name: GetAccountInfo_001 + * @tc.desc: call GetAccountInfo with empty networkId + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DistributedSchedPermissionTest, GetAccountInfo_001, TestSize.Level3) +{ + DTEST_LOG << "DistributedSchedPermissionTest GetAccountInfo_001 begin" << std::endl; + std::string remoteNetworkId; + CallerInfo callerInfo; + IDistributedSched::AccountInfo accountInfo; + int32_t ret = DistributedSchedPermission::GetInstance().GetAccountInfo( + remoteNetworkId, callerInfo, accountInfo); + EXPECT_EQ(ret, ERR_NULL_OBJECT); + DTEST_LOG << "DistributedSchedPermissionTest GetAccountInfo_001 end result:" << ret << std::endl; +} + +/** + * @tc.name: GetAccountInfo_002 + * @tc.desc: call GetAccountInfo with invalid networkId + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DistributedSchedPermissionTest, GetAccountInfo_002, TestSize.Level3) +{ + DTEST_LOG << "DistributedSchedPermissionTest GetAccountInfo_002 begin" << std::endl; + std::string remoteNetworkId = "0"; + CallerInfo callerInfo; + IDistributedSched::AccountInfo accountInfo; + int32_t ret = DistributedSchedPermission::GetInstance().GetAccountInfo( + remoteNetworkId, callerInfo, accountInfo); + EXPECT_EQ(ret, ERR_NULL_OBJECT); + DTEST_LOG << "DistributedSchedPermissionTest GetAccountInfo_002 end result:" << ret << std::endl; +} + +/** + * @tc.name: GetRelatedGroups_001 + * @tc.desc: call GetRelatedGroups with empty bundleNames + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DistributedSchedPermissionTest, GetRelatedGroups_001, TestSize.Level3) +{ + DTEST_LOG << "DistributedSchedPermissionTest GetRelatedGroups_001 begin" << std::endl; + std::string udid; + std::vector bundleNames; + IDistributedSched::AccountInfo accountInfo; + bool ret = DistributedSchedPermission::GetInstance().GetRelatedGroups( + udid, bundleNames, accountInfo); + EXPECT_EQ(ret, false); + DTEST_LOG << "DistributedSchedPermissionTest GetRelatedGroups_001 end result:" << ret << std::endl; +} + +/** + * @tc.name: GetRelatedGroups_002 + * @tc.desc: call GetRelatedGroups with invalid bundleNames + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DistributedSchedPermissionTest, GetRelatedGroups_002, TestSize.Level3) +{ + DTEST_LOG << "DistributedSchedPermissionTest GetRelatedGroups_002 begin" << std::endl; + std::string udid = "0"; + std::vector bundleNames = {"mock.bundle1", "mock.bundle2"}; + IDistributedSched::AccountInfo accountInfo; + bool ret = DistributedSchedPermission::GetInstance().GetRelatedGroups( + udid, bundleNames, accountInfo); + EXPECT_EQ(ret, false); + DTEST_LOG << "DistributedSchedPermissionTest GetRelatedGroups_002 end result:" << ret << std::endl; +} + +/** + * @tc.name: ParseGroupInfos_001 + * @tc.desc: call ParseGroupInfos with empty returnGroupStr + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DistributedSchedPermissionTest, ParseGroupInfos_001, TestSize.Level3) +{ + DTEST_LOG << "DistributedSchedPermissionTest ParseGroupInfos_001 begin" << std::endl; + std::string returnGroupStr; + std::vector groupInfos; + bool ret = DistributedSchedPermission::GetInstance().ParseGroupInfos( + returnGroupStr, groupInfos); + EXPECT_EQ(ret, false); + DTEST_LOG << "DistributedSchedPermissionTest GetRelatedGroups_001 end result:" << ret << std::endl; +} + +/** + * @tc.name: ParseGroupInfos_002 + * @tc.desc: call ParseGroupInfos with invalid returnGroupStr + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DistributedSchedPermissionTest, ParseGroupInfos_002, TestSize.Level3) +{ + DTEST_LOG << "DistributedSchedPermissionTest ParseGroupInfos_002 begin" << std::endl; + std::string returnGroupStr = "mockInvalidGroup"; + std::vector groupInfos; + bool ret = DistributedSchedPermission::GetInstance().ParseGroupInfos( + returnGroupStr, groupInfos); + EXPECT_EQ(ret, false); + DTEST_LOG << "DistributedSchedPermissionTest GetRelatedGroups_002 end result:" << ret << std::endl; +} + +/** + * @tc.name: ParseGroupInfos_003 + * @tc.desc: call ParseGroupInfos + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DistributedSchedPermissionTest, ParseGroupInfos_003, TestSize.Level3) +{ + DTEST_LOG << "DistributedSchedPermissionTest ParseGroupInfos_003 begin" << std::endl; + std::string returnGroupStr = "[{\"groupName\":\"mockGroupName\",\"groupId\":\"mockGroupId\","; + returnGroupStr += "\"groupOwner\":\"mockGroupOwner\",\"groupType\":0,\"groupVisibility\":0}]"; + std::vector groupInfos; + bool ret = DistributedSchedPermission::GetInstance().ParseGroupInfos( + returnGroupStr, groupInfos); + EXPECT_EQ(ret, true); + DTEST_LOG << "DistributedSchedPermissionTest GetRelatedGroups_003 end result:" << ret << std::endl; +} + +/** + * @tc.name: IsFoundationCall_001 + * @tc.desc: call IsFoundationCall not from foundation + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DistributedSchedPermissionTest, IsFoundationCall_001, TestSize.Level3) +{ + DTEST_LOG << "DistributedSchedPermissionTest IsFoundationCall_001 begin" << std::endl; + bool ret = DistributedSchedPermission::GetInstance().IsFoundationCall(); + EXPECT_EQ(ret, false); + DTEST_LOG << "DistributedSchedPermissionTest IsFoundationCall_001 end result:" << ret << std::endl; +} + +/** + * @tc.name: IsFoundationCall_002 + * @tc.desc: call IsFoundationCall from foundation + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DistributedSchedPermissionTest, IsFoundationCall_002, TestSize.Level3) +{ + DTEST_LOG << "DistributedSchedPermissionTest IsFoundationCall_002 begin" << std::endl; + DistributedSchedUtil::MockProcess(FOUNDATION_PROCESS_NAME); + bool ret = DistributedSchedPermission::GetInstance().IsFoundationCall(); + EXPECT_EQ(ret, true); + DTEST_LOG << "DistributedSchedPermissionTest IsFoundationCall_002 end result:" << ret << std::endl; +} + /** * @tc.name: CheckAccountAccessPermission_001 * @tc.desc: call CheckAccountAccessPermission in same account @@ -916,5 +1082,47 @@ HWTEST_F(DistributedSchedPermissionTest, CheckPermission_003, TestSize.Level1) EXPECT_EQ(ret, DMS_PERMISSION_DENIED); DTEST_LOG << "DistributedSchedPermissionTest CheckPermission_003 end result:" << ret << std::endl; } + +/** + * @tc.name: from_json_001 + * @tc.desc: parse groupInfo from json + * @tc.type: FUNC + */ +HWTEST_F(DistributedSchedPermissionTest, FromJson_001, TestSize.Level3) +{ + DTEST_LOG << "DistributedSchedPermissionTest FromJson_001 begin" << std::endl; + GroupInfo groupInfo; + nlohmann::json jsonObject = nlohmann::json{ + {FIELD_GROUP_NAME, MOCK_FIELD_GROUP_NAME}, + {FIELD_GROUP_ID, MOCK_FIELD_GROUP_ID}, + {FIELD_GROUP_OWNER, MOCK_FIELD_GROUP_OWNER}, + {FIELD_GROUP_TYPE, MOCK_FIELD_GROUP_TYPE}, + {FIELD_GROUP_VISIBILITY, MOCK_FIELD_GROUP_VISIBILITY} + }; + if (jsonObject.is_discarded()) { + return; + } + from_json(jsonObject, groupInfo); + EXPECT_EQ(groupInfo.groupName, MOCK_FIELD_GROUP_NAME); + DTEST_LOG << "DistributedSchedPermissionTest FromJson_001 end" << std::endl; +} + +/** + * @tc.name: from_json_002 + * @tc.desc: parse groupInfo from json with invalid params + * @tc.type: FUNC + */ +HWTEST_F(DistributedSchedPermissionTest, FromJson_002, TestSize.Level3) +{ + DTEST_LOG << "DistributedSchedPermissionTest FromJson_002 begin" << std::endl; + GroupInfo groupInfo; + nlohmann::json jsonObject; + if (jsonObject.is_discarded()) { + return; + } + from_json(jsonObject, groupInfo); + EXPECT_EQ(groupInfo.groupName, ""); + DTEST_LOG << "DistributedSchedPermissionTest FromJson_002 end " << std::endl; +} } // namespace DistributedSchedule } // namespace OHOS \ No newline at end of file diff --git a/services/dtbschedmgr/test/unittest/distributed_sched_util.cpp b/services/dtbschedmgr/test/unittest/distributed_sched_util.cpp index 82eda0eb..6e7ecb9c 100644 --- a/services/dtbschedmgr/test/unittest/distributed_sched_util.cpp +++ b/services/dtbschedmgr/test/unittest/distributed_sched_util.cpp @@ -20,20 +20,33 @@ namespace OHOS { namespace DistributedSchedule { +namespace { +const char* DISTSCHED_PROCESS_NAME = "distributedsched"; +} void DistributedSchedUtil::MockPermission() { static const char *PERMS[] = { "ohos.permission.DISTRIBUTED_DATASYNC" }; + MockProcessAndPermission(DISTSCHED_PROCESS_NAME, PERMS, 1); +} + +void DistributedSchedUtil::MockProcess(const char* processName) +{ + MockProcessAndPermission(processName); +} + +void DistributedSchedUtil::MockProcessAndPermission(const char* processName, const char *perms[], int32_t permsNum) +{ uint64_t tokenId; NativeTokenInfoParams infoInstance = { .dcapsNum = 0, - .permsNum = 1, + .permsNum = permsNum, .aclsNum = 0, .dcaps = nullptr, - .perms = PERMS, + .perms = perms, .acls = nullptr, - .processName = "distributedsched", + .processName = processName, .aplStr = "system_core", }; tokenId = GetAccessTokenId(&infoInstance); diff --git a/services/dtbschedmgr/test/unittest/distributed_sched_util.h b/services/dtbschedmgr/test/unittest/distributed_sched_util.h index d89f5f5c..84eb1b22 100644 --- a/services/dtbschedmgr/test/unittest/distributed_sched_util.h +++ b/services/dtbschedmgr/test/unittest/distributed_sched_util.h @@ -16,11 +16,16 @@ #ifndef DISTRIBUTED_SCHED_UTIL_H #define DISTRIBUTED_SCHED_UTIL_H +#include + namespace OHOS { namespace DistributedSchedule { class DistributedSchedUtil { public: static void MockPermission(); + static void MockProcess(const char* processName); + static void MockProcessAndPermission(const char* processName, + const char *perms[] = nullptr, int32_t permsNum = 0); }; } // namespace DistributedSchedule } // namespace OHOS diff --git a/services/dtbschedmgr/test/unittest/dms_token_callback_test.cpp b/services/dtbschedmgr/test/unittest/dms_token_callback_test.cpp new file mode 100644 index 00000000..6f02a5d6 --- /dev/null +++ b/services/dtbschedmgr/test/unittest/dms_token_callback_test.cpp @@ -0,0 +1,170 @@ +/* + * 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 "dms_token_callback_test.h" + +#include "distributed_sched_util.h" +#include "dms_token_callback.h" +#include "dtbschedmgr_log.h" +#include "test_log.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace DistributedSchedule { +namespace { +const char* FOUNDATION_PROCESS_NAME = "foundation"; +} + +void DmsTokenCallbackTest::SetUpTestCase() +{ + DTEST_LOG << "DmsTokenCallbackTest::SetUpTestCase" << std::endl; +} + +void DmsTokenCallbackTest::TearDownTestCase() +{ + DTEST_LOG << "DmsTokenCallbackTest::TearDownTestCase" << std::endl; +} + +void DmsTokenCallbackTest::TearDown() +{ + DTEST_LOG << "DmsTokenCallbackTest::TearDown" << std::endl; +} + +void DmsTokenCallbackTest::SetUp() +{ + DTEST_LOG << "DmsTokenCallbackTest::SetUp" << std::endl; + dmsTokenCallback_ = new DmsTokenCallback(); +} + +/** + * @tc.name: SendResultTest_001 + * @tc.desc: call SendResult out from foundation + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DmsTokenCallbackTest, SendResultTest_001, TestSize.Level3) +{ + DTEST_LOG << "DmsTokenCallbackTest SendResultTest_001 begin" << std::endl; + AAFwk::Want want; + int32_t callerUid = 0; + int32_t requestCode = 0; + uint32_t accessToken = 0; + int32_t resultCode = 0; + int32_t result = dmsTokenCallback_->SendResult(want, callerUid, requestCode, accessToken, resultCode); + EXPECT_EQ(result, INVALID_PARAMETERS_ERR); + DTEST_LOG << "DmsTokenCallbackTest SendResultTest_001 end" << std::endl; +} + +/** + * @tc.name: SendResultTest_002 + * @tc.desc: call SendResult from foundation + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DmsTokenCallbackTest, SendResultTest_002, TestSize.Level3) +{ + DTEST_LOG << "DmsTokenCallbackTest SendResultTest_002 begin" << std::endl; + AAFwk::Want want; + int32_t callerUid = 0; + int32_t requestCode = 0; + uint32_t accessToken = 0; + int32_t resultCode = 0; + DistributedSchedUtil::MockProcess(FOUNDATION_PROCESS_NAME); + int32_t result = dmsTokenCallback_->SendResult(want, callerUid, requestCode, accessToken, resultCode); + EXPECT_EQ(result, INVALID_PARAMETERS_ERR); + DTEST_LOG << "DmsTokenCallbackTest SendResultTest_002 end" << std::endl; +} + +/** + * @tc.name: CheckDeviceIdTest_001 + * @tc.desc: call CheckDeviceId with empty deviceId + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DmsTokenCallbackTest, CheckDeviceIdTest_001, TestSize.Level3) +{ + DTEST_LOG << "DmsTokenCallbackTest CheckDeviceIdTest_001 begin" << std::endl; + std::string localDeviceId; + std::string remoteDeviceId; + bool result = dmsTokenCallback_->CheckDeviceId(localDeviceId, remoteDeviceId); + EXPECT_EQ(result, false); + DTEST_LOG << "DmsTokenCallbackTest CheckDeviceIdTest_001 end" << std::endl; +} + +/** + * @tc.name: CheckDeviceIdTest_002 + * @tc.desc: call CheckDeviceId with same deviceId + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DmsTokenCallbackTest, CheckDeviceIdTest_002, TestSize.Level3) +{ + DTEST_LOG << "DmsTokenCallbackTest CheckDeviceIdTest_002 begin" << std::endl; + std::string localDeviceId = "1"; + std::string remoteDeviceId = "1"; + bool result = dmsTokenCallback_->CheckDeviceId(localDeviceId, remoteDeviceId); + EXPECT_EQ(result, false); + DTEST_LOG << "DmsTokenCallbackTest CheckDeviceIdTest_002 end" << std::endl; +} + +/** + * @tc.name: CheckDeviceIdTest_003 + * @tc.desc: call CheckDeviceId with different deviceId + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DmsTokenCallbackTest, CheckDeviceIdTest_003, TestSize.Level3) +{ + DTEST_LOG << "DmsTokenCallbackTest CheckDeviceIdTest_003 begin" << std::endl; + std::string localDeviceId = "1"; + std::string remoteDeviceId = "2"; + bool result = dmsTokenCallback_->CheckDeviceId(localDeviceId, remoteDeviceId); + EXPECT_EQ(result, true); + DTEST_LOG << "DmsTokenCallbackTest CheckDeviceIdTest_003 end" << std::endl; +} + +/** + * @tc.name: GetRemoteDmsTest_001 + * @tc.desc: call GetRemoteDms with empty deviceId + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DmsTokenCallbackTest, GetRemoteDmsTest_001, TestSize.Level3) +{ + DTEST_LOG << "DmsTokenCallbackTest GetRemoteDmsTest_001 begin" << std::endl; + std::string remoteDeviceId; + sptr result = dmsTokenCallback_->GetRemoteDms(remoteDeviceId); + EXPECT_EQ(result, nullptr); + DTEST_LOG << "DmsTokenCallbackTest GetRemoteDmsTest_001 end" << std::endl; +} + +/** + * @tc.name: GetRemoteDmsTest_002 + * @tc.desc: call GetRemoteDms with invalid deviceId + * @tc.type: FUNC + * @tc.require: I5RWIV + */ +HWTEST_F(DmsTokenCallbackTest, GetRemoteDmsTest_002, TestSize.Level3) +{ + DTEST_LOG << "DmsTokenCallbackTest GetRemoteDmsTest_002 begin" << std::endl; + std::string remoteDeviceId = "1"; + sptr result = dmsTokenCallback_->GetRemoteDms(remoteDeviceId); + EXPECT_EQ(result, nullptr); + DTEST_LOG << "DmsTokenCallbackTest GetRemoteDmsTest_002 end" << std::endl; +} +} +} \ No newline at end of file diff --git a/services/dtbschedmgr/test/unittest/dms_token_callback_test.h b/services/dtbschedmgr/test/unittest/dms_token_callback_test.h new file mode 100644 index 00000000..995ab3fe --- /dev/null +++ b/services/dtbschedmgr/test/unittest/dms_token_callback_test.h @@ -0,0 +1,38 @@ +/* + * 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. + */ + +#ifndef DMS_TOKEN_CB_TEST_H +#define DMS_TOKEN_CB_TEST_H + +#include "gtest/gtest.h" + +#define private public +#include "dms_token_callback.h" +#undef private + +namespace OHOS { +namespace DistributedSchedule { +class DmsTokenCallbackTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + sptr dmsTokenCallback_; +}; +} // namespace DistributedSchedule +} // namespace OHOS +#endif // DMS_TOKEN_CB_TEST_H \ No newline at end of file