]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: test-base64 - Cleanup: Use the same struct for test input and output
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 28 May 2019 09:51:09 +0000 (12:51 +0300)
committerVille Savolainen <ville.savolainen@dovecot.fi>
Tue, 10 Sep 2019 07:02:22 +0000 (10:02 +0300)
src/lib/test-base64.c

index 974be149cc36855ff845a0f778d540a19ca5fa48..d656aa4238394bb55c3852a2d1b5409696b7c428 100644 (file)
@@ -7,55 +7,52 @@
 
 static void test_base64_encode(void)
 {
-       static const char *input[] = {
-               "hello world",
-               "foo barits",
-               "just niin"
-       };
-       static const char *output[] = {
-               "aGVsbG8gd29ybGQ=",
-               "Zm9vIGJhcml0cw==",
-               "anVzdCBuaWlu"
+       const struct {
+               const char *input;
+               const char *output;
+       } tests[] = {
+               { "hello world", "aGVsbG8gd29ybGQ=" },
+               { "foo barits", "Zm9vIGJhcml0cw==" },
+               { "just niin", "anVzdCBuaWlu" },
        };
        string_t *str;
        unsigned int i;
 
        test_begin("base64_encode()");
        str = t_str_new(256);
-       for (i = 0; i < N_ELEMENTS(input); i++) {
+       for (i = 0; i < N_ELEMENTS(tests); i++) {
                str_truncate(str, 0);
-               base64_encode(input[i], strlen(input[i]), str);
-               test_assert(strcmp(output[i], str_c(str)) == 0);
+               base64_encode(tests[i].input, strlen(tests[i].input), str);
+               test_assert(strcmp(tests[i].output, str_c(str)) == 0);
                test_assert(
                        str_len(str) == MAX_BASE64_ENCODED_SIZE(
-                               strlen(input[i])));
+                               strlen(tests[i].input)));
        }
        test_end();
 }
 
-struct test_base64_decode_output {
-       const char *text;
+struct test_base64_decode {
+       const char *input;
+       const char *output;
        int ret;
        unsigned int src_pos;
 };
 
 static void test_base64_decode(void)
 {
-       static const char *input[] = {
-               "\taGVsbG8gd29ybGQ=",
-               "\nZm9v\n \tIGJh  \t\ncml0cw==",
-               "  anVzdCBuaWlu  \n",
-               "aGVsb",
-               "aGVsb!!!!!",
-               "aGVs!!!!!"
-       };
-       static const struct test_base64_decode_output output[] = {
-               { "hello world", 0, UINT_MAX },
-               { "foo barits", 0, UINT_MAX },
-               { "just niin", 1, UINT_MAX },
-               { "hel", 1, 4 },
-               { "hel", -1, 4 },
-               { "hel", -1, 4 }
+       static const struct test_base64_decode tests[] = {
+               { "\taGVsbG8gd29ybGQ=",
+                 "hello world", 0, UINT_MAX },
+               { "\nZm9v\n \tIGJh  \t\ncml0cw==",
+                 "foo barits", 0, UINT_MAX },
+               { "  anVzdCBuaWlu  \n",
+                 "just niin", 1, UINT_MAX },
+               { "aGVsb",
+                 "hel", 1, 4 },
+               { "aGVsb!!!!!",
+                 "hel", -1, 4 },
+               { "aGVs!!!!!",
+                 "hel", -1, 4 },
        };
        string_t *str;
        unsigned int i;
@@ -64,22 +61,22 @@ static void test_base64_decode(void)
 
        test_begin("base64_decode()");
        str = t_str_new(256);
-       for (i = 0; i < N_ELEMENTS(input); i++) {
+       for (i = 0; i < N_ELEMENTS(tests); i++) {
                str_truncate(str, 0);
 
                src_pos = 0;
-               ret = base64_decode(input[i], strlen(input[i]), &src_pos, str);
-
-               test_assert(output[i].ret == ret &&
-                           strcmp(output[i].text, str_c(str)) == 0 &&
-                           (src_pos == output[i].src_pos ||
-                            (output[i].src_pos == UINT_MAX &&
-                             src_pos == strlen(input[i]))));
+               ret = base64_decode(tests[i].input, strlen(tests[i].input),
+                                   &src_pos, str);
 
+               test_assert(tests[i].ret == ret &&
+                           strcmp(tests[i].output, str_c(str)) == 0 &&
+                           (src_pos == tests[i].src_pos ||
+                            (tests[i].src_pos == UINT_MAX &&
+                             src_pos == strlen(tests[i].input))));
                if (ret >= 0) {
                        test_assert(
                                str_len(str) <= MAX_BASE64_DECODED_SIZE(
-                                       strlen(input[i])));
+                                       strlen(tests[i].input)));
                }
        }
        test_end();