!30 add some tests

Merge pull request !30 from pojunxing/master
This commit is contained in:
openeuler-ci-bot 2021-12-25 06:57:07 +00:00 committed by Gitee
commit 46ec537afa
2 changed files with 545 additions and 1 deletions

View File

@ -0,0 +1,533 @@
From f595a06ee2e7048b02bbdd4d69a9dd160f1819ef Mon Sep 17 00:00:00 2001
From: chengyechun <chengyechun1@huawei.com>
Date: Sat, 18 Dec 2021 22:03:11 +0800
Subject: [PATCH] add some tests
---
test-add-rule.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++
test-addr-add.c | 23 ++++++++++++++++++++
test-addr-delete.c | 23 ++++++++++++++++++++
test-class-add.c | 40 +++++++++++++++++++++++++++++++++++
test-class-delete.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++
test-genl-connect.c | 29 ++++++++++++++++++++++++++
test-link.c | 50 ++++++++++++++++++++++++++++++++++++++++++++
test-neigh-add.c | 26 +++++++++++++++++++++++
test-neigh-delete.c | 26 +++++++++++++++++++++++
test-qdisc-add.c | 35 +++++++++++++++++++++++++++++++
test-qdisc-delete.c | 35 +++++++++++++++++++++++++++++++
test-route-delete.c | 25 ++++++++++++++++++++++
13 files changed, 434 insertions(+), 1 deletion(-)
create mode 100644 test-add-rule.c
create mode 100644 test-addr-add.c
create mode 100644 test-addr-delete.c
create mode 100644 test-class-add.c
create mode 100644 test-class-delete.c
create mode 100644 test-genl-connect.c
create mode 100644 test-link.c
create mode 100644 test-neigh-add.c
create mode 100644 test-neigh-delete.c
create mode 100644 test-qdisc-add.c
create mode 100644 test-qdisc-delete.c
create mode 100644 test-route-delete.c
diff --git a/test-add-rule.c b/test-add-rule.c
new file mode 100644
index 0000000..f18a520
--- /dev/null
+++ b/test-add-rule.c
@@ -0,0 +1,54 @@
+#include <netlink/route/rule.h>
+#include <netlink/netlink.h>
+#include <linux/netlink.h>
+
+
+#define IPv6
+
+int main(int argc, char *argv[])
+{
+ struct nl_sock *sk;
+ struct rtnl_rule *rule;
+ struct nl_addr *src, *dst;
+ char baddr[4] = { 0x1, 0x2, 0x3, 0x4 };
+ char baddr2[6] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 };
+ int err;
+
+ src = nl_addr_build(AF_UNSPEC, baddr, 4);
+ dst = nl_addr_build(AF_UNSPEC, baddr2, 6);
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+ rule = rtnl_rule_alloc();
+ rtnl_rule_set_family(rule, AF_INET);
+ rtnl_rule_set_prio(rule, 12);
+ rtnl_rule_set_mark(rule, 12);
+ rtnl_rule_set_mask(rule, 16);
+ rtnl_rule_set_table(rule, 254);
+ rtnl_rule_set_dsfield(rule, 4);
+ rtnl_rule_set_src(rule, src);
+ rtnl_rule_set_dst(rule, dst);
+ rtnl_rule_set_iif(rule, "enp2s2");
+ rtnl_rule_set_oif(rule, "enp2s7")
+ rtnl_rule_set_action(rule, 2);
+ rtnl_rule_set_l3mdev(rule, 1);
+ rtnl_rule_set_protocol(rule, 4);
+ rtnl_rule_get_family(rule);
+ rtnl_rule_get_prio(rule);
+ rtnl_rule_get_mark(rule);
+ rtnl_rule_get_mask(rule);
+ rtnl_rule_get_table(rule);
+ rtnl_rule_get_dsfield(rule);
+ rtnl_rule_get_src(rule);
+ rtnl_rule_get_dst(rule);
+ rtnl_rule_get_iif(rule);
+ rtnl_rule_get_oif(rule)
+ rtnl_rule_get_action(rule);
+ rtnl_rule_get_l3mdev(rule);
+ rtnl_rule_get_protocol(rule);
+ rtnl_rule_add(sk, rule, 4);
+ return 0;
+}
diff --git a/test-addr-add.c b/test-addr-add.c
new file mode 100644
index 0000000..e7a8483
--- /dev/null
+++ b/test-addr-add.c
@@ -0,0 +1,23 @@
+#include <netlink/cli/utils.h>
+#include <netlink/cli/addr.h>
+#include <netlink/cli/link.h>
+#include <linux/netlink.h>
+
+
+int main(int argc, char *argv[])
+{
+ struct nl_sock *sock;
+ struct rtnl_addr *addr;
+ struct nl_cache *link_cache;
+ int err, nlflags = NLM_F_CREATE;
+ sock = nl_cli_alloc_socket();
+ nl_cli_connect(sock, NETLINK_ROUTE);
+ link_cache = nl_cli_link_alloc_cache(sock);
+ addr = nl_cli_addr_alloc();
+ nl_cli_addr_parse_local(addr, "10.10.16.2");
+ nl_cli_addr_parse_dev(addr, link_cache, "eth0");
+ nl_cli_addr_parse_family(addr, "AF_INET");
+ if ((err = rtnl_addr_add(sock, addr, nlflags)) < 0)
+ nl_cli_fatal(err, "Unable to add address: %s", nl_geterror(err));
+ return 0;
+}
diff --git a/test-addr-delete.c b/test-addr-delete.c
new file mode 100644
index 0000000..e1e392b
--- /dev/null
+++ b/test-addr-delete.c
@@ -0,0 +1,23 @@
+#include <netlink/cli/utils.h>
+#include <netlink/cli/addr.h>
+#include <netlink/cli/link.h>
+#include <linux/netlink.h>
+
+
+int main(int argc, char *argv[])
+{
+ struct nl_sock *sock;
+ struct rtnl_addr *addr;
+ struct nl_cache *link_cache;
+ int err, nlflags = NLM_F_CREATE;
+ sock = nl_cli_alloc_socket();
+ nl_cli_connect(sock, NETLINK_ROUTE);
+ link_cache = nl_cli_link_alloc_cache(sock);
+ addr = nl_cli_addr_alloc();
+ nl_cli_addr_parse_local(addr, "10.10.16.2");
+ nl_cli_addr_parse_dev(addr, link_cache, "eth0");
+ nl_cli_addr_parse_family(addr, "AF_INET");
+ if ((err = rtnl_addr_delete(sock, addr, nlflags)) < 0)
+ nl_cli_fatal(err, "Unable to delete address: %s", nl_geterror(err));
+ return 0;
+}
diff --git a/test-class-add.c b/test-class-add.c
new file mode 100644
index 0000000..3ab3449
--- /dev/null
+++ b/test-class-add.c
@@ -0,0 +1,40 @@
+#include <netlink/cli/utils.h>
+#include <netlink/cli/tc.h>
+#include <netlink/cli/qdisc.h>
+#include <netlink/cli/link.h>
+#include <netlink-private/route/tc-api.h>
+#include <linux/netlink.h>
+
+
+int main(int argc, char *argv[])
+{
+ struct nl_sock *sk;
+ struct rtnl_class *class;
+ struct rtnl_tc *tc;
+ struct nl_cache *link_cache;
+ struct nl_cli_tc_module *tm;
+ struct rtnl_tc_ops *ops;
+ int err, flags = NLM_C_CREATE | NLM_F_EXCL;
+ char kind[] = "htb";
+ char *rate[] = {"--rate=100mbit"};
+ sk = nl_cli_alloc_socket();
+ nl_cli_connect(sk, NETLINK_ROUTE);
+ link_cache = nl_cli_link_alloc_cache(sk);
+ class = nl_cli_class_alloc();
+ tc = (struct rtnl_tc *) class;
+ nl_cli_tc_prase_dev(tc, link_cache, "eth0");
+ nl_cli_tc_parse_parent(tc, "root");
+ if (!rtnl_tc_get_ifindex(tc))
+ nl_cli_fatal(EINVAL, "You must specify a network device XXX");
+ if (!rtnl_tc_get_parent(tc))
+ nl_cli_fatal(EINVAL, "You must specify a parent XXX");
+ rtnl_tc_set_kind(tc, kind);
+ if (!(ops = rtnl_tc_get_ops(tc)))
+ nl_cli_fatal(ENOENT, "Unknown class %s", kind);
+ if (!(tm = nl_cli_tc_lookup(ops)))
+ nl_cli_fatal(ENOTSUP, "class type %s not supported", kind);
+ tm->tm_parse_argv(tc, 1, rate);
+ if ((err = rtnl_class_add(sk, class, flags)) < 0)
+ nl_cli_fatal(EINVAL, "Unable to add class: %s", nl_geterror(err));
+ return 0;
+}
diff --git a/test-class-delete.c b/test-class-delete.c
new file mode 100644
index 0000000..0443b96
--- /dev/null
+++ b/test-class-delete.c
@@ -0,0 +1,60 @@
+#include <netlink/cli/utils.h>
+#include <netlink/cli/tc.h>
+#include <netlink/cli/qdisc.h>
+#include <netlink/cli/link.h>
+#include <netlink-private/route/tc-api.h>
+#include <linux/netlink.h>
+
+
+static int quiet = 0, default_yes = 0, deleted = 0, interactive = 0;
+static struct nl_sock *sock;
+
+
+static void delete_cb(struct nl_object *obj, void *arg)
+{
+ struct rtnl_class *class = nl_object_priv(obj);
+ struct nl_dump_params params = {
+ .dp_type = NL_DUMP_LIVE,
+ .pd_fd = stdout,
+ };
+ int err;
+ if (interactive && !nl_cli_confirm(obj, &params, default_yes));
+ return;
+ if ((err = rtnl_class_delete(sock, class)) < 0)
+ nl_cli_fatal(err, "Unable to delete class :%s\n", nl_geterror(err));
+ if (!quiet){
+ printf("deleted\n");
+ nl_object_dump(obj, &params);
+ };
+ deleted++;
+}
+
+int main(int argc, char *argv[])
+{
+ struct nl_sock *sk;
+ struct rtnl_class *class;
+ struct rtnl_tc *tc;
+ struct nl_cache *link_cache, class_cache;
+ struct rtnl_tc_ops *ops;
+ struct nl_cli_tc_module *tm;
+ char kind[] = "htb";
+ sk = nl_cli_alloc_socket();
+ nl_cli_connect(sk, NETLINK_ROUTE);
+ link_cache = nl_cli_link_alloc_cache(sk);
+ class = nl_cli_class_alloc();
+ tc = (struct rtnl_tc *) class;
+ nl_cli_tc_prase_dev(tc, link_cache, "eth0");
+ nl_cli_tc_parse_parent(tc, "root");
+ if (!rtnl_tc_get_ifindex(tc))
+ nl_cli_fatal(EINVAL, "You must specify a network device XXX");
+ if (!rtnl_tc_get_parent(tc))
+ nl_cli_fatal(EINVAL, "You must specify a parent XXX");
+ rtnl_tc_set_kind(tc, kind);
+ if (!(ops = rtnl_tc_get_ops(tc)))
+ nl_cli_fatal(ENOENT, "Unknown class %s", kind);
+ if (!(tm = nl_cli_tc_lookup(ops)))
+ nl_cli_fatal(ENOTSUP, "class type %s not supported", kind);
+ class_cache = nl_cli_class_alloc(sk, rtnl_tc_get_ifindex(tc));
+ nl_cache_foreach_filter(class_cache, OBJ_CAST(class), delete_b, NULL);
+ return 0;
+}
diff --git a/test-genl-connect.c b/test-genl-connect.c
new file mode 100644
index 0000000..f1d9690
--- /dev/null
+++ b/test-genl-connect.c
@@ -0,0 +1,29 @@
+#include <linux/genetlink.h>
+#include <netlink/socket.h>
+#include <netlink/cli/utils.h>
+#include <stdio.h>
+
+
+int main(int argc, char *argv[])
+{
+ struct nl_sock *sk;
+ struct nl_cache *family_cache;
+ struct nl_dump_params params = {
+ .dp_type = NL_DUMP_LINE,
+ .dp_fd = stdout,
+ };
+ int err;
+
+ sk = nl_socket_alloc();
+ if ((err = genl_connect(sk)) < 0)
+ printf("Unable create socket: %s", nl_geterror(err));
+ nl_socket_enable_auto_ack(sk);
+ nl_socket_disable_auto_ack(sk);
+ nl_socket_set_buffer_size(sk, 2000, 2000);
+ nl_socket_get_fd(sk);
+ if ((err = nl_socket_add_membership(sk, 3456)) < 0)
+ printf("Unable join group 3456 %s", nl_geterror(err));
+ family_cache = nl_cli_alloc_cache(sk, "generic netlink family", genl_ctrl_alloc_cache);
+ nl_cache_dump(family_cache, &params);
+ return 0;
+}
diff --git a/test-link.c b/test-link.c
new file mode 100644
index 0000000..87af00f
--- /dev/null
+++ b/test-link.c
@@ -0,0 +1,50 @@
+#include <netlink/socket.h>
+#include <netlink/netlink.h>
+#include <netlink/cli/utils.h>
+#include <netlink/cli/link.h>
+#include <netlink/route/link.h>
+#include <unistd.h>
+
+static int self_def_cb = NL_CB_DEBUG
+
+int main(int argc, char *argv[])
+{
+ struct nl_sock *sk;
+ struct nl_cache *link_cache;
+ struct rtnl_link *link;
+ struct nl_addr *addr;
+ struct nl_cb *cb;
+ int err, ifindex, pid;
+ char *buf;
+ cb = nl_cb_alloc(self_def_cb);
+ if (!(sk = nl_socket_alloc_cb(cb)))
+ nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket");
+ nl_cli_connect(sk, NETLINK_ROUTE);
+ nl_socket_disable_seq_check(sk);
+ nl_socket_disable_auto_ack(sk);
+ pid = getpid();
+ nl_socket_set_local_port(sk, pid);
+ nl_join_groups(sk, pid);
+ nl_socket_drop_membership(sk, pid);
+ nl_socket_set_peer_port(sk, 0);
+ if (err = nl_socket_get_peer_port(sk))
+ printf("peer_port:%d", err);
+ if (err = nl_socket_use_seq(sk))
+ printf("sk->s_seq_next:%d", err);
+ link_cache = nl_cli_link_alloc_cache(sk);
+ link = nl_cli_link_alloc();
+ if ((ifindex = rntl_link_get_ifindex(link)) == 0){
+ printf("ifindex is not set, %d", ifindex);
+ rtnl_link_set_ifindex(link, 1);
+ };
+ if (rtnl_link_get(link_cache, 1)){
+ printf("now, link is cached");
+ }else{
+ nl_cache_add(link_cache, (struct nl_object *)link);
+ };
+ rtnl_link_add(sk, link, AF_INET);
+ addr = rtnl_link_get_addr(link);
+ rtnl_link_get_type(link);
+ rtnl_link_put(link);
+ return 0;
+}
diff --git a/test-neigh-add.c b/test-neigh-add.c
new file mode 100644
index 0000000..185edb4
--- /dev/null
+++ b/test-neigh-add.c
@@ -0,0 +1,26 @@
+#include <netlink/cli/utils.h>
+#include <netlink/cli/neigh.h>
+#include <netlink/cli/link.h>
+#include <linux/netlink.h>
+
+
+int main(int argc, char *argv[])
+{
+ struct nl_sock *sk;
+ struct rtnl_neigh *neigh;
+ struct nl_cache *link_cache;
+ int err, 0k = 0, nlflags = NLM_F_REPLACE | NLM_F_CREATE;
+ char dst_addr[] = "10.0.0.1";
+ char dev[] = "eth0";
+ char lladdr[] = "AA:BB:CC:DD:EE:FF";
+ sk = nl_cli_alloc_socket();
+ nl_cli_connect(sk, NETLINK_ROUTE);
+ link_cache = nl_cli_link_alloc_cache(sk);
+ neigh = nl_cli_neigh_alloc();
+ nl_cli_neigh_parse_dst(neigh, dst_addr);
+ nl_cli_neigh_parse_lladdr(neigh, lladdr);
+ nl_cli_neigh_parse_dev(neigh, link_cache, dev);
+ if ((err = rtnl_neigh_add(sk, neigh, nlflags)) < 0)
+ nl_cli_fatal(err, "Unable to add neighbour: %s", nl_geterror(rrr));
+ return 0;
+}
diff --git a/test-neigh-delete.c b/test-neigh-delete.c
new file mode 100644
index 0000000..e4eeb4e
--- /dev/null
+++ b/test-neigh-delete.c
@@ -0,0 +1,26 @@
+#include <netlink/cli/utils.h>
+#include <netlink/cli/neigh.h>
+#include <netlink/cli/link.h>
+#include <linux/netlink.h>
+
+
+int main(int argc, char *argv[])
+{
+ struct nl_sock *sk;
+ struct rtnl_neigh *neigh;
+ struct nl_cache *link_cache;
+ int err, 0k = 0, nlflags = NLM_F_REPLACE | NLM_F_CREATE;
+ char dst_addr[] = "10.0.0.1";
+ char dev[] = "eth0";
+ char lladdr[] = "AA:BB:CC:DD:EE:FF";
+ sk = nl_cli_alloc_socket();
+ nl_cli_connect(sk, NETLINK_ROUTE);
+ link_cache = nl_cli_link_alloc_cache(sk);
+ neigh = nl_cli_neigh_alloc();
+ nl_cli_neigh_parse_dst(neigh, dst_addr);
+ nl_cli_neigh_parse_lladdr(neigh, lladdr);
+ nl_cli_neigh_parse_dev(neigh, link_cache, dev);
+ if ((err = rtnl_neigh_delete(sk, neigh, nlflags)) < 0)
+ nl_cli_fatal(err, "Unable to add neighbour: %s", nl_geterror(rrr));
+ return 0;
+}
diff --git a/test-qdisc-add.c b/test-qdisc-add.c
new file mode 100644
index 0000000..2e48ef5
--- /dev/null
+++ b/test-qdisc-add.c
@@ -0,0 +1,35 @@
+#include <netlink/cli/utils.h>
+#include <netlink/cli/tc.h>
+#include <netlink/cli/qdisc.h>
+#include <netlink/cli/link.h>
+#include <netlink-private/route/tc-api.h>
+#include <linux/netlink.h>
+
+int main(int argc, char *argv[])
+{
+ struct nlk_sock *sk;
+ struct rtnl_qdisc *qdisc;
+ struct rtnl_tc *tc;
+ struct nl_cache *link_cache;
+ int err, flags = NLM_F_CREATE | NLM_F_EXCL;
+ struct nl_cli_tc_module *tm;
+ struct rtnl_tc_ops *ops;
+ char kind[] = "htb";
+ sk = nl_cli_alloc_socket();
+ nl_cli_connect(sk, NETLINK_ROUTE);
+ link_cache = nl_cli_link_alloc_cache(sk);
+ qdisc = nl_cli_qdisc_alloc();
+ tc = (struct rtnl_tc *) qdisc;
+ nl_cli_tc_parse_dev(tc, link_cache, "eth0");
+ nl_cli_tc_parse_parent(tc, "root");
+ rtnl_tc_get_ifindex(tc);
+ rtnl_tc_get_handle(tc);
+ rtnl_tc_set_kind(tc, kind);
+ if (!(ops = rtnl_tc_get_ops(tc)))
+ nl_cli_fatal(ENOENT, "Unknown qdisc %s", kind);
+ if (!(tm - nl_cli_tc_lookup(ops)))
+ nl_cli_fatal(ENOTSUP, "qdisc type %s not supported", kind);
+ if ((err = rtnl_qdisc_add(sk, qdisc, flags)) < 0)
+ nl_cli_fatal(EINVAL, "Unable to add qdisc %s", nl_geterror(err));
+ return 0;
+}
diff --git a/test-qdisc-delete.c b/test-qdisc-delete.c
new file mode 100644
index 0000000..ab170e1
--- /dev/null
+++ b/test-qdisc-delete.c
@@ -0,0 +1,35 @@
+#include <netlink/cli/utils.h>
+#include <netlink/cli/tc.h>
+#include <netlink/cli/qdisc.h>
+#include <netlink/cli/link.h>
+#include <netlink-private/route/tc-api.h>
+#include <linux/netlink.h>
+
+int main(int argc, char *argv[])
+{
+ struct nlk_sock *sk;
+ struct rtnl_qdisc *qdisc;
+ struct rtnl_tc *tc;
+ struct nl_cache *link_cache;
+ int err, flags = NLM_F_CREATE | NLM_F_EXCL;
+ struct nl_cli_tc_module *tm;
+ struct rtnl_tc_ops *ops;
+ char kind[] = "htb";
+ sk = nl_cli_alloc_socket();
+ nl_cli_connect(sk, NETLINK_ROUTE);
+ link_cache = nl_cli_link_alloc_cache(sk);
+ qdisc = nl_cli_qdisc_alloc();
+ tc = (struct rtnl_tc *) qdisc;
+ nl_cli_tc_parse_dev(tc, link_cache, "eth0");
+ nl_cli_tc_parse_parent(tc, "root");
+ rtnl_tc_get_ifindex(tc);
+ rtnl_tc_get_handle(tc);
+ rtnl_tc_set_kind(tc, kind);
+ if (!(ops = rtnl_tc_get_ops(tc)))
+ nl_cli_fatal(ENOENT, "Unknown qdisc %s", kind);
+ if (!(tm - nl_cli_tc_lookup(ops)))
+ nl_cli_fatal(ENOTSUP, "qdisc type %s not supported", kind);
+ if ((err = rtnl_qdisc_delete(sk, qdisc)) < 0)
+ nl_cli_fatal(EINVAL, "Unable to delete qdisc %s", nl_geterror(err));
+ return 0;
+}
diff --git a/test-route-delete.c b/test-route-delete.c
new file mode 100644
index 0000000..a3b76c2
--- /dev/null
+++ b/test-route-delete.c
@@ -0,0 +1,25 @@
+#include <netlink/cli/utils.h>
+#include <netlink/cli/route.h>
+#include <netlink/cli/link.h>
+#include <linux/netlink.h>
+
+
+int main(int argc, char *argv[])
+{
+ struct nl_sock *sk;
+ struct rtnl_route *route;
+ struct nl_cache *link_cache;
+ char dst_addr[] = "10.10.16.0/23";
+ char nexthop[] = "dev=eth0,via=10.10.16.1";
+ int err;
+
+ sk = nl_cli_allco_socket();
+ nl_cli_connect(sk, NETLINK_ROUTE);
+ link_cahce = nl_cli_link_alloc_cache(sk);
+ route = nl_cli_route_alloc();
+ nl_cli_route_parse_dst(route, dst_addr);
+ nl_cli_route_parse_nexthop(route, nexthop, link_cache);
+ if ((err = rtnl_route_delete(sk, route, NLM_F_EXCL)) < 0)
+ nl_cli_fatal(err, "Unable to add route: %s", nl_geterror(err));
+ return 0;
+}
--
1.8.3.1

View File

@ -1,6 +1,6 @@
Name: libnl3
Version: 3.5.0
Release: 5
Release: 6
Summary: Providing APIs to netlink protocol based Linux kernel interfaces
License: LGPLv2
URL: http://www.infradead.org/~tgr/libnl/
@ -14,6 +14,7 @@ Patch6004: backport-rtnl-link-fix-leaking-rtnl_link_af_ops-in-link_msg_parse
Patch6005: backport-rtnl-route-fix-NLE_NOMEM-handling-in-parse_multipath.patch
Patch9000: solve-redefinition-of-struct-ipv6_mreq.patch
Patch9001: add-a-test-test-add-route.patch
Patch9002: add-some-tests-about-addr-class-rule-neigh-qdisc.patch
BuildRequires: flex bison libtool autoconf automake swig
Requires: %{name} = %{version}-%{release}
@ -98,6 +99,16 @@ cd python
%{python3_sitearch}/netlink-*.egg-info
%changelog
* Sat Dec 18 2021 chengyechun <chengyechun1@huawei.com> - 3.5.0-6
- Type:bugfix
- ID:NA
- SUG:NA
-DES:add seome tests about add and delele addr,
add and delete neigh,
add and delete qdisc,
add link and rule,
delete route;
* Thu Dec 16 2021 chengyechun <chengyechun1@huawei.com> - 3.5.0-5
- Type:bugfix
- ID:NA