util: generate random bytes using OpenSSL instead of CryptoPP

Change-Id: I68bdac565242432fa53a1f113d6f395bf3c6e982
Refs: #3010, #2949
diff --git a/tests/unit-tests/util/random.t.cpp b/tests/unit-tests/util/random.t.cpp
index d56d84b..18af4ff 100644
--- a/tests/unit-tests/util/random.t.cpp
+++ b/tests/unit-tests/util/random.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2016 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -23,13 +23,15 @@
 
 #include "boost-test.hpp"
 #include <boost/mpl/vector.hpp>
+#include "security/detail/openssl.hpp"
 
 #include <cmath>
 
 namespace ndn {
 namespace tests {
 
-BOOST_AUTO_TEST_SUITE(UtilRandom)
+BOOST_AUTO_TEST_SUITE(Util)
+BOOST_AUTO_TEST_SUITE(TestRandom)
 
 class PseudoRandomWord32
 {
@@ -76,11 +78,31 @@
                            SecureRandomWord32,
                            SecureRandomWord64> RandomGenerators;
 
-BOOST_AUTO_TEST_CASE_TEMPLATE(GoodnessOfFit, RandomGenerator, RandomGenerators)
+
+static double
+getDeviation(const std::vector<uint32_t>& counts, size_t size)
 {
   // Kolmogorov-Smirnov Goodness-of-Fit Test
   // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm
 
+  std::vector<double> edf(counts.size(), 0.0);
+  double probability = 0.0;
+  for (size_t i = 0; i < counts.size(); i++) {
+    probability += 1.0 * counts[i] / size;
+    edf[i] = probability;
+  }
+
+  double t = 0.0;
+  for (size_t i = 0; i < counts.size(); i++) {
+    t = std::max(t, std::abs(edf[i] - (i * 1.0 / counts.size())));
+  }
+
+  return t;
+}
+
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(GoodnessOfFit, RandomGenerator, RandomGenerators)
+{
   const size_t MAX_BINS = 32;
   const uint32_t MAX_ITERATIONS = 35;
 
@@ -90,24 +112,98 @@
     counts[RandomGenerator::generate() % MAX_BINS]++;
   }
 
-  std::vector<double> edf(MAX_BINS, 0.0);
-  double probability = 0.0;
-  for (size_t i = 0; i < MAX_BINS; i++) {
-    probability += 1.0 * counts[i] / MAX_ITERATIONS;
-    edf[i] = probability;
-  }
+  // Check if it is uniform distribution with confidence 0.95
+  // http://dlc.erieri.com/onlinetextbook/index.cfm?fuseaction=textbook.appendix&FileName=Table7
+  BOOST_WARN_LE(getDeviation(counts, MAX_ITERATIONS), 0.230);
+}
 
-  double t = 0.0;
-  for (size_t i = 0; i < MAX_BINS; i++) {
-    t = std::max(t, std::abs(edf[i] - (i * 1.0 / MAX_BINS)));
+BOOST_AUTO_TEST_CASE(GenerateRandomBytes)
+{
+  // Kolmogorov-Smirnov Goodness-of-Fit Test
+  // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm
+
+  uint8_t buf[1024] = {0};
+  random::generateSecureBytes(buf, sizeof(buf));
+
+  std::vector<uint32_t> counts(256, 0);
+
+  for (size_t i = 0; i < sizeof(buf); i++) {
+    counts[buf[i]]++;
   }
 
   // Check if it is uniform distribution with confidence 0.95
   // http://dlc.erieri.com/onlinetextbook/index.cfm?fuseaction=textbook.appendix&FileName=Table7
-  BOOST_WARN_LE(t, 0.230);
+  BOOST_WARN_LE(getDeviation(counts, sizeof(buf)), 0.230);
 }
 
-BOOST_AUTO_TEST_SUITE_END()
+// This fixture uses OpenSSL routines to set a dummy random generator that always fails
+class FailRandMethodFixture
+{
+public:
+  FailRandMethodFixture()
+    : m_dummyRandMethod{&FailRandMethodFixture::seed,
+                        &FailRandMethodFixture::bytes,
+                        &FailRandMethodFixture::cleanup,
+                        &FailRandMethodFixture::add,
+                        &FailRandMethodFixture::pseudorand,
+                        &FailRandMethodFixture::status}
+  {
+    m_origRandMethod = RAND_get_rand_method();
+    RAND_set_rand_method(&m_dummyRandMethod);
+  }
+
+  ~FailRandMethodFixture()
+  {
+    RAND_set_rand_method(m_origRandMethod);
+  }
+
+private: // RAND_METHOD callbacks
+  static void
+  seed(const void* buf, int num)
+  {
+  }
+
+  static int
+  bytes(unsigned char *buf, int num)
+  {
+    return 0;
+  }
+
+  static void
+  cleanup()
+  {
+  }
+
+  static void
+  add(const void *buf, int num, double entropy)
+  {
+  }
+
+  static int
+  pseudorand(unsigned char *buf, int num)
+  {
+    return 0;
+  }
+
+  static int
+  status()
+  {
+    return 0;
+  }
+
+private:
+  const RAND_METHOD* m_origRandMethod;
+  RAND_METHOD m_dummyRandMethod;
+};
+
+BOOST_FIXTURE_TEST_CASE(Error, FailRandMethodFixture)
+{
+  uint8_t buf[1024] = {0};
+  BOOST_CHECK_THROW(random::generateSecureBytes(buf, sizeof(buf)), std::runtime_error);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestRandom
+BOOST_AUTO_TEST_SUITE_END() // Util
 
 } // namespace tests
 } // namespace ndn