Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Apr 10, 2024
1 parent 1d05853 commit ba3a2b3
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/tests/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <botan/internal/cpuid.h>
#include <botan/internal/ct_utils.h>
#include <botan/internal/fmt.h>
#include <botan/internal/int_utils.h>
#include <botan/internal/loadstor.h>
#include <botan/internal/parsing.h>
#include <botan/internal/rounding.h>
Expand All @@ -38,6 +39,9 @@ class Utility_Function_Tests final : public Test {
std::vector<Test::Result> run() override {
std::vector<Test::Result> results;

results.push_back(test_checked_add());
results.push_back(test_checked_mul());
results.push_back(test_checked_cast());
results.push_back(test_round_up());
results.push_back(test_loadstore());
results.push_back(test_loadstore_fallback());
Expand All @@ -47,6 +51,81 @@ class Utility_Function_Tests final : public Test {
}

private:
Test::Result test_checked_add() {
Test::Result result("checked_add");

const size_t large = static_cast<size_t>(-5);
const size_t zero = 0;

for(size_t i = 0; i != 5; ++i) {
auto sum1 = Botan::checked_add<size_t>(i, zero, zero, zero, large);
auto sum2 = Botan::checked_add<size_t>(large, zero, zero, zero, i);

result.confirm("checked_add looks at all args", sum1 == sum2);

if(i < 5) {
result.test_eq("checked_add worked", sum1.value(), i + large);
} else {
result.confirm("checked_add did not return a result", !sum1.has_value());
}
}

auto& rng = Test::rng();

for(size_t i = 0; i != 100; ++i) {
const uint16_t x = Botan::make_uint16(rng.next_byte(), rng.next_byte());
const uint16_t y = Botan::make_uint16(rng.next_byte(), rng.next_byte());

const uint32_t ref = static_cast<uint32_t>(x) + y;

if(auto z = Botan::checked_add(x, y)) {
result.test_int_eq("checked_add adds", z.value(), ref);
} else {
result.confirm("checked_add checks", (ref >> 16) > 0);
}
}

return result;
}

Test::Result test_checked_mul() {
Test::Result result("checked_mul");

auto& rng = Test::rng();

for(size_t i = 0; i != 100; ++i) {
const uint16_t x = Botan::make_uint16(rng.next_byte(), rng.next_byte());
const uint16_t y = Botan::make_uint16(rng.next_byte(), rng.next_byte());

const uint32_t ref = static_cast<uint32_t>(x) * y;

if(auto z = Botan::checked_mul(x, y)) {
result.test_int_eq("checked_mul multiplies", z.value(), ref);
} else {
result.confirm("checked_mul checks", (ref >> 16) > 0);
}
}

return result;
}

Test::Result test_checked_cast() {
Test::Result result("checked_cast");

const uint32_t large = static_cast<uint32_t>(-1);
const uint32_t is_16_bits = 0x8123;
const uint32_t is_8_bits = 0x89;

result.confirm("checked_cast checks", !Botan::checked_cast<uint16_t>(large).has_value());
result.confirm("checked_cast checks", !Botan::checked_cast<uint8_t>(large).has_value());

result.test_int_eq("checked_cast converts", Botan::checked_cast<uint32_t>(large).value(), large);
result.test_int_eq("checked_cast converts", Botan::checked_cast<uint16_t>(is_16_bits).value(), 0x8123);
result.test_int_eq("checked_cast converts", Botan::checked_cast<uint8_t>(is_8_bits).value(), 0x89);

return result;
}

Test::Result test_round_up() {
Test::Result result("Util round_up");

Expand Down

0 comments on commit ba3a2b3

Please sign in to comment.