Anna Karas | 072a9b9 | 2020-06-08 15:16:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <stddef.h> |
| 4 | #include <stdint.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <b64_decode.h> |
| 8 | #include <tests/test.h> |
| 9 | |
| 10 | struct messages_t { |
| 11 | const char *enc; |
| 12 | const char *dec; |
| 13 | } messages[] = { |
| 14 | {"QQ==", "A"}, |
| 15 | {"Q\r\nUI=", "AB"}, |
| 16 | {"QUJD", "ABC"}, |
| 17 | {"\nQUJDRA==", "ABCD"}, |
| 18 | {"SGVsbG8\r=", "Hello"}, |
| 19 | {"SGVsbG8h", "Hello!"} |
| 20 | }; |
| 21 | |
| 22 | const char *invalid[] = { |
| 23 | "QQ=-=", |
| 24 | "SGVsbG-8=" |
| 25 | }; |
| 26 | |
Anna Karas | 072a9b9 | 2020-06-08 15:16:19 +0200 | [diff] [blame] | 27 | static void test_b64_decode(void **state) |
| 28 | { |
| 29 | uint8_t *decoded; |
| 30 | size_t res; |
| 31 | |
| 32 | for (int i = 0; i < ARRAY_SIZE(messages); i++) { |
| 33 | decoded = malloc(strlen(messages[i].enc) * sizeof(char)); |
| 34 | |
| 35 | res = b64_decode((uint8_t *)messages[i].enc, strlen(messages[i].enc), decoded); |
| 36 | |
| 37 | assert_int_equal(res, (strlen(messages[i].dec))); |
| 38 | assert_string_equal(decoded, messages[i].dec); |
| 39 | |
| 40 | free(decoded); |
| 41 | } |
| 42 | |
| 43 | for (int i = 0; i < ARRAY_SIZE(invalid); i++) { |
| 44 | decoded = malloc(strlen(invalid[i]) * sizeof(char)); |
| 45 | |
| 46 | res = b64_decode((uint8_t *)invalid[i], strlen(invalid[i]), decoded); |
| 47 | |
| 48 | assert_int_equal(res, 0); |
| 49 | |
| 50 | free(decoded); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | int main(void) |
| 55 | { |
| 56 | const struct CMUnitTest tests[] = { |
| 57 | cmocka_unit_test(test_b64_decode), |
| 58 | }; |
| 59 | |
| 60 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 61 | } |