]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
regress: Add few functions to unittests lib and fix overflow in bstring
authorEric Bollengier <eric@baculasystems.com>
Wed, 7 Apr 2021 07:49:51 +0000 (09:49 +0200)
committerEric Bollengier <eric@baculasystems.com>
Thu, 24 Mar 2022 08:03:01 +0000 (09:03 +0100)
bacula/src/lib/unittests.c
bacula/src/lib/unittests.h

index 9fbf3a40db8e665d43dc8065947a47f15134d2d5..e4db70c5c8492f2e347cc94095a91cb71e406fdc 100644 (file)
@@ -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;
 }
 
index 50156731d187b9aa1220d39512de4c79589e98cb..f1e3e2723f2547d206df76bdd8a404c520d8a828 100644 (file)
  * 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. */
 #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_ */