Passwordfile library: tests/opensslutils.cpp Source File

Passwordfile library 5.0.13 C++ library to read/write passwords from/to encrypted files

Loading...
Searching...
No Matches
opensslutils.cpp
Go to the documentation of this file.
1#include "../util/openssl.h"
2
3#include <c++utilities/conversion/stringconversion.h>
4#include <c++utilities/tests/testutils.h>
5
6#include <cppunit/TestFixture.h>
7#include <cppunit/extensions/HelperMacros.h>
8
9#include <random>
10
11using namespace std;
12using namespace Util::OpenSsl;
13using namespace CppUtilities;
14using namespace CppUtilities::Literals;
15
16using namespace CPPUNIT_NS;
17
21 class OpenSslUtilsTests : public TestFixture {
22 CPPUNIT_TEST_SUITE(OpenSslUtilsTests);
23 CPPUNIT_TEST(testComputeSha256Sum);
24 CPPUNIT_TEST(testGenerateRandomNumber);
25 CPPUNIT_TEST_SUITE_END();
26
27public:
28 void setUp() override;
29 void tearDown() override;
30
31 void testComputeSha256Sum();
32 void testGenerateRandomNumber();
33};
34
35CPPUNIT_TEST_SUITE_REGISTRATION(OpenSslUtilsTests);
36
37 void OpenSslUtilsTests::setUp()
38{
39}
40
41 void OpenSslUtilsTests::tearDown()
42{
43}
44
45 void OpenSslUtilsTests::testComputeSha256Sum()
46{
47 const char someString[] = "hello world";
48 Sha256Sum sum = computeSha256Sum(reinterpret_cast<unsigned const char *>(someString), sizeof(someString));
49 string sumAsHex;
50 sumAsHex.reserve(64);
51 for (unsigned char hashNumber : sum.data) {
52 const string digits = numberToString(hashNumber, static_cast<unsigned char>(16));
53 sumAsHex.push_back(digits.size() < 2 ? '0' : digits.front());
54 sumAsHex.push_back(digits.back());
55 }
56 CPPUNIT_ASSERT_EQUAL("430646847E70344C09F58739E99D5BC96EAC8D5FE7295CF196B986279876BF9B"s, sumAsHex);
57 // note that the termination char is hashed as well
58}
59
60 void OpenSslUtilsTests::testGenerateRandomNumber()
61{
62 CPPUNIT_ASSERT_EQUAL(static_cast<uint32_t>(0u), generateRandomNumber(0u, 0u));
63 CPPUNIT_ASSERT_EQUAL(static_cast<uint32_t>(1u), generateRandomNumber(1u, 1u));
64}
OpenSslUtilsTests
The OpenSslUtilsTests class tests the functions in the Util::OpenSsl namespace.
Definition opensslutils.cpp:21
OpenSslUtilsTests::setUp
void setUp() override
Definition opensslutils.cpp:37
OpenSslUtilsTests::testComputeSha256Sum
void testComputeSha256Sum()
Definition opensslutils.cpp:45
OpenSslUtilsTests::testGenerateRandomNumber
void testGenerateRandomNumber()
Definition opensslutils.cpp:60
OpenSslUtilsTests::tearDown
void tearDown() override
Definition opensslutils.cpp:41
CppUtilities
Definition utils.h:12
Util::OpenSsl
Contains functions utilizing the usage of OpenSSL.
Definition openssl.h:11
Util::OpenSsl::generateRandomNumber
PASSWORD_FILE_EXPORT std::uint32_t generateRandomNumber(std::uint32_t min, std::uint32_t max)
Util::OpenSsl::computeSha256Sum
PASSWORD_FILE_EXPORT Sha256Sum computeSha256Sum(const unsigned char *buffer, std::size_t size)
Computes a SHA-256 sum using OpenSSL.
Definition openssl.cpp:50
openssl.h
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(OpenSslUtilsTests)
Util::OpenSsl::Sha256Sum
Definition openssl.h:13
Util::OpenSsl::Sha256Sum::data
unsigned char data[size]
Definition openssl.h:15