Create separate .c file for include/tst_net.h
PATCH v4 1/3 Create separate .c file for include/tst_net.h
---
Changes since v1:
- new patch
Changes since v2:
- Also separate print_svar*()
- Added tst_ prefix to functions separated into tst_net.c
Changes since v3:
- Split off tst_private.h from tst_net.h
- Make some helper functions in tst_net.c static
include/tst_net.h | 138 +------------------------ include/tst_private.h | 31 ++++++ lib/tst_net.c | 147 +++++++++++++++++++++++++++ testcases/lib/tst_net_iface_prefix.c | 10 +- testcases/lib/tst_net_ip_prefix.c | 11 +- testcases/lib/tst_net_vars.c | 65 ++++++------ 6 files changed, 225 insertions(+), 177 deletions(-) create mode 100644 include/tst_private.h create mode 100644 lib/tst_net.c
include/tst_net.h +2 -136
@@ -4,141 +4,7 @@*/ #include <arpa/inet.h> -#include <errno.h> #include <stdio.h> -#include <stdlib.h> -#include <string.h> -#define MAX_IPV4_PREFIX 32 -#define MAX_IPV6_PREFIX 128 - -#define tst_res_comment (...) { \ - fprintf(stderr, "# "); \ - tst_res(__VA_ARGS__); } \ - - -#define tst_brk_comment (...) { \ - fprintf(stderr, "# "); \ - tst_brk(TCONF, __VA_ARGS__); } \ - -static inline void print_svar (const char *name,const char *val) -{ -if (name && val) -printf ("export %s=\"%s\"\n" , name, val); -} - -static inline void print_svar_change (const char *name,const char *val) -{ -if (name && val) -printf ("export %s=\"${%s:-%s}\"\n" , name, name, val); -} - -/* -* Function bit_count is from ipcalc project, ipcalc.c. -*/ -static inline int bit_count (uint32_t i) -{ -int c =0 ; -unsigned int seen_one =0 ; - -while (i >0 ) { -if (i &1 ) { - seen_one =1 ; - c++; - }else { -if (seen_one) -return -1 ; - } - i >>=1 ; - } - -return c; -} - -/* -* Function mask2prefix is from ipcalc project, ipcalc.c. -*/ -static inline int mask2prefix (struct in_addr mask) -{ -return bit_count (ntohl (mask.s_addr)); -} - -/* -* Function ipv4_mask_to_int is from ipcalc project, ipcalc.c. -*/ -static inline int ipv4_mask_to_int (const char *prefix) -{ -int ret; -struct in_addr in; - - ret =inet_pton (AF_INET , prefix, &in); -if (ret ==0 ) -return -1 ; - -return mask2prefix (in); -} - -/* -* Function safe_atoi is from ipcalc project, ipcalc.c. -*/ -static inline int safe_atoi (const char *s,int *ret_i) -{ -char *x =NULL ; -long l; - - errno =0 ; - l =strtol (s, &x,0 ); - -if (!x || x == s || *x || errno) -return errno >0 ? -errno : -EINVAL ; - -if ((long )(int )l != l) -return -ERANGE ; - - *ret_i = (int )l; - -return 0 ; -} - -/* -* Function get_prefix use code from ipcalc project, str_to_prefix/ipcalc.c. -*/ -static inline int get_prefix (const char *ip_str,int is_ipv6) -{ -char *prefix_str =NULL ; -int prefix =-1 , r; - - prefix_str =strchr (ip_str,'/' ); -if (!prefix_str) -return -1 ; - - *(prefix_str++) ='\0' ; - -if (!is_ipv6 &&strchr (prefix_str,'.' )) - prefix =ipv4_mask_to_int (prefix_str); -else { - r =safe_atoi (prefix_str, &prefix); -if (r !=0 ) -tst_brk_comment ("conversion error: '%s' is not integer" , - prefix_str); - } - -if (prefix <0 || ((is_ipv6 && prefix >MAX_IPV6_PREFIX ) || - (!is_ipv6 && prefix >MAX_IPV4_PREFIX ))) -tst_brk_comment ("bad %s prefix: %s" , is_ipv6 ?"IPv6" :"IPv4" , - prefix_str); - -return prefix; -} - -static inline void get_in_addr (const char *ip_str,struct in_addr *ip) -{ -if (inet_pton (AF_INET , ip_str, ip) <=0 ) -tst_brk_comment ("bad IPv4 address: '%s'" , ip_str); -} - -static inline void get_in6_addr (const char *ip_str,struct in6_addr *ip6) -{ -if (inet_pton (AF_INET6 , ip_str, ip6) <=0 ) -tst_brk_comment ("bad IPv6 address: '%s'" , ip_str); -} +void tst_get_in_addr (const char *ip_str,struct in_addr *ip); +void tst_get_in6_addr (const char *ip_str,struct in6_addr *ip6);
include/tst_private.h new file +31 -0
@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2017-2019 Petr Vorel <[email protected]> +* +* Internal helper functions for the shell library. Do not use directly +* in test programs. +*/ + +#ifndef TST_PRIVATE_H_ +#define TST_PRIVATE_H_ + +#include <stdio.h> + +#define MAX_IPV4_PREFIX 32 +#define MAX_IPV6_PREFIX 128 + +#define tst_res_comment (...) { \ + fprintf(stderr, "# "); \ + tst_res(__VA_ARGS__); } \ + + +#define tst_brk_comment (...) { \ + fprintf(stderr, "# "); \ + tst_brk(TCONF, __VA_ARGS__); } \ + +void tst_print_svar (const char *name,const char *val); +void tst_print_svar_change (const char *name,const char *val); + +int tst_get_prefix (const char *ip_str,int is_ipv6); + +#endif
lib/tst_net.c new file +147 -0
147 lines not on this page · raw message
testcases/lib/tst_net_iface_prefix.c +6 -4
@@ -13,6 +13,7 @@#include "tst_test.h" #include "tst_net.h" +#include "tst_private.h" static char *iface;static int prefix;
@@ -124,7 +125,7 @@ int main(int argc, char *argv[]) prefix_str =strchr (ip_str,'/' );if (prefix_str) { - prefix =get_prefix (ip_str, is_ipv6); + prefix =tst_get_prefix (ip_str, is_ipv6);tst_res_comment (TINFO ,"IP address '%s' contains prefix %d, using it and don't search for iface.\n" , ip_str, prefix);
@@ -136,11 +137,12 @@ int main(int argc, char *argv[])/* checks for validity of IP string */ if (is_ipv6) -get_in6_addr (ip_str, &ip6); +tst_get_in6_addr (ip_str, &ip6);else -get_in_addr (ip_str, &ip); +tst_get_in_addr (ip_str, &ip); -print_svar_change (is_rhost ?"RHOST_IFACES" :"LHOST_IFACES" , iface); +tst_print_svar_change (is_rhost ?"RHOST_IFACES" :"LHOST_IFACES" , + iface);if (is_ipv6)print_ivar (is_rhost ?"IPV6_RPREFIX" :"IPV6_LPREFIX" , prefix);else
testcases/lib/tst_net_ip_prefix.c +6 -5
@@ -10,6 +10,7 @@#include "tst_test.h" #include "tst_net.h" +#include "tst_private.h" #define DEFAULT_IPV4_PREFIX 24#define DEFAULT_IPV6_PREFIX 64
@@ -67,22 +68,22 @@ int main(int argc, char *argv[]) prefix_str =strchr (ip_str,'/' );if (prefix_str) - prefix =get_prefix (ip_str, is_ipv6); + prefix =tst_get_prefix (ip_str, is_ipv6);else prefix = is_ipv6 ?DEFAULT_IPV6_PREFIX :DEFAULT_IPV4_PREFIX ;/* checks for validity of IP string */ if (is_ipv6) -get_in6_addr (ip_str, &ip6); +tst_get_in6_addr (ip_str, &ip6);else -get_in_addr (ip_str, &ip); +tst_get_in_addr (ip_str, &ip);if (is_ipv6) {print_ivar (is_rhost ?"IPV6_RPREFIX" :"IPV6_LPREFIX" , prefix); -print_svar (is_rhost ?"IPV6_RHOST" :"IPV6_LHOST" , ip_str); +tst_print_svar (is_rhost ?"IPV6_RHOST" :"IPV6_LHOST" , ip_str); }else {print_ivar (is_rhost ?"IPV4_RPREFIX" :"IPV4_LPREFIX" , prefix); -print_svar (is_rhost ?"IPV4_RHOST" :"IPV4_LHOST" , ip_str); +tst_print_svar (is_rhost ?"IPV4_RHOST" :"IPV4_LHOST" , ip_str); }exit (EXIT_SUCCESS );
testcases/lib/tst_net_vars.c +33 -32
139 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v4 2/3 Add socket address initialization functions to tst_net library
---
Changes since v1:
- new patch
Changes since v2:
- Added tst_ prefix to the new functions
Changes since v3:
- Added #define protection against multiple #includes to tst_net.h
include/tst_net.h | 21 ++++++++++++++ lib/tst_net.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+)
include/tst_net.h +21 -0
@@ -3,8 +3,29 @@* Copyright (c) 2017-2019 Petr Vorel <[email protected]> */ +#ifndef TST_NET_H_ +#define TST_NET_H_ +#include <arpa/inet.h> #include <stdio.h> +#include <sys/types.h> +#include <netinet/in.h> +#include <netinet/ip.h> void tst_get_in_addr (const char *ip_str,struct in_addr *ip);void tst_get_in6_addr (const char *ip_str,struct in6_addr *ip6); + +/* +* Find valid connection address for a given bound socket +*/ +socklen_t tst_get_connect_address (int sock,struct sockaddr_storage *addr); + +/* +* Initialize AF_INET/AF_INET6 socket address structure with address and port +*/ +void tst_init_sockaddr_inet (struct sockaddr_in *sa,const char *ip_str,uint16_t port); +void tst_init_sockaddr_inet_bin (struct sockaddr_in *sa,uint32_t ip_val,uint16_t port); +void tst_init_sockaddr_inet6 (struct sockaddr_in6 *sa,const char *ip_str,uint16_t port); +void tst_init_sockaddr_inet6_bin (struct sockaddr_in6 *sa,const struct in6_addr *ip_val,uint16_t port); + +#endif
lib/tst_net.c +71 -0
79 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v4 2/3 Add socket address initialization functions to tst_net library
Hi Martin,
lib/tst_net.c void tst_get_in6_addr(const char *ip_str, struct in6_addr *ip6) · line 147
Quoted 3 lines
lib/tst_net.c +0
-0@@ -145,3 +146,73 @@ void tst_get_in6_addr(const char *ip_str, struct in6_addr *ip6)if (inet_pton (AF_INET6 , ip_str, ip6) <=0 )tst_brk_comment ("bad IPv6 address: '%s'" , ip_str);
Just a note: tst_brk_comment() and tst_res_comment() prepend '# ' to the message. This was meant only for tst_net_vars.c, tst_net_ip_prefix.c and tst_net_iface_prefix.c, to prevent shell eval breakage. But let's use it also for tests as the code gets simpler.
Kind regards,
Petr
PATCH v4 2/3 Add socket address initialization functions to tst_net library
Quoted 9 lines · Petr Vorel
Hi Martin,
lib/tst_net.c void tst_get_in6_addr(const char *ip_str, struct in6_addr *ip6) · line 147
Quoted 3 lines
lib/tst_net.c +0
-0@@ -145,3 +146,73 @@ void tst_get_in6_addr(const char *ip_str, struct in6_addr *ip6)if (inet_pton (AF_INET6 , ip_str, ip6) <=0 )tst_brk_comment ("bad IPv6 address: '%s'" , ip_str);Just a note:
tst_brk_comment()andtst_res_comment()prepend '# ' to the message. This was meant only fortst_net_vars.c,tst_net_ip_prefix.candtst_net_iface_prefix.c, to prevent shell eval breakage. But let's use it also for tests as the code gets simpler.
Should I move them back to tst_net.h then? Or tst_test.h even?
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH v4 2/3 Add socket address initialization functions to tst_net library
In reply to Martin DouchaPATCH v4 3/3 Add connection tests for bind()
In reply to Martin DouchaAdd two new test programs to verify that bind() will open sockets for incoming connections. Both programs follow the same test scenario:
- Create and bind() a socket
- Wait for connection from peer thread
- Send request to peer thread
- Receive and verify response from peer thread
bind04 tests stream-oriented sockets (SOCK_STREAM and SOCK_SEQPACKET). bind05 tests datagram-oriented sockets (SOCK_DGRAM).
Both programs test the following socket types:
- AF_UNIX (pathname and abstract addresses)
- AF_INET (loopback)
- AF_INET6 (loopback)
---
Changes since v1:
- added UDPLITE and SCTP test cases
- common constants and test_case data structure moved to libbind.h
Changes since v2:
- Code style fixes
- Makefile fix
runtest/syscalls | 2 + testcases/kernel/syscalls/bind/.gitignore | 2 + testcases/kernel/syscalls/bind/Makefile | 2 + testcases/kernel/syscalls/bind/bind04.c | 172 ++++++++++++++++++++ testcases/kernel/syscalls/bind/bind05.c | 186 ++++++++++++++++++++++ testcases/kernel/syscalls/bind/libbind.h | 29 ++++ 6 files changed, 393 insertions(+) create mode 100644 testcases/kernel/syscalls/bind/bind04.c create mode 100644 testcases/kernel/syscalls/bind/bind05.c create mode 100644 testcases/kernel/syscalls/bind/libbind.h
runtest/syscalls +2 -0
@@ -31,6 +31,8 @@ alarm07 alarm07 bind01 bind01 bind02 bind02 bind03 bind03 +bind04 bind04 +bind05 bind05 bpf_map01 bpf_map01 bpf_prog01 bpf_prog01
testcases/kernel/syscalls/bind/.gitignore +2 -0
@@ -1,3 +1,5 @@ /bind01 /bind02 /bind03 +/bind04 +/bind05
testcases/kernel/syscalls/bind/Makefile +2 -0
@@ -5,4 +5,6 @@ top_srcdir ?= ../../../.. include $(top_srcdir)/include/mk/testcases.mk +bind04 bind05: CFLAGS += -pthread + include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/bind/bind04.c new file +172 -0
172 lines not on this page · raw message
testcases/kernel/syscalls/bind/bind05.c new file +186 -0
186 lines not on this page · raw message
testcases/kernel/syscalls/bind/libbind.h new file +29 -0
@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2019 Martin Doucha <[email protected]> +*/ + +/* +* Common settings and data types for bind() connection tests +*/ + +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <arpa/inet.h> +#include <netinet/in.h> +#include <netinet/ip.h> + +#define MAIN_SOCKET_FILE "test.sock" +#define ABSTRACT_SOCKET_PATH "\0test.sock" +#define PEER_SOCKET_FILE "peer.sock" +#define IPV4_ADDRESS "127.0.0.1" +#define IPV6_ADDRESS "::1" +#define BUFFER_SIZE 128 + +struct test_case { +int type, protocol; +struct sockaddr *address; +socklen_t addrlen; +const char *description; +};
Signature · 1 line
2.25.1
PATCH v4 3/3 Add connection tests for bind()
Hi Martin,
whole patchset is very nice work. Thanks!
Review
Quoted 9 lines
(quoted diff) +9
-0+static void setup(void) +{ + srand(time(0)); + + tst_init_sockaddr_inet(&ipv4_addr, IPV4_ADDRESS, 0); + tst_init_sockaddr_inet_bin(&ipv4_any_addr, INADDR_ANY, 0); + tst_init_sockaddr_inet6_bin(&ipv6_addr, &in6addr_loopback, 0); + tst_init_sockaddr_inet6_bin(&ipv6_any_addr, &in6addr_any, 0); +}
You don't use tst_init_sockaddr_inet6() at all. How about using it?
Or do you have some reason not to use it?
Kind regards,
Petr
kernel/syscalls/bind/bind04.c +1 -1
@@ -77,7 +77,7 @@ static void setup(void)tst_init_sockaddr_inet (&ipv4_addr,IPV4_ADDRESS ,0 );tst_init_sockaddr_inet_bin (&ipv4_any_addr,INADDR_ANY ,0 ); -tst_init_sockaddr_inet6_bin (&ipv6_addr, &in6addr_loopback,0 ); +tst_init_sockaddr_inet6 (&ipv6_addr,IPV6_ADDRESS ,0 );tst_init_sockaddr_inet6_bin (&ipv6_any_addr, &in6addr_any,0 ); }
kernel/syscalls/bind/bind05.c +1 -1
@@ -77,7 +77,7 @@ static void setup(void)tst_init_sockaddr_inet (&ipv4_addr,IPV4_ADDRESS ,0 );tst_init_sockaddr_inet_bin (&ipv4_any_addr,INADDR_ANY ,0 ); -tst_init_sockaddr_inet6_bin (&ipv6_addr, &in6addr_loopback,0 ); +tst_init_sockaddr_inet6 (&ipv6_addr,IPV6_ADDRESS ,0 );tst_init_sockaddr_inet6_bin (&ipv6_any_addr, &in6addr_any,0 ); }
PATCH v4 3/3 Add connection tests for bind()
Quoted 15 lines · Petr Vorel
Hi Martin,
whole patchset is very nice work. Thanks!
Review
Quoted 9 lines
(quoted diff) +9
-0+static void setup(void) +{ + srand(time(0)); + + tst_init_sockaddr_inet(&ipv4_addr, IPV4_ADDRESS, 0); + tst_init_sockaddr_inet_bin(&ipv4_any_addr, INADDR_ANY, 0); + tst_init_sockaddr_inet6_bin(&ipv6_addr, &in6addr_loopback, 0); + tst_init_sockaddr_inet6_bin(&ipv6_any_addr, &in6addr_any, 0); +}You don't use
tst_init_sockaddr_inet6()at all. How about using it?
Quoted 1 line
Or do you have some reason not to use it?
tst_init_sockaddr_inet6() was trivial to implement so it's there for future use. The only reason why I'm not using it is that both loopback and wildcard addresses are provided by libc in expanded form so I don't need to convert them from string. I'll leave this patch as is.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH v4 3/3 Add connection tests for bind()
Hi Martin,
Quoted 4 lines
tst_init_sockaddr_inet6()was trivial to implement so it's there for future use. The only reason why I'm not using it is that both loopback and wildcard addresses are provided by libc in expanded form so I don't need to convert them from string. I'll leave this patch as is.
Understand. My concern was that using it there would be more test coverage, but actually it wouldn't. I'm ok to merge it as it is, just remove IPV6_ADDRESS, which you don't use.
Kind regards,
Petr
PATCH v4 3/3 Add connection tests for bind()
In reply to Martin DouchaHi Martin,
testcases/kernel/syscalls/bind/bind04.c line 143
Quoted 7 lines
testcases/kernel/syscalls/bind/bind04.c new file +7
-0+SAFE_LISTEN (listen_sock,1 ); +SAFE_PTHREAD_CREATE (&thread_id,NULL , peer_thread, &tc_copy); + sock =accept (listen_sock, (struct sockaddr *)&remote_addr, + &remote_len); + +if (sock <0 ) +tst_brk (TBROK |TERRNO ,"accept() failed" );
We can just use
sock = SAFE_ACCEPT(listen_sock, (struct sockaddr *)&remote_addr,
&remote_len);
Whole patch set looks ok to me.
Kind regards,
Petr
PATCH v4 1/3 Create separate .c file for include/tst_net.h
In reply to Martin DouchaHi Martin,
thanks for doing this cleanup, LGTM
(just lib/tst_net.c should get SPDX licence before merging).
Minor thoughts below.
include/tst_net.h line 10
Quoted 5 lines
#include <
arpa/inet.h>include/tst_net.h +0
-3-#include <errno.h> #include <stdio.h> -#include <stdlib.h> -#include <string.h>
BTW (not related to this change) tst_net.h and tst_private.h must be included after tst_test.h. But that's probably ok (include/tst_safe_clocks.h includes it).
...
include/tst_private.h line 10
Quoted 12 lines
+++
b/include/tst_private.hinclude/tst_private.h new file +10
-0@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2017-2019 Petr Vorel <[email protected]> +* +* Internal helper functions for the shell library. Do not use directly +* in test programs. +*/ + +#ifndef TST_PRIVATE_H_ +#define TST_PRIVATE_H_
This file is now just for tst_net_iface_prefix.c tst_net_ip_prefix.c and tst_net_vars.c. And I bet that it stays like this, because most of code in lib/ is actually the API code, which is supposed to be exported. Therefore I'd chose name specific to these 3 files, but tst_private.h is maybe better.
...
Quoted 18 lines
include/tst_private.h new file +6
-0+void tst_print_svar (const char *name,const char *val); +void tst_print_svar_change (const char *name,const char *val); + +int tst_get_prefix (const char *ip_str,int is_ipv6); + +#endif lib/tst_net.c new file +6
-0@@ -0,0 +1,147 @@ +/* +* Copyright (c) 2017 Petr Vorel <[email protected]> +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 2 of the License, or
lib/tst_net.c line 16
Quoted 10 lines
lib/tst_net.c new file +10
-0+* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/
Hm, this should be replaced by simple:
// SPDX-License-Identifier: GPL-2.0-or-later
Can be done during merge.
Quoted 11 lines
lib/tst_net.c new file +11
-0+ +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#define TST_NO_DEFAULT_MAIN +#include "tst_test.h" +#include "tst_net.h" +#include "tst_private.h" + +void tst_print_svar (const char *name,const char *val)
lib/tst_net.c line 37
Quoted 10 lines
lib/tst_net.c new file +10
-0+{ +if (name && val) +printf ("export %s=\"%s\"\n" , name, val); +} + +void tst_print_svar_change (const char *name,const char *val) +{ +if (name && val) +printf ("export %s=\"${%s:-%s}\"\n" , name, name, val); +}
I'd be happy enough to have these functions not exported (i.e. in tst_private.h as static inline), but I believe you that today's compilers strip out unused code :).
Kind regards,
Petr