1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu/osdep.h"
22
23#include "qapi/error.h"
24#include "qemu/base64.h"
25
26static void test_base64_good(void)
27{
28 const char input[] =
29 "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW\n"
30 "lzc2VkIHRoZSBzY29ycGlvbi4=";
31 const char expect[] = "Because we focused on the snake, "
32 "we missed the scorpion.";
33
34 size_t len;
35 uint8_t *actual = qbase64_decode(input,
36 -1,
37 &len,
38 &error_abort);
39
40 g_assert(actual != NULL);
41 g_assert_cmpint(len, ==, strlen(expect));
42 g_assert_cmpstr((char *)actual, ==, expect);
43 g_free(actual);
44}
45
46
47static void test_base64_bad(const char *input,
48 size_t input_len)
49{
50 size_t len;
51 Error *err = NULL;
52 uint8_t *actual = qbase64_decode(input,
53 input_len,
54 &len,
55 &err);
56
57 g_assert(err != NULL);
58 g_assert(actual == NULL);
59 g_assert_cmpint(len, ==, 0);
60 error_free(err);
61}
62
63
64static void test_base64_embedded_nul(void)
65{
66
67
68 const char input[] =
69 "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW\0"
70 "lzc2VkIHRoZSBzY29ycGlvbi4=";
71
72 test_base64_bad(input, G_N_ELEMENTS(input) - 1);
73}
74
75
76static void test_base64_not_nul_terminated(void)
77{
78 const char input[] =
79 "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW\n"
80 "lzc2VkIHRoZSBzY29ycGlvbi4=";
81
82
83
84 test_base64_bad(input, G_N_ELEMENTS(input) - 2);
85}
86
87
88static void test_base64_invalid_chars(void)
89{
90
91
92 const char input[] =
93 "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW'"
94 "lzc2VkIHRoZSBzY29ycGlvbi4=";
95
96 test_base64_bad(input, strlen(input));
97}
98
99
100int main(int argc, char **argv)
101{
102 g_test_init(&argc, &argv, NULL);
103 g_test_add_func("/util/base64/good", test_base64_good);
104 g_test_add_func("/util/base64/embedded-nul", test_base64_embedded_nul);
105 g_test_add_func("/util/base64/not-nul-terminated",
106 test_base64_not_nul_terminated);
107 g_test_add_func("/util/base64/invalid-chars", test_base64_invalid_chars);
108 return g_test_run();
109}
110