linux-user: Support for restarting system calls for OpenRISC targets
[qemu.git] / tests / test-base64.c
1 /*
2 * QEMU base64 helper test
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include "qemu/osdep.h"
22 #include <glib.h>
23
24 #include "qapi/error.h"
25 #include "qemu/base64.h"
26
27 static void test_base64_good(void)
28 {
29 const char input[] =
30 "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW\n"
31 "lzc2VkIHRoZSBzY29ycGlvbi4=";
32 const char expect[] = "Because we focused on the snake, "
33 "we missed the scorpion.";
34
35 size_t len;
36 uint8_t *actual = qbase64_decode(input,
37 -1,
38 &len,
39 &error_abort);
40
41 g_assert(actual != NULL);
42 g_assert_cmpint(len, ==, strlen(expect));
43 g_assert_cmpstr((char *)actual, ==, expect);
44 g_free(actual);
45 }
46
47
48 static void test_base64_bad(const char *input,
49 size_t input_len)
50 {
51 size_t len;
52 Error *err = NULL;
53 uint8_t *actual = qbase64_decode(input,
54 input_len,
55 &len,
56 &err);
57
58 g_assert(err != NULL);
59 g_assert(actual == NULL);
60 g_assert_cmpint(len, ==, 0);
61 error_free(err);
62 }
63
64
65 static void test_base64_embedded_nul(void)
66 {
67 /* We put a NUL character in the middle of the base64
68 * text which is invalid data, given the expected length */
69 const char input[] =
70 "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW\0"
71 "lzc2VkIHRoZSBzY29ycGlvbi4=";
72
73 test_base64_bad(input, G_N_ELEMENTS(input) - 1);
74 }
75
76
77 static void test_base64_not_nul_terminated(void)
78 {
79 const char input[] =
80 "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW\n"
81 "lzc2VkIHRoZSBzY29ycGlvbi4=";
82
83 /* Using '-2' to make us drop the trailing NUL, thus
84 * creating an invalid base64 sequence for decoding */
85 test_base64_bad(input, G_N_ELEMENTS(input) - 2);
86 }
87
88
89 static void test_base64_invalid_chars(void)
90 {
91 /* We put a single quote character in the middle
92 * of the base64 text which is invalid data */
93 const char input[] =
94 "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW'"
95 "lzc2VkIHRoZSBzY29ycGlvbi4=";
96
97 test_base64_bad(input, strlen(input));
98 }
99
100
101 int main(int argc, char **argv)
102 {
103 g_test_init(&argc, &argv, NULL);
104 g_test_add_func("/util/base64/good", test_base64_good);
105 g_test_add_func("/util/base64/embedded-nul", test_base64_embedded_nul);
106 g_test_add_func("/util/base64/not-nul-terminated",
107 test_base64_not_nul_terminated);
108 g_test_add_func("/util/base64/invalid-chars", test_base64_invalid_chars);
109 return g_test_run();
110 }