2
0
mirror of https://github.com/boostorg/uuid.git synced 2026-01-19 04:42:16 +00:00
Files
uuid/test/test_detail_random_provider.cpp
James E. King III a8a1ec3d82 Changed the default random_generator implementation to use
operating-system provided entropy as it is more secure and
faster for the typical use case of generating one uuid at
a time.

This is a breaking change for anyone passing a mt19937
into one of the explicit constructors of random_generator,
which would be quite rare.

Changed the default random provider on Windows to use BCrypt
where available, falling back to Wincrypt when necessary or
when explicitly requested through a macro.

Provide a new random_generator_mt19937 type definition for
use cases where a large number of uuids need to be created
with high performance.  This is equivalent to the previous
definition of random_generator.

Provide a random generation benchmark test showing the
cutoff where the mt19937-based generator will outperform the
standard generator based on wall time.

Removed template specialization for boost::random::random_device
so that any UniformRandomNumberGenerator can be used properly
with random_generator.

Replaced the seed_rng detail implementation (which had a number
of flaws) with a replacement header-only random_provider
implementation.

Note: entropy generation errors will cause an entropy_error
to be thrown from random_generator.  The previous implementation
ignored errors and silently failed.

Added internal support for entropy generation on cloudabi
platform leveraging the new random_provider implementation.

Added internal support for Universal Windows Platform (UWP)
development leveraging the new random_provider implementation.

Added internal support for getentropy() on Linux and OpenBSD
if certain requirements are met.

This fixes #24
This closes #53
2017-12-18 09:56:02 -05:00

78 lines
2.3 KiB
C++

//
// Copyright (c) 2017 James E. King III
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENCE_1_0.txt)
//
// Positive and negative testing for detail::random_provider
//
#include <boost/array.hpp>
#include <boost/cstdint.hpp>
#include <boost/detail/lightweight_test.hpp>
// The mock needs to load first for posix system call redirection to work properly
#include "mock_random.hpp"
#include <boost/uuid/detail/random_provider.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <limits>
#include <string.h>
int main(int, char*[])
{
#if !defined(BOOST_UUID_TEST_RANDOM_MOCK) // Positive Testing
boost::uuids::detail::random_provider provider;
boost::array<unsigned int, 2> ints;
// test generator()
ints[0] = 0;
ints[1] = 0;
provider.generate(ints.begin(), ints.end());
BOOST_TEST_NE(ints[0], ints[1]);
// test name()
BOOST_TEST_GT(strlen(provider.name()), 4u);
// test get_random_bytes()
char buf1[64];
char buf2[64];
provider.get_random_bytes(buf1, 64);
provider.get_random_bytes(buf2, 64);
BOOST_TEST_NE(0, memcmp(buf1, buf2, 64));
#else // Negative Testing
if (expectations_capable())
{
// Test fail acquiring context if the provider supports it
if (provider_acquires_context())
{
expect_next_call_success(false);
BOOST_TEST_THROWS(boost::uuids::detail::random_provider(),
boost::uuids::entropy_error);
BOOST_TEST(expectations_met());
}
// Test fail acquiring entropy
if (provider_acquires_context())
{
expect_next_call_success(true);
}
expect_next_call_success(false);
// 4 is important for the posix negative test (partial read) to work properly
// as it sees a size of 4, returns 1, causing a 2nd loop to read 3 more bytes...
char buf[4];
BOOST_TEST_THROWS(boost::uuids::detail::random_provider().get_random_bytes(buf, 4),
boost::uuids::entropy_error);
BOOST_TEST(expectations_met());
}
#endif
return boost::report_errors();
}