diff --git a/.github/workflows/builds.yml b/.github/workflows/builds.yml index 42932166cd..a8f24b7600 100644 --- a/.github/workflows/builds.yml +++ b/.github/workflows/builds.yml @@ -1419,9 +1419,9 @@ jobs: - run: make install-headers - run: make install-library - - run: TZ="UTC" ./src/suricata -u -U LogCustomFormatTest01 - - run: TZ="Brazil/East" ./src/suricata -u -U LogCustomFormatTest01 - - run: TZ="Saskatchewan/Regina" ./src/suricata -u -U LogCustomFormatTest01 + - run: TZ="UTC" ./src/suricata -u -U CreateFormattedTimeStringTest01 + - run: TZ="Brazil/East" ./src/suricata -u -U CreateFormattedTimeStringTest01 + - run: TZ="Saskatchewan/Regina" ./src/suricata -u -U CreateFormattedTimeStringTest01 ubuntu-24-04-rust-vars: name: Ubuntu 24.04 (RUSTC+CARGO vars) diff --git a/src/log-cf-common.c b/src/log-cf-common.c index fe46c1ac3b..98b6989c62 100644 --- a/src/log-cf-common.c +++ b/src/log-cf-common.c @@ -27,7 +27,6 @@ #include "log-cf-common.h" #include "util-print.h" -#include "util-unittest.h" #include "util-time.h" #include "util-debug.h" @@ -226,53 +225,6 @@ void LogCustomFormatWriteTimestamp(MemBuffer *buffer, const char *fmt, const SCT buffer->size, (uint8_t *)buf,strlen(buf)); } -#ifdef UNITTESTS -/** - * \internal - * \brief This test tests default timestamp format - */ -static int LogCustomFormatTest01(void) -{ - // strftime which underpins LogCustomFormatWriteTimestamp doesn't - // seem to function properly on MinGW -#ifndef __MINGW32__ - struct tm tm; - tm.tm_sec = 0; - tm.tm_min = 30; - tm.tm_hour = 4; - tm.tm_mday = 13; - tm.tm_mon = 0; - tm.tm_year = 114; - tm.tm_wday = 1; - tm.tm_yday = 13; - tm.tm_isdst = -1; - SCTime_t ts = SCTIME_FROM_SECS(mktime(&tm)); - - MemBuffer *buffer = MemBufferCreateNew(62); - FAIL_IF_NULL(buffer); - - LogCustomFormatWriteTimestamp(buffer, "", ts); - /* - * {buffer = "01/13/14-04:30:00", size = 62, offset = 17} - */ - FAIL_IF_NOT( buffer->offset == 17); - FAIL_IF_NOT(buffer->size == 62); - FAIL_IF(strcmp((char *)buffer->buffer, "01/13/14-04:30:00") != 0); - - MemBufferFree(buffer); -#endif - PASS; -} - -static void LogCustomFormatRegisterTests(void) -{ - UtRegisterTest("LogCustomFormatTest01", LogCustomFormatTest01); -} -#endif /* UNITTESTS */ - void LogCustomFormatRegister(void) { -#ifdef UNITTESTS - LogCustomFormatRegisterTests(); -#endif /* UNITTESTS */ } diff --git a/src/runmode-unittests.c b/src/runmode-unittests.c index 3feef59753..4ce39159d9 100644 --- a/src/runmode-unittests.c +++ b/src/runmode-unittests.c @@ -135,6 +135,7 @@ void TmqhSetup (void); static void RegisterUnittests(void) { UTHRegisterTests(); + SCTimeRegisterTests(); StreamTcpRegisterTests(); SigRegisterTests(); SCReputationRegisterTests(); diff --git a/src/util-time.c b/src/util-time.c index ff8b5c6544..e05fba5dcf 100644 --- a/src/util-time.c +++ b/src/util-time.c @@ -65,6 +65,7 @@ #include "tm-threads.h" #include "util-debug.h" #include "util-time.h" +#include "util-unittest.h" #ifdef UNITTESTS static struct timeval current_time = { 0, 0 }; @@ -651,3 +652,42 @@ uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1) { return (uint64_t)(t1.tv_sec - t0.tv_sec) * 1000000L + (t1.tv_usec - t0.tv_usec); } + +#ifdef UNITTESTS +static int CreateFormattedTimeStringTest01(void) +{ + /* strftime, which underpins CreateFormattedTimeString, does not seem to + * function properly on MinGW. */ +#ifndef __MINGW32__ + struct tm tm; + tm.tm_sec = 0; + tm.tm_min = 30; + tm.tm_hour = 4; + tm.tm_mday = 13; + tm.tm_mon = 0; + tm.tm_year = 114; + tm.tm_wday = 1; + tm.tm_yday = 13; + tm.tm_isdst = -1; + + /* mktime() interprets the broken-down time as local time and SCLocalTime() + * converts it back, so the result is independent of the active timezone. + * The CI runs this test under several timezones to guard that round trip. */ + struct tm local_tm; + struct tm *t = SCLocalTime(mktime(&tm), &local_tm); + + char buf[128] = { 0 }; + CreateFormattedTimeString(t, "%m/%d/%y-%H:%M:%S", buf, sizeof(buf)); + + FAIL_IF(strcmp(buf, "01/13/14-04:30:00") != 0); +#endif + PASS; +} +#endif /* UNITTESTS */ + +void SCTimeRegisterTests(void) +{ +#ifdef UNITTESTS + UtRegisterTest("CreateFormattedTimeStringTest01", CreateFormattedTimeStringTest01); +#endif +} diff --git a/src/util-time.h b/src/util-time.h index d978b5d5a7..a9dec3adac 100644 --- a/src/util-time.h +++ b/src/util-time.h @@ -161,4 +161,6 @@ uint64_t SCGetSecondsUntil (const char *str, time_t epoch); uint64_t SCTimespecAsEpochMillis(const struct timespec *ts); uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1); +void SCTimeRegisterTests(void); + #endif /* SURICATA_UTIL_TIME_H */