"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/tests/vnstat_tests.c" (26 Jul 2021, 8532 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 "vnstat_tests.c": 2.7_vs_2.8.

    1 #include "common.h"
    2 #include "cfg.h"
    3 #include "vnstat_tests.h"
    4 #include "common_tests.h"
    5 #include "dbsql_tests.h"
    6 #include "database_tests.h"
    7 #include "config_tests.h"
    8 #include "ifinfo_tests.h"
    9 #include "misc_tests.h"
   10 #include "daemon_tests.h"
   11 #include "datacache_tests.h"
   12 #include "fs_tests.h"
   13 #include "id_tests.h"
   14 #include "iflist_tests.h"
   15 #include "cli_tests.h"
   16 #include "parseargs_tests.h"
   17 #if defined(HAVE_IMAGE)
   18 #include "image_tests.h"
   19 #endif
   20 
   21 int output_suppressed = 0;
   22 
   23 int main(void)
   24 {
   25     Suite *s;
   26     SRunner *sr;
   27     int number_failed = 0;
   28 
   29     verify_fork_status();
   30     s = test_suite();
   31     sr = srunner_create(s);
   32     srunner_set_log(sr, "test.log");
   33     srunner_set_xml(sr, "test.xml");
   34     srunner_run_all(sr, CK_NORMAL);
   35     number_failed = srunner_ntests_failed(sr);
   36     srunner_free(sr);
   37 
   38     if (number_failed == 0) {
   39         remove_directory(TESTDIR);
   40     }
   41 
   42     return number_failed;
   43 }
   44 
   45 Suite *test_suite(void)
   46 {
   47     Suite *s = suite_create("vnStat");
   48 
   49     add_common_tests(s);
   50     add_dbsql_tests(s);
   51     add_database_tests(s);
   52     add_config_tests(s);
   53     add_ifinfo_tests(s);
   54     add_misc_tests(s);
   55     add_daemon_tests(s);
   56     add_datacache_tests(s);
   57     add_fs_tests(s);
   58     add_id_tests(s);
   59     add_iflist_tests(s);
   60     add_cli_tests(s);
   61     add_parseargs_tests(s);
   62 #if defined(HAVE_IMAGE)
   63     add_image_tests(s);
   64 #endif
   65 
   66     return s;
   67 }
   68 
   69 /* tests can't reliably be executed if check doesn't have forking enabled */
   70 /* and only SRunner knows that so a dummy runner needs to be created */
   71 void verify_fork_status(void)
   72 {
   73     Suite *s = suite_create("fork status check");
   74     SRunner *sr = srunner_create(s);
   75     if (srunner_fork_status(sr) == CK_NOFORK) {
   76         printf("Error: Tests require Check to have fork mode enabled.\n");
   77         exit(1);
   78     }
   79     srunner_free(sr);
   80 }
   81 
   82 void setup(void) {
   83     defaultcfg();
   84     debug = 0;
   85 }
   86 
   87 void teardown(void) {
   88     return;
   89 }
   90 
   91 void suppress_output(void)
   92 {
   93     if (!output_suppressed) {
   94         fclose(stdout);
   95         output_suppressed = 1;
   96     }
   97 }
   98 
   99 int pipe_output(void)
  100 {
  101     int out_pipe[2];
  102 
  103     if (pipe(out_pipe) != 0) {
  104         ck_abort_msg("error \"%s\" while creating pipe", strerror(errno));
  105     }
  106 
  107     if (out_pipe[1] != STDOUT_FILENO) {
  108         dup2(out_pipe[1], STDOUT_FILENO);
  109         close(out_pipe[1]);
  110     }
  111 
  112     output_suppressed = 1;
  113     return out_pipe[0];
  114 }
  115 
  116 void disable_logprints(void)
  117 {
  118     noexit = 2;
  119     cfg.uselogging = 0;
  120     disableprints = 1;
  121 }
  122 
  123 int clean_testdbdir(void)
  124 {
  125     struct stat statbuf;
  126 
  127     create_testdir();
  128 
  129     if (stat(TESTDBDIR, &statbuf) != 0) {
  130         if (errno == ENOENT) {
  131             if (mkdir(TESTDBDIR, 0755) == 0) {
  132                 return 1;
  133             }
  134         }
  135         ck_abort_msg("error \"%s\" while creating directory \"%s\"", strerror(errno), TESTDBDIR);
  136     }
  137 
  138     if (!remove_directory(TESTDBDIR)) {
  139         ck_abort_msg("error \"%s\" while removing directory \"%s\", please remove it manually", strerror(errno), TESTDBDIR);
  140     }
  141 
  142     if (mkdir(TESTDBDIR, 0755) != 0) {
  143         ck_abort_msg("error \"%s\" while creating directory \"%s\"", strerror(errno), TESTDBDIR);
  144     }
  145 
  146     return 1;
  147 }
  148 
  149 int create_testdir(void)
  150 {
  151     struct stat statbuf;
  152 
  153     if (stat(TESTDIR, &statbuf) != 0) {
  154         if (errno == ENOENT) {
  155             if (mkdir(TESTDIR, 0755) == 0) {
  156                 return 1;
  157             }
  158         }
  159         ck_abort_msg("error \"%s\" while creating directory \"%s\"", strerror(errno), TESTDIR);
  160     }
  161 
  162     return 1;
  163 }
  164 
  165 int create_directory(const char *directory)
  166 {
  167     struct stat statbuf;
  168 
  169     if (stat(directory, &statbuf) != 0) {
  170         if (errno == ENOENT) {
  171             if (mkdir(directory, 0755) != 0) {
  172                 ck_abort_msg("error \"%s\" while creating directory \"%s\"", strerror(errno), directory);
  173             }
  174         } else {
  175             ck_abort_msg("error \"%s\" while creating directory \"%s\"", strerror(errno), directory);
  176         }
  177     }
  178 
  179     return 1;
  180 }
  181 
  182 int remove_directory(const char *directory)
  183 {
  184     DIR *dir = NULL;
  185     struct dirent *di = NULL;
  186     char entryname[512];
  187 
  188     if ((dir = opendir(directory)) == NULL) {
  189         if (errno == ENOENT) {
  190             return 1;
  191         } else {
  192             return 0;
  193         }
  194     }
  195 
  196     while ((di = readdir(dir))) {
  197         switch (di->d_type) {
  198             case DT_LNK:
  199             case DT_REG:
  200                 snprintf(entryname, 512, "%s/%s", directory, di->d_name);
  201                 if (unlink(entryname) != 0) {
  202                     closedir(dir);
  203                     return 0;
  204                 }
  205                 break;
  206             case DT_DIR:
  207                 if (strcmp(di->d_name, ".") == 0 || strcmp(di->d_name, "..") == 0) {
  208                     continue;
  209                 }
  210                 snprintf(entryname, 512, "%s/%s", directory, di->d_name);
  211                 if (!remove_directory(entryname)) {
  212                     closedir(dir);
  213                     return 0;
  214                 }
  215                 break;
  216             default:
  217                 continue;
  218         }
  219     }
  220     closedir(dir);
  221     if (rmdir(directory) != 0) {
  222         return 0;
  223     }
  224 
  225     return 1;
  226 }
  227 
  228 int create_zerosize_dbfile(const char *iface)
  229 {
  230     FILE *fp;
  231     char filename[512];
  232 
  233     snprintf(filename, 512, "%s/%s", TESTDBDIR, iface);
  234     if ((fp = fopen(filename, "w")) == NULL) {
  235         ck_abort_msg("error \"%s\" while opening file \"%s\" for writing", strerror(errno), filename);
  236     }
  237     fclose(fp);
  238 
  239     return 1;
  240 }
  241 
  242 int check_dbfile_exists(const char *iface, const int minsize)
  243 {
  244     struct stat statbuf;
  245     char filename[512];
  246 
  247     snprintf(filename, 512, "%s/%s", TESTDBDIR, iface);
  248     if (stat(filename, &statbuf) != 0) {
  249         if (errno == ENOENT) {
  250             return 0;
  251         }
  252         ck_abort_msg("error \"%s\" while inspecting file \"%s\"", strerror(errno), filename);
  253     }
  254 
  255     if (statbuf.st_size < minsize) {
  256         ck_abort_msg("file \"%s\" is smaller (%d) then given minimum %d", filename, (int)statbuf.st_size, minsize);
  257     }
  258 
  259     return 1;
  260 }
  261 
  262 int fake_proc_net_dev(const char *mode, const char *iface, const int rx, const int tx, const int rxp, const int txp)
  263 {
  264     FILE *devfp;
  265     char filename[512];
  266 
  267     if (strcmp(mode, "w") != 0 && strcmp(mode, "a") != 0) {
  268         ck_abort_msg("error: only w and a modes are supported");
  269     }
  270 
  271     create_testdir();
  272     create_directory(TESTPROCDIR);
  273 
  274     snprintf(filename, 512, "%s/dev", TESTPROCDIR);
  275     if ((devfp = fopen(filename, mode)) == NULL) {
  276         ck_abort_msg("error \"%s\" while opening file \"%s\" for writing", strerror(errno), filename);
  277     }
  278 
  279     if (strcmp(mode, "w") == 0) {
  280         fprintf(devfp, "Inter-|   Receive                                                |  Transmit\n");
  281         fprintf(devfp, " face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed\n");
  282         fprintf(devfp, "    lo:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0\n");
  283     }
  284     fprintf(devfp, "%6s: %7d %7d    0    0    0     0          0         0  %7d %7d    0    0    0     0       0          0\n", iface, rx, rxp, tx, txp);
  285 
  286     fclose(devfp);
  287 
  288     return 1;
  289 }
  290 
  291 int fake_sys_class_net(const char *iface, const int rx, const int tx, const int rxp, const int txp, const int speed)
  292 {
  293     FILE *devfp;
  294     char dirname[512];
  295     char filename[512];
  296 
  297     create_testdir();
  298     create_directory(TESTSYSCLASSNETDIR);
  299 
  300     snprintf(dirname, 512, "%s/%s", TESTSYSCLASSNETDIR, iface);
  301     create_directory(dirname);
  302 
  303     snprintf(dirname, 512, "%s/%s/statistics", TESTSYSCLASSNETDIR, iface);
  304     create_directory(dirname);
  305 
  306     if (speed != 0) {
  307         snprintf(filename, 512, "%s/%s/speed", TESTSYSCLASSNETDIR, iface);
  308         if ((devfp = fopen(filename, "w")) == NULL) {
  309             ck_abort_msg("error \"%s\" while opening file \"%s\" for writing", strerror(errno), filename);
  310         }
  311         fprintf(devfp, "%d\n", speed);
  312         fclose(devfp);
  313     }
  314 
  315     snprintf(filename, 512, "%s/%s/statistics/rx_bytes", TESTSYSCLASSNETDIR, iface);
  316     if ((devfp = fopen(filename, "w")) == NULL) {
  317         ck_abort_msg("error \"%s\" while opening file \"%s\" for writing", strerror(errno), filename);
  318     }
  319     fprintf(devfp, "%d\n", rx);
  320     fclose(devfp);
  321 
  322     snprintf(filename, 512, "%s/%s/statistics/tx_bytes", TESTSYSCLASSNETDIR, iface);
  323     if ((devfp = fopen(filename, "w")) == NULL) {
  324         ck_abort_msg("error \"%s\" while opening file \"%s\" for writing", strerror(errno), filename);
  325     }
  326     fprintf(devfp, "%d\n", tx);
  327     fclose(devfp);
  328 
  329     snprintf(filename, 512, "%s/%s/statistics/rx_packets", TESTSYSCLASSNETDIR, iface);
  330     if ((devfp = fopen(filename, "w")) == NULL) {
  331         ck_abort_msg("error \"%s\" while opening file \"%s\" for writing", strerror(errno), filename);
  332     }
  333     fprintf(devfp, "%d\n", rxp);
  334     fclose(devfp);
  335 
  336     snprintf(filename, 512, "%s/%s/statistics/tx_packets", TESTSYSCLASSNETDIR, iface);
  337     if ((devfp = fopen(filename, "w")) == NULL) {
  338         ck_abort_msg("error \"%s\" while opening file \"%s\" for writing", strerror(errno), filename);
  339     }
  340     fprintf(devfp, "%d\n", txp);
  341     fclose(devfp);
  342 
  343     return 1;
  344 }
  345 
  346 uint64_t get_timestamp(const int year, const int month, const int day, const int hour, const int minute)
  347 {
  348     struct tm stm;
  349 
  350     memset(&stm, 0, sizeof(struct tm));
  351     stm.tm_year = year - 1900;
  352     stm.tm_mon = month - 1;
  353     stm.tm_mday = day;
  354     stm.tm_hour = hour;
  355     stm.tm_min = minute;
  356     stm.tm_isdst = -1;
  357 
  358     if (cfg.useutc) {
  359         return (uint64_t)timegm(&stm);
  360     } else {
  361         return (uint64_t)mktime(&stm);
  362     }
  363 }