Add unite tests for certsvc_certificate_verify and certsvc_certificate_verify_with_caflag 49/315249/2
authorPhan Xuan Tan <xuan.tan@samsung.com>
Mon, 29 Jul 2024 05:27:35 +0000 (12:27 +0700)
committerDariusz Michaluk <d.michaluk@samsung.com>
Mon, 19 Aug 2024 10:06:58 +0000 (12:06 +0200)
Change-Id: Ia2c8caa0b6c5f5d0a4b4f90a2bb37f6c33e19e80

unit-tests/test_vcore_api_cert.cpp

index 813fb4d..036d2bb 100644 (file)
@@ -797,4 +797,134 @@ NEGATIVE_TEST_CASE(T_certsvc_certificate_dup_pubkey_der_wrong_argument)
        certsvc_instance_free(instance);
 }
 
+POSITIVE_TEST_CASE(T_certsvc_certificate_verify)
+{
+       const int MAXC = 3;
+       CertSvcCertificate certs[MAXC];
+       int result;
+       int status;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       makeCertificateFromFile(
+               instance,
+               CertChain::Signer::Path,
+               certs[0],
+               CertChain::Signer::SubjectCommonName);
+
+       makeCertificateFromFile(
+               instance,
+               CertChain::Intermediate::Path,
+               certs[1],
+               CertChain::Intermediate::SubjectCommonName);
+
+       makeCertificateFromFile(
+               instance,
+               CertChain::Root::Path,
+               certs[2],
+               CertChain::Root::SubjectCommonName);
+
+       result = certsvc_certificate_verify(certs[0], &certs[1], MAXC - 1, NULL, 0, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_SUCCESS);
+
+       result = certsvc_certificate_verify(certs[0], certs, MAXC - 1, NULL, 0, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_FAIL);
+
+       result = certsvc_certificate_verify(certs[0], certs, 1, certs, MAXC, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_FAIL);
+
+       result = certsvc_certificate_verify(certs[0], &certs[2], 1, certs, MAXC, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_SUCCESS);
+
+       result = certsvc_certificate_verify_with_caflag(certs[0], certs, MAXC, NULL, 0, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_SUCCESS);
+
+       result = certsvc_certificate_verify_with_caflag(certs[0], &certs[2], 1, certs, MAXC, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_SUCCESS);
+
+       result = certsvc_certificate_verify_with_caflag(certs[0], certs, MAXC - 1, NULL, 0, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_FAIL);
+
+       result = certsvc_certificate_verify_with_caflag(certs[0], certs, 1, certs, MAXC,&status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_FAIL);
+
+       for (int i = 0; i < MAXC; i++)
+               certsvc_certificate_free(certs[i]);
+
+       certsvc_instance_free(instance);
+}
+
+NEGATIVE_TEST_CASE(T_certsvc_certificate_verify_invalid_argument)
+{
+       const int MAXC = 3;
+       CertSvcCertificate certs[MAXC];
+       CertSvcCertificate cert;
+       int result;
+       int status = -1;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       makeCertificateFromFile(
+               instance,
+               CertChain::Signer::Path,
+               cert,
+               CertChain::Signer::SubjectCommonName);
+
+       certs[0].privateHandler = 10;
+       certs[0].privateInstance = instance;
+       certs[1].privateHandler = 11;
+       certs[1].privateInstance = instance;
+       certs[2].privateHandler = 12;
+       certs[2].privateInstance = instance;
+
+       result = certsvc_certificate_verify(certs[0], NULL, MAXC - 1, NULL, 0, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+       BOOST_CHECK_EQUAL(status, -1);
+
+       result = certsvc_certificate_verify(certs[0], &certs[1], MAXC - 1, NULL, 0, NULL);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+
+       result = certsvc_certificate_verify(certs[0], &certs[1], MAXC - 1, NULL, 0, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+       BOOST_CHECK_EQUAL(status, -1);
+
+       result = certsvc_certificate_verify(cert, &certs[1], MAXC - 1, NULL, 0, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+       BOOST_CHECK_EQUAL(status, -1);
+
+       result = certsvc_certificate_verify(cert, &cert, 1, &certs[1], MAXC - 1, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+       BOOST_CHECK_EQUAL(status, -1);
+
+       result = certsvc_certificate_verify_with_caflag(certs[0], NULL, MAXC - 1, NULL, 0, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+       BOOST_CHECK_EQUAL(status, -1);
+
+       result = certsvc_certificate_verify_with_caflag(certs[0], &certs[1], MAXC - 1, NULL, 0, NULL);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+
+       result = certsvc_certificate_verify_with_caflag(certs[0], &certs[1], MAXC - 1, NULL, 0, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+       BOOST_CHECK_EQUAL(status, -1);
+
+       result = certsvc_certificate_verify_with_caflag(cert, &certs[1], MAXC - 1, NULL, 0, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+       BOOST_CHECK_EQUAL(status, -1);
+
+       result = certsvc_certificate_verify_with_caflag(cert, &cert, 1, &certs[1], MAXC - 1, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+       BOOST_CHECK_EQUAL(status, -1);
+
+       certsvc_certificate_free(cert);
+       certsvc_instance_free(instance);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file