Add test case for update_ca_certificate_file in cert-server-logic.c 94/315594/10
authortranthanhtung2001 <tran.tung@samsung.com>
Mon, 5 Aug 2024 05:39:58 +0000 (12:39 +0700)
committerDariusz Michaluk <d.michaluk@samsung.com>
Tue, 20 Aug 2024 07:44:27 +0000 (07:44 +0000)
Change-Id: I8b6a02542774ea98353beef215c54d824022d413
Signed-off-by: tranthanhtung2001 <tran.tung@samsung.com>
unit-tests/test_cert_server_logic.cpp

index d20c0e9..1a772fa 100644 (file)
@@ -14,6 +14,8 @@
  *    limitations under the License.
  */
 
+#include <string>
+
 #include "cert-svc/ccert.h"
 #include "cert-svc/cerror.h"
 #include "test_common.h"
@@ -99,6 +101,38 @@ void loadCertFromStores(CertStoreType storeType, const std::string &groupName, i
        free(certBlockBuffer);
 }
 
+void installCertToStore(
+       std::string privateKeyGname,
+       std::string associatedGname,
+       std::string dataBlock,
+       CertType certType)
+{
+       int result;
+       for(auto storeType: storeTypeMap)
+       {
+               result = installCertificateToStore(
+                       storeType.second,
+                       gNamePrefix.c_str(),
+                       commonNamePrefix.c_str(),
+                       privateKeyGname.empty() ? NULL : privateKeyGname.c_str(),
+                       associatedGname.empty() ? NULL : associatedGname.c_str(),
+                       dataBlock.empty() ? NULL : dataBlock.c_str(),
+                       certType);
+
+               BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       }
+}
+
+void uninstallCertFromStore()
+{
+       int result;
+       for(auto storeType: storeTypeMap)
+       {
+               result = deleteCertificateFromStore(storeType.second, gNamePrefix.c_str());
+               BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       }
+}
+
 BOOST_AUTO_TEST_SUITE(CERT_SERVER_LOGIC_TEST)
 
 POSITIVE_TEST_CASE(T_install_certificate_to_store)
@@ -421,28 +455,11 @@ POSITIVE_TEST_CASE(T_delete_certificate_same_gname_from_stores)
        result = initialize_db();
        BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
 
-       for(auto storeType: storeTypeMap)
-       {
-               result = installCertificateToStore(
-                       storeType.second,
-                       gNamePrefix.c_str(),
-                       commonNamePrefix.c_str(),
-                       NULL,
-                       NULL,
-                       PemCertInfo::CertPEM.c_str(),
-                       PEM_CRT);
-
-               BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
-       }
+       installCertToStore(std::string(), std::string(), PemCertInfo::CertPEM, PEM_CRT);
 
-       for(auto storeType: storeTypeMap)
-       {
-               result = deleteCertificateFromStore(storeType.second, gNamePrefix.c_str());
-               BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
-       }
+       uninstallCertFromStore();
 
        deinitialize_db();
-
 }
 
 NEGATIVE_TEST_CASE(T_delete_certificate_from_store_error)
@@ -470,4 +487,29 @@ NEGATIVE_TEST_CASE(T_delete_certificate_from_store_error)
        BOOST_CHECK_EQUAL(result, CERTSVC_FAIL);
 }
 
-BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file
+POSITIVE_TEST_CASE(T_update_ca_certificate_file)
+{
+       int result = update_ca_certificate_file(const_cast<char*>(PemCertInfo::CertPEM.c_str()));
+
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+}
+
+NEGATIVE_TEST_CASE(T_update_ca_certificate_file_with_parameter_cert_is_null)
+{
+       int result = update_ca_certificate_file(NULL);
+       BOOST_CHECK_EQUAL(result, CERTSVC_WRONG_ARGUMENT);
+
+       result = initialize_db();
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       installCertToStore(std::string(), std::string(), PemCertInfo::CertPEM, PEM_CRT);
+
+       result = update_ca_certificate_file(NULL);
+
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       uninstallCertFromStore();
+       deinitialize_db();
+}
+
+BOOST_AUTO_TEST_SUITE_END()