network/in6_02: Rewrite to the new library
PATCH 1/2 network/in6_02: Rewrite to the new library
--- testcases/network/lib6/in6_02.c | 222 +++++++++++++++++++--------------------- 1 file changed, 106 insertions(+), 116 deletions(-)
testcases/network/lib6/in6_02.c +106 -116
@@ -1,110 +1,91 @@/* +* Copyright (c) International Business Machines Corp., 2001 +* Copyright (c) 2018 Petr Vorel <[email protected]> * -* Copyright (c) International Business Machines Corp., 2001 -* Author: David L Stevens +* Author: David L Stevens * -* 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 -* (at your option) any later version. +* 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 (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. +* This program is distributed in the hope that it would 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, write to the Free Software Foundation, -* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ -/* -* Description: -* Tests for name to index and index to name functions in IPv6 +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +* +* Description: +* IPv6 name to index and index to name function tests */ + +#include <stdlib.h> +#include <stdio.h> #include <unistd.h> #include <errno.h> #include <sys/socket.h> #include <net/if.h> -#include "test.h" +#include "tst_test.h" + +#define I2N_RNDCOUNT 10/* random ints */ +#define I2N_LOWCOUNT 10/* sequential from 0 */ +#define DEFAULT_IFACE "eth0"static struct {char *name;int nonzero; -} n2i[] = { +} test_case[] = { {"lo" ,1 }, - {"eth0" ,1 }, + {DEFAULT_IFACE ,1 }, {"hoser75" ,0 }, {"6" ,0 }, }; -#define N2I_COUNT (sizeof(n2i)/sizeof(n2i[0])) -#define I2N_RNDCOUNT 10/* random ints */ -#define I2N_LOWCOUNT 10/* sequential from 0 */ -static void setup (void ); -static void n2itest (void ); -static void i2ntest (void ); -static void initest (void ); - -static void (*testfunc[])(void ) = { n2itest, - i2ntest, initest }; - -char *TCID ="in6_02" ; -int TST_TOTAL =ARRAY_SIZE (testfunc); - -int main (int argc,char *argv[]) -{ -int lc; -int i; - -tst_parse_opts (argc, argv,NULL ,NULL ); - -setup (); +static void if_nametoindex_test (void ); +static void if_indextoname_test (void ); +static void if_nameindex_test (void ); -for (lc =0 ;TEST_LOOPING (lc); ++lc) { - tst_count =0 ; +static void (*testfunc[])(void ) = { if_nametoindex_test, if_indextoname_test, + if_nameindex_test }; -for (i =0 ; i <TST_TOTAL ; i++) - (*testfunc[i])(); - } - -tst_exit (); -} - -/* if_nametoindex tests */ -void n2itest (void ) +void if_nametoindex_test (void ) {unsigned int i;char ifname[IF_NAMESIZE ], *pifn; -for (i =0 ; i <N2I_COUNT ; ++i) { -TEST (if_nametoindex (n2i[i].name)); -if (!TEST_RETURN != !n2i[i].nonzero) { -tst_resm (TFAIL ,"if_nametoindex(\"%s\") %ld " -"[should be %szero]" , n2i[i].name,TEST_RETURN , - n2i[i].nonzero ?"non" :"" ); +tst_res (TINFO ,"IPv6 if_nametoindex() test" ); + +for (i =0 ; i <ARRAY_SIZE (test_case); ++i) { +TEST (if_nametoindex (test_case[i].name)); +if (!TEST_RETURN != !test_case[i].nonzero) { +tst_res (TFAIL ,"if_nametoindex(\"%s\") %ld [should be %szero]" , + test_case[i].name,TEST_RETURN , test_case[i].nonzero ?"non" + :"" );return ; }if (TEST_RETURN ) { pifn =if_indextoname (TEST_RETURN , ifname); -if (!pifn ||strcmp (n2i[i].name, pifn)) { -tst_resm (TFAIL ,"if_nametoindex(\"%s\") %ld " -"doesn't match if_indextoname(%ld) " -"\"%s\"" , n2i[i].name,TEST_RETURN , -TEST_RETURN , pifn ? pifn :"" ); +if (!pifn ||strcmp (test_case[i].name, pifn)) { +tst_res (TFAIL , +"if_nametoindex(\"%s\") %ld doesn't match if_indextoname(%ld) \"%s\"" , + test_case[i].name,TEST_RETURN ,TEST_RETURN , pifn ? pifn + :"" );return ; } } -tst_resm (TINFO ,"if_nametoindex(\"%s\") %ld" , - n2i[i].name,TEST_RETURN ); +tst_res (TINFO ,"if_nametoindex(\"%s\") %ld" , + test_case[i].name,TEST_RETURN ); } -tst_resm (TPASS ,"if_nametoindex() tests succeed" ); +tst_res (TPASS ,"if_nametoindex() test succeed" ); } -int sub_i2ntest (unsigned int if_index) +int sub_if_indextoname_test (unsigned int if_index) {char ifname[IF_NAMESIZE ];unsigned int idx;
@@ -112,41 +93,42 @@ int sub_i2ntest(unsigned int if_index)TEST ((ifname ==if_indextoname (if_index, ifname)));if (!TEST_RETURN ) {if (TEST_ERRNO !=ENXIO ) { -tst_resm (TFAIL ,"if_indextoname(%d) returns %ld " -"but errno %d != ENXIO" , if_index,TEST_RETURN , -TEST_ERRNO ); +tst_res (TFAIL , +"if_indextoname(%d) returns %ld but errno %d != ENXIO" , + if_index,TEST_RETURN ,TEST_ERRNO );return 0 ; } -tst_resm (TINFO ,"if_indextoname(%d) returns NULL" , if_index); +tst_res (TINFO ,"if_indextoname(%d) returns NULL" , if_index);return 1 ; }/* else, a valid interface-- double check name */ idx =if_nametoindex (ifname);if (idx != if_index) { -tst_resm (TFAIL ,"if_indextoname(%u) returns \"%s\" but " -"doesn't if_nametoindex(\"%s\") returns %u" , - if_index, ifname, ifname, idx); +tst_res (TFAIL , +"if_indextoname(%u) returns \"%s\" but doesn't if_nametoindex(\"%s\") returns %u" , + if_index, ifname, ifname, idx);return 0 ; } -tst_resm (TINFO ,"if_indextoname(%d) returns \"%s\"" , if_index, ifname); +tst_res (TINFO ,"if_indextoname(%d) returns \"%s\"" , if_index, ifname);return 1 ; } -/* if_indextoname tests */ -void i2ntest (void ) +void if_indextoname_test (void ) {unsigned int i; +tst_res (TINFO ,"IPv6 if_indextoname() test" ); +/* some low-numbered indexes-- likely to get valid interfaces here */ for (i =0 ; i <I2N_LOWCOUNT ; ++i) -if (!sub_i2ntest (i)) +if (!sub_if_indextoname_test (i))return ;/* skip the rest, if broken */ /* some random ints; should mostly fail */ for (i =0 ; i <I2N_RNDCOUNT ; ++i) -if (!sub_i2ntest (rand ())) +if (!sub_if_indextoname_test (rand ()))return ;/* skip the rest, if broken */ -tst_resm (TPASS ,"if_indextoname() tests succeed" ); +tst_res (TPASS ,"if_indextoname() test succeed" ); }/*
@@ -176,8 +158,7 @@ int getdatasize(void)return dsize; } -/* if_nameindex tests */ -void initest (void ) +void if_nameindex_test (void ) {struct if_nameindex *pini;int i;
@@ -186,92 +167,101 @@ void initest(void)int freenicount;int dsize_before, dsize_after; +tst_res (TINFO ,"IPv6 if_nameindex() test" ); + pini =if_nameindex ();if (pini ==NULL ) { -tst_resm (TFAIL ,"if_nameindex() returns NULL, errno %d (%s)" , -TEST_ERRNO ,strerror (TEST_ERRNO )); +tst_res (TFAIL ,"if_nameindex() returns NULL, errno %d (%s)" , +TEST_ERRNO ,strerror (TEST_ERRNO ));return ; }for (i =0 ; pini[i].if_index; ++i) { p =if_indextoname (pini[i].if_index, buf);if (!p ||strcmp (p, pini[i].if_name)) { -tst_resm (TFAIL ,"if_nameindex idx %d name \"%s\" but " -"if_indextoname(%d) is \"%s\"" , - pini[i].if_index, pini[i].if_name, - pini[i].if_index, p ? p :"" ); +tst_res (TFAIL , +"if_nameindex idx %d name \"%s\" but if_indextoname(%d) is \"%s\"" , + pini[i].if_index, pini[i].if_name, + pini[i].if_index, p ? p :"" );return ; } idx =if_nametoindex (pini[i].if_name);if (idx != pini[i].if_index) { -tst_resm (TFAIL ,"if_nameindex idx %d name \"%s\" but " -"if_indextoname(\"%s\") is %d" , - pini[i].if_index, pini[i].if_name, - pini[i].if_name, idx); +tst_res (TFAIL , +"if_nameindex idx %d name \"%s\" but if_indextoname(\"%s\") is %d" , + pini[i].if_index, pini[i].if_name, + pini[i].if_name, idx);return ; } -tst_resm (TINFO ,"if_nameindex idx %d name \"%s\"" , +tst_res (TINFO ,"if_nameindex idx %d name \"%s\"" , pini[i].if_index, pini[i].if_name); }if_freenameindex (pini); -/* if_freenameindex() has no error conditions; see if we run +/* +* if_freenameindex() has no error conditions; see if we run * out of memory if we do it a lot. */ dsize_before =getdatasize ();if (dsize_before <0 ) { -tst_brkm (TBROK ,NULL ,"getdatasize failed: errno %d (%s)" , +tst_brk (TBROK ,"getdatasize failed: errno %d (%s)" , errno,strerror (errno)); } -/* we need to leak at least a page to detect a leak; 1 byte per call + +/* +* we need to leak@least a page to detect a leak; 1 byte per call * will be detected with getpagesize() calls. */ freenicount =getpagesize ();for (i =0 ; i < freenicount; ++i) { pini =if_nameindex ();if (pini ==NULL ) { -tst_resm (TINFO ,"if_freenameindex test failed " -"if_nameindex() iteration %d" , i); +tst_res (TINFO , +"if_freenameindex test failed if_nameindex() iteration %d" , i);break ; }if_freenameindex (pini); } dsize_after =getdatasize ();if (dsize_after <0 ) { -tst_brkm (TBROK ,NULL ,"getdatasize failed: errno %d (%s)" , +tst_brk (TBROK ,"getdatasize failed: errno %d (%s)" , errno,strerror (errno)); }if (dsize_after > dsize_before +getpagesize ()) { -tst_resm (TFAIL ,"if_freenameindex leaking memory " -"(%d iterations) dsize before %d dsize after %d" , i, - dsize_before, dsize_after); +tst_res (TFAIL , +"if_freenameindex leaking memory (%d iterations) dsize before %d dsize after %d" , + i, dsize_before, dsize_after);return ; - }else { -tst_resm (TINFO ,"if_freenameindex passed %d iterations" , i); } +tst_res (TINFO ,"if_freenameindex passed %d iterations" , i); -tst_resm (TPASS ,"if_nameindex() tests succeed" ); +tst_res (TPASS ,"if_nameindex() test succeed" ); }void setup (void ) { -TEST_PAUSE ; - -tst_resm (TINFO ,"get interface name from LHOST_IFACES var" ); -char *ifnames =getenv ("LHOST_IFACES" );if (!ifnames) { -tst_resm (TWARN ,"LHOST_IFACES not defined, default to eth0" ); +tst_res (TWARN ,"LHOST_IFACES not defined, default to %s" ,DEFAULT_IFACE );return ; }static char name[256 ]; -sscanf (ifnames,"%255s" , name); - -if (!strcmp (name, n2i[1 ].name)) +if (!strcmp (name, test_case[1 ].name))return ; -tst_resm (TINFO ,"change default 'eth0' name to '%s'" , name); - n2i[1 ].name = name; +tst_res (TINFO ,"change default '%s' name to '%s'" ,DEFAULT_IFACE , name); + test_case[1 ].name = name; +} + +static void do_test (unsigned int i) +{ + (*testfunc[i])(); } + +static struct tst_test test = { + .tcnt =ARRAY_SIZE (testfunc), + .setup = setup, + .test = do_test, +};
Signature · 1 line
2.16.3
PATCH 2/2 network/in6_02: Don't use default value for LHOST_IFACES
in6_02 can be test without help of network.sh. In that case LHOST_IFACES is not defined and "eth0" is used as default value. This can cause failures.
---
I'm not sure if we can make all C network tests make independent on loading variables via tst_net.sh, but here it does not make much sense to me to fail when it's not done.
--- testcases/network/lib6/in6_02.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
testcases/network/lib6/in6_02.c +6 -5
@@ -33,14 +33,13 @@#define I2N_RNDCOUNT 10/* random ints */ #define I2N_LOWCOUNT 10/* sequential from 0 */ -#define DEFAULT_IFACE "eth0"static struct {char *name;int nonzero; } test_case[] = { {"lo" ,1 }, - {DEFAULT_IFACE ,1 }, + {NULL ,1 }, {"hoser75" ,0 }, {"6" ,0 }, };
@@ -61,6 +60,8 @@ void if_nametoindex_test(void)tst_res (TINFO ,"IPv6 if_nametoindex() test" );for (i =0 ; i <ARRAY_SIZE (test_case); ++i) { +if (test_case[i].name ==NULL ) +continue ;TEST (if_nametoindex (test_case[i].name));if (!TEST_RETURN != !test_case[i].nonzero) { tst_res(TFAIL, "if_nametoindex(\"%s\") %ld [should be %szero]",
@@ -242,16 +243,16 @@ void setup(void)char *ifnames =getenv ("LHOST_IFACES" );if (!ifnames) { -tst_res (TWARN ,"LHOST_IFACES not defined, default to %s" ,DEFAULT_IFACE ); +tst_res (TINFO ,"LHOST_IFACES not defined, it will not be tested" );return ; }static char name[256 ];sscanf (ifnames,"%255s" , name); -if (!strcmp (name, test_case[1 ].name)) +if (!strcmp (name, test_case[0 ].name))return ; -tst_res (TINFO ,"change default '%s' name to '%s'" ,DEFAULT_IFACE , name); +tst_res (TINFO ,"LHOST_IFACES \"%s\"" , name); test_case[1 ].name = name; }
Signature · 1 line
2.16.3
PATCH 1/2 network/in6_02: Rewrite to the new library
In reply to Petr VorelOn 04/17/2018 03:42 PM, Petr Vorel wrote:
...
Quoted 9 lines
testcases/network/lib6/in6_02.c +7
-0+ +for (i =0 ; i <ARRAY_SIZE (test_case); ++i) { +TEST (if_nametoindex (test_case[i].name)); +if (!TEST_RETURN != !test_case[i].nonzero) { +tst_res (TFAIL ,"if_nametoindex(\"%s\") %ld [should be %szero]" , + test_case[i].name,TEST_RETURN , test_case[i].nonzero ?"non" + :"" );return ; }
testcases/network/lib6/in6_02.c line 75
Quoted 10 lines
testcases/network/lib6/in6_02.c +3
-5if (TEST_RETURN ) { pifn =if_indextoname (TEST_RETURN , ifname); -if (!pifn ||strcmp (n2i[i].name, pifn)) { -tst_resm (TFAIL ,"if_nametoindex(\"%s\") %ld " -"doesn't match if_indextoname(%ld) " -"\"%s\"" , n2i[i].name,TEST_RETURN , -TEST_RETURN , pifn ? pifn :"" ); +if (!pifn ||strcmp (test_case[i].name, pifn)) { + tst_res(TFAIL, + "if_nametoindex(\"%s\") %ld doesn't match if_indextoname(%ld) \"%s\"",
Hi Petr,
It would better to replace all occurrence of \" with '. Or even without it, if it is inside parenthesis.
Quoted 7 lines
testcases/network/lib6/in6_02.c +2
-2+ test_case[i].name,TEST_RETURN ,TEST_RETURN , pifn ? pifn + : "");return ; } } -tst_resm (TINFO ,"if_nametoindex(\"%s\") %ld" , - n2i[i].name,TEST_RETURN );
testcases/network/lib6/in6_02.c line 88
Quoted 10 lines
testcases/network/lib6/in6_02.c +4
-2+tst_res (TINFO ,"if_nametoindex(\"%s\") %ld" , + test_case[i].name,TEST_RETURN ); } -tst_resm (TPASS ,"if_nametoindex() tests succeed" ); +tst_res (TPASS ,"if_nametoindex() test succeed" ); } -int sub_i2ntest (unsigned int if_index) +int sub_if_indextoname_test (unsigned int if_index)
Should the functions be static in this test?
Quoted 35 lines
{
testcases/network/lib6/in6_02.c +8
-8char ifname[IF_NAMESIZE ];unsigned int idx;@@ -112,41 +93,42 @@ int sub_i2ntest(unsigned int if_index)TEST ((ifname ==if_indextoname (if_index, ifname)));if (!TEST_RETURN ) {if (TEST_ERRNO !=ENXIO ) { -tst_resm (TFAIL ,"if_indextoname(%d) returns %ld " -"but errno %d != ENXIO" , if_index,TEST_RETURN , -TEST_ERRNO ); +tst_res (TFAIL , +"if_indextoname(%d) returns %ld but errno %d != ENXIO" , + if_index,TEST_RETURN ,TEST_ERRNO );return 0 ; } -tst_resm (TINFO ,"if_indextoname(%d) returns NULL" , if_index); +tst_res (TINFO ,"if_indextoname(%d) returns NULL" , if_index);return 1 ; }/* else, a valid interface-- double check name */ idx =if_nametoindex (ifname);if (idx != if_index) { -tst_resm (TFAIL ,"if_indextoname(%u) returns \"%s\" but " -"doesn't if_nametoindex(\"%s\") returns %u" , - if_index, ifname, ifname, idx); +tst_res (TFAIL , +"if_indextoname(%u) returns \"%s\" but doesn't if_nametoindex(\"%s\") returns %u" , + if_index, ifname, ifname, idx);return 0 ; } -tst_resm (TINFO ,"if_indextoname(%d) returns \"%s\"" , if_index, ifname); +tst_res (TINFO ,"if_indextoname(%d) returns \"%s\"" , if_index, ifname);return 1 ; }
...
testcases/network/lib6/in6_02.c void initest(void) · line 260
Quoted 4 lines
testcases/network/lib6/in6_02.c +4
-0+ +static void do_test (unsigned int i) +{ + (*testfunc[i])();
It can be just testfunc[i]()
Thanks,
Alexey
PATCH 2/2 network/in6_02: Don't use default value for LHOST_IFACES
In reply to Petr VorelQuoted 4 lines · Petr Vorel
in6_02can be test without help ofnetwork.sh. In that caseLHOST_IFACESis not defined and "eth0" is used as default value. This can cause failures.
I think the fix should be before the conversion to the new API, otherwise it unnecessary depends on it.
Quoted 30 lines
Signed-off-by Petr Vorel <[email protected]>---
I'm not sure if we can make all C network tests make independent on loading variables viatst_net.sh, but here it does not make much sense to me to fail when it's not done.--- testcases/network/lib6/in6_02.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)testcases/network/lib6/in6_02.c +1
-2@@ -33,14 +33,13 @@#define I2N_RNDCOUNT 10/* random ints */ #define I2N_LOWCOUNT 10/* sequential from 0 */ -#define DEFAULT_IFACE "eth0"static struct {char *name;int nonzero; } test_case[] = { {"lo" ,1 }, - {DEFAULT_IFACE ,1 }, + {NULL ,1 }, {"hoser75" ,0 }, {"6" ,0 }, };
testcases/network/lib6/in6_02.c void if_nametoindex_test(void) · line 64
Quoted 5 lines
testcases/network/lib6/in6_02.c +2
-0@@ -61,6 +60,8 @@ void if_nametoindex_test(void)tst_res (TINFO ,"IPv6 if_nametoindex() test" );for (i =0 ; i <ARRAY_SIZE (test_case); ++i) { +if (test_case[i].name ==NULL ) +continue ;
What about using TCONF if it skips the test-case because LHOST_IFACE is not defined?
Thanks,
Alexey
PATCH 2/2 network/in6_02: Don't use default value for LHOST_IFACES
Hi Alexey,
Quoted 7 lines · Petr Vorel
Quoted 3 lines
in6_02can be test without help ofnetwork.sh. In that caseLHOST_IFACESis not defined and "eth0" is used as default value. This can cause failures.I think the fix should be before the conversion to the new API, otherwise it unnecessary depends on it.
Agree, I'll fix it.
...
Quoted 6 lines
testcases/network/lib6/in6_02.c void if_nametoindex_test(void) · line 64
Quoted 3 lines
testcases/network/lib6/in6_02.c +2
-0for (i =0 ; i <ARRAY_SIZE (test_case); ++i) { +if (test_case[i].name ==NULL ) +continue ;What about using TCONF if it skips the test-case because
LHOST_IFACEis not defined?
I wanted to keep at least testing of if_nametoindex("lo").
Would be overkill in case of TCONF to create in6_02.sh which would just load tst_net.sh? I don't like the fact, that network C tests depend on being run via network.sh (unlike shell network tests).
Quoted 2 lines
Thanks,
Alexey
Kind regards,
Petr
PATCH 2/2 network/in6_02: Don't use default value for LHOST_IFACES
Quoted 18 lines · Petr Vorel
Hi Alexey,
Quoted 7 lines · Petr Vorel
Quoted 3 lines
in6_02can be test without help ofnetwork.sh. In that caseLHOST_IFACESis not defined and "eth0" is used as default value. This can cause failures.I think the fix should be before the conversion to the new API, otherwise it unnecessary depends on it.
Agree, I'll fix it.
...
Quoted 6 lines
testcases/network/lib6/in6_02.c void if_nametoindex_test(void) · line 64
Quoted 3 lines
testcases/network/lib6/in6_02.c +2
-0for (i =0 ; i <ARRAY_SIZE (test_case); ++i) { +if (test_case[i].name ==NULL ) +continue ;What about using TCONF if it skips the test-case because
LHOST_IFACEis not defined?
Quoted 4 lines
I wanted to keep at least testing of
if_nametoindex("lo").
Would be overkill in case of TCONF to createin6_02.shwhich would just loadtst_net.sh? I don't like the fact, that network C tests depend on being run vianetwork.sh(unlike shell network tests).
Why it depends on network.sh? LHOST_IFACE is just an environment variable. It can be defined on the test machine already before the test is started, or with tst_net.sh.
I mean, using TCONF for the LHOST_IFACE test-case, when the test in6_02, doesn't find the variable and skips it, then proceeds with the rest of test-cases.
PATCH 2/2 network/in6_02: Don't use default value for LHOST_IFACES
Hi Alexey,
Quoted 10 lines
Quoted 6 lines
Quoted 2 lines
What about using TCONF if it skips the test-case because
LHOST_IFACEis not defined?I wanted to keep at least testing of
if_nametoindex("lo").
Would be overkill in case of TCONF to createin6_02.shwhich would just loadtst_net.sh? I don't like the fact, that network C tests depend on being run vianetwork.sh(unlike shell network tests).Why it depends on
network.sh?LHOST_IFACEis just an environment variable. It can be defined on the test machine already before the test is started, or withtst_net.sh.
Yes, but that's what I doesn't like: you need to source tst_net.sh (either with network.sh or manually). Not a big issue, it's just you shouldn't run C tests like in6_02 without variables. This is different from other non-networking tests, which you can run as they are (without any preparation, setup).
This is the reason I was thinking about wrapping any C test which need variables with shell script which source tst_net.sh for it.
Kind regards,
Petr