OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_randapi.cpp
Go to the documentation of this file.
1#include <iostream>
2#include "test_common.hpp"
3
5
6using namespace openvpn;
7
8template <typename IntegralT>
10{
11 public:
12 OPENVPN_EXCEPTION(s_min_error);
13
15
16 // Random algorithm name
17 std::string name() const override
18 {
19 return "IntegralMin";
20 }
21
22 // Fill buffer with minimum value
23 void rand_bytes(unsigned char *buf, size_t size) override
24 {
25 if (!rand_bytes_noexcept(buf, size))
26 throw s_min_error("rand_bytes failed");
27 }
28
29 // Like rand_bytes, but don't throw exception.
30 // Return true on successs, false on fail.
31 bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
32 {
33 if (size < sizeof(IntegralT))
34 return false;
35 IntegralT *int_ptr = reinterpret_cast<IntegralT *>(buf);
36 *int_ptr = std::numeric_limits<IntegralT>::min();
37 return true;
38 }
39
40 IntegralT get_result()
41 {
42 return rand_get_positive<IntegralT>();
43 }
44};
45
46template <typename IntegralT>
47void randapi_signed_min_test(const std::string &test_name)
48{
50
51 IntegralT result = s_min.get_result();
52
53 EXPECT_EQ(result, 0) << "fails for \"" << test_name << "\" test";
54}
55
56#define RANDAPI_SIGNED_MIN_TEST(test) \
57 do \
58 { \
59 randapi_signed_min_test<test>(#test); \
60 } while (0)
61
62
63TEST(misc, randapi_signed_min)
64{
65 RANDAPI_SIGNED_MIN_TEST(signed char);
66 RANDAPI_SIGNED_MIN_TEST(unsigned char);
71}
bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
Fill a buffer with random bytes without throwing exceptions.
std::string name() const override
Get the name of the random number generation algorithm.
OPENVPN_EXCEPTION(s_min_error)
IntegralT get_result()
void rand_bytes(unsigned char *buf, size_t size) override
Fill a buffer with random bytes.
RCPtr< IntegralMin > Ptr
The smart pointer class.
Definition rc.hpp:119
Abstract base class for pseudo random number generators.
Definition randapi.hpp:245
Implementation of the base classes for random number generators.
TEST(misc, randapi_signed_min)
void randapi_signed_min_test(const std::string &test_name)
#define RANDAPI_SIGNED_MIN_TEST(test)