test_base64.cpp 2.03 KB
Newer Older
1
#include "olm/base64.hh"
2
#include "olm/base64.h"
Mark Haines's avatar
Mark Haines committed
3
4
5
6
7
#include "unittest.hh"

int main() {

{ /* Base64 encode test */
8
TestCase test_case("Base64 C++ binding encode test");
Mark Haines's avatar
Mark Haines committed
9
10
11
12
13

std::uint8_t input[] = "Hello World";
std::uint8_t expected_output[] = "SGVsbG8gV29ybGQ";
std::size_t input_length = sizeof(input) - 1;

14
std::size_t output_length = olm::encode_base64_length(input_length);
Mark Haines's avatar
Mark Haines committed
15
16
17
assert_equals(std::size_t(15), output_length);

std::uint8_t output[output_length];
18
olm::encode_base64(input, input_length, output);
Mark Haines's avatar
Mark Haines committed
19
20
21
assert_equals(expected_output, output, output_length);
}

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{
TestCase test_case("Base64 C binding encode test");

std::uint8_t input[] = "Hello World";
std::uint8_t expected_output[] = "SGVsbG8gV29ybGQ";
std::size_t input_length = sizeof(input) - 1;

std::size_t output_length = ::olm_encode_base64_length(input_length);
assert_equals(std::size_t(15), output_length);

std::uint8_t output[output_length];
output_length = ::olm_encode_base64(input, input_length, output);
assert_equals(std::size_t(15), output_length);
assert_equals(expected_output, output, output_length);
}

Mark Haines's avatar
Mark Haines committed
38
{ /* Base64 decode test */
39
TestCase test_case("Base64 C++ binding decode test");
Mark Haines's avatar
Mark Haines committed
40
41
42
43
44

std::uint8_t input[] = "SGVsbG8gV29ybGQ";
std::uint8_t expected_output[] = "Hello World";
std::size_t input_length = sizeof(input) - 1;

45
std::size_t output_length = olm::decode_base64_length(input_length);
Mark Haines's avatar
Mark Haines committed
46
47
48
assert_equals(std::size_t(11), output_length);

std::uint8_t output[output_length];
49
olm::decode_base64(input, input_length, output);
Mark Haines's avatar
Mark Haines committed
50
51
52
assert_equals(expected_output, output, output_length);
}

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{
TestCase test_case("Base64 C binding decode test");

std::uint8_t input[] = "SGVsbG8gV29ybGQ";
std::uint8_t expected_output[] = "Hello World";
std::size_t input_length = sizeof(input) - 1;

std::size_t output_length = ::olm_decode_base64_length(input_length);
assert_equals(std::size_t(11), output_length);

std::uint8_t output[output_length];
output_length = ::olm_decode_base64(input, input_length, output);
assert_equals(std::size_t(11), output_length);
assert_equals(expected_output, output, output_length);
}


Mark Haines's avatar
Mark Haines committed
70
}