From 8e59bacb853dc8bca9e84ceca9d213634d027d54 Mon Sep 17 00:00:00 2001 From: Phan Xuan Tan Date: Tue, 9 Jul 2024 15:36:25 +0700 Subject: [PATCH] Add positive and negative test cases for getCertificateListFromStore and loadCertificatesFromStore Change-Id: Ie4b42b2fd5202dfb9ca6ed623ae81899bd1d6eb9 --- unit-tests/test_cert_server_logic.cpp | 200 ++++++++++++++++++++++++++++++++++ unit-tests/test_constant.cpp | 7 ++ unit-tests/test_constant.h | 5 + 3 files changed, 212 insertions(+) diff --git a/unit-tests/test_cert_server_logic.cpp b/unit-tests/test_cert_server_logic.cpp index 482e001..69ce6b2 100644 --- a/unit-tests/test_cert_server_logic.cpp +++ b/unit-tests/test_cert_server_logic.cpp @@ -16,6 +16,7 @@ #include "cert-svc/ccert.h" #include "cert-svc/cerror.h" +#include "test_common.h" #include "test_constant.h" #include "test_macros.h" #include "vcore/Client.h" @@ -31,7 +32,72 @@ extern "C" static const std::string gNamePrexif = "gname_sever_"; static const std::string commonNamePrefix = "TEST_sever_"; +static const std::string systemGname = "002c0b4f.0"; +static const std::string largeString = createLargeString(VCORE_MAX_RECV_DATA_SIZE + 100); +void getCertListFromStores(int reqType, CertStoreType storeTypes, int isRootApp, int expected) +{ + int result; + char *certListBuffer = NULL; + size_t bufferLen = 0; + size_t certCount = 0; + + result = getCertificateListFromStore( + reqType, + storeTypes, + isRootApp, + &certListBuffer, + &bufferLen, + &certCount); + + BOOST_CHECK_EQUAL(result, expected); + + if (expected == CERTSVC_SUCCESS) { + BOOST_CHECK(certListBuffer); + if (storeTypes & ALL_STORE) { + BOOST_CHECK(bufferLen > 0); + BOOST_CHECK(certCount > 0); + } else { + BOOST_CHECK_EQUAL(bufferLen, 0); + BOOST_CHECK_EQUAL(certCount, 0); + } + } else { + BOOST_CHECK(!certListBuffer); + BOOST_CHECK_EQUAL(bufferLen, 0); + BOOST_CHECK_EQUAL(certCount, 0); + } + + free(certListBuffer); +} + +void loadCertFromStores(CertStoreType storeType, const std::string &groupName, int expected) +{ + int result; + char *certBlockBuffer = NULL; + size_t bufferLen = 0; + size_t certBlockCount = 0; + + result = loadCertificatesFromStore( + storeType, + groupName.c_str(), + &certBlockBuffer, + &bufferLen, + &certBlockCount); + + BOOST_CHECK_EQUAL(result, expected); + + if (expected == CERTSVC_SUCCESS) { + BOOST_CHECK(certBlockBuffer); + BOOST_CHECK(bufferLen > 0); + BOOST_CHECK(certBlockCount > 0); + } else { + BOOST_CHECK(!certBlockBuffer); + BOOST_CHECK_EQUAL(bufferLen, 0); + BOOST_CHECK_EQUAL(certBlockCount, 0); + } + + free(certBlockBuffer); +} BOOST_AUTO_TEST_SUITE(CERT_SERVER_LOGIC_TEST) @@ -186,4 +252,138 @@ NEGATIVE_TEST_CASE(T_install_certificate_to_store) deinitialize_db(); } +POSITIVE_TEST_CASE(T_get_certificate_list_from_stores) +{ + int result; + + result = initialize_db(); + BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS); + + getCertListFromStores( + CERTSVC_GET_USER_CERTIFICATE_LIST, + StoreType::IndividualStore, + DISABLED, + CERTSVC_SUCCESS); + + getCertListFromStores( + CERTSVC_GET_ROOT_CERTIFICATE_LIST, + ALL_STORE, + DISABLED, + CERTSVC_SUCCESS); + + getCertListFromStores( + CERTSVC_GET_CERTIFICATE_LIST, + ALL_STORE, + DISABLED, + CERTSVC_SUCCESS); + + getCertListFromStores( + CERTSVC_GET_CERTIFICATE_LIST, + ALL_STORE, + ENABLED, + CERTSVC_SUCCESS); + + deinitialize_db(); +} + +NEGATIVE_TEST_CASE(T_negative_get_certificate_list_from_stores) +{ + int result; + + result = initialize_db(); + BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS); + + getCertListFromStores( + CERTSVC_GET_USER_CERTIFICATE_LIST, + SYSTEM_STORE, + DISABLED, + CERTSVC_WRONG_ARGUMENT); + + getCertListFromStores( + CERTSVC_GET_USER_CERTIFICATE_LIST, + StoreType::InvalidStore, + DISABLED, + CERTSVC_SUCCESS); + + getCertListFromStores( + CERTSVC_GET_USER_CERTIFICATE_LIST, + NONE_STORE, + DISABLED, + CERTSVC_SUCCESS); + + deinitialize_db(); +} + +POSITIVE_TEST_CASE(T_load_certificate_from_store) +{ + int result; + + result = initialize_db(); + BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS); + + for (auto storeType: storeTypeMap) + { + const std::string gnameEndUser = + gNamePrexif + storeType.first + std::to_string(P12_END_USER); + + loadCertFromStores(storeType.second, gnameEndUser, CERTSVC_SUCCESS); + } + + deinitialize_db(); +} + +NEGATIVE_TEST_CASE(T_negative_load_certificate_from_store) +{ + int result; + + result = initialize_db(); + BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS); + + loadCertFromStores(NONE_STORE, gNamePrexif, CERTSVC_FAIL); + loadCertFromStores(WIFI_STORE, std::string(), CERTSVC_FAIL); + loadCertFromStores(WIFI_STORE, gNamePrexif, CERTSVC_FAIL); + loadCertFromStores(SYSTEM_STORE, systemGname, CERTSVC_FAIL); + + deinitialize_db(); +} + +NEGATIVE_TEST_CASE(T_negative_load_certificate_from_store_large_group_name) +{ + int result; + char *certBlockBuffer = NULL; + size_t bufferLen = 0; + size_t certBlockCount = 0; + + result = initialize_db(); + BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS); + + result = installCertificateToStore( + WIFI_STORE, + largeString.c_str(), + "commonNameTest", + "gnamePKeyTest", + largeString.c_str(), + PemCertInfo::CertPEM.c_str(), + PEM_CRT); + + BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS); + + result = loadCertificatesFromStore( + WIFI_STORE, + largeString.c_str(), + &certBlockBuffer, + &bufferLen, + &certBlockCount); + + BOOST_CHECK_EQUAL(result, CERTSVC_FAIL); + BOOST_CHECK(!certBlockBuffer); + BOOST_CHECK(bufferLen > 0); + BOOST_CHECK(certBlockCount > 0); + + result = deleteCertificateFromStore(WIFI_STORE, largeString.c_str()); + BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS); + + deinitialize_db(); +} + BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/unit-tests/test_constant.cpp b/unit-tests/test_constant.cpp index 97ca233..e5b36b0 100644 --- a/unit-tests/test_constant.cpp +++ b/unit-tests/test_constant.cpp @@ -272,6 +272,13 @@ namespace ServerCert { const std::string ServerPass = "1234"; } +namespace StoreType { + const CertStoreType IndividualStore = + (CertStoreType)(WIFI_STORE | VPN_STORE | EMAIL_STORE); + + const CertStoreType InvalidStore = (CertStoreType)(1 << 4); +} + const std::map storeTypeMap = { {"vpn_", VPN_STORE}, diff --git a/unit-tests/test_constant.h b/unit-tests/test_constant.h index f604a22..25e9c4a 100644 --- a/unit-tests/test_constant.h +++ b/unit-tests/test_constant.h @@ -85,4 +85,9 @@ namespace ServerCert { extern const std::string ServerPass; } +namespace StoreType { + extern const CertStoreType IndividualStore; + extern const CertStoreType InvalidStore; +} + extern const std::map storeTypeMap; \ No newline at end of file -- 2.7.4