"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/tests/daemon_tests.c" (16 Aug 2021, 39680 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 "daemon_tests.c": 2.7_vs_2.8.

    1 #include "common.h"
    2 #include "vnstat_tests.h"
    3 #include "daemon_tests.h"
    4 #include "dbaccess.h"
    5 #include "datacache.h"
    6 #include "dbsql.h"
    7 #include "ifinfo.h"
    8 #include "cfg.h"
    9 #include "ibw.h"
   10 #include "fs.h"
   11 #include "daemon.h"
   12 
   13 START_TEST(debugtimestamp_does_not_exit)
   14 {
   15     suppress_output();
   16     debugtimestamp();
   17 }
   18 END_TEST
   19 
   20 
   21 START_TEST(initdstate_does_not_crash)
   22 {
   23     DSTATE s;
   24     initdstate(&s);
   25 }
   26 END_TEST
   27 
   28 START_TEST(addinterfaces_does_nothing_with_no_files)
   29 {
   30     DSTATE s;
   31 
   32     initdstate(&s);
   33     suppress_output();
   34     ck_assert_int_eq(remove_directory(TESTDIR), 1);
   35     ck_assert_int_eq(clean_testdbdir(), 1);
   36 
   37     ck_assert_int_eq(addinterfaces(&s), 0);
   38 }
   39 END_TEST
   40 
   41 START_TEST(addinterfaces_adds_interfaces)
   42 {
   43     int ret;
   44     DSTATE s;
   45 
   46     initdstate(&s);
   47     suppress_output();
   48     ck_assert_int_eq(remove_directory(TESTDIR), 1);
   49     ck_assert_int_eq(clean_testdbdir(), 1);
   50     fake_proc_net_dev("w", "ethone", 1, 2, 3, 4);
   51     fake_proc_net_dev("a", "lo0", 0, 0, 0, 0);
   52     fake_proc_net_dev("a", "ethtwo", 5, 6, 7, 8);
   53     fake_proc_net_dev("a", "sit0", 0, 0, 0, 0);
   54     ret = db_open_rw(1);
   55     ck_assert_int_eq(ret, 1);
   56 
   57     ret = (int)addinterfaces(&s);
   58     ck_assert_int_eq(ret, 2);
   59 
   60     ck_assert_int_eq(db_getinterfacecountbyname("ethone"), 1);
   61     ck_assert_int_eq(db_getinterfacecountbyname("ethtwo"), 1);
   62 
   63     ck_assert_int_eq(check_dbfile_exists("ethone", sizeof(DATA)), 0);
   64     ck_assert_int_eq(check_dbfile_exists(".ethone", sizeof(DATA)), 0);
   65     ck_assert_int_eq(check_dbfile_exists("ethtwo", sizeof(DATA)), 0);
   66     ck_assert_int_eq(check_dbfile_exists(".ethtwo", sizeof(DATA)), 0);
   67 
   68     ret = db_close();
   69     ck_assert_int_eq(ret, 1);
   70 }
   71 END_TEST
   72 
   73 START_TEST(addinterfaces_adds_only_new_interfaces)
   74 {
   75     int ret;
   76     DSTATE s;
   77 
   78     initdstate(&s);
   79     suppress_output();
   80     ck_assert_int_eq(remove_directory(TESTDIR), 1);
   81     ck_assert_int_eq(clean_testdbdir(), 1);
   82     fake_proc_net_dev("w", "ethone", 1, 2, 3, 4);
   83     fake_proc_net_dev("a", "lo0", 0, 0, 0, 0);
   84     fake_proc_net_dev("a", "ethtwo", 5, 6, 7, 8);
   85     fake_proc_net_dev("a", "sit0", 0, 0, 0, 0);
   86     ret = db_open_rw(1);
   87     ck_assert_int_eq(ret, 1);
   88 
   89     ret = (int)addinterfaces(&s);
   90     ck_assert_int_eq(ret, 2);
   91 
   92     ck_assert_int_eq(db_getinterfacecountbyname("ethone"), 1);
   93     ck_assert_int_eq(db_getinterfacecountbyname("ethtwo"), 1);
   94     ck_assert_int_eq(db_getinterfacecountbyname("eththree"), 0);
   95     ck_assert_int_eq(db_getinterfacecountbyname("lo0"), 0);
   96     ck_assert_int_eq(db_getinterfacecountbyname("sit0"), 0);
   97 
   98     /* legacy database files should not get created */
   99     ck_assert_int_eq(check_dbfile_exists("ethone", sizeof(DATA)), 0);
  100     ck_assert_int_eq(check_dbfile_exists(".ethone", sizeof(DATA)), 0);
  101     ck_assert_int_eq(check_dbfile_exists("ethtwo", sizeof(DATA)), 0);
  102     ck_assert_int_eq(check_dbfile_exists(".ethtwo", sizeof(DATA)), 0);
  103     ck_assert_int_eq(check_dbfile_exists("eththree", sizeof(DATA)), 0);
  104     ck_assert_int_eq(check_dbfile_exists(".eththree", sizeof(DATA)), 0);
  105 
  106     fake_proc_net_dev("a", "eththree", 9, 10, 11, 12);
  107 
  108     ret = (int)addinterfaces(&s);
  109     ck_assert_int_eq(ret, 1);
  110 
  111     ck_assert_int_eq(db_getinterfacecountbyname("ethone"), 1);
  112     ck_assert_int_eq(db_getinterfacecountbyname("ethtwo"), 1);
  113     ck_assert_int_eq(db_getinterfacecountbyname("eththree"), 1);
  114     ck_assert_int_eq(db_getinterfacecountbyname("lo0"), 0);
  115     ck_assert_int_eq(db_getinterfacecountbyname("sit0"), 0);
  116 
  117     /* legacy database files should still not get created */
  118     ck_assert_int_eq(check_dbfile_exists("ethone", sizeof(DATA)), 0);
  119     ck_assert_int_eq(check_dbfile_exists(".ethone", sizeof(DATA)), 0);
  120     ck_assert_int_eq(check_dbfile_exists("ethtwo", sizeof(DATA)), 0);
  121     ck_assert_int_eq(check_dbfile_exists(".ethtwo", sizeof(DATA)), 0);
  122     ck_assert_int_eq(check_dbfile_exists("eththree", sizeof(DATA)), 0);
  123     ck_assert_int_eq(check_dbfile_exists(".eththree", sizeof(DATA)), 0);
  124     ck_assert_int_eq(datacache_count(&s.dcache), 0);
  125 
  126     ret = db_close();
  127     ck_assert_int_eq(ret, 1);
  128 }
  129 END_TEST
  130 
  131 START_TEST(addinterfaces_adds_to_cache_when_running)
  132 {
  133     int ret;
  134     DSTATE s;
  135 
  136     initdstate(&s);
  137     suppress_output();
  138     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  139     ck_assert_int_eq(clean_testdbdir(), 1);
  140     fake_proc_net_dev("w", "ethone", 1, 2, 3, 4);
  141     fake_proc_net_dev("a", "ethtwo", 5, 6, 7, 8);
  142     ret = db_open_rw(1);
  143     ck_assert_int_eq(ret, 1);
  144 
  145     ck_assert_int_eq(datacache_count(&s.dcache), 0);
  146 
  147     s.running = 1;
  148     ret = (int)addinterfaces(&s);
  149     ck_assert_int_eq(ret, 2);
  150     ck_assert_int_eq(datacache_count(&s.dcache), 2);
  151 
  152     ck_assert_int_eq(db_getinterfacecountbyname("ethone"), 1);
  153     ck_assert_int_eq(db_getinterfacecountbyname("ethtwo"), 1);
  154     ck_assert_int_eq(db_getinterfacecountbyname("eththree"), 0);
  155     ck_assert_int_eq(db_getinterfacecountbyname("lo0"), 0);
  156     ck_assert_int_eq(db_getinterfacecountbyname("sit0"), 0);
  157 
  158     ck_assert_int_eq(check_dbfile_exists("ethone", sizeof(DATA)), 0);
  159     ck_assert_int_eq(check_dbfile_exists(".ethone", sizeof(DATA)), 0);
  160     ck_assert_int_eq(check_dbfile_exists("ethtwo", sizeof(DATA)), 0);
  161     ck_assert_int_eq(check_dbfile_exists(".ethtwo", sizeof(DATA)), 0);
  162     ck_assert_int_eq(check_dbfile_exists("eththree", sizeof(DATA)), 0);
  163     ck_assert_int_eq(check_dbfile_exists(".eththree", sizeof(DATA)), 0);
  164 
  165     fake_proc_net_dev("a", "eththree", 9, 10, 11, 12);
  166 
  167     ret = (int)addinterfaces(&s);
  168     ck_assert_int_eq(ret, 1);
  169     ck_assert_int_eq(datacache_count(&s.dcache), 3);
  170 
  171     ck_assert_int_eq(db_getinterfacecountbyname("ethone"), 1);
  172     ck_assert_int_eq(db_getinterfacecountbyname("ethtwo"), 1);
  173     ck_assert_int_eq(db_getinterfacecountbyname("eththree"), 1);
  174     ck_assert_int_eq(db_getinterfacecountbyname("lo0"), 0);
  175     ck_assert_int_eq(db_getinterfacecountbyname("sit0"), 0);
  176 
  177     ck_assert_int_eq(check_dbfile_exists("ethone", sizeof(DATA)), 0);
  178     ck_assert_int_eq(check_dbfile_exists(".ethone", sizeof(DATA)), 0);
  179     ck_assert_int_eq(check_dbfile_exists("ethtwo", sizeof(DATA)), 0);
  180     ck_assert_int_eq(check_dbfile_exists(".ethtwo", sizeof(DATA)), 0);
  181     ck_assert_int_eq(check_dbfile_exists("eththree", sizeof(DATA)), 0);
  182     ck_assert_int_eq(check_dbfile_exists(".eththree", sizeof(DATA)), 0);
  183 
  184     ret = db_close();
  185     ck_assert_int_eq(ret, 1);
  186 
  187     datacache_clear(&s.dcache);
  188 }
  189 END_TEST
  190 
  191 START_TEST(preparedatabase_exits_with_no_database_dir)
  192 {
  193     DSTATE s;
  194 
  195     initdstate(&s);
  196     suppress_output();
  197     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  198 
  199     preparedatabase(&s);
  200 }
  201 END_TEST
  202 
  203 START_TEST(preparedatabase_exits_with_no_database)
  204 {
  205     DSTATE s;
  206 
  207     initdstate(&s);
  208     suppress_output();
  209     strncpy_nt(cfg.dbdir, TESTDBDIR, 512);
  210     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  211     ck_assert_int_eq(clean_testdbdir(), 1);
  212 
  213     preparedatabase(&s);
  214 }
  215 END_TEST
  216 
  217 START_TEST(preparedatabase_exits_with_no_database_and_noadd)
  218 {
  219     DSTATE s;
  220 
  221     initdstate(&s);
  222     s.noadd = 1;
  223     suppress_output();
  224     strncpy_nt(cfg.dbdir, TESTDBDIR, 512);
  225     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  226     ck_assert_int_eq(clean_testdbdir(), 1);
  227 
  228     preparedatabase(&s);
  229 }
  230 END_TEST
  231 
  232 START_TEST(preparedatabase_with_no_database_creates_database)
  233 {
  234     int ret;
  235     DSTATE s;
  236 
  237     initdstate(&s);
  238     suppress_output();
  239     strncpy_nt(cfg.dbdir, TESTDBDIR, 512);
  240     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  241     ck_assert_int_eq(clean_testdbdir(), 1);
  242     fake_proc_net_dev("w", "ethone", 1, 2, 3, 4);
  243     fake_proc_net_dev("a", "lo0", 0, 0, 0, 0);
  244     fake_proc_net_dev("a", "ethtwo", 5, 6, 7, 8);
  245     fake_proc_net_dev("a", "sit0", 0, 0, 0, 0);
  246     ret = db_open_rw(1);
  247     ck_assert_int_eq(ret, 1);
  248 
  249     preparedatabase(&s);
  250 
  251     ck_assert_int_eq(db_getinterfacecountbyname("ethone"), 1);
  252     ck_assert_int_eq(db_getinterfacecountbyname("ethtwo"), 1);
  253 
  254     ck_assert_int_eq(check_dbfile_exists("ethone", sizeof(DATA)), 0);
  255     ck_assert_int_eq(check_dbfile_exists(".ethone", sizeof(DATA)), 0);
  256     ck_assert_int_eq(check_dbfile_exists("ethtwo", sizeof(DATA)), 0);
  257     ck_assert_int_eq(check_dbfile_exists(".ethtwo", sizeof(DATA)), 0);
  258 
  259     ret = db_close();
  260     ck_assert_int_eq(ret, 1);
  261 }
  262 END_TEST
  263 
  264 START_TEST(setsignaltraps_does_not_exit)
  265 {
  266     intsignal = 1;
  267     setsignaltraps();
  268     ck_assert_int_eq(intsignal, 0);
  269 }
  270 END_TEST
  271 
  272 START_TEST(filldatabaselist_exits_with_no_database_dir)
  273 {
  274     DSTATE s;
  275     initdstate(&s);
  276     disable_logprints();
  277     strncpy_nt(cfg.dbdir, TESTDBDIR, 512);
  278     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  279 
  280     filldatabaselist(&s);
  281 }
  282 END_TEST
  283 
  284 START_TEST(filldatabaselist_does_not_exit_with_empty_database_dir)
  285 {
  286     int ret;
  287     DSTATE s;
  288     initdstate(&s);
  289     disable_logprints();
  290     s.sync = 1;
  291     ret = db_open_rw(1);
  292     ck_assert_int_eq(ret, 1);
  293 
  294     filldatabaselist(&s);
  295 
  296     ck_assert_int_eq(s.dbifcount, 0);
  297     ck_assert_int_eq(s.sync, 0);
  298     ck_assert_int_eq(s.updateinterval, 120);
  299     ret = db_close();
  300     ck_assert_int_eq(ret, 1);
  301 }
  302 END_TEST
  303 
  304 START_TEST(filldatabaselist_adds_databases)
  305 {
  306     int ret;
  307     DSTATE s;
  308     initdstate(&s);
  309     disable_logprints();
  310     strncpy_nt(cfg.dbdir, TESTDBDIR, 512);
  311     strncpy_nt(cfg.dbdir, TESTDBDIR, 512);
  312     s.sync = 1;
  313     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  314     ck_assert_int_eq(clean_testdbdir(), 1);
  315     ck_assert_int_eq(create_zerosize_dbfile("name1"), 1);
  316     ck_assert_int_eq(create_zerosize_dbfile("name2"), 1);
  317     ck_assert_int_eq(check_dbfile_exists("name1", 0), 1);
  318     ck_assert_int_eq(check_dbfile_exists(".name1", 0), 0);
  319     ck_assert_int_eq(check_dbfile_exists("name2", 0), 1);
  320     ck_assert_int_eq(check_dbfile_exists(".name2", 0), 0);
  321     ret = db_open_rw(1);
  322     ck_assert_int_eq(ret, 1);
  323     ret = db_addinterface("eth0");
  324     ck_assert_int_eq(ret, 1);
  325     ret = db_addinterface("eth1");
  326     ck_assert_int_eq(ret, 1);
  327 
  328     filldatabaselist(&s);
  329 
  330     /* filldatabaselist() doesn't import legacy dbs */
  331     ck_assert_int_eq(datacache_count(&s.dcache), 2);
  332     ck_assert_int_eq(datacache_activecount(&s.dcache), 2);
  333     ck_assert_int_eq(check_dbfile_exists("name1", 0), 1);
  334     ck_assert_int_eq(check_dbfile_exists(".name1", 0), 0);
  335     ck_assert_int_eq(check_dbfile_exists("name2", 0), 1);
  336     ck_assert_int_eq(check_dbfile_exists(".name2", 0), 0);
  337     ck_assert_int_eq(s.dbifcount, 2);
  338     ck_assert_int_eq(s.sync, 0);
  339     ck_assert_int_eq(s.updateinterval, 0);
  340     ck_assert_int_eq(intsignal, 42);
  341     ret = db_close();
  342     ck_assert_int_eq(ret, 1);
  343 
  344     datacache_clear(&s.dcache);
  345 }
  346 END_TEST
  347 
  348 START_TEST(adjustsaveinterval_with_empty_cache)
  349 {
  350     DSTATE s;
  351     initdstate(&s);
  352     s.saveinterval = 0;
  353     ck_assert_int_eq(datacache_activecount(&s.dcache), 0);
  354 
  355     adjustsaveinterval(&s);
  356 
  357     ck_assert_int_eq(s.saveinterval, cfg.offsaveinterval * 60);
  358 }
  359 END_TEST
  360 
  361 START_TEST(adjustsaveinterval_with_filled_cache)
  362 {
  363     int ret;
  364     DSTATE s;
  365     initdstate(&s);
  366     s.saveinterval = 0;
  367 
  368     ret = datacache_add(&s.dcache, "name1", 0);
  369     ck_assert_int_eq(ret, 1);
  370     ck_assert_int_eq(datacache_activecount(&s.dcache), 1);
  371 
  372     adjustsaveinterval(&s);
  373 
  374     ck_assert_int_eq(s.saveinterval, cfg.saveinterval * 60);
  375 
  376     datacache_clear(&s.dcache);
  377 }
  378 END_TEST
  379 
  380 START_TEST(checkdbsaveneed_has_no_need)
  381 {
  382     DSTATE s;
  383     initdstate(&s);
  384     s.dodbsave = 2;
  385     s.current = 10;
  386     s.prevdbsave = 0;
  387     s.saveinterval = 30;
  388     s.forcesave = 0;
  389 
  390     checkdbsaveneed(&s);
  391 
  392     ck_assert_int_eq(s.dodbsave, 0);
  393     ck_assert_int_ne(s.prevdbsave, s.current);
  394 }
  395 END_TEST
  396 
  397 START_TEST(checkdbsaveneed_is_forced)
  398 {
  399     DSTATE s;
  400     initdstate(&s);
  401     s.dodbsave = 2;
  402     s.current = 125;
  403     s.prevdbsave = 110;
  404     s.saveinterval = 30;
  405     s.forcesave = 1;
  406 
  407     checkdbsaveneed(&s);
  408 
  409     ck_assert_int_eq(s.dodbsave, 1);
  410     ck_assert_int_eq(s.prevdbsave, 120);
  411     ck_assert_int_eq(s.forcesave, 0);
  412 }
  413 END_TEST
  414 
  415 START_TEST(checkdbsaveneed_needs)
  416 {
  417     DSTATE s;
  418     initdstate(&s);
  419     s.dodbsave = 2;
  420     s.current = 65;
  421     s.prevdbsave = 5;
  422     s.saveinterval = 30;
  423     s.forcesave = 0;
  424 
  425     checkdbsaveneed(&s);
  426 
  427     ck_assert_int_eq(s.dodbsave, 1);
  428     ck_assert_int_eq(s.prevdbsave, 60);
  429     ck_assert_int_eq(s.forcesave, 0);
  430 }
  431 END_TEST
  432 
  433 START_TEST(processdatacache_empty_does_nothing)
  434 {
  435     DSTATE s;
  436     initdstate(&s);
  437 
  438     processdatacache(&s);
  439 }
  440 END_TEST
  441 
  442 START_TEST(processdatacache_can_process_things)
  443 {
  444     int ret;
  445     DSTATE s;
  446     initdstate(&s);
  447     suppress_output();
  448 
  449     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  450     debug = 1;
  451 
  452     s.iflisthash = 42;
  453     s.bootdetected = 1;
  454     s.dodbsave = 1;
  455     s.cleanuphour = getcurrenthour() + 1;
  456     s.dbifcount = 3;
  457 
  458     ck_assert_int_eq(datacache_count(&s.dcache), 0);
  459     ret = datacache_add(&s.dcache, "ethnotindb", 0);
  460     ck_assert_int_eq(ret, 1);
  461     ret = datacache_add(&s.dcache, "ethonlyindb", 0);
  462     ck_assert_int_eq(ret, 1);
  463     ret = datacache_add(&s.dcache, "ethexisting", 0);
  464     ck_assert_int_eq(ret, 1);
  465     ck_assert_int_eq(datacache_count(&s.dcache), 3);
  466 
  467     ret = db_open_rw(1);
  468     ck_assert_int_eq(ret, 1);
  469     ret = db_addinterface("ethonlyindb");
  470     ck_assert_int_eq(ret, 1);
  471     ret = db_addinterface("ethexisting");
  472     ck_assert_int_eq(ret, 1);
  473 
  474     fake_proc_net_dev("w", "ethexisting", 10, 20, 30, 40);
  475 
  476     processdatacache(&s);
  477 
  478     ck_assert_int_eq(s.iflisthash, 0);
  479     ck_assert_int_eq(s.bootdetected, 0);
  480     ck_assert_int_eq(s.dodbsave, 0);
  481     ck_assert_int_eq(datacache_count(&s.dcache), 1);
  482 
  483     ret = db_close();
  484     ck_assert_int_eq(ret, 1);
  485 
  486     datacache_clear(&s.dcache);
  487 }
  488 END_TEST
  489 
  490 START_TEST(handleintsignals_handles_no_signal)
  491 {
  492     DSTATE s;
  493     initdstate(&s);
  494     s.running = 1;
  495     s.dbifcount = 1;
  496 
  497     intsignal = 0;
  498     handleintsignals(&s);
  499     ck_assert_int_eq(intsignal, 0);
  500     ck_assert_int_eq(s.running, 1);
  501     ck_assert_int_eq(s.dbifcount, 1);
  502 }
  503 END_TEST
  504 
  505 START_TEST(handleintsignals_handles_42)
  506 {
  507     DSTATE s;
  508     initdstate(&s);
  509     s.running = 1;
  510     s.dbifcount = 1;
  511 
  512     intsignal = 42;
  513     handleintsignals(&s);
  514     ck_assert_int_eq(intsignal, 0);
  515     ck_assert_int_eq(s.running, 1);
  516     ck_assert_int_eq(s.dbifcount, 1);
  517 }
  518 END_TEST
  519 
  520 START_TEST(handleintsignals_handles_unknown_signal)
  521 {
  522     DSTATE s;
  523     initdstate(&s);
  524     s.running = 1;
  525     s.dbifcount = 1;
  526 
  527     disable_logprints();
  528 
  529     intsignal = 43;
  530     handleintsignals(&s);
  531     ck_assert_int_eq(intsignal, 0);
  532     ck_assert_int_eq(s.running, 1);
  533     ck_assert_int_eq(s.dbifcount, 1);
  534 }
  535 END_TEST
  536 
  537 START_TEST(handleintsignals_handles_sigterm)
  538 {
  539     DSTATE s;
  540     initdstate(&s);
  541     s.running = 1;
  542     s.dbifcount = 1;
  543 
  544     disable_logprints();
  545 
  546     intsignal = SIGTERM;
  547     handleintsignals(&s);
  548     ck_assert_int_eq(intsignal, 0);
  549     ck_assert_int_eq(s.running, 0);
  550     ck_assert_int_eq(s.dbifcount, 1);
  551 }
  552 END_TEST
  553 
  554 START_TEST(handleintsignals_handles_sigint)
  555 {
  556     DSTATE s;
  557     initdstate(&s);
  558     s.running = 1;
  559     s.dbifcount = 1;
  560 
  561     disable_logprints();
  562 
  563     s.running = 1;
  564     intsignal = SIGINT;
  565     handleintsignals(&s);
  566     ck_assert_int_eq(intsignal, 0);
  567     ck_assert_int_eq(s.running, 0);
  568     ck_assert_int_eq(s.dbifcount, 1);
  569 }
  570 END_TEST
  571 
  572 START_TEST(handleintsignals_handles_sighup)
  573 {
  574     int ret;
  575     DSTATE s;
  576     initdstate(&s);
  577     s.running = 1;
  578     s.dbifcount = 1;
  579 
  580     disable_logprints();
  581 
  582     ret = db_open_rw(1);
  583     ck_assert_int_eq(ret, 1);
  584     ret = db_addinterface("eth0");
  585     ck_assert_int_eq(ret, 1);
  586     filldatabaselist(&s);
  587 
  588     disable_logprints();
  589 
  590     s.running = 1;
  591     intsignal = SIGHUP;
  592     strncpy_nt(cfg.dbdir, TESTDBDIR, 512);
  593     handleintsignals(&s);
  594     ck_assert_int_eq(intsignal, 0);
  595     ck_assert_int_eq(s.running, 1);
  596     ck_assert_int_eq(s.dbifcount, 0);
  597 
  598     ret = db_close();
  599     ck_assert_int_eq(ret, 1);
  600 }
  601 END_TEST
  602 
  603 START_TEST(preparedirs_with_no_dir)
  604 {
  605     char logdir[500], piddir[500];
  606 
  607     DSTATE s;
  608     initdstate(&s);
  609     cfg.uselogging = 1;
  610     s.rundaemon = 1;
  611     strncpy_nt(cfg.dbdir, TESTDBDIR, 512);
  612     snprintf(logdir, 500, "%s/log/vnstat", TESTDIR);
  613     snprintf(piddir, 500, "%s/pid/vnstat", TESTDIR);
  614     snprintf(cfg.logfile, 512, "%s/vnstat.log", logdir);
  615     snprintf(cfg.pidfile, 512, "%s/vnstat.pid", piddir);
  616 
  617     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  618     ck_assert_int_eq(direxists(TESTDBDIR), 0);
  619     ck_assert_int_eq(direxists(logdir), 0);
  620     ck_assert_int_eq(direxists(piddir), 0);
  621     preparedirs(&s);
  622     ck_assert_int_eq(direxists(TESTDBDIR), 1);
  623     ck_assert_int_eq(direxists(logdir), 1);
  624     ck_assert_int_eq(direxists(piddir), 1);
  625 }
  626 END_TEST
  627 
  628 START_TEST(preparedirs_with_dir)
  629 {
  630     char logdir[500], piddir[500];
  631 
  632     DSTATE s;
  633     initdstate(&s);
  634     cfg.uselogging = 1;
  635     s.rundaemon = 1;
  636     strncpy_nt(cfg.dbdir, TESTDBDIR, 512);
  637     snprintf(logdir, 500, "%s/log/vnstat", TESTDIR);
  638     snprintf(piddir, 500, "%s/pid/vnstat", TESTDIR);
  639     snprintf(cfg.logfile, 512, "%s/vnstat.log", logdir);
  640     snprintf(cfg.pidfile, 512, "%s/vnstat.pid", piddir);
  641 
  642     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  643     ck_assert_int_eq(direxists(TESTDBDIR), 0);
  644     ck_assert_int_eq(mkpath(TESTDBDIR, 0775), 1);
  645     ck_assert_int_eq(direxists(TESTDBDIR), 1);
  646     ck_assert_int_eq(direxists(logdir), 0);
  647     ck_assert_int_eq(direxists(piddir), 0);
  648     preparedirs(&s);
  649     ck_assert_int_eq(direxists(TESTDBDIR), 1);
  650     ck_assert_int_eq(direxists(logdir), 1);
  651     ck_assert_int_eq(direxists(piddir), 1);
  652 }
  653 END_TEST
  654 
  655 START_TEST(interfacechangecheck_with_no_interfaces)
  656 {
  657     DSTATE s;
  658 
  659     initdstate(&s);
  660     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  661     interfacechangecheck(&s);
  662     ck_assert_int_eq(s.iflisthash, 0);
  663     ck_assert_int_eq(s.forcesave, 0);
  664 }
  665 END_TEST
  666 
  667 START_TEST(interfacechangecheck_with_empty_cache)
  668 {
  669     DSTATE s;
  670 
  671     initdstate(&s);
  672     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  673     fake_proc_net_dev("w", "ethsomething", 1, 2, 3, 4);
  674     fake_proc_net_dev("a", "ethelse", 5, 6, 7, 8);
  675 
  676     interfacechangecheck(&s);
  677     ck_assert_int_ne(s.iflisthash, 0);
  678     ck_assert_int_eq(s.forcesave, 0);
  679 }
  680 END_TEST
  681 
  682 START_TEST(interfacechangecheck_with_no_changes_in_iflist)
  683 {
  684     DSTATE s;
  685     uint32_t ifhash;
  686     char *ifacelist;
  687 
  688     initdstate(&s);
  689     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  690     fake_proc_net_dev("w", "ethsomething", 1, 2, 3, 4);
  691     fake_proc_net_dev("a", "ethelse", 5, 6, 7, 8);
  692     ck_assert_int_ne(getifliststring(&ifacelist, 0), 0);
  693     ifhash = simplehash(ifacelist, (int)strlen(ifacelist));
  694     s.iflisthash = ifhash;
  695 
  696     interfacechangecheck(&s);
  697     ck_assert_int_eq(s.iflisthash, ifhash);
  698     ck_assert_int_eq(s.forcesave, 0);
  699 
  700         free(ifacelist);
  701 }
  702 END_TEST
  703 
  704 START_TEST(interfacechangecheck_with_filled_cache)
  705 {
  706     int ret;
  707     DSTATE s;
  708     datacache *iterator;
  709 
  710     initdstate(&s);
  711     disable_logprints();
  712     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  713     s.iflisthash = 123;
  714 
  715     ck_assert_int_eq(datacache_count(&s.dcache), 0);
  716     ret = datacache_add(&s.dcache, "ethbasic", 0);
  717     ck_assert_int_eq(ret, 1);
  718     ret = datacache_add(&s.dcache, "ethactive", 0);
  719     ck_assert_int_eq(ret, 1);
  720 
  721     /* cache data needs to appear filled during this test */
  722     iterator = s.dcache;
  723     while (iterator != NULL) {
  724         iterator->filled = 1;
  725         iterator = iterator->next;
  726     }
  727 
  728     ck_assert_int_eq(datacache_count(&s.dcache), 2);
  729     ck_assert_int_eq(datacache_activecount(&s.dcache), 2);
  730 
  731     fake_proc_net_dev("w", "ethbasic", 1, 2, 3, 4);
  732 
  733     interfacechangecheck(&s);
  734     ck_assert_int_ne(s.iflisthash, 0);
  735     ck_assert_int_eq(s.forcesave, 1);
  736     ck_assert_int_eq(datacache_count(&s.dcache), 2);
  737     ck_assert_int_eq(datacache_activecount(&s.dcache), 1);
  738 
  739     datacache_clear(&s.dcache);
  740 }
  741 END_TEST
  742 
  743 START_TEST(simplehash_with_empty_strings)
  744 {
  745     ck_assert_int_eq(simplehash(NULL, 10), 0);
  746     ck_assert_int_eq(simplehash("empty", 0), 0);
  747 }
  748 END_TEST
  749 
  750 START_TEST(simplehash_with_simple_strings)
  751 {
  752     ck_assert_int_eq(simplehash("0", 1), 49);
  753     ck_assert_int_eq(simplehash("1", 1), 50);
  754     ck_assert_int_eq(simplehash("12", 2), 101);
  755 }
  756 END_TEST
  757 
  758 START_TEST(initcachevalues_does_not_init_without_database)
  759 {
  760     int ret;
  761     DSTATE s;
  762 
  763     initdstate(&s);
  764     disable_logprints();
  765 
  766     ret = datacache_add(&s.dcache, "eth0", 0);
  767     ck_assert_int_eq(ret, 1);
  768 
  769     ret = initcachevalues(&s, &s.dcache);
  770     ck_assert_int_eq(ret, 0);
  771 
  772     datacache_clear(&s.dcache);
  773 }
  774 END_TEST
  775 
  776 START_TEST(initcachevalues_does_init)
  777 {
  778     int ret;
  779     DSTATE s;
  780 
  781     initdstate(&s);
  782     disable_logprints();
  783     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  784     ck_assert_int_eq(clean_testdbdir(), 1);
  785     strncpy_nt(cfg.dbdir, TESTDBDIR, 512);
  786     ret = db_open_rw(1);
  787     ck_assert_int_eq(ret, 1);
  788 
  789     ret = db_addinterface("eth0");
  790     ck_assert_int_eq(ret, 1);
  791 
  792     ret = db_setcounters("eth0", 1, 2);
  793     ck_assert_int_eq(ret, 1);
  794 
  795     ret = datacache_add(&s.dcache, "eth0", 0);
  796     ck_assert_int_eq(ret, 1);
  797 
  798     ret = initcachevalues(&s, &s.dcache);
  799     ck_assert_int_eq(ret, 1);
  800 
  801     ck_assert_int_eq(s.dcache->currx, 1);
  802     ck_assert_int_eq(s.dcache->curtx, 2);
  803     ck_assert_int_ne(s.dcache->updated, 0);
  804 
  805     ret = db_close();
  806     ck_assert_int_eq(ret, 1);
  807 
  808     datacache_clear(&s.dcache);
  809 }
  810 END_TEST
  811 
  812 START_TEST(getcurrenthour_returns_something_realistic)
  813 {
  814     int ret;
  815 
  816     ret = getcurrenthour();
  817     ck_assert_int_ge(ret, 0);
  818     ck_assert_int_le(ret, 23);
  819 }
  820 END_TEST
  821 
  822 START_TEST(waittimesync_does_not_wait_unless_configured_to_do_so)
  823 {
  824     int ret;
  825     DSTATE s;
  826 
  827     initdstate(&s);
  828     disable_logprints();
  829     cfg.timesyncwait = 0;
  830 
  831     ret = waittimesync(&s);
  832     ck_assert_int_eq(ret, 0);
  833 }
  834 END_TEST
  835 
  836 START_TEST(waittimesync_does_not_wait_with_no_interfaces)
  837 {
  838     int ret;
  839     DSTATE s;
  840 
  841     initdstate(&s);
  842     suppress_output();
  843     debug = 1;
  844 
  845     ret = waittimesync(&s);
  846     ck_assert_int_eq(ret, 0);
  847     ck_assert_int_eq(s.prevdbsave, 0);
  848 }
  849 END_TEST
  850 
  851 START_TEST(waittimesync_does_not_wait_with_new_interfaces)
  852 {
  853     int ret;
  854     DSTATE s;
  855 
  856     initdstate(&s);
  857     suppress_output();
  858     debug = 1;
  859     cfg.timesyncwait = 60;
  860 
  861     ret = db_open_rw(1);
  862     ck_assert_int_eq(ret, 1);
  863     ret = db_addinterface("eth0");
  864     ck_assert_int_eq(ret, 1);
  865     /* 'updated' needs to be slightly adjusted in order to be sure to never trigger an error */
  866     ret = db_exec("update interface set updated=datetime('now', '-2 seconds', 'localtime') where id=1;");
  867     ck_assert_int_eq(ret, 1);
  868 
  869     filldatabaselist(&s);
  870     s.prevdbsave = 0;
  871 
  872     ret = waittimesync(&s);
  873     ck_assert_int_eq(ret, 0);
  874     ck_assert_int_ne(s.prevdbsave, 0);
  875 
  876     ret = db_close();
  877     ck_assert_int_eq(ret, 1);
  878 
  879     datacache_clear(&s.dcache);
  880 }
  881 END_TEST
  882 
  883 START_TEST(waittimesync_knows_when_to_wait)
  884 {
  885     int ret;
  886     DSTATE s;
  887 
  888     initdstate(&s);
  889     suppress_output();
  890     debug = 1;
  891     cfg.timesyncwait = 60;
  892 
  893     ret = db_open_rw(1);
  894     ck_assert_int_eq(ret, 1);
  895     ret = db_addinterface("eth0");
  896     ck_assert_int_eq(ret, 1);
  897     ret = db_exec("update interface set updated=datetime('now', '+7 days', 'localtime') where id=1;");
  898     ck_assert_int_eq(ret, 1);
  899 
  900     filldatabaselist(&s);
  901     s.prevdbsave = 0;
  902 
  903     ret = waittimesync(&s);
  904     ck_assert_int_eq(ret, 1);
  905 
  906     s.prevdbsave = time(NULL) - 100;
  907 
  908     ret = waittimesync(&s);
  909     ck_assert_int_eq(ret, 0);
  910 
  911     ret = db_close();
  912     ck_assert_int_eq(ret, 1);
  913 
  914     datacache_clear(&s.dcache);
  915 }
  916 END_TEST
  917 
  918 START_TEST(waittimesync_knows_when_to_give_up)
  919 {
  920     int ret;
  921     DSTATE s;
  922 
  923     initdstate(&s);
  924     suppress_output();
  925     debug = 1;
  926     cfg.timesyncwait = 60;
  927 
  928     ret = db_open_rw(1);
  929     ck_assert_int_eq(ret, 1);
  930     ret = db_addinterface("eth0");
  931     ck_assert_int_eq(ret, 1);
  932     ret = db_exec("update interface set updated=datetime('now', '+7 days', 'localtime') where id=1;");
  933     ck_assert_int_eq(ret, 1);
  934 
  935     filldatabaselist(&s);
  936     s.prevdbsave = 0;
  937 
  938     ret = waittimesync(&s);
  939     ck_assert_int_eq(ret, 1);
  940 
  941     s.prevdbupdate -= 5000;
  942 
  943     ret = waittimesync(&s);
  944     ck_assert_int_eq(ret, 0);
  945 
  946     ret = db_close();
  947     ck_assert_int_eq(ret, 1);
  948 
  949     datacache_clear(&s.dcache);
  950 }
  951 END_TEST
  952 
  953 START_TEST(detectboot_sets_btime_if_missing_from_database)
  954 {
  955     int ret;
  956     DSTATE s;
  957     char *buffer;
  958 
  959     initdstate(&s);
  960     ret = db_open_rw(1);
  961     ck_assert_int_eq(ret, 1);
  962 
  963     ret = db_exec("delete from info where name='btime';");
  964     ck_assert_int_eq(ret, 1);
  965 
  966     buffer = db_getinfo("btime");
  967     ck_assert_int_eq((int)strlen(buffer), 0);
  968 
  969     ck_assert_int_eq(s.bootdetected, 0);
  970 
  971     detectboot(&s);
  972 
  973     ck_assert_int_eq(s.bootdetected, 0);
  974 
  975     buffer = db_getinfo("btime");
  976     ck_assert_int_ne((int)strlen(buffer), 0);
  977 
  978     ret = db_close();
  979     ck_assert_int_eq(ret, 1);
  980 }
  981 END_TEST
  982 
  983 START_TEST(detectboot_sets_btime_for_new_database)
  984 {
  985     int ret;
  986     DSTATE s;
  987     char *buffer;
  988     char temp[64];
  989 
  990     initdstate(&s);
  991     ret = db_open_rw(1);
  992     ck_assert_int_eq(ret, 1);
  993 
  994     buffer = db_getinfo("btime");
  995     ck_assert_int_ne((int)strlen(buffer), 0);
  996 
  997     strncpy_nt(temp, buffer, 64);
  998     ck_assert_str_eq(buffer, temp);
  999 
 1000     ck_assert_int_eq(s.bootdetected, 0);
 1001 
 1002     detectboot(&s);
 1003 
 1004     ck_assert_int_eq(s.bootdetected, 0);
 1005 
 1006     buffer = db_getinfo("btime");
 1007     ck_assert_int_ne((int)strlen(buffer), 0);
 1008 
 1009     ck_assert_str_ne(buffer, temp);
 1010 
 1011     ret = db_close();
 1012     ck_assert_int_eq(ret, 1);
 1013 }
 1014 END_TEST
 1015 
 1016 START_TEST(detectboot_can_detect_boot)
 1017 {
 1018     int ret;
 1019     DSTATE s;
 1020     char *buffer;
 1021     char temp[64];
 1022 
 1023     initdstate(&s);
 1024     suppress_output();
 1025     debug = 1;
 1026     ret = db_open_rw(1);
 1027     ck_assert_int_eq(ret, 1);
 1028 
 1029     db_setinfo("btime", "1", 1);
 1030 
 1031     buffer = db_getinfo("btime");
 1032     ck_assert_int_ne((int)strlen(buffer), 0);
 1033 
 1034     strncpy_nt(temp, buffer, 64);
 1035     ck_assert_str_eq(buffer, temp);
 1036 
 1037     ck_assert_int_eq(s.bootdetected, 0);
 1038 
 1039     detectboot(&s);
 1040 
 1041     ck_assert_int_eq(s.bootdetected, 1);
 1042 
 1043     buffer = db_getinfo("btime");
 1044     ck_assert_int_ne((int)strlen(buffer), 0);
 1045 
 1046     ck_assert_str_ne(buffer, temp);
 1047 
 1048     ret = db_close();
 1049     ck_assert_int_eq(ret, 1);
 1050 }
 1051 END_TEST
 1052 
 1053 START_TEST(handledatabaseerror_exits_on_fatal_error)
 1054 {
 1055     DSTATE s;
 1056     initdstate(&s);
 1057     disable_logprints();
 1058 
 1059     db_errcode = SQLITE_ERROR;
 1060     handledatabaseerror(&s);
 1061 }
 1062 END_TEST
 1063 
 1064 START_TEST(handledatabaseerror_does_not_exit_if_limit_is_not_exceeded)
 1065 {
 1066     int i;
 1067     DSTATE s;
 1068     initdstate(&s);
 1069     disable_logprints();
 1070 
 1071     ck_assert_int_eq(s.dbretrycount, 0);
 1072 
 1073     db_errcode = SQLITE_BUSY;
 1074     handledatabaseerror(&s);
 1075 
 1076     ck_assert_int_eq(s.dbretrycount, 1);
 1077 
 1078     for (i = 1; i < DBRETRYLIMIT; i++) {
 1079         handledatabaseerror(&s);
 1080     }
 1081 
 1082     ck_assert_int_eq(s.dbretrycount, DBRETRYLIMIT);
 1083 }
 1084 END_TEST
 1085 
 1086 START_TEST(handledatabaseerror_exits_if_limit_is_exceeded)
 1087 {
 1088     int i;
 1089     DSTATE s;
 1090     initdstate(&s);
 1091     disable_logprints();
 1092 
 1093     ck_assert_int_eq(s.dbretrycount, 0);
 1094 
 1095     db_errcode = SQLITE_BUSY;
 1096     handledatabaseerror(&s);
 1097 
 1098     ck_assert_int_eq(s.dbretrycount, 1);
 1099 
 1100     for (i = 1; i < DBRETRYLIMIT + 1; i++) {
 1101         handledatabaseerror(&s);
 1102     }
 1103 }
 1104 END_TEST
 1105 
 1106 START_TEST(cleanremovedinterfaces_allows_interfaces_to_be_removed)
 1107 {
 1108     int ret;
 1109     DSTATE s;
 1110     initdstate(&s);
 1111     disable_logprints();
 1112 
 1113     ck_assert_int_eq(datacache_count(&s.dcache), 0);
 1114     ret = datacache_add(&s.dcache, "ethnotindb1", 0);
 1115     ck_assert_int_eq(ret, 1);
 1116     ret = datacache_add(&s.dcache, "ethindb1", 0);
 1117     ck_assert_int_eq(ret, 1);
 1118     ret = datacache_add(&s.dcache, "ethnotindb2", 0);
 1119     ck_assert_int_eq(ret, 1);
 1120     ret = datacache_add(&s.dcache, "ethindb2", 0);
 1121     ck_assert_int_eq(ret, 1);
 1122     ret = datacache_add(&s.dcache, "ethindb3", 0);
 1123     ck_assert_int_eq(ret, 1);
 1124     ret = datacache_add(&s.dcache, "ethnotindb3", 0);
 1125     ck_assert_int_eq(ret, 1);
 1126     ret = datacache_add(&s.dcache, "ethnotindb4", 0);
 1127     ck_assert_int_eq(ret, 1);
 1128     ck_assert_int_eq(datacache_count(&s.dcache), 7);
 1129     s.dbifcount = 7;
 1130 
 1131     ret = db_open_rw(1);
 1132     ck_assert_int_eq(ret, 1);
 1133     ret = db_addinterface("ethindb1");
 1134     ck_assert_int_eq(ret, 1);
 1135     ret = db_addinterface("ethindb2");
 1136     ck_assert_int_eq(ret, 1);
 1137     ret = db_addinterface("ethindb3");
 1138     ck_assert_int_eq(ret, 1);
 1139 
 1140     cleanremovedinterfaces(&s);
 1141 
 1142     ck_assert_int_eq(s.dbifcount, 3);
 1143     ck_assert_int_eq(datacache_count(&s.dcache), 3);
 1144 
 1145     ret = db_close();
 1146     ck_assert_int_eq(ret, 1);
 1147 
 1148     datacache_clear(&s.dcache);
 1149 }
 1150 END_TEST
 1151 
 1152 START_TEST(processifinfo_syncs_when_needed)
 1153 {
 1154     int ret;
 1155     DSTATE s;
 1156     initdstate(&s);
 1157 
 1158     ifinfo.rx = 11;
 1159     ifinfo.tx = 22;
 1160 
 1161     ck_assert_int_eq(datacache_count(&s.dcache), 0);
 1162     ret = datacache_add(&s.dcache, "ethsomething", 1);
 1163     ck_assert_int_eq(ret, 1);
 1164     ck_assert_int_eq(s.dcache->syncneeded, 1);
 1165     ck_assert_int_eq(s.dcache->currx, 0);
 1166     ck_assert_int_eq(s.dcache->curtx, 0);
 1167 
 1168     ret = processifinfo(&s, &s.dcache);
 1169     ck_assert_int_eq(ret, 1);
 1170 
 1171     ck_assert_int_eq(s.dcache->syncneeded, 0);
 1172     ck_assert_int_eq(s.dcache->currx, 11);
 1173     ck_assert_int_eq(s.dcache->curtx, 22);
 1174 
 1175     datacache_clear(&s.dcache);
 1176 }
 1177 END_TEST
 1178 
 1179 START_TEST(processifinfo_skips_update_if_timestamps_make_no_sense)
 1180 {
 1181     int ret;
 1182     DSTATE s;
 1183     initdstate(&s);
 1184 
 1185     ifinfo.rx = 11;
 1186     ifinfo.tx = 22;
 1187     ifinfo.timestamp = 250;
 1188 
 1189     ck_assert_int_eq(datacache_count(&s.dcache), 0);
 1190     ret = datacache_add(&s.dcache, "ethsomething", 0);
 1191     ck_assert_int_eq(ret, 1);
 1192     ck_assert_int_eq(s.dcache->syncneeded, 0);
 1193     ck_assert_int_eq(s.dcache->currx, 0);
 1194     ck_assert_int_eq(s.dcache->curtx, 0);
 1195     s.dcache->updated = 300;
 1196 
 1197     ret = processifinfo(&s, &s.dcache);
 1198     ck_assert_int_eq(ret, 0);
 1199 
 1200     ck_assert_int_eq(s.dcache->syncneeded, 0);
 1201     ck_assert_int_eq(s.dcache->currx, 0);
 1202     ck_assert_int_eq(s.dcache->curtx, 0);
 1203 
 1204     datacache_clear(&s.dcache);
 1205 }
 1206 END_TEST
 1207 
 1208 START_TEST(processifinfo_exits_if_timestamps_really_make_no_sense)
 1209 {
 1210     int ret;
 1211     DSTATE s;
 1212     initdstate(&s);
 1213     disable_logprints();
 1214 
 1215     ifinfo.rx = 11;
 1216     ifinfo.tx = 22;
 1217     ifinfo.timestamp = 250;
 1218 
 1219     ck_assert_int_eq(datacache_count(&s.dcache), 0);
 1220     ret = datacache_add(&s.dcache, "ethsomething", 0);
 1221     ck_assert_int_eq(ret, 1);
 1222     ck_assert_int_eq(s.dcache->syncneeded, 0);
 1223     ck_assert_int_eq(s.dcache->currx, 0);
 1224     ck_assert_int_eq(s.dcache->curtx, 0);
 1225     s.dcache->updated = 100000;
 1226 
 1227     processifinfo(&s, &s.dcache);
 1228 }
 1229 END_TEST
 1230 
 1231 START_TEST(processifinfo_syncs_if_timestamps_match)
 1232 {
 1233     int ret;
 1234     DSTATE s;
 1235     initdstate(&s);
 1236 
 1237     ifinfo.rx = 11;
 1238     ifinfo.tx = 22;
 1239     ifinfo.timestamp = 250;
 1240 
 1241     ck_assert_int_eq(datacache_count(&s.dcache), 0);
 1242     ret = datacache_add(&s.dcache, "ethsomething", 0);
 1243     ck_assert_int_eq(ret, 1);
 1244     ck_assert_int_eq(s.dcache->syncneeded, 0);
 1245     ck_assert_int_eq(s.dcache->currx, 0);
 1246     ck_assert_int_eq(s.dcache->curtx, 0);
 1247     s.dcache->updated = 250;
 1248 
 1249     ret = processifinfo(&s, &s.dcache);
 1250     ck_assert_int_eq(ret, 1);
 1251 
 1252     ck_assert_int_eq(s.dcache->syncneeded, 0);
 1253     ck_assert_int_eq(s.dcache->currx, 11);
 1254     ck_assert_int_eq(s.dcache->curtx, 22);
 1255     ck_assert_ptr_eq(s.dcache->log, NULL);
 1256 
 1257     datacache_clear(&s.dcache);
 1258 }
 1259 END_TEST
 1260 
 1261 START_TEST(processifinfo_adds_traffic)
 1262 {
 1263     int ret;
 1264     DSTATE s;
 1265     initdstate(&s);
 1266     suppress_output();
 1267     debug = 1;
 1268 
 1269     ifinfo.rx = 11;
 1270     ifinfo.tx = 22;
 1271     ifinfo.timestamp = 250;
 1272 
 1273     ck_assert_int_eq(datacache_count(&s.dcache), 0);
 1274     ret = datacache_add(&s.dcache, "ethsomething", 0);
 1275     ck_assert_int_eq(ret, 1);
 1276     ck_assert_int_eq(s.dcache->syncneeded, 0);
 1277     ck_assert_int_eq(s.dcache->currx, 0);
 1278     ck_assert_int_eq(s.dcache->curtx, 0);
 1279     s.dcache->updated = 200;
 1280 
 1281     ret = ibwadd("ethsomething", 1000);
 1282     ck_assert_int_eq(ret, 1);
 1283 
 1284     ret = processifinfo(&s, &s.dcache);
 1285     ck_assert_int_eq(ret, 1);
 1286 
 1287     ck_assert_int_eq(s.dcache->syncneeded, 0);
 1288     ck_assert_int_eq(s.dcache->currx, 11);
 1289     ck_assert_int_eq(s.dcache->curtx, 22);
 1290     ck_assert_ptr_ne(s.dcache->log, NULL);
 1291 
 1292     datacache_clear(&s.dcache);
 1293 }
 1294 END_TEST
 1295 
 1296 START_TEST(processifinfo_does_not_add_traffic_when_over_limit)
 1297 {
 1298     int ret;
 1299     DSTATE s;
 1300     initdstate(&s);
 1301     suppress_output();
 1302     debug = 1;
 1303 
 1304     ifinfo.rx = 1111111;
 1305     ifinfo.tx = 2222222;
 1306     ifinfo.timestamp = 250;
 1307     cfg.trafficlessentries = 0;
 1308 
 1309     ck_assert_int_eq(datacache_count(&s.dcache), 0);
 1310     ret = datacache_add(&s.dcache, "ethsomething", 0);
 1311     ck_assert_int_eq(ret, 1);
 1312     ck_assert_int_eq(s.dcache->syncneeded, 0);
 1313     ck_assert_int_eq(s.dcache->currx, 0);
 1314     ck_assert_int_eq(s.dcache->curtx, 0);
 1315     s.dcache->updated = 249;
 1316 
 1317     ret = ibwadd("ethsomething", 1);
 1318     ck_assert_int_eq(ret, 1);
 1319 
 1320     ret = processifinfo(&s, &s.dcache);
 1321     ck_assert_int_eq(ret, 1);
 1322 
 1323     ck_assert_int_eq(s.dcache->syncneeded, 0);
 1324     ck_assert_int_eq(s.dcache->currx, 1111111);
 1325     ck_assert_int_eq(s.dcache->curtx, 2222222);
 1326     ck_assert_ptr_eq(s.dcache->log, NULL);
 1327 
 1328     datacache_clear(&s.dcache);
 1329 }
 1330 END_TEST
 1331 
 1332 START_TEST(processifinfo_adds_zero_traffic_when_over_limit)
 1333 {
 1334     int ret;
 1335     DSTATE s;
 1336     initdstate(&s);
 1337     suppress_output();
 1338     debug = 1;
 1339 
 1340     ifinfo.rx = 1111111;
 1341     ifinfo.tx = 2222222;
 1342     ifinfo.timestamp = 250;
 1343     cfg.trafficlessentries = 1;
 1344 
 1345     ck_assert_int_eq(datacache_count(&s.dcache), 0);
 1346     ret = datacache_add(&s.dcache, "ethsomething", 0);
 1347     ck_assert_int_eq(ret, 1);
 1348     ck_assert_int_eq(s.dcache->syncneeded, 0);
 1349     ck_assert_int_eq(s.dcache->currx, 0);
 1350     ck_assert_int_eq(s.dcache->curtx, 0);
 1351     s.dcache->updated = 249;
 1352 
 1353     ret = ibwadd("ethsomething", 1);
 1354     ck_assert_int_eq(ret, 1);
 1355 
 1356     ret = processifinfo(&s, &s.dcache);
 1357     ck_assert_int_eq(ret, 1);
 1358 
 1359     ck_assert_int_eq(s.dcache->syncneeded, 0);
 1360     ck_assert_int_eq(s.dcache->currx, 1111111);
 1361     ck_assert_int_eq(s.dcache->curtx, 2222222);
 1362     ck_assert_ptr_ne(s.dcache->log, NULL);
 1363     ck_assert_int_eq(s.dcache->log->rx, 0);
 1364     ck_assert_int_eq(s.dcache->log->tx, 0);
 1365 
 1366     datacache_clear(&s.dcache);
 1367 }
 1368 END_TEST
 1369 
 1370 START_TEST(datacache_status_can_handle_nothing)
 1371 {
 1372     datacache *dcache;
 1373     disable_logprints();
 1374     dcache = NULL;
 1375 
 1376     datacache_status(&dcache);
 1377 }
 1378 END_TEST
 1379 
 1380 START_TEST(datacache_status_can_show_limits)
 1381 {
 1382     int ret;
 1383     datacache *dcache;
 1384     disable_logprints();
 1385     dcache = NULL;
 1386 
 1387     ret = datacache_add(&dcache, "ethdefault", 0);
 1388     ck_assert_int_eq(ret, 1);
 1389     ret = datacache_add(&dcache, "ethslow", 0);
 1390     ck_assert_int_eq(ret, 1);
 1391     ret = datacache_add(&dcache, "ethfast", 0);
 1392     ck_assert_int_eq(ret, 1);
 1393     ret = datacache_add(&dcache, "ethnolimit", 0);
 1394     ck_assert_int_eq(ret, 1);
 1395 
 1396     ret = ibwadd("ethslow", 1);
 1397     ck_assert_int_eq(ret, 1);
 1398     ret = ibwadd("ethfast", 1000);
 1399     ck_assert_int_eq(ret, 1);
 1400     ret = ibwadd("ethnolimit", 0);
 1401     ck_assert_int_eq(ret, 1);
 1402 
 1403     datacache_status(&dcache);
 1404 
 1405     datacache_clear(&dcache);
 1406 }
 1407 END_TEST
 1408 
 1409 START_TEST(datacache_status_has_no_issues_with_large_number_of_interfaces)
 1410 {
 1411     int i, ret;
 1412     char buffer[8];
 1413     datacache *dcache;
 1414     disable_logprints();
 1415     dcache = NULL;
 1416 
 1417     for (i = 0; i < 100; i++) {
 1418         snprintf(buffer, 8, "eth%d", i);
 1419         ret = datacache_add(&dcache, buffer, 0);
 1420         ck_assert_int_eq(ret, 1);
 1421         ret = ibwadd(buffer, (uint32_t)i);
 1422         ck_assert_int_eq(ret, 1);
 1423     }
 1424 
 1425     datacache_status(&dcache);
 1426 
 1427     datacache_clear(&dcache);
 1428 }
 1429 END_TEST
 1430 
 1431 START_TEST(rescandatabaseforinterfaces_does_nothing_if_database_has_no_interfaces)
 1432 {
 1433     int ret;
 1434     DSTATE s;
 1435     initdstate(&s);
 1436 
 1437     ck_assert_int_eq(datacache_count(&s.dcache), 0);
 1438     ck_assert_int_eq(s.dbifcount, 0);
 1439 
 1440     ret = db_open_rw(1);
 1441     ck_assert_int_eq(ret, 1);
 1442     ck_assert_int_eq(db_getinterfacecount(), 0);
 1443 
 1444     rescandatabaseforinterfaces(&s);
 1445 
 1446     ck_assert_int_eq(datacache_count(&s.dcache), 0);
 1447     ck_assert_int_eq(s.dbifcount, 0);
 1448 
 1449     ck_assert_int_eq(db_getinterfacecount(), 0);
 1450 
 1451     ret = db_close();
 1452     ck_assert_int_eq(ret, 1);
 1453 
 1454     datacache_clear(&s.dcache);
 1455 }
 1456 END_TEST
 1457 
 1458 START_TEST(rescandatabaseforinterfaces_does_nothing_if_interface_lists_are_the_same)
 1459 {
 1460     int ret;
 1461     DSTATE s;
 1462     initdstate(&s);
 1463 
 1464     ck_assert_int_eq(datacache_count(&s.dcache), 0);
 1465     ret = datacache_add(&s.dcache, "ethtwo", 0);
 1466     ck_assert_int_eq(ret, 1);
 1467     ret = datacache_add(&s.dcache, "eththree", 0);
 1468     ck_assert_int_eq(ret, 1);
 1469     ret = datacache_add(&s.dcache, "ethone", 0);
 1470     ck_assert_int_eq(ret, 1);
 1471     ck_assert_int_eq(datacache_count(&s.dcache), 3);
 1472     s.dbifcount = 3;
 1473 
 1474     ret = db_open_rw(1);
 1475     ck_assert_int_eq(ret, 1);
 1476     ck_assert_int_eq(db_getinterfacecount(), 0);
 1477     ret = db_addinterface("ethtwo");
 1478     ck_assert_int_eq(ret, 1);
 1479     ret = db_addinterface("ethone");
 1480     ck_assert_int_eq(ret, 1);
 1481     ret = db_addinterface("eththree");
 1482     ck_assert_int_eq(ret, 1);
 1483     ck_assert_int_eq(db_getinterfacecount(), 3);
 1484 
 1485     rescandatabaseforinterfaces(&s);
 1486 
 1487     ck_assert_int_eq(datacache_count(&s.dcache), 3);
 1488     ck_assert_int_eq(s.dbifcount, 3);
 1489 
 1490     ck_assert_int_eq(db_getinterfacecount(), 3);
 1491 
 1492     ret = db_close();
 1493     ck_assert_int_eq(ret, 1);
 1494 
 1495     datacache_clear(&s.dcache);
 1496 }
 1497 END_TEST
 1498 
 1499 START_TEST(rescandatabaseforinterfaces_adds_missing_interfaces_to_update_list)
 1500 {
 1501     int ret;
 1502     DSTATE s;
 1503     initdstate(&s);
 1504     disable_logprints();
 1505 
 1506     ck_assert_int_eq(datacache_count(&s.dcache), 0);
 1507     ret = datacache_add(&s.dcache, "ethone", 0);
 1508     ck_assert_int_eq(ret, 1);
 1509     ret = datacache_add(&s.dcache, "ethfour", 0);
 1510     ck_assert_int_eq(ret, 1);
 1511     ck_assert_int_eq(datacache_count(&s.dcache), 2);
 1512     s.dbifcount = 2;
 1513 
 1514     ret = db_open_rw(1);
 1515     ck_assert_int_eq(ret, 1);
 1516     ck_assert_int_eq(db_getinterfacecount(), 0);
 1517     ret = db_addinterface("ethtwo");
 1518     ck_assert_int_eq(ret, 1);
 1519     ret = db_addinterface("ethone");
 1520     ck_assert_int_eq(ret, 1);
 1521     ret = db_addinterface("eththree");
 1522     ck_assert_int_eq(ret, 1);
 1523     ck_assert_int_eq(db_getinterfacecount(), 3);
 1524 
 1525     rescandatabaseforinterfaces(&s);
 1526 
 1527     ck_assert_int_eq(datacache_count(&s.dcache), 4);
 1528     ck_assert_int_eq(s.dbifcount, 4);
 1529 
 1530     ck_assert_int_eq(db_getinterfacecount(), 3);
 1531 
 1532     ret = db_close();
 1533     ck_assert_int_eq(ret, 1);
 1534 
 1535     datacache_clear(&s.dcache);
 1536 }
 1537 END_TEST
 1538 
 1539 void add_daemon_tests(Suite *s)
 1540 {
 1541     TCase *tc_daemon = tcase_create("Daemon");
 1542     tcase_add_checked_fixture(tc_daemon, setup, teardown);
 1543     tcase_add_unchecked_fixture(tc_daemon, setup, teardown);
 1544     tcase_add_test(tc_daemon, debugtimestamp_does_not_exit);
 1545     tcase_add_test(tc_daemon, initdstate_does_not_crash);
 1546     tcase_add_test(tc_daemon, addinterfaces_does_nothing_with_no_files);
 1547     tcase_add_test(tc_daemon, addinterfaces_adds_interfaces);
 1548     tcase_add_test(tc_daemon, addinterfaces_adds_only_new_interfaces);
 1549     tcase_add_test(tc_daemon, addinterfaces_adds_to_cache_when_running);
 1550     tcase_add_exit_test(tc_daemon, preparedatabase_exits_with_no_database_dir, 1);
 1551     tcase_add_exit_test(tc_daemon, preparedatabase_exits_with_no_database, 1);
 1552     tcase_add_exit_test(tc_daemon, preparedatabase_exits_with_no_database_and_noadd, 1);
 1553     tcase_add_test(tc_daemon, preparedatabase_with_no_database_creates_database);
 1554     tcase_add_test(tc_daemon, setsignaltraps_does_not_exit);
 1555     tcase_add_exit_test(tc_daemon, filldatabaselist_exits_with_no_database_dir, 1);
 1556     tcase_add_test(tc_daemon, filldatabaselist_does_not_exit_with_empty_database_dir);
 1557     tcase_add_test(tc_daemon, filldatabaselist_adds_databases);
 1558     tcase_add_test(tc_daemon, adjustsaveinterval_with_empty_cache);
 1559     tcase_add_test(tc_daemon, adjustsaveinterval_with_filled_cache);
 1560     tcase_add_test(tc_daemon, checkdbsaveneed_has_no_need);
 1561     tcase_add_test(tc_daemon, checkdbsaveneed_is_forced);
 1562     tcase_add_test(tc_daemon, checkdbsaveneed_needs);
 1563     tcase_add_test(tc_daemon, processdatacache_empty_does_nothing);
 1564     tcase_add_test(tc_daemon, processdatacache_can_process_things);
 1565     tcase_add_test(tc_daemon, handleintsignals_handles_no_signal);
 1566     tcase_add_test(tc_daemon, handleintsignals_handles_42);
 1567     tcase_add_test(tc_daemon, handleintsignals_handles_unknown_signal);
 1568     tcase_add_test(tc_daemon, handleintsignals_handles_sigterm);
 1569     tcase_add_test(tc_daemon, handleintsignals_handles_sigint);
 1570     tcase_add_test(tc_daemon, handleintsignals_handles_sighup);
 1571     tcase_add_test(tc_daemon, preparedirs_with_no_dir);
 1572     tcase_add_test(tc_daemon, preparedirs_with_dir);
 1573     tcase_add_test(tc_daemon, interfacechangecheck_with_no_interfaces);
 1574     tcase_add_test(tc_daemon, interfacechangecheck_with_empty_cache);
 1575     tcase_add_test(tc_daemon, interfacechangecheck_with_no_changes_in_iflist);
 1576     tcase_add_test(tc_daemon, interfacechangecheck_with_filled_cache);
 1577     tcase_add_test(tc_daemon, simplehash_with_empty_strings);
 1578     tcase_add_test(tc_daemon, simplehash_with_simple_strings);
 1579     tcase_add_test(tc_daemon, initcachevalues_does_not_init_without_database);
 1580     tcase_add_test(tc_daemon, initcachevalues_does_init);
 1581     tcase_add_test(tc_daemon, getcurrenthour_returns_something_realistic);
 1582     tcase_add_test(tc_daemon, waittimesync_does_not_wait_unless_configured_to_do_so);
 1583     tcase_add_test(tc_daemon, waittimesync_does_not_wait_with_no_interfaces);
 1584     tcase_add_test(tc_daemon, waittimesync_does_not_wait_with_new_interfaces);
 1585     tcase_add_test(tc_daemon, waittimesync_knows_when_to_wait);
 1586     tcase_add_test(tc_daemon, waittimesync_knows_when_to_give_up);
 1587     tcase_add_test(tc_daemon, detectboot_sets_btime_if_missing_from_database);
 1588     tcase_add_test(tc_daemon, detectboot_sets_btime_for_new_database);
 1589     tcase_add_test(tc_daemon, detectboot_can_detect_boot);
 1590     tcase_add_exit_test(tc_daemon, handledatabaseerror_exits_on_fatal_error, 1);
 1591     tcase_add_test(tc_daemon, handledatabaseerror_does_not_exit_if_limit_is_not_exceeded);
 1592     tcase_add_exit_test(tc_daemon, handledatabaseerror_exits_if_limit_is_exceeded, 1);
 1593     tcase_add_test(tc_daemon, cleanremovedinterfaces_allows_interfaces_to_be_removed);
 1594     tcase_add_test(tc_daemon, processifinfo_syncs_when_needed);
 1595     tcase_add_test(tc_daemon, processifinfo_skips_update_if_timestamps_make_no_sense);
 1596     tcase_add_exit_test(tc_daemon, processifinfo_exits_if_timestamps_really_make_no_sense, 1);
 1597     tcase_add_test(tc_daemon, processifinfo_syncs_if_timestamps_match);
 1598     tcase_add_test(tc_daemon, processifinfo_adds_traffic);
 1599     tcase_add_test(tc_daemon, processifinfo_does_not_add_traffic_when_over_limit);
 1600     tcase_add_test(tc_daemon, processifinfo_adds_zero_traffic_when_over_limit);
 1601     tcase_add_test(tc_daemon, datacache_status_can_handle_nothing);
 1602     tcase_add_test(tc_daemon, datacache_status_can_show_limits);
 1603     tcase_add_test(tc_daemon, datacache_status_has_no_issues_with_large_number_of_interfaces);
 1604     tcase_add_test(tc_daemon, rescandatabaseforinterfaces_does_nothing_if_database_has_no_interfaces);
 1605     tcase_add_test(tc_daemon, rescandatabaseforinterfaces_does_nothing_if_interface_lists_are_the_same);
 1606     tcase_add_test(tc_daemon, rescandatabaseforinterfaces_adds_missing_interfaces_to_update_list);
 1607     suite_add_tcase(s, tc_daemon);
 1608 }