tests: introduce TEST_NETLINK and TEST_NETLINK_ macros
* tests/test_netlink.h: New file.
* tests/Makefile.am (libtests_a_SOURCES): Add it.
* tests/netlink_sock_diag.c: Include "test_netlink.h"
instead of "netlink.h".
(TEST_SOCK_DIAG): New macro.
(test_unix_diag_req, test_unix_diag_msg,
test_netlink_diag_req, test_netlink_diag_msg,
test_packet_diag_req, test_packet_diag_msg,
test_inet_diag_req, test_inet_diag_req_v2,
test_inet_diag_msg, test_smc_diag_req,
test_smc_diag_msg): Use it.
(test_odd_family_req, test_odd_family_msg,
test_inet_diag_sockid): Use TEST_NETLINK macro.
2017-07-08 16:36:46 +08:00
# include "tests.h"
# include "print_fields.h"
# include <stdio.h>
# include <stdint.h>
# include <string.h>
# include <sys/socket.h>
# include "netlink.h"
# define TEST_NETLINK_(fd_, nlh0_, \
type_ , type_str_ , \
flags_ , flags_str_ , \
data_len_ , src_ , slen_ , . . . ) \
do { \
struct nlmsghdr * const TEST_NETLINK_nlh = \
( nlh0_ ) - ( slen_ ) ; \
const unsigned int msg_len = \
NLMSG_HDRLEN + ( data_len_ ) ; \
\
SET_STRUCT ( struct nlmsghdr , TEST_NETLINK_nlh , \
. nlmsg_len = msg_len , \
. nlmsg_type = ( type_ ) , \
. nlmsg_flags = ( flags_ ) \
) ; \
memcpy ( NLMSG_DATA ( TEST_NETLINK_nlh ) , ( src_ ) , ( slen_ ) ) ; \
\
const char * const errstr = \
sprintrc ( sendto ( ( fd_ ) , TEST_NETLINK_nlh , \
msg_len , MSG_DONTWAIT , \
NULL , 0 ) ) ; \
\
printf ( " sendto(%d, {{len=%u, type=%s " \
" , flags=%s, seq=0, pid=0}, " , \
( fd_ ) , msg_len , ( type_str_ ) , ( flags_str_ ) ) ; \
\
{ __VA_ARGS__ ; } \
\
printf ( " }, %u, MSG_DONTWAIT, NULL, 0) = %s \n " , \
msg_len , errstr ) ; \
} while ( 0 )
# define TEST_NETLINK(fd_, nlh0_, type_, flags_, \
data_len_ , src_ , slen_ , . . . ) \
TEST_NETLINK_ ( ( fd_ ) , ( nlh0_ ) , \
( type_ ) , # type_ , \
( flags_ ) , # flags_ , \
( data_len_ ) , ( src_ ) , ( slen_ ) , __VA_ARGS__ )
2017-07-15 09:11:33 +08:00
2017-07-20 15:58:28 +08:00
# define TEST_NETLINK_OBJECT_EX_(fd_, nlh0_, \
type_ , type_str_ , \
flags_ , flags_str_ , \
obj_ , fallback_func , . . . ) \
2017-07-15 09:11:33 +08:00
do { \
char pattern [ DEFAULT_STRLEN ] ; \
fill_memory_ex ( pattern , sizeof ( pattern ) , \
' a ' , ' z ' - ' a ' + 1 ) ; \
const unsigned int plen = \
sizeof ( obj_ ) - 1 > DEFAULT_STRLEN \
? DEFAULT_STRLEN : ( int ) sizeof ( obj_ ) - 1 ; \
/* len < sizeof(obj_) */ \
TEST_NETLINK_ ( ( fd_ ) , ( nlh0_ ) , \
2017-07-20 15:58:28 +08:00
( type_ ) , ( type_str_ ) , \
( flags_ ) , ( flags_str_ ) , \
2017-07-15 09:11:33 +08:00
plen , pattern , plen , \
2017-07-20 15:58:28 +08:00
( fallback_func ) ( pattern , plen ) ) ; \
2017-07-15 09:11:33 +08:00
/* short read of sizeof(obj_) */ \
TEST_NETLINK_ ( ( fd_ ) , ( nlh0_ ) , \
2017-07-20 15:58:28 +08:00
( type_ ) , ( type_str_ ) , \
( flags_ ) , ( flags_str_ ) , \
2017-07-15 09:11:33 +08:00
sizeof ( obj_ ) , \
pattern , plen , \
printf ( " %p " , \
NLMSG_DATA ( TEST_NETLINK_nlh ) ) ) ; \
/* sizeof(obj_) */ \
TEST_NETLINK_ ( ( fd_ ) , ( nlh0_ ) , \
2017-07-20 15:58:28 +08:00
( type_ ) , ( type_str_ ) , \
( flags_ ) , ( flags_str_ ) , \
2017-07-15 09:11:33 +08:00
sizeof ( obj_ ) , \
& ( obj_ ) , sizeof ( obj_ ) , \
__VA_ARGS__ ) ; \
} while ( 0 )
2017-07-20 15:58:28 +08:00
# define TEST_NETLINK_OBJECT_EX(fd_, nlh0_, \
type_ , flags_ , \
obj_ , fallback_func , . . . ) \
TEST_NETLINK_OBJECT_EX_ ( ( fd_ ) , ( nlh0 ) , \
( type_ ) , # type_ , \
( flags_ ) , # flags_ , \
( obj_ ) , ( fallback_func ) , __VA_ARGS__ )
# define TEST_NETLINK_OBJECT(fd_, nlh0_, \
type_ , flags_ , \
obj_ , . . . ) \
TEST_NETLINK_OBJECT_EX_ ( ( fd_ ) , ( nlh0 ) , \
( type_ ) , # type_ , \
( flags_ ) , # flags_ , \
( obj_ ) , print_quoted_hex , __VA_ARGS__ )