diff options
author | Thomas Graf <tgraf@suug.ch> | 2007-09-14 23:28:01 (GMT) |
---|---|---|
committer | Thomas Graf <tgraf@suug.ch> | 2007-09-14 23:28:01 (GMT) |
commit | 44d362409d5469aed47d19e7908d19bd194493a4 (patch) | |
tree | 5d1e739a4566f3af796273e5c3f78ca53d234df6 /tests | |
download | libnl-44d362409d5469aed47d19e7908d19bd194493a4.zip libnl-44d362409d5469aed47d19e7908d19bd194493a4.tar.gz libnl-44d362409d5469aed47d19e7908d19bd194493a4.tar.bz2 |
Initial import
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile | 37 | ||||
-rw-r--r-- | tests/test-cache-mngr.c | 73 | ||||
-rw-r--r-- | tests/test-genl.c | 56 | ||||
-rw-r--r-- | tests/test-nf-cache-mngr.c | 58 | ||||
-rw-r--r-- | tests/test-socket-creation.c | 15 |
5 files changed, 239 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..b5cec34 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,37 @@ +# +# src/Makefile +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation version 2.1 +# of the License. +# +# Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch> +# + +ifeq ($(shell [ ! -r ../Makefile.opts ] && echo 1),) + include ../Makefile.opts +endif + +LDFLAGS += -L../lib -lnl ../src/utils.o +CIN := $(wildcard test-*.c) +TOOLS := $(CIN:%.c=%) + +all: $(TOOLS) + +$(TOOLS): ../src/utils.o + +test-%: test-%.c + @echo " LD $@"; \ + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + +clean: + @echo " CLEAN src"; \ + rm -f $(TOOLS) + +distclean: clean + +install: + @true + +include ../Makefile.rules diff --git a/tests/test-cache-mngr.c b/tests/test-cache-mngr.c new file mode 100644 index 0000000..4d70e31 --- /dev/null +++ b/tests/test-cache-mngr.c @@ -0,0 +1,73 @@ +#include "../src/utils.h" + +static void change_cb(struct nl_cache *cache, struct nl_object *obj, + int action) +{ + struct nl_dump_params dp = { + .dp_type = NL_DUMP_BRIEF, + .dp_fd = stdout, + }; + + if (action == NL_ACT_NEW) + printf("NEW "); + else if (action == NL_ACT_DEL) + printf("DEL "); + else if (action == NL_ACT_CHANGE) + printf("CHANGE "); + + nl_object_dump(obj, &dp); +} + +int main(int argc, char *argv[]) +{ + struct nl_cache_mngr *mngr; + struct nl_cache *lc, *nc, *ac, *rc; + struct nl_handle *handle; + + nltool_init(argc, argv); + + handle = nltool_alloc_handle(); + + mngr = nl_cache_mngr_alloc(handle, NETLINK_ROUTE, NL_AUTO_PROVIDE); + if (!mngr) { + nl_perror("nl_cache_mngr_alloc"); + return -1; + } + + lc = nl_cache_mngr_add(mngr, "route/link", &change_cb); + if (lc == NULL) { + nl_perror("nl_cache_mngr_add(route/link"); + return -1; + } + + nc = nl_cache_mngr_add(mngr, "route/neigh", &change_cb); + if (nc == NULL) { + nl_perror("nl_cache_mngr_add(route/neigh"); + return -1; + } + + ac = nl_cache_mngr_add(mngr, "route/addr", &change_cb); + if (ac == NULL) { + nl_perror("nl_cache_mngr_add(route/addr"); + return -1; + } + + rc = nl_cache_mngr_add(mngr, "route/route", &change_cb); + if (rc == NULL) { + nl_perror("nl_cache_mngr_add(route/route"); + return -1; + } + + for (;;) { + int err = nl_cache_mngr_poll(mngr, 5000); + if (err < 0) { + nl_perror("nl_cache_mngr_poll()"); + return -1; + } + + } + + nl_cache_mngr_free(mngr); + + return 0; +} diff --git a/tests/test-genl.c b/tests/test-genl.c new file mode 100644 index 0000000..e44b3fb --- /dev/null +++ b/tests/test-genl.c @@ -0,0 +1,56 @@ +#include "../src/utils.h" + +int main(int argc, char *argv[]) +{ + struct nl_handle *h; + struct nl_msg *msg; + void *hdr; + + if (nltool_init(argc, argv) < 0) + return -1; + + h = nltool_alloc_handle(); + if (!h) { + nl_perror("nl_handle_alloc"); + return -1; + } + + if (genl_connect(h) < 0) { + nl_perror("genl_connect"); + return -1; + } + + msg = nlmsg_alloc(); + if (msg == NULL) { + nl_perror("nlmsg_alloc"); + return -1; + } + + hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, GENL_ID_CTRL, + 0, 0, CTRL_CMD_GETFAMILY, 1); + if (hdr == NULL) { + nl_perror("genlmsg_put"); + return -1; + } + + if (nla_put_u32(msg, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL) < 0) { + nl_perror("nla_put_u32(CTRL_ATTR_FAMILY_ID)"); + return -1; + } + + if (nl_send_auto_complete(h, msg) < 0) { + nl_perror("nl_send_auto_complete"); + return -1; + } + + if (nl_recvmsgs_default(h) < 0) { + nl_perror("nl_recvmsgs_def"); + return -1; + } + + nlmsg_free(msg); + + nl_close(h); + + return 0; +} diff --git a/tests/test-nf-cache-mngr.c b/tests/test-nf-cache-mngr.c new file mode 100644 index 0000000..86cbabb --- /dev/null +++ b/tests/test-nf-cache-mngr.c @@ -0,0 +1,58 @@ +#include "../src/utils.h" + +static void change_cb(struct nl_cache *cache, struct nl_object *obj, + int action) +{ + struct nfnl_ct *ct = (struct nfnl_ct *) obj; + static struct nl_addr *hack = NULL; + + if (!hack) + hack = nl_addr_parse("194.88.212.233", AF_INET); + + if (!nl_addr_cmp(hack, nfnl_ct_get_src(ct, 1)) || + !nl_addr_cmp(hack, nfnl_ct_get_dst(ct, 1))) { + struct nl_dump_params dp = { + .dp_type = NL_DUMP_BRIEF, + .dp_fd = stdout, + }; + + printf("UPDATE "); + nl_object_dump(obj, &dp); + } +} + +int main(int argc, char *argv[]) +{ + struct nl_cache_mngr *mngr; + struct nl_handle *handle; + struct nl_cache *ct; + + nltool_init(argc, argv); + + handle = nltool_alloc_handle(); + + mngr = nl_cache_mngr_alloc(handle, NETLINK_NETFILTER, NL_AUTO_PROVIDE); + if (!mngr) { + nl_perror("nl_cache_mngr_alloc"); + return -1; + } + + ct = nl_cache_mngr_add(mngr, "netfilter/ct", &change_cb); + if (ct == NULL) { + nl_perror("nl_cache_mngr_add(netfilter/ct)"); + return -1; + } + + for (;;) { + int err = nl_cache_mngr_poll(mngr, 5000); + if (err < 0) { + nl_perror("nl_cache_mngr_poll()"); + return -1; + } + + } + + nl_cache_mngr_free(mngr); + + return 0; +} diff --git a/tests/test-socket-creation.c b/tests/test-socket-creation.c new file mode 100644 index 0000000..5a06661 --- /dev/null +++ b/tests/test-socket-creation.c @@ -0,0 +1,15 @@ +#include "../src/utils.h" + +int main(int argc, char *argv[]) +{ + struct nl_handle *h; + int i; + + for (i = 0; i < 1025; i++) { + h = nl_handle_alloc(); + printf("Created handle with port 0x%x\n", + nl_socket_get_local_port(h)); + } + + return 0; +} |