Codebase list ell / e39d74f
unit: add base64 failure cases James Prestwood authored 2 years ago Denis Kenzior committed 2 years ago
1 changed file(s) with 46 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
2929
3030 struct base64_decode_test {
3131 const char *input;
32 size_t input_size;
3233 const uint8_t *output;
3334 size_t output_size;
3435 };
7879 assert(!memcmp(decoded, test->output, decoded_size));
7980
8081 l_free(decoded);
82 }
83
84 /* Length != string length */
85 static const struct base64_decode_test error_1 = {
86 .input = "cGxlYXN1cmUu",
87 .input_size = 11
88 };
89
90 /* Length doesn't include pad */
91 static const struct base64_decode_test error_2 = {
92 .input = "bGVhc3VyZS4=",
93 .input_size = 11,
94 };
95
96 /* Length doesn't include pad */
97 static const struct base64_decode_test error_3 = {
98 .input = "ZWFzdXJlLg==",
99 .input_size = 10
100 };
101
102 /* Length correct, but data after padding */
103 static const struct base64_decode_test error_4 = {
104 .input = "ZWFzdXJlLg==bG",
105 .input_size = 14
106 };
107
108 /* Only pad */
109 static const struct base64_decode_test error_5 = {
110 .input = "==",
111 .input_size = 2
112 };
113
114 static void test_base64_error(const void *data)
115 {
116 const struct base64_decode_test *test = data;
117 uint8_t *decoded;
118 size_t decoded_size;
119
120 decoded = l_base64_decode(test->input, test->input_size, &decoded_size);
121 assert(!decoded);
81122 }
82123
83124 struct base64_encode_test {
132173 l_test_add("base64/decode/test2", test_base64_decode, &decode_2);
133174 l_test_add("base64/decode/test3", test_base64_decode, &decode_3);
134175 l_test_add("base64/decode/test4", test_base64_decode, &decode_4);
176 l_test_add("base64/decode/test5", test_base64_error, &error_1);
177 l_test_add("base64/decode/test6", test_base64_error, &error_2);
178 l_test_add("base64/decode/test7", test_base64_error, &error_3);
179 l_test_add("base64/decode/test8", test_base64_error, &error_4);
180 l_test_add("base64/decode/test9", test_base64_error, &error_5);
135181
136182 l_test_add("base64/encode/test1", test_base64_encode, &encode_1);
137183 l_test_add("base64/encode/test2", test_base64_encode, &encode_2);