2021-09-11 14:41:18 +02:00
|
|
|
#include <hex/providers/provider.hpp>
|
2021-09-11 14:40:53 +02:00
|
|
|
|
2021-09-11 14:41:18 +02:00
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
#include <stdexcept>
|
2021-09-11 14:40:53 +02:00
|
|
|
|
2021-09-11 14:41:18 +02:00
|
|
|
namespace hex::test {
|
|
|
|
using namespace hex::prv;
|
|
|
|
|
|
|
|
class TestProvider : public prv::Provider {
|
|
|
|
public:
|
2022-03-04 20:52:39 +01:00
|
|
|
explicit TestProvider(std::vector<u8> *data) : Provider() {
|
2021-10-12 21:32:33 +02:00
|
|
|
this->setData(data);
|
2021-09-11 14:41:18 +02:00
|
|
|
}
|
|
|
|
~TestProvider() override = default;
|
|
|
|
|
2021-09-21 02:48:41 +02:00
|
|
|
[[nodiscard]] bool isAvailable() const override { return true; }
|
|
|
|
[[nodiscard]] bool isReadable() const override { return true; }
|
|
|
|
[[nodiscard]] bool isWritable() const override { return false; }
|
|
|
|
[[nodiscard]] bool isResizable() const override { return false; }
|
|
|
|
[[nodiscard]] bool isSavable() const override { return false; }
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
void setData(std::vector<u8> *data) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_data = data;
|
2021-10-12 21:32:33 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:48:41 +02:00
|
|
|
[[nodiscard]] std::string getName() const override {
|
|
|
|
return "";
|
|
|
|
}
|
2021-09-11 14:41:18 +02:00
|
|
|
|
2023-06-04 10:42:11 +02:00
|
|
|
[[nodiscard]] std::vector<Description> getDataDescription() const override {
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2021-09-11 14:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void readRaw(u64 offset, void *buffer, size_t size) override {
|
2023-12-19 13:10:25 +01:00
|
|
|
if (offset + size > m_data->size()) return;
|
2021-10-12 21:32:33 +02:00
|
|
|
|
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
|
|
|
std::memcpy(buffer, m_data->data() + offset, size);
|
2021-09-11 14:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeRaw(u64 offset, const void *buffer, size_t size) override {
|
2023-12-19 13:10:25 +01:00
|
|
|
if (offset + size > m_data->size()) return;
|
2021-10-12 21:32:33 +02:00
|
|
|
|
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
|
|
|
std::memcpy(m_data->data() + offset, buffer, size);
|
2021-09-11 14:41:18 +02:00
|
|
|
}
|
|
|
|
|
2023-12-07 12:06:26 +01:00
|
|
|
[[nodiscard]] u64 getActualSize() const override {
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_data->size();
|
2021-09-11 14:41:18 +02:00
|
|
|
}
|
|
|
|
|
2023-03-13 09:25:07 +01:00
|
|
|
[[nodiscard]] virtual std::string getTypeName() const override { return "hex.test.provider.test"; }
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
bool open() override { return true; }
|
|
|
|
void close() override { }
|
|
|
|
|
2021-09-11 14:41:18 +02:00
|
|
|
private:
|
2022-03-04 20:52:39 +01:00
|
|
|
std::vector<u8> *m_data = nullptr;
|
2021-09-11 14:41:18 +02:00
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|