0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-10-18 09:58:43 +00:00

Birdtest: Add unfinished filter test

Add birdtest utils for testing parsing configurations files
Add tests with parsing filter examples
This commit is contained in:
Pavel Tvrdík 2015-08-14 16:14:32 +02:00
parent aa96f486e0
commit 39059e21b7
13 changed files with 277 additions and 14 deletions

View File

@ -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;

View File

@ -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);

View File

@ -138,7 +138,7 @@ expr_us:
/* Switches */
bool:
expr {$$ = !!$1; }
expr { $$ = !!$1; }
| ON { $$ = 1; }
| YES { $$ = 1; }
| OFF { $$ = 0; }

99
filter/filter_test.c Normal file
View File

@ -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 <string.h>
#include <stdlib.h>
#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();
}

View File

@ -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)

48
filter/tree_test.c Normal file
View File

@ -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();
}

View File

@ -39,6 +39,8 @@
#include "unix.h"
#include "krt.h"
#include "lib/main_helper.h"
/*
* Debugging
*/

View File

@ -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
*/

93
test/utils.c Normal file
View File

@ -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;
}

18
test/utils.h Normal file
View File

@ -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_ */

View File

@ -23,4 +23,5 @@ distclean: clean
clean-tests:
find . -name '*_test' | xargs rm -f
rm -f $(objdir)/test/birdtest.o
find . -name '*_test.o' | xargs rm -f
rm -f $(objdir)/test/*.o

View File

@ -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 $@

View File

@ -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