Add unit test for certsvc_message_verify 74/315274/2
authorPhan Xuan Tan <xuan.tan@samsung.com>
Mon, 29 Jul 2024 08:58:57 +0000 (15:58 +0700)
committerDariusz Michaluk <d.michaluk@samsung.com>
Mon, 19 Aug 2024 10:22:51 +0000 (10:22 +0000)
Change-Id: I435e88b56964bcfe76805ab7fc3d0e089aa28dc3

unit-tests/test_constant.cpp
unit-tests/test_constant.h
unit-tests/test_vcore_api_cert.cpp

index 45ab8a3..283d4a7 100644 (file)
@@ -422,3 +422,35 @@ namespace SignatureTest {
 
 const std::string base64Test = "TestBase64";
 const std::string base64DecodeTest = "VGVzdEJhc2U2NA==";
+
+namespace SignatureComponent {
+       const std::string Message = "Q3plZ28gdHUgc3p1a2Fzej8K";
+
+       const std::string DigestSHA1 =
+               "xEIpVjEIUoDkYGtX2ih6Gbya0/gr7OMdvbBKmjqzfNh9GHqwrgjglByeC5sspUzPBUF4Vmg/hZqL"
+               "gSsxXw9bKEa8c6mTQoNX51IC0ELPsoUMIJF1gGdFu0SzKptvU0+ksiiOM+70+s5t8s3z0G5PeA7O"
+               "99oq8UlrX7GDlxaoTU4=";
+
+       const std::string DigestSHA256 =
+               "a5nGT6wnbQ8MLwLkG965E4e1Rv983E+v3nolLvvjuAKnfgWYb+70Da+T9ggYDTjngq+EBgC30w1p"
+               "EScrwye8ELefvRxDWy1+tWR4QRW/Nd4oN2U/pvozoabDSpe9Cvt0ECEOWKDqIYYnoWFjOiXg9VwD"
+               "HVVkQXvsSYu6thX/Xsk=";
+
+       const std::string Cert =
+               "MIIC4zCCAkygAwIBAgIJAMdKgvadG/Z+MA0GCSqGSIb3DQEBBQUAMHIxCzAJBgNV"
+               "BAYTAlBMMQwwCgYDVQQIEwNNYXoxEDAOBgNVBAoTB1NhbXN1bmcxDTALBgNVBAsT"
+               "BFNQUkMxEDAOBgNVBAMTB1NhbXN1bmcxIjAgBgkqhkiG9w0BCQEWE3NhbXN1bmdA"
+               "c2Ftc3VuZy5jb20wHhcNMTExMDA1MTIwMDUxWhcNMjExMDAyMTIwMDUxWjB4MQsw"
+               "CQYDVQQGEwJQTDEMMAoGA1UECBMDTUFaMQwwCgYDVQQHEwNMZWcxDDAKBgNVBAoT"
+               "A1NhbTENMAsGA1UECxMEU1BSQzEOMAwGA1UEAxMFRmlsaXAxIDAeBgkqhkiG9w0B"
+               "CQEWEWZpbGlwQHNhbXN1bmcuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB"
+               "gQDS/sS0wXSCb34ojN8bWFd4Pl9eTLHh18UNGsPpLpp4itdfuc/OgyqaSoDwBzVh"
+               "EWAVLCTxexUa4Ncva+41NbkW4RCsFzeGs0ktpu1+8Q+v0QEOGqVF2rQkgilzDF/o"
+               "O56Fxw9vG1OA+qdQd3yOAV2EqLNBPrEYB9K5GFyffrakSQIDAQABo3sweTAJBgNV"
+               "HRMEAjAAMCwGCWCGSAGG+EIBDQQfFh1PcGVuU1NMIEdlbmVyYXRlZCBDZXJ0aWZp"
+               "Y2F0ZTAdBgNVHQ4EFgQUeyy3iV75KtOkpPFd6mnR9dFGZMwwHwYDVR0jBBgwFoAU"
+               "ggh/2wAChuhTKqX6WK5nfxQ4yGAwDQYJKoZIhvcNAQEFBQADgYEADtv0CBrQ1QCM"
+               "H9jKFjpSpq7zFKMXQeVtb/Zie823//woicg8kxnP5sS4dJWNXNb1iMLdhgV80g1y"
+               "t3gTWPxTtFzprQyNiJHTmrbNWXLX1roRVGUE/I8Q4xexqpbNlJIW2Jjm/kqoKfnK"
+               "xORG6HNPXZV29NY2fDRPPOIYoFQzrXI=";
+}
index b9ac639..82ec4d1 100644 (file)
@@ -134,3 +134,10 @@ namespace SignatureTest {
 
 extern const std::string base64Test;
 extern const std::string base64DecodeTest;
+
+namespace SignatureComponent {
+       extern const std::string Message;
+       extern const std::string DigestSHA1;
+       extern const std::string DigestSHA256;
+       extern const std::string Cert;
+}
\ No newline at end of file
index c9bfb54..2c86539 100644 (file)
@@ -986,4 +986,112 @@ NEGATIVE_TEST_CASE(T_certsvc_certificate_get_visibility_invalid_argument)
        certsvc_instance_free(instance);
 }
 
+POSITIVE_TEST_CASE(T_certsvc_message_verify)
+{
+       CertSvcString msgb64, msg, sig, sigb64, sigSHA256, sigb64SHA256;
+       CertSvcCertificate cert;
+       int status, result;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       result = certsvc_string_new(
+               instance,
+               SignatureComponent::Message.c_str(),
+               SignatureComponent::Message.size(),
+               &msgb64);
+
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       result = certsvc_string_new(
+               instance,
+               SignatureComponent::DigestSHA1.c_str(),
+               SignatureComponent::DigestSHA1.size(),
+               &sigb64);
+
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       result = certsvc_string_new(
+               instance,
+               SignatureComponent::DigestSHA256.c_str(),
+               SignatureComponent::DigestSHA256.size(),
+               &sigb64SHA256);
+
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       makeCertificateFromMemory(instance, SignatureComponent::Cert, cert);
+
+       result = certsvc_base64_decode(msgb64, &msg);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       result = certsvc_base64_decode(sigb64, &sig);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       result = certsvc_base64_decode(sigb64SHA256, &sigSHA256);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       result = certsvc_message_verify(cert, msg, sig, "sha1", &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_SUCCESS);
+
+       result = certsvc_message_verify(cert, msg, sig, NULL, &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_SUCCESS);
+
+       result = certsvc_message_verify(cert, msg, sigSHA256, "sha256", &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_SUCCESS);
+
+       certsvc_string_free(sigSHA256);
+       certsvc_string_free(sigb64SHA256);
+       certsvc_string_free(msgb64);
+       certsvc_string_free(msg);
+       certsvc_string_free(sigb64);
+       certsvc_string_free(sig);
+       certsvc_certificate_free(cert);
+       certsvc_instance_free(instance);
+}
+
+NEGATIVE_TEST_CASE(T_certsvc_message_verify)
+{
+       CertSvcString msgb64, msg, sig;
+       CertSvcCertificate cert;
+       int result, status;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       result = certsvc_message_verify(cert, msg, sig, "sha1", NULL);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+
+       cert.privateHandler = 10;
+       cert.privateInstance = instance;
+
+       result = certsvc_message_verify(cert, msg, sig, "sha1", &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+
+       makeCertificateFromMemory(instance, SignatureComponent::Cert, cert);
+
+       result = certsvc_string_new(
+               instance,
+               SignatureComponent::Message.c_str(),
+               SignatureComponent::Message.size(),
+               &msgb64);
+
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       result = certsvc_base64_decode(msgb64, &msg);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       result = certsvc_message_verify(cert, msg, msg, "shaaaa", &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_INVALID_ALGORITHM);
+
+       result = certsvc_message_verify(cert, msg, msg, "sha1", &status);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       BOOST_CHECK_EQUAL(status, CERTSVC_INVALID_SIGNATURE);
+
+       certsvc_string_free(msgb64);
+       certsvc_string_free(msg);
+       certsvc_certificate_free(cert);
+       certsvc_instance_free(instance);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file