Add test vcore time conversion 40/313040/6
authortranthanhtung2001 <tran.tung@samsung.com>
Tue, 18 Jun 2024 10:04:24 +0000 (17:04 +0700)
committertran.tung <tran.tung@samsung.com>
Wed, 26 Jun 2024 01:58:38 +0000 (08:58 +0700)
Change-Id: I4219356f3338eb863db859082c742b5272251850
Signed-off-by: tranthanhtung2001 <tran.tung@samsung.com>
unit-tests/CMakeLists.txt
unit-tests/test_vcore_time_conversion.cpp [new file with mode: 0644]

index 1dd5d63027573cc1dc9718c0558199da5432bc53..683c70afe45c8fda219c51d96314d8a36e1bb2f6 100644 (file)
@@ -61,6 +61,7 @@ SET(UNIT_TESTS_SOURCES
     test_main.cpp
     test_vcore_cert_store_type.cpp
     test_vcore_api.cpp
+    test_vcore_time_conversion.cpp
     test_cert_server_db.cpp
     colour_log_formatter.cpp
     ${PROJECT_SOURCE_DIR}/src/server/src/cert-server-logic.c
diff --git a/unit-tests/test_vcore_time_conversion.cpp b/unit-tests/test_vcore_time_conversion.cpp
new file mode 100644 (file)
index 0000000..ff4c998
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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_macros.h"
+#include "vcore/TimeConversion.h"
+
+using namespace ValidationCore;
+
+BOOST_AUTO_TEST_SUITE(TIME_CONVERSION_TEST)
+
+int convertToTimeT(const char *str, time_t *res_time)
+{
+       ASN1_TIME *asn1_time = new ASN1_TIME();
+       ASN1_TIME_set_string(asn1_time, str);
+
+       int result = asn1TimeToTimeT(asn1_time, res_time);
+
+       delete asn1_time;
+       return result;
+}
+
+POSITIVE_TEST_CASE(T_asn1_time_to_time_t)
+{
+       time_t res_time;
+       unsigned long expected_res_time = 1718668800;
+       BOOST_CHECK_EQUAL(convertToTimeT("240618000000Z", &res_time), 1);
+       BOOST_CHECK_EQUAL((unsigned long)res_time, expected_res_time);
+}
+
+NEGATIVE_TEST_CASE(T_asn1_time_to_time_t_wrong_parameter)
+{
+       time_t res_time;
+       BOOST_CHECK_EQUAL(convertToTimeT("240618000000Z", NULL), 0);
+       BOOST_CHECK_EQUAL(convertToTimeT("490618240000Z", &res_time), 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END()