]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Birdtest: Add unfinished filter test
authorPavel Tvrdík <pawel.tvrdik@gmail.cz>
Fri, 14 Aug 2015 14:14:32 +0000 (16:14 +0200)
committerPavel Tvrdík <pawel.tvrdik@gmail.cz>
Fri, 14 Aug 2015 14:14:32 +0000 (16:14 +0200)
Add birdtest utils for testing parsing configurations files
Add tests with parsing filter examples

13 files changed:
conf/conf.c
conf/conf.h
conf/confbase.Y
filter/filter_test.c [new file with mode: 0644]
filter/tree.c
filter/tree_test.c [new file with mode: 0644]
sysdep/unix/main_helper.c
sysdep/unix/main_helper.h
test/utils.c [new file with mode: 0644]
test/utils.h [new file with mode: 0644]
tools/Makefile-top.in
tools/Makefile.in
tools/Rules.in

index a907402d936ef34062326ce65b196213de7ce8f8..6933ef3ceeea54651167aff76f73e438eae339fa 100644 (file)
 
 #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;
index 3d12b5d0d952bd827757d1a00eed42eb358438ea..cca6da81e1e9f36aaf43b191cd45bd1aaedd15a9 100644 (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);
index 5f487c1dd0503dab783883fa6ef08c570d78349e..529e3f47fed41be5c536a5af60ef64f9fa8f77fa 100644 (file)
@@ -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 (file)
index 0000000..dd4ec2d
--- /dev/null
@@ -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();
+}
index ee9f448a11e3399284ccfded838fe396cbf471a5..31ae06a81a50295cf767d7a86e99d0be407b0da8 100644 (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)
diff --git a/filter/tree_test.c b/filter/tree_test.c
new file mode 100644 (file)
index 0000000..f1df40c
--- /dev/null
@@ -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();
+}
index b46dc9179f8b417a89e2cd5c09cc4e547211b3af..0af7b80a0635b8ba0dfdeade50cb435f17daa5bd 100644 (file)
@@ -39,6 +39,8 @@
 #include "unix.h"
 #include "krt.h"
 
+#include "lib/main_helper.h"
+
 /*
  *     Debugging
  */
index 9b76bd1c7ad89aa1ef4ad3ca3d06e26dddea2fa6..d08418e171631cfc6492a67b99614a90afb54d6f 100644 (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
  */
diff --git a/test/utils.c b/test/utils.c
new file mode 100644 (file)
index 0000000..47ce9c1
--- /dev/null
@@ -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 (file)
index 0000000..fdb24fb
--- /dev/null
@@ -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
index c68ed54702ecbcf52f9746431ae4d0f67cd28a59..d37931e711a637be452b9144e09988c2d95e84dc 100644 (file)
@@ -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
index cd18a801bae688d512738a5a5547e7b41fd6c95f..1eb1ecce0deeef256bc359fb11034f6189c3ef67 100644 (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 $@
 
index dcf077885528d9fa394d7c67666e8a671a603282..c383dad8231567075efc8ec49f9d562fe9f7f03f 100644 (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