[tor-commits] [tor/master] Add test for random-int-in-range

nickm at torproject.org nickm at torproject.org
Thu Apr 23 13:21:49 UTC 2015


commit e48ad353a31f3ee376f7914563d960916ad6aecd
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Apr 21 11:41:31 2015 -0400

    Add test for random-int-in-range
---
 src/test/test_crypto.c |   25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/src/test/test_crypto.c b/src/test/test_crypto.c
index e9fb8bf..9158390 100644
--- a/src/test/test_crypto.c
+++ b/src/test/test_crypto.c
@@ -105,6 +105,30 @@ test_crypto_rng(void *arg)
   ;
 }
 
+static void
+test_crypto_rng_range(void *arg)
+{
+  int got_smallest = 0, got_largest = 0;
+  int i;
+
+  (void)arg;
+  for (i = 0; i < 1000; ++i) {
+    int x = crypto_rand_int_range(5,9);
+    tt_int_op(x, OP_GE, 5);
+    tt_int_op(x, OP_LT, 9);
+    if (x == 5)
+      got_smallest = 1;
+    if (x == 8)
+      got_largest = 1;
+  }
+
+  /* These fail with probability 1/10^603. */
+  tt_assert(got_smallest);
+  tt_assert(got_largest);
+ done:
+  ;
+}
+
 /** Run unit tests for our AES functionality */
 static void
 test_crypto_aes(void *arg)
@@ -1605,6 +1629,7 @@ test_crypto_siphash(void *arg)
 struct testcase_t crypto_tests[] = {
   CRYPTO_LEGACY(formats),
   CRYPTO_LEGACY(rng),
+  { "rng_range", test_crypto_rng_range, 0, NULL, NULL },
   { "aes_AES", test_crypto_aes, TT_FORK, &passthrough_setup, (void*)"aes" },
   { "aes_EVP", test_crypto_aes, TT_FORK, &passthrough_setup, (void*)"evp" },
   CRYPTO_LEGACY(sha),





More information about the tor-commits mailing list