!1133 代码优化

Merge pull request !1133 from 孟立/master
This commit is contained in:
openharmony_ci 2024-04-30 06:36:14 +00:00 committed by Gitee
commit 0722f95c2e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 39 additions and 36 deletions

View File

@ -51,7 +51,7 @@ static ListAliasesAsyncContext CreateListAliasesAsyncContext()
static void DeleteListAliasesAsyncContext(napi_env env, ListAliasesAsyncContext &context) static void DeleteListAliasesAsyncContext(napi_env env, ListAliasesAsyncContext &context)
{ {
if (context== nullptr) { if (context == nullptr) {
return; return;
} }
if (context->asyncWork != nullptr) { if (context->asyncWork != nullptr) {

View File

@ -111,7 +111,8 @@ int32_t AppendNewInfoForUseKeyInService(const struct HksParamSet *paramSet,
int32_t AppendStorageLevelIfNotExist(const struct HksParamSet *paramSet, struct HksParamSet **outParamSet); int32_t AppendStorageLevelIfNotExist(const struct HksParamSet *paramSet, struct HksParamSet **outParamSet);
int32_t HksServiceListAliases(const struct HksProcessInfo *processInfo, const struct HksParamSet *paramSet, struct HksKeyAliasSet **outData); int32_t HksServiceListAliases(const struct HksProcessInfo *processInfo, const struct HksParamSet *paramSet,
struct HksKeyAliasSet **outData);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -860,7 +860,7 @@ void HksServiceDeleteUIDKeyAliasFile(const struct HksProcessInfo *processInfo)
HKS_FREE(uidData); HKS_FREE(uidData);
} }
static int32_t getHksKeyAliasSet(const struct HksFileEntry *fileNameList, const uint32_t fileCount, static int32_t GetHksKeyAliasSet(const struct HksFileEntry *fileNameList, const uint32_t fileCount,
struct HksKeyAliasSet **outData) struct HksKeyAliasSet **outData)
{ {
if (fileCount == 0) { if (fileCount == 0) {
@ -904,7 +904,7 @@ static int32_t getHksKeyAliasSet(const struct HksFileEntry *fileNameList, const
return ret; return ret;
} }
static int32_t getHksFileEntry(const struct HksStoreFileInfo *fileInfo, struct HksFileEntry **fileNameList) static int32_t GetHksFileEntry(const struct HksStoreFileInfo *fileInfo, struct HksFileEntry **fileNameList)
{ {
uint32_t fileCount; uint32_t fileCount;
int32_t ret = GetFileCount(fileInfo->mainPath.path, &fileCount); int32_t ret = GetFileCount(fileInfo->mainPath.path, &fileCount);
@ -941,12 +941,12 @@ int32_t HksListAliasesByProcessName(const struct HksStoreFileInfo *fileInfo, str
int32_t ret; int32_t ret;
struct HksFileEntry *fileNameList = NULL; struct HksFileEntry *fileNameList = NULL;
do { do {
ret = getHksFileEntry(fileInfo, &fileNameList); ret = GetHksFileEntry(fileInfo, &fileNameList);
HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "get file entry failed, ret = %" LOG_PUBLIC "d.", ret) HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "get file entry failed, ret = %" LOG_PUBLIC "d.", ret)
// case success and has data // case success and has data
if (fileNameList != NULL) { if (fileNameList != NULL) {
ret = getHksKeyAliasSet(fileNameList, fileNameList->fileNameLen, outData); ret = GetHksKeyAliasSet(fileNameList, fileNameList->fileNameLen, outData);
HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "get key alias set failed, ret = %" LOG_PUBLIC "d.", ret) HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "get key alias set failed, ret = %" LOG_PUBLIC "d.", ret)
} }
} while (0); } while (0);

View File

@ -48,29 +48,28 @@ public:
void TearDown(); void TearDown();
}; };
static const char *g_alias = "testAlias"; static const char ALIAS[] = "testAlias";
const struct HksBlob testKeyAlias = { sizeof(g_alias), (uint8_t *)g_alias }; static const struct HksBlob TEST_KEY_ALIAS = { strlen(ALIAS), (uint8_t *)ALIAS };
static const char *g_invisableAlias = "#test("; static const char INVISABLE_ALIAS[] = "#test(";
const struct HksBlob testInvisableKeyAlias = { sizeof(g_invisableAlias), (uint8_t *)g_invisableAlias }; static const struct HksBlob TEST_INVISABLE_KEY_ALIAS = { strlen(INVISABLE_ALIAS), (uint8_t *)INVISABLE_ALIAS };
static const uint32_t g_expect_min_num = 1; static const uint32_t EXPECT_MIN_NUM = 1;
static const uint32_t g_invalid_de_specific_user_id = -1; static const uint32_t INVALID_DE_SPECIFIC_USER_ID = -1;
static const uint32_t g_invalid_ce_specific_user_id = 99; static const uint32_t INVALID_CE_SPECIFIC_USER_ID = 99;
static const uint32_t g_valid_ce_specific_user_id = 100; static const uint32_t VALID_CE_SPECIFIC_USER_ID = 100;
static const uint32_t g_error_size = 2; static const uint32_t ERROR_SIZE = 2;
static const std::vector<std::vector<HksParam>> INIT_ADD_PARAMS = {
static const std::vector<std::vector<HksParam>> g_initAddParam = {
#ifdef HKS_INTERACT_ABILITY #ifdef HKS_INTERACT_ABILITY
{ {
{ .tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = g_valid_ce_specific_user_id }, { .tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = VALID_CE_SPECIFIC_USER_ID },
{ .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_CE }, { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_CE },
}, { }, {
{ .tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = g_valid_ce_specific_user_id }, { .tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = VALID_CE_SPECIFIC_USER_ID },
{ .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_ECE }, { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_ECE },
}, },
#endif #endif
@ -79,7 +78,7 @@ static const std::vector<std::vector<HksParam>> g_initAddParam = {
} }
}; };
static const struct HksParam g_initCommonParams[] = { static const struct HksParam INIT_COMMON_PARAMS[] = {
{ .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES }, { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
{ .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT }, { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
{ .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 }, { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
@ -122,11 +121,12 @@ void HksListAliasesTest::SetUpTestCase(void)
#endif #endif
struct HksParamSet *paramSet = nullptr; struct HksParamSet *paramSet = nullptr;
for (std::size_t i = 0; i < g_initAddParam.size(); i++) { for (std::size_t i = 0; i < INIT_ADD_PARAMS.size(); i++) {
ret = HuksTest::TestBuildInitParamSet(g_initCommonParams, g_initAddParam[i], HKS_ARRAY_SIZE(g_initCommonParams), &paramSet); ret = HuksTest::TestBuildInitParamSet(INIT_COMMON_PARAMS, INIT_ADD_PARAMS[i],
HKS_ARRAY_SIZE(INIT_COMMON_PARAMS), &paramSet);
ASSERT_EQ(ret, HKS_SUCCESS); ASSERT_EQ(ret, HKS_SUCCESS);
ret = HksGenerateKey(&testKeyAlias, paramSet, nullptr); ret = HksGenerateKey(&TEST_KEY_ALIAS, paramSet, nullptr);
ASSERT_EQ(ret, HKS_SUCCESS); ASSERT_EQ(ret, HKS_SUCCESS);
HksFreeParamSet(&paramSet); HksFreeParamSet(&paramSet);
@ -192,7 +192,7 @@ HWTEST_F(HksListAliasesTest, HksListAliasesTest003, TestSize.Level0)
HksFreeParamSet(&queryParamSet); HksFreeParamSet(&queryParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "HksListAliases failed, ret = " << ret; EXPECT_EQ(ret, HKS_SUCCESS) << "HksListAliases failed, ret = " << ret;
EXPECT_EQ(outData->aliasesCnt >= g_expect_min_num, true) << "HksListAliases failed, cnt = " << outData->aliasesCnt; EXPECT_EQ(outData->aliasesCnt >= EXPECT_MIN_NUM, true) << "HksListAliases failed, cnt = " << outData->aliasesCnt;
HksFreeKeyAliasSet(outData); HksFreeKeyAliasSet(outData);
} }
@ -215,7 +215,7 @@ HWTEST_F(HksListAliasesTest, HksListAliasesTest004, TestSize.Level0)
HksFreeParamSet(&queryParamSet); HksFreeParamSet(&queryParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "HksListAliases failed, ret = " << ret; EXPECT_EQ(ret, HKS_SUCCESS) << "HksListAliases failed, ret = " << ret;
EXPECT_EQ(outData->aliasesCnt >= g_expect_min_num, true) << "HksListAliases failed, cnt = " << outData->aliasesCnt; EXPECT_EQ(outData->aliasesCnt >= EXPECT_MIN_NUM, true) << "HksListAliases failed, cnt = " << outData->aliasesCnt;
HksFreeKeyAliasSet(outData); HksFreeKeyAliasSet(outData);
} }
@ -234,7 +234,7 @@ HWTEST_F(HksListAliasesTest, HksListAliasesTest005, TestSize.Level0)
struct HksParamSet *queryParamSet = nullptr; struct HksParamSet *queryParamSet = nullptr;
struct HksParam addParams[] = { struct HksParam addParams[] = {
{ .tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = g_valid_ce_specific_user_id }, { .tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = VALID_CE_SPECIFIC_USER_ID },
{ .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_CE } { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_CE }
}; };
ret = HuksTest::TestBuildParamSet(addParams, sizeof(addParams) / sizeof(addParams[0]), &queryParamSet); ret = HuksTest::TestBuildParamSet(addParams, sizeof(addParams) / sizeof(addParams[0]), &queryParamSet);
@ -247,7 +247,7 @@ HWTEST_F(HksListAliasesTest, HksListAliasesTest005, TestSize.Level0)
#ifdef HKS_INTERACT_ABILITY #ifdef HKS_INTERACT_ABILITY
EXPECT_EQ(ret, HKS_SUCCESS) << "HksListAliases failed, ret = " << ret; EXPECT_EQ(ret, HKS_SUCCESS) << "HksListAliases failed, ret = " << ret;
EXPECT_EQ(outData->aliasesCnt >= g_expect_min_num, true) << "HksListAliases failed, cnt = " << outData->aliasesCnt; EXPECT_EQ(outData->aliasesCnt >= EXPECT_MIN_NUM, true) << "HksListAliases failed, cnt = " << outData->aliasesCnt;
#else #else
EXPECT_EQ(ret, HKS_ERROR_NO_PERMISSION) << "HksListAliases failed, ret = " << ret; EXPECT_EQ(ret, HKS_ERROR_NO_PERMISSION) << "HksListAliases failed, ret = " << ret;
#endif #endif
@ -270,7 +270,7 @@ HWTEST_F(HksListAliasesTest, HksListAliasesTest006, TestSize.Level0)
struct HksParamSet *queryParamSet = nullptr; struct HksParamSet *queryParamSet = nullptr;
struct HksParam addParams[] = { struct HksParam addParams[] = {
{ .tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = g_valid_ce_specific_user_id }, { .tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = VALID_CE_SPECIFIC_USER_ID },
{ .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_ECE } { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_ECE }
}; };
ret = HuksTest::TestBuildParamSet(addParams, sizeof(addParams) / sizeof(addParams[0]), &queryParamSet); ret = HuksTest::TestBuildParamSet(addParams, sizeof(addParams) / sizeof(addParams[0]), &queryParamSet);
@ -283,7 +283,7 @@ HWTEST_F(HksListAliasesTest, HksListAliasesTest006, TestSize.Level0)
#ifdef HKS_INTERACT_ABILITY #ifdef HKS_INTERACT_ABILITY
EXPECT_EQ(ret, HKS_SUCCESS) << "HksListAliases failed, ret = " << ret; EXPECT_EQ(ret, HKS_SUCCESS) << "HksListAliases failed, ret = " << ret;
EXPECT_EQ(outData->aliasesCnt >= g_expect_min_num, true) << "HksListAliases failed, cnt = " << outData->aliasesCnt; EXPECT_EQ(outData->aliasesCnt >= EXPECT_MIN_NUM, true) << "HksListAliases failed, cnt = " << outData->aliasesCnt;
#else #else
EXPECT_EQ(ret, HKS_ERROR_NO_PERMISSION) << "HksListAliases failed, ret = " << ret; EXPECT_EQ(ret, HKS_ERROR_NO_PERMISSION) << "HksListAliases failed, ret = " << ret;
#endif #endif
@ -307,7 +307,7 @@ HWTEST_F(HksListAliasesTest, HksListAliasesTest007, TestSize.Level0)
struct HksParamSet *queryParamSet = nullptr; struct HksParamSet *queryParamSet = nullptr;
struct HksParam addParams[] = { struct HksParam addParams[] = {
{ .tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = g_invalid_ce_specific_user_id }, { .tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = INVALID_CE_SPECIFIC_USER_ID },
{ .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_CE } { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_CE }
}; };
@ -344,7 +344,7 @@ HWTEST_F(HksListAliasesTest, HksListAliasesTest008, TestSize.Level0)
struct HksParamSet *queryParamSet = nullptr; struct HksParamSet *queryParamSet = nullptr;
struct HksParam addParams[] = { struct HksParam addParams[] = {
{ .tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = g_invalid_de_specific_user_id }, { .tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = INVALID_DE_SPECIFIC_USER_ID },
{ .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_DE } { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_DE }
}; };
ret = HuksTest::TestBuildParamSet(addParams, sizeof(addParams) / sizeof(addParams[0]), &queryParamSet); ret = HuksTest::TestBuildParamSet(addParams, sizeof(addParams) / sizeof(addParams[0]), &queryParamSet);
@ -379,7 +379,7 @@ HWTEST_F(HksListAliasesTest, HksListAliasesTest009, TestSize.Level0)
int32_t ret = HuksTest::TestBuildParamSet(addParams, sizeof(addParams) / sizeof(addParams[0]), &queryParamSet); int32_t ret = HuksTest::TestBuildParamSet(addParams, sizeof(addParams) / sizeof(addParams[0]), &queryParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "HuksTest::TestBuildParamSet failed, ret = " << ret; EXPECT_EQ(ret, HKS_SUCCESS) << "HuksTest::TestBuildParamSet failed, ret = " << ret;
queryParamSet->paramsCnt = g_error_size; queryParamSet->paramsCnt = ERROR_SIZE;
struct HksKeyAliasSet *outData = nullptr; struct HksKeyAliasSet *outData = nullptr;
ret = HksListAliases(queryParamSet, &outData); ret = HksListAliases(queryParamSet, &outData);
@ -389,11 +389,12 @@ HWTEST_F(HksListAliasesTest, HksListAliasesTest009, TestSize.Level0)
HksFreeKeyAliasSet(outData); HksFreeKeyAliasSet(outData);
} }
static bool IsMatch(const HksKeyAliasSet *aliasSet) { static bool IsMatch(const HksKeyAliasSet *aliasSet)
{
bool ret = false; bool ret = false;
if (aliasSet->aliasesCnt > 0 && aliasSet->aliases != NULL) { if (aliasSet->aliasesCnt > 0 && aliasSet->aliases != NULL) {
for (uint32_t i = 0; i < aliasSet->aliasesCnt; i++) { for (uint32_t i = 0; i < aliasSet->aliasesCnt; i++) {
if (HksMemCmp(aliasSet->aliases[i].data, testInvisableKeyAlias.data, testInvisableKeyAlias.size)) { if (HksMemCmp(aliasSet->aliases[i].data, TEST_INVISABLE_KEY_ALIAS.data, TEST_INVISABLE_KEY_ALIAS.size)) {
return true; return true;
} }
} }
@ -414,8 +415,9 @@ HWTEST_F(HksListAliasesTest, HksListAliasesTest010, TestSize.Level0)
const std::vector<HksParam> param = { const std::vector<HksParam> param = {
{ .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_DE }, { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_DE },
}; };
int32_t ret = HuksTest::TestBuildInitParamSet(g_initCommonParams, param, HKS_ARRAY_SIZE(g_initCommonParams), &initParamSet); int32_t ret = HuksTest::TestBuildInitParamSet(INIT_COMMON_PARAMS, param, HKS_ARRAY_SIZE(INIT_COMMON_PARAMS),
ret = HksGenerateKey(&testInvisableKeyAlias, initParamSet, nullptr); &initParamSet);
ret = HksGenerateKey(&TEST_INVISABLE_KEY_ALIAS, initParamSet, nullptr);
ASSERT_EQ(ret, HKS_SUCCESS); ASSERT_EQ(ret, HKS_SUCCESS);
HksFreeParamSet(&initParamSet); HksFreeParamSet(&initParamSet);