[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r11167: Long overdue test.c refactoring: add --warn/--notice/--info/ (in tor/trunk: . src/or)



Author: nickm
Date: 2007-08-18 15:39:14 -0400 (Sat, 18 Aug 2007)
New Revision: 11167

Modified:
   tor/trunk/
   tor/trunk/ChangeLog
   tor/trunk/src/or/test.c
Log:
 r14661@catbus:  nickm | 2007-08-18 15:38:08 -0400
 Long overdue test.c refactoring: add --warn/--notice/--info/--debug command line options to set logging levels, and let the user specify which tests to run from the commmand line.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r14661] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog	2007-08-18 18:20:42 UTC (rev 11166)
+++ tor/trunk/ChangeLog	2007-08-18 19:39:14 UTC (rev 11167)
@@ -38,6 +38,12 @@
     - Implement options to allow the controller to pick a new location for
       the cookie authentication file, and to make it group-readable.
 
+  o Minor features (unit testing):
+    - Add command-line arguments to unit-test executable so that we can
+      invoke any chosen test from the command line rather than having to
+      run the whole test suite at once; and so that we can turn on logging
+      for the unit tests.
+
   o Minor bugfixes (other):
     - If we require CookieAuthentication but we fail to write the
       cookie file, we would warn but not exit, and end up in a state

Modified: tor/trunk/src/or/test.c
===================================================================
--- tor/trunk/src/or/test.c	2007-08-18 18:20:42 UTC (rev 11166)
+++ tor/trunk/src/or/test.c	2007-08-18 19:39:14 UTC (rev 11167)
@@ -1079,7 +1079,7 @@
   STMT_END
 
 static void
-test_ip6_helpers(void)
+test_util_ip6_helpers(void)
 {
   char buf[TOR_ADDR_BUF_LEN], bug[TOR_ADDR_BUF_LEN];
   struct in6_addr a1, a2;
@@ -1271,14 +1271,16 @@
   /* get interface addresses */
   r = get_interface_address6(0, AF_INET, &t1);
   i = get_interface_address6(0, AF_INET6, &t2);
+#if 0
   tor_inet_ntop(AF_INET, &t1.sa.sin_addr, buf, sizeof(buf));
   printf("\nv4 address: %s  (family=%i)", buf, IN_FAMILY(&t1));
   tor_inet_ntop(AF_INET6, &t2.sa6.sin6_addr, buf, sizeof(buf));
   printf("\nv6 address: %s  (family=%i)", buf, IN_FAMILY(&t2));
+#endif
 }
 
 static void
-test_smartlist(void)
+test_util_smartlist(void)
 {
   smartlist_t *sl;
   char *cp;
@@ -1545,7 +1547,7 @@
 }
 
 static void
-test_bitarray(void)
+test_util_bitarray(void)
 {
   bitarray_t *ba;
   int i, j, ok=1;
@@ -1629,7 +1631,7 @@
 }
 
 static void
-test_threads(void)
+test_util_threads(void)
 {
   char *s1, *s2;
   int done = 0, timedout = 0;
@@ -1694,7 +1696,7 @@
 }
 
 static void
-test_pqueue(void)
+test_util_pqueue(void)
 {
   smartlist_t *sl;
   int (*cmp)(const void *, const void*);
@@ -1748,7 +1750,7 @@
 }
 
 static void
-test_gzip(void)
+test_util_gzip(void)
 {
   char *buf1, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
   const char *ccp2;
@@ -1854,7 +1856,7 @@
 }
 
 static void
-test_strmap(void)
+test_util_strmap(void)
 {
   strmap_t *map;
   strmap_iter_t *iter;
@@ -1933,7 +1935,7 @@
 }
 
 static void
-test_mmap(void)
+test_util_mmap(void)
 {
   char *fname1 = tor_strdup(get_fname("mapped_1"));
   char *fname2 = tor_strdup(get_fname("mapped_2"));
@@ -1991,7 +1993,7 @@
 }
 
 static void
-test_control_formats(void)
+test_util_control_formats(void)
 {
   char *out;
   const char *inp =
@@ -2391,7 +2393,7 @@
 }
 
 static void
-test_dirvote_helpers(void)
+test_util_dirvote_helpers(void)
 {
   smartlist_t *sl = smartlist_create();
   int a=12,b=24,c=25,d=60,e=77;
@@ -2443,8 +2445,6 @@
   char *v1_text, *v2_text, *v3_text, *consensus_text, *cp;
   smartlist_t *votes = smartlist_create();
 
-  add_stream_log(LOG_NOTICE, LOG_ERR, "", stdout);//XXXX020 remove me.
-
   /* Parse certificates and keys. */
   cert1 = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
   test_assert(cert1);
@@ -3066,7 +3066,7 @@
 }
 
 static void
-test_mempool(void)
+test_util_mempool(void)
 {
   mp_pool_t *pool;
   smartlist_t *allocated;
@@ -3113,13 +3113,105 @@
   smartlist_free(allocated);
 }
 
+#define ENT(x) { #x, test_ ## x, 0, 0 }
+#define SUBENT(x,y) { #x "/" #y, test_ ## x ## _ ## y, 1, 0 }
+
+static struct {
+  const char *test_name;
+  void (*test_fn)(void);
+  int is_subent;
+  int selected;
+} test_array[] = {
+  ENT(buffers),
+  ENT(crypto),
+  SUBENT(crypto, dh),
+  SUBENT(crypto, s2k),
+  ENT(util),
+  SUBENT(util, ip6_helpers),
+  SUBENT(util, gzip),
+  SUBENT(util, smartlist),
+  SUBENT(util, bitarray),
+  SUBENT(util, mempool),
+  SUBENT(util, strmap),
+  SUBENT(util, control_formats),
+  SUBENT(util, pqueue),
+  SUBENT(util, mmap),
+  SUBENT(util, threads),
+  SUBENT(util, dirvote_helpers),
+  ENT(onion_handshake),
+  ENT(dir_format),
+  ENT(v3_networkstatus),
+  ENT(policies),
+  ENT(rend_fns),
+  { NULL, NULL, 0, 0 },
+};
+
+static void syntax(void) ATTR_NORETURN;
+static void
+syntax(void)
+{
+  int i;
+  printf("Syntax:\n"
+         "  test [-v|--verbose] [--warn|--notice|--info|--debug]\n"
+         "       [testname...]\n"
+         "Recognized tests are:\n");
+  for (i = 0; test_array[i].test_name; ++i) {
+    printf("   %s\n", test_array[i].test_name);
+  }
+
+  exit(0);
+}
+
 int
 main(int c, char**v)
 {
   or_options_t *options = options_new();
   char *errmsg = NULL;
-  (void) c;
-  (void) v;
+  int i;
+  int verbose = 0, any_selected = 0;
+  int loglevel = LOG_ERR;
+
+  for (i = 1; i < c; ++i) {
+    if (!strcmp(v[i], "-v") || !strcmp(v[i], "--verbose"))
+      verbose++;
+    else if (!strcmp(v[i], "--warn"))
+      loglevel = LOG_WARN;
+    else if (!strcmp(v[i], "--notice"))
+      loglevel = LOG_NOTICE;
+    else if (!strcmp(v[i], "--info"))
+      loglevel = LOG_INFO;
+    else if (!strcmp(v[i], "--debug"))
+      loglevel = LOG_DEBUG;
+    else if (!strcmp(v[i], "--help") || !strcmp(v[i], "-h") || v[i][0] == '-')
+      syntax();
+    else {
+      int j, found=0;
+      for (j = 0; test_array[j].test_name; ++j) {
+        if (!strcmp(v[i], test_array[j].test_name) ||
+            (test_array[j].is_subent &&
+             !strcmpstart(test_array[j].test_name, v[i]) &&
+             test_array[j].test_name[strlen(v[i])] == '/') ||
+            (v[i][0] == '=' && !strcmp(v[i]+1, test_array[j].test_name))) {
+          test_array[j].selected = 1;
+          any_selected = 1;
+          found = 1;
+        }
+      }
+      if (!found) {
+        printf("Unknown test: %s\n", v[i]);
+        syntax();
+      }
+    }
+  }
+
+  if (!any_selected) {
+    for (i = 0; test_array[i].test_name; ++i) {
+      test_array[i].selected = 1;
+    }
+  }
+
+  add_stream_log(loglevel, LOG_ERR, "", stdout);
+
   options->command = CMD_RUN_UNITTESTS;
   rep_hist_init();
   network_init();
@@ -3143,47 +3235,16 @@
 
   printf("Running Tor unit tests on %s\n", get_uname());
 
-  puts("========================== Buffers =========================");
-  test_buffers();
-  puts("\n========================== Crypto ==========================");
-  // add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout);
-  test_crypto();
-  test_crypto_dh();
-  test_crypto_s2k();
-  puts("\n========================= Util ============================"
-       "\n--IPv6");
-  test_ip6_helpers();
-  puts("\n--gzip");
-  test_gzip();
-  puts("\n--util");
-  test_util();
-  puts("\n--smartlist");
-  test_smartlist();
-  puts("\n--bitarray");
-  test_bitarray();
-  puts("\n--mempool");
-  test_mempool();
-  puts("\n--strmap");
-  test_strmap();
-  puts("\n--control formats");
-  test_control_formats();
-  puts("\n--pqueue");
-  test_pqueue();
-  puts("\n--mmap");
-  test_mmap();
-  puts("\n--threads");
-  test_threads();
-  puts("\n--dirvote-helpers");
-  test_dirvote_helpers();
-  puts("\n========================= Onion Skins =====================");
-  test_onion_handshake();
-  puts("\n========================= Directory Formats ===============");
-  test_dir_format();
-  test_v3_networkstatus();
-  puts("\n========================= Policies ===================");
-  test_policies();
-  puts("\n========================= Rendezvous functionality ========");
-  test_rend_fns();
+  for (i = 0; test_array[i].test_name; ++i) {
+    if (!test_array[i].selected)
+      continue;
+    if (!test_array[i].is_subent) {
+      printf("\n============================== %s\n",test_array[i].test_name);
+    } else if (test_array[i].is_subent && verbose) {
+      printf("\n%s", test_array[i].test_name);
+    }
+    test_array[i].test_fn();
+  }
   puts("");
 
   if (have_failed)