Commit 39059e21 authored by Pavel Tvrdík's avatar Pavel Tvrdík
Browse files

Birdtest: Add unfinished filter test

Add birdtest utils for testing parsing configurations files
Add tests with parsing filter examples
parent aa96f486
Loading
Loading
Loading
Loading
+2 −7
Original line number Diff line number Diff line
@@ -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;
+1 −1
Original line number Diff line number Diff line
@@ -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);

filter/filter_test.c

0 → 100644
+99 −0
Original line number Diff line number Diff line
/*
 *	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();
}
+1 −1
Original line number Diff line number Diff line
@@ -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)

filter/tree_test.c

0 → 100644
+48 −0
Original line number Diff line number Diff line
/*
 *	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();
}
Loading