]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
unit-tests: Add support for more than one warning per test case
authorTobias Brunner <tobias@strongswan.org>
Fri, 4 Mar 2022 14:16:12 +0000 (15:16 +0100)
committerTobias Brunner <tobias@strongswan.org>
Thu, 14 Apr 2022 17:05:44 +0000 (19:05 +0200)
Warnings are usually short (as compared to failures that contain data
dumps), so the buffer size can be reduced.

src/libstrongswan/tests/test_runner.c
src/libstrongswan/tests/test_suite.c
src/libstrongswan/tests/test_suite.h

index 67fe80f7f8dc326f45df086514208e72f783aa8e..6abdfd0c1a166ec519a2f6c9a7242caba391c1bf 100644 (file)
@@ -456,23 +456,45 @@ static void collect_failure_info(array_t *failures, char *name, int i)
        array_insert(failures, -1, &failure);
 }
 
+/**
+ * Context data to collect warnings
+ */
+typedef struct {
+       char *name;
+       int i;
+       array_t *warnings;
+} warning_ctx_t;
+
+/**
+ * Callback to collect warnings
+ */
+CALLBACK(warning_cb, void,
+       warning_ctx_t *ctx, const char *msg, const char *file, const int line)
+{
+       failure_t warning = {
+               .name = ctx->name,
+               .i = ctx->i,
+               .file = file,
+               .line = line,
+       };
+
+       strncpy(warning.msg, msg, sizeof(warning.msg) - 1);
+       warning.msg[sizeof(warning.msg)-1] = 0;
+       array_insert(ctx->warnings, -1, &warning);
+}
+
 /**
  * Collect warning information, add failure_t to array
  */
 static bool collect_warning_info(array_t *warnings, char *name, int i)
 {
-       failure_t warning = {
+       warning_ctx_t ctx = {
                .name = name,
                .i = i,
+               .warnings = warnings,
        };
 
-       warning.line = test_warning_get(warning.msg, sizeof(warning.msg),
-                                                                       &warning.file);
-       if (warning.line)
-       {
-               array_insert(warnings, -1, &warning);
-       }
-       return warning.line;
+       return test_warnings_get(warning_cb, &ctx);
 }
 
 /**
index 412d9fbf6609d3f84cb637ffdb774e7014811907..1149e1357f06ffffdfa6dc9eb10455c5fbbf3599 100644 (file)
@@ -50,19 +50,26 @@ static backtrace_t *failure_backtrace;
 static bool worker_failed;
 
 /**
- * Warning message buf
+ * Warning information
  */
-static char warning_buf[4096];
+typedef struct {
+       /** Warning message */
+       char msg[BUF_LEN];
+       /** Source file warning was issued */
+       const char *file;
+       /** Line of source warning was issued */
+       int line;
+} warning_info_t;
 
 /**
- * Source file warning was issued
+ * Warnings that occurred
  */
-static const char *warning_file;
+static warning_info_t warnings[3];
 
 /**
- * Line of source file warning was issued
+ * Current warning index
  */
-static int warning_line;
+static int warning_idx = -1;
 
 /**
  * See header.
@@ -442,11 +449,16 @@ void test_warn_msg(const char *file, int line, char *fmt, ...)
 {
        va_list args;
 
+       if (++warning_idx >= countof(warnings))
+       {
+               return;
+       }
        va_start(args, fmt);
-       vsnprintf(warning_buf, sizeof(warning_buf), fmt, args);
-       warning_line = line;
-       warning_file = file;
+       vsnprintf(warnings[warning_idx].msg, sizeof(warnings[warning_idx].msg),
+                         fmt, args);
        va_end(args);
+       warnings[warning_idx].file = file;
+       warnings[warning_idx].line = line;
 }
 
 /**
@@ -479,20 +491,22 @@ int test_failure_get(char *msg, int len, const char **file)
 /**
  * See header.
  */
-int test_warning_get(char *msg, int len, const char **file)
+bool test_warnings_get(void (*cb)(void *ctx, const char *msg, const char *file,
+                                                                 const int line), void *ctx)
 {
-       int line = warning_line;
+       int i;
 
-       if (!line)
+       if (warning_idx < 0)
        {
-               return 0;
+               return FALSE;
+       }
+       for (i = 0; i <= warning_idx && i < countof(warnings); i++)
+       {
+               cb(ctx, warnings[i].msg, warnings[i].file, warnings[i].line);
        }
-       strncpy(msg, warning_buf, len - 1);
-       msg[len - 1] = 0;
-       *file = warning_file;
        /* reset state */
-       warning_line = 0;
-       return line;
+       warning_idx = -1;
+       return TRUE;
 }
 
 /**
index a35e9ec63f1a9f886aaef88b02912ac51962e631..e0629356b9b45784d594423b493b84d380c97076 100644 (file)
@@ -219,6 +219,18 @@ void test_setup_timeout(int s);
  */
 int test_failure_get(char *msg, int len, const char **file);
 
+/**
+ * Get info about warnings if any were issued during the test. Resets the
+ * warning state.
+ *
+ * @param cb           callback that receives a custom context object, message,
+ *                                     source file and line of each warning
+ * @param ctx          context object
+ * @return                     TRUE if any warnings were issued
+ */
+bool test_warnings_get(void (*cb)(void *ctx, const char *msg, const char *file,
+                                                                 const int line), void *ctx);
+
 /**
  * Get info about a warning if one was issued during the test. Resets the
  * warning state.