Debian Codegraph @

tests/check_base64.c

raw symbols SCIP
1 /*
2 * AIDE (Advanced Intrusion Detection Environment)
3 *
4 * Copyright (C) 2024 Hannes von Haugwitz
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <check.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "base64.h"
26
27 typedef struct {
28 char* orig;
29 char* base64;
30 } base64_t;
31
32 static base64_t base64_tests[] = {
33 { "A", "QQ==" },
34 { "AA", "QUE=" },
35 { "AAA", "QUFB" },
36 { "AAAA", "QUFBQQ==" },
37 { "AAAAA", "QUFBQUE=" },
38 { "AAAAAA", "QUFBQUFB" },
39 { "AAAAAAA", "QUFBQUFBQQ==" },
40 { "AAAAAAA", "QUFBQUFBQQ==" },
41 };
42
43 static int num_base64_tests = sizeof base64_tests / sizeof(base64_t);
44
45 START_TEST (test_base64) {
46 size_t orig_length = strlen(base64_tests[_i].orig);
47 char *base64 = encode_base64((byte *) base64_tests[_i].orig, orig_length);
48 ck_assert_msg(strcmp(base64_tests[_i].base64, base64) == 0, "\n"
49 "encode_base64('%s', %zu ):\n"
50 "string returned '%s'\n"
51 " expected '%s'",
52 base64_tests[_i].orig, orig_length, base64, base64_tests[_i].base64);
53
54 size_t orig_length_pointer;
55 size_t base64_length = strlen(base64);
56 byte *orig = decode_base64(base64, base64_length, &orig_length_pointer);
57 ck_assert_msg(strcmp(base64_tests[_i].orig, (char *) orig) == 0, "\n"
58 "padded decode_base64('%s', %zu ):\n"
59 "string returned '%s'\n"
60 " expected '%s'",
61 base64, base64_length, orig, base64_tests[_i].orig);
62
63 ck_assert_msg(orig_length_pointer == orig_length, "padded decode_base64(' %s', %zu) returned length %zu (expected: %zu)", base64, base64_length, orig_length_pointer, orig_length);
64
65 free(orig);
66 free(base64);
67 }
68 END_TEST
69
70 Suite *make_base64_suite(void) {
71
72 Suite *s = suite_create ("base64");
73
74 TCase *tc_base64 = tcase_create ("base64");
75
76 tcase_add_loop_test (tc_base64, test_base64, 0, num_base64_tests);
77
78 suite_add_tcase (s, tc_base64);
79
80 return s;
81 }