[or-cvs] r11121: Add unit tests for median functions; enforce sensible ranges (in tor/trunk: . src/or)

nickm at seul.org nickm at seul.org
Wed Aug 15 15:38:59 UTC 2007


Author: nickm
Date: 2007-08-15 11:38:58 -0400 (Wed, 15 Aug 2007)
New Revision: 11121

Modified:
   tor/trunk/
   tor/trunk/src/or/config.c
   tor/trunk/src/or/dirserv.c
   tor/trunk/src/or/dirvote.c
   tor/trunk/src/or/or.h
   tor/trunk/src/or/routerparse.c
   tor/trunk/src/or/test.c
Log:
 r14032 at Kushana:  nickm | 2007-08-15 11:35:16 -0400
 Add unit tests for median functions; enforce sensible ranges for intervals and delays.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r14032] on c95137ef-5f19-0410-b913-86e773d04f59

Modified: tor/trunk/src/or/config.c
===================================================================
--- tor/trunk/src/or/config.c	2007-08-15 15:38:53 UTC (rev 11120)
+++ tor/trunk/src/or/config.c	2007-08-15 15:38:58 UTC (rev 11121)
@@ -3008,9 +3008,13 @@
     REJECT("V3AuthVoteDelay and V3AuthDistDelay must be no more than half "
            "V3AuthVotingInterval");
   }
-  if (options->V3AuthNIntervalsValid < 2) {
+  if (options->V3AuthVoteDelay < MIN_VOTE_SECONDS)
+    REJECT("V3AuthVoteDelay is way too low.");
+  if (options->V3AuthDistDelay < MIN_DIST_SECONDS)
+    REJECT("V3AuthDistDelay is way too low.");
+
+  if (options->V3AuthNIntervalsValid < 2)
     REJECT("V3AuthNIntervalsValid must be at least 2.");
-  }
 
   if (options->V3AuthVotingInterval < 300) {
     REJECT("V3AuthVotingInterval is insanely low.");

Modified: tor/trunk/src/or/dirserv.c
===================================================================
--- tor/trunk/src/or/dirserv.c	2007-08-15 15:38:53 UTC (rev 11120)
+++ tor/trunk/src/or/dirserv.c	2007-08-15 15:38:58 UTC (rev 11121)
@@ -1979,6 +1979,9 @@
     (timing.vote_interval * timing.n_intervals_valid);
   v3_out->vote_seconds = timing.vote_delay;
   v3_out->dist_seconds = timing.dist_delay;
+  tor_assert(v3_out->vote_seconds > 0);
+  tor_assert(v3_out->dist_seconds > 0);
+  tor_assert(timing.n_intervals_valid > 0);
 
   v3_out->client_versions = client_versions;
   v3_out->server_versions = server_versions;

Modified: tor/trunk/src/or/dirvote.c
===================================================================
--- tor/trunk/src/or/dirvote.c	2007-08-15 15:38:53 UTC (rev 11120)
+++ tor/trunk/src/or/dirvote.c	2007-08-15 15:38:58 UTC (rev 11121)
@@ -107,7 +107,7 @@
 }
 
 /** Given a list of one or more time_t*, return the (low) median. */
-static time_t
+/*static*/ time_t
 median_time(smartlist_t *times)
 {
   int idx;
@@ -118,7 +118,7 @@
 }
 
 /** Given a list of one or more int*, return the (low) median. */
-static int
+/*static*/ int
 median_int(smartlist_t *ints)
 {
   int idx;
@@ -365,6 +365,19 @@
     vote_seconds = median_int(votesec_list);
     dist_seconds = median_int(distsec_list);
 
+    /*
+    SMARTLIST_FOREACH(va_times, int*, i,
+                      printf("VA: %d\n", *i));
+    SMARTLIST_FOREACH(fu_times, int*, i,
+                      printf("FU: %d\n", *i));
+    printf("%d..%d\n", (int)valid_after, (int)valid_until);
+    */
+
+    tor_assert(valid_after+MIN_VOTE_INTERVAL <= fresh_until);
+    tor_assert(fresh_until+MIN_VOTE_INTERVAL <= valid_until);
+    tor_assert(vote_seconds >= MIN_VOTE_SECONDS);
+    tor_assert(dist_seconds >= MIN_DIST_SECONDS);
+
     for (j = 0; j < 2; ++j) {
       smartlist_t *lst =
         j ? combined_server_versions : combined_client_versions;
@@ -1234,7 +1247,7 @@
     goto err;
   }
   tor_assert(smartlist_len(vote->voters) == 1);
-  vi = smartlist_get(vote->voters, 0);
+  vi = get_voter(vote);
   tor_assert(vi->good_signature == 1);
   ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
   if (!ds || !(ds->type & V3_AUTHORITY)) {
@@ -1260,7 +1273,7 @@
       if (! memcmp(v->vote->cert->cache_info.identity_digest,
                    vote->cert->cache_info.identity_digest,
                    DIGEST_LEN)) {
-        networkstatus_voter_info_t *vi_old = smartlist_get(v->vote->voters, 0);
+        networkstatus_voter_info_t *vi_old = get_voter(v->vote);
         if (!memcmp(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
           /* Ah, it's the same vote. Not a problem. */
           log_info(LD_DIR, "Discarding a vote we already have.");

Modified: tor/trunk/src/or/or.h
===================================================================
--- tor/trunk/src/or/or.h	2007-08-15 15:38:53 UTC (rev 11120)
+++ tor/trunk/src/or/or.h	2007-08-15 15:38:58 UTC (rev 11121)
@@ -2824,6 +2824,13 @@
 
 /********************************* dirvote.c ************************/
 
+/* XXXX020 enforce */
+/* XXXX020 document in dir-spec.txt */
+/*DOCDOC*/
+#define MIN_VOTE_SECONDS 20
+#define MIN_DIST_SECONDS 20
+#define MIN_VOTE_INTERVAL 300
+
 void dirvote_free_all(void);
 
 /* vote manipulation */
@@ -2873,6 +2880,8 @@
 int dirvote_publish_consensus(void);
 
 #ifdef DIRVOTE_PRIVATE
+time_t median_time(smartlist_t *times);
+int median_int(smartlist_t *times);
 int networkstatus_check_voter_signature(networkstatus_vote_t *consensus,
                                         networkstatus_voter_info_t *voter,
                                         authority_cert_t *cert);

Modified: tor/trunk/src/or/routerparse.c
===================================================================
--- tor/trunk/src/or/routerparse.c	2007-08-15 15:38:53 UTC (rev 11120)
+++ tor/trunk/src/or/routerparse.c	2007-08-15 15:38:58 UTC (rev 11121)
@@ -1874,6 +1874,22 @@
     (int) tor_parse_long(tok->args[1], 10, 0, INT_MAX, &ok, NULL);
   if (!ok)
     goto err;
+  if (ns->valid_after + MIN_VOTE_INTERVAL > ns->fresh_until) {
+    log_warn(LD_DIR, "Vote/consensus freshness interval is too short");
+    goto err;
+  }
+  if (ns->valid_after + MIN_VOTE_INTERVAL*2 > ns->valid_until) {
+    log_warn(LD_DIR, "Vote/consensus liveness interval is too short");
+    goto err;
+  }
+  if (ns->vote_seconds < MIN_VOTE_SECONDS) {
+    log_warn(LD_DIR, "Vote seconds is too short");
+    goto err;
+  }
+  if (ns->dist_seconds < MIN_DIST_SECONDS) {
+    log_warn(LD_DIR, "Dist seconds is too short");
+    goto err;
+  }
 
   if ((tok = find_first_by_keyword(tokens, K_CLIENT_VERSIONS))) {
     ns->client_versions = tok->args[0];

Modified: tor/trunk/src/or/test.c
===================================================================
--- tor/trunk/src/or/test.c	2007-08-15 15:38:53 UTC (rev 11120)
+++ tor/trunk/src/or/test.c	2007-08-15 15:38:58 UTC (rev 11121)
@@ -2391,6 +2391,45 @@
 }
 
 static void
+test_dirvote_helpers(void)
+{
+  smartlist_t *sl = smartlist_create();
+  int a=12,b=24,c=25,d=60,e=77;
+  time_t v=99, w=150, x=700, y=1000, z=time(NULL);
+
+  test_assert(y<z);
+  smartlist_add(sl, &a);
+  test_eq(a, median_int(sl)); /* a */
+  smartlist_add(sl, &e);
+  smartlist_shuffle(sl);
+  test_eq(a, median_int(sl)); /* a,e */
+  smartlist_add(sl, &e);
+  smartlist_shuffle(sl);
+  test_eq(e, median_int(sl)); /* a,e,e */
+  smartlist_add(sl, &b);
+  test_eq(b, median_int(sl)); /* a,b,e,e */
+  smartlist_add(sl, &d);
+  smartlist_add(sl, &a);
+  smartlist_add(sl, &c);
+  smartlist_shuffle(sl);
+  test_eq(c, median_int(sl)); /* a,a,b,c,d,e,e */
+
+  smartlist_clear(sl);
+  smartlist_add(sl, &y);
+  test_eq(y, median_time(sl)); /*y*/
+  smartlist_add(sl, &w);
+  test_eq(w, median_time(sl)); /*w,y*/
+  smartlist_add(sl, &x);
+  test_eq(x, median_time(sl)); /*w,x,y*/
+  smartlist_add(sl, &v);
+  test_eq(w, median_time(sl)); /*v,w,x,y*/
+  smartlist_add(sl, &z);
+  test_eq(x, median_time(sl)); /*v,w,x,y,z*/
+
+  smartlist_free(sl);
+}
+
+static void
 test_v3_networkstatus(void)
 {
   authority_cert_t *cert1, *cert2, *cert3;
@@ -2433,9 +2472,9 @@
   vote = tor_malloc_zero(sizeof(networkstatus_vote_t));
   vote->is_vote = 1;
   vote->published = now;
-  vote->valid_after = now+100;
-  vote->fresh_until = now+200;
-  vote->valid_until = now+300;
+  vote->valid_after = now+1000;
+  vote->fresh_until = now+2000;
+  vote->valid_until = now+3000;
   vote->vote_seconds = 100;
   vote->dist_seconds = 200;
   vote->client_versions = tor_strdup("0.1.2.14,0.1.2.15");
@@ -2560,7 +2599,7 @@
   /* Generate second vote. It disagrees on some of the times,
    * and doesn't list versions, and knows some crazy flags */
   vote->published = now+1;
-  vote->fresh_until = now+205;
+  vote->fresh_until = now+3005;
   vote->dist_seconds = 300;
   authority_cert_free(vote->cert);
   vote->cert = authority_cert_dup(cert2);
@@ -2598,7 +2637,7 @@
 
   /* Generate the third vote. */
   vote->published = now;
-  vote->fresh_until = now+203;
+  vote->fresh_until = now+2003;
   vote->dist_seconds = 250;
   authority_cert_free(vote->cert);
   vote->cert = authority_cert_dup(cert3);
@@ -2639,9 +2678,9 @@
   /* Check consensus contents. */
   test_assert(!con->is_vote);
   test_eq(con->published, 0); /* this field only appears in votes. */
-  test_eq(con->valid_after, now+100);
-  test_eq(con->fresh_until, now+203); /* median */
-  test_eq(con->valid_until, now+300);
+  test_eq(con->valid_after, now+1000);
+  test_eq(con->fresh_until, now+2003); /* median */
+  test_eq(con->valid_until, now+3000);
   test_eq(con->vote_seconds, 100);
   test_eq(con->dist_seconds, 250); /* median */
   test_streq(con->client_versions, "0.1.2.14");
@@ -3130,6 +3169,8 @@
   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 ===============");



More information about the tor-commits mailing list