• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2017 James E. King III
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 //   https://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Positive and negative testing for detail::random_provider
9 //
10 
11 #include <boost/array.hpp>
12 #include <boost/cstdint.hpp>
13 #include <boost/detail/lightweight_test.hpp>
14 
15 // The mock needs to load first for posix system call redirection to work properly
16 #include "mock_random.hpp"
17 #include <boost/uuid/detail/random_provider.hpp>
18 #include <boost/uuid/uuid.hpp>
19 #include <boost/uuid/uuid_io.hpp>
20 #include <limits>
21 #include <string.h>
22 
23 
main(int,char * [])24 int main(int, char*[])
25 {
26 #if !defined(BOOST_UUID_TEST_RANDOM_MOCK)   // Positive Testing
27 
28     boost::uuids::detail::random_provider provider;
29     boost::array<unsigned int, 2> ints;
30 
31     // test generator()
32     ints[0] = 0;
33     ints[1] = 0;
34     provider.generate(ints.begin(), ints.end());
35     BOOST_TEST_NE(ints[0], ints[1]);
36 
37     // test name()
38     BOOST_TEST_GT(strlen(provider.name()), 4u);
39 
40     // test get_random_bytes()
41     char buf1[64];
42     char buf2[64];
43     provider.get_random_bytes(buf1, 64);
44     provider.get_random_bytes(buf2, 64);
45     BOOST_TEST_NE(0, memcmp(buf1, buf2, 64));
46 
47 #else                                       // Negative Testing
48 
49     if (expectations_capable())
50     {
51         // Test fail acquiring context if the provider supports it
52         if (provider_acquires_context())
53         {
54             expect_next_call_success(false);
55             BOOST_TEST_THROWS(boost::uuids::detail::random_provider(),
56                               boost::uuids::entropy_error);
57             BOOST_TEST(expectations_met());
58         }
59 
60         // Test fail acquiring entropy
61         if (provider_acquires_context())
62         {
63             expect_next_call_success(true);
64         }
65         expect_next_call_success(false);
66         // 4 is important for the posix negative test (partial read) to work properly
67         // as it sees a size of 4, returns 1, causing a 2nd loop to read 3 more bytes...
68         char buf[4];
69         BOOST_TEST_THROWS(boost::uuids::detail::random_provider().get_random_bytes(buf, 4),
70                           boost::uuids::entropy_error);
71         BOOST_TEST(expectations_met());
72     }
73 
74 #endif
75 
76     return boost::report_errors();
77 }
78