aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/netgraph/basic.c
diff options
context:
space:
mode:
authorLutz Donnerhacke <donner@FreeBSD.org>2021-06-02 22:29:46 +0000
committerLutz Donnerhacke <donner@FreeBSD.org>2021-06-15 12:31:32 +0000
commit36c7408818c53ae0c1f1aee0250c5d3fe16b06e0 (patch)
treee9c680fe4f0a76a3083be5f20f599f0f9695a9a9 /tests/sys/netgraph/basic.c
parentd0eaf95edcafcaeebfbc3ce9f361e98914830a49 (diff)
downloadsrc-36c7408818c53ae0c1f1aee0250c5d3fe16b06e0.tar.gz
src-36c7408818c53ae0c1f1aee0250c5d3fe16b06e0.zip
tests/netgraph: Inital framework for testing libnetgraph
Provide a framework of functions to test various netgraph modules. Tests contain: - creating, renaming, and destroying nodes - connecting and removing hooks - sending and receiving data - sending ASCII messages and receiving binary responses - errors can be passed for indiviual inspection or fail the test Reviewed by: kp Differential Revision: https://reviews.freebsd.org/D30629 Differential Revision: https://reviews.freebsd.org/D30657 Differential Revision: https://reviews.freebsd.org/D30671 Differential Revision: https://reviews.freebsd.org/D30699 (cherry picked from commit 24ea1dbf257aa6757f469bcd859f90e9ad851e59) (cherry picked from commit 09307dbfb888a98232096c751a96ecb3344aa77c) (cherry picked from commit 9021c46603bf29b9700f24b8dce8796b434d7c8f) (cherry picked from commit 5554abd9cc9702af30af90925b33c5efff4e7d88) Also contains some fixups: - indent all files correctly - finish factoring out - remove debugging code - check for renaming issues reported in PR241954 PR: 241954 Differential Revision: https://reviews.freebsd.org/D30692 Differential Revision: https://reviews.freebsd.org/D30714 Differential Revision: https://reviews.freebsd.org/D30713 (cherry picked from commit a664ade93972ce617f0888ff79e715dff9cf0f87) (cherry picked from commit 0afa9be03937d60cb5aeba64c81e3e2165bd3737) (cherry picked from commit 43e4821315c31db067e23564b9bfafb519e77b2b)
Diffstat (limited to 'tests/sys/netgraph/basic.c')
-rw-r--r--tests/sys/netgraph/basic.c191
1 files changed, 191 insertions, 0 deletions
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();
+}