From 5eec94baacc074ff9dd64923792f0efb5e897957 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 9 Jun 2018 19:47:50 +0200 Subject: [PATCH] Make OpenSslRandomDevice usable with uniform_int_distribution --- CMakeLists.txt | 5 ++-- tests/opensslrandomdevice.cpp | 51 +++++++++++++++++++++++++++++++++++ util/opensslrandomdevice.cpp | 27 ++++++++----------- util/opensslrandomdevice.h | 17 ++++++++++++ 4 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 tests/opensslrandomdevice.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e96ee9..73e934f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/tests/opensslrandomdevice.cpp b/tests/opensslrandomdevice.cpp new file mode 100644 index 0000000..a80cd51 --- /dev/null +++ b/tests/opensslrandomdevice.cpp @@ -0,0 +1,51 @@ +#include "../util/opensslrandomdevice.h" + +#include + +#include +#include + +#include + +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); +} diff --git a/util/opensslrandomdevice.cpp b/util/opensslrandomdevice.cpp index e6d7e2b..77a2844 100644 --- a/util/opensslrandomdevice.cpp +++ b/util/opensslrandomdevice.cpp @@ -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(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); } /*! diff --git a/util/opensslrandomdevice.h b/util/opensslrandomdevice.h index afa8ebe..68deef0 100644 --- a/util/opensslrandomdevice.h +++ b/util/opensslrandomdevice.h @@ -5,14 +5,31 @@ #include +#include + 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::min(); +} + +constexpr OpenSslRandomDevice::result_type OpenSslRandomDevice::max() +{ + return std::numeric_limits::max(); +} + } // namespace Util #endif // OPENSSLRANDOMDEVICE_H