util-time: relocate timestamp format unit test from log-cf-common

log-cf-common is not longer needed with the removal of http-log, but this test
is still valid, so move until util-time.
pull/15509/head
Jason Ish 1 month ago committed by Victor Julien
parent 865b7243a0
commit 3806bf4260

@ -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)

@ -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 */
}

@ -135,6 +135,7 @@ void TmqhSetup (void);
static void RegisterUnittests(void)
{
UTHRegisterTests();
SCTimeRegisterTests();
StreamTcpRegisterTests();
SigRegisterTests();
SCReputationRegisterTests();

@ -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
}

@ -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 */

Loading…
Cancel
Save