kernel-netlink: Add a simple send message test querying available links
[strongswan.git] / src / libhydra / plugins / kernel_netlink / suites / test_socket.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <test_suite.h>
17
18 #include "../kernel_netlink_shared.h"
19
20 START_TEST(test_echo)
21 {
22 netlink_socket_t *s;
23 struct nlmsghdr *out, *current;
24 struct rtgenmsg *msg;
25 size_t len;
26 netlink_buf_t request = {
27 .hdr = {
28 .nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
29 .nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT,
30 .nlmsg_type = RTM_GETLINK,
31 },
32 };
33
34 s = netlink_socket_create(NETLINK_ROUTE, NULL);
35 msg = NLMSG_DATA(&request.hdr);
36 msg->rtgen_family = AF_UNSPEC;
37
38 ck_assert(s->send(s, &request.hdr, &out, &len) == SUCCESS);
39 current = out;
40 while (TRUE)
41 {
42 ck_assert(NLMSG_OK(current, len));
43 if (current->nlmsg_type == NLMSG_DONE)
44 {
45 break;
46 }
47 ck_assert_int_eq(current->nlmsg_type, RTM_NEWLINK);
48 current = NLMSG_NEXT(current, len);
49 }
50 free(out);
51 s->destroy(s);
52 }
53 END_TEST
54
55 Suite *socket_suite_create()
56 {
57 Suite *s;
58 TCase *tc;
59
60 s = suite_create("netlink socket");
61
62 tc = tcase_create("echo");
63 tcase_add_test(tc, test_echo);
64 suite_add_tcase(s, tc);
65
66 return s;
67 }