Add unit test for certsvc_certificate_is_signed_by 87/315287/2
authorPhan Xuan Tan <xuan.tan@samsung.com>
Mon, 29 Jul 2024 10:38:47 +0000 (17:38 +0700)
committerDariusz Michaluk <d.michaluk@samsung.com>
Mon, 19 Aug 2024 10:28:51 +0000 (10:28 +0000)
Change-Id: I0d1ab2a8443e69d8c82377d2675fa35cfeb73d12

unit-tests/test_vcore_api_cert.cpp
unit-tests/test_vcore_api_pkcs12.cpp

index 2c86539..b1cca5f 100644 (file)
@@ -1094,4 +1094,79 @@ NEGATIVE_TEST_CASE(T_certsvc_message_verify)
        certsvc_instance_free(instance);
 }
 
+POSITIVE_TEST_CASE(T_certsvc_certificate_is_signed_by)
+{
+       CertSvcCertificate child, parent;
+       int status, result;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       makeCertificateFromFile(
+               instance,
+               CertChain::Signer::Path,
+               child,
+               CertChain::Signer::SubjectCommonName);
+
+       makeCertificateFromFile(
+               instance,
+               CertChain::Intermediate::Path,
+               parent,
+               CertChain::Intermediate::SubjectCommonName);
+
+       result = certsvc_certificate_is_signed_by(child, parent, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_TRUE);
+
+       certsvc_certificate_free(child);
+       certsvc_certificate_free(parent);
+       certsvc_instance_free(instance);
+}
+
+NEGATIVE_TEST_CASE(T_certsvc_certificate_is_signed_by)
+{
+       CertSvcCertificate child, parent;
+       int status, result;
+       CertSvcInstance vinstance;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&vinstance), CERTSVC_SUCCESS);
+
+       child.privateInstance = instance;
+       child.privateHandler = 10;
+       parent.privateInstance = vinstance;
+       parent.privateHandler = 10;
+
+       result = certsvc_certificate_is_signed_by(child, parent, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+
+       parent.privateInstance = instance;
+
+       result = certsvc_certificate_is_signed_by(child, parent, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+
+       makeCertificateFromFile(
+               instance,
+               CertChain::Signer::Path,
+               child,
+               CertChain::Signer::SubjectCommonName);
+
+       result = certsvc_certificate_is_signed_by(child, parent, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+
+       makeCertificateFromFile(
+               instance,
+               CertChain::Intermediate::Path,
+               parent,
+               CertChain::Intermediate::SubjectCommonName);
+
+       result = certsvc_certificate_is_signed_by(parent, child, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_FALSE);
+
+       certsvc_certificate_free(child);
+       certsvc_certificate_free(parent);
+       certsvc_instance_free(instance);
+       certsvc_instance_free(vinstance);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file
index 9f753b2..03477b9 100644 (file)
@@ -379,6 +379,31 @@ void getEvpPrivateKeyFromStore(CertStoreType storeType, CertSvcString gNameStr,i
        }
 }
 
+void loadCertListFromStore(CertSvcCertificateList &certificateList)
+{
+       int result;
+       CertSvcStoreCertList *certList = NULL;
+
+       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);
+
+       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);
+
+       certsvc_string_free(gNameStr);
+       freeCertList(certList);
+}
+
 BOOST_AUTO_TEST_SUITE(VCORE_API_PKCS12_TEST)
 
 POSITIVE_TEST_CASE(T_import_cert_from_to_individual_store)
@@ -921,36 +946,20 @@ NEGATIVE_TEST_CASE(T_certsvc_pkcs12_get_certificate_status_to_store)
 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_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
 
-       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
-       BOOST_CHECK_EQUAL(certificateList.privateHandler, 0);
+       loadCertListFromStore(certificateList);
 
        size_t certCount = 0;
        result = certsvc_certificate_list_get_length(certificateList, &certCount);
        BOOST_CHECK(certCount > 0);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
 
        certsvc_certificate_list_free(certificateList);
-       certsvc_string_free(gNameStr);
-       freeCertList(certList);
        certsvc_instance_free(instance);
 }
 
@@ -1099,6 +1108,53 @@ NEGATIVE_TEST_CASE(T_certsvc_pkcs12_dup_evp_pkey_from_store_invalid_argument)
        certsvc_instance_free(instance);
 }
 
+POSITIVE_TEST_CASE(T_certsvc_certificate_list_get_one)
+{
+       int result;
+
+       CertSvcCertificateList certificateList;
+       certificateList.privateHandler = -1;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       loadCertListFromStore(certificateList);
+
+       CertSvcCertificate cert;
+       cert.privateHandler = -1;
+       result = certsvc_certificate_list_get_one(certificateList, 0, &cert);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(cert.privateHandler, 0);
+
+       certsvc_certificate_free(cert);
+       certsvc_certificate_list_all_free(certificateList);
+       certsvc_instance_free(instance);
+}
+
+NEGATIVE_TEST_CASE(T_certsvc_certificate_list_get_one)
+{
+       int result;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       CertSvcCertificateList certificateList;
+       certificateList.privateHandler = -1;
+       certificateList.privateInstance = instance;
+
+       CertSvcCertificate cert;
+       cert.privateHandler = -1;
+
+       result = certsvc_certificate_list_get_one(certificateList, 0, &cert);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+
+       loadCertListFromStore(certificateList);
+
+       result = certsvc_certificate_list_get_one(certificateList, 100, &cert);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+
+       certsvc_certificate_list_all_free(certificateList);
+       certsvc_instance_free(instance);
+}
+
 POSITIVE_TEST_CASE(T_certsvc_pkcs12_delete_certificate_from_store)
 {
        int result;