]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Unit test for tor_weak_random_range
authorNick Mathewson <nickm@torproject.org>
Fri, 8 Feb 2013 21:46:35 +0000 (16:46 -0500)
committerNick Mathewson <nickm@torproject.org>
Fri, 8 Feb 2013 21:46:35 +0000 (16:46 -0500)
src/test/test_util.c

index bed33fac2504422a6d9bc7fe9f7c2d721d0a2757..a66941b00400d742c0fce070d497da216b31ab07 100644 (file)
@@ -3241,6 +3241,34 @@ test_util_set_env_var_in_sl(void *ptr)
   smartlist_free(expected_resulting_env_vars);
 }
 
+static void
+test_util_weak_random(void *arg)
+{
+  int i, j, n[16];
+  tor_weak_rng_t rng;
+  (void) arg;
+
+  tor_init_weak_random(&rng, (unsigned)time(NULL));
+
+  for (i = 1; i <= 256; ++i) {
+    for (j=0;j<100;++j) {
+      int r = tor_weak_random_range(&rng, i);
+      tt_int_op(0, <=, r);
+      tt_int_op(r, <, i);
+    }
+  }
+
+  memset(n,0,sizeof(n));
+  for (j=0;j<8192;++j) {
+    n[tor_weak_random_range(&rng, 16)]++;
+  }
+
+  for (i=0;i<16;++i)
+    tt_int_op(n[i], >, 0);
+ done:
+  ;
+}
+
 static void
 test_util_mathlog(void *arg)
 {
@@ -3312,6 +3340,7 @@ struct testcase_t util_tests[] = {
   UTIL_TEST(read_file_eof_two_loops, 0),
   UTIL_TEST(read_file_eof_zero_bytes, 0),
   UTIL_TEST(mathlog, 0),
+  UTIL_TEST(weak_random, 0),
   END_OF_TESTCASES
 };