You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

209 lines
4.9 KiB

  1. #define BOOST_TEST_DYN_LINK
  2. #define BOOST_TEST_NO_MAIN
  3. // Disable this code for gcc 4.8 and lower
  4. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ > 8) || !__GNUC__
  5. #ifdef HAVE_CONFIG_H
  6. #include "config.h"
  7. #endif
  8. #include <boost/test/unit_test.hpp>
  9. #include <boost/assign/std/map.hpp>
  10. #include <boost/accumulators/statistics/median.hpp>
  11. #include <boost/accumulators/statistics/mean.hpp>
  12. #include <boost/accumulators/accumulators.hpp>
  13. #include <boost/accumulators/statistics.hpp>
  14. #include "arguments.hh"
  15. #include "dns_random.hh"
  16. #include "namespaces.hh"
  17. using namespace boost;
  18. using namespace boost::accumulators;
  19. typedef accumulator_set<
  20. double
  21. , stats<boost::accumulators::tag::median(with_p_square_quantile),
  22. boost::accumulators::tag::mean(immediate)
  23. >
  24. > acc_t;
  25. BOOST_AUTO_TEST_SUITE(test_dns_random_hh)
  26. BOOST_AUTO_TEST_CASE(test_dns_random_auto_average) {
  27. ::arg().set("rng")="auto";
  28. ::arg().set("entropy-source")="/dev/urandom";
  29. dns_random_init("", true);
  30. acc_t acc;
  31. for(unsigned int n=0; n < 100000; ++n) {
  32. acc(dns_random(100000)/100000.0);
  33. }
  34. BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
  35. BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
  36. // please add covariance tests, chi-square, Kolmogorov-Smirnov
  37. }
  38. BOOST_AUTO_TEST_CASE(test_dns_random_urandom_average) {
  39. ::arg().set("rng")="urandom";
  40. ::arg().set("entropy-source")="/dev/urandom";
  41. dns_random_init("", true);
  42. acc_t acc;
  43. for(unsigned int n=0; n < 100000; ++n) {
  44. acc(dns_random(100000)/100000.0);
  45. }
  46. BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
  47. BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
  48. // please add covariance tests, chi-square, Kolmogorov-Smirnov
  49. }
  50. BOOST_AUTO_TEST_CASE(test_dns_random_garbage) {
  51. ::arg().set("rng")="garbage";
  52. ::arg().set("entropy-source")="/dev/urandom";
  53. BOOST_CHECK_THROW(dns_random_init("", true), std::runtime_error);
  54. }
  55. BOOST_AUTO_TEST_CASE(test_dns_random_upper_bound) {
  56. ::arg().set("rng")="auto";
  57. ::arg().set("entropy-source")="/dev/urandom";
  58. dns_random_init("", true);
  59. map<int, bool> seen;
  60. for(unsigned int n=0; n < 100000; ++n) {
  61. seen[dns_random(10)] = true;
  62. }
  63. BOOST_CHECK_EQUAL(seen[0], true);
  64. BOOST_CHECK_EQUAL(seen[1], true);
  65. BOOST_CHECK_EQUAL(seen[2], true);
  66. BOOST_CHECK_EQUAL(seen[3], true);
  67. BOOST_CHECK_EQUAL(seen[4], true);
  68. BOOST_CHECK_EQUAL(seen[5], true);
  69. BOOST_CHECK_EQUAL(seen[6], true);
  70. BOOST_CHECK_EQUAL(seen[7], true);
  71. BOOST_CHECK_EQUAL(seen[8], true);
  72. BOOST_CHECK_EQUAL(seen[9], true);
  73. BOOST_CHECK_EQUAL(seen[10], false);
  74. }
  75. #if defined(HAVE_GETRANDOM)
  76. BOOST_AUTO_TEST_CASE(test_dns_random_getrandom_average) {
  77. ::arg().set("rng")="getrandom";
  78. ::arg().set("entropy-source")="/dev/urandom";
  79. dns_random_init("", true);
  80. acc_t acc;
  81. for(unsigned int n=0; n < 100000; ++n) {
  82. acc(dns_random(100000)/100000.0);
  83. }
  84. BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
  85. BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
  86. // please add covariance tests, chi-square, Kolmogorov-Smirnov
  87. }
  88. #endif
  89. #if defined(HAVE_ARC4RANDOM)
  90. BOOST_AUTO_TEST_CASE(test_dns_random_arc4random_average) {
  91. ::arg().set("rng")="arc4random";
  92. ::arg().set("entropy-source")="/dev/urandom";
  93. dns_random_init("", true);
  94. acc_t acc;
  95. for(unsigned int n=0; n < 100000; ++n) {
  96. acc(dns_random(100000)/100000.0);
  97. }
  98. BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
  99. BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
  100. // please add covariance tests, chi-square, Kolmogorov-Smirnov
  101. }
  102. #endif
  103. #if defined(HAVE_RANDOMBYTES_STIR)
  104. BOOST_AUTO_TEST_CASE(test_dns_random_sodium_average) {
  105. ::arg().set("rng")="sodium";
  106. ::arg().set("entropy-source")="/dev/urandom";
  107. dns_random_init("", true);
  108. acc_t acc;
  109. for(unsigned int n=0; n < 100000; ++n) {
  110. acc(dns_random(100000)/100000.0);
  111. }
  112. BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
  113. BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
  114. // please add covariance tests, chi-square, Kolmogorov-Smirnov
  115. }
  116. #endif
  117. #if defined(HAVE_RAND_BYTES)
  118. BOOST_AUTO_TEST_CASE(test_dns_random_openssl_average) {
  119. ::arg().set("rng")="openssl";
  120. ::arg().set("entropy-source")="/dev/urandom";
  121. dns_random_init("", true);
  122. acc_t acc;
  123. for(unsigned int n=0; n < 100000; ++n) {
  124. acc(dns_random(100000)/100000.0);
  125. }
  126. BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
  127. BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
  128. // please add covariance tests, chi-square, Kolmogorov-Smirnov
  129. }
  130. #endif
  131. #if defined(HAVE_KISS_RNG)
  132. BOOST_AUTO_TEST_CASE(test_dns_random_kiss_average) {
  133. ::arg().set("rng")="kiss";
  134. ::arg().set("entropy-source")="/dev/urandom";
  135. dns_random_init("", true);
  136. acc_t acc;
  137. for(unsigned int n=0; n < 100000; ++n) {
  138. acc(dns_random(100000)/100000.0);
  139. }
  140. BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
  141. BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
  142. // please add covariance tests, chi-square, Kolmogorov-Smirnov
  143. }
  144. #endif
  145. BOOST_AUTO_TEST_SUITE_END()
  146. #endif