From 725f5813ee3125ad3c55dfbe3aeb5d8155e93e8f Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Mon, 17 Oct 2022 20:16:58 +0800
Subject: [PATCH 18/43] add ut for cutils timestamp

Signed-off-by: haozi007 <liuhao27@huawei.com>
---
 test/cutils/CMakeLists.txt                    |   1 +
 test/cutils/utils_timestamp/CMakeLists.txt    |  16 ++
 .../utils_timestamp/utils_timestamp_ut.cc     | 241 ++++++++++++++++++
 3 files changed, 258 insertions(+)
 create mode 100644 test/cutils/utils_timestamp/CMakeLists.txt
 create mode 100644 test/cutils/utils_timestamp/utils_timestamp_ut.cc

diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 4e67d9ed..7f454f75 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -28,3 +28,4 @@ add_subdirectory(utils_aes)
 add_subdirectory(utils_error)
 add_subdirectory(utils_fs)
 add_subdirectory(utils_filters)
+add_subdirectory(utils_timestamp)
diff --git a/test/cutils/utils_timestamp/CMakeLists.txt b/test/cutils/utils_timestamp/CMakeLists.txt
new file mode 100644
index 00000000..38aec640
--- /dev/null
+++ b/test/cutils/utils_timestamp/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_timestamp_ut)
+
+add_executable(${EXE}
+    utils_timestamp_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+    ${GTEST_INCLUDE_DIR}
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+    )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_timestamp/utils_timestamp_ut.cc b/test/cutils/utils_timestamp/utils_timestamp_ut.cc
new file mode 100644
index 00000000..d6756e3a
--- /dev/null
+++ b/test/cutils/utils_timestamp/utils_timestamp_ut.cc
@@ -0,0 +1,241 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *     http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: haozi007
+ * Create: 2022-10-13
+ * Description: utils timestamp unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "utils_timestamp.h"
+
+TEST(utils_timestamp, test_util_types_timestamp_cmp)
+{
+    types_timestamp_t t1 = { 0 };
+    types_timestamp_t t2 = { 0 };
+
+    // t1 == t2
+    ASSERT_EQ(util_types_timestamp_cmp(&t1, &t2), 0);
+
+    t1.has_seconds = true;
+    t1.seconds = 2;
+    t1.has_nanos = false;
+    t1.nanos = 0;
+    t2.has_seconds = false;
+    t2.seconds = 0;
+    t2.has_nanos = false;
+    t2.nanos = 100;
+    // t1 > t2
+    ASSERT_EQ(util_types_timestamp_cmp(&t1, &t2), 1);
+
+    t1.has_seconds = false;
+    t1.seconds = 2;
+    t1.has_nanos = false;
+    t1.nanos = 0;
+    t2.has_seconds = true;
+    t2.seconds = 1;
+    t2.has_nanos = true;
+    t2.nanos = 100;
+    // t1 < t2
+    ASSERT_EQ(util_types_timestamp_cmp(&t1, &t2), -1);
+
+    t1.has_seconds = true;
+    t1.seconds = 2;
+    t1.has_nanos = false;
+    t1.nanos = 0;
+    t2.has_seconds = true;
+    t2.seconds = 2;
+    t2.has_nanos = false;
+    t2.nanos = 0;
+    // t1 == t2
+    ASSERT_EQ(util_types_timestamp_cmp(&t1, &t2), 0);
+
+    t1.has_nanos = true;
+    t1.nanos = 88;
+    t2.has_nanos = true;
+    t2.nanos = 88;
+    // t1 == t2
+    ASSERT_EQ(util_types_timestamp_cmp(&t1, &t2), 0);
+}
+
+TEST(utils_timestamp, test_util_get_timestamp)
+{
+    types_timestamp_t t = { 0 };
+    std::string invalid_str = "1970-01-02X:00:xx";
+    std::string dstr1 = "1970-01-01T00:00:01.000000800";
+    struct tm tm_local = { 0 };
+    const time_t now_time = time(NULL);
+    long int tm_gmtoff = 0;
+
+    (void)localtime_r(&now_time, &tm_local);
+#ifdef __USE_MISC
+    tm_gmtoff = tm_local.tm_gmtoff;
+#else
+    tm_gmtoff = tm_local.__tm_gmtoff;
+#endif
+
+    ASSERT_EQ(util_get_timestamp(dstr1.c_str(), &t), true);
+    t.seconds += tm_gmtoff;
+    ASSERT_EQ(t.has_seconds, true);
+    ASSERT_EQ(t.seconds, 1);
+    ASSERT_EQ(t.has_nanos, true);
+    ASSERT_EQ(t.nanos, 800);
+
+    // invalid agruments check
+    ASSERT_EQ(util_get_timestamp(nullptr, &t), false);
+    ASSERT_EQ(util_get_timestamp(dstr1.c_str(), nullptr), false);
+    ASSERT_EQ(util_get_timestamp(invalid_str.c_str(), nullptr), false);
+}
+
+TEST(utils_timestamp, test_util_get_now_local_utc_time_buffer)
+{
+    char local_time[128] = { 0 };
+
+    ASSERT_EQ(util_get_now_local_utc_time_buffer(local_time, 128), true);
+    ASSERT_EQ(util_get_now_local_utc_time_buffer(nullptr, 0), false);
+}
+
+TEST(utils_timestamp, test_util_get_time_interval)
+{
+    types_timestamp_t t1 = { 0 };
+    types_timestamp_t t2 = { 0 };
+    int64_t ret = 0;
+
+    ASSERT_EQ(util_get_time_interval(t1, t2, &ret), 0);
+    ASSERT_EQ(ret, 0);
+
+    t2.has_seconds = true;
+    t2.seconds = 8;
+    t2.has_nanos = true;
+    t2.nanos = 8;
+    ASSERT_EQ(util_get_time_interval(t1, t2, &ret), 0);
+    ASSERT_EQ(ret, 8000000008);
+
+    t2.has_seconds = true;
+    t2.seconds = INT64_MAX;
+    t2.has_nanos = false;
+    t2.nanos = 0;
+    ASSERT_NE(util_get_time_interval(t1, t2, &ret), 0);
+
+    t2.seconds = INT64_MAX - 1;
+    t2.has_nanos = true;
+    t2.nanos = 100;
+    ASSERT_NE(util_get_time_interval(t1, t2, &ret), 0);
+}
+
+TEST(utils_timestamp, test_util_get_tm_from_str)
+{
+    std::string invalid_str = "2016-01-02T15:04:01:03";
+
+    std::vector<std::tuple<std::string, int, int, int, int, int, int, int>> cases = {
+        std::make_tuple("1970-01-01T01", 0, 0, 0, 1, 1, 0, 70),
+        std::make_tuple("1980-02-02T02:02", 0, 0, 2, 2, 2, 1, 80),
+        std::make_tuple("1990-03-03T03:03:03", 0, 3, 3, 3, 3, 2, 90),
+    };
+
+    for (const auto &cs : cases) {
+        struct tm got = { 0 };
+        int32_t nano = 0;
+        ASSERT_EQ(util_get_tm_from_str(std::get<0>(elem).c_str(), &got, &nano), true);
+        ASSERT_EQ(nano, std::get<1>(elem));
+        ASSERT_EQ(got.tm_sec, std::get<2>(elem));
+        ASSERT_EQ(got.tm_min, std::get<3>(elem));
+        ASSERT_EQ(got.tm_hour, std::get<4>(elem));
+        ASSERT_EQ(got.tm_mday, std::get<5>(elem));
+        ASSERT_EQ(got.tm_mon, std::get<6>(elem));
+        ASSERT_EQ(got.tm_year, std::get<7>(elem));
+    }
+
+    // check invalid cases
+    ASSERT_NE(util_get_tm_from_str(invalid_str.c_str(), &got, &nano), true);
+    ASSERT_NE(util_get_tm_from_str(nullptr, &got, &nano), true);
+    ASSERT_NE(util_get_tm_from_str(invalid_str.c_str(), nullptr, &nano), true);
+    ASSERT_NE(util_get_tm_from_str(invalid_str.c_str(), &got, nullptr), true);
+}
+
+TEST(utils_timestamp, test_util_time_seconds_since)
+{
+    std::string defaultstr = "-";
+    std::string invalid_str = "2016-01-02T15:04:01:03";
+    std::string dstr1 = "1990-03-03T03:03:03";
+    types_timestamp_t currt = { 0 };
+    char tbuf[128] = { 0 };
+    int64_t ret;
+
+    ASSERT_EQ(util_get_now_time_stamp(&currt), true);
+    currt.seconds -= 10;
+    ASSERT_EQ(util_get_time_buffer(&currt, tbuf, 128), true);
+    ret = util_time_seconds_since(tbuf);
+    ASSERT_GE(ret, 9);
+    ASSERT_LE(ret, 11);
+
+    ASSERT_EQ(util_time_seconds_since(dstr1.c_str()), 0);
+
+    // invalid cases
+    ASSERT_EQ(util_time_seconds_since(invalid_str.c_str()), 0);
+    ASSERT_EQ(util_time_seconds_since(nullptr), 0);
+    ASSERT_EQ(util_time_seconds_since(defaultContainerTime), 0);
+    ASSERT_EQ(util_time_seconds_since(defaultstr.c_str()), 0);
+}
+
+TEST(utils_timestamp, test_util_time_format_duration)
+{
+    std::string invalid_str = "2016-01-02T15:04:01:03";
+    std::string dstr3 = "1990-03-03T03:03:03.000000000+08:00";
+    std::string defaultstr = "-";
+    char out[128] = { 0 };
+
+    ASSERT_EQ(util_time_format_duration(dstr3.c_str(), out, 128), 0);
+
+    // invalid cases
+    ASSERT_EQ(util_time_format_duration(invalid_str.c_str(), out, 128), 1);
+    ASSERT_EQ(util_time_format_duration(nullptr, out, 128), 1);
+    ASSERT_EQ(util_time_format_duration(defaultContainerTime, out, 128), 1);
+    ASSERT_EQ(util_time_format_duration(defaultstr.c_str(), out, 128), 1);
+    ASSERT_EQ(util_time_format_duration(invalid_str.c_str(), out, 0), -1);
+}
+
+TEST(utils_timestamp, test_util_to_unix_nanos_from_str)
+{
+    std::string invalid_str = "2016-01-02T15:04:01:03";
+    std::string dstr3 = "1970-01-01T00:00:01.0+00:00";
+    int64_t ret = 0;
+
+    ASSERT_EQ(util_to_unix_nanos_from_str(dstr3.c_str(), &ret), 0);
+    ASSERT_EQ(ret, 1000000000);
+
+    // invalid cases
+    ASSERT_NE(util_to_unix_nanos_from_str(invalid_str.c_str(), &ret), 0);
+    ASSERT_EQ(util_to_unix_nanos_from_str(nullptr, &ret), 0);
+}
+
+TEST(utils_timestamp, test_util_time_str_to_nanoseconds)
+{
+    int64_t ret = 0;
+    std::string invalid_str = "xxxxxxx";
+    std::string dstr2 = "1ms";
+    std::string dstr3 = "2s";
+    std::string dstr4 = "1m";
+    std::string dstr5 = "1h";
+
+    ASSERT_EQ(util_time_str_to_nanoseconds(dstr2.c_str(), &ret), 0);
+    ASSERT_EQ(ret, Time_Milli);
+    ASSERT_EQ(util_time_str_to_nanoseconds(dstr3.c_str(), &ret), 0);
+    ASSERT_EQ(ret, 2 * Time_Second);
+    ASSERT_EQ(util_time_str_to_nanoseconds(dstr4.c_str(), &ret), 0);
+    ASSERT_EQ(ret, 60 * Time_Second);
+    ASSERT_EQ(util_time_str_to_nanoseconds(dstr5.c_str(), &ret), 0);
+    ASSERT_EQ(ret, 3600 * Time_Second);
+
+    // invalid cases
+    ASSERT_NE(util_time_str_to_nanoseconds(invalid_str.c_str(), &ret), 0);
+    ASSERT_NE(util_time_str_to_nanoseconds(nullptr, &ret), 0);
+    ASSERT_NE(util_time_str_to_nanoseconds(dstr3.c_str(), nullptr), 0);
+}
\ No newline at end of file
-- 
2.25.1

