From 17ac8b8ab3bc7e4b02147e7ebc14161ddd0f0185 Mon Sep 17 00:00:00 2001 From: zhuxu Date: Wed, 6 Apr 2022 20:57:08 +0800 Subject: [PATCH] fix distributed camera ut bug Signed-off-by: zhuxu --- bundle.json | 3 ++ .../cameraoperator/client/BUILD.gn | 2 + .../client/include/dcamera_client.h | 52 +++++++++++++++++++ .../client/src/dcamera_client.cpp | 16 ++++++ .../client/src/dcamera_client_common.cpp | 16 ++++++ .../unittest/common/cameraoperator/BUILD.gn | 2 + services/cameraservice/sinkservice/BUILD.gn | 2 + .../dcamera_service_state_listener.cpp | 4 +- .../dcamera_source_dev.cpp | 11 ++++ 9 files changed, 106 insertions(+), 2 deletions(-) diff --git a/bundle.json b/bundle.json index 302bb55..dfcb1b6 100644 --- a/bundle.json +++ b/bundle.json @@ -85,6 +85,9 @@ } ], "test":[ + "//foundation/distributedhardware/distributedcamera/services/cameraservice/cameraoperator/client/test/unittest:camera_client_test", + "//foundation/distributedhardware/distributedcamera/services/cameraservice/cameraoperator/handler/test/unittest:camera_handler_test", + "//foundation/distributedhardware/distributedcamera/services/cameraservice/sinkservice/test/unittest:sink_service_test", "//foundation/distributedhardware/distributedcamera/services/cameraservice/sourceservice/test/unittest:source_service_test", "//foundation/distributedhardware/distributedcamera/services/cameraservice/base/test/unittest:services_base_test" ] diff --git a/services/cameraservice/cameraoperator/client/BUILD.gn b/services/cameraservice/cameraoperator/client/BUILD.gn index ca1797d..a5fc17a 100644 --- a/services/cameraservice/cameraoperator/client/BUILD.gn +++ b/services/cameraservice/cameraoperator/client/BUILD.gn @@ -87,6 +87,8 @@ ohos_shared_library("distributed_camera_client") { ] external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", "hiviewdfx_hilog_native:libhilog", "multimedia_media_standard:media_client", ] diff --git a/services/cameraservice/cameraoperator/client/include/dcamera_client.h b/services/cameraservice/cameraoperator/client/include/dcamera_client.h index f97d2ee..12c76ee 100644 --- a/services/cameraservice/cameraoperator/client/include/dcamera_client.h +++ b/services/cameraservice/cameraoperator/client/include/dcamera_client.h @@ -18,14 +18,19 @@ #include "icamera_operator.h" +#include "access_token.h" +#include "accesstoken_kit.h" #include "camera_info.h" #include "camera_input.h" #include "camera_manager.h" #include "capture_input.h" #include "capture_output.h" #include "capture_session.h" +#include "hap_token_info.h" +#include "ipc_skeleton.h" #include "photo_output.h" #include "preview_output.h" +#include "token_setproc.h" #include "video_output.h" #include "dcamera_photo_surface_listener.h" @@ -33,6 +38,52 @@ namespace OHOS { namespace DistributedHardware { +const std::string OHOS_PERMISSION_CAMERA = "ohos.permission.CAMERA"; +const std::string APP_ID_DESC = "distributed camera"; +const std::string LABEL = "label"; +const std::string DESCRIPTION = "distributed camera"; +const std::string RES_DEVICE_ID = "local"; +const std::string DOMAIN = "distribtued_camera.domain"; +const int32_t USER_ID = 1; +const int32_t INST_INDEX = 0; +const int32_t GRANT_MODE = 1; +const int32_t GRANT_FLAGS = 1; +const int32_t LABEL_ID = 1; +const int32_t DESCRIPTION_ID = 1; + +const Security::AccessToken::HapInfoParams HAP_INFO_PARAMS = { + .userID = USER_ID, + .bundleName = OHOS_PERMISSION_CAMERA, + .instIndex = INST_INDEX, + .appIDDesc = APP_ID_DESC +}; + +const Security::AccessToken::PermissionDef PERMISSION_DEF = { + .permissionName = OHOS_PERMISSION_CAMERA, + .bundleName = OHOS_PERMISSION_CAMERA, + .grantMode = GRANT_MODE, + .availableLevel = Security::AccessToken::ATokenAplEnum::APL_NORMAL, + .label = LABEL, + .labelId = LABEL_ID, + .description = DESCRIPTION, + .descriptionId = DESCRIPTION_ID +}; + +const Security::AccessToken::PermissionStateFull PERMISSION_STATE_FULL = { + .permissionName = OHOS_PERMISSION_CAMERA, + .isGeneral = true, + .resDeviceID = {RES_DEVICE_ID}, + .grantStatus = {Security::AccessToken::PermissionState::PERMISSION_GRANTED}, + .grantFlags = {GRANT_FLAGS} +}; + +const Security::AccessToken::HapPolicyParams HAP_POLICY_PARAMS = { + .apl = Security::AccessToken::ATokenAplEnum::APL_NORMAL, + .domain = DOMAIN, + .permList = {PERMISSION_DEF}, + .permStateList = {PERMISSION_STATE_FULL} +}; + class DCameraClient : public ICameraOperator { public: explicit DCameraClient(const std::string& dhId); @@ -73,6 +124,7 @@ private: std::shared_ptr resultCallback_; std::shared_ptr photoListener_; std::shared_ptr videoListener_; + Security::AccessToken::AccessTokenIDEx tokenIdEx_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp b/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp index 5f50dc9..f3b901c 100644 --- a/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp +++ b/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp @@ -36,6 +36,21 @@ DCameraClient::DCameraClient(const std::string& dhId) DHLOGI("DCameraClient Constructor dhId: %s", GetAnonyString(dhId).c_str()); cameraId_ = dhId.substr(CAMERA_ID_PREFIX.size()); isInit_ = false; + + tokenIdEx_ = Security::AccessToken::AccessTokenKit::AllocHapToken(HAP_INFO_PARAMS, HAP_POLICY_PARAMS); + if (tokenIdEx_.tokenIdExStruct.tokenID == 0) { + DHLOGE("DCameraClient Constructor alloc tokenId failed, dhId: %s", GetAnonyString(dhId).c_str()); + return; + } + + (void)SetSelfTokenID(tokenIdEx_.tokenIdExStruct.tokenID); + + int32_t ret = Security::AccessToken::AccessTokenKit::GrantPermission(tokenIdEx_.tokenIdExStruct.tokenID, + "ohos.permission.CAMERA", Security::AccessToken::PERMISSION_USER_FIXED); + if (ret != 0) { + DHLOGE("DCameraClient Constructor grant permission failed, dhId: %s", GetAnonyString(dhId).c_str()); + return; + } } DCameraClient::~DCameraClient() @@ -43,6 +58,7 @@ DCameraClient::~DCameraClient() if (isInit_) { UnInit(); } + (void)Security::AccessToken::AccessTokenKit::DeleteToken(tokenIdEx_.tokenIdExStruct.tokenID); } int32_t DCameraClient::Init() diff --git a/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp b/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp index 2a026d4..452cae5 100644 --- a/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp +++ b/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp @@ -35,6 +35,21 @@ DCameraClient::DCameraClient(const std::string& dhId) DHLOGI("DCameraClientCommon Constructor dhId: %s", GetAnonyString(dhId).c_str()); cameraId_ = dhId.substr(CAMERA_ID_PREFIX.size()); isInit_ = false; + + tokenIdEx_ = Security::AccessToken::AccessTokenKit::AllocHapToken(HAP_INFO_PARAMS, HAP_POLICY_PARAMS); + if (tokenIdEx_.tokenIdExStruct.tokenID == 0) { + DHLOGE("DCameraClientCommon Constructor alloc tokenId failed, dhId: %s", GetAnonyString(dhId).c_str()); + return; + } + + (void)SetSelfTokenID(tokenIdEx_.tokenIdExStruct.tokenID); + + int32_t ret = Security::AccessToken::AccessTokenKit::GrantPermission(tokenIdEx_.tokenIdExStruct.tokenID, + "ohos.permission.CAMERA", Security::AccessToken::PERMISSION_USER_FIXED); + if (ret != 0) { + DHLOGE("DCameraClientCommon Constructor grant permission failed, dhId: %s", GetAnonyString(dhId).c_str()); + return; + } } DCameraClient::~DCameraClient() @@ -42,6 +57,7 @@ DCameraClient::~DCameraClient() if (isInit_) { UnInit(); } + (void)Security::AccessToken::AccessTokenKit::DeleteToken(tokenIdEx_.tokenIdExStruct.tokenID); } int32_t DCameraClient::Init() diff --git a/services/cameraservice/cameraoperator/client/test/unittest/common/cameraoperator/BUILD.gn b/services/cameraservice/cameraoperator/client/test/unittest/common/cameraoperator/BUILD.gn index 4608245..d1bab1e 100644 --- a/services/cameraservice/cameraoperator/client/test/unittest/common/cameraoperator/BUILD.gn +++ b/services/cameraservice/cameraoperator/client/test/unittest/common/cameraoperator/BUILD.gn @@ -71,6 +71,8 @@ ohos_unittest("DCameraClientTest") { ] external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", "hiviewdfx_hilog_native:libhilog", "eventhandler:libeventhandler", "ipc:ipc_core", diff --git a/services/cameraservice/sinkservice/BUILD.gn b/services/cameraservice/sinkservice/BUILD.gn index 2ce72cc..2765ebe 100644 --- a/services/cameraservice/sinkservice/BUILD.gn +++ b/services/cameraservice/sinkservice/BUILD.gn @@ -117,6 +117,8 @@ ohos_shared_library("distributed_camera_sink") { ] external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", "eventhandler:libeventhandler", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp b/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp index d62fdcf..18eb547 100644 --- a/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp +++ b/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp @@ -52,7 +52,7 @@ int32_t DCameraServiceStateListener::OnRegisterNotify(const std::string& devId, DHLOGE("DCameraServiceStateListener OnRegisterNotify OnNotifyRegResult failed: %d", ret); } if (status != DCAMERA_OK) { - std::thread([this, devId, dhId]() { + std::thread([devId, dhId]() { DHLOGI("DCameraServiceStateListener OnRegisterNotify thread delete devId: %s dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); DCameraIndex camIndex(devId, dhId); @@ -78,7 +78,7 @@ int32_t DCameraServiceStateListener::OnUnregisterNotify(const std::string& devId } if (status == DCAMERA_OK) { - std::thread([this, devId, dhId]() { + std::thread([devId, dhId]() { DHLOGI("DCameraServiceStateListener OnUnregisterNotify thread delete devId: %s dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); DCameraIndex camIndex(devId, dhId); diff --git a/services/cameraservice/sourceservice/src/distributedcameramgr/dcamera_source_dev.cpp b/services/cameraservice/sourceservice/src/distributedcameramgr/dcamera_source_dev.cpp index ff476e1..80dd255 100644 --- a/services/cameraservice/sourceservice/src/distributedcameramgr/dcamera_source_dev.cpp +++ b/services/cameraservice/sourceservice/src/distributedcameramgr/dcamera_source_dev.cpp @@ -57,6 +57,17 @@ DCameraSourceDev::~DCameraSourceDev() { DHLOGI("DCameraSourceDev Delete devId %s dhId %s", GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str()); + eventBus_ = nullptr; + hdiCallback_ = nullptr; + input_ = nullptr; + controller_ = nullptr; + stateMachine_ = nullptr; + stateListener_ = nullptr; + + eventResultMap_.clear(); + memberFuncMap_.clear(); + DHLOGI("DCameraSourceDev Delete devId %s dhId %s end", GetAnonyString(devId_).c_str(), + GetAnonyString(dhId_).c_str()); } int32_t DCameraSourceDev::InitDCameraSourceDev()