From 5c3bbb8d6127f1d51981d64b50109ee6eb84c9f3 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Fri, 13 Nov 2009 18:09:29 +0100 Subject: [PATCH] Time handling: improve time handling in unittests - make sure before each unittest is run the time is reset - add functions to set the time to current time and increment the time - convert alert-unified* Rotate tests to use them - convert time based counters to use them - use GetTime instead of gettimeofday for creating the unified* filenames --- src/alert-unified-alert.c | 11 +++++------ src/alert-unified-log.c | 11 +++++------ src/alert-unified2-alert.c | 17 +++++++++-------- src/counters.c | 22 ++-------------------- src/util-time.c | 23 +++++++++++++++++++++++ src/util-time.h | 4 ++++ src/util-unittest.c | 6 ++++++ 7 files changed, 54 insertions(+), 40 deletions(-) diff --git a/src/alert-unified-alert.c b/src/alert-unified-alert.c index 6e7dabce37..20984d17a8 100644 --- a/src/alert-unified-alert.c +++ b/src/alert-unified-alert.c @@ -23,6 +23,7 @@ #include "tm-modules.h" #include "util-unittest.h" +#include "util-time.h" #define DEFAULT_LOG_FILENAME "unified.alert" @@ -290,13 +291,10 @@ int AlertUnifiedAlertOpenFileCtx(LogFileCtx *file_ctx, char *config_file) * Load the default configuration. */ - /* get the time so we can have a filename with seconds since epoch - * in it. XXX review if we can take this info from somewhere else. - * This is used both during init and runtime, so it must be thread - * safe. */ + /* get the time so we can have a filename with seconds since epoch */ struct timeval ts; memset (&ts, 0, sizeof(struct timeval)); - gettimeofday(&ts, NULL); + TimeGet(&ts); /* create the filename to use */ char *log_dir; @@ -346,7 +344,8 @@ static int AlertUnifiedAlertTestRotate01(void) return 0; } - sleep(1); + TimeSetIncrementTime(1); + ret = AlertUnifiedAlertRotateFile(&tv, data); if (ret == -1) goto error; diff --git a/src/alert-unified-log.c b/src/alert-unified-log.c index 002a70cfa5..d4692c7c0f 100644 --- a/src/alert-unified-log.c +++ b/src/alert-unified-log.c @@ -23,6 +23,7 @@ #include "tm-modules.h" #include "util-unittest.h" +#include "util-time.h" #define DEFAULT_LOG_FILENAME "unified.log" @@ -308,13 +309,10 @@ int AlertUnifiedLogOpenFileCtx(LogFileCtx *file_ctx, char *config_file) * Load the default configuration. */ - /* get the time so we can have a filename with seconds since epoch - * in it. XXX review if we can take this info from somewhere else. - * This is used both during init and runtime, so it must be thread - * safe. */ + /* get the time so we can have a filename with seconds since epoch */ struct timeval ts; memset (&ts, 0, sizeof(struct timeval)); - gettimeofday(&ts, NULL); + TimeGet(&ts); /* create the filename to use */ char *log_dir; @@ -364,7 +362,8 @@ static int AlertUnifiedLogTestRotate01(void) return 0; } - sleep(1); + TimeSetIncrementTime(1); + ret = AlertUnifiedLogRotateFile(&tv, data); if (ret == -1) goto error; diff --git a/src/alert-unified2-alert.c b/src/alert-unified2-alert.c index 3d20660b43..3ea7018654 100644 --- a/src/alert-unified2-alert.c +++ b/src/alert-unified2-alert.c @@ -19,6 +19,7 @@ #include "decode-ipv4.h" #include "util-debug.h" +#include "util-time.h" /*prototypes*/ TmEcode Unified2Alert (ThreadVars *, Packet *, void *, PacketQueue *); @@ -574,13 +575,10 @@ int Unified2AlertOpenFileCtx(LogFileCtx *file_ctx, char *config_file) * Load the default configuration. */ - /** get the time so we can have a filename with seconds since epoch - * in it. XXX review if we can take this info from somewhere else. - * This is used both during init and runtime, so it must be thread - * safe. */ + /** get the time so we can have a filename with seconds since epoch */ struct timeval ts; - memset(&ts, 0, sizeof(struct timeval)); - gettimeofday(&ts, NULL); + memset(&ts, 0x00, sizeof(struct timeval)); + TimeGet(&ts); /* create the filename to use */ char *log_dir; @@ -959,13 +957,16 @@ static int Unified2TestRotate01(void) return 0; } - sleep(1); + TimeSetIncrementTime(1); + ret = Unified2AlertRotateFile(&tv, data); if (ret == -1) goto error; - if (strcmp(filename, lf->filename) == 0) + if (strcmp(filename, lf->filename) == 0) { + printf("filename \"%s\" == \"%s\": ", filename, lf->filename); goto error; + } r = 1; diff --git a/src/counters.c b/src/counters.c index 3d46a1749b..10b63e51bd 100644 --- a/src/counters.c +++ b/src/counters.c @@ -2079,12 +2079,6 @@ static int SCPerfTestIntervalQual16() memset(&tv, 0, sizeof(ThreadVars)); - TimeModeSetOffline(); - struct timeval timev; - memset(&timev, 0x00, sizeof(timev)); - gettimeofday(&timev, NULL); - TimeSet(&timev); - id1 = SCPerfRegisterIntervalCounter("t1", "c1", SC_PERF_TYPE_DOUBLE, NULL, &tv.sc_perf_pctx, "3s"); @@ -2100,10 +2094,7 @@ static int SCPerfTestIntervalQual16() SCPerfUpdateCounterArray(pca, &tv.sc_perf_pctx, 0); /* forward the time 6 seconds */ - memset(&timev, 0x00, sizeof(timev)); - TimeGet(&timev); - timev.tv_sec += 6; - TimeSet(&timev); + TimeSetIncrementTime(6); SCPerfOutputCalculateCounterValue(tv.sc_perf_pctx.head, &d_temp); @@ -2122,12 +2113,6 @@ static int SCPerfTestIntervalQual17() memset(&tv, 0, sizeof(ThreadVars)); - TimeModeSetOffline(); - struct timeval timev; - memset(&timev, 0x00, sizeof(timev)); - gettimeofday(&timev, NULL); - TimeSet(&timev); - id1 = SCPerfRegisterIntervalCounter("t1", "c1", SC_PERF_TYPE_DOUBLE, NULL, &tv.sc_perf_pctx, "2m30s"); @@ -2143,10 +2128,7 @@ static int SCPerfTestIntervalQual17() SCPerfUpdateCounterArray(pca, &tv.sc_perf_pctx, 0); /* forward the time 3 seconds */ - memset(&timev, 0x00, sizeof(timev)); - TimeGet(&timev); - timev.tv_sec += 3; - TimeSet(&timev); + TimeSetIncrementTime(3); SCPerfOutputCalculateCounterValue(tv.sc_perf_pctx.head, &d_temp); diff --git a/src/util-time.c b/src/util-time.c index f00ff9b63d..184fe25c7b 100644 --- a/src/util-time.c +++ b/src/util-time.c @@ -39,6 +39,16 @@ void TimeSet(struct timeval *tv) mutex_unlock(¤t_time_mutex); } +/** \brief set the time to "gettimeofday" meant for testing */ +void TimeSetToCurrentTime(void) { + struct timeval tv; + memset(&tv, 0x00, sizeof(tv)); + + gettimeofday(&tv, NULL); + + TimeSet(&tv); +} + void TimeGet(struct timeval *tv) { if (tv == NULL) @@ -56,3 +66,16 @@ void TimeGet(struct timeval *tv) SCLogDebug("time we got is %" PRIuMAX " sec, %" PRIuMAX " usec", (uintmax_t)tv->tv_sec, (uintmax_t)tv->tv_usec); } + +/** \brief increment the time in the engine + * \param tv_sec seconds to increment the time with */ +void TimeSetIncrementTime(uint32_t tv_sec) { + struct timeval tv; + memset(&tv, 0x00, sizeof(tv)); + TimeGet(&tv); + + tv.tv_sec += tv_sec; + + TimeSet(&tv); +} + diff --git a/src/util-time.h b/src/util-time.h index 888f40f688..5597e700e2 100644 --- a/src/util-time.h +++ b/src/util-time.h @@ -3,6 +3,10 @@ void TimeSet(struct timeval *); void TimeGet(struct timeval *); + +void TimeSetToCurrentTime(void); +void TimeSetIncrementTime(uint32_t); + void TimeModeSetLive(void); void TimeModeSetOffline (void); diff --git a/src/util-unittest.c b/src/util-unittest.c index eead465cc1..1fb49d87e5 100644 --- a/src/util-unittest.c +++ b/src/util-unittest.c @@ -8,6 +8,7 @@ #include "eidps-common.h" #include "util-unittest.h" #include "util-debug.h" +#include "util-time.h" static pcre *parse_regex; static pcre_extra *parse_regex_study; @@ -166,6 +167,11 @@ uint32_t UtRunTests(char *regex_arg) { printf("Test %-60.60s : ", ut->name); matchcnt++; fflush(stdout); /* flush so in case of a segv we see the testname */ + + /* reset the time */ + TimeModeSetOffline(); + TimeSetToCurrentTime(); + ret = ut->TestFn(); printf("%s\n", (ret == ut->evalue) ? "pass" : "FAILED"); if (ret != ut->evalue) {