summaryrefslogtreecommitdiffstats
path: root/tests/helpers
diff options
context:
space:
mode:
authorChristian Franke <chris@opensourcerouting.org>2017-02-03 16:58:11 +0100
committerChristian Franke <chris@opensourcerouting.org>2017-02-13 16:54:48 +0100
commit30c48775607a8b0e277360e7d87a04b5a3356b80 (patch)
tree3486a692aab57e5a6f19cedd73c00bbc4199a3a9 /tests/helpers
parenttests: remove dejagnu (diff)
downloadfrr-30c48775607a8b0e277360e7d87a04b5a3356b80.tar.xz
frr-30c48775607a8b0e277360e7d87a04b5a3356b80.zip
tests: reorganize tests hierarchically
Signed-off-by: Christian Franke <chris@opensourcerouting.org>
Diffstat (limited to 'tests/helpers')
-rw-r--r--tests/helpers/c/main.c201
-rw-r--r--tests/helpers/c/prng.c132
-rw-r--r--tests/helpers/c/prng.h38
-rw-r--r--tests/helpers/c/tests.h31
4 files changed, 402 insertions, 0 deletions
diff --git a/tests/helpers/c/main.c b/tests/helpers/c/main.c
new file mode 100644
index 000000000..b3e6e706f
--- /dev/null
+++ b/tests/helpers/c/main.c
@@ -0,0 +1,201 @@
+/*
+ * $Id: main.c,v 1.1 2005/04/25 16:42:24 paul Exp $
+ *
+ * This file is part of Quagga.
+ *
+ * Quagga is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * Quagga is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Quagga; see the file COPYING. If not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#include <zebra.h>
+
+#include <lib/version.h>
+#include "getopt.h"
+#include "thread.h"
+#include "vty.h"
+#include "command.h"
+#include "memory.h"
+#include "memory_vty.h"
+
+extern void test_init();
+
+struct thread_master *master;
+
+struct option longopts[] =
+{
+ { "daemon", no_argument, NULL, 'd'},
+ { "config_file", required_argument, NULL, 'f'},
+ { "help", no_argument, NULL, 'h'},
+ { "vty_addr", required_argument, NULL, 'A'},
+ { "vty_port", required_argument, NULL, 'P'},
+ { "version", no_argument, NULL, 'v'},
+ { 0 }
+};
+
+DEFUN (daemon_exit,
+ daemon_exit_cmd,
+ "daemon-exit",
+ "Make the daemon exit\n")
+{
+ exit(0);
+}
+
+static int timer_count;
+static int
+test_timer (struct thread *thread)
+{
+ int *count = THREAD_ARG(thread);
+
+ printf ("run %d of timer\n", (*count)++);
+ thread_add_timer (master, test_timer, count, 5);
+ return 0;
+}
+
+static void
+test_timer_init()
+{
+ thread_add_timer (master, test_timer, &timer_count, 10);
+}
+
+static void
+test_vty_init()
+{
+ install_element (VIEW_NODE, &daemon_exit_cmd);
+}
+
+/* Help information display. */
+static void
+usage (char *progname, int status)
+{
+ if (status != 0)
+ fprintf (stderr, "Try `%s --help' for more information.\n", progname);
+ else
+ {
+ printf ("Usage : %s [OPTION...]\n\
+Daemon which does 'slow' things.\n\n\
+-d, --daemon Runs in daemon mode\n\
+-f, --config_file Set configuration file name\n\
+-A, --vty_addr Set vty's bind address\n\
+-P, --vty_port Set vty's port number\n\
+-v, --version Print program version\n\
+-h, --help Display this help and exit\n\
+\n\
+Report bugs to %s\n", progname, FRR_BUG_ADDRESS);
+ }
+ exit (status);
+}
+
+
+/* main routine. */
+int
+main (int argc, char **argv)
+{
+ char *p;
+ char *vty_addr = NULL;
+ int vty_port = 4000;
+ int daemon_mode = 0;
+ char *progname;
+ struct thread thread;
+ char *config_file = NULL;
+
+ /* Set umask before anything for security */
+ umask (0027);
+
+ /* get program name */
+ progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
+
+ /* master init. */
+ master = thread_master_create ();
+
+ while (1)
+ {
+ int opt;
+
+ opt = getopt_long (argc, argv, "dhf:A:P:v", longopts, 0);
+
+ if (opt == EOF)
+ break;
+
+ switch (opt)
+ {
+ case 0:
+ break;
+ case 'f':
+ config_file = optarg;
+ break;
+ case 'd':
+ daemon_mode = 1;
+ break;
+ case 'A':
+ vty_addr = optarg;
+ break;
+ case 'P':
+ /* Deal with atoi() returning 0 on failure */
+ if (strcmp(optarg, "0") == 0)
+ {
+ vty_port = 0;
+ break;
+ }
+ vty_port = atoi (optarg);
+ vty_port = (vty_port ? vty_port : 4000);
+ break;
+ case 'v':
+ print_version (progname);
+ exit (0);
+ break;
+ case 'h':
+ usage (progname, 0);
+ break;
+ default:
+ usage (progname, 1);
+ break;
+ }
+ }
+
+ /* Library inits. */
+ cmd_init (1);
+ vty_init (master);
+ memory_init ();
+
+ /* OSPF vty inits. */
+ test_vty_init ();
+
+ /* Change to the daemon program. */
+ if (daemon_mode && daemon (0, 0) < 0)
+ {
+ fprintf(stderr, "daemon failed: %s", strerror(errno));
+ exit (1);
+ }
+
+ /* Create VTY socket */
+ vty_serv_sock (vty_addr, vty_port, "/tmp/.heavy.sock");
+
+ /* Configuration file read*/
+ if (!config_file)
+ usage (progname, 1);
+ vty_read_config (config_file, NULL);
+
+ test_timer_init();
+
+ test_init();
+
+ /* Fetch next active thread. */
+ while (thread_fetch (master, &thread))
+ thread_call (&thread);
+
+ /* Not reached. */
+ exit (0);
+}
+
diff --git a/tests/helpers/c/prng.c b/tests/helpers/c/prng.c
new file mode 100644
index 000000000..bdcfb07af
--- /dev/null
+++ b/tests/helpers/c/prng.c
@@ -0,0 +1,132 @@
+/*
+ * Very simple prng to allow for randomized tests with reproducable
+ * results.
+ *
+ * Copyright (C) 2012 by Open Source Routing.
+ * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
+ *
+ * This file is part of Quagga
+ *
+ * Quagga is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * Quagga is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Quagga; see the file COPYING. If not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#include <zebra.h>
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "prng.h"
+
+struct prng
+{
+ unsigned long long state1;
+ unsigned long long state2;
+};
+
+static char
+prng_bit(struct prng *prng)
+{
+ prng->state1 *= 2416;
+ prng->state1 += 374441;
+ prng->state1 %= 1771875;
+
+ if (prng->state1 % 2)
+ {
+ prng->state2 *= 84589;
+ prng->state2 += 45989;
+ prng->state2 %= 217728;
+ }
+
+ return prng->state2 % 2;
+}
+
+struct prng*
+prng_new(unsigned long long seed)
+{
+ struct prng *rv = calloc(sizeof(*rv), 1);
+ assert(rv);
+
+ rv->state1 = rv->state2 = seed;
+
+ return rv;
+}
+
+unsigned int
+prng_rand(struct prng *prng)
+{
+ unsigned int i, rv = 0;
+
+ for (i = 0; i < 32; i++)
+ {
+ rv |= prng_bit(prng);
+ rv <<= 1;
+ }
+ return rv;
+}
+
+const char *
+prng_fuzz(struct prng *prng,
+ const char *string,
+ const char *charset,
+ unsigned int operations)
+{
+ static char buf[256];
+ unsigned int charset_len;
+ unsigned int i;
+ unsigned int offset;
+ unsigned int op;
+ unsigned int character;
+
+ assert(strlen(string) < sizeof(buf));
+
+ strncpy(buf, string, sizeof(buf));
+ charset_len = strlen(charset);
+
+ for (i = 0; i < operations; i++)
+ {
+ offset = prng_rand(prng) % strlen(buf);
+ op = prng_rand(prng) % 3;
+
+ switch (op)
+ {
+ case 0:
+ /* replace */
+ character = prng_rand(prng) % charset_len;
+ buf[offset] = charset[character];
+ break;
+ case 1:
+ /* remove */
+ memmove(buf + offset, buf + offset + 1, strlen(buf) - offset);
+ break;
+ case 2:
+ /* insert */
+ assert(strlen(buf) + 1 < sizeof(buf));
+
+ memmove(buf + offset + 1, buf + offset, strlen(buf) + 1 - offset);
+ character = prng_rand(prng) % charset_len;
+ buf[offset] = charset[character];
+ break;
+ }
+ }
+ return buf;
+}
+
+void
+prng_free(struct prng *prng)
+{
+ free(prng);
+}
diff --git a/tests/helpers/c/prng.h b/tests/helpers/c/prng.h
new file mode 100644
index 000000000..cf0bacc5f
--- /dev/null
+++ b/tests/helpers/c/prng.h
@@ -0,0 +1,38 @@
+/*
+ * Very simple prng to allow for randomized tests with reproducable
+ * results.
+ *
+ * Copyright (C) 2012 by Open Source Routing.
+ * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
+ *
+ * This file is part of Quagga
+ *
+ * Quagga is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * Quagga is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Quagga; see the file COPYING. If not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+#ifndef _PRNG_H
+#define _PRNG_H
+
+struct prng;
+
+struct prng* prng_new(unsigned long long seed);
+unsigned int prng_rand(struct prng*);
+const char * prng_fuzz(struct prng*,
+ const char *string,
+ const char *charset,
+ unsigned int operations);
+void prng_free(struct prng *);
+
+#endif
diff --git a/tests/helpers/c/tests.h b/tests/helpers/c/tests.h
new file mode 100644
index 000000000..a528e55f0
--- /dev/null
+++ b/tests/helpers/c/tests.h
@@ -0,0 +1,31 @@
+/*
+ * Test wrappers common header file
+ *
+ * Copyright (C) 2015 by David Lamparter,
+ * for Open Source Routing./ NetDEF, Inc.
+ *
+ * This file is part of Quagga
+ *
+ * Quagga is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * Quagga is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Quagga; see the file COPYING. If not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#ifndef _QUAGGA_TESTS_H
+#define _QUAGGA_TESTS_H
+
+extern void test_init (void);
+extern void test_init_cmd (void);
+
+#endif /* _QUAGGA_TESTS_H */