为cert_manager_auth_mgr.c添加锁

Signed-off-by: lizan10 <lizan10@huawei.com>
This commit is contained in:
lizan10 2023-04-10 19:27:05 -07:00
parent 5f8a82ec1b
commit 0c9076dd5e
9 changed files with 1299 additions and 0 deletions

View File

@ -15,6 +15,7 @@ group("cert_manager_sdk_test") {
testonly = true
if (os_level == "standard") {
deps = [
"./test:multi_thread_test",
"//base/security/certificate_manager/test:module_test",
"//base/security/certificate_manager/test:permission_test",
"//base/security/certificate_manager/test:unittest",

View File

@ -69,6 +69,7 @@
"test": [
"//base/security/certificate_manager/test:unittest",
"//base/security/certificate_manager/test:module_test",
"//base/security/certificate_manager/test:multi_thread_test",
"//base/security/certificate_manager/test:permission_test",
"//base/security/certificate_manager/test/fuzz_test:fuzztest"
]

View File

@ -15,6 +15,8 @@
#include "cert_manager_auth_mgr.h"
#include <pthread.h>
#include "securec.h"
#include "cert_manager_auth_list_mgr.h"
@ -31,6 +33,8 @@
#define OUT_OF_HEX 16
#define DEC 10
static pthread_mutex_t g_authMgrLock = PTHREAD_MUTEX_INITIALIZER;
static char HexToChar(uint8_t hex)
{
return (hex > NUMBER_9_IN_DECIMAL) ? (hex + 0x37) : (hex + 0x30); /* Convert to the corresponding character */
@ -322,9 +326,11 @@ static int32_t GenerateAuthUri(const struct CMUri *uriObj, uint32_t clientUid, s
int32_t CmAuthGrantAppCertificate(const struct CmContext *context, const struct CmBlob *keyUri,
uint32_t appUid, struct CmBlob *authUri)
{
pthread_mutex_lock(&g_authMgrLock);
int32_t ret = CmCheckCredentialExist(context, keyUri);
if (ret != CM_SUCCESS) {
CM_LOG_E("credential not exist when grant auth, ret = %d", ret);
pthread_mutex_unlock(&g_authMgrLock);
return ret;
}
@ -333,6 +339,7 @@ int32_t CmAuthGrantAppCertificate(const struct CmContext *context, const struct
ret = GetAndCheckUriObj(&uriObj, keyUri);
if (ret != CM_SUCCESS) {
CM_LOG_E("uri decode failed, ret = %d", ret);
pthread_mutex_unlock(&g_authMgrLock);
return ret;
}
@ -358,6 +365,7 @@ int32_t CmAuthGrantAppCertificate(const struct CmContext *context, const struct
}
} while (0);
pthread_mutex_unlock(&g_authMgrLock);
if (ret != CM_SUCCESS) {
(void)CmAuthRemoveGrantedApp(context, keyUri, appUid); /* clear auth info */
}
@ -368,11 +376,13 @@ int32_t CmAuthGrantAppCertificate(const struct CmContext *context, const struct
int32_t CmAuthGetAuthorizedAppList(const struct CmContext *context, const struct CmBlob *keyUri,
struct CmAppUidList *appUidList)
{
pthread_mutex_lock(&g_authMgrLock);
struct CMUri uriObj;
(void)memset_s(&uriObj, sizeof(uriObj), 0, sizeof(uriObj));
int32_t ret = GetAndCheckUriObj(&uriObj, keyUri);
if (ret != CM_SUCCESS) {
CM_LOG_E("uri decode failed, ret = %d", ret);
pthread_mutex_unlock(&g_authMgrLock);
return ret;
}
@ -409,6 +419,7 @@ int32_t CmAuthGetAuthorizedAppList(const struct CmContext *context, const struct
CM_FREE_PTR(tempAppUidList.appUid);
(void)CertManagerFreeUri(&uriObj);
pthread_mutex_unlock(&g_authMgrLock);
return ret;
}
@ -492,11 +503,13 @@ int32_t CmAuthIsAuthorizedApp(const struct CmContext *context, const struct CmBl
int32_t CmAuthRemoveGrantedApp(const struct CmContext *context, const struct CmBlob *keyUri, uint32_t appUid)
{
pthread_mutex_lock(&g_authMgrLock);
struct CMUri uriObj;
(void)memset_s(&uriObj, sizeof(uriObj), 0, sizeof(uriObj));
int32_t ret = GetAndCheckUriObj(&uriObj, keyUri);
if (ret != CM_SUCCESS) {
CM_LOG_E("uri decode failed, ret = %d", ret);
pthread_mutex_unlock(&g_authMgrLock);
return ret;
}
@ -527,6 +540,7 @@ int32_t CmAuthRemoveGrantedApp(const struct CmContext *context, const struct CmB
} while (0);
(void)CertManagerFreeUri(&uriObj);
pthread_mutex_unlock(&g_authMgrLock);
return ret;
}
@ -557,6 +571,7 @@ static int32_t DeleteAuthInfo(uint32_t userId, const struct CmBlob *uri, const s
/* clear auth info when delete public credential */
int32_t CmAuthDeleteAuthInfo(const struct CmContext *context, const struct CmBlob *uri)
{
pthread_mutex_lock(&g_authMgrLock);
struct CmAppUidList appUidList = { 0, NULL };
int32_t ret;
do {
@ -584,12 +599,14 @@ int32_t CmAuthDeleteAuthInfo(const struct CmContext *context, const struct CmBlo
} while (0);
CM_FREE_PTR(appUidList.appUid);
pthread_mutex_unlock(&g_authMgrLock);
return ret;
}
/* clear auth info when delete user */
int32_t CmAuthDeleteAuthInfoByUserId(uint32_t userId, const struct CmBlob *uri)
{
pthread_mutex_lock(&g_authMgrLock);
struct CmAppUidList appUidList = { 0, NULL };
int32_t ret;
do {
@ -613,20 +630,24 @@ int32_t CmAuthDeleteAuthInfoByUserId(uint32_t userId, const struct CmBlob *uri)
} while (0);
CM_FREE_PTR(appUidList.appUid);
pthread_mutex_unlock(&g_authMgrLock);
return ret;
}
/* clear auth info when delete application */
int32_t CmAuthDeleteAuthInfoByUid(uint32_t userId, uint32_t targetUid, const struct CmBlob *uri)
{
pthread_mutex_lock(&g_authMgrLock);
bool isInAuthList = false;
int32_t ret = CmCheckIsAuthUidExistByUserId(userId, targetUid, uri, &isInAuthList);
if (ret != CM_SUCCESS) {
CM_LOG_E("check is in auth list failed, ret = %d", ret);
pthread_mutex_unlock(&g_authMgrLock);
return ret;
}
if (!isInAuthList) {
pthread_mutex_unlock(&g_authMgrLock);
return CM_SUCCESS;
}
@ -635,6 +656,7 @@ int32_t CmAuthDeleteAuthInfoByUid(uint32_t userId, uint32_t targetUid, const str
ret = DeleteAuthInfo(userId, uri, &appUidList);
if (ret != CM_SUCCESS) {
CM_LOG_E("delete mac key info failed, ret = %d", ret);
pthread_mutex_unlock(&g_authMgrLock);
return ret;
}
@ -642,6 +664,7 @@ int32_t CmAuthDeleteAuthInfoByUid(uint32_t userId, uint32_t targetUid, const str
if (ret != CM_SUCCESS) {
CM_LOG_E("remove auth uid by user id failed, ret = %d", ret);
}
pthread_mutex_unlock(&g_authMgrLock);
return ret;
}

View File

@ -78,3 +78,8 @@ group("permission_test") {
"//base/security/certificate_manager/test/unittest/inner_permission_test:cm_inner_permission_test",
]
}
group("multi_thread_test") {
testonly = true
deps = [ "./unittest/multi_thread_test:cm_multi_thread_test" ]
}

View File

@ -0,0 +1,50 @@
# Copyright (c) 2023 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")
module_output_path = "cert_manager_standard/cert_manager_standard_test"
ohos_unittest("cm_multi_thread_test") {
module_out_path = module_output_path
sources = [
"../src/cm_test_common.cpp",
"src/cm_app_cert_multi_thread_test.cpp",
"src/cm_get_auth_list_multi_thread_test.cpp",
"src/cm_grant_multi_thread_test.cpp",
"src/cm_remove_grant_multi_thread_test.cpp",
]
include_dirs = [
"../include",
"//third_party/bounds_checking_function/include",
]
cflags_cc = [
"-Wall",
"-Werror",
]
cflags = cflags_cc
deps = [
"../../../frameworks/cert_manager_standard/main:cert_manager_standard_frameworks",
"../../../interfaces/innerkits/cert_manager_standard/main:cert_manager_sdk",
"//third_party/bounds_checking_function:libsec_static",
"//third_party/googletest:gtest_main",
]
defines = [ "_CM_LOG_ENABLE_" ]
external_deps = [
"access_token:libaccesstoken_sdk",
"access_token:libnativetoken",
"access_token:libtoken_setproc",
"c_utils:utils",
]
}

View File

@ -0,0 +1,131 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
#include <gtest/hwext/gtest-multithread.h>
#include "cert_manager_api.h"
#include "cm_cert_data_ecc.h"
#include "cm_cert_data_part1_rsa.h"
#include "cm_mem.h"
#include "cm_test_common.h"
using namespace testing::ext;
using namespace testing::mt;
using namespace CertmanagerTest;
namespace {
static constexpr uint32_t MULTI_THREAD_NUM = 10;
static const struct CmBlob g_appCert = { sizeof(g_rsa2048P12CertInfo), const_cast<uint8_t *>(g_rsa2048P12CertInfo) };
static const struct CmBlob g_appCertPwd = { sizeof(g_certPwd), const_cast<uint8_t *>(g_certPwd) };
class CmAppCertMultiThreadTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void CmAppCertMultiThreadTest::SetUpTestCase(void)
{
SetATPermission();
}
void CmAppCertMultiThreadTest::TearDownTestCase(void)
{
}
void CmAppCertMultiThreadTest::SetUp()
{
}
void CmAppCertMultiThreadTest::TearDown()
{
}
/**
* @tc.name: CmAppCertMultiThreadTest001
* @tc.desc: Test CertManager unInstall app cert interface abnormal function
* @tc.type: FUNC
* @tc.require: AR000H0MI8 /SR000H09N9
*/
HWMTEST_F(CmAppCertMultiThreadTest, CmAppCertMultiThreadTest001, TestSize.Level0, MULTI_THREAD_NUM)
{
int32_t ret = CmUninstallAppCert(nullptr, CM_CREDENTIAL_STORE);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmAppCertMultiThreadTest001 test failed, retcode:" << ret;
}
/**
* @tc.name: CmAppCertMultiThreadTest002
* @tc.desc: Test CertManager unInstall app cert interface abnormal function
* @tc.type: FUNC
* @tc.require: AR000H0MI8 /SR000H09N9
*/
HWMTEST_F(CmAppCertMultiThreadTest, CmAppCertMultiThreadTest002, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t keyUriBuf[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
int32_t ret = CmUninstallAppCert(&keyUri, CM_PRI_CREDENTIAL_STORE + 1);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmAppCertMultiThreadTest002 test failed, retcode:" << ret;
}
/**
* @tc.name: CmAppCertMultiThreadTest003
* @tc.desc: Test CertManager unInstall all app cert interface abnormal function
* @tc.type: FUNC
* @tc.require: AR000H0MI8 /SR000H09N9
*/
HWMTEST_F(CmAppCertMultiThreadTest, CmAppCertMultiThreadTest003, TestSize.Level0, MULTI_THREAD_NUM)
{
int32_t ret = CmUninstallAllAppCert();
EXPECT_EQ(ret, CM_SUCCESS) << "CmAppCertMultiThreadTest003 test failed, retcode:" << ret;
}
static void TestUninstallAppCertMultiThread(void)
{
int32_t ret = CmUninstallAllAppCert();
EXPECT_EQ(ret, CM_SUCCESS) << "TestUninstallAppCertMultiThread test failed, retcode:" << ret;
}
/**
* @tc.name: CmAppCertMultiThreadTest004
* @tc.desc: Test CertManager unInstall all app cert interface base function
* @tc.type: FUNC
* @tc.require: AR000H0MI8 /SR000H09N9
*/
HWTEST_F(CmAppCertMultiThreadTest, CmAppCertMultiThreadTest004, TestSize.Level0)
{
uint8_t certAliasBuf[] = "keyB";
struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
uint8_t uriBuf[MAX_LEN_URI] = {0};
struct CmBlob keyUri = { sizeof(uriBuf), uriBuf };
int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
EXPECT_EQ(ret, CM_SUCCESS) << "CmAppCertMultiThreadTest004 1 failed, retcode:" << ret;
ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
EXPECT_EQ(ret, CM_SUCCESS) << "CmAppCertMultiThreadTest004 2 failed, retcode:" << ret;
SET_THREAD_NUM(MULTI_THREAD_NUM);
GTEST_RUN_TASK(TestUninstallAppCertMultiThread);
}
} // end of namespace

View File

@ -0,0 +1,402 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
#include <gtest/hwext/gtest-multithread.h>
#include "cm_test_common.h"
#include "cert_manager_api.h"
using namespace testing::ext;
using namespace testing::mt;
using namespace CertmanagerTest;
namespace {
static constexpr uint32_t DEFAULT_AUTH_URI_LEN = 256;
static constexpr uint32_t DEFAULT_BASE_APP_ID = 1000;
static constexpr uint32_t APP_UID_COUNT_ONE = 1;
static constexpr uint32_t APP_UID_COUNT_MULTI = 10;
static constexpr uint32_t APP_UID_REMOVE_COUNT = 4;
static constexpr uint32_t DEFAULT_APP_UID_COUNT = 256;
static constexpr uint32_t MULTI_THREAD_NUM = 10;
static uint32_t g_checkListItemCount = 0;
static uint32_t g_checkBaseAppId = 0;
class CmGetAuthListMultiThreadTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void CmGetAuthListMultiThreadTest::SetUpTestCase(void)
{
SetATPermission();
}
void CmGetAuthListMultiThreadTest::TearDownTestCase(void)
{
}
static const uint8_t g_uriData[] = "oh:t=ak;o=GetAuthList;u=0;a=0";
static const CmBlob g_keyUri = { sizeof(g_uriData), (uint8_t *)g_uriData };
void CmGetAuthListMultiThreadTest::SetUp()
{
uint8_t aliasData[] = "GetAuthList";
struct CmBlob alias = { sizeof(aliasData), aliasData };
int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
uint32_t appId = DEFAULT_BASE_APP_ID;
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
ret = CmGrantAppCertificate(&g_keyUri, appId, &authUri);
EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
}
void CmGetAuthListMultiThreadTest::TearDown()
{
int32_t ret = CmUninstallAppCert(&g_keyUri, CM_CREDENTIAL_STORE);
EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
uint32_t appUid[DEFAULT_APP_UID_COUNT] = {0};
struct CmAppUidList appUidList = { DEFAULT_APP_UID_COUNT, appUid };
ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAuthorizedAppList failed, retcode:" << ret;
uint32_t expectCount = 0;
EXPECT_EQ(appUidList.appUidCount, expectCount);
}
static void TestRemoveGrant(uint32_t count, uint32_t baseAppId)
{
int32_t ret;
uint32_t appId;
for (uint32_t i = 0; i < count; ++i) {
appId = baseAppId + i;
ret = CmRemoveGrantedApp(&g_keyUri, appId);
EXPECT_EQ(ret, CM_SUCCESS) << "CmRemoveGrantedApp failed, retcode:" << ret;
}
}
static void TestGrant(uint32_t count, uint32_t baseAppId)
{
uint32_t appId;
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
int32_t ret;
for (uint32_t i = 0; i < count; ++i) {
appId = baseAppId + i;
authUri.size = DEFAULT_AUTH_URI_LEN;
ret = CmGrantAppCertificate(&g_keyUri, appId, &authUri);
EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
ret = CmIsAuthorizedApp(&authUri);
EXPECT_EQ(ret, CM_SUCCESS) << "CmIsAuthorizedApp failed, retcode:" << ret;
}
}
static void TestGetAuthedList(void)
{
uint32_t appUid[DEFAULT_APP_UID_COUNT] = {0};
struct CmAppUidList appUidList = { DEFAULT_APP_UID_COUNT, appUid };
int32_t ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAuthorizedAppList failed, retcode:" << ret;
EXPECT_EQ(appUidList.appUidCount, g_checkListItemCount);
uint32_t uidValidCount = 0;
for (uint32_t i = 0; i < g_checkListItemCount; ++i) {
for (uint32_t j = 0; j < appUidList.appUidCount; ++j) {
if ((g_checkBaseAppId + i) == appUidList.appUid[j]) {
uidValidCount++;
}
}
}
EXPECT_EQ(uidValidCount, g_checkListItemCount);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest001
* @tc.desc: Test CmGetAuthListMultiThreadTest keyUri is NULL
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest001, TestSize.Level0, MULTI_THREAD_NUM)
{
struct CmBlob *keyUri = nullptr; /* keyUri is NULL */
struct CmAppUidList appUidList = { 0, nullptr };
int32_t ret = CmGetAuthorizedAppList(keyUri, &appUidList);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest002
* @tc.desc: Test CmGetAuthListMultiThreadTest keyUri size is 0
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest002, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { 0, uriData }; /* keyUri size is 0 */
struct CmAppUidList appUidList = { 0, nullptr };
int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest003
* @tc.desc: Test CmGetAuthListMultiThreadTest keyUri data is null
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest003, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), nullptr }; /* keyUri data is null */
struct CmAppUidList appUidList = { 0, nullptr };
int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest004
* @tc.desc: Test CmGetAuthListMultiThreadTest keyUri data not end of '\0'
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest004, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { strlen((char *)uriData), uriData }; /* keyUri data not end of '\0' */
struct CmAppUidList appUidList = { 0, nullptr };
int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest005
* @tc.desc: Test CmGetAuthListMultiThreadTest keyUri data has no app
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest005, TestSize.Level0, MULTI_THREAD_NUM)
{
/* keyUri data has no app */
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
struct CmAppUidList appUidList = { 0, nullptr };
int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest006
* @tc.desc: Test CmGetAuthListMultiThreadTest keyUri data has no user
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest006, TestSize.Level0, MULTI_THREAD_NUM)
{
/* keyUri data has no user */
uint8_t uriData[] = "oh:t=ak;o=keyA;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
struct CmAppUidList appUidList = { 0, nullptr };
int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest007
* @tc.desc: Test CmGetAuthListMultiThreadTest keyUri data has no object
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest007, TestSize.Level0, MULTI_THREAD_NUM)
{
/* keyUri data has no object */
uint8_t uriData[] = "oh:t=ak;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
struct CmAppUidList appUidList = { 0, nullptr };
int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest008
* @tc.desc: Test CmGetAuthListMultiThreadTest keyUri data type not ak
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest008, TestSize.Level0, MULTI_THREAD_NUM)
{
/* keyUri data type not ak */
uint8_t uriData[] = "oh:t=m;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
struct CmAppUidList appUidList = { 0, nullptr };
int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest009
* @tc.desc: Test CmGetAuthListMultiThreadTest authUriList is NULL
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest009, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
struct CmAppUidList *appUidList = nullptr; /* authUriList is NULL */
int32_t ret = CmGetAuthorizedAppList(&keyUri, appUidList);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest010
* @tc.desc: Test CmGetAuthListMultiThreadTest authlist count too small
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest010, TestSize.Level0, MULTI_THREAD_NUM)
{
struct CmAppUidList appUidList = { 0, nullptr };
int32_t ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
EXPECT_EQ(ret, CMR_ERROR_BUFFER_TOO_SMALL);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest011
* @tc.desc: Test CmGetAuthListMultiThreadTest authlist data NULL
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest011, TestSize.Level0, MULTI_THREAD_NUM)
{
struct CmAppUidList appUidList = { APP_UID_COUNT_ONE, nullptr }; /* setup has granted 1 app uid */
int32_t ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest012
* @tc.desc: Test CmGetAuthListMultiThreadTest authlist count too big > MAX_OUT_BLOB_SIZE
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest012, TestSize.Level0, MULTI_THREAD_NUM)
{
struct CmAppUidList appUidList = { MAX_OUT_BLOB_SIZE + 1, nullptr }; /* count too big */
int32_t ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest013
* @tc.desc: Test CmGetAuthListMultiThreadTest not grant, get grant list { 0, NULL }
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest013, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
struct CmAppUidList appUidList = { 0, nullptr };
int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList); /* auth uid not exist */
EXPECT_EQ(ret, CM_SUCCESS);
uint32_t expectCount = 0;
EXPECT_EQ(appUidList.appUidCount, expectCount);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest014
* @tc.desc: Test CmGetAuthListMultiThreadTest grant 1, get authlist
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest014, TestSize.Level0, MULTI_THREAD_NUM)
{
uint32_t tempUid = 0;
struct CmAppUidList appUidList = { APP_UID_COUNT_ONE, &tempUid };
int32_t ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
EXPECT_EQ(ret, CM_SUCCESS);
EXPECT_EQ(appUidList.appUidCount, APP_UID_COUNT_ONE);
EXPECT_EQ(*(appUidList.appUid), DEFAULT_BASE_APP_ID);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest015
* @tc.desc: Test CmGetAuthListMultiThreadTest grant 10, get authlist
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest015, TestSize.Level0)
{
TestGrant(APP_UID_COUNT_MULTI, DEFAULT_BASE_APP_ID);
g_checkListItemCount = APP_UID_COUNT_MULTI;
g_checkBaseAppId = DEFAULT_BASE_APP_ID;
SET_THREAD_NUM(MULTI_THREAD_NUM);
GTEST_RUN_TASK(TestGetAuthedList);
/* clear environment */
TestRemoveGrant(APP_UID_COUNT_MULTI, DEFAULT_BASE_APP_ID);
g_checkListItemCount = 0;
g_checkBaseAppId = 0;
GTEST_RUN_TASK(TestGetAuthedList);
}
/**
* @tc.name: CmGetAuthListMultiThreadTest016
* @tc.desc: Test CmGetAuthListMultiThreadTest grant 10, remove grant 4, get authlist
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWTEST_F(CmGetAuthListMultiThreadTest, CmGetAuthListMultiThreadTest016, TestSize.Level0)
{
TestGrant(APP_UID_COUNT_MULTI, DEFAULT_BASE_APP_ID);
g_checkListItemCount = APP_UID_COUNT_MULTI;
g_checkBaseAppId = DEFAULT_BASE_APP_ID;
SET_THREAD_NUM(MULTI_THREAD_NUM);
GTEST_RUN_TASK(TestGetAuthedList);
uint32_t remainCount = APP_UID_COUNT_MULTI - APP_UID_REMOVE_COUNT;
uint32_t remainBaseAppId = DEFAULT_BASE_APP_ID + APP_UID_REMOVE_COUNT;
if (APP_UID_REMOVE_COUNT != 0) {
TestRemoveGrant(APP_UID_REMOVE_COUNT, DEFAULT_BASE_APP_ID);
g_checkListItemCount = remainCount;
g_checkBaseAppId = remainBaseAppId;
GTEST_RUN_TASK(TestGetAuthedList);
}
/* clear environment */
TestRemoveGrant(remainCount, remainBaseAppId);
g_checkListItemCount = 0;
g_checkBaseAppId = 0;
GTEST_RUN_TASK(TestGetAuthedList);
}
} // end of namespace

View File

@ -0,0 +1,399 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
#include <gtest/hwext/gtest-multithread.h>
#include "cm_test_common.h"
#include "cert_manager_api.h"
using namespace testing::ext;
using namespace testing::mt;
using namespace CertmanagerTest;
namespace {
static constexpr uint32_t DEFAULT_AUTH_URI_LEN = 256;
static constexpr uint32_t INVALID_AUTH_URI_LEN = 100;
static constexpr uint32_t DEFAULT_APP_ID = 1000;
static constexpr uint32_t GRANT_ONE_APP_ID = 1;
static constexpr uint32_t GRANT_MULTIPLE_APP_ID = 10;
static constexpr uint32_t MULTI_THREAD_NUM = 10;
static uint32_t g_count = 0;
static uint32_t g_isSameUid = 0;
class CmGrantMultiThreadTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void CmGrantMultiThreadTest::SetUpTestCase(void)
{
SetATPermission();
}
void CmGrantMultiThreadTest::TearDownTestCase(void)
{
}
void CmGrantMultiThreadTest::SetUp()
{
}
void CmGrantMultiThreadTest::TearDown()
{
}
static void TestGrantAppCertificatePreAction(void)
{
uint8_t aliasData[] = "TestNormalGrant";
struct CmBlob alias = { sizeof(aliasData), aliasData };
int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
}
static void TestGrantAppCertificate(void)
{
uint8_t uriData[] = "oh:t=ak;o=TestNormalGrant;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = 0;
for (uint32_t i = 0; i < g_count; ++i) {
if (!g_isSameUid) {
appId += i;
}
authUri.size = DEFAULT_AUTH_URI_LEN; /* clear authUri size */
ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
}
}
static void TestGrantAppCertificateAfterAction(void)
{
uint8_t uriData[] = "oh:t=ak;o=TestNormalGrant;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
int32_t ret = CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
}
/**
* @tc.name: CmGrantMultiThreadTest001
* @tc.desc: Test CmGrantMultiThreadTest keyUri is NULL
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest001, TestSize.Level0, MULTI_THREAD_NUM)
{
struct CmBlob *keyUri = nullptr; /* keyUri is NULL */
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = CmGrantAppCertificate(keyUri, appId, &authUri);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGrantMultiThreadTest002
* @tc.desc: Test CmGrantMultiThreadTest keyUri size is 0
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest002, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { 0, uriData }; /* keyUri size is 0 */
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGrantMultiThreadTest003
* @tc.desc: Test CmGrantMultiThreadTest keyUri data is null
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest003, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), nullptr }; /* keyUri data is null */
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGrantMultiThreadTest004
* @tc.desc: Test CmGrantMultiThreadTest keyUri data not end of '\0'
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest004, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { strlen((char *)uriData), uriData }; /* keyUri data not end of '\0' */
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGrantMultiThreadTest005
* @tc.desc: Test CmGrantMultiThreadTest keyUri data has no app: can't find cert
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest005, TestSize.Level0, MULTI_THREAD_NUM)
{
/* keyUri data has no app */
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
EXPECT_EQ(ret, CMR_ERROR_NOT_EXIST);
}
/**
* @tc.name: CmGrantMultiThreadTest006
* @tc.desc: Test CmGrantMultiThreadTest keyUri data has no user: can't find cert
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest006, TestSize.Level0, MULTI_THREAD_NUM)
{
/* keyUri data has no user */
uint8_t uriData[] = "oh:t=ak;o=keyA;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
EXPECT_EQ(ret, CMR_ERROR_NOT_EXIST);
}
/**
* @tc.name: CmGrantMultiThreadTest007
* @tc.desc: Test CmGrantMultiThreadTest keyUri data has no object: can't find cert
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest007, TestSize.Level0, MULTI_THREAD_NUM)
{
/* keyUri data has no object */
uint8_t uriData[] = "oh:t=ak;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
EXPECT_EQ(ret, CMR_ERROR_NOT_EXIST);
}
/**
* @tc.name: CmGrantMultiThreadTest008
* @tc.desc: Test CmGrantMultiThreadTest keyUri data type not ak: can't find cert
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest008, TestSize.Level0, MULTI_THREAD_NUM)
{
/* keyUri data type not ak */
uint8_t uriData[] = "oh:t=m;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
EXPECT_EQ(ret, CMR_ERROR_NOT_EXIST);
}
/**
* @tc.name: CmGrantMultiThreadTest009
* @tc.desc: Test CmGrantMultiThreadTest authUri null
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest009, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint32_t appId = DEFAULT_APP_ID;
struct CmBlob *authUri = nullptr; /* authUri nullptr */
int32_t ret = CmGrantAppCertificate(&keyUri, appId, authUri);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGrantMultiThreadTest010
* @tc.desc: Test CmGrantMultiThreadTest authUri size is 0
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest010, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { 0, authUriData }; /* authUri size is 0 */
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGrantMultiThreadTest011
* @tc.desc: Test CmGrantMultiThreadTest authUri data is NULL
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest011, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, nullptr }; /* authUri data is NULL */
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmGrantMultiThreadTest012
* @tc.desc: Test CmGrantMultiThreadTest normal case: grant 1 app id
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest012, TestSize.Level0)
{
g_count = GRANT_ONE_APP_ID;
g_isSameUid = true; /* grant 1 app id */
TestGrantAppCertificatePreAction();
SET_THREAD_NUM(MULTI_THREAD_NUM);
GTEST_RUN_TASK(TestGrantAppCertificate);
TestGrantAppCertificateAfterAction();
}
/**
* @tc.name: CmGrantMultiThreadTest013
* @tc.desc: Test CmGrantMultiThreadTest normal case: grant 10 same app id
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest013, TestSize.Level0)
{
g_count = GRANT_MULTIPLE_APP_ID;
g_isSameUid = true; /* grant 10 same app id */
TestGrantAppCertificatePreAction();
SET_THREAD_NUM(MULTI_THREAD_NUM);
GTEST_RUN_TASK(TestGrantAppCertificate);
TestGrantAppCertificateAfterAction();
}
/**
* @tc.name: CmGrantMultiThreadTest014
* @tc.desc: Test CmGrantMultiThreadTest normal case: grant 10 different app id
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest014, TestSize.Level0)
{
g_count = GRANT_MULTIPLE_APP_ID;
g_isSameUid = false; /* grant 10 different app id */
TestGrantAppCertificatePreAction();
SET_THREAD_NUM(MULTI_THREAD_NUM);
GTEST_RUN_TASK(TestGrantAppCertificate);
TestGrantAppCertificateAfterAction();
}
static void TestBufferTooSmall(void)
{
uint8_t uriData[] = "oh:t=ak;o=CmGrantMultiThreadTest014;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint8_t authUriData[INVALID_AUTH_URI_LEN] = {0}; /* size too small */
struct CmBlob authUri = { INVALID_AUTH_URI_LEN, authUriData };
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
EXPECT_EQ(ret, CMR_ERROR_BUFFER_TOO_SMALL) << "CmGrantAppCertificate failed, retcode:" << ret;
}
/**
* @tc.name: CmGrantMultiThreadTest015
* @tc.desc: Test CmGrantMultiThreadTest authUri size too small
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWTEST_F(CmGrantMultiThreadTest, CmGrantMultiThreadTest015, TestSize.Level0)
{
uint8_t aliasData[] = "CmGrantMultiThreadTest014";
struct CmBlob alias = { sizeof(aliasData), aliasData };
int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
uint8_t uriData[] = "oh:t=ak;o=CmGrantMultiThreadTest014;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
SET_THREAD_NUM(MULTI_THREAD_NUM);
GTEST_RUN_TASK(TestBufferTooSmall);
ret = CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
}
} // end of namespace

View File

@ -0,0 +1,287 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
#include <gtest/hwext/gtest-multithread.h>
#include "cm_test_common.h"
#include "cert_manager_api.h"
using namespace testing::ext;
using namespace testing::mt;
using namespace CertmanagerTest;
namespace {
static constexpr uint32_t DEFAULT_AUTH_URI_LEN = 256;
static constexpr uint32_t DEFAULT_APP_ID = 1000;
static constexpr uint32_t REMOVE_TWICE = 2;
static constexpr uint32_t REMOVE_ONCE = 1;
static constexpr uint32_t REMOVE_NOT_EXIST_ID = 1001;
static constexpr uint32_t MULTI_THREAD_NUM = 10;
static uint32_t g_removeAppUid = 0;
static uint32_t g_removeCount = 0;
class CmRemoveGrantMultiThreadTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void CmRemoveGrantMultiThreadTest::SetUpTestCase(void)
{
SetATPermission();
}
void CmRemoveGrantMultiThreadTest::TearDownTestCase(void)
{
}
void CmRemoveGrantMultiThreadTest::SetUp()
{
}
void CmRemoveGrantMultiThreadTest::TearDown()
{
}
static void TestRemoveGrantPreAction(void)
{
uint8_t aliasData[] = "TestRemoveGrant";
struct CmBlob alias = { sizeof(aliasData), aliasData };
int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
uint8_t uriData[] = "oh:t=ak;o=TestRemoveGrant;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
uint32_t appId = DEFAULT_APP_ID;
ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
}
static void TestRemoveGrant(void)
{
int32_t ret = 0;
uint8_t uriData[] = "oh:t=ak;o=TestRemoveGrant;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
for (uint32_t i = 0; i < g_removeCount; ++i) {
ret = CmRemoveGrantedApp(&keyUri, g_removeAppUid);
EXPECT_EQ(ret, CM_SUCCESS) << "CmRemoveGrantedApp failed, index:" << i << ", retcode:" << ret;
}
}
static void TestRemoveGrantAfterAction(void)
{
uint8_t uriData[] = "oh:t=ak;o=TestRemoveGrant;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
int32_t ret = CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
}
/**
* @tc.name: CmRemoveGrantMultiThreadTest001
* @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri is NULL
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest001, TestSize.Level0, MULTI_THREAD_NUM)
{
struct CmBlob *keyUri = nullptr; /* keyUri is NULL */
uint32_t appId = 0;
int32_t ret = CmRemoveGrantedApp(keyUri, appId);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmRemoveGrantMultiThreadTest002
* @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri size is 0
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest002, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { 0, uriData }; /* keyUri size is 0 */
uint32_t appId = 0;
int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmRemoveGrantMultiThreadTest003
* @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri data is null
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest003, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), nullptr }; /* keyUri data is null */
uint32_t appId = 0;
int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmRemoveGrantMultiThreadTest004
* @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri data not end of '\0'
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest004, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { strlen((char *)uriData), uriData }; /* keyUri data not end of '\0' */
uint32_t appId = 0;
int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmRemoveGrantMultiThreadTest005
* @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri data has no app
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest005, TestSize.Level0, MULTI_THREAD_NUM)
{
/* keyUri data has no app */
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint32_t appId = 0;
int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmRemoveGrantMultiThreadTest006
* @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri data has no user
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest006, TestSize.Level0, MULTI_THREAD_NUM)
{
/* keyUri data has no user */
uint8_t uriData[] = "oh:t=ak;o=keyA;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint32_t appId = 0;
int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmRemoveGrantMultiThreadTest007
* @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri data has no object
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest007, TestSize.Level0, MULTI_THREAD_NUM)
{
/* keyUri data has no object */
uint8_t uriData[] = "oh:t=ak;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint32_t appId = 0;
int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmRemoveGrantMultiThreadTest008
* @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri data type not ak
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest008, TestSize.Level0, MULTI_THREAD_NUM)
{
/* keyUri data type not ak */
uint8_t uriData[] = "oh:t=m;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint32_t appId = 0;
int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
}
/**
* @tc.name: CmRemoveGrantMultiThreadTest009
* @tc.desc: Test CmRemoveGrantMultiThreadTest remove while keyUri not exist
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest009, TestSize.Level0, MULTI_THREAD_NUM)
{
uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
struct CmBlob keyUri = { sizeof(uriData), uriData };
uint32_t appId = DEFAULT_APP_ID;
int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
EXPECT_EQ(ret, CM_SUCCESS);
}
/**
* @tc.name: CmRemoveGrantMultiThreadTest010
* @tc.desc: Test CmRemoveGrantMultiThreadTest remove while app uid not exist
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest010, TestSize.Level0)
{
TestRemoveGrantPreAction();
g_removeAppUid = REMOVE_NOT_EXIST_ID;
g_removeCount = REMOVE_ONCE;
SET_THREAD_NUM(MULTI_THREAD_NUM);
GTEST_RUN_TASK(TestRemoveGrant); /* remove not exist app uid */
TestRemoveGrantAfterAction();
}
/**
* @tc.name: CmRemoveGrantMultiThreadTest011
* @tc.desc: Test CmRemoveGrantMultiThreadTest remove while app uid exist
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest011, TestSize.Level0)
{
TestRemoveGrantPreAction();
g_removeAppUid = DEFAULT_APP_ID;
g_removeCount = REMOVE_ONCE;
SET_THREAD_NUM(MULTI_THREAD_NUM);
GTEST_RUN_TASK(TestRemoveGrant); /* remove exist app uid */
TestRemoveGrantAfterAction();
}
/**
* @tc.name: CmRemoveGrantMultiThreadTest012
* @tc.desc: Test CmRemoveGrantMultiThreadTest remove same app uid twice
* @tc.type: FUNC
* @tc.require: AR000H0MIA /SR000H09NA
*/
HWTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest012, TestSize.Level0)
{
TestRemoveGrantPreAction();
g_removeAppUid = DEFAULT_APP_ID;
g_removeCount = REMOVE_TWICE;
SET_THREAD_NUM(MULTI_THREAD_NUM);
GTEST_RUN_TASK(TestRemoveGrant); /* remove same app uid twice */
TestRemoveGrantAfterAction();
}
} // end of namespace