fix appImageInfo bug

Signed-off-by: yuqianyuan <yuqianyuan@huawei.com>
This commit is contained in:
yuqianyuan
2021-09-16 15:02:18 +08:00
parent 2c6e90a0bf
commit 6f795abb0f
12 changed files with 503 additions and 103 deletions
+4
View File
@@ -158,6 +158,10 @@ namespace DistributedHardware {
const int32_t BUSINESS_FA_MIRGRATION = 0;
const int32_t BUSINESS_RESOURCE_ACCESS = 1;
// Base64 Constants
const int32_t BASE64_BYTE_LEN_3 = 3;
const int32_t BASE64_BYTE_LEN_4 = 4;
}
}
#endif
@@ -35,6 +35,46 @@ public:
SaveData(appIcon_, appIconLen_, appThumbnail_, appThumbnailLen_);
}
void ResetIcon(uint8_t *appIcon_, int32_t appIconLen_)
{
SaveIconData(appIcon_, appIconLen_);
}
void InitThumbnail(int32_t appThumbnailLen_)
{
if (appThumbnailLen_ <= 0 || appThumbnailLen_ > THUMB_MAX_LEN) {
appThumbnailLen = 0;
appThumbnail = nullptr;
return;
}
appThumbnail = new (std::nothrow) uint8_t[appThumbnailLen_] {0};
if (appThumbnail != nullptr) {
appThumbnailLen = appThumbnailLen_;
}
}
int32_t SetThumbnailData(uint8_t *srcBuffer, int32_t srcBufferLen, int32_t copyIndex, int32_t copyLen)
{
if (srcBuffer == nullptr || srcBufferLen <= 0 || copyLen > srcBufferLen || copyIndex < 0) {
return -1;
}
if ((copyIndex + copyLen) > appThumbnailLen) {
return -1;
}
if (appThumbnail == nullptr) {
return -1;
}
if (memcpy_s(appThumbnail + copyIndex, appThumbnailLen - copyLen, srcBuffer, copyLen) != 0) {
return -1;
}
return 0;
}
~DmAppImageInfo()
{
if (appIcon != nullptr) {
@@ -86,6 +126,12 @@ public:
}
private:
void SaveData(const uint8_t *appIcon_, int32_t appIconLen_, const uint8_t *appThumbnail_, int32_t appThumbnailLen_)
{
SaveIconData(appIcon_, appIconLen_);
SaveThumbnailData(appThumbnail_, appThumbnailLen_);
}
void SaveIconData(const uint8_t *appIcon_, int32_t appIconLen_)
{
if (appIconLen_ > 0 && appIconLen_ < ICON_MAX_LEN && appIcon_ != nullptr) {
if (appIconLen < appIconLen_) {
@@ -101,6 +147,10 @@ private:
(void)memcpy_s(appIcon, appIconLen, appIcon_, appIconLen_);
}
}
}
void SaveThumbnailData(const uint8_t *appThumbnail_, int32_t appThumbnailLen_)
{
if (appThumbnailLen_ > 0 && appThumbnailLen_ < THUMB_MAX_LEN && appThumbnail_ != nullptr) {
if (appThumbnailLen < appThumbnailLen_) {
if (appThumbnail != nullptr && appThumbnailLen > 0) {
+3
View File
@@ -25,6 +25,9 @@
"//foundation/distributedhardware/devicemanager/services/devicemanagerservice:devicemanagerservice",
"//foundation/distributedhardware/devicemanager/sa_profile:dm_sa_profile"
],
"test_list": [
"//foundation/distributedhardware/devicemanager/test:test"
],
"system_kits": []
}
},
@@ -41,7 +41,6 @@ public:
std::vector<std::string> Encode();
static std::shared_ptr<MsgRequestAuth> Decode(nlohmann::json &json, std::shared_ptr<MsgRequestAuth> msgIn);
static void SetThumbnailSize(nlohmann::json &json, std::shared_ptr<MsgRequestAuth> msg);
void GetDecodeAppInfo(const std::string appString, uint8_t **outBuffer, int32_t &outBufferLen);
int32_t GetMsgSlice();
int32_t GetMsgCnt();
std::string GetRequestDeviceId();
@@ -55,21 +54,19 @@ public:
std::string mDeviceType_;
std::string mAppName_;
std::string mAppDescription_;
std::string mAppIcon_;
std::string mAppThumbnail_;
int32_t mAuthType_ {AUTH_TYPE_PIN};
int32_t mGroupVisibility_ {GROUP_VISIBILITY_IS_PRIVATE};
int32_t mMsgSlice_ {0};
int32_t mMsgCnt_ {0};
int32_t mThumbnailSize_ {0};
int32_t mAppIconSize_ {0};
DmAppImageInfo mImageInfo_;
private:
std::string ToHexString(int32_t value);
std::string EncodeDevInfo();
static int32_t DecodeFirstPackageMsg(nlohmann::json &json, std::shared_ptr<MsgRequestAuth> msg);
static void DecodeDeviceInfo(nlohmann::json &json, std::shared_ptr<MsgRequestAuth> msg);
static std::string StringSub(std::string &thumbStr, int32_t start, int32_t length);
int32_t GetEncodedAppInfo(const uint8_t *dataSrc, size_t srcLen, std::string &outString);
int32_t GetEncodedAppInfo(const uint8_t *dataSrc, int32_t srcLen, std::string &outString);
void GetDecodeAppInfo(const std::string appString, uint8_t **outBuffer, int32_t &outBufferLen);
static bool IsMsgValid(std::shared_ptr<MsgRequestAuth> msgIn, nlohmann::json &json, std::string &deviceId,
int32_t index);
static bool IsAppInfoValid(nlohmann::json &json);
@@ -265,8 +265,8 @@ ON_IPC_CMD(AUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply)
uint8_t *appIcon = appIconLen > 0? (uint8_t *)data.ReadRawData(appIconLen) : nullptr;
uint8_t *appThumbnail = appThumbnailLen > 0? (uint8_t *)data.ReadRawData(appThumbnailLen) : nullptr;
DmAppImageInfo imageInfo(appIcon, appIconLen, appThumbnail, appThumbnailLen);
int32_t result = IpcServerAdapter::GetInstance().AuthenticateDevice(pkgName, *deviceInfo, imageInfo, extra);
DmAppImageInfo imageInfo(appIcon, appIconLen, appThumbnail, appThumbnailLen);
int32_t result = IpcServerAdapter::GetInstance().AuthenticateDevice(pkgName, *deviceInfo, imageInfo, extra);
if (!reply.WriteInt32(result)) {
DMLOG(DM_LOG_ERROR, "write result failed");
@@ -57,35 +57,32 @@ MsgRequestAuth::MsgRequestAuth(std::string &token, std::string hostPkgName, std:
mGroupVisibility_ = groupVisibility;
mAppName_ = jsonObject[APP_NAME_KEY];
mAppDescription_ = jsonObject[APP_DESCRIPTION_KEY];
std::string appIconStr = "";
GetEncodedAppInfo(imageInfo.GetAppIcon(), imageInfo.GetAppIconLen(), appIconStr);
std::string appThumbStr = "";
GetEncodedAppInfo(imageInfo.GetAppThumbnail(), imageInfo.GetAppThumbnailLen(), appThumbStr);
mAppIcon_ = appIconStr;
mAppThumbnail_ = appThumbStr;
mImageInfo_ = imageInfo;
mThumbnailSize_ = mImageInfo_.GetAppThumbnailLen();
mAppIconSize_ = mImageInfo_.GetAppIconLen();
mDeviceType_ = ToHexString(devReqInfo.deviceTypeId);
DMLOG(DM_LOG_INFO, "MsgRequestAuth construction completed");
}
int32_t MsgRequestAuth::GetEncodedAppInfo(const uint8_t *dataSrc, size_t srcLen, std::string &outString)
int32_t MsgRequestAuth::GetEncodedAppInfo(const uint8_t *dataSrc, int32_t srcLen, std::string &outString)
{
DMLOG(DM_LOG_INFO, "MsgRequestAuth GetEncodedAppInfo started");
if (srcLen == 0 || dataSrc == nullptr) {
if (srcLen <= 0 || dataSrc == nullptr) {
DMLOG(DM_LOG_ERROR, "data string is empty");
return DEVICEMANAGER_OK;
}
size_t outLen = 0;
char *tmpBuf = (char *)calloc(sizeof(char), THUMB_MAX_LEN);
int32_t tempBufLen = ((srcLen / BASE64_BYTE_LEN_3) + 1) * BASE64_BYTE_LEN_4 + 1;
char *tmpBuf = (char *)calloc(sizeof(char), tempBufLen);
if (tmpBuf == nullptr) {
DMLOG(DM_LOG_ERROR, "getEncodedAppInfoString: malloc mem error");
DMLOG(DM_LOG_ERROR, "getEncodedAppInfoString: malloc mem error, size %d", tempBufLen);
return DEVICEMANAGER_MALLOC_ERROR;
}
EncryptUtils::MbedtlsBase64Encode((uint8_t *)tmpBuf, THUMB_MAX_LEN, &outLen, dataSrc, srcLen);
if (outLen > THUMB_MAX_LEN) {
DMLOG(DM_LOG_ERROR, "encode appIcon error");
size_t outLen = 0;
int32_t ret = EncryptUtils::MbedtlsBase64Encode((uint8_t *)tmpBuf, tempBufLen, &outLen, dataSrc, (size_t)srcLen);
if (ret != 0) {
DMLOG(DM_LOG_ERROR, "MbedtlsBase64Encode error");
free(tmpBuf);
return ENCODE_DATA_ERROR;
}
@@ -99,22 +96,25 @@ int32_t MsgRequestAuth::GetEncodedAppInfo(const uint8_t *dataSrc, size_t srcLen,
void MsgRequestAuth::GetDecodeAppInfo(const std::string appString, uint8_t **outBuffer, int32_t &outBufferLen)
{
DMLOG(DM_LOG_INFO, "MsgRequestAuth GetDecodeAppInfo started");
size_t outLen = 0;
uint8_t *buffer = (uint8_t *)calloc(sizeof(char), THUMB_MAX_LEN);
int32_t tempBufLen = appString.length() + 1;
uint8_t *buffer = (uint8_t *)calloc(sizeof(char), tempBufLen);
if (buffer == nullptr) {
DMLOG(DM_LOG_ERROR, "GetDecodeAppInfo: malloc mem error");
DMLOG(DM_LOG_ERROR, "GetDecodeAppInfo: malloc mem error, tempBufLen %d", tempBufLen);
return;
}
int32_t ret = EncryptUtils::MbedtlsBase64Decode(buffer, THUMB_MAX_LEN, &outLen,
(const uint8_t*)appString.c_str(), strlen(appString.c_str()));
if (ret != 0) {
DMLOG(DM_LOG_ERROR, "BuildAuthenticationInfo: MbedtlsBase64Decode failed");
size_t outLen = 0;
int32_t ret = EncryptUtils::MbedtlsBase64Decode(buffer, tempBufLen, &outLen,
(const uint8_t*)appString.c_str(), appString.length());
if (ret != 0 || outLen > tempBufLen) {
DMLOG(DM_LOG_ERROR, "MbedtlsBase64Decode failed, ret %d, outLen %d, tempBufLen %d",
ret, outLen, tempBufLen);
outBufferLen = 0;
*outBuffer = nullptr;
free(buffer);
return;
}
DMLOG(DM_LOG_INFO, "MsgRequestAuth GetDecodeAppInfo outBufferLen %d", outBufferLen);
outBufferLen = outLen;
*outBuffer = buffer;
@@ -138,8 +138,11 @@ std::string MsgRequestAuth::EncodeDevInfo()
}
jsonObj[TAG_APP_NAME] = mAppName_;
jsonObj[TAG_APP_DESCRIPTION] = mAppDescription_;
jsonObj[TAG_APP_ICON] = mAppIcon_;
jsonObj[TAG_THUMBNAIL_SIZE] = mAppThumbnail_.size();
std::string appIconStr = "";
GetEncodedAppInfo(mImageInfo_.GetAppIcon(), mImageInfo_.GetAppIconLen(), appIconStr);
jsonObj[TAG_APP_ICON] = appIconStr;
jsonObj[TAG_THUMBNAIL_SIZE] = mThumbnailSize_;
jsonObj[TAG_AUTH_TYPE] = mAuthType_;
DMLOG(DM_LOG_INFO, "MsgRequestAuth EncodeDevInfo completed");
return jsonObj.dump();
@@ -158,7 +161,16 @@ void MsgRequestAuth::DecodeDeviceInfo(nlohmann::json &json, std::shared_ptr<MsgR
}
msg->mAppName_ = json[TAG_APP_NAME];
msg->mAppDescription_ = json[TAG_APP_DESCRIPTION];
msg->mAppIcon_ = json[TAG_APP_ICON];
const std::string iconStr = json[TAG_APP_ICON];
uint8_t *appIcon = nullptr;
int32_t appIconLen = 0;
msg->GetDecodeAppInfo(iconStr, &appIcon, appIconLen);
if (appIcon != nullptr) {
msg->mImageInfo_.ResetIcon(appIcon, appIconLen);
free(appIcon);
}
SetThumbnailSize(json, msg);
msg->mAuthType_ = json[TAG_AUTH_TYPE];
}
@@ -168,7 +180,7 @@ std::vector<std::string> MsgRequestAuth::Encode()
DMLOG(DM_LOG_INFO, "MsgRequestAuth encode started");
std::vector<std::string> jsonStrs;
int32_t thumbnailSlice =
(mAppThumbnail_.size() / MSG_MAX_SIZE) + ((mAppThumbnail_.size() % MSG_MAX_SIZE) == 0 ? 0 : 1);
((mThumbnailSize_ / MSG_MAX_SIZE) + (mThumbnailSize_ % MSG_MAX_SIZE) == 0 ? 0 : 1);
mMsgSlice_ = thumbnailSlice + 1;
jsonStrs.push_back(EncodeDevInfo());
for (int32_t idx = 0; idx < thumbnailSlice; idx++) {
@@ -177,8 +189,20 @@ std::vector<std::string> MsgRequestAuth::Encode()
jsonObj[TAG_SLICE_NUM] = mMsgSlice_;
jsonObj[TAG_INDEX] = idx + 1;
jsonObj[TAG_DEVICE_ID] = mDeviceId_;
jsonObj[TAG_THUMBNAIL_SIZE] = mAppThumbnail_.size();
jsonObj[TAG_APP_THUMBNAIL] = StringSub(mAppThumbnail_, idx * MSG_MAX_SIZE, MSG_MAX_SIZE);
jsonObj[TAG_THUMBNAIL_SIZE] = mThumbnailSize_;
// frag thumbnail by 45KB
std::string thumbnailStr = "";
int32_t leftLen = mImageInfo_.GetAppThumbnailLen() - idx * MSG_MAX_SIZE;
int32_t sliceLen = (leftLen > MSG_MAX_SIZE) ? MSG_MAX_SIZE : leftLen;
DMLOG(DM_LOG_INFO, "TAG_APP_THUMBNAIL encode, idx %d, encodeLen %d, mThumbnailSize_ %d",
idx, sliceLen, mThumbnailSize_);
const uint8_t *thumbnail = mImageInfo_.GetAppThumbnail();
GetEncodedAppInfo(thumbnail + idx * MSG_MAX_SIZE, sliceLen, thumbnailStr);
jsonObj[TAG_APP_THUMBNAIL] = thumbnailStr;
jsonStrs.push_back(jsonObj.dump());
}
DMLOG(DM_LOG_INFO, "MsgRequestAuth encode completed");
@@ -206,9 +230,7 @@ std::shared_ptr<MsgRequestAuth> MsgRequestAuth::Decode(nlohmann::json &json, std
msg->mHead_ = MsgHead::Decode(json);
msg->mMsgSlice_ = json[TAG_SLICE_NUM];
if (idx == 0) {
if (DecodeFirstPackageMsg(json, msg) != DEVICEMANAGER_OK) {
return nullptr;
}
DecodeDeviceInfo(json, msg);
} else {
SetThumbnailSize(json, msg);
msg->mDeviceId_ = deviceId;
@@ -216,47 +238,31 @@ std::shared_ptr<MsgRequestAuth> MsgRequestAuth::Decode(nlohmann::json &json, std
DMLOG(DM_LOG_ERROR, "err json string, TAG_APP_THUMBNAIL not exit");
return nullptr;
}
std::string src = json[TAG_APP_THUMBNAIL];
if (msg->mAppThumbnail_.size() < src.size() + (idx - 1) * MSG_MAX_SIZE) {
std::string thumbnailStr = json[TAG_APP_THUMBNAIL];
uint8_t *thumbnail = nullptr;
int32_t thumbnailLen = 0;
msg->GetDecodeAppInfo(thumbnailStr, &thumbnail, thumbnailLen);
if (thumbnail == nullptr) {
DMLOG(DM_LOG_ERROR, "TAG_APP_THUMBNAIL Decode error");
return nullptr;
}
DMLOG(DM_LOG_INFO, "TAG_APP_THUMBNAIL decode, idx %d, decodeLen %d, mThumbnailSize_ %d",
idx, thumbnailLen, msg->mThumbnailSize_);
if (msg->mThumbnailSize_ < thumbnailLen + (idx - 1) * MSG_MAX_SIZE) {
auto inValidReqMsg = std::make_shared<MsgRequestAuth>();
inValidReqMsg->mMsgSlice_ = FAIL;
free(thumbnail);
return inValidReqMsg;
}
msg->mAppThumbnail_ += StringSub(src, (idx - 1) * MSG_MAX_SIZE, MSG_MAX_SIZE);
msg->mImageInfo_.SetThumbnailData(thumbnail, thumbnailLen, (idx - 1) * MSG_MAX_SIZE, thumbnailLen);
free(thumbnail);
}
msg->mMsgCnt_++;
return msg;
}
int32_t MsgRequestAuth::DecodeFirstPackageMsg(nlohmann::json &json, std::shared_ptr<MsgRequestAuth> msg)
{
if (!json.contains(TAG_REQUESTER) || !json.contains(TAG_DEVICE_TYPE) || !json.contains(TAG_TOKEN) ||
!json.contains(TAG_VISIBILITY) || !json.contains(TAG_APP_NAME) || !json.contains(TAG_APP_DESCRIPTION) ||
!json.contains(TAG_APP_ICON)) {
DMLOG(DM_LOG_ERROR, "err json string, second time");
return DEVICEMANAGER_FAILED;
}
msg->mDeviceName_ = json[TAG_REQUESTER];
msg->mDeviceId_ = json[TAG_DEVICE_ID];
msg->mDeviceType_ = json[TAG_DEVICE_TYPE];
msg->mToken_ = json[TAG_TOKEN];
msg->mGroupVisibility_ = json[TAG_VISIBILITY];
if (msg->mGroupVisibility_ == GROUP_VISIBILITY_IS_PRIVATE) {
if (!json.contains(TAG_TARGET) || !json.contains(TAG_HOST)) {
DMLOG(DM_LOG_ERROR, "err json string, third time");
return DEVICEMANAGER_FAILED;
}
msg->mTargetPkg_ = json[TAG_TARGET];
msg->mHostPkg_ = json[TAG_HOST];
}
msg->mAppName_ = json[TAG_APP_NAME];
msg->mAppDescription_ = json[TAG_APP_DESCRIPTION];
msg->mAppIcon_ = json[TAG_APP_ICON];
SetThumbnailSize(json, msg);
SetAuthType(json, msg);
return DEVICEMANAGER_OK;
}
int32_t MsgRequestAuth::GetMsgSlice()
{
return mMsgSlice_;
@@ -275,7 +281,7 @@ std::string MsgRequestAuth::GetRequestDeviceId()
bool MsgRequestAuth::IsMsgValid(std::shared_ptr<MsgRequestAuth> msgIn, nlohmann::json &json,
std::string &deviceId, int32_t index)
{
if (msgIn != nullptr && msgIn->mMsgCnt_ != msgIn->mMsgSlice_ && !deviceId.compare(msgIn->mDeviceId_)) {
if (msgIn != nullptr && msgIn->mMsgCnt_ != msgIn->mMsgSlice_ && deviceId.compare(msgIn->mDeviceId_)) {
DMLOG(DM_LOG_ERROR, "IsMsgValid, msgIn error");
return false;
}
@@ -306,7 +312,7 @@ bool MsgRequestAuth::IsAppInfoValid(nlohmann::json &json)
{
if (!json.contains(TAG_REQUESTER) || !json.contains(TAG_DEVICE_TYPE) || !json.contains(TAG_TOKEN) ||
!json.contains(TAG_VISIBILITY) || !json.contains(TAG_APP_NAME) || !json.contains(TAG_APP_DESCRIPTION) ||
!json.contains(TAG_APP_ICON) || !json.contains(TAG_AUTH_TYPE)) {
!json.contains(TAG_APP_ICON) || !json.contains(TAG_AUTH_TYPE) || !json.contains(TAG_DEVICE_ID)) {
DMLOG(DM_LOG_ERROR, "IsAppInfoValid:: err json string");
return false;
}
@@ -314,13 +320,13 @@ bool MsgRequestAuth::IsAppInfoValid(nlohmann::json &json)
int32_t groupVisibility = json[TAG_VISIBILITY];
if (groupVisibility == GROUP_VISIBILITY_IS_PRIVATE) {
if (!json.contains(TAG_TARGET) || !json.contains(TAG_HOST)) {
DMLOG(DM_LOG_ERROR, "IsAppInfoValid:: err json string, TAG_TARGET or TAG_HOST not contain");
DMLOG(DM_LOG_ERROR, "IsAppInfoValid:: err json string, TAG_TARGET or TAG_HOST not contain");
return false;
}
}
if (json[TAG_APP_ICON].size() > ICON_MAX_LEN) {
DMLOG(DM_LOG_ERROR, "IsAppInfoValid, mAppIcon_ size error");
DMLOG(DM_LOG_ERROR, "IsAppInfoValid, appIcon size error");
return false;
}
@@ -349,20 +355,12 @@ void MsgRequestAuth::SetThumbnailSize(nlohmann::json &json, std::shared_ptr<MsgR
}
int32_t thumbnailSlice = json[TAG_THUMBNAIL_SIZE];
if (msg->mThumbnailSize_ == 0) {
msg->mThumbnailSize_ = thumbnailSlice;
DMLOG(DM_LOG_INFO, "mThumbnailSize_ is, %d", msg->mThumbnailSize_);
msg->mAppThumbnail_ = "";
msg->mImageInfo_.InitThumbnail(thumbnailSlice);
msg->mThumbnailSize_ = msg->mImageInfo_.GetAppThumbnailLen();
DMLOG(DM_LOG_INFO, "thumbnailSlice %d, mThumbnailSize_ is, %d", thumbnailSlice, msg->mThumbnailSize_);
}
}
std::string MsgRequestAuth::StringSub(std::string &thumbStr, int32_t start, int32_t length)
{
int32_t copyLen = start + length > (int32_t)thumbStr.size() ? (thumbStr.size() - start) : length;
std::string ret;
ret.assign(thumbStr, start, copyLen);
return ret;
}
std::string MsgRequestAuth::ToHexString(int32_t value)
{
std::stringstream ioss;
@@ -215,21 +215,7 @@ void ResponseSession::BuildAuthenticationInfo(DmAuthParam &authParam)
authParam.pincode = mPincode_;
if (mMsgRequestAuthPtr_ != nullptr) {
uint8_t *appIcon = nullptr;
int32_t appIconLen = 0;
uint8_t *appThumbnail = nullptr;
int32_t appThumbnailLen = 0;
mMsgRequestAuthPtr_->GetDecodeAppInfo(mMsgRequestAuthPtr_->mAppIcon_, &appIcon, appIconLen);
mMsgRequestAuthPtr_->GetDecodeAppInfo(mMsgRequestAuthPtr_->mAppThumbnail_, &appThumbnail, appThumbnailLen);
authParam.imageinfo.Reset(appIcon, appIconLen, appThumbnail, appThumbnailLen);
if (appIcon != nullptr) {
free(appIcon);
appIcon = nullptr;
}
if (appThumbnail != nullptr) {
free(appThumbnail);
appThumbnail = nullptr;
}
authParam.imageinfo = mMsgRequestAuthPtr_->mImageInfo_;
}
}
+18
View File
@@ -0,0 +1,18 @@
# 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.
group("test") {
testonly = true
deps = [ "unittest:unittest" ]
}
+78
View File
@@ -0,0 +1,78 @@
# 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/test.gni")
import("//foundation/distributedhardware/devicemanager/devicemanager.gni")
module_out_path = "graphic_standard/vsync"
group("unittest") {
testonly = true
deps = [ ":device_manager_impl_test" ]
}
## UnitTest device_manager_impl_test {{{
ohos_unittest("device_manager_impl_test") {
module_out_path = module_out_path
sources = [ "device_manager_impl_test.cpp" ]
deps = [ ":device_manager_test_common" ]
}
## UnitTest device_manager_impl_test }}}
## Build device_manager_test_common.a {{{
config("device_manager_test_common_public_config") {
include_dirs = [
"//utils/native/base/include",
"//utils/system/safwk/native/include",
"//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include",
"//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include/ipc/standard",
"//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include/ipc",
"//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include/notify",
"${utils_path}/include/log",
"${common_path}/include/ipc",
"${common_path}/include/ipc/model",
"${utils_path}/include/ipc/standard",
"${common_path}/include",
"//third_party/json/include",
]
cflags = [
"-Wall",
"-Werror",
"-g3",
"-Dprivate=public",
"-Dprotected=public",
]
}
ohos_static_library("device_manager_test_common") {
testonly = true
visibility = [ ":*" ]
public_configs = [ ":device_manager_test_common_public_config" ]
public_deps = [
"${utils_path}:devicemanagerutils",
"//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core",
"//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk",
"//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy",
"//third_party/googletest:gmock",
"//utils/native/base:utils",
"//utils/native/base:utils",
]
}
## Build device_manager_test_common.a }}}
+183
View File
@@ -0,0 +1,183 @@
/*
* 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 "device_manager_impl_test.h"
#include "device_manager_errno.h"
#include "dm_device_info.h"
#include <unistd.h>
namespace OHOS {
namespace DistributedHardware {
void DeviceManagerImplTest::SetUp()
{
}
void DeviceManagerImplTest::TearDown()
{
}
void DeviceManagerImplTest::SetUpTestCase()
{
}
void DeviceManagerImplTest::TearDownTestCase()
{
}
namespace {
HWTEST_F(DeviceManagerImplTest, InitDeviceManager, testing::ext::TestSize.Level0)
{
std::string packName = "";
int32_t ret= DeviceManager::GetInstance().InitDeviceManager(packName, nullptr);
ASSERT_EQ(ret, DEVICEMANAGER_INVALID_VALUE);
}
HWTEST_F(DeviceManagerImplTest, AuthenticateDevice1, testing::ext::TestSize.Level0)
{
std::string packName = "";
DmDeviceInfo dmDeviceInfo;
DmAppImageInfo dmAppImageInfo;
std::string extra= "";
std::shared_ptr<AuthenticateCallback> callback = nullptr;
int32_t ret= DeviceManager::GetInstance().AuthenticateDevice(packName, dmDeviceInfo, dmAppImageInfo, extra, callback);
ASSERT_EQ(ret, DEVICEMANAGER_INVALID_VALUE);
}
HWTEST_F(DeviceManagerImplTest, AuthenticateDevice2, testing::ext::TestSize.Level0)
{
std::string packName = "com.ohos.helloworld";
DmDeviceInfo dmDeviceInfo;
DmAppImageInfo dmAppImageInfo;
std::string extra = "";
std::shared_ptr<AuthenticateCallback> callback = nullptr;
std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
EXPECT_CALL(*mockInstance,SendRequest(testing::_,testing::_,testing::_))
.Times(1).WillOnce(testing::Return(DEVICEMANAGER_FAILED));
int32_t ret= DeviceManager::GetInstance().AuthenticateDevice(packName, dmDeviceInfo, dmAppImageInfo, extra, callback);
ASSERT_EQ(ret, DEVICEMANAGER_IPC_FAILED);
DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
}
HWTEST_F(DeviceManagerImplTest, AuthenticateDevice3, testing::ext::TestSize.Level0)
{
std::string packName = "com.ohos.helloworld";
DmDeviceInfo dmDeviceInfo;
DmAppImageInfo dmAppImageInfo;
std::string extra = "";
std::shared_ptr<AuthenticateCallback> callback = nullptr;
std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
EXPECT_CALL(*mockInstance,SendRequest(testing::_,testing::_,testing::_))
.Times(1).WillOnce(testing::Return(DEVICEMANAGER_OK));
int32_t ret= DeviceManager::GetInstance().AuthenticateDevice(packName, dmDeviceInfo, dmAppImageInfo, extra, callback);
ASSERT_EQ(ret, DEVICEMANAGER_OK);
DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
}
HWTEST_F(DeviceManagerImplTest, CheckAuthentication1, testing::ext::TestSize.Level0)
{
std::string packName = "";
std::string authPara = "";
std::shared_ptr<CheckAuthCallback> callback = nullptr;
int32_t ret = DeviceManager::GetInstance().CheckAuthentication(packName, authPara ,callback);
ASSERT_EQ(ret, DEVICEMANAGER_INVALID_VALUE);
}
HWTEST_F(DeviceManagerImplTest, CheckAuthentication2, testing::ext::TestSize.Level0)
{
std::string packName = "com.ohos.helloworld";
std::string authPara = "";
std::shared_ptr<CheckAuthCallback> callback = nullptr;
std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
EXPECT_CALL(*mockInstance,SendRequest(testing::_,testing::_,testing::_))
.Times(1).WillOnce(testing::Return(DEVICEMANAGER_FAILED));
int32_t ret= DeviceManager::GetInstance().CheckAuthentication(packName, authPara ,callback);
ASSERT_EQ(ret, DEVICEMANAGER_IPC_FAILED);
DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
}
HWTEST_F(DeviceManagerImplTest, CheckAuthentication3, testing::ext::TestSize.Level0)
{
std::string packName = "com.ohos.helloworld";
std::string authPara = "";
std::shared_ptr<CheckAuthCallback> callback = nullptr;
std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
EXPECT_CALL(*mockInstance,SendRequest(testing::_,testing::_,testing::_))
.Times(1).WillOnce(testing::Return(DEVICEMANAGER_OK));
int32_t ret= DeviceManager::GetInstance().CheckAuthentication(packName, authPara ,callback);
ASSERT_EQ(ret, DEVICEMANAGER_OK);
DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
}
HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery1, testing::ext::TestSize.Level0)
{
std::string packName = "";
DmSubscribeInfo subscribeInfo;
std::shared_ptr<DiscoverCallback> callback = nullptr;
int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo ,callback);
ASSERT_EQ(ret, DEVICEMANAGER_INVALID_VALUE);
}
HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery2, testing::ext::TestSize.Level0)
{
std::string packName = "com.ohos.helloworld";
DmSubscribeInfo subscribeInfo;
test_callback_ = std::make_shared<DeviceDiscoverCallback>();
std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
EXPECT_CALL(*mockInstance,SendRequest(testing::_,testing::_,testing::_))
.Times(1).WillOnce(testing::Return(DEVICEMANAGER_OK));
int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo ,test_callback_);
ASSERT_EQ(ret, DEVICEMANAGER_OK);
DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
}
HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery3, testing::ext::TestSize.Level0)
{
std::string packName = "com.ohos.helloworld";
DmSubscribeInfo subscribeInfo;
test_callback_ = std::make_shared<DeviceDiscoverCallback>();
std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
EXPECT_CALL(*mockInstance,SendRequest(testing::_,testing::_,testing::_))
.Times(1).WillOnce(testing::Return(DEVICEMANAGER_FAILED));
int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo ,test_callback_);
ASSERT_EQ(ret, DEVICEMANAGER_IPC_FAILED);
DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
}
} // namespace
void DeviceDiscoverCallback::OnDiscoverySuccess(uint16_t subscribeId)
{
}
void DeviceDiscoverCallback::OnDiscoverFailed(uint16_t subscribeId, int32_t failedReason)
{
}
void DeviceDiscoverCallback::OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo)
{
}
} // namespace Vsync
} // namespace OHOS
+50
View File
@@ -0,0 +1,50 @@
/*
* 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.
*/
#ifndef OHOS_DEVICE_MANAGER_IMPL_TEST_H
#define OHOS_DEVICE_MANAGER_IMPL_TEST_H
#include <gtest/gtest.h>
#include <refbase.h>
#include "mock/mock_ipc_client_proxy.h"
#include "device_manager_impl.h"
#include "device_manager_callback.h"
#include "device_manager.h"
namespace OHOS {
namespace DistributedHardware {
class DeviceManagerImplTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
virtual void SetUp() override;
virtual void TearDown() override;
private:
std::shared_ptr<DiscoverCallback> test_callback_ = nullptr;
};
class DeviceDiscoverCallback : public DiscoverCallback{
public:
DeviceDiscoverCallback() : DiscoverCallback() {}
virtual ~DeviceDiscoverCallback() override {}
virtual void OnDiscoverySuccess(uint16_t subscribeId) override;
virtual void OnDiscoverFailed(uint16_t subscribeId, int32_t failedReason) override;
virtual void OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo) override;
};
} // namespace Vsync
} // namespace OHOS
#endif // OHOS_DEVICE_MANAGER_IMPL_TEST_H
@@ -0,0 +1,33 @@
/*
* 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.
*/
#ifndef OHOS_MOCK_IPC_CLIENT_PROXY_H
#define OHOS_MOCK_IPC_CLIENT_PROXY_H
#include <gmock/gmock.h>
#include <refbase.h>
#include "ipc_client_proxy.h"
namespace OHOS {
namespace DistributedHardware {
class MockIpcClient : public IpcClientProxy ,public RefBase{
public:
MOCK_METHOD3(SendRequest,int32_t(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp));
};
} // namespace DistributedHardware
} // namespace OHOS
#endif // OHOS_MOCK_IPC_CLIENT_PROXY_H