From: Eric Bollengier Date: Wed, 7 Apr 2021 07:49:51 +0000 (+0200) Subject: regress: Add few functions to unittests lib and fix overflow in bstring X-Git-Tag: Release-11.3.2~606 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bf1831c9526910c46469f09f9507ae845f4b804d;p=thirdparty%2Fbacula.git regress: Add few functions to unittests lib and fix overflow in bstring --- diff --git a/bacula/src/lib/unittests.c b/bacula/src/lib/unittests.c index 9fbf3a40d..e4db70c5c 100644 --- a/bacula/src/lib/unittests.c +++ b/bacula/src/lib/unittests.c @@ -53,6 +53,7 @@ int main() #include "bacula.h" #include "unittests.h" +static int expected=-1; static int err=0; static int nb=0; static bool lmgrinit = false; @@ -64,9 +65,13 @@ Unittests::Unittests(const char *name, bool lmgr/*=false*/, bool motd/*=true*/) if (getenv("UNITTEST_PRINT_VAR")) { print_var = true; } + if (getenv("UNITTEST_TEST_QUIET")) { + quiet = true; + } prolog(name, lmgr, motd); }; +/* Configure the current test with TEST_XXX flags */ void configure_test(uint64_t options) { if (options & TEST_QUIET) { @@ -77,6 +82,12 @@ void configure_test(uint64_t options) } } +/* Set the number of expected tests */ +void unittest_set_nb_tests(int nb) +{ + expected = nb; +} + /* Get the total number of tests */ int unittest_get_nb_tests() { @@ -230,8 +241,15 @@ bool _isnt(const char *file, int l, const char *op, int64_t v, int64_t v2, const */ int report() { + /* We do not count the extra expected check in the display */ + int nb_ok = nb - err; + int nb_total = nb; Pmsg0(-1, "==== Report ====\n"); - Pmsg2(-1, "Result %i/%i OK\n", nb - err, nb); + /* Do an extra check if the expected variable is set */ + if (expected > 0) { + is(nb, expected, "Checking expected tests number"); + } + Pmsg2(-1, "Result %i/%i OK\n", nb_ok, nb_total); return err > 0; } diff --git a/bacula/src/lib/unittests.h b/bacula/src/lib/unittests.h index 50156731d..f1e3e2723 100644 --- a/bacula/src/lib/unittests.h +++ b/bacula/src/lib/unittests.h @@ -20,24 +20,27 @@ * Support routines for Unit Tests. */ -#ifndef _UNITTESTS_H_ -#define _UNITTESTS_H_ +#ifndef UNITTESTS_H_ +#define UNITTESTS_H_ #include "bacula.h" -// Test success if value x is not zero +/* Test success if value x is not zero */ #define ok(x, label) _ok(__FILE__, __LINE__, #x, (x), label) -// Test success if value x is zero + +/* Test success if value x is zero */ #define nok(x, label) _nok(__FILE__, __LINE__, #x, (x), label) -// Test success if value x is a valid path +/* Test success if value x is a valid path */ #define stat_ok(x) _stat_ok(__FILE__, __LINE__, (x)) -// Test success if value x is not a valid path +/* Test success if value x is not a valid path */ #define stat_nok(x) _stat_nok(__FILE__, __LINE__, (x)) +/* Test if a variable has some value */ #define is(x, y, label) _is(__FILE__, __LINE__, #x, (x), (y), label) #define isnt(x, y, label) _isnt(__FILE__, __LINE__, #x, (x), (y), label) +/* Test and return if not correct */ #define rok(x, label) { bool v=(x); if (!v) { _ok(__FILE__, __LINE__, #x, v, label); return 1; } } /* TODO: log() ported from BEE it should be updated. */ @@ -54,41 +57,60 @@ #endif enum { - TEST_VERBOSE = 1, - TEST_QUIET = 2, - TEST_END = 4, - TEST_PRINT_LOCAL = 8 + TEST_VERBOSE = 1, // Display all messages + TEST_QUIET = 2, // Display only ERROR messages + TEST_END = 4, // Not used + TEST_PRINT_LOCAL = 8 // Dump all local variables after an error }; + +/* Configure the unittest framework with the TEST_ options */ void configure_test(uint64_t options); + bool _ok(const char *file, int l, const char *op, int value, const char *label); bool _nok(const char *file, int l, const char *op, int value, const char *label); bool _is(const char *file, int l, const char *op, const char *str, const char *str2, const char *label); bool _isnt(const char *file, int l, const char *op, const char *str, const char *str2, const char *label); bool _is(const char *file, int l, const char *op, int64_t nb, int64_t nb2, const char *label); bool _isnt(const char *file, int l, const char *op, int64_t nb, int64_t nb2, const char *label); + bool _stat_ok(const char *file, int l, const char *path); bool _stat_nok(const char *file, int l, const char *path); + + +/* Print report about the current usage */ int report(); + +/* Dummy function used by Bacula signal handler */ void terminate(int sig); + +/* Print header message and configure some Bacula features (lock manager) */ void prolog(const char *name, bool lmgr=false, bool motd=true); + +/* Unconfigure Bacula features (lock manager, poolmemory) */ void epilog(); + +/* Get the number of tests done */ int unittest_get_nb_tests(); int unittest_get_nb_errors(); +/* Set the number of expected tests */ +void unittest_set_nb_tests(int nb); + /* The class based approach for C++ geeks */ class Unittests { public: Unittests(const char *name, bool lmgr=false, bool motd=true); - ~Unittests() { epilog(); }; + virtual ~Unittests() { epilog(); }; void configure(uint64_t v) { configure_test(v); }; + void set_nb_tests(int nb) { unittest_set_nb_tests(nb); }; }; /* POOL_MEM subclass with convenience methods */ class bstring : public POOL_MEM { public: - bstring() : POOL_MEM() {}; + bstring() : POOL_MEM() {}; bstring(const char *str) : POOL_MEM(str) {}; bstring(const bstring &fmt, ...): POOL_MEM() { va_list arg_ptr; @@ -97,9 +119,10 @@ public: for (;;) { maxlen = this->max_size() - 1; va_start(arg_ptr, fmt); - len = bvsnprintf(this->c_str(), maxlen, fmt, arg_ptr); + bvsnprintf(this->c_str(), maxlen, fmt, arg_ptr); + len = strlen(this->c_str()); va_end(arg_ptr); - if (len < 0 || len >= (maxlen-5)) { + if (len >= (maxlen - 5)) { // Was truncated, need to resize this->realloc_pm(maxlen + maxlen/2); continue; } @@ -134,4 +157,4 @@ void fsu_mvfile(const char *src, const char *dst); void fsu_cpfile(const char *src, const char *dst); void unix_to_win_path(char *path); -#endif /* _UNITTESTS_H_ */ +#endif /* UNITTESTS_H_ */