Make OpenSslRandomDevice usable with uniform_int_distribution
This commit is contained in:
parent
0e40562866
commit
5eec94baac
|
@ -25,6 +25,7 @@ set(TEST_HEADER_FILES
|
|||
set(TEST_SRC_FILES
|
||||
tests/cppunit.cpp
|
||||
tests/passwordfiletests.cpp
|
||||
tests/opensslrandomdevice.cpp
|
||||
)
|
||||
|
||||
set(DOC_FILES
|
||||
|
@ -39,8 +40,8 @@ set(META_APP_AUTHOR "Martchus")
|
|||
set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}")
|
||||
set(META_APP_DESCRIPTION "C++ library to read/write passwords from/to encrypted files")
|
||||
set(META_VERSION_MAJOR 3)
|
||||
set(META_VERSION_MINOR 1)
|
||||
set(META_VERSION_PATCH 4)
|
||||
set(META_VERSION_MINOR 2)
|
||||
set(META_VERSION_PATCH 0)
|
||||
set(META_PUBLIC_SHARED_LIB_DEPENDS c++utilities)
|
||||
set(META_PUBLIC_STATIC_LIB_DEPENDS c++utilities_static)
|
||||
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
#include "../util/opensslrandomdevice.h"
|
||||
|
||||
#include <c++utilities/tests/testutils.h>
|
||||
|
||||
#include <cppunit/TestFixture.h>
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include <random>
|
||||
|
||||
using namespace std;
|
||||
using namespace Util;
|
||||
using namespace TestUtilities::Literals;
|
||||
|
||||
using namespace CPPUNIT_NS;
|
||||
|
||||
/*!
|
||||
* \brief The OpenSslRandomDeviceTests class tests the Util::OpenSslRandomDevice class.
|
||||
*/
|
||||
class OpenSslRandomDeviceTests : public TestFixture {
|
||||
CPPUNIT_TEST_SUITE(OpenSslRandomDeviceTests);
|
||||
CPPUNIT_TEST(testUsageWithStandardClasses);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
||||
void testUsageWithStandardClasses();
|
||||
};
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION(OpenSslRandomDeviceTests);
|
||||
|
||||
void OpenSslRandomDeviceTests::setUp()
|
||||
{
|
||||
}
|
||||
|
||||
void OpenSslRandomDeviceTests::tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Tests using the OpenSslRandomDevice with std::uniform_int_distribution.
|
||||
*/
|
||||
void OpenSslRandomDeviceTests::testUsageWithStandardClasses()
|
||||
{
|
||||
uniform_int_distribution<> dist(1, 10);
|
||||
const Util::OpenSslRandomDevice random;
|
||||
const auto val = dist(random);
|
||||
CPPUNIT_ASSERT_GREATEREQUAL(1, val);
|
||||
CPPUNIT_ASSERT_LESSEQUAL(10, val);
|
||||
}
|
|
@ -13,11 +13,6 @@ using namespace std;
|
|||
|
||||
namespace Util {
|
||||
|
||||
/*!
|
||||
* \namespace QtGui
|
||||
* \brief Contains all miscellaneous utility functions.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \class OpenSslRandomDevice
|
||||
* \brief Provides a random device using the OpenSSL function RAND_bytes().
|
||||
|
@ -38,18 +33,18 @@ uint32 OpenSslRandomDevice::operator()() const
|
|||
unsigned char buf[4];
|
||||
if (RAND_bytes(buf, sizeof(buf))) {
|
||||
return ConversionUtilities::LE::toUInt32(reinterpret_cast<char *>(buf));
|
||||
} else {
|
||||
string msg;
|
||||
unsigned long errorCode = ERR_get_error();
|
||||
while (errorCode != 0) {
|
||||
if (!msg.empty()) {
|
||||
msg += '\n';
|
||||
}
|
||||
msg += ERR_error_string(errorCode, 0);
|
||||
errorCode = ERR_get_error();
|
||||
}
|
||||
throw Io::CryptoException(msg);
|
||||
}
|
||||
|
||||
// handle error case
|
||||
string errorMsg;
|
||||
while (unsigned long errorCode = ERR_get_error()) {
|
||||
if (!errorMsg.empty()) {
|
||||
errorMsg += '\n';
|
||||
}
|
||||
errorMsg += ERR_error_string(errorCode, nullptr);
|
||||
errorCode = ERR_get_error();
|
||||
}
|
||||
throw Io::CryptoException(errorMsg);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
|
@ -5,14 +5,31 @@
|
|||
|
||||
#include <c++utilities/conversion/types.h>
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace Util {
|
||||
|
||||
class PASSWORD_FILE_EXPORT OpenSslRandomDevice {
|
||||
public:
|
||||
using result_type = uint32;
|
||||
|
||||
OpenSslRandomDevice();
|
||||
uint32 operator()() const;
|
||||
bool status() const;
|
||||
static constexpr result_type min();
|
||||
static constexpr result_type max();
|
||||
};
|
||||
|
||||
constexpr OpenSslRandomDevice::result_type OpenSslRandomDevice::min()
|
||||
{
|
||||
return std::numeric_limits<result_type>::min();
|
||||
}
|
||||
|
||||
constexpr OpenSslRandomDevice::result_type OpenSslRandomDevice::max()
|
||||
{
|
||||
return std::numeric_limits<result_type>::max();
|
||||
}
|
||||
|
||||
} // namespace Util
|
||||
|
||||
#endif // OPENSSLRANDOMDEVICE_H
|
||||
|
|
Loading…
Reference in New Issue