%dir %cert_svc_tests
%cert_svc_tests/p12
%cert_svc_tests/certs
+%cert_svc_tests/unit_test_data
%_libdir/libcert-svc-validator-plugin.so
%bin_dir/cert-svc-example*
SET(UNIT_TESTS_SOURCES
test_main.cpp
+ test_constant.cpp
+ test_common.cpp
test_vcore_cert_store_type.cpp
test_vcore_api.cpp
test_vcore_time_conversion.cpp
test_vcore_certificate_collection.cpp
+ test_vcore_api_cert.cpp
test_cert_server_db.cpp
colour_log_formatter.cpp
${PROJECT_SOURCE_DIR}/src/server/src/cert-server-logic.c
)
INSTALL(TARGETS ${TARGET_UNIT_TESTS} DESTINATION ${BIN_DIR})
+
+INSTALL(
+ FILES
+ data/server_without_pass_cert.pem
+ DESTINATION ${CERT_SVC_TESTS}/unit_test_data
+)
\ No newline at end of file
--- /dev/null
+-----BEGIN CERTIFICATE-----
+MIIEETCCAvmgAwIBAgIUNu213WuIIkWbgZk/rVk1GswrVs0wDQYJKoZIhvcNAQEL
+BQAwgZcxCzAJBgNVBAYTAlZOMQ8wDQYDVQQIDAZIYSBOb2kxFDASBgNVBAcMC05h
+bSBUdSBMaWVtMRAwDgYDVQQKDAdTYW1zdW5nMQwwCgYDVQQLDANTUlYxHDAaBgNV
+BAMME1RpemVuIFNlY3VyaXR5IFJvb3QxIzAhBgkqhkiG9w0BCQEWFHh1YW4udGFu
+QHNhbXN1bmcuY29tMB4XDTI0MDUwMzA2NTYxMloXDTM0MDUwMTA2NTYxMlowgZcx
+CzAJBgNVBAYTAlZOMQ8wDQYDVQQIDAZIYSBOb2kxFDASBgNVBAcMC05hbSBUdSBM
+aWVtMRAwDgYDVQQKDAdTYW1zdW5nMQwwCgYDVQQLDANTUlYxHDAaBgNVBAMME1Rp
+emVuIFNlY3VyaXR5IFJvb3QxIzAhBgkqhkiG9w0BCQEWFHh1YW4udGFuQHNhbXN1
+bmcuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuPBn/vP84tzH
+Bb1TNHt9NGP16r/E/fESqOKB92nzCD8aQaPT/j53BahTDJvS0KNwGeT5mSosr/uy
+LYL6R1FzL5V3yQJ9QPruWfXL1a/nuVsmLcZvNORcYgSV0h0brNjlRG0Zsxaw/w1M
+uX2jCu9LFdywJuRIzrZ7UrGytljNIBdt4P5JMMgkRiYsuF4b+mecODnmuh1ybHa3
+8Zz+i7LXSA8MYpvSTfyUg8XJ/aijhv5iMY7Og2hpBoOZUgnABlsArvjUzGcP8kNH
+1h25OZenH3MbBDFENZ7pPz/9sNCMInmJ+6sW9n8OEJoFYmafGVeNXP82KdrIeufX
+gB7f6AUktQIDAQABo1MwUTAdBgNVHQ4EFgQUxm3azuhY4LTJqMdtWLcN2/F+cTEw
+HwYDVR0jBBgwFoAUxm3azuhY4LTJqMdtWLcN2/F+cTEwDwYDVR0TAQH/BAUwAwEB
+/zANBgkqhkiG9w0BAQsFAAOCAQEApgWx+ueTsiP+cwMwCnnYBuDgOH00BMcAQEsV
+8EFu/HkL1Nt1lbPwEFV7LRF6eBBLGPyDD2IH/kFmirulcbTjwSHsipV6ND+mATwq
+aa+kil6aPhZCOzadKyT3dk2iYu++o20fi1qtFUofwydA0cjOO5RJn4XSsHIm9Tdu
+MA9IB0mZWT1h92Gq3L1DLLuIwJ5YWrvvPnxj0B7ejympjmESPy+sMDXNxhQLwvgl
+js8VVDEyj1i9d2FEz/iw5YWAqg4xe1ykcA347NrK5h5hAEOYPR5JoXZxEXH01Mzv
+ijIjYjZqXDbZB4wlylG/QZp2/huV8itizz2NFI1vet8X3V0xgQ==
+-----END CERTIFICATE-----
--- /dev/null
+/*
+ * 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 "test_common.h"
+#include "cert-svc/ccert.h"
+#include <cstring>
+#include <sstream>
+
+static inline int convertHexToInt(std::string hexPair)
+{
+ std::stringstream stream;
+ int n;
+ stream << hexPair;
+ stream >> std::hex >> n;
+ return n;
+}
+
+int convertStringToBytes(const std::string hexString, unsigned char **content)
+{
+ int length = hexString.length();
+ unsigned char *bytes = (unsigned char *) malloc(length / 2 + 1);
+
+ if (bytes == NULL) {
+ return CERTSVC_BAD_ALLOC;
+ }
+
+ memset(bytes, 0x00, length / 2 + 1);
+
+ for (int i = 0; i < length; i += 2) {
+ std::string hexPair = hexString.substr(i, 2);
+ bytes[i / 2] = convertHexToInt(hexPair);
+ }
+
+ bytes[length / 2] = '\0';
+ *content = bytes;
+
+ return CERTSVC_SUCCESS;
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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 <string>
+
+int convertStringToBytes(const std::string hexString, unsigned char **content);
\ No newline at end of file
--- /dev/null
+/*
+ * 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 "test_constant.h"
+
+namespace ServerCertInfo {
+
+ const std::string ServerCertPemWithoutPassPath =
+ std::string(TESTAPP_RES_DIR) +
+ "unit_test_data/server_without_pass_cert.pem";
+
+ const std::string FullFieldCertPem =
+ "MIIDpDCCAowCAQEwDQYJKoZIhvcNAQELBQAwgZcxCzAJBgNVBAYTAlZOMQ8wDQYD"
+ "VQQIDAZIYSBOb2kxFDASBgNVBAcMC05hbSBUdSBMaWVtMRAwDgYDVQQKDAdTYW1z"
+ "dW5nMQwwCgYDVQQLDANTUlYxHDAaBgNVBAMME1RpemVuIFNlY3VyaXR5IFJvb3Qx"
+ "IzAhBgkqhkiG9w0BCQEWFHh1YW4udGFuQHNhbXN1bmcuY29tMB4XDTI0MDQyNzA4"
+ "MDYxNVoXDTI1MDQyNzA4MDYxNVowgZcxCzAJBgNVBAYTAlZOMQ8wDQYDVQQIDAZI"
+ "YSBOb2kxFDASBgNVBAcMC05hbSBUdSBMaWVtMRAwDgYDVQQKDAdTYW1zdW5nMQww"
+ "CgYDVQQLDANTUlYxHDAaBgNVBAMME1RpemVuIFNlY3VyaXR5IFJvb3QxIzAhBgkq"
+ "hkiG9w0BCQEWFHh1YW4udGFuQHNhbXN1bmcuY29tMIIBIjANBgkqhkiG9w0BAQEF"
+ "AAOCAQ8AMIIBCgKCAQEAkdx/g1gWqoU4U35cvPEo++b/+YO0jfdpyLLEkdYbtOC8"
+ "JRVXm4DkbnckJ/CFxtrGfArhwJB7KYfAqE/Yj1UYPaaAxg2nDq+F0Se6Ws5SxukP"
+ "2gGH+yU345qzv85dtOBMuRjvjCWGgk4pryrAt9im4DnMfAedWvuZpNca/XmMIkkE"
+ "fD3CFEMK4KIUkwND9mOKWxFf+7GlwhIcl+/rVctfn0xg+GYwLjfe4X9LgsB1Y/GS"
+ "0QZ92dkl2z/ZoMVPq4wa76n7oeCyRuhcXoGzSAA4Za/10vt51EWF6ZdlswQLjMDB"
+ "lXZH+XmYr4J1m/kM6D/UGcrL0XjikfZafonUHo+ijwIDAQABMA0GCSqGSIb3DQEB"
+ "CwUAA4IBAQBdDPXq2HI8AhkLC86mTH6VE6N5FfUBmWfpM3VDmIMAyGF+CDjpCN27"
+ "C0xP2Y1Lnbmn3wl2RbsbBZw0X2aQfgQ6PLG+uyVYeQxrmDm+2Urq8IoKLBSPwa24"
+ "BQjzBDDM4QNDWqDZPI+rAYI+9JDgJxSU9qqWMQYjY+BogUyoRABMdEmc/kJU85KK"
+ "FRr8SgcpohHyr3H67eil2ypOI3Ec5TOzr3nzvsm1WNTFM8NQA0rnzp7TsGYSYCFI"
+ "1q8YLZpPsOiEwhblAQhYuyQN4ZMZFdNZCFUagWIbTYfXdsFsjWYpeMzTXSZmafFd"
+ "1ghu0Jd/B9m+FukR6nycl+iWBetXZVkB";
+
+ const std::string CertDer =
+ "30820411308202f9a003020102021436edb5dd6b8822459b81993fad59351acc"
+ "2b56cd300d06092a864886f70d01010b0500308197310b300906035504061302"
+ "564e310f300d06035504080c064861204e6f693114301206035504070c0b4e61"
+ "6d205475204c69656d3110300e060355040a0c0753616d73756e67310c300a06"
+ "0355040b0c03535256311c301a06035504030c1354697a656e20536563757269"
+ "747920526f6f743123302106092a864886f70d01090116147875616e2e74616e"
+ "4073616d73756e672e636f6d301e170d3234303530333036353631325a170d33"
+ "34303530313036353631325a308197310b300906035504061302564e310f300d"
+ "06035504080c064861204e6f693114301206035504070c0b4e616d205475204c"
+ "69656d3110300e060355040a0c0753616d73756e67310c300a060355040b0c03"
+ "535256311c301a06035504030c1354697a656e20536563757269747920526f6f"
+ "743123302106092a864886f70d01090116147875616e2e74616e4073616d7375"
+ "6e672e636f6d30820122300d06092a864886f70d01010105000382010f003082"
+ "010a0282010100b8f067fef3fce2dcc705bd53347b7d3463f5eabfc4fdf112a8"
+ "e281f769f3083f1a41a3d3fe3e7705a8530c9bd2d0a37019e4f9992a2caffbb2"
+ "2d82fa4751732f9577c9027d40faee59f5cbd5afe7b95b262dc66f34e45c6204"
+ "95d21d1bacd8e5446d19b316b0ff0d4cb97da30aef4b15dcb026e448ceb67b52"
+ "b1b2b658cd20176de0fe4930c82446262cb85e1bfa679c3839e6ba1d726c76b7"
+ "f19cfe8bb2d7480f0c629bd24dfc9483c5c9fda8a386fe62318ece8368690683"
+ "995209c0065b00aef8d4cc670ff24347d61db93997a71f731b043144359ee93f"
+ "3ffdb0d08c227989fbab16f67f0e109a0562669f19578d5cff3629dac87ae7d7"
+ "801edfe80524b50203010001a3533051301d0603551d0e04160414c66ddacee8"
+ "58e0b4c9a8c76d58b70ddbf17e7131301f0603551d23041830168014c66ddace"
+ "e858e0b4c9a8c76d58b70ddbf17e7131300f0603551d130101ff040530030101"
+ "ff300d06092a864886f70d01010b05000382010100a605b1fae793b223fe7303"
+ "300a79d806e0e0387d3404c700404b15f0416efc790bd4db7595b3f010557b2d"
+ "117a78104b18fc830f6207fe41668abba571b4e3c121ec8a957a343fa6013c2a"
+ "69afa48a5e9a3e16423b369d2b24f7764da262efbea36d1f8b5aad154a1fc327"
+ "40d1c8ce3b94499f85d2b07226f5376e300f48074999593d61f761aadcbd432c"
+ "bb88c09e585abbef3e7c63d01ede8f29a98e61123f2fac3035cdc6140bc2f825"
+ "8ecf155431328f58bd776144cff8b0e58580aa0e317b5ca4700df8ecdacae61e"
+ "610043983d1e49a176711171f4d4ccef8a322362366a5c36d9078c25ca51bf41"
+ "9a76fe1b95f22b62cf3d8d148d6f7adf17dd5d3181";
+
+ const std::string SubjectCommonName = "Tizen Security Root";
+}
--- /dev/null
+/*
+ * 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 <string>
+
+namespace ServerCertInfo {
+ extern const std::string ServerCertPemWithoutPassPath;
+ extern const std::string FullFieldCertPem;
+ extern const std::string CertDer;
+ extern const std::string SubjectCommonName;
+}
\ No newline at end of file
#include "dpl/log/log.h"
#include <cstring>
-CertSvcInstance instance;
+static CertSvcInstance instance;
static const char *cStrTest = "Test";
BOOST_AUTO_TEST_SUITE(VCORE_API_TEST)
--- /dev/null
+/*
+ * 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;
+
+void checkFiledCertificate(CertSvcCertificate certificate, const char *expected)
+{
+ CertSvcString subjectStr;
+ const char *buffer = nullptr;
+ size_t bufferLength;
+ int result;
+
+ result = certsvc_certificate_get_string_field(
+ certificate,
+ CERTSVC_SUBJECT_COMMON_NAME,
+ &subjectStr);
+
+ BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+ certsvc_string_to_cstring(subjectStr, &buffer, &bufferLength);
+
+ BOOST_CHECK_EQUAL(bufferLength, std::strlen(expected));
+ BOOST_CHECK(buffer);
+ BOOST_CHECK_EQUAL(std::strcmp(expected, buffer), 0);
+
+ certsvc_string_free(subjectStr);
+}
+
+BOOST_AUTO_TEST_SUITE(VCORE_API_CERT_TEST)
+
+POSITIVE_TEST_CASE(T_certsvc_certificate_new_from_file)
+{
+ int result;
+ CertSvcCertificate certificate;
+
+ const char *location = ServerCertInfo::ServerCertPemWithoutPassPath.c_str();
+
+ BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+ result = certsvc_certificate_new_from_file(instance, location, &certificate);
+ BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+ BOOST_CHECK_EQUAL(certificate.privateHandler, 0);
+
+ checkFiledCertificate(certificate, ServerCertInfo::SubjectCommonName.c_str());
+
+ certsvc_certificate_free(certificate);
+ certsvc_instance_free(instance);
+}
+
+NEGATIVE_TEST_CASE(T_certsvc_certificate_new_from_file_invalid_argument)
+{
+ int result;
+ CertSvcCertificate certificate;
+ char *location = nullptr;
+
+ BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+ result = certsvc_certificate_new_from_file(instance, location, &certificate);
+ BOOST_CHECK_EQUAL(result, CERTSVC_FAIL);
+
+ certsvc_instance_free(instance);
+}
+
+POSITIVE_TEST_CASE(T_certsvc_certificate_new_from_memory_base64)
+{
+ int result;
+ CertSvcCertificate certificate;
+
+ BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+ result = certsvc_certificate_new_from_memory(
+ instance,
+ reinterpret_cast<const unsigned char *>(ServerCertInfo::FullFieldCertPem.c_str()),
+ ServerCertInfo::FullFieldCertPem.size(),
+ CERTSVC_FORM_DER_BASE64,
+ &certificate);
+
+ BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+ BOOST_CHECK_EQUAL(certificate.privateHandler, 0);
+
+ checkFiledCertificate(certificate, ServerCertInfo::SubjectCommonName.c_str());
+
+ certsvc_certificate_free(certificate);
+ certsvc_instance_free(instance);
+}
+
+POSITIVE_TEST_CASE(T_certsvc_certificate_new_from_memory_der)
+{
+ int result;
+ CertSvcCertificate certificate;
+ unsigned char *content = nullptr;
+
+ result = convertStringToBytes(ServerCertInfo::CertDer, &content);
+ BOOST_CHECK(content);
+ BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+ BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+ result = certsvc_certificate_new_from_memory(
+ instance,
+ reinterpret_cast<const unsigned char *>(content),
+ ServerCertInfo::CertDer.size() / 2,
+ CERTSVC_FORM_DER,
+ &certificate);
+
+ BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+ BOOST_CHECK_EQUAL(certificate.privateHandler, 0);
+
+ checkFiledCertificate(certificate, ServerCertInfo::SubjectCommonName.c_str());
+
+ free(content);
+ certsvc_certificate_free(certificate);
+ certsvc_instance_free(instance);
+}
+
+NEGATIVE_TEST_CASE(T_certsvc_certificate_new_from_memory_wrong_argument)
+{
+ int result;
+ CertSvcCertificate certificate;
+ const char *content = "MIIDp DvcN";
+
+ BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+ result = certsvc_certificate_new_from_memory(
+ instance,
+ reinterpret_cast<const unsigned char *>(content),
+ strlen(content),
+ CERTSVC_FORM_DER_BASE64,
+ &certificate);
+
+ BOOST_CHECK_EQUAL(result, CERTSVC_FAIL);
+
+ certsvc_instance_free(instance);
+}
+
+NEGATIVE_TEST_CASE(T_certsvc_certificate_new_from_memory_invalid_cert_format)
+{
+ int result;
+ CertSvcCertificate certificate;
+ unsigned char *content = nullptr;
+
+ result = convertStringToBytes(ServerCertInfo::CertDer, &content);
+ BOOST_CHECK(content);
+ BOOST_CHECK_EQUAL(result, CERTSVC_SUCCESS);
+
+ BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+ result = certsvc_certificate_new_from_memory(
+ instance,
+ reinterpret_cast<const unsigned char *>(content),
+ ServerCertInfo::CertDer.size() / 2,
+ CERTSVC_FORM_DER_BASE64,
+ &certificate);
+
+ BOOST_CHECK_EQUAL(result, CERTSVC_FAIL);
+
+ result = certsvc_certificate_new_from_memory(
+ instance,
+ reinterpret_cast<const unsigned char *>(ServerCertInfo::FullFieldCertPem.c_str()),
+ ServerCertInfo::FullFieldCertPem.size(),
+ CERTSVC_FORM_DER,
+ &certificate);
+
+ BOOST_CHECK_EQUAL(result, CERTSVC_FAIL);
+
+ free(content);
+ certsvc_instance_free(instance);
+}
+
+NEGATIVE_TEST_CASE(T_certsvc_certificate_new_from_memory_invalid_memory_size)
+{
+ int result;
+ CertSvcCertificate certificate;
+ size_t invalid_size = 10;
+ const unsigned char *content = nullptr;
+
+ BOOST_REQUIRE_EQUAL(certsvc_instance_new(&instance), CERTSVC_SUCCESS);
+
+ result = certsvc_certificate_new_from_memory(
+ instance,
+ content,
+ 0,
+ CERTSVC_FORM_DER,
+ &certificate);
+
+ BOOST_CHECK_EQUAL(result, CERTSVC_FAIL);
+
+ result = certsvc_certificate_new_from_memory(
+ instance,
+ reinterpret_cast<const unsigned char *>(ServerCertInfo::FullFieldCertPem.c_str()),
+ invalid_size,
+ CERTSVC_FORM_DER_BASE64,
+ &certificate);
+
+ BOOST_CHECK_EQUAL(result, CERTSVC_FAIL);
+
+ certsvc_instance_free(instance);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file