LCOV - code coverage report
Current view: top level - test - testing_common.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 149 182 81.9 %
Date: 2021-11-24 03:28:48 Functions: 13 15 86.7 %

          Line data    Source code
       1             : /* Copyright (c) 2001-2004, Roger Dingledine.
       2             :  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
       3             :  * Copyright (c) 2007-2021, The Tor Project, Inc. */
       4             : /* See LICENSE for licensing information */
       5             : 
       6             : /**
       7             :  * \file test_common.c
       8             :  * \brief Common pieces to implement unit tests.
       9             :  **/
      10             : 
      11             : #define MAINLOOP_PRIVATE
      12             : #include "orconfig.h"
      13             : #include "core/or/or.h"
      14             : #include "feature/control/control.h"
      15             : #include "feature/control/control_events.h"
      16             : #include "app/config/config.h"
      17             : #include "lib/crypt_ops/crypto_dh.h"
      18             : #include "lib/crypt_ops/crypto_ed25519.h"
      19             : #include "lib/crypt_ops/crypto_rand.h"
      20             : #include "feature/stats/predict_ports.h"
      21             : #include "feature/stats/bwhist.h"
      22             : #include "feature/stats/rephist.h"
      23             : #include "lib/err/backtrace.h"
      24             : #include "test/test.h"
      25             : #include "core/or/channelpadding.h"
      26             : #include "core/mainloop/mainloop.h"
      27             : #include "lib/compress/compress.h"
      28             : #include "lib/evloop/compat_libevent.h"
      29             : #include "lib/crypt_ops/crypto_init.h"
      30             : #include "lib/version/torversion.h"
      31             : #include "app/main/subsysmgr.h"
      32             : 
      33             : #include <stdio.h>
      34             : #ifdef HAVE_FCNTL_H
      35             : #include <fcntl.h>
      36             : #endif
      37             : #ifdef HAVE_UNISTD_H
      38             : #include <unistd.h>
      39             : #endif
      40             : #ifdef HAVE_SYS_STAT_H
      41             : #include <sys/stat.h>
      42             : #endif
      43             : 
      44             : #ifdef _WIN32
      45             : /* For mkdir() */
      46             : #include <direct.h>
      47             : #else
      48             : #include <dirent.h>
      49             : #endif /* defined(_WIN32) */
      50             : 
      51             : /** Temporary directory (set up by setup_directory) under which we store all
      52             :  * our files during testing. */
      53             : static char temp_dir[256];
      54             : #ifdef _WIN32
      55             : #define pid_t int
      56             : #endif
      57             : static pid_t temp_dir_setup_in_pid = 0;
      58             : 
      59             : /** Select and create the temporary directory we'll use to run our unit tests.
      60             :  * Store it in <b>temp_dir</b>.  Exit immediately if we can't create it.
      61             :  * idempotent. */
      62             : static void
      63         207 : setup_directory(void)
      64             : {
      65         207 :   static int is_setup = 0;
      66         207 :   int r;
      67         207 :   char rnd[256], rnd32[256];
      68         207 :   if (is_setup) return;
      69             : 
      70             : /* Due to base32 limitation needs to be a multiple of 5. */
      71             : #define RAND_PATH_BYTES 5
      72           9 :   crypto_rand(rnd, RAND_PATH_BYTES);
      73           9 :   base32_encode(rnd32, sizeof(rnd32), rnd, RAND_PATH_BYTES);
      74             : 
      75             : #ifdef _WIN32
      76             :   {
      77             :     char buf[MAX_PATH];
      78             :     const char *tmp = buf;
      79             :     const char *extra_backslash = "";
      80             :     /* If this fails, we're probably screwed anyway */
      81             :     if (!GetTempPathA(sizeof(buf),buf))
      82             :       tmp = "c:\\windows\\temp\\";
      83             :     if (strcmpend(tmp, "\\")) {
      84             :       /* According to MSDN, it should be impossible for GetTempPath to give us
      85             :        * an answer that doesn't end with \.  But let's make sure. */
      86             :       extra_backslash = "\\";
      87             :     }
      88             :     tor_snprintf(temp_dir, sizeof(temp_dir),
      89             :                  "%s%stor_test_%d_%s", tmp, extra_backslash,
      90             :                  (int)getpid(), rnd32);
      91             :     r = mkdir(temp_dir);
      92             :   }
      93             : #elif defined(__ANDROID__)
      94             :   /* tor might not like the default perms, so create a subdir */
      95             :   tor_snprintf(temp_dir, sizeof(temp_dir),
      96             :                "/data/local/tmp/tor_%d_%d_%s",
      97             :                (int) getuid(), (int) getpid(), rnd32);
      98             :   r = mkdir(temp_dir, 0700);
      99             :   if (r) {
     100             :     fprintf(stderr, "Can't create directory %s:", temp_dir);
     101             :     perror("");
     102             :     exit(1);
     103             :   }
     104             : #else /* !defined(_WIN32) */
     105           9 :   tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d_%s",
     106           9 :                (int) getpid(), rnd32);
     107           9 :   r = mkdir(temp_dir, 0700);
     108           9 :   if (!r) {
     109             :     /* undo sticky bit so tests don't get confused. */
     110           9 :     r = chown(temp_dir, getuid(), getgid());
     111             :   }
     112             : #endif /* defined(_WIN32) || ... */
     113           9 :   if (r) {
     114           0 :     fprintf(stderr, "Can't create directory %s:", temp_dir);
     115           0 :     perror("");
     116           0 :     exit(1);
     117             :   }
     118           9 :   is_setup = 1;
     119           9 :   temp_dir_setup_in_pid = getpid();
     120             : }
     121             : 
     122             : /** Return a filename relative to our testing temporary directory, based on
     123             :  * name and suffix. If name is NULL, return the name of the testing temporary
     124             :  * directory. */
     125             : static const char *
     126         198 : get_fname_suffix(const char *name, const char *suffix)
     127             : {
     128         198 :   static char buf[1024];
     129         198 :   setup_directory();
     130         198 :   if (!name)
     131             :     return temp_dir;
     132         328 :   tor_snprintf(buf,sizeof(buf),"%s%s%s%s%s", temp_dir, PATH_SEPARATOR, name,
     133             :                suffix ? "_" : "", suffix ? suffix : "");
     134         194 :   return buf;
     135             : }
     136             : 
     137             : /** Return a filename relative to our testing temporary directory. If name is
     138             :  * NULL, return the name of the testing temporary directory. */
     139             : const char *
     140         138 : get_fname(const char *name)
     141             : {
     142         138 :   return get_fname_suffix(name, NULL);
     143             : }
     144             : 
     145             : /** Return a filename with a random suffix, relative to our testing temporary
     146             :  * directory. If name is NULL, return the name of the testing temporary
     147             :  * directory, without any suffix. */
     148             : const char *
     149          60 : get_fname_rnd(const char *name)
     150             : {
     151          60 :   char rnd[256], rnd32[256];
     152          60 :   crypto_rand(rnd, RAND_PATH_BYTES);
     153          60 :   base32_encode(rnd32, sizeof(rnd32), rnd, RAND_PATH_BYTES);
     154          60 :   return get_fname_suffix(name, rnd32);
     155             : }
     156             : 
     157             : /* Remove a directory and all of its subdirectories */
     158             : static void
     159         156 : rm_rf(const char *dir)
     160             : {
     161         156 :   struct stat st;
     162         156 :   smartlist_t *elements;
     163             : 
     164         156 :   elements = tor_listdir(dir);
     165         156 :   if (elements) {
     166         696 :     SMARTLIST_FOREACH_BEGIN(elements, const char *, cp) {
     167         542 :          char *tmp = NULL;
     168         542 :          tor_asprintf(&tmp, "%s"PATH_SEPARATOR"%s", dir, cp);
     169         542 :          if (0 == stat(tmp,&st) && (st.st_mode & S_IFDIR)) {
     170         147 :            rm_rf(tmp);
     171             :          } else {
     172         395 :            if (unlink(tmp)) {
     173           0 :              fprintf(stderr, "Error removing %s: %s\n", tmp, strerror(errno));
     174             :            }
     175             :          }
     176         542 :          tor_free(tmp);
     177         542 :     } SMARTLIST_FOREACH_END(cp);
     178         696 :     SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
     179         154 :     smartlist_free(elements);
     180             :   }
     181         156 :   if (rmdir(dir))
     182           0 :     fprintf(stderr, "Error removing directory %s: %s\n", dir, strerror(errno));
     183         156 : }
     184             : 
     185             : /** Remove all files stored under the temporary directory, and the directory
     186             :  * itself.  Called by atexit(). */
     187             : static void
     188         937 : remove_directory(void)
     189             : {
     190         937 :   if (getpid() != temp_dir_setup_in_pid) {
     191             :     /* Only clean out the tempdir when the main process is exiting. */
     192             :     return;
     193             :   }
     194             : 
     195           9 :   rm_rf(temp_dir);
     196             : }
     197             : 
     198             : static void *
     199          80 : passthrough_test_setup(const struct testcase_t *testcase)
     200             : {
     201             :   /* Make sure the passthrough doesn't unintentionally fail or skip tests */
     202          80 :   tor_assert(testcase->setup_data);
     203          80 :   tor_assert(testcase->setup_data != (void*)TT_SKIP);
     204          80 :   return testcase->setup_data;
     205             : }
     206             : static int
     207          80 : passthrough_test_cleanup(const struct testcase_t *testcase, void *ptr)
     208             : {
     209          80 :   (void)testcase;
     210          80 :   (void)ptr;
     211          80 :   return 1;
     212             : }
     213             : 
     214             : static void *
     215          18 : ed25519_testcase_setup(const struct testcase_t *testcase)
     216             : {
     217          18 :   crypto_ed25519_testing_force_impl(testcase->setup_data);
     218          18 :   return testcase->setup_data;
     219             : }
     220             : static int
     221          18 : ed25519_testcase_cleanup(const struct testcase_t *testcase, void *ptr)
     222             : {
     223          18 :   (void)testcase;
     224          18 :   (void)ptr;
     225          18 :   crypto_ed25519_testing_restore_impl();
     226          18 :   return 1;
     227             : }
     228             : const struct testcase_setup_t ed25519_test_setup = {
     229             :   ed25519_testcase_setup, ed25519_testcase_cleanup
     230             : };
     231             : 
     232             : const struct testcase_setup_t passthrough_setup = {
     233             :   passthrough_test_setup, passthrough_test_cleanup
     234             : };
     235             : 
     236             : static void
     237           0 : an_assertion_failed(void)
     238             : {
     239           0 :   tinytest_set_test_failed_();
     240           0 : }
     241             : 
     242             : void tinytest_prefork(void);
     243             : void tinytest_postfork(void);
     244             : void
     245         927 : tinytest_prefork(void)
     246             : {
     247         927 :   free_pregenerated_keys();
     248         927 :   subsystems_prefork();
     249         927 : }
     250             : void
     251        1854 : tinytest_postfork(void)
     252             : {
     253        1854 :   subsystems_postfork();
     254        1854 :   init_pregenerated_keys();
     255        1854 : }
     256             : 
     257             : static void
     258           0 : log_callback_failure(int severity, log_domain_mask_t domain, const char *msg)
     259             : {
     260           0 :   (void)msg;
     261           0 :   if (severity == LOG_ERR || (domain & LD_BUG)) {
     262           0 :     tinytest_set_test_failed_();
     263             :   }
     264           0 : }
     265             : 
     266             : /** Main entry point for unit test code: parse the command line, and run
     267             :  * some unit tests. */
     268             : int
     269           9 : main(int c, const char **v)
     270             : {
     271           9 :   or_options_t *options;
     272           9 :   char *errmsg = NULL;
     273           9 :   int i, i_out;
     274           9 :   int loglevel = LOG_ERR;
     275           9 :   int accel_crypto = 0;
     276             : 
     277           9 :   subsystems_init();
     278             : 
     279           9 :   options = options_new();
     280             : 
     281           9 :   struct tor_libevent_cfg_t cfg;
     282           9 :   memset(&cfg, 0, sizeof(cfg));
     283           9 :   tor_libevent_initialize(&cfg);
     284             : 
     285           9 :   control_initialize_event_queue();
     286             : 
     287             :   /* Don't add default logs; the tests manage their own. */
     288           9 :   quiet_level = QUIET_SILENT;
     289             : 
     290           9 :   unsigned num=1, den=1;
     291             : 
     292          17 :   for (i_out = i = 1; i < c; ++i) {
     293           8 :     if (!strcmp(v[i], "--warn")) {
     294             :       loglevel = LOG_WARN;
     295           8 :     } else if (!strcmp(v[i], "--notice")) {
     296             :       loglevel = LOG_NOTICE;
     297           8 :     } else if (!strcmp(v[i], "--info")) {
     298             :       loglevel = LOG_INFO;
     299           8 :     } else if (!strcmp(v[i], "--debug")) {
     300             :       loglevel = LOG_DEBUG;
     301           8 :     } else if (!strcmp(v[i], "--accel")) {
     302             :       accel_crypto = 1;
     303           8 :     } else if (!strcmp(v[i], "--fraction")) {
     304           8 :       if (i+1 == c) {
     305           0 :         printf("--fraction needs an argument.\n");
     306           0 :         return 1;
     307             :       }
     308           8 :       const char *fracstr = v[++i];
     309           8 :       char ch;
     310           8 :       if (sscanf(fracstr, "%u/%u%c", &num, &den, &ch) != 2) {
     311           0 :         printf("--fraction expects a fraction as an input.\n");
     312             :       }
     313           8 :       if (den == 0 || num == 0 || num > den) {
     314           8 :         printf("--fraction expects a valid fraction as an input.\n");
     315             :       }
     316             :     } else {
     317           0 :       v[i_out++] = v[i];
     318             :     }
     319             :   }
     320           9 :   c = i_out;
     321             : 
     322             :   {
     323             :     /* setup logs to stdout */
     324           9 :     log_severity_list_t s;
     325           9 :     memset(&s, 0, sizeof(s));
     326           9 :     set_log_severity_config(loglevel, LOG_ERR, &s);
     327             :     /* ALWAYS log bug warnings. */
     328           9 :     s.masks[SEVERITY_MASK_IDX(LOG_WARN)] |= LD_BUG;
     329           9 :     add_stream_log(&s, "", fileno(stdout));
     330             :   }
     331             :   {
     332             :     /* Setup logs that cause failure. */
     333           9 :     log_severity_list_t s;
     334           9 :     memset(&s, 0, sizeof(s));
     335           9 :     set_log_severity_config(LOG_ERR, LOG_ERR, &s);
     336           9 :     s.masks[SEVERITY_MASK_IDX(LOG_WARN)] |= LD_BUG;
     337           9 :     add_callback_log(&s, log_callback_failure);
     338             :   }
     339           9 :   flush_log_messages_from_startup();
     340           9 :   init_protocol_warning_severity_level();
     341             : 
     342           9 :   options->command = CMD_RUN_UNITTESTS;
     343           9 :   if (crypto_global_init(accel_crypto, NULL, NULL)) {
     344           0 :     printf("Can't initialize crypto subsystem; exiting.\n");
     345           0 :     return 1;
     346             :   }
     347           9 :   if (crypto_seed_rng() < 0) {
     348           0 :     printf("Couldn't seed RNG; exiting.\n");
     349           0 :     return 1;
     350             :   }
     351           9 :   rep_hist_init();
     352           9 :   bwhist_init();
     353           9 :   setup_directory();
     354           9 :   initialize_mainloop_events();
     355           9 :   options_init(options);
     356           9 :   options->DataDirectory = tor_strdup(temp_dir);
     357           9 :   options->DataDirectory_option = tor_strdup(temp_dir);
     358           9 :   tor_asprintf(&options->KeyDirectory, "%s"PATH_SEPARATOR"keys",
     359             :                options->DataDirectory);
     360           9 :   options->CacheDirectory = tor_strdup(temp_dir);
     361           9 :   options->EntryStatistics = 1;
     362           9 :   if (set_options(options, &errmsg) < 0) {
     363           0 :     printf("Failed to set initial options: %s\n", errmsg);
     364           0 :     tor_free(errmsg);
     365           0 :     return 1;
     366             :   }
     367             : 
     368           9 :   tor_set_failed_assertion_callback(an_assertion_failed);
     369             : 
     370           9 :   init_pregenerated_keys();
     371             : 
     372           9 :   channelpadding_new_consensus_params(NULL);
     373             : 
     374           9 :   predicted_ports_init();
     375             : 
     376           9 :   atexit(remove_directory);
     377             : 
     378             :   /* Look for TOR_SKIP_TESTCASES: a space-separated list of tests to skip. */
     379           9 :   const char *skip_tests = getenv("TOR_SKIP_TESTCASES");
     380           9 :   if (skip_tests) {
     381           0 :     smartlist_t *skip = smartlist_new();
     382           0 :     smartlist_split_string(skip, skip_tests, NULL,
     383             :                            SPLIT_IGNORE_BLANK, -1);
     384           0 :     int n = 0;
     385           0 :     SMARTLIST_FOREACH_BEGIN(skip, char *, cp) {
     386           0 :       n += tinytest_skip(testgroups, cp);
     387           0 :       tor_free(cp);
     388           0 :     } SMARTLIST_FOREACH_END(cp);
     389           0 :     printf("Skipping %d testcases.\n", n);
     390           0 :     smartlist_free(skip);
     391             :   }
     392             : 
     393           9 :   if (den != 1) {
     394             :     // count the tests. Linear but fast.
     395             :     unsigned n_tests = 0;
     396             :     struct testgroup_t *tg;
     397             :     struct testcase_t *tc;
     398         944 :     for (tg = testgroups; tg->prefix != NULL; ++tg) {
     399       12840 :       for (tc = tg->cases; tc->name != NULL; ++tc) {
     400       11904 :         ++n_tests;
     401             :       }
     402             :     }
     403             :     // Which tests should we run?  This can give iffy results if den is huge
     404             :     // but it doesn't actually matter in practice.
     405           8 :     unsigned tests_per_chunk = CEIL_DIV(n_tests, den);
     406           8 :     unsigned start_at = (num-1) * tests_per_chunk;
     407             : 
     408             :     // Skip the tests that are outside of the range.
     409           8 :     unsigned idx = 0;
     410         944 :     for (tg = testgroups; tg->prefix != NULL; ++tg) {
     411       12840 :       for (tc = tg->cases; tc->name != NULL; ++tc) {
     412       11904 :         if (idx < start_at || idx >= start_at + tests_per_chunk) {
     413       10416 :           tc->flags |= TT_SKIP;
     414             :         }
     415       11904 :         ++idx;
     416             :       }
     417             :     }
     418             :   }
     419             : 
     420           9 :   int have_failed = (tinytest_main(c, v, testgroups) != 0);
     421             : 
     422           9 :   free_pregenerated_keys();
     423             : 
     424           9 :   if (have_failed)
     425             :     return 1;
     426             :   else
     427           9 :     return 0;
     428             : }

Generated by: LCOV version 1.14