2021-10-14 21:19:31 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
#include <utility>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
Tests for the CRC and hash algorithms (#335)
* Update TEST_ASSERT to do nothing if condition is true
The TEST_ASSERT should not return if the condition is true, because:
- it prevents the usage of multiple TEST_ASSERT in a single test case,
- that behavior differs from how the assert in the standard library
works, and thus may give unexpected results.
Make the TEST_ASSERT to print an error message (with an formatted
optional user part) when it fails to make debugging easier.
* Fix some bugs in TestProvider, add unit tests
Use pointer-to-vector in TestProvider so writes can be tested, too.
* Add test EncodeDecode16, fix some encode16 bugs
The function mbedtls_mpi_write_string needs a bit longer buffer than the
resulting string actually will be.
Known bug: mbedtls_mpi_read_binary ingores initial null bytes
* Add test EncodeDecode64, fix some bugs
The functions mbedtls_base64_encode and mbedtls_base64_decode needs a
bit longer buffer than the resulting string actually will be.
* Remove check for empty data from TestProvider
It can be valid to get the hash of empty string.
* Add tests for CRC calculation
Two type of thests:
- compare the result of the CRC calculation to a known to be good
results,
- generate random data as message, calculate of it's CRC and append that
to the message, the CRC of this new data should be 0.
* Add test for hash algorithms
* Add includes in tests
* Remove the use of C++20 ranges
It seems that Apple Clang does not support range-based constrained
algorithms at this time.
* Replace encode16 implementation
To encode the zero bytes at the begining of the input vector, too.
2021-11-26 22:14:44 +01:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2022-01-29 17:14:15 +01:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2021-10-14 21:19:31 +02:00
|
|
|
|
2023-03-13 10:24:48 +01:00
|
|
|
#include <wolv/utils/preproc.hpp>
|
|
|
|
|
2021-10-14 21:19:31 +02:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <functional>
|
|
|
|
|
2023-03-13 10:24:48 +01:00
|
|
|
#define TEST_SEQUENCE(...) static auto WOLV_ANONYMOUS_VARIABLE(TEST_SEQUENCE) = ::hex::test::TestSequenceExecutor(__VA_ARGS__) + []() -> int
|
2022-01-24 20:53:17 +01:00
|
|
|
#define TEST_FAIL() return EXIT_FAILURE
|
|
|
|
#define TEST_SUCCESS() return EXIT_SUCCESS
|
|
|
|
#define FAILING true
|
|
|
|
#define TEST_ASSERT(x, ...) \
|
|
|
|
do { \
|
|
|
|
auto ret = (x); \
|
|
|
|
if (!ret) { \
|
Tests for the CRC and hash algorithms (#335)
* Update TEST_ASSERT to do nothing if condition is true
The TEST_ASSERT should not return if the condition is true, because:
- it prevents the usage of multiple TEST_ASSERT in a single test case,
- that behavior differs from how the assert in the standard library
works, and thus may give unexpected results.
Make the TEST_ASSERT to print an error message (with an formatted
optional user part) when it fails to make debugging easier.
* Fix some bugs in TestProvider, add unit tests
Use pointer-to-vector in TestProvider so writes can be tested, too.
* Add test EncodeDecode16, fix some encode16 bugs
The function mbedtls_mpi_write_string needs a bit longer buffer than the
resulting string actually will be.
Known bug: mbedtls_mpi_read_binary ingores initial null bytes
* Add test EncodeDecode64, fix some bugs
The functions mbedtls_base64_encode and mbedtls_base64_decode needs a
bit longer buffer than the resulting string actually will be.
* Remove check for empty data from TestProvider
It can be valid to get the hash of empty string.
* Add tests for CRC calculation
Two type of thests:
- compare the result of the CRC calculation to a known to be good
results,
- generate random data as message, calculate of it's CRC and append that
to the message, the CRC of this new data should be 0.
* Add test for hash algorithms
* Add includes in tests
* Remove the use of C++20 ranges
It seems that Apple Clang does not support range-based constrained
algorithms at this time.
* Replace encode16 implementation
To encode the zero bytes at the begining of the input vector, too.
2021-11-26 22:14:44 +01:00
|
|
|
hex::log::error("Test assert '" #x "' failed {} at {}:{}", \
|
2022-02-01 22:09:44 +01:00
|
|
|
hex::format("" __VA_ARGS__), \
|
|
|
|
__FILE__, \
|
|
|
|
__LINE__); \
|
2022-01-24 20:53:17 +01:00
|
|
|
return EXIT_FAILURE; \
|
|
|
|
} \
|
Tests for the CRC and hash algorithms (#335)
* Update TEST_ASSERT to do nothing if condition is true
The TEST_ASSERT should not return if the condition is true, because:
- it prevents the usage of multiple TEST_ASSERT in a single test case,
- that behavior differs from how the assert in the standard library
works, and thus may give unexpected results.
Make the TEST_ASSERT to print an error message (with an formatted
optional user part) when it fails to make debugging easier.
* Fix some bugs in TestProvider, add unit tests
Use pointer-to-vector in TestProvider so writes can be tested, too.
* Add test EncodeDecode16, fix some encode16 bugs
The function mbedtls_mpi_write_string needs a bit longer buffer than the
resulting string actually will be.
Known bug: mbedtls_mpi_read_binary ingores initial null bytes
* Add test EncodeDecode64, fix some bugs
The functions mbedtls_base64_encode and mbedtls_base64_decode needs a
bit longer buffer than the resulting string actually will be.
* Remove check for empty data from TestProvider
It can be valid to get the hash of empty string.
* Add tests for CRC calculation
Two type of thests:
- compare the result of the CRC calculation to a known to be good
results,
- generate random data as message, calculate of it's CRC and append that
to the message, the CRC of this new data should be 0.
* Add test for hash algorithms
* Add includes in tests
* Remove the use of C++20 ranges
It seems that Apple Clang does not support range-based constrained
algorithms at this time.
* Replace encode16 implementation
To encode the zero bytes at the begining of the input vector, too.
2021-11-26 22:14:44 +01:00
|
|
|
} while (0)
|
2021-10-14 21:19:31 +02:00
|
|
|
|
|
|
|
namespace hex::test {
|
|
|
|
|
|
|
|
struct Test {
|
|
|
|
std::function<int()> function;
|
|
|
|
bool shouldFail;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Tests {
|
|
|
|
public:
|
|
|
|
static auto addTest(const std::string &name, const std::function<int()> &func, bool shouldFail) noexcept {
|
2022-01-24 20:53:17 +01:00
|
|
|
s_tests.insert({
|
|
|
|
name, {func, shouldFail}
|
|
|
|
});
|
2021-10-14 21:19:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
static auto &get() noexcept {
|
2021-10-14 21:19:31 +02:00
|
|
|
return s_tests;
|
|
|
|
}
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2021-10-14 21:19:31 +02:00
|
|
|
private:
|
|
|
|
static inline std::map<std::string, Test> s_tests;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
class TestSequence {
|
|
|
|
public:
|
2022-01-24 20:53:17 +01:00
|
|
|
TestSequence(const std::string &name, F func, bool shouldFail) noexcept {
|
2021-10-14 21:19:31 +02:00
|
|
|
Tests::addTest(name, func, shouldFail);
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
TestSequence &operator=(TestSequence &&) = delete;
|
2021-10-14 21:19:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TestSequenceExecutor {
|
|
|
|
explicit TestSequenceExecutor(std::string name, bool shouldFail = false) noexcept : m_name(std::move(name)), m_shouldFail(shouldFail) {
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
[[nodiscard]] const auto &getName() const noexcept {
|
2021-10-14 21:19:31 +02:00
|
|
|
return this->m_name;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
[[nodiscard]] bool shouldFail() const noexcept {
|
2021-10-14 21:19:31 +02:00
|
|
|
return this->m_shouldFail;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string m_name;
|
|
|
|
bool m_shouldFail;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<typename F>
|
2022-03-04 20:52:39 +01:00
|
|
|
TestSequence<F> operator+(const TestSequenceExecutor &executor, F &&f) noexcept {
|
2021-10-14 21:19:31 +02:00
|
|
|
return TestSequence<F>(executor.getName(), std::forward<F>(f), executor.shouldFail());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|