test-lib.c revision b5fbbd9b27a204d3acd78d81c91bf5b31f934134
76b43e4417bab52e913da39b5f5bc2a130d3f149Timo Sirainen/* Copyright (c) 2007 Dovecot authors, see the included COPYING file */
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainenstatic void test_base64_encode(void)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen static const char *input[] = {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen "hello world",
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen "foo barits",
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen static const char *output[] = {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen "aGVsbG8gd29ybGQ=",
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen "Zm9vIGJhcml0cw==",
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen "anVzdCBuaWlu"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen unsigned int i;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen base64_encode(input[i], strlen(input[i]), str);
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen test_out(t_strdup_printf("base64_encode(%d)", i), success);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen unsigned int src_pos;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatic void test_base64_decode(void)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen static const char *input[] = {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen "\taGVsbG8gd29ybGQ=",
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen "\nZm9v\n \tIGJh \t\ncml0cw==",
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen " anVzdCBuaWlu \n",
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen "aGVsb!!!!!",
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen static const struct test_base64_decode_output output[] = {
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen unsigned int i;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen ret = base64_decode(input[i], strlen(input[i]), &src_pos, str);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen test_out(t_strdup_printf("base64_decode(%d)", i), success);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatic int cmp_uint(const void *p1, const void *p2)
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen static const unsigned int input[] = {
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen const unsigned int *cur;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (bsearch_insert_pos(&key, cur, len, sizeof(*cur),
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen else if (idx == 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen test_out(t_strdup_printf("bsearch_insert_pos(%d,%d)", i, key),
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen static const unsigned int input_min = 1, input_max = 5;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen static const unsigned int input[] = {
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen for (i = num = 0; input[i] != -1U; num++, i++) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen for (j = start; j < i; j++) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen seq_range_array_invert(&range, input_min, input_max);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen for (seq = input_min; seq <= input_max; seq++) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (seq_range_exists(&range, seq) == old_exists)
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen test_out(t_strdup_printf("seq_range_array_invert(%u)", num),
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const char *str;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen unsigned int max_len;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatic void test_str_sanitize(void)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen static const char *output[] = {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const char *str;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen unsigned int i;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen str = str_sanitize(input[i].str, input[i].max_len);
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen test_out(t_strdup_printf("str_sanitize(%d)", i), success);
return test_deinit();