Add unit test to get private key from stores 08/315208/1
authorPhan Xuan Tan <xuan.tan@samsung.com>
Fri, 26 Jul 2024 10:06:55 +0000 (17:06 +0700)
committerPhan Xuan Tan <xuan.tan@samsung.com>
Fri, 26 Jul 2024 10:06:55 +0000 (17:06 +0700)
Change-Id: I2c6c0e3d56c15775a999517123296e70b39af86f

unit-tests/test_vcore_api_pkcs12.cpp

index eda7043..d84ce66 100644 (file)
@@ -17,6 +17,7 @@
 #include "cert-svc/ccert.h"
 #include "cert-svc/cinstance.h"
 #include "cert-svc/cpkcs12.h"
+#include "cert-svc/cprimitives.h"
 #include "cert-svc/cstring.h"
 #include "test_constant.h"
 #include "test_common.h"
@@ -329,6 +330,55 @@ void getCertStatusFromStore(
        BOOST_CHECK_EQUAL(result, expected);
 }
 
+bool startsWith(const std::string& str, const std::string& prefix)
+{
+       if (str.size() < prefix.size())
+               return false;
+
+       return str.compare(0, prefix.size(), prefix) == 0;
+}
+
+void getPrivateKeyFromStore(CertStoreType storeType, CertSvcString gNameStr,int expected)
+{
+       int result;
+       char *buffer = NULL;
+       size_t size = 0;
+
+       result = certsvc_pkcs12_private_key_dup_from_store(
+               instance,
+               storeType,
+               gNameStr,
+               &buffer,
+               &size);
+
+       BOOST_CHECK_EQUAL(result, expected);
+
+       if (expected == CERTSVC_SUCCESS) {
+               BOOST_CHECK(buffer);
+               BOOST_CHECK(size > 0);
+               free(buffer);
+       } else {
+               BOOST_CHECK(!buffer);
+               BOOST_CHECK(size == 0);
+       }
+}
+
+void getEvpPrivateKeyFromStore(CertStoreType storeType, CertSvcString gNameStr,int expected)
+{
+       int result;
+       EVP_PKEY *privatekey = NULL;
+       result = certsvc_pkcs12_dup_evp_pkey_from_store(instance, storeType, gNameStr, &privatekey);
+
+       BOOST_CHECK_EQUAL(result, expected);
+
+       if (expected == CERTSVC_SUCCESS) {
+               BOOST_CHECK(privatekey);
+               certsvc_pkcs12_free_evp_pkey(privatekey);
+       } else {
+               BOOST_CHECK(!privatekey);
+       }
+}
+
 BOOST_AUTO_TEST_SUITE(VCORE_API_PKCS12_TEST)
 
 POSITIVE_TEST_CASE(T_import_cert_from_to_individual_store)
@@ -868,6 +918,187 @@ NEGATIVE_TEST_CASE(T_certsvc_pkcs12_get_certificate_status_to_store)
        certsvc_instance_free(instance);
 }
 
+POSITIVE_TEST_CASE(T_certsvc_pkcs12_load_certificate_list_from_store)
+{
+       int result;
+       CertSvcStoreCertList *certList = NULL;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       getCertListFromStore(WIFI_STORE, ENABLED, &certList, CERTSVC_SUCCESS);
+       BOOST_CHECK(certList);
+       BOOST_CHECK_EQUAL(strlen(certList->gname), 40);
+
+       CertSvcString gNameStr = wrapperCertsvcStringNew(certList->gname, &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       CertSvcCertificateList certificateList;
+       certificateList.privateHandler = -1;
+
+       result = certsvc_pkcs12_load_certificate_list_from_store(
+               instance,
+               WIFI_STORE,
+               gNameStr,
+               &certificateList);
+
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(certificateList.privateHandler, 0);
+
+       size_t certCount = 0;
+       result = certsvc_certificate_list_get_length(certificateList, &certCount);
+       BOOST_CHECK(certCount > 0);
+
+       certsvc_certificate_list_free(certificateList);
+       certsvc_string_free(gNameStr);
+       freeCertList(certList);
+       certsvc_instance_free(instance);
+}
+
+NEGATIVE_TEST_CASE(T_certsvc_pkcs12_load_certificate_list_from_invalid_agrument)
+{
+       int result;
+       CertSvcCertificateList certificateList;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       CertSvcString gNameStr = wrapperCertsvcStringNew(GroupNameTest, &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       result = certsvc_pkcs12_load_certificate_list_from_store(
+               instance,
+               NONE_STORE,
+               gNameStr,
+               &certificateList);
+
+       BOOST_CHECK_EQUAL(result, CERTSVC_INVALID_STORE_TYPE);
+
+       result = certsvc_pkcs12_load_certificate_list_from_store(
+               instance,
+               WIFI_STORE,
+               gNameStr,
+               &certificateList);
+
+       BOOST_CHECK_EQUAL(result, CERTSVC_FAIL);
+
+       certsvc_string_free(gNameStr);
+       certsvc_instance_free(instance);
+}
+
+POSITIVE_TEST_CASE(T_certsvc_pkcs12_private_key_dup_from_individual_store)
+{
+       int result;
+       CertSvcStoreCertList *certList = NULL;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       getCertListFromStore(WIFI_STORE, ENABLED, &certList, CERTSVC_SUCCESS);
+       BOOST_CHECK(certList);
+       BOOST_CHECK_EQUAL(strlen(certList->gname), 40);
+
+       while (certList)
+       {
+               if (startsWith(certList->title, P12Prefix))
+               {
+                       CertSvcString gNameStr = wrapperCertsvcStringNew(certList->gname, &result);
+                       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+                       getPrivateKeyFromStore(WIFI_STORE, gNameStr, CERTSVC_SUCCESS);
+
+                       certsvc_string_free(gNameStr);
+               }
+
+               certList = certList->next;
+       }
+
+       certsvc_instance_free(instance);
+}
+
+POSITIVE_TEST_CASE(T_certsvc_pkcs12_private_key_dup_from_system_store)
+{
+       int result;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       CertSvcString gNameSystemStr = wrapperCertsvcStringNew(GroupNameSystem, &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       getPrivateKeyFromStore(SYSTEM_STORE, gNameSystemStr, CERTSVC_SUCCESS);
+
+       certsvc_string_free(gNameSystemStr);
+       certsvc_instance_free(instance);
+}
+
+NEGATIVE_TEST_CASE(T_certsvc_pkcs12_private_key_dup_from_store_invalid_argument)
+{
+       int result;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       CertSvcString gNameStr = wrapperCertsvcStringNew(GroupNameTest, &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       CertSvcString invalidNameStr = wrapperCertsvcStringNew(std::string(), &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       getPrivateKeyFromStore(NONE_STORE, gNameStr, CERTSVC_INVALID_STORE_TYPE);
+       getPrivateKeyFromStore(WIFI_STORE, gNameStr, CERTSVC_FAIL);
+       getPrivateKeyFromStore(SYSTEM_STORE, gNameStr, CERTSVC_FAIL);
+       getPrivateKeyFromStore(SYSTEM_STORE, invalidNameStr, CERTSVC_WRONG_ARGUMENT);
+
+       certsvc_string_free(gNameStr);
+       certsvc_instance_free(instance);
+}
+
+POSITIVE_TEST_CASE(T_certsvc_pkcs12_dup_evp_pkey_from_store)
+{
+       int result;
+       CertSvcStoreCertList *certList = NULL;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       getCertListFromStore(WIFI_STORE, ENABLED, &certList, CERTSVC_SUCCESS);
+       BOOST_CHECK(certList);
+       BOOST_CHECK_EQUAL(strlen(certList->gname), 40);
+
+       while (certList)
+       {
+               if (startsWith(certList->title, P12Prefix))
+               {
+                       CertSvcString gNameStr = wrapperCertsvcStringNew(certList->gname, &result);
+                       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+                       getEvpPrivateKeyFromStore(WIFI_STORE, gNameStr, CERTSVC_SUCCESS);
+
+                       certsvc_string_free(gNameStr);
+               }
+
+               certList = certList->next;
+       }
+
+       certsvc_instance_free(instance);
+}
+
+NEGATIVE_TEST_CASE(T_certsvc_pkcs12_dup_evp_pkey_from_store_invalid_argument)
+{
+       int result;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       CertSvcString gNameStr = wrapperCertsvcStringNew(GroupNameTest, &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       CertSvcString invalidNameStr = wrapperCertsvcStringNew(std::string(), &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       getEvpPrivateKeyFromStore(NONE_STORE, gNameStr, CERTSVC_INVALID_STORE_TYPE);
+       getEvpPrivateKeyFromStore(WIFI_STORE, gNameStr, CERTSVC_FAIL);
+       getEvpPrivateKeyFromStore(SYSTEM_STORE, gNameStr, CERTSVC_FAIL);
+       getEvpPrivateKeyFromStore(SYSTEM_STORE, invalidNameStr, CERTSVC_WRONG_ARGUMENT);
+
+       certsvc_string_free(gNameStr);
+       certsvc_instance_free(instance);
+}
+
 POSITIVE_TEST_CASE(T_certsvc_pkcs12_delete_certificate_from_store)
 {
        int result;