2015-06-27 01:15:23 +02:00
|
|
|
#include "olm/base64.hh"
|
2016-05-18 18:13:39 +02:00
|
|
|
#include "olm/base64.h"
|
2021-06-27 18:47:39 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <vector>
|
2021-12-22 19:45:33 +01:00
|
|
|
#include <array>
|
2015-06-15 18:48:09 +02:00
|
|
|
|
2021-12-22 19:45:33 +01:00
|
|
|
#include "testing.hh"
|
2015-06-15 18:48:09 +02:00
|
|
|
|
2021-12-22 19:45:33 +01:00
|
|
|
/* Base64 encode test */
|
|
|
|
TEST_CASE("Base64 C++ binding encode test") {
|
2015-06-15 18:48:09 +02:00
|
|
|
|
|
|
|
std::uint8_t input[] = "Hello World";
|
|
|
|
std::uint8_t expected_output[] = "SGVsbG8gV29ybGQ";
|
|
|
|
std::size_t input_length = sizeof(input) - 1;
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
std::size_t output_length = olm::encode_base64_length(input_length);
|
2021-12-22 19:45:33 +01:00
|
|
|
REQUIRE_EQ(std::size_t(15), output_length);
|
2015-06-15 18:48:09 +02:00
|
|
|
|
2021-12-22 19:45:33 +01:00
|
|
|
std::uint8_t output[15] = {};
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::encode_base64(input, input_length, output);
|
2021-12-22 19:45:33 +01:00
|
|
|
CHECK_EQ_SIZE(expected_output, output, output_length);
|
2015-06-15 18:48:09 +02:00
|
|
|
}
|
|
|
|
|
2021-12-22 19:45:33 +01:00
|
|
|
TEST_CASE("Base64 C binding encode test") {
|
2016-05-18 18:13:39 +02:00
|
|
|
|
|
|
|
std::uint8_t input[] = "Hello World";
|
|
|
|
std::uint8_t expected_output[] = "SGVsbG8gV29ybGQ";
|
|
|
|
std::size_t input_length = sizeof(input) - 1;
|
|
|
|
|
2016-05-20 12:59:31 +02:00
|
|
|
std::size_t output_length = ::_olm_encode_base64_length(input_length);
|
2021-12-22 19:45:33 +01:00
|
|
|
REQUIRE_EQ(std::size_t(15), output_length);
|
2016-05-18 18:13:39 +02:00
|
|
|
|
2019-04-22 16:12:42 +02:00
|
|
|
std::uint8_t output[15];
|
2016-05-20 12:59:31 +02:00
|
|
|
output_length = ::_olm_encode_base64(input, input_length, output);
|
2021-12-22 19:45:33 +01:00
|
|
|
CHECK_EQ(std::size_t(15), output_length);
|
|
|
|
CHECK_EQ_SIZE(expected_output, output, output_length);
|
2016-05-18 18:13:39 +02:00
|
|
|
}
|
|
|
|
|
2021-12-22 19:45:33 +01:00
|
|
|
/* Base64 decode test */
|
|
|
|
TEST_CASE("Base64 C++ binding decode test") {
|
2015-06-15 18:48:09 +02:00
|
|
|
|
|
|
|
std::uint8_t input[] = "SGVsbG8gV29ybGQ";
|
|
|
|
std::uint8_t expected_output[] = "Hello World";
|
|
|
|
std::size_t input_length = sizeof(input) - 1;
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
std::size_t output_length = olm::decode_base64_length(input_length);
|
2021-12-22 19:45:33 +01:00
|
|
|
REQUIRE_EQ(std::size_t(11), output_length);
|
2015-06-15 18:48:09 +02:00
|
|
|
|
2019-04-22 16:12:42 +02:00
|
|
|
std::uint8_t output[11];
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::decode_base64(input, input_length, output);
|
2021-12-22 19:45:33 +01:00
|
|
|
CHECK_EQ_SIZE(expected_output, output, output_length);
|
2015-06-15 18:48:09 +02:00
|
|
|
}
|
|
|
|
|
2021-12-22 19:45:33 +01:00
|
|
|
|
|
|
|
TEST_CASE("Base64 C binding decode test") {
|
2016-05-18 18:13:39 +02:00
|
|
|
|
|
|
|
std::uint8_t input[] = "SGVsbG8gV29ybGQ";
|
|
|
|
std::uint8_t expected_output[] = "Hello World";
|
|
|
|
std::size_t input_length = sizeof(input) - 1;
|
|
|
|
|
2016-05-20 12:59:31 +02:00
|
|
|
std::size_t output_length = ::_olm_decode_base64_length(input_length);
|
2021-12-22 19:45:33 +01:00
|
|
|
REQUIRE_EQ(std::size_t(11), output_length);
|
2016-05-18 18:13:39 +02:00
|
|
|
|
2019-04-22 16:12:42 +02:00
|
|
|
std::uint8_t output[11];
|
2016-05-20 12:59:31 +02:00
|
|
|
output_length = ::_olm_decode_base64(input, input_length, output);
|
2021-12-22 19:45:33 +01:00
|
|
|
REQUIRE_EQ(std::size_t(11), output_length);
|
|
|
|
CHECK_EQ_SIZE(expected_output, output, output_length);
|
2016-05-18 18:13:39 +02:00
|
|
|
}
|
|
|
|
|
2021-12-22 19:45:33 +01:00
|
|
|
TEST_CASE("Decoding base64 of invalid length fails with -1") {
|
2021-05-18 11:38:33 +02:00
|
|
|
std::uint8_t input[] = "SGVsbG8gV29ybGQab";
|
|
|
|
std::size_t input_length = sizeof(input) - 1;
|
|
|
|
|
|
|
|
/* We use a longer but valid input length here so that we don't get back -1.
|
|
|
|
* Nothing will be written to the output buffer anyway because the input is
|
|
|
|
* invalid. */
|
|
|
|
std::size_t buf_length = olm::decode_base64_length(input_length + 1);
|
2021-06-27 18:47:39 +02:00
|
|
|
std::vector<std::uint8_t> output(buf_length, 0);
|
|
|
|
std::vector<std::uint8_t> expected_output(buf_length, 0);
|
2021-05-18 11:38:33 +02:00
|
|
|
|
2021-06-27 18:47:39 +02:00
|
|
|
std::size_t output_length = ::_olm_decode_base64(input, input_length, output.data());
|
2021-12-22 19:45:33 +01:00
|
|
|
REQUIRE_EQ(std::size_t(-1), output_length);
|
|
|
|
CHECK_EQ(output, expected_output);
|
2021-05-18 11:38:33 +02:00
|
|
|
}
|
2016-05-18 18:13:39 +02:00
|
|
|
|