Add positve and negative test cases to import cert file to individual stores 95/314095/8
authorPhan Xuan Tan <xuan.tan@samsung.com>
Fri, 5 Jul 2024 06:24:51 +0000 (13:24 +0700)
committerPhan Xuan Tan <xuan.tan@samsung.com>
Fri, 12 Jul 2024 03:54:38 +0000 (10:54 +0700)
Change-Id: I0088aaa3d6ad0a6ae4a743aeec793019aec0463f

19 files changed:
unit-tests/CMakeLists.txt
unit-tests/data/cert_chain.p12 [new file with mode: 0644]
unit-tests/data/cert_chain.pem [new file with mode: 0644]
unit-tests/data/cert_chain.pfx [new file with mode: 0755]
unit-tests/data/cert_with_pass.crt [new file with mode: 0755]
unit-tests/data/cert_with_pass.p12 [new file with mode: 0755]
unit-tests/data/cert_with_pass.pfx [new file with mode: 0755]
unit-tests/data/cert_without_pass.p12 [new file with mode: 0755]
unit-tests/data/empty_cert.pem [new file with mode: 0755]
unit-tests/data/end_user.p12 [new file with mode: 0644]
unit-tests/data/invalid_cert.p12 [new file with mode: 0755]
unit-tests/data/invalid_cert.pem [new file with mode: 0755]
unit-tests/data/trusted_cert.pem [new file with mode: 0644]
unit-tests/test_cert_server_logic.cpp
unit-tests/test_common.cpp
unit-tests/test_common.h
unit-tests/test_constant.cpp
unit-tests/test_constant.h
unit-tests/test_vcore_api_pkcs12.cpp [new file with mode: 0644]

index 49f336eaa4a16993f4ae62a1267cd5eeee9cff8e..93d66c60fb00099f45131e96daa3246904e7f764 100644 (file)
@@ -67,6 +67,7 @@ SET(UNIT_TESTS_SOURCES
     test_vcore_time_conversion.cpp
     test_vcore_certificate_collection.cpp
     test_vcore_api_cert.cpp
+    test_vcore_api_pkcs12.cpp
     test_cert_server_db.cpp
     test_cert_server_logic.cpp
     colour_log_formatter.cpp
@@ -109,7 +110,19 @@ INSTALL(TARGETS ${TARGET_UNIT_TESTS} DESTINATION ${BIN_DIR})
 
 INSTALL(
     FILES
+        data/cert_chain.p12
+        data/cert_chain.pem
+        data/cert_chain.pfx
+        data/cert_with_pass.crt
+        data/cert_with_pass.p12
+        data/cert_with_pass.pfx
+        data/cert_without_pass.p12
+        data/empty_cert.pem
+        data/end_user.p12
+        data/invalid_cert.p12
+        data/invalid_cert.pem
         data/server_without_pass_cert.pem
         data/signer_cert.pem
+        data/trusted_cert.pem
     DESTINATION ${CERT_SVC_TESTS}/unit_test_data
 )
\ No newline at end of file
diff --git a/unit-tests/data/cert_chain.p12 b/unit-tests/data/cert_chain.p12
new file mode 100644 (file)
index 0000000..0d49074
Binary files /dev/null and b/unit-tests/data/cert_chain.p12 differ
diff --git a/unit-tests/data/cert_chain.pem b/unit-tests/data/cert_chain.pem
new file mode 100644 (file)
index 0000000..e5ce4f3
--- /dev/null
@@ -0,0 +1,55 @@
+-----BEGIN CERTIFICATE-----
+MIIC7jCCAlegAwIBAgIBADANBgkqhkiG9w0BAQsFADB9MQswCQYDVQQGEwJLUjEO
+MAwGA1UECAwFU2VvdWwxEDAOBgNVBAoMB1NhbXN1bmcxEzARBgNVBAsMClRpemVu
+IFRlc3QxGjAYBgNVBAMMEVRlc3QgSW50ZXJtZWRpYXRlMRswGQYJKoZIhvcNAQkB
+Fgx0dEBnbWFpbC5jb20wIBcNMjQwNjEwMTExMjM1WhgPOTk5OTEyMzExMTEyMzVa
+MHUxCzAJBgNVBAYTAktSMQ4wDAYDVQQIDAVTZW91bDEQMA4GA1UECgwHU2Ftc3Vu
+ZzETMBEGA1UECwwKVGl6ZW4gVGVzdDESMBAGA1UEAwwJVGVzdCBMZWFmMRswGQYJ
+KoZIhvcNAQkBFgx0dEBnbWFpbC5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ
+AoGBANXDr7WS0uJSI1hXiAiHmfjLqTB8KeMKpsAVAdmgNIHRzvxums7zpyXgTFBU
+8R+tok/drRQTW+tGZwxphDnucq2MmjlW9BHX58GryDj4gHXyh2prWKmEMNfjo/Bm
+hjzEvjs0+p/7lC8kHH+pOSIqm4ySkWg2nFsS1ocf98QcHEq7AgMBAAGjgYMwgYAw
+cwYIKwY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LQIDAQABo4HJMIHGMB0GA1UdDgQW
+BBRuGerDSPswyii/2vKCRXOTqHLiODAfBgNVHSMEGDAWgBRuGerDSPswyii/2vKC
+RXOTqHLiODAPBgNVHRMBAf8EBTADAQH/MHMGCCsGAQUFBwEBBGcwZTAhBggrBgEF
+BQcwAYYVaHR0cDovLzEyNy4wLjAuMTo4ODg4MEAGCCsGAQUFBzAChjRodHRwOi8v
+U1ZSU2VjdXJlLUczLWFpYS52ZXJpc2lnbi5jb20vU1ZSU2VjdXJlRzMuY2VyMA0G
+CSqGSIb3DQEBCwUAA4GBAMdMoU5L6gxxpguiUpxhI6K+1yXBidT5LuHbT95ehm+m
+mUsDyhuUcTSTAGroIRkhyVtYYpSe2phgey8MwKAFCri+b/wX9aw45br6wb0vbKXd
+wf5EkzG55FICFclNgRr9nLSVGSzLfKnTVZy87VhofNEutmetWpSRN5YaPxQLRMXt
+-----END CERTIFICATE-----
diff --git a/unit-tests/data/cert_chain.pfx b/unit-tests/data/cert_chain.pfx
new file mode 100755 (executable)
index 0000000..d6d15fa
Binary files /dev/null and b/unit-tests/data/cert_chain.pfx differ
diff --git a/unit-tests/data/cert_with_pass.crt b/unit-tests/data/cert_with_pass.crt
new file mode 100755 (executable)
index 0000000..b468af2
--- /dev/null
@@ -0,0 +1,20 @@
+-----BEGIN CERTIFICATE-----
+MIIDWTCCAkECFGv8pGrsRZAYhh5cyPbG4Np4I0gFMA0GCSqGSIb3DQEBCwUAMGkx
+CzAJBgNVBAYTAlZOMQ8wDQYDVQQIDAZIYSBOb2kxFDASBgNVBAcMC0hhIE5vaSBD
+aXR5MRAwDgYDVQQKDAdTYW1zdW5nMREwDwYDVQQLDAhTZWN1cml0eTEOMAwGA1UE
+AwwFVGl6ZW4wHhcNMjQwNDI0MDg0NDA3WhcNMjUwNDI0MDg0NDA3WjBpMQswCQYD
+VQQGEwJWTjEPMA0GA1UECAwGSGEgTm9pMRQwEgYDVQQHDAtIYSBOb2kgQ2l0eTEQ
+MA4GA1UECgwHU2Ftc3VuZzERMA8GA1UECwwIU2VjdXJpdHkxDjAMBgNVBAMMBVRp
+emVuMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwDj0drnSDuIxqiq2
+MCIUsvoBtEp9Q8Ad1EoqO04EMJkh5yh8XhaPRPfC3wPowF0EFSm+SaQvwcI9Oipr
+81nhcHR7HgPYsGUxtunnjqrdOho1lop1Q/hCStSHcUdLuqoIvTdkyXMSRe4O3PXi
+mgmuBTfck7reVJlefGnYLV3KMg5D6BcM1WMAPfODIy3fPnGpLfnALPPyKdS5AyCI
+Q56VSa0+LBR3IfpU3EHacStc3Yp0rOXIImSMy/4diQdIZBKki2MvdUkYtkRocwCd
+1Yf6jFkTMox0mWf1zwrpZ4GZYiz27/xO8/MxGeqRL5hTGfn3IRoTC4BZRVN3pfQW
+5GVPOwIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQCuHw9Td/6T3K57nnDDZyDm5GBL
+0e5jz5p7woCDn4T5sEc3neqzxQPOLkQEX7YD1Zie6GbXSKf1cIfDEhj5sHSqXN8+
++MxO6l7r781XpTvkhjQQ1zeZ34jxA5fgaDGOv/LEMEBax8CL0MvueTbjJJ77k/WR
+DoaVMRZE3kNFZ1lQWVOpCTSHIrS3Obb0HKhZac0RKo81OI8xjpqQCbaojd8ABDqK
+ljZcXeqye/DY3fCyYHfZAkbc9KEjItjQpqK0NULrXM+2meYWkbD2mSIb/tkrqa5V
+K/4/biX4QdhvBMmlwIItT10OwPgd1eoOh2LWDh8Gp1vGedJCw1tbUOHRAj+9
+-----END CERTIFICATE-----
diff --git a/unit-tests/data/cert_with_pass.p12 b/unit-tests/data/cert_with_pass.p12
new file mode 100755 (executable)
index 0000000..c71c6cc
Binary files /dev/null and b/unit-tests/data/cert_with_pass.p12 differ
diff --git a/unit-tests/data/cert_with_pass.pfx b/unit-tests/data/cert_with_pass.pfx
new file mode 100755 (executable)
index 0000000..df226ff
Binary files /dev/null and b/unit-tests/data/cert_with_pass.pfx differ
diff --git a/unit-tests/data/cert_without_pass.p12 b/unit-tests/data/cert_without_pass.p12
new file mode 100755 (executable)
index 0000000..b9c3eaf
Binary files /dev/null and b/unit-tests/data/cert_without_pass.p12 differ
diff --git a/unit-tests/data/empty_cert.pem b/unit-tests/data/empty_cert.pem
new file mode 100755 (executable)
index 0000000..e69de29
diff --git a/unit-tests/data/end_user.p12 b/unit-tests/data/end_user.p12
new file mode 100644 (file)
index 0000000..b2ad881
Binary files /dev/null and b/unit-tests/data/end_user.p12 differ
diff --git a/unit-tests/data/invalid_cert.p12 b/unit-tests/data/invalid_cert.p12
new file mode 100755 (executable)
index 0000000..c14436f
Binary files /dev/null and b/unit-tests/data/invalid_cert.p12 differ
diff --git a/unit-tests/data/invalid_cert.pem b/unit-tests/data/invalid_cert.pem
new file mode 100755 (executable)
index 0000000..e095078
--- /dev/null
@@ -0,0 +1,10 @@
+-----BEGIN-----
+MIIDszCCApugAwIBAgIULSnx++ZVeyQiJm2Lza1P1mOy4d8wDQYJKoZIhvcNAQEL
+BQAwaTELMAkGA1UEBhMCVk4xEDAOBgNVBAgMB1ZpZXROYW0xDzANBgNVBAcMBkhh
+IE5vaTEQMA4GA1UECgwHU2Ftc3VuZzERMA8GA1UECwwIU2VjdXJpdHkxEjAQBgNV
+BAMMCVRpemVuIFNFUzAeFw0yNDA0MjQwODIyMzNaFw0zNDA0MjIwODIyMzNaMGkx
+CzAJBgNVBAYTAlZOMRAwDgYDVQQIDAdWaWV0TmFtMQ8wDQYDVQQHDAZIYSBOb2kx
+EDAOBgNVBAoMB1NhbXN1bmcxETAPBgNVBAsMCFNlY3VyaXR5MRIwEAYDVQQDDAlU
+aXplbiBTRVMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDWOmaGLi6Y
+x2WYbRP48xavVVwrNil/8oEYXTGBepfdJTPLmFLDZOd0GhBReyEAj8Q/CnMWXf77
+-----END-----
diff --git a/unit-tests/data/trusted_cert.pem b/unit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B8GA1UdIwQY
+MBaAFCXdQbbfxClyJAQcBkQTZd6lB+c6MA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZI
+hvcNAQELBQADggEBAKUXWdxIPdnHv60UHhi05Q3IMlE2jLZqInrL7A02PC6JE9Ra
+6Ou1be1tUgUO0U+ahdPohoLi1lTHnhE50T/o9j2shixM2xP+JYS2qpXNdBJjj7QQ
+0wNZbV5Z/ZtBMO/icI2HaklJOdmk7FavaIEsylNNje1GFOMLNGT33v3tE8VUyv9Z
+YtocqrgrGYyR8VShkPrr8HVeSuLtxFvFXHflUH5U0lPTuoxcZTbyplanWOsOEMIM
+N3f+OG22Ad+A0Dm7uXRosRec7iOC/sq29r2BtK1Nv+v5JJEIt2CQIOXmRj0R0gKx
+NumJAIm3njgfGfpkLi5nbHSMocQ0JYNi0V/121owDDAKBggrBgEFBQcDAQ==
+-----END TRUSTED CERTIFICATE-----
index e296220953f81f95d8d2e133eea1b17b9065d09b..482e001a34f8c941a39b9f01448dfb8fd2406438 100644 (file)
@@ -32,13 +32,6 @@ extern "C"
 static const std::string gNamePrexif = "gname_sever_";
 static const std::string commonNamePrefix = "TEST_sever_";
 
-const std::map<std::string, CertStoreType> storeTypeMap =
-{
-       {"vpn_", VPN_STORE},
-       {"email_", EMAIL_STORE},
-       {"wifi_", WIFI_STORE}
-};
-
 
 BOOST_AUTO_TEST_SUITE(CERT_SERVER_LOGIC_TEST)
 
index 16c0dadcb9dd08dc19a18755af5ebd44ce4a0e8c..e90db53e47c2e885f032f6d53f942e510b7528a7 100644 (file)
@@ -48,4 +48,9 @@ int convertStringToBytes(const std::string hexString, unsigned char **content)
        *content = bytes;
 
        return CERTSVC_SUCCESS;
-}
\ No newline at end of file
+}
+
+std::string createLargeString(size_t size)
+{
+       return std::string(size, 'a');
+}
index 37482ab69be3a92dd9a52507ba381354377a8302..7ab51342fd47c3d88c82b73c946990e02ed1623b 100644 (file)
@@ -16,4 +16,5 @@
 
 #include <string>
 
-int convertStringToBytes(const std::string hexString, unsigned char **content);
\ No newline at end of file
+int convertStringToBytes(const std::string hexString, unsigned char **content);
+std::string createLargeString(size_t size);
\ No newline at end of file
index 4630613e27f395a4425e7b7b43c886d73e75da9a..97ca233fe0aa3b7cbdc82841844d146b2d6b689b 100644 (file)
@@ -14,7 +14,9 @@
  *    limitations under the License.
  */
 
+#include "cert-svc/ccert.h"
 #include "test_constant.h"
+#include <map>
 
 namespace ServerCertInfo {
        const std::string ServerCertPemWithoutPassPath =
@@ -226,3 +228,53 @@ namespace SignerCertInfo {
        unsigned long TimeNotBefore = 1720084024;
        unsigned long TimeNotAfter = 1751620024;
 }
+
+namespace ServerCert {
+       const std::string PemWithoutPassPath =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/server_without_pass_cert.pem";
+
+       const std::string TrustedPemPath =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/trusted_cert.pem";
+
+       const std::string PemChainPath =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/cert_chain.pem";
+
+       const std::string CrtWithPassPath =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/cert_with_pass.crt";
+
+       const std::string P12WithoutPassPath =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/cert_without_pass.p12";
+
+       const std::string P12WithPassPath =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/cert_with_pass.p12";
+
+       const std::string P12ChainPath =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/cert_chain.p12";
+
+       const std::string P12EndUserPath =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/end_user.p12";
+
+       const std::string PfxWithPassPath =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/cert_with_pass.pfx";
+
+       const std::string PfxChainPath =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/cert_chain.pfx";
+
+       const std::string EmptyCert =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/empty_cert.pem";
+
+       const std::string PemInvalidCert =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/invalid_cert.pem";
+
+       const std::string P12InvalidCert =
+               std::string(TESTAPP_RES_DIR) + "unit_test_data/invalid_cert.p12";
+
+       const std::string ServerPass = "1234";
+}
+
+const std::map<std::string, CertStoreType> storeTypeMap =
+{
+       {"vpn_", VPN_STORE},
+       {"email_", EMAIL_STORE},
+       {"wifi_", WIFI_STORE}
+};
index ef31b1b21d5fe3bd6b1a75bf15315edfc168571f..f604a22999d0d3cacb60f6b749a4286f23cf9d53 100644 (file)
@@ -14,7 +14,9 @@
  *    limitations under the License.
  */
 
+#include "cert-svc/ccert.h"
 #include <string>
+#include <map>
 
 namespace ServerCertInfo {
        extern const std::string ServerCertPemWithoutPassPath;
@@ -65,3 +67,22 @@ namespace SignerCertInfo {
        extern unsigned long TimeNotBefore;
        extern unsigned long TimeNotAfter;
 }
+
+namespace ServerCert {
+       extern const std::string PemWithoutPassPath;
+       extern const std::string TrustedPemPath;
+       extern const std::string PemChainPath;
+       extern const std::string CrtWithPassPath;
+       extern const std::string P12WithoutPassPath;
+       extern const std::string P12WithPassPath;
+       extern const std::string P12ChainPath;
+       extern const std::string P12EndUserPath;
+       extern const std::string PfxWithPassPath;
+       extern const std::string PfxChainPath;
+       extern const std::string EmptyCert;
+       extern const std::string PemInvalidCert;
+       extern const std::string P12InvalidCert;
+       extern const std::string ServerPass;
+}
+
+extern const std::map<std::string, CertStoreType> storeTypeMap;
\ No newline at end of file
diff --git a/unit-tests/test_vcore_api_pkcs12.cpp b/unit-tests/test_vcore_api_pkcs12.cpp
new file mode 100644 (file)
index 0000000..3d0dfd0
--- /dev/null
@@ -0,0 +1,333 @@
+/*
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include "cert-svc/ccert.h"
+#include "cert-svc/cinstance.h"
+#include "cert-svc/cpkcs12.h"
+#include "cert-svc/cstring.h"
+#include "test_constant.h"
+#include "test_common.h"
+#include "test_macros.h"
+#include <cstring>
+#include <openssl/x509.h>
+
+
+static CertSvcInstance instance;
+static const std::string PemPrefix = "PEM_";
+static const std::string CrtPrefix = "CRT_";
+static const std::string P12Prefix = "P12_";
+static const std::string PfxPrefix = "PFX_";
+static const std::string Trusted = "TRUSTED_";
+static const std::string Chain = "CHAIN_";
+static const std::string WithPass = "PASS_";
+static const std::string EndUser = "END_USER_";
+static const std::string RetList = "RL_";
+
+CertSvcString wrapperCertsvcStringNew(std::string originStr, int *retVal)
+{
+       CertSvcString certsvcStr;
+
+       if (originStr.empty())
+               *retVal = certsvc_string_new(instance, NULL, 0, &certsvcStr);
+       else
+               *retVal = certsvc_string_new(instance, originStr.c_str(), originStr.length(), &certsvcStr);
+
+       return certsvcStr;
+}
+
+void importCertFromFileToStore(
+       CertStoreType storeType,
+       std::string path,
+       std::string pass,
+       std::string alias,
+       int expected)
+{
+       int result;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       CertSvcString cStrPath = wrapperCertsvcStringNew(path, &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       CertSvcString cStrPass = wrapperCertsvcStringNew(pass, &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       CertSvcString cStrAlias = wrapperCertsvcStringNew(alias, &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       result = certsvc_pkcs12_import_from_file_to_store(
+               instance,
+               storeType,
+               cStrPath,
+               cStrPass,
+               cStrAlias);
+
+       BOOST_CHECK_EQUAL(result, expected);
+
+       certsvc_string_free(cStrPath);
+       certsvc_string_free(cStrPass);
+       certsvc_string_free(cStrAlias);
+       certsvc_instance_free(instance);
+}
+
+void importCertFromFileToStoreRetList(
+       CertStoreType storeType,
+       std::string path,
+       std::string pass,
+       std::string alias,
+       bool isLen,
+       int expected)
+{
+       int result;
+       CertSvcStoreCertList *certList = NULL;
+       size_t length = 0;
+
+       BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+       CertSvcString cStrPath = wrapperCertsvcStringNew(path, &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       CertSvcString cStrPass = wrapperCertsvcStringNew(pass, &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       CertSvcString cStrAlias = wrapperCertsvcStringNew(alias, &result);
+       BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+       result = certsvc_pkcs12_import_from_file_to_store_ret_list(
+               instance,
+               storeType,
+               cStrPath,
+               cStrPass,
+               cStrAlias,
+               &certList,
+               isLen ? &length : nullptr);
+
+       BOOST_CHECK_EQUAL(result, expected);
+
+       if (expected == CERTSVC_SUCCESS) {
+               BOOST_CHECK(certList);
+               BOOST_CHECK(length > 0);
+               result = certsvc_pkcs12_free_certificate_list_loaded_from_store(instance, &certList);
+               BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+       }
+
+       certsvc_string_free(cStrPath);
+       certsvc_string_free(cStrPass);
+       certsvc_string_free(cStrAlias);
+       certsvc_instance_free(instance);
+}
+
+BOOST_AUTO_TEST_SUITE(VCORE_API_PKCS12_TEST)
+
+POSITIVE_TEST_CASE(T_import_cert_from_to_individual_store)
+{
+       std::string path, pass, alias;
+       pass = ServerCert::ServerPass;
+
+       for (auto storeType: storeTypeMap)
+       {
+               path = ServerCert::PemWithoutPassPath;
+               alias = PemPrefix + storeType.first + std::to_string(PEM_CRT);
+               importCertFromFileToStore(storeType.second, path, std::string(), alias, CERTSVC_SUCCESS);
+
+               path = ServerCert::TrustedPemPath;
+               alias = PemPrefix + Trusted + storeType.first + std::to_string(PEM_CRT);
+               importCertFromFileToStore(storeType.second, path, std::string(), alias, CERTSVC_SUCCESS);
+
+               path = ServerCert::PemChainPath;
+               alias = PemPrefix + Chain + storeType.first + std::to_string(PEM_CRT);
+               importCertFromFileToStore(storeType.second, path, std::string(), alias, CERTSVC_SUCCESS);
+
+               path = ServerCert::CrtWithPassPath;
+               alias = CrtPrefix + storeType.first + std::to_string(PEM_CRT);
+               importCertFromFileToStore(storeType.second, path, pass, alias, CERTSVC_SUCCESS);
+
+               path = ServerCert::P12WithoutPassPath;
+               alias = P12Prefix + storeType.first + std::to_string(P12_END_USER);
+               importCertFromFileToStore(storeType.second, path, std::string(), alias, CERTSVC_SUCCESS);
+
+               path = ServerCert::P12WithPassPath;
+               alias = P12Prefix + WithPass + storeType.first + std::to_string(P12_END_USER);
+               importCertFromFileToStore(storeType.second, path, pass, alias, CERTSVC_SUCCESS);
+
+               path = ServerCert::P12ChainPath;
+               alias = P12Prefix + Chain + storeType.first + std::to_string(P12_END_USER);
+               importCertFromFileToStore(storeType.second, path, std::string(), alias, CERTSVC_SUCCESS);
+
+               path = ServerCert::P12EndUserPath;
+               alias = P12Prefix + EndUser + storeType.first + std::to_string(P12_END_USER);
+               importCertFromFileToStore(storeType.second, path, std::string(), alias, CERTSVC_SUCCESS);
+
+               path = ServerCert::PfxWithPassPath;
+               alias = PfxPrefix + storeType.first;
+               importCertFromFileToStore(storeType.second, path, pass, alias, CERTSVC_SUCCESS);
+
+               path = ServerCert::PfxChainPath;
+               alias = PfxPrefix + Chain + storeType.first;
+               importCertFromFileToStore(storeType.second, path, pass, alias, CERTSVC_SUCCESS);
+       }
+}
+
+NEGATIVE_TEST_CASE(T_import_cert_from_to_individual_store_error)
+{
+       std::string path;
+       std::string alias = PemPrefix + std::to_string(PEM_CRT);
+
+       path = ServerCert::PemInvalidCert;
+       importCertFromFileToStore(WIFI_STORE, path, std::string(), alias, CERTSVC_FAIL);
+
+       path = "Not Found";
+       importCertFromFileToStore(WIFI_STORE, path, std::string(), alias, CERTSVC_IO_ERROR);
+
+       path = "Not Found.pem";
+       importCertFromFileToStore(WIFI_STORE, path, std::string(), alias, CERTSVC_IO_ERROR);
+
+       path = ServerCert::EmptyCert;
+       importCertFromFileToStore(WIFI_STORE, path, std::string(), alias, CERTSVC_IO_ERROR);
+
+       path = ServerCert::P12InvalidCert;
+       importCertFromFileToStore(WIFI_STORE, path, std::string(), alias, CERTSVC_FALSE);
+
+       path = ServerCert::PfxWithPassPath;
+       importCertFromFileToStore(WIFI_STORE, path, std::string(), alias, CERTSVC_FAIL);
+
+       path = ServerCert::P12WithPassPath;
+       importCertFromFileToStore(WIFI_STORE, path, std::string(), alias, CERTSVC_FAIL);
+
+       path = ServerCert::PemWithoutPassPath;
+       importCertFromFileToStore(
+               NONE_STORE,
+               path,
+               std::string(),
+               alias,
+               CERTSVC_INVALID_STORE_TYPE);
+
+       importCertFromFileToStore(
+               SYSTEM_STORE,
+               path,
+               std::string(),
+               alias,
+               CERTSVC_INVALID_STORE_TYPE);
+
+       importCertFromFileToStore(
+               WIFI_STORE,
+               std::string(),
+               std::string(),
+               alias,
+               CERTSVC_INVALID_STORE_TYPE);
+
+       importCertFromFileToStore(
+               WIFI_STORE,
+               path,
+               std::string(),
+               std::string(),
+               CERTSVC_WRONG_ARGUMENT);
+
+       alias = createLargeString(1024 * 1024);
+       importCertFromFileToStore(WIFI_STORE, path, std::string(), alias, CERTSVC_WRONG_ARGUMENT);
+
+       path = ServerCert::P12WithoutPassPath;
+       importCertFromFileToStore(WIFI_STORE, path, std::string(), alias, CERTSVC_WRONG_ARGUMENT);
+
+}
+
+POSITIVE_TEST_CASE(T_certsvc_pkcs12_import_from_file_to_store_ret_list)
+{
+       std::string alias, path, pass;
+       pass = ServerCert::ServerPass;
+
+       for (auto storeType: storeTypeMap)
+       {
+               path = ServerCert::P12WithoutPassPath;
+               alias = P12Prefix + RetList + storeType.first + std::to_string(P12_END_USER);
+               importCertFromFileToStoreRetList(
+                       storeType.second,
+                       path,
+                       std::string(),
+                       alias,
+                       true,
+                       CERTSVC_SUCCESS);
+
+               path = ServerCert::P12WithPassPath;
+               alias = P12Prefix + RetList + WithPass + storeType.first + std::to_string(P12_END_USER);
+               importCertFromFileToStoreRetList(
+                       storeType.second,
+                       path,
+                       pass,
+                       alias,
+                       true,
+                       CERTSVC_SUCCESS);
+       }
+}
+
+NEGATIVE_TEST_CASE(T_certsvc_pkcs12_import_from_file_to_store_ret_list_error)
+{
+       std::string path = ServerCert::P12WithPassPath;
+       std::string alias = P12Prefix + std::to_string(P12_END_USER);
+
+       importCertFromFileToStoreRetList(
+               WIFI_STORE,
+               path,
+               std::string(),
+               alias,
+               false,
+               CERTSVC_WRONG_ARGUMENT);
+
+       importCertFromFileToStoreRetList(
+               NONE_STORE,
+               path,
+               std::string(),
+               alias,
+               true,
+               CERTSVC_INVALID_STORE_TYPE);
+
+       importCertFromFileToStoreRetList(
+               SYSTEM_STORE,
+               path,
+               std::string(),
+               alias,
+               true,
+               CERTSVC_INVALID_STORE_TYPE);
+
+       path = ServerCert::EmptyCert;
+       importCertFromFileToStoreRetList(
+               WIFI_STORE,
+               path,
+               std::string(),
+               alias,
+               true,
+               CERTSVC_IO_ERROR);
+
+       path = ServerCert::P12InvalidCert;
+       importCertFromFileToStoreRetList(
+               WIFI_STORE,
+               path,
+               std::string(),
+               alias,
+               true,
+               CERTSVC_FALSE);
+
+       path = ServerCert::P12WithPassPath;
+       importCertFromFileToStoreRetList(
+               WIFI_STORE,
+               path,
+               std::string(),
+               alias,
+               true,
+               CERTSVC_FAIL);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file