aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/sys/netgraph/Makefile6
-rw-r--r--tests/sys/netgraph/basic.c191
-rw-r--r--tests/sys/netgraph/util.c277
-rw-r--r--tests/sys/netgraph/util.h114
4 files changed, 587 insertions, 1 deletions
diff --git a/tests/sys/netgraph/Makefile b/tests/sys/netgraph/Makefile
index aef190bbe178..9f220a620bbe 100644
--- a/tests/sys/netgraph/Makefile
+++ b/tests/sys/netgraph/Makefile
@@ -10,6 +10,10 @@ TAP_TESTS_SH+= ng_macfilter_test
TEST_METADATA.ng_macfilter_test+= required_user="root"
TEST_METADATA.ng_macfilter_test+= required_programs="perl"
-MAN=
+ATF_TESTS_C+= basic \
+
+SRCS.basic= basic.c util.c
+
+LIBADD+= netgraph
.include <bsd.test.mk>
diff --git a/tests/sys/netgraph/basic.c b/tests/sys/netgraph/basic.c
new file mode 100644
index 000000000000..573422add694
--- /dev/null
+++ b/tests/sys/netgraph/basic.c
@@ -0,0 +1,191 @@
+/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright 2021 Lutz Donnerhacke
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+ * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <atf-c.h>
+#include <errno.h>
+#include <stdio.h>
+
+#include "util.h"
+
+ATF_TC(send_recv);
+ATF_TC_HEAD(send_recv, conf)
+{
+ atf_tc_set_md_var(conf, "require.user", "root");
+}
+
+ATF_TC_BODY(send_recv, dummy)
+{
+ char msg[] = "test";
+ ng_counter_t r;
+
+ ng_init();
+ ng_connect(".", "a", ".", "b");
+ ng_register_data("b", get_data0);
+ ng_send_data("a", msg, sizeof(msg));
+
+ ng_counter_clear(r);
+ ng_handle_events(50, &r);
+ ATF_CHECK(r[0] == 1);
+}
+
+ATF_TC(node);
+ATF_TC_HEAD(node, conf)
+{
+ atf_tc_set_md_var(conf, "require.user", "root");
+}
+
+ATF_TC_BODY(node, dummy)
+{
+ char msg[] = "test";
+ ng_counter_t r;
+
+ ng_init();
+ ng_mkpeer(".", "a", "hub", "a");
+ ng_name("a", "test hub");
+
+ ng_errors(PASS);
+ ng_name("a", "test hub");
+ ng_errors(FAIL);
+ if (errno == EADDRINUSE)
+ atf_tc_expect_fail("PR241954");
+ ATF_CHECK_ERRNO(0, 1);
+ atf_tc_expect_pass();
+
+ ng_connect(".", "b", "test hub:", "b");
+ ng_connect(".", "c", "test hub:", "c");
+ ng_register_data("a", get_data0);
+ ng_register_data("b", get_data1);
+ ng_register_data("c", get_data2);
+
+ ng_counter_clear(r);
+ ng_send_data("a", msg, sizeof(msg));
+ ng_handle_events(50, &r);
+ ATF_CHECK(r[0] == 0 && r[1] == 1 && r[2] == 1);
+
+ ng_rmhook(".", "b");
+ ng_counter_clear(r);
+ ng_send_data("a", msg, sizeof(msg));
+ ng_handle_events(50, &r);
+ ATF_CHECK(r[0] == 0 && r[1] == 0 && r[2] == 1);
+
+ ng_shutdown("test hub:");
+}
+
+ATF_TC(message);
+ATF_TC_HEAD(message, conf)
+{
+ atf_tc_set_md_var(conf, "require.user", "root");
+}
+
+ATF_TC_BODY(message, dummy)
+{
+ ng_init();
+ ng_mkpeer(".", "a", "hub", "a");
+ ng_name("a", "test hub");
+
+ ng_send_msg("test hub:", "setpersistent");
+ ng_rmhook(".", "a");
+
+ ng_shutdown("test hub:");
+}
+
+ATF_TC(same_name);
+ATF_TC_HEAD(same_name, conf)
+{
+ atf_tc_set_md_var(conf, "require.user", "root");
+}
+
+ATF_TC_BODY(same_name, dummy)
+{
+ ng_init();
+ ng_mkpeer(".", "a", "hub", "a");
+ ng_name("a", "test");
+
+ ng_errors(PASS);
+ ng_connect(".", "a", ".", "b");
+ ATF_CHECK_ERRNO(EEXIST, 1);
+ ng_connect(".", "b", ".", "b");
+ ATF_CHECK_ERRNO(EEXIST, 1);
+ ng_name(".", "test");
+ ATF_CHECK_ERRNO(EADDRINUSE, 1);
+
+ ng_errors(FAIL);
+ ng_shutdown("test:");
+}
+
+ATF_TC(queuelimit);
+ATF_TC_HEAD(queuelimit, conf)
+{
+ atf_tc_set_md_var(conf, "require.user", "root");
+}
+
+ATF_TC_BODY(queuelimit, dummy)
+{
+ ng_counter_t r;
+ int i;
+ char msg[] = "test";
+ const int MAX = 1000;
+
+ ng_init();
+ ng_connect(".", "a", ".", "b");
+ ng_register_data("b", get_data0);
+
+ ng_errors(PASS);
+ for (i = 0; i < MAX; i++)
+ {
+ ng_send_data("a", msg, sizeof(msg));
+ if (errno != 0)
+ break;
+ /* no ng_handle_events -> messages stall */
+ }
+ ng_errors(FAIL);
+
+ ng_counter_clear(r);
+ ng_handle_events(50, &r);
+ ATF_CHECK(r[0] > 100);
+ ATF_CHECK(r[0] == i);
+ atf_tc_expect_fail("Queue full (%d)", i);
+ ATF_CHECK(r[0] == MAX);
+ atf_tc_expect_pass();
+}
+
+ATF_TP_ADD_TCS(basic)
+{
+ ATF_TP_ADD_TC(basic, send_recv);
+ ATF_TP_ADD_TC(basic, node);
+ ATF_TP_ADD_TC(basic, message);
+ ATF_TP_ADD_TC(basic, same_name);
+ ATF_TP_ADD_TC(basic, queuelimit);
+
+ return atf_no_error();
+}
diff --git a/tests/sys/netgraph/util.c b/tests/sys/netgraph/util.c
new file mode 100644
index 000000000000..b25e63dfa76d
--- /dev/null
+++ b/tests/sys/netgraph/util.c
@@ -0,0 +1,277 @@
+/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright 2021 Lutz Donnerhacke
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+ * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <atf-c.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/select.h>
+#include <sys/queue.h>
+
+#include "util.h"
+
+
+static int cs = -1, ds = -1;
+static ng_error_t error_handling = FAIL;
+
+#define CHECK(r, x) do { \
+ if (!(x)) { \
+ if (error_handling == PASS) \
+ return r; \
+ atf_tc_fail_requirement(file, line, "%s (%s)", \
+ #x " not met", strerror(errno));\
+ } \
+} while(0)
+
+struct data_handler
+{
+ char const *hook;
+ ng_data_handler_t handler;
+ SLIST_ENTRY(data_handler) next;
+};
+static SLIST_HEAD(, data_handler) data_head = SLIST_HEAD_INITIALIZER(data_head);
+static ng_msg_handler_t msg_handler = NULL;
+
+static void handle_data(void *ctx);
+static void handle_msg(void *ctx);
+
+void
+_ng_connect(char const *path1, char const *hook1,
+ char const *path2, char const *hook2,
+ char const *file, size_t line)
+{
+ struct ngm_connect c;
+
+ strncpy(c.ourhook, hook1, sizeof(c.ourhook));
+ strncpy(c.peerhook, hook2, sizeof(c.peerhook));
+ strncpy(c.path, path2, sizeof(c.path));
+
+ CHECK(, -1 != NgSendMsg(cs, path1,
+ NGM_GENERIC_COOKIE, NGM_CONNECT,
+ &c, sizeof(c)));
+}
+
+void
+_ng_mkpeer(char const *path1, char const *hook1,
+ char const *type, char const *hook2,
+ char const *file, size_t line)
+{
+ struct ngm_mkpeer p;
+
+ strncpy(p.ourhook, hook1, sizeof(p.ourhook));
+ strncpy(p.peerhook, hook2, sizeof(p.peerhook));
+ strncpy(p.type, type, sizeof(p.type));
+
+ CHECK(, -1 != NgSendMsg(cs, path1,
+ NGM_GENERIC_COOKIE, NGM_MKPEER,
+ &p, sizeof(p)));
+}
+
+void
+_ng_rmhook(char const *path, char const *hook,
+ char const *file, size_t line)
+{
+ struct ngm_rmhook h;
+
+ strncpy(h.ourhook, hook, sizeof(h.ourhook));
+
+ CHECK(, -1 != NgSendMsg(cs, path,
+ NGM_GENERIC_COOKIE, NGM_RMHOOK,
+ &h, sizeof(h)));
+}
+
+void
+_ng_name(char const *path, char const *name,
+ char const *file, size_t line)
+{
+ struct ngm_name n;
+
+ strncpy(n.name, name, sizeof(n.name));
+
+ CHECK(, -1 != NgSendMsg(cs, path,
+ NGM_GENERIC_COOKIE, NGM_NAME,
+ &n, sizeof(n)));
+}
+
+void
+_ng_shutdown(char const *path,
+ char const *file, size_t line)
+{
+ CHECK(, -1 != NgSendMsg(cs, path,
+ NGM_GENERIC_COOKIE, NGM_SHUTDOWN,
+ NULL, 0));
+}
+
+void
+ng_register_data(char const *hook, ng_data_handler_t proc)
+{
+ struct data_handler *p;
+
+ ATF_REQUIRE(NULL != (p = calloc(1, sizeof(struct data_handler))));
+ ATF_REQUIRE(NULL != (p->hook = strdup(hook)));
+ ATF_REQUIRE(NULL != (p->handler = proc));
+ SLIST_INSERT_HEAD(&data_head, p, next);
+}
+
+void
+_ng_send_data(char const *hook,
+ void const *data, size_t len,
+ char const *file, size_t line)
+{
+ CHECK(, -1 != NgSendData(ds, hook, data, len));
+}
+
+void
+ng_register_msg(ng_msg_handler_t proc)
+{
+ msg_handler = proc;
+}
+
+static void
+handle_msg(void *ctx)
+{
+ struct ng_mesg *m;
+ char path[NG_PATHSIZ];
+
+ ATF_REQUIRE(-1 != NgAllocRecvMsg(cs, &m, path));
+
+ if (msg_handler != NULL)
+ (*msg_handler) (path, m, ctx);
+
+ free(m);
+}
+
+static void
+handle_data(void *ctx)
+{
+ char hook[NG_HOOKSIZ];
+ struct data_handler *hnd;
+ u_char *data;
+ int len;
+
+ ATF_REQUIRE(0 < (len = NgAllocRecvData(ds, &data, hook)));
+ SLIST_FOREACH(hnd, &data_head, next)
+ {
+ if (0 == strcmp(hnd->hook, hook))
+ break;
+ }
+
+ if (hnd != NULL)
+ (*(hnd->handler)) (data, len, ctx);
+
+ free(data);
+}
+
+int
+ng_handle_event(unsigned int ms, void *context)
+{
+ fd_set fds;
+ int maxfd = (ds < cs) ? cs : ds;
+ struct timeval timeout = {0, ms * 1000lu};
+
+ FD_ZERO(&fds);
+ FD_SET(cs, &fds);
+ FD_SET(ds, &fds);
+retry:
+ switch (select(maxfd + 1, &fds, NULL, NULL, &timeout))
+ {
+ case -1:
+ ATF_REQUIRE_ERRNO(EINTR, 1);
+ goto retry;
+ case 0: /* timeout */
+ return 0;
+ default: /* something to do */
+ if (FD_ISSET(cs, &fds))
+ handle_msg(context);
+ if (FD_ISSET(ds, &fds))
+ handle_data(context);
+ return 1;
+ }
+}
+
+void
+ng_handle_events(unsigned int ms, void *context)
+{
+ while (ng_handle_event(ms, context))
+ ;
+}
+
+int
+_ng_send_msg(char const *path, char const *msg,
+ char const *file, size_t line)
+{
+ int res;
+
+ CHECK(-1, -1 != (res = NgSendAsciiMsg(cs, path, "%s", msg)));
+ return (res);
+}
+
+ng_error_t
+ng_errors(ng_error_t n)
+{
+ ng_error_t o = error_handling;
+
+ error_handling = n;
+ return (o);
+}
+
+void
+_ng_init(char const *file, size_t line)
+{
+ if (cs >= 0) /* prevent reinit */
+ return;
+
+ CHECK(, 0 == NgMkSockNode(NULL, &cs, &ds));
+ NgSetDebug(3);
+}
+
+#define GD(x) void \
+get_data##x(void *data, size_t len, void *ctx) {\
+ int *cnt = ctx; \
+ \
+ (void)data; \
+ (void)len; \
+ cnt[x]++; \
+}
+
+GD(0)
+GD(1)
+GD(2)
+GD(3)
+GD(4)
+GD(5)
+GD(6)
+GD(7)
+GD(8)
+GD(9)
diff --git a/tests/sys/netgraph/util.h b/tests/sys/netgraph/util.h
new file mode 100644
index 000000000000..44c9f3a360a7
--- /dev/null
+++ b/tests/sys/netgraph/util.h
@@ -0,0 +1,114 @@
+/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright 2021 Lutz Donnerhacke
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+ * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <netgraph.h>
+
+void
+_ng_connect(char const *path1, char const *hook1,
+ char const *path2, char const *hook2,
+ char const *file, size_t line);
+#define ng_connect(p1,h1,p2,h2) \
+ _ng_connect(p1,h1,p2,h2,__FILE__,__LINE__)
+
+void
+_ng_mkpeer(char const *path1, char const *hook1,
+ char const *type, char const *hook2,
+ char const *file, size_t line);
+#define ng_mkpeer(p1,h1,t,h2) \
+ _ng_mkpeer(p1,h1,t,h2,__FILE__,__LINE__)
+
+void
+_ng_shutdown(char const *path,
+ char const *file, size_t line);
+#define ng_shutdown(p) \
+ _ng_shutdown(p,__FILE__,__LINE__)
+
+void
+_ng_rmhook(char const *path, char const *hook,
+ char const *file, size_t line);
+#define ng_rmhook(p,h) \
+ _ng_rmhook(p,h,__FILE__,__LINE__)
+
+void
+_ng_name(char const *path, char const *name,
+ char const *file, size_t line);
+#define ng_name(p,n) \
+ _ng_name(p,n,__FILE__,__LINE__)
+
+
+typedef void (*ng_data_handler_t)(void *, size_t, void *ctx);
+void ng_register_data(char const *hook, ng_data_handler_t proc);
+void
+_ng_send_data(char const *hook, void const *, size_t,
+ char const *file, size_t line);
+#define ng_send_data(h,d,l) \
+ _ng_send_data(h,d,l,__FILE__,__LINE__)
+
+typedef void (*ng_msg_handler_t)(char const *, struct ng_mesg *, void *);
+void ng_register_msg(ng_msg_handler_t proc);
+int
+_ng_send_msg(char const *path, char const *msg,
+ char const *file, size_t line);
+#define ng_send_msg(p,m) \
+ _ng_send_msg(p,m,__FILE__,__LINE__)
+
+int ng_handle_event(unsigned int ms, void *ctx);
+void ng_handle_events(unsigned int ms, void *ctx);
+
+typedef enum
+{
+ FAIL, PASS
+} ng_error_t;
+ng_error_t ng_errors(ng_error_t);
+
+void _ng_init(char const *file, size_t line);
+#define ng_init() \
+ _ng_init(__FILE__,__LINE__)
+
+/* Helper function to count received data */
+
+typedef int ng_counter_t[10];
+#define ng_counter_clear(x)\
+ bzero((x), sizeof(x))
+
+void get_data0(void *data, size_t len, void *ctx);
+void get_data1(void *data, size_t len, void *ctx);
+void get_data2(void *data, size_t len, void *ctx);
+void get_data3(void *data, size_t len, void *ctx);
+void get_data4(void *data, size_t len, void *ctx);
+void get_data5(void *data, size_t len, void *ctx);
+void get_data6(void *data, size_t len, void *ctx);
+void get_data7(void *data, size_t len, void *ctx);
+void get_data8(void *data, size_t len, void *ctx);
+void get_data9(void *data, size_t len, void *ctx);