From 0676c6a5fdf7db542c1bace32c8bf360cb6dc847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E4=B8=8D=E5=87=BA=E5=88=AB=E5=90=8D?= Date: Tue, 14 Sep 2021 15:40:33 +0800 Subject: [PATCH 01/11] =?UTF-8?q?IPC=20L1=20FreeBuffer=E5=BD=92=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 想不出别名 --- .../native_cpp/include/dm_device_info.h | 2 +- .../src/ipc/lite/ipc_client_stub.cpp | 8 +++-- .../src/ipc/lite/ipc_cmd_parser.cpp | 11 ------- .../src/ipc/lite/ipc_cmd_parser.cpp | 6 ++-- .../src/ipc/lite/ipc_server_stub.cpp | 31 +++++++------------ 5 files changed, 22 insertions(+), 36 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/include/dm_device_info.h b/interfaces/inner_kits/native_cpp/include/dm_device_info.h index 45ccf822c..076a2c007 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -21,7 +21,7 @@ #include "dm_app_image_info.h" #define DM_MAX_DEVICE_ID_LEN (96) -#define DM_MAX_DEVICE_NAME_LEN (65) +#define DM_MAX_DEVICE_NAME_LEN (128) namespace OHOS { namespace DistributedHardware { diff --git a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_stub.cpp b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_stub.cpp index 9ae99e19a..c24231546 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_stub.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_stub.cpp @@ -36,8 +36,12 @@ static int32_t ClientIpcInterfaceMsgHandle(const IpcContext *ctx, void *ipcMsg, uint32_t code = 0; GetCode(ipcMsg, &code); - DMLOG(DM_LOG_INFO, "receive ipc transact code(%u)", code); - return IpcCmdRegister::GetInstance().OnIpcCmd(code, *io, *ctx, ipcMsg); + int32_t errCode = DEVICEMANAGER_OK; + + errCode = IpcCmdRegister::GetInstance().OnIpcCmd(code, *io, *ctx, ipcMsg); + DMLOG(DM_LOG_INFO, "receive ipc transact code:%u, retCode=%d", code, errCode); + FreeBuffer(ctx, ipcMsg); + return errCode; } int32_t IpcClientStub::Init() diff --git a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_cmd_parser.cpp index 2178b0644..f17317b7e 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_cmd_parser.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_cmd_parser.cpp @@ -195,7 +195,6 @@ ON_IPC_CMD(SERVER_DEVICE_STATE_NOTIFY, IpcIo &reply, const IpcContext &ctx, void const DmDeviceInfo *deviceInfo = (const DmDeviceInfo*)IpcIoPopFlatObj(&reply, &size); if (pkgName == "" || len == 0 || deviceInfo == NULL) { DMLOG(DM_LOG_ERROR, "OnDeviceOnline, get para failed"); - FreeBuffer(&ctx, ipcMsg); return; } switch (deviceState) { @@ -212,7 +211,6 @@ ON_IPC_CMD(SERVER_DEVICE_STATE_NOTIFY, IpcIo &reply, const IpcContext &ctx, void DMLOG(DM_LOG_ERROR, "unknown device state:%d", deviceState); break; } - FreeBuffer(&ctx, ipcMsg); } ON_IPC_CMD(SERVER_DEVICE_FOUND, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) @@ -224,11 +222,9 @@ ON_IPC_CMD(SERVER_DEVICE_FOUND, IpcIo &reply, const IpcContext &ctx, void *ipcMs const DmDeviceInfo *deviceInfo = (const DmDeviceInfo*)IpcIoPopFlatObj(&reply, &size); if (pkgName == "" || len == 0 || deviceInfo == NULL) { DMLOG(DM_LOG_ERROR, "OnDeviceChanged, get para failed"); - FreeBuffer(&ctx, ipcMsg); return; } DeviceManagerNotify::GetInstance().OnDeviceFound(pkgName, subscribeId, *deviceInfo); - FreeBuffer(&ctx, ipcMsg); } ON_IPC_CMD(SERVER_DISCOVER_FINISH, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) @@ -240,7 +236,6 @@ ON_IPC_CMD(SERVER_DISCOVER_FINISH, IpcIo &reply, const IpcContext &ctx, void *ip if (pkgName == "" || len == 0) { DMLOG(DM_LOG_ERROR, "OnDiscoverySuccess, get para failed"); - FreeBuffer(&ctx, ipcMsg); return; } if (failedReason == DEVICEMANAGER_OK) { @@ -248,7 +243,6 @@ ON_IPC_CMD(SERVER_DISCOVER_FINISH, IpcIo &reply, const IpcContext &ctx, void *ip } else { DeviceManagerNotify::GetInstance().OnDiscoverFailed(pkgName, subscribeId, failedReason); } - FreeBuffer(&ctx, ipcMsg); } ON_IPC_CMD(SERVER_AUTH_RESULT, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) @@ -263,11 +257,9 @@ ON_IPC_CMD(SERVER_AUTH_RESULT, IpcIo &reply, const IpcContext &ctx, void *ipcMsg if (pkgName == "" || len == 0 || deviceId == "" || devIdLen == 0) { DMLOG(DM_LOG_ERROR, "OnAuthResult, get para failed"); - FreeBuffer(&ctx, ipcMsg); return; } DeviceManagerNotify::GetInstance().OnAuthResult(pkgName, deviceId, pinToken, status, reason); - FreeBuffer(&ctx, ipcMsg); } ON_IPC_CMD(SERVER_CHECK_AUTH_RESULT, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) @@ -281,11 +273,9 @@ ON_IPC_CMD(SERVER_CHECK_AUTH_RESULT, IpcIo &reply, const IpcContext &ctx, void * if (pkgName == "" || len == 0 || deviceId == "" || devIdLen == 0) { DMLOG(DM_LOG_ERROR, "OnAuthResult, get para failed"); - FreeBuffer(&ctx, ipcMsg); return; } DeviceManagerNotify::GetInstance().OnCheckAuthResult(pkgName, deviceId, resultCode, flag); - FreeBuffer(&ctx, ipcMsg); } ON_IPC_SET_REQUEST(SERVER_GET_AUTHENTCATION_INFO, std::shared_ptr pBaseReq, IpcIo& request, @@ -363,7 +353,6 @@ ON_IPC_CMD(SERVER_DEVICEMANAGER_FA_NOTIFY, IpcIo &reply, const IpcContext &ctx, std::string paramJson = (const char *)IpcIoPopString(&reply, &jsonLen); DeviceManagerNotify::GetInstance().OnFaCall(packagename, paramJson); IpcIoPushInt32(&reply, DEVICEMANAGER_OK); - FreeBuffer(&ctx, ipcMsg); } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/ipc/lite/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/lite/ipc_cmd_parser.cpp index 45dc5a862..b71b3edfd 100644 --- a/services/devicemanagerservice/src/ipc/lite/ipc_cmd_parser.cpp +++ b/services/devicemanagerservice/src/ipc/lite/ipc_cmd_parser.cpp @@ -161,12 +161,14 @@ ON_IPC_SERVER_CMD(GET_TRUST_DEVICE_LIST, IpcIo &req, IpcIo &reply) ON_IPC_SERVER_CMD(REGISTER_DEVICE_MANAGER_LISTENER, IpcIo &req, IpcIo &reply) { - RegisterDeviceManagerListener(&req, &reply); + int32_t errCode = RegisterDeviceManagerListener(&req, &reply); + IpcIoPushInt32(&reply, errCode); } ON_IPC_SERVER_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER, IpcIo &req, IpcIo &reply) { - UnRegisterDeviceManagerListener(&req, &reply); + int32_t errCode = UnRegisterDeviceManagerListener(&req, &reply); + IpcIoPushInt32(&reply, errCode); } ON_IPC_SERVER_CMD(START_DEVICE_DISCOVER, IpcIo &req, IpcIo &reply) diff --git a/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp b/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp index 775245f59..1cb044337 100644 --- a/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp +++ b/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp @@ -77,7 +77,6 @@ static int32_t DeathCb(const IpcContext *context, void *ipcMsg, IpcIo *data, voi sid.token = svcId.token; sid.cookie = svcId.cookie; UnregisterDeathCallback(sid, svcId.cbId); - return DEVICEMANAGER_OK; } @@ -85,13 +84,11 @@ int32_t RegisterDeviceManagerListener(IpcIo *req, IpcIo *reply) { DMLOG(DM_LOG_INFO, "register service listener."); size_t len = 0; - int32_t ret = DEVICEMANAGER_OK; uint8_t *name = IpcIoPopString(req, &len); SvcIdentity *svc = IpcIoPopSvc(req); if (name == NULL || svc == NULL || len == 0) { DMLOG(DM_LOG_ERROR, "get para failed"); - IpcIoPushInt32(reply, ret); - return DEVICEMANAGER_OK; + return DEVICEMANAGER_INVALID_PARAM; } CommonSvcId svcId = {0}; @@ -106,26 +103,21 @@ 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!"); - IpcIoPushInt32(reply, DEVICEMANAGER_FAILED); - return DEVICEMANAGER_FAILED; + return DEVICEMANAGER_MALLOC_ERROR; } if (strcpy_s(pkgName, len + 1, (const char *)name) != DEVICEMANAGER_OK) { DMLOG(DM_LOG_ERROR, "strcpy_s failed!"); free(pkgName); - IpcIoPushInt32(reply, DEVICEMANAGER_FAILED); - return DEVICEMANAGER_FAILED; + return DEVICEMANAGER_COPY_FAILED; } uint32_t cbId = 0; RegisterDeathCallback(NULL, sid, DeathCb, pkgName, &cbId); svcId.cbId = cbId; std::string strPkgName = (const char *)name; - ret = IpcServerListenermgr::GetInstance().RegisterListener(strPkgName, &svcId); - IpcIoPushInt32(reply, ret); - return DEVICEMANAGER_OK; + return IpcServerListenermgr::GetInstance().RegisterListener(strPkgName, &svcId); } int32_t UnRegisterDeviceManagerListener(IpcIo *req, IpcIo *reply) @@ -138,24 +130,23 @@ int32_t UnRegisterDeviceManagerListener(IpcIo *req, IpcIo *reply) return DEVICEMANAGER_FAILED; } - CommonSvcId svcId = {0}; - if (IpcServerListenermgr::GetInstance().GetListenerByPkgName(pkgName, &svcId) != DEVICEMANAGER_OK) { - DMLOG(DM_LOG_ERROR, "not found listener by package name."); - return DEVICEMANAGER_FAILED; - } int32_t ret = IpcServerListenermgr::GetInstance().UnregisterListener(pkgName); if (ret == DEVICEMANAGER_OK) { + CommonSvcId svcId; + if (IpcServerListenermgr::GetInstance().GetListenerByPkgName(pkgName, &svcId) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "not found listener by package name."); + return DEVICEMANAGER_FAILED; + } #ifdef __LINUX__ BinderRelease(svcId.ipcCtx, svcId.handle); #endif - SvcIdentity sid = {0}; + SvcIdentity sid; sid.handle = svcId.handle; sid.token = svcId.token; sid.cookie = svcId.cookie; ret = UnregisterDeathCallback(sid, svcId.cbId); } - IpcIoPushInt32(reply, ret); - return DEVICEMANAGER_OK; + return ret; } static const char *GetName(Service *service) -- Gitee From 41db26228a4e130c9e4e19c4ea372012b3ba2271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E4=B8=8D=E5=87=BA=E5=88=AB=E5=90=8D?= Date: Tue, 14 Sep 2021 17:49:50 +0800 Subject: [PATCH 02/11] codex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 想不出别名 --- .../ipc/model/ipc_notify_auth_result_req.h | 6 ++--- .../model/ipc_notify_check_auth_result_req.h | 4 +-- .../ipc/model/ipc_notify_device_found_req.h | 2 +- .../ipc/model/ipc_notify_device_state_req.h | 2 +- .../model/ipc_notify_discover_result_req.h | 4 +-- .../ipc/model/ipc_set_useroperation_req.h | 2 +- .../ipc/model/ipc_stop_discovery_req.h | 2 +- .../include/message/msg_request_auth.h | 17 ++++++------ .../include/message/msg_response_auth.h | 10 +++---- .../src/ipc/ipc_server_adapter.cpp | 16 ++++++++---- .../src/ipc/standard/ipc_cmd_parser.cpp | 16 +++++++++--- .../src/softbus/softbus_adapter.cpp | 26 ++++++++++++------- 12 files changed, 65 insertions(+), 42 deletions(-) diff --git a/common/include/ipc/model/ipc_notify_auth_result_req.h b/common/include/ipc/model/ipc_notify_auth_result_req.h index 5fb8e53f7..afee8f664 100644 --- a/common/include/ipc/model/ipc_notify_auth_result_req.h +++ b/common/include/ipc/model/ipc_notify_auth_result_req.h @@ -66,9 +66,9 @@ public: } private: std::string deviceId_; - int32_t pinToken_; - int32_t status_; - int32_t reason_; + int32_t pinToken_ {0}; + int32_t status_ {0}; + int32_t reason_ {0}; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_notify_check_auth_result_req.h b/common/include/ipc/model/ipc_notify_check_auth_result_req.h index f98db1e21..0e153d560 100644 --- a/common/include/ipc/model/ipc_notify_check_auth_result_req.h +++ b/common/include/ipc/model/ipc_notify_check_auth_result_req.h @@ -56,8 +56,8 @@ public: } private: std::string deviceId_; - int32_t result_; - int32_t flag_; + int32_t result_ {0}; + int32_t flag_ {0}; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_notify_device_found_req.h b/common/include/ipc/model/ipc_notify_device_found_req.h index 7ffb99d88..6c0947660 100644 --- a/common/include/ipc/model/ipc_notify_device_found_req.h +++ b/common/include/ipc/model/ipc_notify_device_found_req.h @@ -45,7 +45,7 @@ public: subscribeId_ = subscribeId; } private: - uint16_t subscribeId_; + uint16_t subscribeId_ {0}; DmDeviceInfo dmDeviceInfo_; }; } // namespace DistributedHardware diff --git a/common/include/ipc/model/ipc_notify_device_state_req.h b/common/include/ipc/model/ipc_notify_device_state_req.h index 2ae13ea04..0f20867c5 100644 --- a/common/include/ipc/model/ipc_notify_device_state_req.h +++ b/common/include/ipc/model/ipc_notify_device_state_req.h @@ -45,7 +45,7 @@ public: dmDeviceInfo_ = dmDeviceInfo; } private: - int32_t deviceState_; + int32_t deviceState_ {0}; DmDeviceInfo dmDeviceInfo_; }; } // namespace DistributedHardware diff --git a/common/include/ipc/model/ipc_notify_discover_result_req.h b/common/include/ipc/model/ipc_notify_discover_result_req.h index 00ea2e5d8..ea6776ed0 100644 --- a/common/include/ipc/model/ipc_notify_discover_result_req.h +++ b/common/include/ipc/model/ipc_notify_discover_result_req.h @@ -45,8 +45,8 @@ public: result_ = result; } private: - uint16_t subscribeId_; - int32_t result_; + uint16_t subscribeId_ {0}; + int32_t result_ {0}; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_set_useroperation_req.h b/common/include/ipc/model/ipc_set_useroperation_req.h index 7c7416955..e2f51db3f 100644 --- a/common/include/ipc/model/ipc_set_useroperation_req.h +++ b/common/include/ipc/model/ipc_set_useroperation_req.h @@ -34,7 +34,7 @@ public: } private: - int32_t action_; + int32_t action_ {0}; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_stop_discovery_req.h b/common/include/ipc/model/ipc_stop_discovery_req.h index 3497ec6f3..bf1de6b17 100644 --- a/common/include/ipc/model/ipc_stop_discovery_req.h +++ b/common/include/ipc/model/ipc_stop_discovery_req.h @@ -35,7 +35,7 @@ public: subscribeId_ = subscribeId; } private: - uint16_t subscribeId_; + uint16_t subscribeId_ {0}; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/include/message/msg_request_auth.h b/services/devicemanagerservice/include/message/msg_request_auth.h index f38be9b9e..af6df7079 100644 --- a/services/devicemanagerservice/include/message/msg_request_auth.h +++ b/services/devicemanagerservice/include/message/msg_request_auth.h @@ -30,7 +30,8 @@ namespace OHOS { namespace DistributedHardware { class MsgRequestAuth { public: - MsgRequestAuth() {}; + MsgRequestAuth() = default; + ~MsgRequestAuth() = default; MsgRequestAuth(std::string &token, std::string hostPkgName, std::string targetPkgName, const DmDeviceInfo &devReqInfo, const DmAppImageInfo &imageInfo, std::string &extras) : MsgRequestAuth( token, hostPkgName, targetPkgName, GROUP_VISIBILITY_IS_PRIVATE, devReqInfo, imageInfo, extras) {}; @@ -45,7 +46,7 @@ public: int32_t GetMsgCnt(); std::string GetRequestDeviceId(); public: - std::shared_ptr mHead_; + std::shared_ptr mHead_ {nullptr}; std::string mHostPkg_; std::string mTargetPkg_; std::string mDeviceName_; @@ -56,12 +57,12 @@ public: std::string mAppDescription_; std::string mAppIcon_; std::string mAppThumbnail_; - int32_t mAuthType_; - int32_t mGroupVisibility_; - int32_t mMsgSlice_; - int32_t mMsgCnt_ = 0; - int32_t mThumbnailSize_; - int32_t mAppIconSize_; + 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}; private: std::string ToHexString(int32_t value); std::string EncodeDevInfo(); diff --git a/services/devicemanagerservice/include/message/msg_response_auth.h b/services/devicemanagerservice/include/message/msg_response_auth.h index 8694aadbf..cec27b713 100644 --- a/services/devicemanagerservice/include/message/msg_response_auth.h +++ b/services/devicemanagerservice/include/message/msg_response_auth.h @@ -19,6 +19,7 @@ #include #include +#include "constants.h" #include "nlohmann/json.hpp" #include "msg_head.h" @@ -43,16 +44,15 @@ public: void SavePinCode(int32_t pinCode); int64_t GetRequestId(); std::vector GetSyncGroupList(); - private: - std::shared_ptr mHead_; - int32_t mReply_; + std::shared_ptr mHead_ {nullptr}; + int32_t mReply_ {SESSION_REPLY_UNKNOWN}; std::string mNetId_; std::string mGroupId_; std::string mGroupName_; std::string mDeviceId_; - int32_t mPinCode_; - int64_t mRequestId_; + int32_t mPinCode_ {-1}; + int64_t mRequestId_ {-1}; std::vector mSyncGroupList_; }; } diff --git a/services/devicemanagerservice/src/ipc/ipc_server_adapter.cpp b/services/devicemanagerservice/src/ipc/ipc_server_adapter.cpp index 095dcebda..240306ce5 100644 --- a/services/devicemanagerservice/src/ipc/ipc_server_adapter.cpp +++ b/services/devicemanagerservice/src/ipc/ipc_server_adapter.cpp @@ -77,6 +77,9 @@ int32_t IpcServerAdapter::ModuleInit() int32_t IpcServerAdapter::GetTrustedDeviceList(std::string &pkgName, std::string &extra, DmDeviceInfo **info, int32_t *infoNum) { + if (info == nullptr || infoNum == nullptr) { + return DEVICEMANAGER_NULLPTR; + } DMLOG(DM_LOG_INFO, "In, pkgName: %s", pkgName.c_str()); NodeBasicInfo *nodeInfo = nullptr; int32_t ret = SoftbusAdapter::GetTrustDevices(pkgName, &nodeInfo, infoNum); @@ -91,11 +94,14 @@ int32_t IpcServerAdapter::GetTrustedDeviceList(std::string &pkgName, std::string for (int32_t i = 0; i < *infoNum; ++i) { NodeBasicInfo *nodeBasicInfo = nodeInfo + i; DmDeviceInfo *deviceInfo = *info + i; - - (void)memcpy_s(deviceInfo->deviceId, sizeof(deviceInfo->deviceId), nodeBasicInfo->networkId, - std::min(sizeof(deviceInfo->deviceId), sizeof(nodeBasicInfo->networkId))); - (void)memcpy_s(deviceInfo->deviceName, sizeof(deviceInfo->deviceName), nodeBasicInfo->deviceName, - std::min(sizeof(deviceInfo->deviceName), sizeof(nodeBasicInfo->deviceName))); + if (memcpy_s(deviceInfo->deviceId, sizeof(deviceInfo->deviceId), nodeBasicInfo->networkId, + std::min(sizeof(deviceInfo->deviceId), sizeof(nodeBasicInfo->networkId))) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "memcpy failed"); + } + if (memcpy_s(deviceInfo->deviceName, sizeof(deviceInfo->deviceName), nodeBasicInfo->deviceName, + std::min(sizeof(deviceInfo->deviceName), sizeof(nodeBasicInfo->deviceName))) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "memcpy failed"); + } deviceInfo->deviceTypeId = (DMDeviceType)nodeBasicInfo->deviceTypeId; } FreeNodeInfo(nodeInfo); diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp index 9df2dadcd..875202361 100644 --- a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp @@ -229,8 +229,12 @@ ON_IPC_CMD(START_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply) { std::string pkgName = data.ReadString(); DmSubscribeInfo *subscribeInfo = (DmSubscribeInfo *)data.ReadRawData(sizeof(DmSubscribeInfo)); - DMLOG(DM_LOG_INFO, "pkgName:%s, subscribeId: %d", pkgName.c_str(), subscribeInfo->subscribeId); - int32_t result = IpcServerAdapter::GetInstance().StartDeviceDiscovery(pkgName, *subscribeInfo); + int32_t result = DEVICEMANAGER_NULLPTR; + + if (subscribeInfo != nullptr) { + DMLOG(DM_LOG_INFO, "pkgName:%s, subscribeId: %d", pkgName.c_str(), subscribeInfo->subscribeId); + result = IpcServerAdapter::GetInstance().StartDeviceDiscovery(pkgName, *subscribeInfo); + } if (!reply.WriteInt32(result)) { DMLOG(DM_LOG_ERROR, "write result failed"); return DEVICEMANAGER_WRITE_FAILED; @@ -260,8 +264,12 @@ ON_IPC_CMD(AUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply) int32_t appThumbnailLen = data.ReadInt32(); 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); + int32_t result = DEVICEMANAGER_NULLPTR; + + if (appIcon != nullptr && appThumbnail != nullptr) { + DmAppImageInfo imageInfo(appIcon, appIconLen, appThumbnail, appThumbnailLen); + result = IpcServerAdapter::GetInstance().AuthenticateDevice(pkgName, *deviceInfo, imageInfo, extra); + } if (!reply.WriteInt32(result)) { DMLOG(DM_LOG_ERROR, "write result failed"); return DEVICEMANAGER_WRITE_FAILED; diff --git a/services/devicemanagerservice/src/softbus/softbus_adapter.cpp b/services/devicemanagerservice/src/softbus/softbus_adapter.cpp index d855b7eb1..de85815c8 100644 --- a/services/devicemanagerservice/src/softbus/softbus_adapter.cpp +++ b/services/devicemanagerservice/src/softbus/softbus_adapter.cpp @@ -42,7 +42,7 @@ namespace DistributedHardware { namespace { const std::string DEVICE_MANAGER_PACKAGE_NAME = "com.huawei.devicemanager"; const int32_t CHECK_INTERVAL = 100000; // 100ms -const int32_t SUBSCRIBE_ID_PREFIX_LEN = 16; +const uint32_t SUBSCRIBE_ID_PREFIX_LEN = 16; const int32_t SUBSCRIBE_ID_MASK = 0x0000FFFF; const int32_t DISCOVER_DEVICEINFO_MAX_SIZE = 20; } @@ -483,20 +483,28 @@ ConnectionAddr *SoftbusAdapter::GetConnectAddr(std::string deviceId) void SoftbusAdapter::NodeBasicInfoCopyToDmDevice(DmDeviceInfo &dmDeviceInfo, NodeBasicInfo &nodeBasicInfo) { (void)memset_s(&dmDeviceInfo, sizeof(DmDeviceInfo), 0, sizeof(DmDeviceInfo)); - (void)memcpy_s(dmDeviceInfo.deviceId, sizeof(dmDeviceInfo.deviceId), nodeBasicInfo.networkId, - std::min(sizeof(dmDeviceInfo.deviceId), sizeof(nodeBasicInfo.networkId))); - (void)memcpy_s(dmDeviceInfo.deviceName, sizeof(dmDeviceInfo.deviceName), nodeBasicInfo.deviceName, - std::min(sizeof(dmDeviceInfo.deviceName), sizeof(nodeBasicInfo.deviceName))); + if (memcpy_s(dmDeviceInfo.deviceId, sizeof(dmDeviceInfo.deviceId), nodeBasicInfo.networkId, + std::min(sizeof(dmDeviceInfo.deviceId), sizeof(nodeBasicInfo.networkId))) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "memcpy failed"); + } + if (memcpy_s(dmDeviceInfo.deviceName, sizeof(dmDeviceInfo.deviceName), nodeBasicInfo.deviceName, + std::min(sizeof(dmDeviceInfo.deviceName), sizeof(nodeBasicInfo.deviceName))) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "memcpy failed"); + } dmDeviceInfo.deviceTypeId = (DMDeviceType)nodeBasicInfo.deviceTypeId; } void SoftbusAdapter::DeviceInfoCopyToDmDevice(DmDeviceInfo &dmDeviceInfo, const DeviceInfo &deviceInfo) { (void)memset_s(&dmDeviceInfo, sizeof(DmDeviceInfo), 0, sizeof(DmDeviceInfo)); - (void)memcpy_s(dmDeviceInfo.deviceId, sizeof(dmDeviceInfo.deviceId), deviceInfo.devId, - std::min(sizeof(dmDeviceInfo.deviceId), sizeof(deviceInfo.devId))); - (void)memcpy_s(dmDeviceInfo.deviceName, sizeof(dmDeviceInfo.deviceName), deviceInfo.devName, - std::min(sizeof(dmDeviceInfo.deviceName), sizeof(deviceInfo.devName))); + if (memcpy_s(dmDeviceInfo.deviceId, sizeof(dmDeviceInfo.deviceId), deviceInfo.devId, + std::min(sizeof(dmDeviceInfo.deviceId), sizeof(deviceInfo.devId))) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "memcpy failed"); + } + if (memcpy_s(dmDeviceInfo.deviceName, sizeof(dmDeviceInfo.deviceName), deviceInfo.devName, + std::min(sizeof(dmDeviceInfo.deviceName), sizeof(deviceInfo.devName))) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "memcpy failed"); + } dmDeviceInfo.deviceTypeId = (DMDeviceType)deviceInfo.devType; } -- Gitee From 8168260780ff919a339ca5d0e93550be09caafbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E4=B8=8D=E5=87=BA=E5=88=AB=E5=90=8D?= Date: Wed, 15 Sep 2021 16:00:32 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E5=9B=9E=E9=80=80codex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 想不出别名 --- .../src/ipc/standard/ipc_cmd_parser.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp index 875202361..c6bc39322 100644 --- a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp @@ -264,12 +264,10 @@ ON_IPC_CMD(AUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply) int32_t appThumbnailLen = data.ReadInt32(); uint8_t *appIcon = appIconLen > 0? (uint8_t *)data.ReadRawData(appIconLen) : nullptr; uint8_t *appThumbnail = appThumbnailLen > 0? (uint8_t *)data.ReadRawData(appThumbnailLen) : nullptr; - int32_t result = DEVICEMANAGER_NULLPTR; - if (appIcon != nullptr && appThumbnail != nullptr) { - DmAppImageInfo imageInfo(appIcon, appIconLen, appThumbnail, appThumbnailLen); - 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"); return DEVICEMANAGER_WRITE_FAILED; -- Gitee From e1aa0acd9fea9fdb851701603bf4d3b9cd73bfa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E4=B8=8D=E5=87=BA=E5=88=AB=E5=90=8D?= Date: Wed, 15 Sep 2021 17:49:22 +0800 Subject: [PATCH 04/11] bug fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 想不出别名 --- .../src/ipc/lite/ipc_server_stub.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp b/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp index 1cb044337..77a5b5efa 100644 --- a/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp +++ b/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp @@ -129,14 +129,13 @@ 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."); + return DEVICEMANAGER_FAILED; + } int32_t ret = IpcServerListenermgr::GetInstance().UnregisterListener(pkgName); if (ret == DEVICEMANAGER_OK) { - CommonSvcId svcId; - if (IpcServerListenermgr::GetInstance().GetListenerByPkgName(pkgName, &svcId) != DEVICEMANAGER_OK) { - DMLOG(DM_LOG_ERROR, "not found listener by package name."); - return DEVICEMANAGER_FAILED; - } #ifdef __LINUX__ BinderRelease(svcId.ipcCtx, svcId.handle); #endif -- Gitee From 6f795abb0f6efe7abfe302ec4ab7b0305360ba79 Mon Sep 17 00:00:00 2001 From: yuqianyuan Date: Thu, 16 Sep 2021 15:02:18 +0800 Subject: [PATCH 05/11] fix appImageInfo bug Signed-off-by: yuqianyuan --- common/include/constants.h | 4 + .../native_cpp/include/dm_app_image_info.h | 50 +++++ ohos.build | 3 + .../include/message/msg_request_auth.h | 9 +- .../src/ipc/standard/ipc_cmd_parser.cpp | 4 +- .../src/message/msg_request_auth.cpp | 158 ++++++++------- .../src/requestauth/response_session.cpp | 16 +- test/BUILD.gn | 18 ++ test/unittest/BUILD.gn | 78 ++++++++ test/unittest/device_manager_impl_test.cpp | 183 ++++++++++++++++++ test/unittest/device_manager_impl_test.h | 50 +++++ test/unittest/mock/mock_ipc_client_proxy.h | 33 ++++ 12 files changed, 503 insertions(+), 103 deletions(-) create mode 100644 test/BUILD.gn create mode 100644 test/unittest/BUILD.gn create mode 100644 test/unittest/device_manager_impl_test.cpp create mode 100644 test/unittest/device_manager_impl_test.h create mode 100644 test/unittest/mock/mock_ipc_client_proxy.h diff --git a/common/include/constants.h b/common/include/constants.h index 31e75813d..f8cf91ac7 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 164bd5f4c..d088ce58a 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 eeead16d4..f544c0dcc 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 af6df7079..2af901915 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/standard/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp index c6bc39322..17ebb26f2 100644 --- a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp @@ -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"); diff --git a/services/devicemanagerservice/src/message/msg_request_auth.cpp b/services/devicemanagerservice/src/message/msg_request_auth.cpp index 9d9a727f8..ef3ede248 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 ff71485c3..9405548f6 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 000000000..d29faeb19 --- /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 000000000..ad14fadfa --- /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 000000000..b757bf77d --- /dev/null +++ b/test/unittest/device_manager_impl_test.cpp @@ -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 + +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 DeviceDiscoverCallback::OnDiscoverFailed(uint16_t subscribeId, int32_t failedReason) +{ + +} + +void DeviceDiscoverCallback::OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo) +{ + +} +} // 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 000000000..b3cd5fd9c --- /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 000000000..f2c1e3966 --- /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 MockIpcClient : 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 -- Gitee From a52dd026516fdfd7ba2bbc10bf3e96af8b316f64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E4=B8=8D=E5=87=BA=E5=88=AB=E5=90=8D?= Date: Thu, 16 Sep 2021 15:17:46 +0800 Subject: [PATCH 06/11] L1 do not support js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 想不出别名 --- BUILD.gn | 1 - .../src/ipc/lite/ipc_client_stub.cpp | 2 +- .../src/ipc/lite/ipc_cmd_parser.cpp | 13 +- interfaces/kits/js/BUILD.gn | 147 ++++++------------ utils/include/ipc/lite/ipc_cmd_register.h | 10 +- utils/src/ipc/lite/ipc_cmd_register.cpp | 4 +- 6 files changed, 59 insertions(+), 118 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 8029a02d0..103ee5a70 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -18,7 +18,6 @@ if (defined(ohos_lite)) { "utils:devicemanagerutils", "services/devicemanagerservice:devicemanagerservice", "interfaces/inner_kits/native_cpp:devicemanagersdk", - "interfaces/kits/js:devicemanager", ] } } diff --git a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_stub.cpp b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_stub.cpp index c24231546..b500aebb2 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_stub.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_stub.cpp @@ -38,7 +38,7 @@ static int32_t ClientIpcInterfaceMsgHandle(const IpcContext *ctx, void *ipcMsg, GetCode(ipcMsg, &code); int32_t errCode = DEVICEMANAGER_OK; - errCode = IpcCmdRegister::GetInstance().OnIpcCmd(code, *io, *ctx, ipcMsg); + errCode = IpcCmdRegister::GetInstance().OnIpcCmd(code, *io); DMLOG(DM_LOG_INFO, "receive ipc transact code:%u, retCode=%d", code, errCode); FreeBuffer(ctx, ipcMsg); return errCode; diff --git a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_cmd_parser.cpp index f17317b7e..bd67cd215 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_cmd_parser.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_cmd_parser.cpp @@ -186,7 +186,7 @@ ON_IPC_READ_RESPONSE(CHECK_AUTHENTICATION, IpcIo &reply, std::shared_ptr return DEVICEMANAGER_OK; } -ON_IPC_CMD(SERVER_DEVICE_STATE_NOTIFY, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +ON_IPC_CMD(SERVER_DEVICE_STATE_NOTIFY, IpcIo &reply) { size_t len = 0; std::string pkgName = (const char *)IpcIoPopString(&reply, &len); @@ -213,7 +213,7 @@ ON_IPC_CMD(SERVER_DEVICE_STATE_NOTIFY, IpcIo &reply, const IpcContext &ctx, void } } -ON_IPC_CMD(SERVER_DEVICE_FOUND, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +ON_IPC_CMD(SERVER_DEVICE_FOUND, IpcIo &reply) { size_t len = 0; std::string pkgName = (const char *)IpcIoPopString(&reply, &len); @@ -227,7 +227,7 @@ ON_IPC_CMD(SERVER_DEVICE_FOUND, IpcIo &reply, const IpcContext &ctx, void *ipcMs DeviceManagerNotify::GetInstance().OnDeviceFound(pkgName, subscribeId, *deviceInfo); } -ON_IPC_CMD(SERVER_DISCOVER_FINISH, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +ON_IPC_CMD(SERVER_DISCOVER_FINISH, IpcIo &reply) { size_t len = 0; std::string pkgName = (const char *)IpcIoPopString(&reply, &len); @@ -245,7 +245,7 @@ ON_IPC_CMD(SERVER_DISCOVER_FINISH, IpcIo &reply, const IpcContext &ctx, void *ip } } -ON_IPC_CMD(SERVER_AUTH_RESULT, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +ON_IPC_CMD(SERVER_AUTH_RESULT, IpcIo &reply) { size_t len = 0; std::string pkgName = (const char *)IpcIoPopString(&reply, &len); @@ -262,7 +262,7 @@ ON_IPC_CMD(SERVER_AUTH_RESULT, IpcIo &reply, const IpcContext &ctx, void *ipcMsg DeviceManagerNotify::GetInstance().OnAuthResult(pkgName, deviceId, pinToken, status, reason); } -ON_IPC_CMD(SERVER_CHECK_AUTH_RESULT, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +ON_IPC_CMD(SERVER_CHECK_AUTH_RESULT, IpcIo &reply) { size_t len = 0; std::string pkgName = (const char *)IpcIoPopString(&reply, &len); @@ -345,14 +345,13 @@ ON_IPC_READ_RESPONSE(SERVER_USER_AUTHORIZATION_OPERATION, IpcIo& reply, std::sha return DEVICEMANAGER_OK; } -ON_IPC_CMD(SERVER_DEVICEMANAGER_FA_NOTIFY, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +ON_IPC_CMD(SERVER_DEVICEMANAGER_FA_NOTIFY, IpcIo &reply) { size_t len = 0; std::string packagename = (const char *)IpcIoPopString(&reply, &len); size_t jsonLen = 0; std::string paramJson = (const char *)IpcIoPopString(&reply, &jsonLen); DeviceManagerNotify::GetInstance().OnFaCall(packagename, paramJson); - IpcIoPushInt32(&reply, DEVICEMANAGER_OK); } } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index b82cf50ab..5c6777207 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -11,115 +11,58 @@ # See the License for the specific language governing permissions and # limitations under the License. -if (defined(ohos_lite)) { - import("//build/lite/config/component/lite_component.gni") -} else { - import("//build/ohos.gni") -} +import("//build/ohos.gni") import("//foundation/distributedhardware/devicemanager/devicemanager.gni") -if (defined(ohos_lite)) { - shared_library("devicemanager") { - include_dirs = [ - "//third_party/node/src", - "//third_party/json/include", - "${common_path}/include", - "//foundation/ace/napi/native_engine", - "//foundation/ace/napi/interfaces/kits", - "//utils/native/base/include", - "include", - "${utils_path}/include/log", - "${common_path}/include/ipc", - "${common_path}/include/ipc/model", - "${common_path}/include/ipc/lite", - "${innerkits_path}/native_cpp/include", - ] - include_dirs += [ - "//utils/native/lite/include", - "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", - "//third_party/bounds_checking_function/include", - "//foundation/communication/ipc_lite/interfaces/kits", - "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", - ] - - sources = [ - "src/dm_native_event.cpp", - "src/native_devicemanager_js.cpp", - ] - - deps = [ - "${utils_path}:devicemanagerutils", - "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", - "//foundation/communication/ipc_lite:liteipc_adapter", - "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", - "//foundation/distributedschedule/samgr_lite/samgr:samgr", - "//third_party/bounds_checking_function:libsec_shared", - "//utils/native/lite:utils", - ] +ohos_shared_library("devicemanager") { + include_dirs = [ + "//third_party/node/src", + "//third_party/json/include", + "${common_path}/include", + "//foundation/ace/napi/native_engine", + "//foundation/ace/napi/interfaces/kits", + "//utils/native/base/include", + "include", + "${utils_path}/include/log", + "${common_path}/include/ipc", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/standard", + ] - cflags_cc = build_flags + sources = [ + "src/dm_native_event.cpp", + "src/native_devicemanager_js.cpp", + ] - defines = [ - "LITE_DEVICE", - "HI_LOG_ENABLE", - "DH_LOG_TAG=\"devicemanagerkit_js\"", - "LOG_DOMAIN=0xD004100", - ] - } - group("devicemanager_native_js") { - deps = [ ":devicemanager" ] - } -} else { - ohos_shared_library("devicemanager") { - include_dirs = [ - "//third_party/node/src", - "//third_party/json/include", - "${common_path}/include", - "//foundation/ace/napi/native_engine", - "//foundation/ace/napi/interfaces/kits", - "//utils/native/base/include", - "include", - "${utils_path}/include/log", - "${common_path}/include/ipc", - "${innerkits_path}/native_cpp/include", - "${innerkits_path}/native_cpp/include/standard", - ] + deps = [ + "${utils_path}:devicemanagerutils", + "//foundation/ace/napi:ace_napi", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//utils/native/base:utils", + ] - sources = [ - "src/dm_native_event.cpp", - "src/native_devicemanager_js.cpp", - ] + cflags_cc = build_flags - deps = [ - "${utils_path}:devicemanagerutils", - "//foundation/ace/napi:ace_napi", - "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", - "//utils/native/base:utils", - ] + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"devicemanagerkit_js\"", + "LOG_DOMAIN=0xD004100", + ] - cflags_cc = build_flags + external_deps = [ + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:appexecfwk_core", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] - defines = [ - "HI_LOG_ENABLE", - "DH_LOG_TAG=\"devicemanagerkit_js\"", - "LOG_DOMAIN=0xD004100", - ] - - external_deps = [ - "appexecfwk_standard:appexecfwk_base", - "appexecfwk_standard:appexecfwk_core", - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr_L2:samgr_proxy", - ] - - subsystem_name = "distributedhardware" - relative_install_dir = "module/distributedhardware" - part_name = "device_manager_base" - } + subsystem_name = "distributedhardware" + relative_install_dir = "module/distributedhardware" + part_name = "device_manager_base" +} - group("devicemanager_native_js") { - deps = [ ":devicemanager" ] - } +group("devicemanager_native_js") { + deps = [ ":devicemanager" ] } diff --git a/utils/include/ipc/lite/ipc_cmd_register.h b/utils/include/ipc/lite/ipc_cmd_register.h index 6b52515aa..36c20961f 100644 --- a/utils/include/ipc/lite/ipc_cmd_register.h +++ b/utils/include/ipc/lite/ipc_cmd_register.h @@ -49,8 +49,8 @@ namespace DistributedHardware { IpcRegisterReadResponseFunc##cmdCode g_IpcRegisterReadResponseFunc##cmdCode; \ static int32_t IpcReadResponse##cmdCode(paraA, paraB) \ -#define ON_IPC_CMD(cmdCode, paraA, paraB, paraC) \ - static void IpcCmdProcess##cmdCode(paraA, paraB, paraC); \ +#define ON_IPC_CMD(cmdCode, paraA) \ + static void IpcCmdProcess##cmdCode(paraA); \ struct IpcRegisterCmdProcessFunc##cmdCode { \ IpcRegisterCmdProcessFunc##cmdCode() \ { \ @@ -58,7 +58,7 @@ namespace DistributedHardware { } \ }; \ IpcRegisterCmdProcessFunc##cmdCode g_IpcRegisterCmdProcessFunc##cmdCode; \ - static void IpcCmdProcess##cmdCode(paraA, paraB, paraC) \ + static void IpcCmdProcess##cmdCode(paraA) \ #define ON_IPC_SERVER_CMD(cmdCode, paraA, paraB) \ static void IpcServerCmdProcess##cmdCode(paraA, paraB); \ @@ -75,7 +75,7 @@ namespace DistributedHardware { using SetIpcRequestFunc = int32_t (*)(std::shared_ptr pBaseReq, IpcIo &request, uint8_t *buffer, size_t bufferLen); using ReadResponseFunc = int32_t (*)(IpcIo &reply, std::shared_ptr pBaseRsp); -using OnIpcCmdFunc = void (*)(IpcIo &reply, const IpcContext &ctx, void *ipcMsg); +using OnIpcCmdFunc = void (*)(IpcIo &reply); using OnIpcServerCmdFunc = void (*)(IpcIo &req, IpcIo &reply); class IpcCmdRegister { @@ -100,7 +100,7 @@ public: int32_t SetRequest(int32_t cmdCode, std::shared_ptr pBaseReq, IpcIo &request, uint8_t *buffer, size_t buffLen); int32_t ReadResponse(int32_t cmdCode, IpcIo &reply, std::shared_ptr pBaseRsp); - int32_t OnIpcCmd(int32_t cmdCode, IpcIo &reply, const IpcContext &ctx, void *ipcMsg); + int32_t OnIpcCmd(int32_t cmdCode, IpcIo &reply); int32_t OnIpcServerCmd(int32_t cmdCode, IpcIo &req, IpcIo &reply); private: std::unordered_map setIpcRequestFuncMap_; diff --git a/utils/src/ipc/lite/ipc_cmd_register.cpp b/utils/src/ipc/lite/ipc_cmd_register.cpp index 8faa2a22b..4fbab5a16 100644 --- a/utils/src/ipc/lite/ipc_cmd_register.cpp +++ b/utils/src/ipc/lite/ipc_cmd_register.cpp @@ -43,14 +43,14 @@ int32_t IpcCmdRegister::ReadResponse(int32_t cmdCode, IpcIo &reply, std::shared_ return (readResponseMapIter->second)(reply, pBaseRsp); } -int32_t IpcCmdRegister::OnIpcCmd(int32_t cmdCode, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +int32_t IpcCmdRegister::OnIpcCmd(int32_t cmdCode, IpcIo &reply) { auto onIpcCmdMapIter = onIpcCmdFuncMap_.find(cmdCode); if (onIpcCmdMapIter == onIpcCmdFuncMap_.end()) { DMLOG(DM_LOG_ERROR, "cmdCode:%d not register OnIpcCmdFunc", cmdCode); return DEVICEMANAGER_IPC_NOT_REGISTER_FUNC; } - (onIpcCmdMapIter->second)(reply, ctx, ipcMsg); + (onIpcCmdMapIter->second)(reply); return DEVICEMANAGER_OK; } -- Gitee From 4cd4cfdfc5dcd4d7214cd427c4732bbeeb4aab9b Mon Sep 17 00:00:00 2001 From: yuqianyuan Date: Thu, 16 Sep 2021 16:14:36 +0800 Subject: [PATCH 07/11] bug fix Signed-off-by: yuqianyuan --- test/unittest/device_manager_impl_test.cpp | 37 +++++++++++----------- test/unittest/mock/mock_ipc_client_proxy.h | 4 +-- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/test/unittest/device_manager_impl_test.cpp b/test/unittest/device_manager_impl_test.cpp index b757bf77d..e4a7e79f2 100644 --- a/test/unittest/device_manager_impl_test.cpp +++ b/test/unittest/device_manager_impl_test.cpp @@ -43,7 +43,6 @@ 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) @@ -53,7 +52,8 @@ HWTEST_F(DeviceManagerImplTest, AuthenticateDevice1, testing::ext::TestSize.Leve DmAppImageInfo dmAppImageInfo; std::string extra= ""; std::shared_ptr callback = nullptr; - int32_t ret= DeviceManager::GetInstance().AuthenticateDevice(packName, dmDeviceInfo, dmAppImageInfo, extra, callback); + int32_t ret= DeviceManager::GetInstance().AuthenticateDevice(packName, dmDeviceInfo, + dmAppImageInfo, extra, callback); ASSERT_EQ(ret, DEVICEMANAGER_INVALID_VALUE); } @@ -67,8 +67,9 @@ HWTEST_F(DeviceManagerImplTest, AuthenticateDevice2, testing::ext::TestSize.Leve 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); + .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; } @@ -82,9 +83,10 @@ HWTEST_F(DeviceManagerImplTest, AuthenticateDevice3, testing::ext::TestSize.Leve 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); + 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; } @@ -94,7 +96,7 @@ HWTEST_F(DeviceManagerImplTest, CheckAuthentication1, testing::ext::TestSize.Lev std::string packName = ""; std::string authPara = ""; std::shared_ptr callback = nullptr; - int32_t ret = DeviceManager::GetInstance().CheckAuthentication(packName, authPara ,callback); + int32_t ret = DeviceManager::GetInstance().CheckAuthentication(packName, authPara, callback); ASSERT_EQ(ret, DEVICEMANAGER_INVALID_VALUE); } @@ -106,8 +108,8 @@ HWTEST_F(DeviceManagerImplTest, CheckAuthentication2, testing::ext::TestSize.Lev 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); + .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; } @@ -120,8 +122,8 @@ HWTEST_F(DeviceManagerImplTest, CheckAuthentication3, testing::ext::TestSize.Lev 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); + .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; } @@ -131,7 +133,7 @@ HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery1, testing::ext::TestSize.Le std::string packName = ""; DmSubscribeInfo subscribeInfo; std::shared_ptr callback = nullptr; - int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo ,callback); + int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo, callback); ASSERT_EQ(ret, DEVICEMANAGER_INVALID_VALUE); } @@ -143,8 +145,8 @@ HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery2, testing::ext::TestSize.Le 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_); + .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; } @@ -157,12 +159,11 @@ HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery3, testing::ext::TestSize.Le 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_); + .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) diff --git a/test/unittest/mock/mock_ipc_client_proxy.h b/test/unittest/mock/mock_ipc_client_proxy.h index f2c1e3966..49b1b06f7 100644 --- a/test/unittest/mock/mock_ipc_client_proxy.h +++ b/test/unittest/mock/mock_ipc_client_proxy.h @@ -23,9 +23,9 @@ namespace OHOS { namespace DistributedHardware { -class MockIpcClient : public IpcClientProxy ,public RefBase{ +class MockIpcClient : public IpcClientProxy, public RefBase{ public: - MOCK_METHOD3(SendRequest,int32_t(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp)); + MOCK_METHOD3(SendRequest, int32_t(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp)); }; } // namespace DistributedHardware } // namespace OHOS -- Gitee From 08f02607bf02f81fd921d1f85eb643e3393affb8 Mon Sep 17 00:00:00 2001 From: yuqianyuan Date: Thu, 16 Sep 2021 16:55:46 +0800 Subject: [PATCH 08/11] bug fix Signed-off-by: yuqianyuan --- .../src/ipc/standard/ipc_cmd_parser.cpp | 4 ++-- test/unittest/device_manager_impl_test.cpp | 17 +++++++++-------- test/unittest/device_manager_impl_test.h | 2 +- test/unittest/mock/mock_ipc_client_proxy.h | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp index 17ebb26f2..c6bc39322 100644 --- a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp @@ -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"); diff --git a/test/unittest/device_manager_impl_test.cpp b/test/unittest/device_manager_impl_test.cpp index e4a7e79f2..ddd504ba2 100644 --- a/test/unittest/device_manager_impl_test.cpp +++ b/test/unittest/device_manager_impl_test.cpp @@ -66,7 +66,7 @@ HWTEST_F(DeviceManagerImplTest, AuthenticateDevice2, testing::ext::TestSize.Leve std::shared_ptr callback = nullptr; std::shared_ptr mockInstance = std::make_shared(); DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; - EXPECT_CALL(*mockInstance,SendRequest(testing::_,testing::_,testing::_)) + 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); @@ -107,7 +107,7 @@ HWTEST_F(DeviceManagerImplTest, CheckAuthentication2, testing::ext::TestSize.Lev std::shared_ptr callback = nullptr; std::shared_ptr mockInstance = std::make_shared(); DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; - EXPECT_CALL(*mockInstance,SendRequest(testing::_,testing::_,testing::_)) + 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); @@ -121,7 +121,7 @@ HWTEST_F(DeviceManagerImplTest, CheckAuthentication3, testing::ext::TestSize.Lev std::shared_ptr callback = nullptr; std::shared_ptr mockInstance = std::make_shared(); DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; - EXPECT_CALL(*mockInstance,SendRequest(testing::_,testing::_,testing::_)) + 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); @@ -144,7 +144,7 @@ HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery2, testing::ext::TestSize.Le test_callback_ = std::make_shared(); std::shared_ptr mockInstance = std::make_shared(); DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; - EXPECT_CALL(*mockInstance,SendRequest(testing::_,testing::_,testing::_)) + 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); @@ -158,7 +158,7 @@ HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery3, testing::ext::TestSize.Le test_callback_ = std::make_shared(); std::shared_ptr mockInstance = std::make_shared(); DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; - EXPECT_CALL(*mockInstance,SendRequest(testing::_,testing::_,testing::_)) + 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); @@ -168,17 +168,18 @@ HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery3, testing::ext::TestSize.Le 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 index b3cd5fd9c..9efa3619b 100644 --- a/test/unittest/device_manager_impl_test.h +++ b/test/unittest/device_manager_impl_test.h @@ -36,7 +36,7 @@ private: std::shared_ptr test_callback_ = nullptr; }; -class DeviceDiscoverCallback : public DiscoverCallback{ +class DeviceDiscoverCallback : public DiscoverCallback { public: DeviceDiscoverCallback() : DiscoverCallback() {} virtual ~DeviceDiscoverCallback() override {} diff --git a/test/unittest/mock/mock_ipc_client_proxy.h b/test/unittest/mock/mock_ipc_client_proxy.h index 49b1b06f7..ece83e9fb 100644 --- a/test/unittest/mock/mock_ipc_client_proxy.h +++ b/test/unittest/mock/mock_ipc_client_proxy.h @@ -23,7 +23,7 @@ namespace OHOS { namespace DistributedHardware { -class MockIpcClient : public IpcClientProxy, public RefBase{ +class MockIpcClientProxy : public IpcClientProxy, public RefBase { public: MOCK_METHOD3(SendRequest, int32_t(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp)); }; -- Gitee From d5ccbd94e2bea81698961336d04714b3ae2b8779 Mon Sep 17 00:00:00 2001 From: yuqianyuan Date: Thu, 16 Sep 2021 17:22:59 +0800 Subject: [PATCH 09/11] bug fix Signed-off-by: yuqianyuan --- test/unittest/device_manager_impl_test.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/unittest/device_manager_impl_test.cpp b/test/unittest/device_manager_impl_test.cpp index ddd504ba2..abde98f33 100644 --- a/test/unittest/device_manager_impl_test.cpp +++ b/test/unittest/device_manager_impl_test.cpp @@ -66,7 +66,7 @@ HWTEST_F(DeviceManagerImplTest, AuthenticateDevice2, testing::ext::TestSize.Leve std::shared_ptr callback = nullptr; std::shared_ptr mockInstance = std::make_shared(); DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; - EXPECT_CALL(*mockInstance, SendRequest(testing::_,testing::_,testing::_)) + 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); @@ -83,7 +83,7 @@ HWTEST_F(DeviceManagerImplTest, AuthenticateDevice3, testing::ext::TestSize.Leve std::shared_ptr callback = nullptr; std::shared_ptr mockInstance = std::make_shared(); DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; - EXPECT_CALL(*mockInstance, SendRequest(testing::_,testing::_,testing::_)) + 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); @@ -107,7 +107,7 @@ HWTEST_F(DeviceManagerImplTest, CheckAuthentication2, testing::ext::TestSize.Lev std::shared_ptr callback = nullptr; std::shared_ptr mockInstance = std::make_shared(); DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; - EXPECT_CALL(*mockInstance, SendRequest(testing::_,testing::_,testing::_)) + 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); @@ -121,7 +121,7 @@ HWTEST_F(DeviceManagerImplTest, CheckAuthentication3, testing::ext::TestSize.Lev std::shared_ptr callback = nullptr; std::shared_ptr mockInstance = std::make_shared(); DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; - EXPECT_CALL(*mockInstance, SendRequest(testing::_,testing::_,testing::_)) + 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); @@ -144,7 +144,7 @@ HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery2, testing::ext::TestSize.Le test_callback_ = std::make_shared(); std::shared_ptr mockInstance = std::make_shared(); DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; - EXPECT_CALL(*mockInstance, SendRequest(testing::_,testing::_,testing::_)) + 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); @@ -158,7 +158,7 @@ HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery3, testing::ext::TestSize.Le test_callback_ = std::make_shared(); std::shared_ptr mockInstance = std::make_shared(); DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; - EXPECT_CALL(*mockInstance, SendRequest(testing::_,testing::_,testing::_)) + 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); -- Gitee From 161dc34a84c828480ddb64a2197c501d4914eb00 Mon Sep 17 00:00:00 2001 From: yuqianyuan Date: Thu, 16 Sep 2021 17:29:37 +0800 Subject: [PATCH 10/11] bug fix Signed-off-by: yuqianyuan --- test/unittest/device_manager_impl_test.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/unittest/device_manager_impl_test.cpp b/test/unittest/device_manager_impl_test.cpp index abde98f33..a6070729f 100644 --- a/test/unittest/device_manager_impl_test.cpp +++ b/test/unittest/device_manager_impl_test.cpp @@ -64,7 +64,7 @@ HWTEST_F(DeviceManagerImplTest, AuthenticateDevice2, testing::ext::TestSize.Leve DmAppImageInfo dmAppImageInfo; std::string extra = ""; std::shared_ptr callback = nullptr; - std::shared_ptr mockInstance = 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)); @@ -81,7 +81,7 @@ HWTEST_F(DeviceManagerImplTest, AuthenticateDevice3, testing::ext::TestSize.Leve DmAppImageInfo dmAppImageInfo; std::string extra = ""; std::shared_ptr callback = nullptr; - std::shared_ptr mockInstance = 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)); @@ -105,7 +105,7 @@ HWTEST_F(DeviceManagerImplTest, CheckAuthentication2, testing::ext::TestSize.Lev std::string packName = "com.ohos.helloworld"; std::string authPara = ""; std::shared_ptr callback = nullptr; - std::shared_ptr mockInstance = 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)); @@ -119,7 +119,7 @@ HWTEST_F(DeviceManagerImplTest, CheckAuthentication3, testing::ext::TestSize.Lev std::string packName = "com.ohos.helloworld"; std::string authPara = ""; std::shared_ptr callback = nullptr; - std::shared_ptr mockInstance = 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)); @@ -142,7 +142,7 @@ HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery2, testing::ext::TestSize.Le std::string packName = "com.ohos.helloworld"; DmSubscribeInfo subscribeInfo; test_callback_ = std::make_shared(); - std::shared_ptr mockInstance = 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)); @@ -156,7 +156,7 @@ HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery3, testing::ext::TestSize.Le std::string packName = "com.ohos.helloworld"; DmSubscribeInfo subscribeInfo; test_callback_ = std::make_shared(); - std::shared_ptr mockInstance = 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)); -- Gitee From bf253fcd123f48278478b65324038f82852d2720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E4=B8=8D=E5=87=BA=E5=88=AB=E5=90=8D?= Date: Sat, 18 Sep 2021 17:12:50 +0800 Subject: [PATCH 11/11] Codex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 想不出别名 --- .../native_cpp/include/dm_app_image_info.h | 8 ++- .../kits/js/src/native_devicemanager_js.cpp | 56 +++++++++---------- .../src/auth/hichain_connector.cpp | 3 +- .../src/ipc/ipc_server_adapter.cpp | 30 +++++----- .../src/ipc/standard/ipc_cmd_parser.cpp | 5 +- .../src/requestauth/request_session.cpp | 2 + .../src/requestauth/response_session.cpp | 7 +++ .../src/softbus/softbus_adapter.cpp | 5 +- .../src/softbus/softbus_session.cpp | 4 +- .../src/timer/dm_timer.cpp | 5 +- 10 files changed, 75 insertions(+), 50 deletions(-) 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 d088ce58a..d98d9b791 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 @@ -144,7 +144,9 @@ private: } if (appIcon != nullptr) { appIconLen = appIconLen_; - (void)memcpy_s(appIcon, appIconLen, appIcon_, appIconLen_); + if (memcpy_s(appIcon, appIconLen, appIcon_, appIconLen_) != 0) { + return; + } } } } @@ -162,7 +164,9 @@ private: } if (appThumbnail != nullptr) { appThumbnailLen = appThumbnailLen_; - (void)memcpy_s(appThumbnail, appThumbnailLen, appThumbnail_, appThumbnailLen_); + if (memcpy_s(appThumbnail, appThumbnailLen, appThumbnail_, appThumbnailLen_) != 0) { + return; + } } } } diff --git a/interfaces/kits/js/src/native_devicemanager_js.cpp b/interfaces/kits/js/src/native_devicemanager_js.cpp index a555fc582..67bd749b0 100644 --- a/interfaces/kits/js/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js/src/native_devicemanager_js.cpp @@ -216,11 +216,11 @@ DeviceManagerNapi *DeviceManagerNapi::GetDeviceManagerNapi(std::string &buldleNa void DeviceManagerNapi::OnDeviceStateChange(DmNapiDevStateChangeAction action, const DmDeviceInfo &deviceInfo) { - napi_value result; + napi_value result = nullptr; napi_create_object(env_, &result); SetValueInt32(env_, "action", (int)action, result); - napi_value device; + napi_value device = nullptr; napi_create_object(env_, &device); SetValueUtf8String(env_, "deviceId", deviceInfo.deviceId, device); SetValueUtf8String(env_, "deviceName", deviceInfo.deviceName, device); @@ -233,11 +233,11 @@ void DeviceManagerNapi::OnDeviceStateChange(DmNapiDevStateChangeAction action, c void DeviceManagerNapi::OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo) { DMLOG(DM_LOG_INFO, "OnDeviceFound for subscribeId %d", (int32_t)subscribeId); - napi_value result; + napi_value result = nullptr; napi_create_object(env_, &result); SetValueInt32(env_, "subscribeId", (int)subscribeId, result); - napi_value device; + napi_value device = nullptr; napi_create_object(env_, &device); SetValueUtf8String(env_, "deviceId", deviceInfo.deviceId, device); SetValueUtf8String(env_, "deviceName", deviceInfo.deviceName, device); @@ -250,7 +250,7 @@ void DeviceManagerNapi::OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo & void DeviceManagerNapi::OnDiscoverFailed(uint16_t subscribeId, int32_t failedReason) { DMLOG(DM_LOG_INFO, "OnDiscoverFailed for subscribeId %d", (int32_t)subscribeId); - napi_value result; + napi_value result = nullptr; napi_create_object(env_, &result); SetValueInt32(env_, "subscribeId", (int)subscribeId, result); SetValueInt32(env_, "reason", (int)failedReason, result); @@ -260,7 +260,7 @@ void DeviceManagerNapi::OnDiscoverFailed(uint16_t subscribeId, int32_t failedRea void DeviceManagerNapi::OnDmfaCall(const std::string ¶mJson) { DMLOG(DM_LOG_INFO, "OnCall for paramJson"); - napi_value result; + napi_value result = nullptr; napi_create_object(env_, &result); SetValueUtf8String(env_, "param", paramJson, result); OnEvent("dmFaCallback", DM_NAPI_ARGS_ONE, &result); @@ -333,7 +333,7 @@ void DeviceManagerNapi::OnVerifyResult(const std::string &deviceId, int32_t resu void DeviceManagerNapi::SetValueUtf8String(const napi_env &env, const std::string &fieldStr, const std::string &str, napi_value &result) { - napi_value value; + napi_value value = nullptr; napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &value); napi_set_named_property(env, result, fieldStr.c_str(), value); } @@ -341,7 +341,7 @@ void DeviceManagerNapi::SetValueUtf8String(const napi_env &env, const std::strin void DeviceManagerNapi::SetValueInt32(const napi_env &env, const std::string &fieldStr, const int32_t intValue, napi_value &result) { - napi_value value; + napi_value value = nullptr; napi_create_int32(env, intValue, &value); napi_set_named_property(env, result, fieldStr.c_str(), value); } @@ -350,7 +350,7 @@ void DeviceManagerNapi::DeviceInfoToJsArray(const napi_env &env, const std::vector &vecDevInfo, const int32_t idx, napi_value &arrayResult) { - napi_value result; + napi_value result = nullptr; napi_create_object(env, &result); SetValueUtf8String(env, "deviceId", vecDevInfo[idx].deviceId, result); @@ -369,7 +369,7 @@ void DeviceManagerNapi::DmAuthParamToJsAuthParamy(const napi_env &env, DMLOG(DM_LOG_INFO, "DmAuthParamToJsAuthParamy in"); SetValueInt32(env, "authType", authParam.authType, paramResult); - napi_value extraInfo; + napi_value extraInfo = nullptr; napi_create_object(env, &extraInfo); SetValueInt32(env, "direction", authParam.direction, extraInfo); SetValueInt32(env, "pinToken", authParam.pinToken, extraInfo); @@ -420,8 +420,8 @@ void DeviceManagerNapi::JsObjectToString(const napi_env &env, const napi_value & bool hasProperty = false; NAPI_CALL_RETURN_VOID(env, napi_has_named_property(env, object, fieldStr.c_str(), &hasProperty)); if (hasProperty) { - napi_value field; - napi_valuetype valueType; + napi_value field = nullptr; + napi_valuetype valueType = napi_undefined; napi_get_named_property(env, object, fieldStr.c_str(), &field); NAPI_CALL_RETURN_VOID(env, napi_typeof(env, field, &valueType)); @@ -467,8 +467,8 @@ void DeviceManagerNapi::JsObjectToInt(const napi_env &env, const napi_value &obj bool hasProperty = false; NAPI_CALL_RETURN_VOID(env, napi_has_named_property(env, object, fieldStr.c_str(), &hasProperty)); if (hasProperty) { - napi_value field; - napi_valuetype valueType; + napi_value field = nullptr; + napi_valuetype valueType = napi_undefined; napi_get_named_property(env, object, fieldStr.c_str(), &field); NAPI_CALL_RETURN_VOID(env, napi_typeof(env, field, &valueType)); @@ -485,8 +485,8 @@ void DeviceManagerNapi::JsObjectToBool(const napi_env &env, const napi_value &ob bool hasProperty = false; NAPI_CALL_RETURN_VOID(env, napi_has_named_property(env, object, fieldStr.c_str(), &hasProperty)); if (hasProperty) { - napi_value field; - napi_valuetype valueType; + napi_value field = nullptr; + napi_valuetype valueType = napi_undefined; napi_get_named_property(env, object, fieldStr.c_str(), &field); NAPI_CALL_RETURN_VOID(env, napi_typeof(env, field, &valueType)); @@ -588,9 +588,9 @@ void DeviceManagerNapi::JsToDmBuffer(const napi_env &env, const napi_value &obje return; } - napi_value field; + napi_value field = nullptr; napi_get_named_property(env, object, fieldStr.c_str(), &field); - napi_typedarray_type type; + napi_typedarray_type type = napi_uint8_array; size_t length = 0; napi_value buffer = nullptr; size_t offset = 0; @@ -626,7 +626,7 @@ void DeviceManagerNapi::JsToJsonObject(const napi_env &env, const napi_value &ob return; } - napi_value jsonField; + napi_value jsonField = nullptr; napi_get_named_property(env, object, fieldStr.c_str(), &jsonField); napi_valuetype jsValueType = napi_undefined; napi_value jsProNameList = nullptr; @@ -794,7 +794,7 @@ napi_value DeviceManagerNapi::SetUserOperationSync(napi_env env, napi_callback_i { DMLOG(DM_LOG_INFO, "SetUserOperationSync in"); GET_PARAMS(env, info, DM_NAPI_ARGS_ONE); - napi_valuetype valueType; + napi_valuetype valueType = napi_undefined; napi_typeof(env, argv[0], &valueType); NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Object expected."); @@ -883,7 +883,7 @@ napi_value DeviceManagerNapi::StartDeviceDiscoverSync(napi_env env, napi_callbac DMLOG(DM_LOG_INFO, "StartDeviceDiscoverSync in"); GET_PARAMS(env, info, DM_NAPI_ARGS_ONE); napi_value result = nullptr; - napi_valuetype valueType; + napi_valuetype valueType = napi_undefined; napi_typeof(env, argv[0], &valueType); NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Object expected."); @@ -919,7 +919,7 @@ napi_value DeviceManagerNapi::StopDeviceDiscoverSync(napi_env env, napi_callback DMLOG(DM_LOG_INFO, "StopDeviceDiscoverSync in"); GET_PARAMS(env, info, DM_NAPI_ARGS_ONE); napi_value result = nullptr; - napi_valuetype valueType; + napi_valuetype valueType = napi_undefined; napi_typeof(env, argv[0], &valueType); NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Object expected."); @@ -948,11 +948,11 @@ napi_value DeviceManagerNapi::AuthenticateDevice(napi_env env, napi_callback_inf DMLOG(DM_LOG_INFO, "AuthenticateDevice in"); GET_PARAMS(env, info, DM_NAPI_ARGS_THREE); napi_value result = nullptr; - napi_valuetype deviceInfoType; + napi_valuetype deviceInfoType = napi_undefined; napi_typeof(env, argv[0], &deviceInfoType); NAPI_ASSERT(env, deviceInfoType == napi_object, "Wrong argument type. Object expected."); - napi_valuetype authparamType; + napi_valuetype authparamType = napi_undefined; napi_typeof(env, argv[PARAM_INDEX_ONE], &authparamType); NAPI_ASSERT(env, authparamType == napi_object, "Wrong argument type. Object expected."); @@ -997,7 +997,7 @@ napi_value DeviceManagerNapi::VerifyAuthInfo(napi_env env, napi_callback_info in DMLOG(DM_LOG_INFO, "VerifyAuthInfo in"); GET_PARAMS(env, info, DM_NAPI_ARGS_TWO); napi_value result = nullptr; - napi_valuetype valueType; + napi_valuetype valueType = napi_undefined; napi_typeof(env, argv[0], &valueType); NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Object expected."); @@ -1161,8 +1161,8 @@ void DeviceManagerNapi::HandleCreateDmCallBack(const napi_env &env, AsyncCallbac (void)status; AsyncCallbackInfo *asCallbackInfo = (AsyncCallbackInfo *)data; napi_value result[DM_NAPI_ARGS_TWO] = { 0 }; - napi_value ctor; - napi_value argv; + napi_value ctor = nullptr; + napi_value argv = nullptr; napi_get_reference_value(env, sConstructor_, &ctor); napi_create_string_utf8(env, asCallbackInfo->bundleName, NAPI_AUTO_LENGTH, &argv); napi_status ret = napi_new_instance(env, ctor, DM_NAPI_ARGS_ONE, &argv, &result[1]); @@ -1250,7 +1250,7 @@ napi_value DeviceManagerNapi::Constructor(napi_env env, napi_callback_info info) napi_value DeviceManagerNapi::Init(napi_env env, napi_value exports) { - napi_value dmClass; + napi_value dmClass = nullptr; napi_property_descriptor dmProperties[] = { DECLARE_NAPI_FUNCTION("release", ReleaseDeviceManager), DECLARE_NAPI_FUNCTION("getTrustedDeviceListSync", GetTrustedDeviceListSync), diff --git a/services/devicemanagerservice/src/auth/hichain_connector.cpp b/services/devicemanagerservice/src/auth/hichain_connector.cpp index a023019fa..06c3716fe 100644 --- a/services/devicemanagerservice/src/auth/hichain_connector.cpp +++ b/services/devicemanagerservice/src/auth/hichain_connector.cpp @@ -359,8 +359,7 @@ void HichainAuthenCallBack::onFinish(int64_t requestId, int32_t operationCode, c if (operationCode == GroupOperationCode::GROUP_CREATE) { DMLOG(DM_LOG_INFO, "Create group success"); - std::string returnStr = returnData; - HichainConnector::GetInstance().OnGroupCreated(requestId, returnStr); + HichainConnector::GetInstance().OnGroupCreated(requestId, data); } if (operationCode == GroupOperationCode::MEMBER_DELETE) { diff --git a/services/devicemanagerservice/src/ipc/ipc_server_adapter.cpp b/services/devicemanagerservice/src/ipc/ipc_server_adapter.cpp index 240306ce5..3aef68c8e 100644 --- a/services/devicemanagerservice/src/ipc/ipc_server_adapter.cpp +++ b/services/devicemanagerservice/src/ipc/ipc_server_adapter.cpp @@ -90,21 +90,25 @@ int32_t IpcServerAdapter::GetTrustedDeviceList(std::string &pkgName, std::string *info = nullptr; if (*infoNum > 0) { *info = (DmDeviceInfo *)malloc(sizeof(DmDeviceInfo) * (*infoNum)); - } - for (int32_t i = 0; i < *infoNum; ++i) { - NodeBasicInfo *nodeBasicInfo = nodeInfo + i; - DmDeviceInfo *deviceInfo = *info + i; - if (memcpy_s(deviceInfo->deviceId, sizeof(deviceInfo->deviceId), nodeBasicInfo->networkId, - std::min(sizeof(deviceInfo->deviceId), sizeof(nodeBasicInfo->networkId))) != DEVICEMANAGER_OK) { - DMLOG(DM_LOG_ERROR, "memcpy failed"); - } - if (memcpy_s(deviceInfo->deviceName, sizeof(deviceInfo->deviceName), nodeBasicInfo->deviceName, - std::min(sizeof(deviceInfo->deviceName), sizeof(nodeBasicInfo->deviceName))) != DEVICEMANAGER_OK) { - DMLOG(DM_LOG_ERROR, "memcpy failed"); + if (*info != nullptr) { + for (int32_t i = 0; i < *infoNum; ++i) { + NodeBasicInfo *nodeBasicInfo = nodeInfo + i; + DmDeviceInfo *deviceInfo = *info + i; + if (memcpy_s(deviceInfo->deviceId, sizeof(deviceInfo->deviceId), nodeBasicInfo->networkId, + std::min(sizeof(deviceInfo->deviceId), sizeof(nodeBasicInfo->networkId))) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "memcpy failed"); + } + if (memcpy_s(deviceInfo->deviceName, sizeof(deviceInfo->deviceName), nodeBasicInfo->deviceName, + std::min(sizeof(deviceInfo->deviceName), sizeof(nodeBasicInfo->deviceName))) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "memcpy failed"); + } + deviceInfo->deviceTypeId = (DMDeviceType)nodeBasicInfo->deviceTypeId; + } } - deviceInfo->deviceTypeId = (DMDeviceType)nodeBasicInfo->deviceTypeId; } - FreeNodeInfo(nodeInfo); + if (nodeInfo != nullptr) { + FreeNodeInfo(nodeInfo); + } DMLOG(DM_LOG_INFO, "success, pkgName:%s, deviceCount %d", pkgName.c_str(), *infoNum); return DEVICEMANAGER_OK; } diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp index c6bc39322..1f58b838d 100644 --- a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp @@ -266,8 +266,11 @@ ON_IPC_CMD(AUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply) 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); + int32_t result = DEVICEMANAGER_OK; + if (deviceInfo != nullptr) { + result = IpcServerAdapter::GetInstance().AuthenticateDevice(pkgName, *deviceInfo, imageInfo, extra); + } if (!reply.WriteInt32(result)) { DMLOG(DM_LOG_ERROR, "write result failed"); return DEVICEMANAGER_WRITE_FAILED; diff --git a/services/devicemanagerservice/src/requestauth/request_session.cpp b/services/devicemanagerservice/src/requestauth/request_session.cpp index bd8ab35d7..143b5cb1b 100644 --- a/services/devicemanagerservice/src/requestauth/request_session.cpp +++ b/services/devicemanagerservice/src/requestauth/request_session.cpp @@ -61,6 +61,8 @@ RequestSession::RequestSession(std::string &hostPkgName, const DmDeviceInfo &dev mTargetPkgName = targetPkgName; mStatus_ = StatusType::STATUS_INIT; mPinToken_ = IpcServerAdapter::GenRandInt(MIN_PIN_TOKEN, MAX_PIN_TOKEN); + mChannelId_ = -1; + mRequestId_ = -1; char randStr[TOKEN_LEN] = {0}; bool res = EncryptUtils::MbedtlsGenRandomStr(randStr, sizeof(randStr), false); if (res == false) { diff --git a/services/devicemanagerservice/src/requestauth/response_session.cpp b/services/devicemanagerservice/src/requestauth/response_session.cpp index 9405548f6..54681ec86 100644 --- a/services/devicemanagerservice/src/requestauth/response_session.cpp +++ b/services/devicemanagerservice/src/requestauth/response_session.cpp @@ -48,6 +48,13 @@ ResponseSession::ResponseSession() { mSessionStatus_ = ResponseSessionStatus::SESSION_INIT; sem_init(&mSem_, 0, 0); + mRequestId_ = -1; + mGroupId_ = ""; + mGroupName_ = ""; + mReqDeviceId_ = ""; + mMsgRequestAuthPtr_ = nullptr; + mChannelId_ = -1; + mPincode_ = -1; } int64_t ResponseSession::GetRequestId() diff --git a/services/devicemanagerservice/src/softbus/softbus_adapter.cpp b/services/devicemanagerservice/src/softbus/softbus_adapter.cpp index de85815c8..dd3959eac 100644 --- a/services/devicemanagerservice/src/softbus/softbus_adapter.cpp +++ b/services/devicemanagerservice/src/softbus/softbus_adapter.cpp @@ -304,7 +304,10 @@ int32_t SoftbusAdapter::StartDiscovery(std::string &pkgName, SubscribeInfo *info subinfo->subscribeIdOrigin = info->subscribeId; subinfo->subscribeIdPrefix = subscribeIdPrefix++; subinfo->info = *info; - subinfo->info.subscribeId = (subinfo->subscribeIdPrefix << SUBSCRIBE_ID_PREFIX_LEN) | info->subscribeId; + + uint32_t uSubscribeId = static_cast(info->subscribeId); + uSubscribeId = (subinfo->subscribeIdPrefix << SUBSCRIBE_ID_PREFIX_LEN) | uSubscribeId; + subinfo->info.subscribeId = static_cast(uSubscribeId); } if (vectorIter == subinfoVector.end()) { subinfoVector.push_back(subinfo); diff --git a/services/devicemanagerservice/src/softbus/softbus_session.cpp b/services/devicemanagerservice/src/softbus/softbus_session.cpp index 8a74c2e48..84bf739a2 100644 --- a/services/devicemanagerservice/src/softbus/softbus_session.cpp +++ b/services/devicemanagerservice/src/softbus/softbus_session.cpp @@ -217,9 +217,9 @@ void SoftbusSession::GetPeerDeviceId(int32_t sessionId, std::string &peerDevId) char peerDeviceId[DEVICE_UUID_LENGTH] = {0}; int32_t ret = ::GetPeerDeviceId(sessionId, &peerDeviceId[0], DEVICE_UUID_LENGTH); if (ret == 0) { - DMLOG(DM_LOG_INFO, "GetPeerDeviceId success for session:%d, peerDeviceId:%s", sessionId, - GetAnonyString(peerDeviceId).c_str()); peerDevId = peerDeviceId; + DMLOG(DM_LOG_INFO, "GetPeerDeviceId success for session:%d, peerDeviceId:%s", sessionId, + GetAnonyString(peerDevId).c_str()); return; } diff --git a/services/devicemanagerservice/src/timer/dm_timer.cpp b/services/devicemanagerservice/src/timer/dm_timer.cpp index 8dcd7a703..09c7cfecd 100644 --- a/services/devicemanagerservice/src/timer/dm_timer.cpp +++ b/services/devicemanagerservice/src/timer/dm_timer.cpp @@ -26,6 +26,7 @@ DmTimer::DmTimer(std::string &name) { mStatus_ = DmTimerStatus::DM_STATUS_INIT; mTimerName_ = name; + mEpFd_ = 0; } DmTimer::~DmTimer() @@ -133,7 +134,9 @@ void DmTimer::Release() mStatus_ = DmTimerStatus::DM_STATUS_INIT; close(mTimeFd_[0]); close(mTimeFd_[1]); - close(mEpFd_); + if (mEpFd_ >= 0) { + close(mEpFd_); + } mTimeFd_[0] = 0; mTimeFd_[1] = 0; mEpFd_ = 0; -- Gitee