!390 添加DMS单元测试用例

Merge pull request !390 from 王洋/master_ut
This commit is contained in:
openharmony_ci 2022-09-20 06:20:39 +00:00 committed by Gitee
commit 1c45333670
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 438 additions and 3 deletions

View File

@ -129,6 +129,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

View File

@ -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<std::string> 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<std::string> 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<GroupInfo> 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<GroupInfo> 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<GroupInfo> 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

View File

@ -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);

View File

@ -16,11 +16,16 @@
#ifndef DISTRIBUTED_SCHED_UTIL_H
#define DISTRIBUTED_SCHED_UTIL_H
#include <stdint.h>
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

View File

@ -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<IDistributedSched> 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<IDistributedSched> result = dmsTokenCallback_->GetRemoteDms(remoteDeviceId);
EXPECT_EQ(result, nullptr);
DTEST_LOG << "DmsTokenCallbackTest GetRemoteDmsTest_002 end" << std::endl;
}
}
}

View File

@ -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> dmsTokenCallback_;
};
} // namespace DistributedSchedule
} // namespace OHOS
#endif // DMS_TOKEN_CB_TEST_H