bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen#include "test-lib.h"
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen#include "array.h"
86441ffc028f11857152c15fe7b0d24ff0874504Timo Sirainen
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainenstatic void test_p_strdup(void)
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen{
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_begin("p_strdup()");
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(p_strdup(default_pool, NULL) == NULL);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen const char *src = "foo";
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen char *str = p_strdup(default_pool, src);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(str != src && str != NULL && strcmp(src, str) == 0);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen p_free(default_pool, str);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_end();
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen}
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainenstatic void test_p_strdup_empty(void)
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen{
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_begin("p_strdup_empty()");
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(p_strdup_empty(default_pool, NULL) == NULL);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(p_strdup_empty(default_pool, "") == NULL);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen const char *src = "foo";
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen char *str = p_strdup_empty(default_pool, src);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(str != src && str != NULL && strcmp(src, str) == 0);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen p_free(default_pool, str);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_end();
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen}
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainenstatic void test_p_strdup_until(void)
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen{
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen const char src[] = "foo\0bar";
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen char *str;
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_begin("p_strdup_until()");
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen str = p_strdup_until(default_pool, src, src+2);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(strcmp(str, "fo") == 0);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen p_free(default_pool, str);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen str = p_strdup_until(default_pool, src, src+3);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(strcmp(str, "foo") == 0);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen p_free(default_pool, str);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen /* \0 is ignored */
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen str = p_strdup_until(default_pool, src, src+7);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(memcmp(str, src, sizeof(src)) == 0);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen p_free(default_pool, str);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen str = p_strdup_until(default_pool, src, src+8);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(memcmp(str, src, sizeof(src)) == 0);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen p_free(default_pool, str);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_end();
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen}
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainenstatic void test_p_strarray_dup(void)
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen{
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen const char *input[][3] = {
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen { NULL },
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen { "a", NULL },
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen { "foobar", NULL },
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen { "a", "foo", NULL }
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen };
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen const char **ret;
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen unsigned int i, j;
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen test_begin("p_strarray_dup");
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen for (i = 0; i < N_ELEMENTS(input); i++) {
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen ret = p_strarray_dup(default_pool, input[i]);
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen for (j = 0; input[i][j] != NULL; j++) {
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen test_assert(strcmp(input[i][j], ret[j]) == 0);
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen test_assert(input[i][j] != ret[j]);
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen }
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen test_assert(ret[j] == NULL);
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen i_free(ret);
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen }
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen test_end();
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen}
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen
a1fff894178eb4279a3acb44948c1ba98db104deTimo Sirainenstatic void test_t_strsplit(void)
a1fff894178eb4279a3acb44948c1ba98db104deTimo Sirainen{
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen struct {
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen const char *input;
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen const char *const *output;
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen } tests[] = {
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen /* empty string -> empty array. was this perhaps a mistake for
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen the API to do this originally?.. can't really change now
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen anyway. */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "", (const char *const []) { NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "\n", (const char *const []) { "", "", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "\n\n", (const char *const []) { "", "", "", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo", (const char *const []) { "foo", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo\n", (const char *const []) { "foo", "", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo\nbar", (const char *const []) { "foo", "bar", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo\nbar\n", (const char *const []) { "foo", "bar", "", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "\nfoo\n\nbar\n\n", (const char *const []) { "", "foo", "", "bar", "", "", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen };
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen const char *const *args, *const *args2, *const *args3;
a1fff894178eb4279a3acb44948c1ba98db104deTimo Sirainen
a1fff894178eb4279a3acb44948c1ba98db104deTimo Sirainen test_begin("t_strsplit");
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) {
b7b202953ea655bba74ca8b0236694c722a0b771Timo Sirainen /* split_str_fast() with single separator */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen args = t_strsplit(tests[i].input, "\n");
b7b202953ea655bba74ca8b0236694c722a0b771Timo Sirainen /* split_str_slow() with a secondary separator */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen args2 = t_strsplit(tests[i].input, "\r\n");
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen /* also as suffix */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen args3 = t_strsplit(tests[i].input, "\n\r");
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen for (unsigned int j = 0; tests[i].output[j] != NULL; j++) {
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen test_assert_idx(null_strcmp(tests[i].output[j], args[j]) == 0, i);
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen test_assert_idx(null_strcmp(args[j], args2[j]) == 0, i);
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen test_assert_idx(null_strcmp(args[j], args3[j]) == 0, i);
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen }
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen }
a1fff894178eb4279a3acb44948c1ba98db104deTimo Sirainen test_end();
a1fff894178eb4279a3acb44948c1ba98db104deTimo Sirainen}
a1fff894178eb4279a3acb44948c1ba98db104deTimo Sirainen
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainenstatic void test_t_strsplit_spaces(void)
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen{
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen struct {
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen const char *input;
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen const char *const *output;
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen } tests[] = {
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen /* empty strings */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "", (const char *const []) { NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "\n", (const char *const []) { NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "\n\n", (const char *const []) { NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen /* normal */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo", (const char *const []) { "foo", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo\n", (const char *const []) { "foo", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo\nbar", (const char *const []) { "foo", "bar", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo\nbar\n", (const char *const []) { "foo", "bar", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "\nfoo\n\nbar\n\n", (const char *const []) { "foo", "bar", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen };
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen const char *const *args, *const *args2, *const *args3;
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen test_begin("t_strsplit_spaces");
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) {
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen args = t_strsplit_spaces(tests[i].input, "\n");
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen /* test also with a secondary nonexistent separator */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen args2 = t_strsplit_spaces(tests[i].input, "\r\n");
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen /* also as suffix */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen args3 = t_strsplit_spaces(tests[i].input, "\n\r");
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen for (unsigned int j = 0; tests[i].output[j] != NULL; j++) {
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen test_assert_idx(null_strcmp(tests[i].output[j], args[j]) == 0, i);
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen test_assert_idx(null_strcmp(args[j], args2[j]) == 0, i);
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen test_assert_idx(null_strcmp(args[j], args3[j]) == 0, i);
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen }
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen }
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen /* multiple separators */
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen args = t_strsplit_spaces(" , , ,str1 , ,,, , str2 , ", " ,");
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen test_assert(strcmp(args[0], "str1") == 0);
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen test_assert(strcmp(args[1], "str2") == 0);
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen test_assert(args[2] == NULL);
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen test_end();
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen}
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainenstatic void test_t_str_replace(void)
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen{
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_begin("t_str_replace");
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("foo", 'a', 'b'), "foo") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("fooa", 'a', 'b'), "foob") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("afooa", 'a', 'b'), "bfoob") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("", 'a', 'b'), "") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("a", 'a', 'b'), "b") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("aaa", 'a', 'b'), "bbb") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("bbb", 'a', 'b'), "bbb") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("aba", 'a', 'b'), "bbb") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_end();
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen}
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Boschstatic void test_t_str_trim(void)
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch{
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_begin("t_str_trim");
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_trim("", " "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_trim(" ", " "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_trim(" \t ", "\t "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_trim("f \t ", "\t "), "f") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("foo", ""), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("foo", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("foo ", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim(" foo", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim(" foo ", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("\tfoo ", "\t "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim(" \tfoo\t ", "\t "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("\r \tfoo\t \r", "\t \r"), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("\r \tfoo foo\t \r", "\t \r"), "foo foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("\tfoo\tfoo\t", "\t \r"), "foo\tfoo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_end();
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch}
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Boschstatic void test_t_str_ltrim(void)
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch{
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_begin("t_str_ltrim");
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_ltrim("", " "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_ltrim(" ", " "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_ltrim(" \t ", "\t "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_ltrim(" \t f", "\t "), "f") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("foo", ""), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("foo", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("foo ", " "), "foo ") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim(" foo", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim(" foo ", " "), "foo ") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("\tfoo ", "\t "), "foo ") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim(" \tfoo\t ", "\t "), "foo\t ") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("\r \tfoo\t \r", "\t \r"), "foo\t \r") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("\r \tfoo foo\t \r", "\t \r"), "foo foo\t \r") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("\tfoo\tfoo\t", "\t \r"), "foo\tfoo\t") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_end();
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch}
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Boschstatic void test_t_str_rtrim(void)
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch{
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_begin("t_str_rtrim");
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_rtrim("", " "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_rtrim(" ", " "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_rtrim(" \t ", "\t "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_rtrim("f \t ", "\t "), "f") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("foo", ""), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("foo", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("foo ", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim(" foo", " "), " foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim(" foo ", " "), " foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("\tfoo ", "\t "), "\tfoo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim(" \tfoo\t ", "\t "), " \tfoo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("\r \tfoo\t \r", "\t \r"), "\r \tfoo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("\r \tfoo foo\t \r", "\t \r"), "\r \tfoo foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("\tfoo\tfoo\t", "\t \r"), "\tfoo\tfoo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_end();
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch}
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainenstatic const char *const test_strarray_input[] = {
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen "", "hello", "world", "", "yay", "", NULL
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen};
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainenstatic const struct {
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen const char *separator;
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen const char *output;
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen} test_strarray_outputs[] = {
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen { "", "helloworldyay" },
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi { " ", " hello world yay " },
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi { "!-?", "!-?hello!-?world!-?!-?yay!-?" }
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi};
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvistatic const char *const test_strarray_input2[] = {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi "", "", "hello", "world", "", "yay", "", NULL
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi};
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvistatic struct {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi const char *separator;
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi const char *output;
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi} test_strarray_outputs2[] = {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi { "", "helloworldyay" },
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi { " ", " hello world yay " },
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi { "!-?", "!-?!-?hello!-?world!-?!-?yay!-?" }
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi};
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvistatic const char *const test_strarray_input3[] = {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi "hello", "", "", "yay", NULL
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi};
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvistatic struct {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi const char *separator;
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi const char *output;
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi} test_strarray_outputs3[] = {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi { "", "helloyay" },
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi { " ", "hello yay" },
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi { "!-?", "hello!-?!-?!-?yay" }
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen};
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainenstatic void test_t_strarray_join(void)
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen{
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen const char *null = NULL;
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen unsigned int i;
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen test_begin("t_strarray_join()");
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen /* empty array -> empty string */
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen test_assert(strcmp(t_strarray_join(&null, " "), "") == 0);
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen for (i = 0; i < N_ELEMENTS(test_strarray_outputs); i++) {
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen test_assert_idx(strcmp(t_strarray_join(test_strarray_input,
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen test_strarray_outputs[i].separator),
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen test_strarray_outputs[i].output) == 0, i);
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen }
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi for (i = 0; i < N_ELEMENTS(test_strarray_outputs2); i++) {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi test_assert_idx(strcmp(t_strarray_join(test_strarray_input2,
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi test_strarray_outputs2[i].separator),
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi test_strarray_outputs2[i].output) == 0, i);
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi }
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi for (i = 0; i < N_ELEMENTS(test_strarray_outputs3); i++) {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi test_assert_idx(strcmp(t_strarray_join(test_strarray_input3,
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi test_strarray_outputs3[i].separator),
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi test_strarray_outputs3[i].output) == 0, i);
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi }
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen test_end();
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen}
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainenstatic void test_p_array_const_string_join(void)
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen{
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen ARRAY_TYPE(const_string) arr;
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen unsigned int i;
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen char *res;
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen test_begin("p_array_const_string_join()");
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen i_array_init(&arr, 2);
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen /* empty array -> empty string */
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen test_assert(strcmp(t_array_const_string_join(&arr, " "), "") == 0);
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen array_append(&arr, test_strarray_input,
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen str_array_length(test_strarray_input));
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen for (i = 0; i < N_ELEMENTS(test_strarray_outputs); i++) {
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen res = p_array_const_string_join(default_pool, &arr,
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen test_strarray_outputs[i].separator);
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen test_assert_idx(strcmp(res, test_strarray_outputs[i].output) == 0, i);
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen i_free(res);
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen }
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen array_free(&arr);
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen test_end();
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen}
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainenstatic void test_mem_equals_timing_safe(void)
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen{
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen const struct {
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen const char *a, *b;
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen } tests[] = {
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen { "", "" },
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen { "a", "a" },
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen { "b", "a" },
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen { "ab", "ab" },
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen { "ab", "ba" },
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen { "ab", "bc" },
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen };
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen test_begin("mem_equals_timing_safe()");
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) {
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen size_t len = strlen(tests[i].a);
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen i_assert(len == strlen(tests[i].b));
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen test_assert((memcmp(tests[i].a, tests[i].b, len) == 0) ==
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen mem_equals_timing_safe(tests[i].a, tests[i].b, len));
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen test_assert((memcmp(tests[i].a, tests[i].b, len) == 0) ==
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen mem_equals_timing_safe(tests[i].b, tests[i].a, len));
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen }
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen test_end();
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen}
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainenstatic void test_dec2str_buf(void)
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen{
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen const uintmax_t test_input[] = {
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen 99, 999, 9999, 65535, 65536, 99999, 999999, 9999999,
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen 99999999, 999999999, 4294967295, 4294967296ULL,
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen 9999999999999999999ULL,
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen 18446744073709551615ULL
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen };
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen char buf[MAX_INT_STRLEN], buf2[MAX_INT_STRLEN];
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen test_begin("dec2str_buf()");
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(test_input); i++) {
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen i_snprintf(buf2, sizeof(buf2), "%ju", test_input[i]);
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen test_assert_idx(strcmp(dec2str_buf(buf, test_input[i]),
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen buf2) == 0, i);
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen }
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen test_end();
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen}
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainenvoid test_strfuncs(void)
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen{
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_p_strdup();
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_p_strdup_empty();
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_p_strdup_until();
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen test_p_strarray_dup();
a1fff894178eb4279a3acb44948c1ba98db104deTimo Sirainen test_t_strsplit();
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen test_t_strsplit_spaces();
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_t_str_replace();
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_t_str_trim();
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_t_str_ltrim();
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_t_str_rtrim();
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen test_t_strarray_join();
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen test_p_array_const_string_join();
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen test_mem_equals_timing_safe();
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen test_dec2str_buf();
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen}