File: test_base64.cpp

package info (click to toggle)
olm 3.2.16%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,208 kB
  • sloc: cpp: 15,245; ansic: 10,894; java: 3,244; objc: 2,291; javascript: 1,882; python: 1,839; makefile: 439; sh: 245; asm: 7; xml: 1
file content (85 lines) | stat: -rw-r--r-- 2,695 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "olm/base64.hh"
#include "olm/base64.h"
#include <cstring>
#include <vector>
#include <array>

#include "testing.hh"

 /* Base64 encode test */
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);
REQUIRE_EQ(std::size_t(15), output_length);

std::uint8_t output[15] = {};
olm::encode_base64(input, input_length, output);
CHECK_EQ_SIZE(expected_output, output, output_length);
}

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);
REQUIRE_EQ(std::size_t(15), output_length);

std::uint8_t output[15];
output_length = ::_olm_encode_base64(input, input_length, output);
CHECK_EQ(std::size_t(15), output_length);
CHECK_EQ_SIZE(expected_output, output, output_length);
}

 /* Base64 decode test */
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);
REQUIRE_EQ(std::size_t(11), output_length);

std::uint8_t output[11];
olm::decode_base64(input, input_length, output);
CHECK_EQ_SIZE(expected_output, output, output_length);
}


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);
REQUIRE_EQ(std::size_t(11), output_length);

std::uint8_t output[11];
output_length = ::_olm_decode_base64(input, input_length, output);
REQUIRE_EQ(std::size_t(11), output_length);
CHECK_EQ_SIZE(expected_output, output, output_length);
}

TEST_CASE("Decoding base64 of invalid length fails with -1") {
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);
std::vector<std::uint8_t> output(buf_length, 0);
std::vector<std::uint8_t> expected_output(buf_length, 0);

std::size_t output_length = ::_olm_decode_base64(input, input_length, output.data());
REQUIRE_EQ(std::size_t(-1), output_length);
CHECK_EQ(output, expected_output);
}