diff --git a/common/include/constants.h b/common/include/constants.h index 31e75813d53868e30dd9c4e7c4cf2ae8c1ad0df5..f8cf91ac7973277f053df97a9bfe7613f6064e6f 100644 --- a/common/include/constants.h +++ b/common/include/constants.h @@ -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 diff --git a/interfaces/inner_kits/native_cpp/include/dm_app_image_info.h b/interfaces/inner_kits/native_cpp/include/dm_app_image_info.h index 164bd5f4c74f58bf64858516ecc40677197ffe53..d088ce58a0fce854ef8b5e6ea746c1672200bc16 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_app_image_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_app_image_info.h @@ -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) { diff --git a/ohos.build b/ohos.build index eeead16d4e580bc2ea1981776019babd0d790451..f544c0dcc5235233f3748d3b24b5313debe618b8 100644 --- a/ohos.build +++ b/ohos.build @@ -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": [] } }, diff --git a/services/devicemanagerservice/include/message/msg_request_auth.h b/services/devicemanagerservice/include/message/msg_request_auth.h index af6df707921cf0c0d589f902022ccd5772681aeb..2af901915e056edf10abd52146e15bac10188a8d 100644 --- a/services/devicemanagerservice/include/message/msg_request_auth.h +++ b/services/devicemanagerservice/include/message/msg_request_auth.h @@ -41,7 +41,6 @@ public: std::vector Encode(); static std::shared_ptr Decode(nlohmann::json &json, std::shared_ptr msgIn); static void SetThumbnailSize(nlohmann::json &json, std::shared_ptr 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 msg); static void DecodeDeviceInfo(nlohmann::json &json, std::shared_ptr 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 msgIn, nlohmann::json &json, std::string &deviceId, int32_t index); static bool IsAppInfoValid(nlohmann::json &json); diff --git a/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp b/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp index 24bfb9bbaaabdacd2e1a5ffbfcbec8fc5f325178..77a5b5efa33a7b99c95a8ca6bb753179629d85db 100644 --- a/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp +++ b/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp @@ -103,7 +103,6 @@ int32_t RegisterDeviceManagerListener(IpcIo *req, IpcIo *reply) free(svc); svc = NULL; #endif - char *pkgName = (char *)malloc(len + 1); if (pkgName == NULL) { DMLOG(DM_LOG_ERROR, "malloc failed!"); @@ -130,7 +129,6 @@ int32_t UnRegisterDeviceManagerListener(IpcIo *req, IpcIo *reply) DMLOG(DM_LOG_ERROR, "get para failed"); return DEVICEMANAGER_FAILED; } - CommonSvcId svcId; if (IpcServerListenermgr::GetInstance().GetListenerByPkgName(pkgName, &svcId) != DEVICEMANAGER_OK) { DMLOG(DM_LOG_ERROR, "not found listener by package name."); diff --git a/services/devicemanagerservice/src/message/msg_request_auth.cpp b/services/devicemanagerservice/src/message/msg_request_auth.cpp index 9d9a727f83c3890d13f3f6df6651eef5b844e07b..ef3ede248ddd6be8d95b8ebe8659e7b1ce07a341 100644 --- a/services/devicemanagerservice/src/message/msg_request_auth.cpp +++ b/services/devicemanagerservice/src/message/msg_request_auth.cpp @@ -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_ptrmAppName_ = 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 MsgRequestAuth::Encode() DMLOG(DM_LOG_INFO, "MsgRequestAuth encode started"); std::vector 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 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::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::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(); 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 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 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_ptrmThumbnailSize_ == 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; diff --git a/services/devicemanagerservice/src/requestauth/response_session.cpp b/services/devicemanagerservice/src/requestauth/response_session.cpp index ff71485c368ac96783388c7660e0fea4dfef1084..9405548f656f8d8fa035a319fb927a2cb75a5b13 100644 --- a/services/devicemanagerservice/src/requestauth/response_session.cpp +++ b/services/devicemanagerservice/src/requestauth/response_session.cpp @@ -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_; } } diff --git a/test/BUILD.gn b/test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d29faeb194418a02c8220d91acb1068c98a57961 --- /dev/null +++ b/test/BUILD.gn @@ -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" ] +} diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ad14fadfaca9932393c5a6fe395716066a669d49 --- /dev/null +++ b/test/unittest/BUILD.gn @@ -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 }}} diff --git a/test/unittest/device_manager_impl_test.cpp b/test/unittest/device_manager_impl_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6070729ffbf759182472fdbc004f5d40dda0ad8 --- /dev/null +++ b/test/unittest/device_manager_impl_test.cpp @@ -0,0 +1,185 @@ +/* + * 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 + +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 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 callback = nullptr; + std::shared_ptr mockInstance = std::make_shared(); + 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 callback = nullptr; + std::shared_ptr mockInstance = std::make_shared(); + 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 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 callback = nullptr; + std::shared_ptr mockInstance = std::make_shared(); + 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 callback = nullptr; + std::shared_ptr mockInstance = std::make_shared(); + 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 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(); + std::shared_ptr mockInstance = std::make_shared(); + 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(); + std::shared_ptr mockInstance = std::make_shared(); + 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)subscribeId; +} + +void DeviceDiscoverCallback::OnDiscoverFailed(uint16_t subscribeId, int32_t failedReason) +{ + (void)subscribeId; + (void)failedReason; +} + +void DeviceDiscoverCallback::OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo) +{ + (void)subscribeId; +} +} // namespace Vsync +} // namespace OHOS diff --git a/test/unittest/device_manager_impl_test.h b/test/unittest/device_manager_impl_test.h new file mode 100644 index 0000000000000000000000000000000000000000..9efa3619b2ce0d80a715b6e82702ee5eb26df811 --- /dev/null +++ b/test/unittest/device_manager_impl_test.h @@ -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 +#include + +#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 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 diff --git a/test/unittest/mock/mock_ipc_client_proxy.h b/test/unittest/mock/mock_ipc_client_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..ece83e9fba5db1c3bfda2dbdf4e8f2f709af346e --- /dev/null +++ b/test/unittest/mock/mock_ipc_client_proxy.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 +#include + +#include "ipc_client_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +class MockIpcClientProxy : public IpcClientProxy, public RefBase { +public: + MOCK_METHOD3(SendRequest, int32_t(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp)); +}; +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_MOCK_IPC_CLIENT_PROXY_H