]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
nest/proto.c: if reload called on not-UP protocol, it logs that no reload will be...
authorKaterina Kubecova <katerina.kubecova@nic.cz>
Tue, 26 Mar 2024 15:46:34 +0000 (16:46 +0100)
committerOndrej Zajicek <santiago@crfreenet.org>
Tue, 23 Apr 2024 14:36:49 +0000 (16:36 +0200)
birdtest: in unit tests, function bt_assert_bug() checks if given code calls bug() with given message

lib/Makefile
lib/birdlib.h
nest/proto.c
sysdep/unix/log.c
sysdep/unix/main.c
test/birdtest.c
test/birdtest.h

index 812f721cc844f4c8fcd325b9c66089122010b566..76640a4557a47c260c5f8b92e38441b53d97e954 100644 (file)
@@ -2,6 +2,6 @@ src := bitmap.c bitops.c blake2s.c blake2b.c checksum.c event.c flowspec.c idm.c
 obj := $(src-o-files)
 $(all-daemon)
 
-tests_src := bitmap_test.c heap_test.c buffer_test.c event_test.c flowspec_test.c bitops_test.c patmatch_test.c fletcher16_test.c slist_test.c checksum_test.c lists_test.c mac_test.c ip_test.c hash_test.c printf_test.c slab_test.c
+tests_src := bitmap_test.c heap_test.c buffer_test.c event_test.c flowspec_test.c bitops_test.c patmatch_test.c fletcher16_test.c slist_test.c checksum_test.c lists_test.c mac_test.c ip_test.c hash_test.c printf_test.c slab_test.c fail_test.c
 tests_targets := $(tests_targets) $(tests-target-files)
 tests_objs := $(tests_objs) $(src-o-files)
index b722641116cf4b55406b3bd3dfbbc8359fa0567b..534886f35b3b239f180f072ec4c6ca8e9575b3a1 100644 (file)
@@ -9,6 +9,8 @@
 #ifndef _BIRD_BIRDLIB_H_
 #define _BIRD_BIRDLIB_H_
 
+#include <setjmp.h>
+
 #include "lib/alloca.h"
 
 /* Ugly structure offset handling macros */
@@ -43,6 +45,13 @@ struct align_probe { char x; long int y; };
 #define CALL(fn, args...) ({ if (fn) fn(args); })
 #define ADVANCE(w, r, l) ({ r -= (l); w += (l); })
 
+extern const enum build_target {
+  BT_BIRD,
+  BT_TEST,
+} build_target;
+
+jmp_buf *get_test_bug_jump(char *msg);
+
 static inline int uint_cmp(uint i1, uint i2)
 { return (int)(i1 > i2) - (int)(i1 < i2); }
 
index 88f4813ef572de5ca5a92cd360e4ca3dbf062815..25f142494210bab2b8ede185c299ff3d61674b33 100644 (file)
@@ -2233,7 +2233,10 @@ proto_cmd_reload(struct proto *p, uintptr_t dir, int cnt UNUSED)
 
   /* If the protocol in not UP, it has no routes */
   if (p->proto_state != PS_UP)
+  {
+    cli_msg(-8, "%s: not reloading - protocol is not UP", p->name);
     return;
+  }
 
   /* All channels must support reload */
   if (dir != CMD_RELOAD_OUT)
index 613a6aa54c914ddab7cd47fbbcd5293c571e9d98..d9281021521d15e4f9002a10537b38f0eaf45bac 100644 (file)
@@ -23,6 +23,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <errno.h>
+#include <setjmp.h>
 
 #include "nest/bird.h"
 #include "nest/cli.h"
@@ -336,6 +337,8 @@ log_rl(struct tbf *f, const char *msg, ...)
 void
 bug(const char *msg, ...)
 {
+  if (build_target == BT_TEST)
+    longjmp(*get_test_bug_jump(msg), 1);
   va_list args;
 
   va_start(args, msg);
index 0d7788bb418d872ee5065527e1f4db59fed12914..0616b6085f161eab56f847394936bf2962b179bf 100644 (file)
@@ -43,6 +43,8 @@
 #include "unix.h"
 #include "krt.h"
 
+const enum build_target build_target = BT_BIRD;
+
 /*
  *     Debugging
  */
index 3bf1fa770cbaadf55d2a33fa13903f9c99a7affe..4f07eda2dc10d164352ce55938ac3d3afc418095 100644 (file)
@@ -13,6 +13,7 @@
 #include <signal.h>
 #include <time.h>
 #include <unistd.h>
+#include <setjmp.h>
 
 #include <sys/ioctl.h>
 #include <sys/resource.h>
@@ -31,6 +32,8 @@
 #define sprintf_concat(s, format, ...) \
     snprintf(s + strlen(s), sizeof(s) - strlen(s), format, ##__VA_ARGS__)
 
+const enum build_target build_target = BT_TEST;
+
 static const char *request;
 static int list_tests;
 static int do_core;
@@ -51,6 +54,9 @@ const char *bt_test_id;
 int bt_result;                 /* Overall program run result */
 int bt_suite_result;           /* One suit result */
 char bt_out_fmt_buf[1024];     /* Temporary memory buffer for output of testing function */
+jmp_buf bug_jump_buf;
+int bug_expected = 0;
+char *expected_bug_message;
 
 struct timespec bt_begin, bt_suite_begin, bt_suite_case_begin;
 
@@ -417,6 +423,35 @@ bt_exit_value(void)
   return bt_result ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
+/**
+ * It tests that given function calls bug with given error massage. Sets jump, and bug() function jumps back.
+*/
+int
+bt_assert_bug(void (*functionPtr)(void), char *expected_message)
+{
+  bug_expected = 1;
+  expected_bug_message = expected_message;
+  if (setjmp(bug_jump_buf))
+  {
+    bug_expected = 0;
+    expected_bug_message = "";
+    return 1;
+  }
+  else
+    (*functionPtr)();
+  bug_expected = 0;
+  expected_bug_message = "";
+  return 0;
+}
+
+jmp_buf *
+get_test_bug_jump(char *msg)
+{
+  if (!bug_expected || strcmp(msg, expected_bug_message) != 0)
+    abort();
+  return &bug_jump_buf;
+}
+
 /**
  * bt_assert_batch__ - test a batch of inputs/outputs tests
  * @opts: includes all necessary data
index 540092d6436f532253b277d10df7892295b84241..896d3fa78448c6c58c35322cd115daacf1317899 100644 (file)
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <errno.h>
 #include <sys/types.h>
+#include <setjmp.h>
 
 #include "nest/bird.h"
 
@@ -31,7 +32,8 @@ extern const char *bt_filename;
 extern const char *bt_test_id;
 
 void bt_init(int argc, char *argv[]);
-int  bt_exit_value(void);
+int bt_exit_value(void);
+int bt_assert_bug(void (*functionPtr)(void), char *msg);
 void bt_reset_suite_case_timer(void);
 int bt_test_suite_base(int (*test_fn)(const void *), const char *test_id, const void *test_fn_argument, int forked, int timeout, const char *dsc, ...);
 static inline u64 bt_random(void)