tests: Add tests for Base64
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-04-26 21:17:09 +02:00
parent 9d33e22ae0
commit fb22e14524
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 150 additions and 0 deletions

View File

@ -17,3 +17,4 @@ function(luna_test SOURCE_FILE APP_NAME SETUID)
endfunction()
luna_test(libluna/TestVector.cpp TestVector OFF)
luna_test(libluna/TestBase64.cpp TestBase64 OFF)

View File

@ -0,0 +1,149 @@
#include <luna/Base64.h>
#include <test.h>
TestResult test_base64_encode_unpadded_message()
{
auto encoded = TRY(Base64::encode("abc"_sv));
validate(encoded.view() == "YWJj");
test_success;
}
TestResult test_base64_decode_unpadded_message()
{
auto rc = Base64::decode_string("YWJj"_sv);
if (rc.has_error())
{
validate(rc.error() != EINVAL);
return rc.release_error();
}
auto decoded = rc.release_value();
validate(decoded.view() == "abc"_sv);
test_success;
}
TestResult test_base64_encode_padded_message()
{
auto encoded = TRY(Base64::encode("abcd"_sv));
validate(encoded.view() == "YWJjZA==");
test_success;
}
TestResult test_base64_encode_padded_message_2()
{
auto encoded = TRY(Base64::encode("abcde"_sv));
validate(encoded.view() == "YWJjZGU=");
test_success;
}
TestResult test_base64_decode_padded_message()
{
auto rc = Base64::decode_string("YWJjZA=="_sv);
if (rc.has_error())
{
validate(rc.error() != EINVAL);
return rc.release_error();
}
auto decoded = rc.release_value();
validate(decoded.view() == "abcd"_sv);
test_success;
}
TestResult test_base64_decode_padded_message_2()
{
auto rc = Base64::decode_string("YWJjZGU="_sv);
if (rc.has_error())
{
validate(rc.error() != EINVAL);
return rc.release_error();
}
auto decoded = rc.release_value();
validate(decoded.view() == "abcde"_sv);
test_success;
}
TestResult test_base64_decode_padded_message_with_newlines()
{
auto rc = Base64::decode_string("YWJj\nZA==\n"_sv);
if (rc.has_error())
{
validate(rc.error() != EINVAL);
return rc.release_error();
}
auto decoded = rc.release_value();
validate(decoded.view() == "abcd"_sv);
test_success;
}
TestResult test_base64_disallow_characters_after_padding()
{
auto rc = Base64::decode_string("YWJjZA==bd"_sv);
validate(rc.has_error());
if (rc.error() != EINVAL) return rc.release_error();
test_success;
}
TestResult test_base64_disallow_garbage_chars_by_default()
{
auto rc = Base64::decode_string("YWJj?-ZA=="_sv);
validate(rc.has_error());
if (rc.error() != EINVAL) return rc.release_error();
test_success;
}
TestResult test_base64_skip_garbage_chars_if_allowed()
{
auto rc = Base64::decode_string("YWJj?-ZA=="_sv, true);
if (rc.has_error())
{
validate(rc.error() != EINVAL);
return rc.release_error();
}
auto decoded = rc.release_value();
validate(decoded.view() == "abcd"_sv);
test_success;
}
Result<void> test_main()
{
test_prelude;
run_test(test_base64_encode_unpadded_message);
run_test(test_base64_decode_unpadded_message);
run_test(test_base64_encode_padded_message);
run_test(test_base64_encode_padded_message_2);
run_test(test_base64_decode_padded_message);
run_test(test_base64_decode_padded_message_2);
run_test(test_base64_decode_padded_message_with_newlines);
run_test(test_base64_disallow_characters_after_padding);
run_test(test_base64_disallow_garbage_chars_by_default);
run_test(test_base64_skip_garbage_chars_if_allowed);
return {};
}