From 0bcb1f9746cb9db5274e4fcacdda4f686a27743c Mon Sep 17 00:00:00 2001 From: Katerina Kubecova Date: Tue, 26 Mar 2024 16:46:34 +0100 Subject: [PATCH] nest/proto.c: if reload called on not-UP protocol, it logs that no reload will be done birdtest: in unit tests, function bt_assert_bug() checks if given code calls bug() with given message --- lib/Makefile | 2 +- lib/birdlib.h | 9 +++++++++ nest/proto.c | 3 +++ sysdep/unix/log.c | 3 +++ sysdep/unix/main.c | 2 ++ test/birdtest.c | 35 +++++++++++++++++++++++++++++++++++ test/birdtest.h | 4 +++- 7 files changed, 56 insertions(+), 2 deletions(-) diff --git a/lib/Makefile b/lib/Makefile index 812f721c..76640a45 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -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) diff --git a/lib/birdlib.h b/lib/birdlib.h index b7226411..534886f3 100644 --- a/lib/birdlib.h +++ b/lib/birdlib.h @@ -9,6 +9,8 @@ #ifndef _BIRD_BIRDLIB_H_ #define _BIRD_BIRDLIB_H_ +#include + #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); } diff --git a/nest/proto.c b/nest/proto.c index 88f4813e..25f14249 100644 --- a/nest/proto.c +++ b/nest/proto.c @@ -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) diff --git a/sysdep/unix/log.c b/sysdep/unix/log.c index 613a6aa5..d9281021 100644 --- a/sysdep/unix/log.c +++ b/sysdep/unix/log.c @@ -23,6 +23,7 @@ #include #include #include +#include #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); diff --git a/sysdep/unix/main.c b/sysdep/unix/main.c index 0d7788bb..0616b608 100644 --- a/sysdep/unix/main.c +++ b/sysdep/unix/main.c @@ -43,6 +43,8 @@ #include "unix.h" #include "krt.h" +const enum build_target build_target = BT_BIRD; + /* * Debugging */ diff --git a/test/birdtest.c b/test/birdtest.c index 3bf1fa77..4f07eda2 100644 --- a/test/birdtest.c +++ b/test/birdtest.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -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 diff --git a/test/birdtest.h b/test/birdtest.h index 540092d6..896d3fa7 100644 --- a/test/birdtest.h +++ b/test/birdtest.h @@ -13,6 +13,7 @@ #include #include #include +#include #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)