"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/tests/ifinfo_tests.c" (26 Jul 2021, 10194 Bytes) of package /linux/misc/vnstat-2.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "ifinfo_tests.c": 2.7_vs_2.8.

    1 #include "common.h"
    2 #include "vnstat_tests.h"
    3 #include "ifinfo_tests.h"
    4 #include "ifinfo.h"
    5 #include "dbaccess.h"
    6 #include "misc.h"
    7 #include "cfg.h"
    8 #include "ibw.h"
    9 
   10 START_TEST(getifliststring_no_source)
   11 {
   12     char *ifacelist;
   13 
   14     linuxonly;
   15 
   16     ck_assert_int_eq(remove_directory(TESTDIR), 1);
   17     ck_assert_int_eq(getifliststring(&ifacelist, 0), 0);
   18     ck_assert_str_eq(ifacelist, "");
   19 
   20     free(ifacelist);
   21 }
   22 END_TEST
   23 
   24 START_TEST(getifliststring_proc_one_interface)
   25 {
   26     char *ifacelist;
   27 
   28     linuxonly;
   29 
   30     ck_assert_int_eq(remove_directory(TESTDIR), 1);
   31     fake_proc_net_dev("w", "ethunusual", 0, 0, 0, 0);
   32 
   33     ck_assert_int_eq(getifliststring(&ifacelist, 1), 1);
   34     ck_assert_str_eq(ifacelist, "ethunusual ");
   35 
   36     free(ifacelist);
   37 }
   38 END_TEST
   39 
   40 START_TEST(getifliststring_proc_one_interface_with_speed)
   41 {
   42     char *ifacelist;
   43 
   44     linuxonly;
   45 
   46     ck_assert_int_eq(remove_directory(TESTDIR), 1);
   47     fake_proc_net_dev("w", "ethunusual", 0, 0, 0, 0);
   48     fake_sys_class_net("ethunusual", 0, 0, 0, 0, 10);
   49 
   50     ck_assert_int_eq(getifliststring(&ifacelist, 1), 1);
   51     ck_assert_str_eq(ifacelist, "ethunusual (10 Mbit) ");
   52 
   53     free(ifacelist);
   54 }
   55 END_TEST
   56 
   57 START_TEST(getifliststring_proc_multiple_interfaces)
   58 {
   59     char *ifacelist;
   60 
   61     linuxonly;
   62 
   63     ck_assert_int_eq(remove_directory(TESTDIR), 1);
   64     fake_proc_net_dev("w", "random", 0, 0, 0, 0);
   65     fake_proc_net_dev("a", "interfaces", 0, 0, 0, 0);
   66     fake_proc_net_dev("a", "having", 0, 0, 0, 0);
   67     fake_proc_net_dev("a", "fun", 0, 0, 0, 0);
   68     fake_proc_net_dev("a", "i", 0, 0, 0, 0);
   69 
   70     ck_assert_int_eq(getifliststring(&ifacelist, 0), 1);
   71     ck_assert_str_eq(ifacelist, "random interfaces having fun i ");
   72 
   73     free(ifacelist);
   74 }
   75 END_TEST
   76 
   77 START_TEST(getifliststring_proc_multiple_interfaces_validating)
   78 {
   79     char *ifacelist;
   80 
   81     linuxonly;
   82 
   83     ck_assert_int_eq(remove_directory(TESTDIR), 1);
   84     fake_proc_net_dev("w", "random", 0, 0, 0, 0);
   85     fake_proc_net_dev("a", "interfaces", 0, 0, 0, 0);
   86     fake_proc_net_dev("a", "having", 0, 0, 0, 0);
   87     fake_proc_net_dev("a", "sit0", 0, 0, 0, 0);
   88     fake_proc_net_dev("a", "fun", 0, 0, 0, 0);
   89     fake_proc_net_dev("a", "lo0", 0, 0, 0, 0);
   90     fake_proc_net_dev("a", "eth0:0", 0, 0, 0, 0);
   91     fake_proc_net_dev("a", "i", 0, 0, 0, 0);
   92 
   93     ck_assert_int_eq(getifliststring(&ifacelist, 0), 1);
   94     ck_assert_str_eq(ifacelist, "random interfaces having fun eth0 i ");
   95 
   96     free(ifacelist);
   97 }
   98 END_TEST
   99 
  100 START_TEST(getifliststring_proc_long_interface_names)
  101 {
  102     char *ifacelist;
  103 
  104     linuxonly;
  105 
  106     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  107     fake_proc_net_dev("w", "random", 0, 0, 0, 0);
  108     fake_proc_net_dev("a", "interfaces", 0, 0, 0, 0);
  109     fake_proc_net_dev("a", "having", 0, 0, 0, 0);
  110     fake_proc_net_dev("a", "toomuchfun", 0, 0, 0, 0);
  111     fake_proc_net_dev("a", "longinterfaceislong", 0, 0, 0, 0);
  112     fake_proc_net_dev("a", "longestinterfaceislongerthanshouldbeexpectedanywhereinanormallyfunctioningenvironment", 0, 0, 0, 0);
  113     fake_proc_net_dev("a", "a", 0, 0, 0, 0);
  114 
  115     ck_assert_int_eq(getifliststring(&ifacelist, 0), 1);
  116     ck_assert_str_eq(ifacelist, "random interfaces having toomuchfun longinterfaceislong a ");
  117 
  118     free(ifacelist);
  119 }
  120 END_TEST
  121 
  122 START_TEST(getifliststring_sysclassnet_one_interface)
  123 {
  124     char *ifacelist;
  125 
  126     linuxonly;
  127 
  128     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  129     fake_sys_class_net("ethunusual", 0, 0, 0, 0, 0);
  130 
  131     ck_assert_int_eq(getifliststring(&ifacelist, 1), 1);
  132     ck_assert_str_eq(ifacelist, "ethunusual ");
  133 
  134     free(ifacelist);
  135 }
  136 END_TEST
  137 
  138 START_TEST(getifliststring_sysclassnet_one_interface_with_speed)
  139 {
  140     char *ifacelist;
  141 
  142     linuxonly;
  143 
  144     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  145     fake_sys_class_net("ethunusual", 0, 0, 0, 0, 10);
  146 
  147     ck_assert_int_eq(getifliststring(&ifacelist, 1), 1);
  148     ck_assert_str_eq(ifacelist, "ethunusual (10 Mbit) ");
  149 
  150     free(ifacelist);
  151 }
  152 END_TEST
  153 
  154 START_TEST(getifliststring_sysclassnet_multiple_interfaces)
  155 {
  156     char *ifacelist;
  157 
  158     linuxonly;
  159 
  160     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  161     fake_sys_class_net("random", 0, 0, 0, 0, 0);
  162     fake_sys_class_net("interfaces", 0, 0, 0, 0, 0);
  163     fake_sys_class_net("having", 0, 0, 0, 0, 0);
  164     fake_sys_class_net("fun", 0, 0, 0, 0, 0);
  165     fake_sys_class_net("i", 0, 0, 0, 0, 0);
  166 
  167     ck_assert_int_eq(getifliststring(&ifacelist, 0), 1);
  168     ck_assert_int_eq(strlen(ifacelist), 31);
  169 
  170     free(ifacelist);
  171 }
  172 END_TEST
  173 
  174 START_TEST(getifliststring_sysclassnet_multiple_interfaces_validating)
  175 {
  176     char *ifacelist;
  177 
  178     linuxonly;
  179 
  180     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  181     fake_sys_class_net("lo", 0, 0, 0, 0, 0);
  182     fake_sys_class_net("random", 0, 0, 0, 0, 0);
  183     fake_sys_class_net("lo0", 0, 0, 0, 0, 0);
  184     fake_sys_class_net("interfaces", 0, 0, 0, 0, 0);
  185     fake_sys_class_net("having", 0, 0, 0, 0, 0);
  186     fake_sys_class_net("fun", 0, 0, 0, 0, 0);
  187     fake_sys_class_net("sit0", 0, 0, 0, 0, 0);
  188     fake_sys_class_net("eth0:0", 0, 0, 0, 0, 0);
  189     fake_sys_class_net("i", 0, 0, 0, 0, 0);
  190 
  191     ck_assert_int_eq(getifliststring(&ifacelist, 0), 1);
  192     ck_assert_int_eq(strlen(ifacelist), 31);
  193 
  194     free(ifacelist);
  195 }
  196 END_TEST
  197 
  198 START_TEST(getifliststring_sysclassnet_long_interface_names)
  199 {
  200     char *ifacelist;
  201 
  202     linuxonly;
  203 
  204     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  205     fake_sys_class_net("random", 0, 0, 0, 0, 0);
  206     fake_sys_class_net("interfaces", 0, 0, 0, 0, 0);
  207     fake_sys_class_net("having", 0, 0, 0, 0, 0);
  208     fake_sys_class_net("toomuchfun", 0, 0, 0, 0, 0);
  209     fake_sys_class_net("longinterfaceislong", 0, 0, 0, 0, 0);
  210     fake_sys_class_net("longestinterfaceislongerthanshouldbeexpectedanywhereinanormallyfunctioningenvironment", 0, 0, 0, 0, 0);
  211     fake_sys_class_net("a", 0, 0, 0, 0, 0);
  212 
  213     ck_assert_int_eq(getifliststring(&ifacelist, 0), 1);
  214     ck_assert_int_eq(strlen(ifacelist), 58);
  215 
  216     free(ifacelist);
  217 }
  218 END_TEST
  219 
  220 START_TEST(readproc_no_file)
  221 {
  222     linuxonly;
  223 
  224     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  225     ck_assert_int_eq(readproc("ethunusual"), 0);
  226 }
  227 END_TEST
  228 
  229 START_TEST(readproc_not_found)
  230 {
  231     linuxonly;
  232 
  233     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  234     fake_proc_net_dev("w", "ethwrong", 10, 20, 30, 40);
  235 
  236     ck_assert_int_eq(readproc("ethunusual"), 0);
  237 }
  238 END_TEST
  239 
  240 START_TEST(readproc_success)
  241 {
  242     linuxonly;
  243 
  244     noexit = 0;
  245     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  246     fake_proc_net_dev("w", "ethwrong", 10, 20, 30, 40);
  247     fake_proc_net_dev("a", "ethunusual", 1, 2, 3, 4);
  248 
  249     ck_assert_int_eq(readproc("ethunusual"), 1);
  250     ck_assert_str_eq(ifinfo.name, "ethunusual");
  251     ck_assert_int_eq(ifinfo.filled, 1);
  252     ck_assert_int_eq(ifinfo.rx, 1);
  253     ck_assert_int_eq(ifinfo.tx, 2);
  254     ck_assert_int_eq(ifinfo.rxp, 3);
  255     ck_assert_int_eq(ifinfo.txp, 4);
  256 }
  257 END_TEST
  258 
  259 START_TEST(readsysclassnet_not_found)
  260 {
  261     linuxonly;
  262 
  263     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  264     fake_sys_class_net("ethwrong", 10, 20, 30, 40, 50);
  265 
  266     ck_assert_int_eq(readsysclassnet("ethunusual"), 0);
  267 }
  268 END_TEST
  269 
  270 START_TEST(readsysclassnet_success)
  271 {
  272     linuxonly;
  273 
  274     noexit = 0;
  275     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  276     fake_sys_class_net("ethwrong", 10, 20, 30, 40, 50);
  277     fake_sys_class_net("ethunusual", 1, 2, 3, 4, 5);
  278 
  279     ck_assert_int_eq(readsysclassnet("ethunusual"), 1);
  280     ck_assert_str_eq(ifinfo.name, "ethunusual");
  281     ck_assert_int_eq(ifinfo.filled, 1);
  282     ck_assert_int_eq(ifinfo.rx, 1);
  283     ck_assert_int_eq(ifinfo.tx, 2);
  284     ck_assert_int_eq(ifinfo.rxp, 3);
  285     ck_assert_int_eq(ifinfo.txp, 4);
  286 }
  287 END_TEST
  288 
  289 START_TEST(getifinfo_not_found)
  290 {
  291     linuxonly;
  292 
  293     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  294     fake_proc_net_dev("w", "ethwrong", 10, 20, 30, 40);
  295 
  296     suppress_output();
  297 
  298     ck_assert_int_eq(getifinfo("ethunusual"), 0);
  299 }
  300 END_TEST
  301 
  302 START_TEST(getifinfo_success)
  303 {
  304     linuxonly;
  305 
  306     noexit = 0;
  307     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  308     fake_proc_net_dev("w", "ethwrong", 10, 20, 30, 40);
  309     fake_proc_net_dev("a", "ethunusual", 1, 2, 3, 4);
  310 
  311     suppress_output();
  312 
  313     ck_assert_int_eq(getifinfo("ethunusual"), 1);
  314     ck_assert_str_eq(ifinfo.name, "ethunusual");
  315     ck_assert_int_eq(ifinfo.filled, 1);
  316     ck_assert_int_eq(ifinfo.rx, 1);
  317     ck_assert_int_eq(ifinfo.tx, 2);
  318     ck_assert_int_eq(ifinfo.rxp, 3);
  319     ck_assert_int_eq(ifinfo.txp, 4);
  320 }
  321 END_TEST
  322 
  323 START_TEST(isifavailable_knows_interface_availability)
  324 {
  325     linuxonly;
  326 
  327     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  328     fake_proc_net_dev("w", "eth0", 10, 20, 30, 40);
  329     fake_proc_net_dev("a", "eth1", 1, 2, 3, 4);
  330 
  331     suppress_output();
  332 
  333     ck_assert_int_eq(isifavailable("eth0"), 1);
  334     ck_assert_int_eq(isifavailable("eth1"), 1);
  335     ck_assert_int_eq(isifavailable("eth2"), 0);
  336 }
  337 END_TEST
  338 
  339 START_TEST(istun_knows_what_a_tun_interface_looks_like)
  340 {
  341     linuxonly;
  342 
  343     ck_assert_int_eq(istun("eth0"), 0);
  344     ck_assert_int_eq(istun("not_tun0"), 0);
  345     ck_assert_int_eq(istun("tun"), 0);
  346     ck_assert_int_eq(istun("tuna"), 0);
  347     ck_assert_int_eq(istun("tun0"), 1);
  348     ck_assert_int_eq(istun("tun1"), 1);
  349     ck_assert_int_eq(istun("tun12"), 1);
  350     ck_assert_int_eq(istun("tun123"), 1);
  351     ck_assert_int_eq(istun("tun1234"), 1);
  352 }
  353 END_TEST
  354 
  355 void add_ifinfo_tests(Suite *s)
  356 {
  357     TCase *tc_ifinfo = tcase_create("Ifinfo");
  358     tcase_add_checked_fixture(tc_ifinfo, setup, teardown);
  359     tcase_add_unchecked_fixture(tc_ifinfo, setup, teardown);
  360     tcase_add_test(tc_ifinfo, getifliststring_no_source);
  361     tcase_add_test(tc_ifinfo, getifliststring_proc_one_interface);
  362     tcase_add_test(tc_ifinfo, getifliststring_proc_one_interface_with_speed);
  363     tcase_add_test(tc_ifinfo, getifliststring_proc_multiple_interfaces);
  364     tcase_add_test(tc_ifinfo, getifliststring_proc_multiple_interfaces_validating);
  365     tcase_add_test(tc_ifinfo, getifliststring_proc_long_interface_names);
  366     tcase_add_test(tc_ifinfo, getifliststring_sysclassnet_one_interface);
  367     tcase_add_test(tc_ifinfo, getifliststring_sysclassnet_one_interface_with_speed);
  368     tcase_add_test(tc_ifinfo, getifliststring_sysclassnet_multiple_interfaces);
  369     tcase_add_test(tc_ifinfo, getifliststring_sysclassnet_multiple_interfaces_validating);
  370     tcase_add_test(tc_ifinfo, getifliststring_sysclassnet_long_interface_names);
  371     tcase_add_test(tc_ifinfo, readproc_no_file);
  372     tcase_add_test(tc_ifinfo, readproc_not_found);
  373     tcase_add_test(tc_ifinfo, readproc_success);
  374     tcase_add_test(tc_ifinfo, readsysclassnet_not_found);
  375     tcase_add_test(tc_ifinfo, readsysclassnet_success);
  376     tcase_add_test(tc_ifinfo, getifinfo_not_found);
  377     tcase_add_test(tc_ifinfo, getifinfo_success);
  378     tcase_add_test(tc_ifinfo, isifavailable_knows_interface_availability);
  379     tcase_add_test(tc_ifinfo, istun_knows_what_a_tun_interface_looks_like);
  380     suite_add_tcase(s, tc_ifinfo);
  381 }