From 63ed6f00231b74fbd64b081a75b5af3981a6fcb4 Mon Sep 17 00:00:00 2001 From: haixiangw Date: Mon, 16 Dec 2024 00:33:10 -0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=90=8C=E6=AD=A5master=E3=80=91=20?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=B3=BB=E7=BB=9F=E5=8F=8A=E7=94=A8=E6=88=B7?= =?UTF-8?q?CA=E7=9A=84=E5=AD=98=E5=82=A8=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: haixiangw --- .../main/include/cert_manager_api.h | 5 +- .../main/include/cm_type.h | 9 + .../main/src/cert_manager_api.c | 31 ++- interfaces/kits/napi/BUILD.gn | 6 + interfaces/kits/napi/include/cm_napi_common.h | 10 +- .../include/cm_napi_get_cert_store_path.h | 26 ++ interfaces/kits/napi/src/cm_napi.cpp | 26 ++ interfaces/kits/napi/src/cm_napi_common.cpp | 22 ++ .../napi/src/cm_napi_get_cert_store_path.cpp | 223 ++++++++++++++++++ test/unittest/src/cm_get_certlist_test.cpp | 86 ++++++- test/unittest/src/cm_test_common.cpp | 2 +- 11 files changed, 437 insertions(+), 9 deletions(-) create mode 100644 interfaces/kits/napi/include/cm_napi_get_cert_store_path.h create mode 100644 interfaces/kits/napi/src/cm_napi_get_cert_store_path.cpp diff --git a/interfaces/innerkits/cert_manager_standard/main/include/cert_manager_api.h b/interfaces/innerkits/cert_manager_standard/main/include/cert_manager_api.h index c5e72bc..1789e58 100644 --- a/interfaces/innerkits/cert_manager_standard/main/include/cert_manager_api.h +++ b/interfaces/innerkits/cert_manager_standard/main/include/cert_manager_api.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 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 @@ -81,6 +81,9 @@ CM_API_EXPORT int32_t CmInstallUserCACert(const struct CmBlob *userCert, CM_API_EXPORT int32_t CmGetUserCACertList(const struct UserCAProperty *property, struct CertList *certificateList); +CM_API_EXPORT int32_t CmGetCertStorePath(const enum CmCertType type, const uint32_t userId, + char *path, uint32_t pathLen); + #ifdef __cplusplus } #endif diff --git a/interfaces/innerkits/cert_manager_standard/main/include/cm_type.h b/interfaces/innerkits/cert_manager_standard/main/include/cm_type.h index 997288f..d68bbc0 100644 --- a/interfaces/innerkits/cert_manager_standard/main/include/cm_type.h +++ b/interfaces/innerkits/cert_manager_standard/main/include/cm_type.h @@ -83,6 +83,10 @@ extern "C" { #define CM_STORE_CHECK(a) \ (((a) != CM_CREDENTIAL_STORE) && ((a) != CM_PRI_CREDENTIAL_STORE) && ((a) != CM_SYS_CREDENTIAL_STORE)) +#define CA_STORE_PATH_SYSTEM "/etc/security/certificates" +#define CA_STORE_PATH_USER_SANDBOX_BASE "/data/certificates/user_cacerts/" +#define CA_STORE_PATH_USER_SERVICE_BASE "/data/service/el1/public/cert_manager_service/certificates/user_open/" + enum CmKeyDigest { CM_DIGEST_NONE = 0, CM_DIGEST_MD5 = 1, @@ -380,6 +384,11 @@ struct CertName { struct CmBlob *subjectName; }; +enum CmCertType { + CM_CA_CERT_SYSTEM = 0, + CM_CA_CERT_USER = 1, +}; + enum CmCertScope { CM_ALL_USER = 0, CM_CURRENT_USER = 1, diff --git a/interfaces/innerkits/cert_manager_standard/main/src/cert_manager_api.c b/interfaces/innerkits/cert_manager_standard/main/src/cert_manager_api.c index 6d66543..11fb015 100644 --- a/interfaces/innerkits/cert_manager_standard/main/src/cert_manager_api.c +++ b/interfaces/innerkits/cert_manager_standard/main/src/cert_manager_api.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 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 @@ -380,4 +380,31 @@ CM_API_EXPORT int32_t CmGetUserCACertList(const struct UserCAProperty *property, int32_t ret = CmClientGetUserCertList(property, store, certificateList); CM_LOG_D("leave get user ca cert list, result = %d", ret); return ret; -} \ No newline at end of file +} + +CM_API_EXPORT int32_t CmGetCertStorePath(const enum CmCertType type, const uint32_t userId, + char *path, uint32_t pathLen) +{ + if (path == NULL) { + return CMR_ERROR_NULL_POINTER; + } + + if (type == CM_CA_CERT_SYSTEM) { + if (strcpy_s(path, pathLen, CA_STORE_PATH_SYSTEM) != EOK) { + CM_LOG_E("get system ca path: out path len[%u] too small.", pathLen); + return CMR_ERROR_BUFFER_TOO_SMALL; + } + return CM_SUCCESS; + } + + if (type == CM_CA_CERT_USER) { + if (sprintf_s(path, pathLen, "%s%u", CA_STORE_PATH_USER_SERVICE_BASE, userId) < 0) { + CM_LOG_E("get user ca path: out path len[%u] too small.", pathLen); + return CMR_ERROR_BUFFER_TOO_SMALL; + } + return CM_SUCCESS; + } + + return CMR_ERROR_INVALID_ARGUMENT; +} + diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index 40a5f4f..1d10a31 100644 --- a/interfaces/kits/napi/BUILD.gn +++ b/interfaces/kits/napi/BUILD.gn @@ -34,6 +34,7 @@ ohos_shared_library("certmanager") { "src/cm_napi_get_app_cert_info_common.cpp", "src/cm_napi_get_app_cert_list.cpp", "src/cm_napi_get_app_cert_list_common.cpp", + "src/cm_napi_get_cert_store_path.cpp", "src/cm_napi_get_system_cert_info.cpp", "src/cm_napi_get_system_cert_list.cpp", "src/cm_napi_grant.cpp", @@ -48,8 +49,13 @@ ohos_shared_library("certmanager") { ] external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "c_utils:utils", + "ipc:ipc_core", "napi:ace_napi", + "os_account:os_account_innerkits", + "samgr:samgr_proxy", ] cflags_cc = [ "-Wall", diff --git a/interfaces/kits/napi/include/cm_napi_common.h b/interfaces/kits/napi/include/cm_napi_common.h index ec0c755..ea0b333 100644 --- a/interfaces/kits/napi/include/cm_napi_common.h +++ b/interfaces/kits/napi/include/cm_napi_common.h @@ -24,9 +24,6 @@ #include "cm_mem.h" #include "cm_type.h" -#define DATA_SIZE_64KB (1024 * 64) -#define NAPI_ASSERT_FUNC(f) if (CM_SUCCESS != (f)) { CM_LOG_W("Failed: %s\n", #f); return; } - namespace CMNapi { static const std::string CM_CERT_PROPERTY_URI = "uri"; static const std::string CM_CERT_PROPERTY_TYPE = "type"; @@ -57,7 +54,9 @@ static const std::string CM_RESULT_PRPPERTY_CERTINFO = "certInfo"; static const std::string CM_RESULT_PRPPERTY_CREDENTIAL_LIST = "credentialList"; static const std::string CM_RESULT_PRPPERTY_CREDENTIAL = "credential"; -static const int32_t CERT_MANAGER_SYS_CAP = 17500000; +static const std::string CM_CERT_SCOPE_STR = "certScope"; +static const std::string CM_CERT_TYPE_STR = "certType"; + static const int32_t RESULT_NUMBER = 2; static const uint32_t APPLICATION_CERTIFICATE_STORE = 0; static const uint32_t APPLICATION_PRIVATE_CERTIFICATE_STORE = 3; @@ -90,6 +89,9 @@ void GeneratePromise(napi_env env, napi_deferred deferred, int32_t resultCode, void GenerateCallback(napi_env env, napi_ref callback, napi_value *result, int32_t arrLength, int32_t ret); void GenerateNapiPromise(napi_env env, napi_ref callback, napi_deferred *deferred, napi_value *promise); +bool IsValidCertType(const uint32_t certType); +bool IsValidCertScope(const uint32_t scope); + inline napi_value GetNull(napi_env env) { napi_value result = nullptr; diff --git a/interfaces/kits/napi/include/cm_napi_get_cert_store_path.h b/interfaces/kits/napi/include/cm_napi_get_cert_store_path.h new file mode 100644 index 0000000..f3593c6 --- /dev/null +++ b/interfaces/kits/napi/include/cm_napi_get_cert_store_path.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024-2024 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 CM_NAPI_GET_CERT_STORE_PATH_H +#define CM_NAPI_GET_CERT_STORE_PATH_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace CMNapi { +napi_value CMNapiGetCertStorePath(napi_env env, napi_callback_info info); +} + +#endif // CM_NAPI_GET_CERT_STORE_PATH_H diff --git a/interfaces/kits/napi/src/cm_napi.cpp b/interfaces/kits/napi/src/cm_napi.cpp index 81e4f65..4accac2 100644 --- a/interfaces/kits/napi/src/cm_napi.cpp +++ b/interfaces/kits/napi/src/cm_napi.cpp @@ -29,6 +29,7 @@ #include "cm_napi_grant.h" #include "cm_napi_sign_verify.h" #include "cm_napi_user_trusted_cert.h" +#include "cm_napi_get_cert_store_path.h" namespace CMNapi { inline void AddInt32Property(napi_env env, napi_value object, const char *name, int32_t value) @@ -100,6 +101,26 @@ namespace CMNapi { AddInt32Property(env, keyPadding, "CM_PADDING_PKCS1_V1_5", CM_JS_PADDING_PKCS1_V1_5); return keyPadding; } + + static napi_value CreateCertType(napi_env env) + { + napi_value type = nullptr; + NAPI_CALL(env, napi_create_object(env, &type)); + + AddInt32Property(env, type, "CA_CERT_SYSTEM", CM_CA_CERT_SYSTEM); + AddInt32Property(env, type, "CA_CERT_USER", CM_CA_CERT_USER); + return type; + } + + static napi_value CreateCertScope(napi_env env) + { + napi_value scope = nullptr; + NAPI_CALL(env, napi_create_object(env, &scope)); + + AddInt32Property(env, scope, "CURRENT_USER", CM_CURRENT_USER); + AddInt32Property(env, scope, "GLOBAL_USER", CM_GLOBAL_USER); + return scope; + } } // namespace CertManagerNapi using namespace CMNapi; @@ -112,6 +133,8 @@ extern "C" { DECLARE_NAPI_PROPERTY("CmKeyPurpose", CreateCMKeyPurpose(env)), DECLARE_NAPI_PROPERTY("CmKeyDigest", CreateCMKeyDigest(env)), DECLARE_NAPI_PROPERTY("CmKeyPadding", CreateCMKeyPadding(env)), + DECLARE_NAPI_PROPERTY("CertType", CreateCertType(env)), + DECLARE_NAPI_PROPERTY("CertScope", CreateCertScope(env)), /* system ca */ DECLARE_NAPI_FUNCTION("getSystemTrustedCertificateList", CMNapiGetSystemCertList), @@ -154,6 +177,9 @@ extern "C" { DECLARE_NAPI_FUNCTION("uninstallSystemAppCertificate", CMNapiUninstallSystemAppCert), DECLARE_NAPI_FUNCTION("getAllSystemAppCertificates", CMNapiGetSystemAppCertList), DECLARE_NAPI_FUNCTION("getSystemAppCertificate", CMNapiGetSystemAppCertInfo), + + /* get store path */ + DECLARE_NAPI_FUNCTION("getCertificateStorePath", CMNapiGetCertStorePath), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); return exports; diff --git a/interfaces/kits/napi/src/cm_napi_common.cpp b/interfaces/kits/napi/src/cm_napi_common.cpp index 24fb21d..1634b50 100644 --- a/interfaces/kits/napi/src/cm_napi_common.cpp +++ b/interfaces/kits/napi/src/cm_napi_common.cpp @@ -639,4 +639,26 @@ void FreeCredential(Credential *&credential) CmFree(credential); credential = nullptr; } + +bool IsValidCertType(const uint32_t certType) +{ + switch (static_cast(certType)) { + case CM_CA_CERT_SYSTEM: + case CM_CA_CERT_USER: + return true; + default: + return false; + } +} + +bool IsValidCertScope(const uint32_t scope) +{ + switch (static_cast(scope)) { + case CM_CURRENT_USER: + case CM_GLOBAL_USER: + return true; + default: + return false; + } +} } // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_get_cert_store_path.cpp b/interfaces/kits/napi/src/cm_napi_get_cert_store_path.cpp new file mode 100644 index 0000000..7033e62 --- /dev/null +++ b/interfaces/kits/napi/src/cm_napi_get_cert_store_path.cpp @@ -0,0 +1,223 @@ +/* + * Copyright (c) 2024-2024 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 "cm_napi_get_cert_store_path.h" + +#include "cm_log.h" +#include "cm_napi_common.h" +#include "cm_type.h" + +#include "bundle_mgr_proxy.h" +#include "iservice_registry.h" +#include "os_account_manager.h" +#include "system_ability_definition.h" + +using namespace std; +using namespace OHOS; +using namespace OHOS::AppExecFwk; + +namespace CMNapi { +namespace { +constexpr int CM_NAPI_GET_CERT_STORE_PATH_ARGS = 1; +} + +static sptr GetBundleMgrProxy() +{ + auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + CM_LOG_E("Failed to get system ability mgr."); + return nullptr; + } + + auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (!remoteObject) { + CM_LOG_E("Failed to get bundle manager proxy."); + return nullptr; + } + return iface_cast(remoteObject); +} + +static int32_t GetUserCaStorePath(const enum CmCertScope certScope, string &path) +{ + path += CA_STORE_PATH_USER_SANDBOX_BASE; + if (certScope == CM_GLOBAL_USER) { + path += "0"; + return CM_SUCCESS; + } + + int32_t userId = 0; + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (bundleMgrProxy == nullptr) { + CM_LOG_E("Failed to get bundle manager proxy."); + return CM_FAILURE; + } + + BundleInfo bundleInfo; + int32_t flags = static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_DEFAULT) | + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION); + int32_t ret = bundleMgrProxy->GetBundleInfoForSelf(flags, bundleInfo); + if (ret != 0) { + CM_LOG_E("Failed to get bundle info for self"); + return CM_FAILURE; + } + + ret = AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleInfo.applicationInfo.uid, userId); + if (ret != 0) { + CM_LOG_E("Failed to get userid from uid[%d]", bundleInfo.applicationInfo.uid); + return CM_FAILURE; + } + + path += to_string(userId); + return CM_SUCCESS; +} + +static napi_value GetCertStorePath(napi_env env, const enum CmCertType certType, const enum CmCertScope certScope) +{ + string path = ""; + if (certType == CM_CA_CERT_SYSTEM) { + path += CA_STORE_PATH_SYSTEM; + } else { + int32_t ret = GetUserCaStorePath(certScope, path); + if (ret != CM_SUCCESS) { + CM_LOG_E("Failed to get user ca path."); + ThrowError(env, INNER_FAILURE, "there is an internal error"); + return nullptr; + } + } + + napi_value result = nullptr; + napi_status status = napi_create_string_utf8(env, path.c_str(), path.length(), &result); + if (status != napi_ok) { + CM_LOG_E("Failed to creat string out."); + ThrowError(env, INNER_FAILURE, "there is an internal error"); + return nullptr; + } + return result; +} + +static int32_t GetCertScope(napi_env env, napi_value arg, uint32_t &scope) +{ + bool hasScope = false; + napi_status status = napi_has_named_property(env, arg, CM_CERT_SCOPE_STR.c_str(), &hasScope); + if (status != napi_ok || !hasScope) { + CM_LOG_D("property certScope not exist"); + return CM_SUCCESS; /* certScope is optional parameter, can be unset */ + } + + napi_value obj = nullptr; + status = napi_get_named_property(env, arg, CM_CERT_SCOPE_STR.c_str(), &obj); + if (status != napi_ok) { + CM_LOG_E("Failed to get property certScope"); + return CM_FAILURE; + } + + napi_valuetype valueType; + status = napi_typeof(env, obj, &valueType); + if (status != napi_ok) { + CM_LOG_E("Failed to get property type"); + return CM_FAILURE; + } + + if (valueType == napi_undefined) { + CM_LOG_D("property certScope is undefined"); + return CM_SUCCESS; /* certScope is optional parameter, can be undefined */ + } + + napi_value result = ParseUint32(env, obj, scope); + if (result == nullptr) { + CM_LOG_E("Failed to get scope value"); + return CM_FAILURE; + } + + if (!IsValidCertScope(scope)) { /* the scope needs to be checked regardless of the certType type */ + CM_LOG_E("certScope[%u] is invalid", scope); + return CM_FAILURE; + } + + return CM_SUCCESS; +} + +static int32_t GetAndCheckCertType(napi_env env, napi_value arg, uint32_t &type) +{ + napi_value certType = nullptr; + napi_status status = napi_get_named_property(env, arg, CM_CERT_TYPE_STR.c_str(), &certType); + if (status != napi_ok) { + CM_LOG_E("Failed to get certType"); + return CM_FAILURE; + } + + napi_value result = ParseUint32(env, certType, type); + if (result == nullptr) { + CM_LOG_E("Failed to get certType value"); + return CM_FAILURE; + } + + if (!IsValidCertType(type)) { + CM_LOG_E("certType[%u] is invalid", type); + return CM_FAILURE; + } + return CM_SUCCESS; +} + +static int32_t GetAndCheckCertScope(napi_env env, napi_value arg, const enum CmCertType type, uint32_t &scope) +{ + int32_t ret = GetCertScope(env, arg, scope); + if (ret != CM_SUCCESS) { + CM_LOG_E("Failed to get certScope"); + return CM_FAILURE; + } + + if ((type == CM_CA_CERT_USER) && (scope == INIT_INVALID_VALUE)) { + CM_LOG_E("get user ca cert store path, but scope is not set"); + return CM_FAILURE; + } + return CM_SUCCESS; +} + +napi_value CMNapiGetCertStorePath(napi_env env, napi_callback_info info) +{ + // get params + size_t argc = CM_NAPI_GET_CERT_STORE_PATH_ARGS; + napi_value argv[CM_NAPI_GET_CERT_STORE_PATH_ARGS] = { nullptr }; + napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); + if (status != napi_ok) { + ThrowError(env, PARAM_ERROR, "Failed to get params"); + return nullptr; + } + + // check param count should be 1. + if (argc != CM_NAPI_GET_CERT_STORE_PATH_ARGS) { + ThrowError(env, PARAM_ERROR, "param count invalid, should be 1."); + CM_LOG_E("args count[%zu] invalid, should be 1.", argc); + return nullptr; + } + + uint32_t type; + int32_t ret = GetAndCheckCertType(env, argv[0], type); + if (ret != CM_SUCCESS) { + ThrowError(env, PARAM_ERROR, "Failed to get param certType"); + return nullptr; + } + + uint32_t scope = INIT_INVALID_VALUE; + ret = GetAndCheckCertScope(env, argv[0], static_cast(type), scope); + if (ret != CM_SUCCESS) { + ThrowError(env, PARAM_ERROR, "Failed to get param certScope"); + return nullptr; + } + + return GetCertStorePath(env, static_cast(type), static_cast(scope)); +} +} // namespace CertManagerNapi diff --git a/test/unittest/src/cm_get_certlist_test.cpp b/test/unittest/src/cm_get_certlist_test.cpp index 4d1dc4f..66ba762 100644 --- a/test/unittest/src/cm_get_certlist_test.cpp +++ b/test/unittest/src/cm_get_certlist_test.cpp @@ -22,6 +22,7 @@ using namespace testing::ext; using namespace CertmanagerTest; namespace { static const int TIMES_PERFORMANCE = 10; +static const uint32_t PATH_LEN = 1000; struct CertAbstractResult { struct CertAbstract certAbstract; @@ -171,4 +172,87 @@ HWTEST_F(CmGetCertListTest, ExceptionGetCertList005, TestSize.Level0) EXPECT_EQ(CmGetCertList(CM_SYSTEM_TRUSTED_STORE, NULL), CMR_ERROR_NULL_POINTER); EXPECT_EQ(CmGetCertList(10, lstCert), CMR_ERROR_INVALID_ARGUMENT); } -} \ No newline at end of file + +/** + * @tc.name: GetCertStorePath001 + * @tc.desc: get system ca path + * @tc.type: FUNC + */ +HWTEST_F(CmGetCertListTest, GetCertStorePath001, TestSize.Level0) +{ + char path[PATH_LEN] = {0}; + int32_t ret = CmGetCertStorePath(CM_CA_CERT_SYSTEM, 0, path, sizeof(path)); + EXPECT_EQ(ret, CM_SUCCESS); + EXPECT_EQ(strcmp(path, CA_STORE_PATH_SYSTEM), 0); +} + +/** + * @tc.name: GetCertStorePath002 + * @tc.desc: get user ca path + * @tc.type: FUNC + */ +HWTEST_F(CmGetCertListTest, GetCertStorePath002, TestSize.Level0) +{ + char path[PATH_LEN] = {0}; + uint32_t userId = 101; + int32_t ret = CmGetCertStorePath(CM_CA_CERT_USER, userId, path, sizeof(path)); + EXPECT_EQ(ret, CM_SUCCESS); + + std::string expectPath = CA_STORE_PATH_USER_SERVICE_BASE + std::to_string(userId); + std::cout << "1:" << path << std::endl; + std::cout << "2:" << expectPath << std::endl; + EXPECT_EQ(strcmp(path, expectPath.c_str()), 0); +} + +/** + * @tc.name: GetCertStorePath003 + * @tc.desc: type invalid + * @tc.type: FUNC + */ +HWTEST_F(CmGetCertListTest, GetCertStorePath003, TestSize.Level0) +{ + char path[PATH_LEN] = {0}; + int32_t type = 3; /* type invalid */ + int32_t ret = CmGetCertStorePath(static_cast(type), 0, path, sizeof(path)); + EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT); +} + +/** + * @tc.name: GetCertStorePath004 + * @tc.desc: path is null + * @tc.type: FUNC + */ +HWTEST_F(CmGetCertListTest, GetCertStorePath004, TestSize.Level0) +{ + int32_t ret = CmGetCertStorePath(CM_CA_CERT_SYSTEM, 0, nullptr, PATH_LEN); + EXPECT_EQ(ret, CMR_ERROR_NULL_POINTER); +} + +/** + * @tc.name: GetCertStorePath005 + * @tc.desc: get system ca path, len too small + * @tc.type: FUNC + */ +HWTEST_F(CmGetCertListTest, GetCertStorePath005, TestSize.Level0) +{ + char path[PATH_LEN] = {0}; + uint32_t length = strlen(CA_STORE_PATH_SYSTEM); + int32_t ret = CmGetCertStorePath(CM_CA_CERT_SYSTEM, 0, path, length); + EXPECT_EQ(ret, CMR_ERROR_BUFFER_TOO_SMALL); +} + +/** + * @tc.name: GetCertStorePath006 + * @tc.desc: get user ca path, len too small + * @tc.type: FUNC + */ +HWTEST_F(CmGetCertListTest, GetCertStorePath006, TestSize.Level0) +{ + char path[PATH_LEN] = {0}; + uint32_t userId = 100; + std::string expectPath = CA_STORE_PATH_USER_SERVICE_BASE + std::to_string(userId); + uint32_t length = expectPath.length(); + int32_t ret = CmGetCertStorePath(CM_CA_CERT_USER, userId, path, length); + EXPECT_EQ(ret, CMR_ERROR_BUFFER_TOO_SMALL); +} +} diff --git a/test/unittest/src/cm_test_common.cpp b/test/unittest/src/cm_test_common.cpp index 5421772..54044c2 100644 --- a/test/unittest/src/cm_test_common.cpp +++ b/test/unittest/src/cm_test_common.cpp @@ -119,7 +119,7 @@ int32_t InitCertList(struct CertList **cList) return CMR_ERROR_MALLOC_FAIL; } - uint32_t buffSize = MAX_COUNT_CERTIFICATE_ALL * sizeof(struct CertAbstract); + uint32_t buffSize = MAX_COUNT_CERTIFICATE_ALL * sizeof(struct CertAbstract); (*cList)->certAbstract = static_cast(CmMalloc(buffSize)); if ((*cList)->certAbstract == NULL) { return CMR_ERROR_MALLOC_FAIL; -- Gitee