Passwordfile library: tests/opensslutils.cpp Source File

Passwordfile library 5.1.0 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 "../io/cryptoexception.h"
4
5#include <c++utilities/chrono/datetime.h>
6#include <c++utilities/conversion/stringconversion.h>
7#include <c++utilities/tests/testutils.h>
8
9#include <cppunit/TestFixture.h>
10#include <cppunit/extensions/HelperMacros.h>
11
12#include <random>
13
14using namespace std;
15using namespace Util::OpenSsl;
16using namespace CppUtilities;
17using namespace CppUtilities::Literals;
18
19using namespace CPPUNIT_NS;
20
24 class OpenSslUtilsTests : public TestFixture {
25 CPPUNIT_TEST_SUITE(OpenSslUtilsTests);
26 CPPUNIT_TEST(testComputeSha256Sum);
27 CPPUNIT_TEST(testGenerateRandomNumber);
28 CPPUNIT_TEST(testComputeTOTP);
29 CPPUNIT_TEST_SUITE_END();
30
31public:
32 void setUp() override;
33 void tearDown() override;
34
35 void testComputeSha256Sum();
36 void testGenerateRandomNumber();
37 void testComputeTOTP();
38};
39
40CPPUNIT_TEST_SUITE_REGISTRATION(OpenSslUtilsTests);
41
42 void OpenSslUtilsTests::setUp()
43{
44}
45
46 void OpenSslUtilsTests::tearDown()
47{
48}
49
50 void OpenSslUtilsTests::testComputeSha256Sum()
51{
52 const char someString[] = "hello world";
53 Sha256Sum sum = computeSha256Sum(reinterpret_cast<unsigned const char *>(someString), sizeof(someString));
54 string sumAsHex;
55 sumAsHex.reserve(64);
56 for (unsigned char hashNumber : sum.data) {
57 const string digits = numberToString(hashNumber, static_cast<unsigned char>(16));
58 sumAsHex.push_back(digits.size() < 2 ? '0' : digits.front());
59 sumAsHex.push_back(digits.back());
60 }
61 CPPUNIT_ASSERT_EQUAL("430646847E70344C09F58739E99D5BC96EAC8D5FE7295CF196B986279876BF9B"s, sumAsHex);
62 // note that the termination char is hashed as well
63}
64
65 void OpenSslUtilsTests::testGenerateRandomNumber()
66{
67 CPPUNIT_ASSERT_EQUAL(static_cast<uint32_t>(0u), generateRandomNumber(0u, 0u));
68 CPPUNIT_ASSERT_EQUAL(static_cast<uint32_t>(1u), generateRandomNumber(1u, 1u));
69}
70
71 void OpenSslUtilsTests::testComputeTOTP()
72{
73 const auto urlDigits6Period30 = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=30&digits=6&issuer=foo%20bar";
74 const auto urlDigits8Period15 = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=15&digits=8&issuer=foo%20bar";
75 const auto urlSha256Digits8 = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=30&digits=8&algorithm=SHA256";
76 const auto urlSha512Digits10 = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=30&digits=10&algorithm=SHA512";
77 const auto urlInvalidSecret = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABC1&period=30&digits=10&algorithm=SHA512";
78 const auto urlInvalidAlgo = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=30&digits=10&algorithm=SHA513";
79
80 const auto time = DateTime::fromDateAndTime(2026, 5, 2, 10, 52, 30);
81 CPPUNIT_ASSERT_EQUAL("757702"s, computeTOTP(urlDigits6Period30, time).digits);
82 CPPUNIT_ASSERT_EQUAL("41448963"s, computeTOTP(urlDigits8Period15, time).digits);
83 CPPUNIT_ASSERT_EQUAL("10222808"s, computeTOTP(urlSha256Digits8, time).digits);
84 CPPUNIT_ASSERT_EQUAL("0340892126"s, computeTOTP(urlSha512Digits10, time).digits);
85 CPPUNIT_ASSERT_THROW(computeTOTP(urlInvalidSecret, time), ConversionException);
86 CPPUNIT_ASSERT_THROW(computeTOTP(urlInvalidAlgo, time), Io::CryptoException);
87}
Io::CryptoException
The exception that is thrown when an encryption/decryption error occurs.
Definition cryptoexception.h:11
OpenSslUtilsTests
The OpenSslUtilsTests class tests the functions in the Util::OpenSsl namespace.
Definition opensslutils.cpp:24
OpenSslUtilsTests::testComputeTOTP
void testComputeTOTP()
Definition opensslutils.cpp:71
OpenSslUtilsTests::setUp
void setUp() override
Definition opensslutils.cpp:42
OpenSslUtilsTests::testComputeSha256Sum
void testComputeSha256Sum()
Definition opensslutils.cpp:50
OpenSslUtilsTests::testGenerateRandomNumber
void testGenerateRandomNumber()
Definition opensslutils.cpp:65
OpenSslUtilsTests::tearDown
void tearDown() override
Definition opensslutils.cpp:46
cryptoexception.h
CppUtilities
Definition openssl.h:13
Util::OpenSsl
Contains functions utilizing the usage of OpenSSL.
Definition openssl.h:19
Util::OpenSsl::generateRandomNumber
PASSWORD_FILE_EXPORT std::uint32_t generateRandomNumber(std::uint32_t min, std::uint32_t max)
Generates a random number using OpenSSL.
Definition openssl.cpp:140
Util::OpenSsl::computeTOTP
PASSWORD_FILE_EXPORT TOTP computeTOTP(std::string_view url, CppUtilities::DateTime time)
Compute a token following the TOTP standard (RFC 6238).
Definition openssl.cpp:157
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:130
openssl.h
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(OpenSslUtilsTests)
Util::OpenSsl::Sha256Sum
Definition openssl.h:21
Util::OpenSsl::Sha256Sum::data
unsigned char data[size]
Definition openssl.h:23