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

[tor-commits] [tor/release-0.2.3] add a unit test to expose bug 7192



commit 4c8b58f9005b7a187878273b8ff0f8ec23e86326
Author: Roger Dingledine <arma@xxxxxxxxxxxxxx>
Date:   Mon Oct 22 17:09:43 2012 -0400

    add a unit test to expose bug 7192
---
 src/or/policies.c |   27 +++++++++++++++++++++++++++
 src/or/policies.h |    1 +
 src/test/test.c   |    4 ++++
 3 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/src/or/policies.c b/src/or/policies.c
index 6e98421..486c264 100644
--- a/src/or/policies.c
+++ b/src/or/policies.c
@@ -1413,6 +1413,33 @@ parse_short_policy(const char *summary)
   return result;
 }
 
+/** Write <b>policy</b> back out into a string. Used only for unit tests
+ * currently. */
+const char *
+write_short_policy(const short_policy_t *policy)
+{
+  int i;
+  char *answer;
+  smartlist_t *sl = smartlist_new();
+
+  smartlist_add_asprintf(sl, "%s", policy->is_accept ? "accept " : "reject ");
+
+  for(i=0; i < policy->n_entries; i++) {
+    const short_policy_entry_t *e = &policy->entries[i];
+    if (e->min_port == e->max_port) {
+      smartlist_add_asprintf(sl, "%d", e->min_port);
+    } else {
+      smartlist_add_asprintf(sl, "%d-%d", e->min_port, e->max_port);
+    }
+    if (i < policy->n_entries-1)
+      smartlist_add(sl, tor_strdup(","));
+  }
+  answer = smartlist_join_strings(sl, "", 0, NULL);
+  SMARTLIST_FOREACH(sl, char *, a, tor_free(a));
+  smartlist_free(sl);
+  return answer;
+}
+
 /** Release all storage held in <b>policy</b>. */
 void
 short_policy_free(short_policy_t *policy)
diff --git a/src/or/policies.h b/src/or/policies.h
index 31f3f06..b385d8e 100644
--- a/src/or/policies.h
+++ b/src/or/policies.h
@@ -61,6 +61,7 @@ void policies_free_all(void);
 char *policy_summarize(smartlist_t *policy);
 
 short_policy_t *parse_short_policy(const char *summary);
+const char *write_short_policy(const short_policy_t *policy);
 void short_policy_free(short_policy_t *policy);
 int short_policy_is_reject_star(const short_policy_t *policy);
 addr_policy_result_t compare_tor_addr_to_short_policy(
diff --git a/src/test/test.c b/src/test/test.c
index 6bf2d28..9b510d2 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1014,6 +1014,7 @@ test_policy_summary_helper(const char *policy_str,
   config_line_t line;
   smartlist_t *policy = smartlist_new();
   char *summary = NULL;
+  const char *summary_after;
   int r;
   short_policy_t *short_policy = NULL;
 
@@ -1030,8 +1031,11 @@ test_policy_summary_helper(const char *policy_str,
 
   short_policy = parse_short_policy(summary);
   tt_assert(short_policy);
+  summary_after = write_short_policy(short_policy);
+  test_streq(summary, summary_after);
 
  done:
+  tor_free(summary_after);
   tor_free(summary);
   if (policy)
     addr_policy_list_free(policy);



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits