Skip to content

Commit

Permalink
Add tests, and fix checked_add
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Apr 10, 2024
1 parent 9b8f44c commit 6a3e6e7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/utils/int_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Botan {
template <std::unsigned_integral T>
constexpr inline std::optional<T> checked_add(T a, T b) {
const T r = a + b;
if(r - a != b) {
if(r < a || r < b) {
return {};
}
return r;
Expand Down
80 changes: 80 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,82 @@ 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(int si = -15; si != 15; ++si) {
const size_t i = static_cast<size_t>(si);
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 6a3e6e7

Please sign in to comment.