Skip to content

Commit

Permalink
base32 updated tests + output length check
Browse files Browse the repository at this point in the history
  • Loading branch information
itzandroidtab committed Mar 6, 2024
1 parent e6a1127 commit 1fd5fa6
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions klib/crypt/base32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TEST_CASE("Base32 invalid input string size works", "[klib::base32]") {
uint8_t buf[32];

// check if a invalid input length exits without success
REQUIRE(!klib::crypt::base32::decode("123", buf, sizeof(buf)));
REQUIRE(!klib::crypt::base32::decode("123", buf));
}

TEST_CASE("Base32 invalid output size works", "[klib::base32]") {
Expand All @@ -16,10 +16,10 @@ TEST_CASE("Base32 invalid output size works", "[klib::base32]") {
uint8_t buf[5];

// check if we return failure when the buffer is too small
REQUIRE(!klib::crypt::base32::decode("ABCDEFGH", buf, 1));
REQUIRE(!klib::crypt::base32::decode("ABCDEFGH", buf, 2));
REQUIRE(!klib::crypt::base32::decode("ABCDEFGH", buf, 3));
REQUIRE(!klib::crypt::base32::decode("ABCDEFGH", buf, 4));
REQUIRE(!klib::crypt::base32::decode("ABCDEFGH", {buf, 1}));
REQUIRE(!klib::crypt::base32::decode("ABCDEFGH", {buf, 2}));
REQUIRE(!klib::crypt::base32::decode("ABCDEFGH", {buf, 3}));
REQUIRE(!klib::crypt::base32::decode("ABCDEFGH", {buf, 4}));
}

TEST_CASE("Base32 valid output size works", "[klib::base32]") {
Expand All @@ -28,36 +28,47 @@ TEST_CASE("Base32 valid output size works", "[klib::base32]") {
uint8_t buf[5];

// this should pass as we need 5 bytes
REQUIRE(klib::crypt::base32::decode("ABCDEFGH", buf, sizeof(buf)));
REQUIRE(klib::crypt::base32::decode("ABCDEFGH", buf));
}

TEST_CASE("Base32 invalid padding returns failure", "[klib::base32]") {
uint8_t buf[32];

// check if a invalid padding gives a failure
REQUIRE(!klib::crypt::base32::decode("ABCDEF=H", buf, sizeof(buf)));
REQUIRE(!klib::crypt::base32::decode("ABCDEF=H", buf));
}

TEST_CASE("Base32 invalid characters (numbers) returns failure", "[klib::base32]") {
uint8_t buf[32];

// check if invalid numbers give a error
REQUIRE(!klib::crypt::base32::decode("189ABCDE", buf, sizeof(buf)));
REQUIRE(!klib::crypt::base32::decode("189ABCDE", buf));
}

TEST_CASE("Base32 valid input does not fail", "[klib::base32]") {
uint8_t buf[32];

REQUIRE(klib::crypt::base32::decode("ABCDEFGH", buf, sizeof(buf)));
REQUIRE(klib::crypt::base32::decode("23456723", buf, sizeof(buf)));
REQUIRE(klib::crypt::base32::decode("ABCDEFGH", buf));
REQUIRE(klib::crypt::base32::decode("23456723", buf));
}

TEST_CASE("Base32 output is valid", "[klib::crypt::base32]") {
uint8_t buf[32];

// make sure we do not have a failure
REQUIRE(klib::crypt::base32::decode("MFRGGZDFMYYTEMZUGU3DOOBZGA======", buf, sizeof(buf)));
REQUIRE(klib::crypt::base32::decode("MFRGGZDFMYYTEMZUGU3DOOBZGA======", buf));

// check if the output matches
REQUIRE(std::strcmp(reinterpret_cast<const char*>(buf), "abcdef1234567890") == 0);
}

TEST_CASE("Base32 output length is correct", "[klib::crypt::base32]") {
uint8_t buf[32];

// check if the output matches
REQUIRE(klib::crypt::base32::decode("MFRGGZDFMYYTEMZUGU3DOOBZGA======", buf) == std::strlen("abcdef1234567890"));
REQUIRE(klib::crypt::base32::decode("GEZDG===", buf) == std::strlen("123"));
REQUIRE(klib::crypt::base32::decode("GEZDGNBV", buf) == std::strlen("12345"));
REQUIRE(klib::crypt::base32::decode("GEZDGNBVGY======", buf) == std::strlen("123456"));
}

0 comments on commit 1fd5fa6

Please sign in to comment.