From 1f64c9d8700e742ed34b1a66f4b828ae524ab17a Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Fri, 16 Sep 2022 10:30:14 +0000 Subject: [PATCH] modify test case about ut Signed-off-by: wuzhihuitmac Change-Id: Ia68e00565084d36138dea547a16293060793e6ac --- interfaces/native/test/BUILD.gn | 3 + .../test/unittest/sensor_agent_test.cpp | 60 ++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index 251c2d75..a9913aa7 100644 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -39,6 +39,9 @@ ohos_unittest("SensorAgentTest") { "//third_party/googletest:gtest_main", ] external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/interfaces/native/test/unittest/sensor_agent_test.cpp b/interfaces/native/test/unittest/sensor_agent_test.cpp index 4b99e819..d324468e 100755 --- a/interfaces/native/test/unittest/sensor_agent_test.cpp +++ b/interfaces/native/test/unittest/sensor_agent_test.cpp @@ -17,6 +17,10 @@ #include #include +#include "accesstoken_kit.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" + #include "sensor_agent.h" #include "sensors_errors.h" #include "system_info.h" @@ -25,11 +29,46 @@ namespace OHOS { namespace Sensors { using namespace testing::ext; using namespace OHOS::HiviewDFX; +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorAgentTest" }; -constexpr int32_t sensorId { 0 }; +constexpr int32_t sensorId { 1 }; constexpr int32_t invalidValue { -1 }; + +PermissionDef infoManagerTestPermDef_ = { + .permissionName = "ohos.permission.ACCELEROMETER", + .bundleName = "accesstoken_test", + .grantMode = 1, + .label = "label", + .labelId = 1, + .description = "test sensor agent", + .descriptionId = 1, + .availableLevel = APL_NORMAL +}; + +PermissionStateFull infoManagerTestState_ = { + .grantFlags = {1}, + .grantStatus = {PermissionState::PERMISSION_GRANTED}, + .isGeneral = true, + .permissionName = "ohos.permission.ACCELEROMETER", + .resDeviceID = {"local"} +}; + +HapPolicyParams infoManagerTestPolicyPrams_ = { + .apl = APL_NORMAL, + .domain = "test.domain", + .permList = {infoManagerTestPermDef_}, + .permStateList = {infoManagerTestState_} +}; + +HapInfoParams infoManagerTestInfoParms_ = { + .bundleName = "sensoragent_test", + .userID = 1, + .instIndex = 0, + .appIDDesc = "sensorAgentTest" +}; } // namespace class SensorAgentTest : public testing::Test { @@ -38,13 +77,28 @@ public: static void TearDownTestCase(); void SetUp(); void TearDown(); +private: + static AccessTokenID tokenID_; }; +AccessTokenID SensorAgentTest::tokenID_ = 0; + void SensorAgentTest::SetUpTestCase() -{} +{ + AccessTokenIDEx tokenIdEx = {0}; + tokenIdEx = AccessTokenKit::AllocHapToken(infoManagerTestInfoParms_, infoManagerTestPolicyPrams_); + tokenID_ = tokenIdEx.tokenIdExStruct.tokenID; + ASSERT_NE(0, tokenID_); + ASSERT_EQ(0, SetSelfTokenID(tokenID_)); +} void SensorAgentTest::TearDownTestCase() -{} +{ + int32_t ret = AccessTokenKit::DeleteToken(tokenID_); + if (tokenID_ != 0) { + ASSERT_EQ(RET_SUCCESS, ret); + } +} void SensorAgentTest::SetUp() {} -- Gitee