From: Pavel TvrdĂ­k Date: Fri, 14 Aug 2015 14:14:32 +0000 (+0200) Subject: Birdtest: Add unfinished filter test X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=39059e21b751544135a81c801775057b74cb904e;p=thirdparty%2Fbird.git Birdtest: Add unfinished filter test Add birdtest utils for testing parsing configurations files Add tests with parsing filter examples --- diff --git a/conf/conf.c b/conf/conf.c index a907402d9..6933ef3ce 100644 --- a/conf/conf.c +++ b/conf/conf.c @@ -47,14 +47,9 @@ #include "nest/bird.h" #include "nest/route.h" -#include "nest/protocol.h" #include "nest/iface.h" -#include "lib/resource.h" -#include "lib/string.h" #include "lib/event.h" -#include "lib/timer.h" -#include "conf/conf.h" -#include "filter/filter.h" + static jmp_buf conf_jmpbuf; @@ -491,7 +486,7 @@ order_shutdown(void) * error in the configuration. */ void -cf_error(char *msg, ...) +cf_error(const char *msg, ...) { char buf[1024]; va_list args; diff --git a/conf/conf.h b/conf/conf.h index 3d12b5d0d..cca6da81e 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -71,7 +71,7 @@ int config_commit(struct config *, int type, int timeout); int config_confirm(void); int config_undo(void); void config_init(void); -void cf_error(char *msg, ...) NORET; +void cf_error(const char *msg, ...) NORET; void config_add_obstacle(struct config *); void config_del_obstacle(struct config *); void order_shutdown(void); diff --git a/conf/confbase.Y b/conf/confbase.Y index 5f487c1dd..529e3f47f 100644 --- a/conf/confbase.Y +++ b/conf/confbase.Y @@ -138,7 +138,7 @@ expr_us: /* Switches */ bool: - expr {$$ = !!$1; } + expr { $$ = !!$1; } | ON { $$ = 1; } | YES { $$ = 1; } | OFF { $$ = 0; } diff --git a/filter/filter_test.c b/filter/filter_test.c new file mode 100644 index 000000000..dd4ec2d83 --- /dev/null +++ b/filter/filter_test.c @@ -0,0 +1,99 @@ +/* + * Filters: Utility Functions Tests + * + * (c) 2015 CZ.NIC z.s.p.o. + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#include +#include + +#include "test/birdtest.h" +#include "test/utils.h" + +#include "filter/filter.h" +#include "lib/main_helper.h" + +static int +t_filter(void) +{ +#define TESTING_FILTER_NAME "testing_filter" + + bt_bird_init(); + + bt_config_parse( + BT_CONFIG_PARSE_ROUTER_ID + BT_CONFIG_PARSE_KERNEL_DEVICE + "\n" + "filter " TESTING_FILTER_NAME "\n" + "{\n" + " if net ~ 10.0.0.0/20 then\n" + " accept;\n" + " else\n" + " reject;\n" + "}\n" + ); + + struct symbol *sym = NULL; + sym = cf_find_symbol(TESTING_FILTER_NAME); + + /* TODO: check the testing filter */ + + return BT_SUCCESS; +} + +static char * +load_file(const char *filename) +{ + FILE *f = fopen(filename, "rb"); + bt_assert_msg(f, "Cannot open file %s", filename); + fseek(f, 0, SEEK_END); + long pos = ftell(f); + fseek(f, 0, SEEK_SET); + + char *file_body = mb_allocz(&root_pool, pos+1); + bt_assert_msg(file_body, "Memory allocation failed for file %s", filename); + bt_assert_msg(fread(file_body, pos, 1, f) == 1, "Failed reading from file %s", filename); + + fclose(f); + return file_body; +} + +static int +t_example_config_files(void *filename_void) +{ + bt_bird_init(); + + const char *filename = filename_void; + char *cfg_str = load_file(filename); + bt_config_parse(cfg_str); + mb_free(cfg_str); + + bt_debug("Parsing configuration from %s\n", filename); + config_name = filename; + read_config(); + struct config *conf = read_config(); + config_commit(conf, RECONFIG_HARD, 0); + + return bt_test_suite_success; +} + +int +main(int argc, char *argv[]) +{ + bt_init(argc, argv); + + bt_test_suite(t_filter, "Test all example config files"); + + const char *files[] = { + "filter/test.conf", + "filter/test.conf2", + "filter/test6.conf", + }; + size_t files_arr_size = sizeof(files)/sizeof(files[0]); + for (size_t i = 0; i < files_arr_size; i++) + bt_test_suite_arg_extra(t_example_config_files, files[i], BT_DEFAULT_FORKING, 30, "Test a example config file %s", files[i]); + + return bt_end(); +} diff --git a/filter/tree.c b/filter/tree.c index ee9f448a1..31ae06a81 100644 --- a/filter/tree.c +++ b/filter/tree.c @@ -63,7 +63,7 @@ tree_compare(const void *p1, const void *p2) * build_tree * @from: degenerated tree (linked by @tree->left) to be transformed into form suitable for find_tree() * - * Transforms denerated tree into balanced tree. + * Transforms degenerated tree into balanced tree. */ struct f_tree * build_tree(struct f_tree *from) diff --git a/filter/tree_test.c b/filter/tree_test.c new file mode 100644 index 000000000..f1df40c13 --- /dev/null +++ b/filter/tree_test.c @@ -0,0 +1,48 @@ +/* + * Filters: Utility Functions Tests + * + * (c) 2015 CZ.NIC z.s.p.o. + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#include "test/birdtest.h" +#include "test/utils.h" + +#include "filter/filter.h" + +static void +show_buffer(buffer *b) +{ + byte *p; + for (p=b->start; p != b->pos; p++) + bt_debug("%c", *p); + bt_debug("\n"); +} + +static int +t_tree(void) +{ + bt_bird_init(); + + struct f_tree *a = f_new_tree(); + struct f_tree *b = f_new_tree(); + bt_assert(same_tree(a, b)); + + buffer buffer1; + LOG_BUFFER_INIT(buffer1); + tree_format(a, &buffer1); + + show_buffer(&buffer1); + + return BT_SUCCESS; +} +int +main(int argc, char *argv[]) +{ + bt_init(argc, argv); + + bt_test_suite(t_tree, "Tree Test"); + + return bt_end(); +} diff --git a/sysdep/unix/main_helper.c b/sysdep/unix/main_helper.c index b46dc9179..0af7b80a0 100644 --- a/sysdep/unix/main_helper.c +++ b/sysdep/unix/main_helper.c @@ -39,6 +39,8 @@ #include "unix.h" #include "krt.h" +#include "lib/main_helper.h" + /* * Debugging */ diff --git a/sysdep/unix/main_helper.h b/sysdep/unix/main_helper.h index 9b76bd1c7..d08418e17 100644 --- a/sysdep/unix/main_helper.h +++ b/sysdep/unix/main_helper.h @@ -9,6 +9,10 @@ #ifndef _BIRD_MAIN_HELPER_H_ #define _BIRD_MAIN_HELPER_H_ +#include "lib/birdlib.h" +#include "lib/socket.h" +#include "sysdep/config.h" +#include "nest/cli.h" /* * Global variables */ diff --git a/test/utils.c b/test/utils.c new file mode 100644 index 000000000..47ce9c154 --- /dev/null +++ b/test/utils.c @@ -0,0 +1,93 @@ +/* + * BIRD Test -- Utils for testing parsing configuration file + * + * (c) 2015 CZ.NIC z.s.p.o. + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#include "test/birdtest.h" +#include "test/utils.h" + +#include "filter/filter.h" +#include "nest/iface.h" +#include "nest/locks.h" +#include "lib/unix.h" +#include "lib/krt.h" + +static const byte *bt_config_parse_pos; +static uint bt_config_parse_remain_len; + +int static +cf_txt_read(byte *dest_buf, uint max_len, UNUSED int fd) { + if (max_len > bt_config_parse_remain_len) + max_len = bt_config_parse_remain_len; + memcpy(dest_buf, bt_config_parse_pos, max_len); + bt_config_parse_pos += max_len; + bt_config_parse_remain_len -= max_len; + + return max_len; +} + +void +bt_bird_init(void) { + if(bt_verbose) + log_init_debug(""); + log_switch(bt_verbose != 0, NULL, NULL); + + resource_init(); + olock_init(); + io_init(); + rt_init(); + if_init(); + roa_init(); + config_init(); + + protos_build(); + proto_build(&proto_unix_kernel); + proto_build(&proto_unix_iface); + + bt_config_parse( + BT_CONFIG_PARSE_ROUTER_ID + BT_CONFIG_PARSE_KERNEL_DEVICE + ); +} + +static void +bt_debug_with_line_nums(const char *str) +{ + const char *c = str; + uint lineno = 1; + while (*c) + { + bt_debug("%3u ", lineno); + do + { + bt_debug("%c", *c); + } while (*c && *(c++) != '\n'); + lineno++; + } + bt_debug("\n"); +} + +struct config * +bt_config_parse(const char *str_cfg) +{ + bt_debug("Parsing new configuration:\n"); + bt_debug_with_line_nums(str_cfg); + struct config *cfg = config_alloc(""); + bt_config_parse_pos = str_cfg; + bt_config_parse_remain_len = strlen(str_cfg); + cf_read_hook = cf_txt_read; + + if (config_parse(cfg)) + { + config_commit(cfg, RECONFIG_HARD, 0); + new_config = cfg; + + return cfg; + } + + bt_assert_msg(0, "At line %d is error: %s \n", new_config->err_lino, new_config->err_msg); + return NULL; +} diff --git a/test/utils.h b/test/utils.h new file mode 100644 index 000000000..fdb24fbe5 --- /dev/null +++ b/test/utils.h @@ -0,0 +1,18 @@ +/* + * BIRD Test -- Utils for testing parsing configuration file + * + * (c) 2015 CZ.NIC z.s.p.o. + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#ifndef _BIRDTEST_UTILS_H_ +#define _BIRDTEST_UTILS_H_ + +#define BT_CONFIG_PARSE_ROUTER_ID "router id 10.0.0.1; \n" +#define BT_CONFIG_PARSE_KERNEL_DEVICE "protocol device {} \n" + +void bt_bird_init(void); +struct config *bt_config_parse(const char *str_cfg); + +#endif /* _BIRDTEST_UTILS_H_ */ \ No newline at end of file diff --git a/tools/Makefile-top.in b/tools/Makefile-top.in index c68ed5470..d37931e71 100644 --- a/tools/Makefile-top.in +++ b/tools/Makefile-top.in @@ -23,4 +23,5 @@ distclean: clean clean-tests: find . -name '*_test' | xargs rm -f - rm -f $(objdir)/test/birdtest.o \ No newline at end of file + find . -name '*_test.o' | xargs rm -f + rm -f $(objdir)/test/*.o \ No newline at end of file diff --git a/tools/Makefile.in b/tools/Makefile.in index cd18a801b..1eb1ecce0 100644 --- a/tools/Makefile.in +++ b/tools/Makefile.in @@ -9,11 +9,14 @@ include Rules all: sysdep/paths.h .dep-stamp subdir lib/main.o daemon birdcl @CLIENT@ -tests: test/birdtest.o +tests: test/all.o set -e ; for a in $(dynamic-dirs) ; do $(MAKE) -C $$a $@ ; done set -e ; for a in $(static-dirs) $(client-dirs) ; do $(MAKE) -C $$a -f $(srcdir_abs)/$$a/Makefile $@ ; done -test/birdtest.o: $(srcdir)/test/birdtest.c $(srcdir)/test/birdtest.h +test/all.o: test/birdtest.o test/utils.o + $(CC) -nostdlib -r -o $@ $^ + +test/%.o: $(srcdir)/test/%.c mkdir -p test $(CC) $(CFLAGS) $(TARGET_ARCH) -c $< $(LDLIBS) -o $@ diff --git a/tools/Rules.in b/tools/Rules.in index dcf077885..c383dad82 100644 --- a/tools/Rules.in +++ b/tools/Rules.in @@ -52,7 +52,7 @@ tests: $(tests_executables) test-dep := $(addprefix $(root-rel), $(addsuffix /all.o, $(static-dirs)) conf/all.o lib/birdlib.a) -%_test: $(srcdir)/$(dir-name)/%_test.o $(root-rel)test/birdtest.o $(test-dep) +%_test: $(srcdir)/$(dir-name)/%_test.o $(root-rel)test/all.o $(test-dep) $(CC) $(LDFLAGS) $^ $(LIBS) -o $@ ifdef source