test-lib.c revision 49a7e4dba84bbf35d82669d1ae79ad43949eed19
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
047c00cd3f7f403672f81569413669238df8c15aTimo Sirainenstatic void test_array(void)
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen int input[] = { -1234567890, -272585721, 2724859223U, 824725652 };
f37ecd72aad9b806aae83f71bacafdce32146945Timo Sirainen unsigned int i, j;
8b5c520883aa37bb55646286d375fdbae294d710Timo Sirainen output = i == 0 ? NULL : array_idx(&intarr, 0);
0679f8a70a8dda43b204ae35fc6a903818cc6584Timo Sirainen for (j = 0; j < i; j++) {
b68b98e1545bad8af9cb58ef89e8d7f6e16577beAki Tuomistatic void test_base64_encode(void)
b68b98e1545bad8af9cb58ef89e8d7f6e16577beAki Tuomi static const char *input[] = {
b68b98e1545bad8af9cb58ef89e8d7f6e16577beAki Tuomi "hello world",
b68b98e1545bad8af9cb58ef89e8d7f6e16577beAki Tuomi "foo barits",
b68b98e1545bad8af9cb58ef89e8d7f6e16577beAki Tuomi static const char *output[] = {
b68b98e1545bad8af9cb58ef89e8d7f6e16577beAki Tuomi "aGVsbG8gd29ybGQ=",
b68b98e1545bad8af9cb58ef89e8d7f6e16577beAki Tuomi "Zm9vIGJhcml0cw==",
b68b98e1545bad8af9cb58ef89e8d7f6e16577beAki Tuomi "anVzdCBuaWlu"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int i, j, max;
72f21884c0bb9bb26edad63623427ac2120901eaStephan Bosch base64_encode(input[i], strlen(input[i]), str);
72f21884c0bb9bb26edad63623427ac2120901eaStephan Bosch test_out(t_strdup_printf("base64_encode(%d)", i), success);
009217abb57a24a4076092e8e4e165545747839eStephan Bosch for (i = 0; i < 1000; i++) {
b68b98e1545bad8af9cb58ef89e8d7f6e16577beAki Tuomi for (j = 0; j < max; j++)
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen base64_decode(str_data(str), str_len(str), NULL, dest);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int src_pos;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstatic void test_base64_decode(void)
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen static const char *input[] = {
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "\taGVsbG8gd29ybGQ=",
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "\nZm9v\n \tIGJh \t\ncml0cw==",
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen " anVzdCBuaWlu \n",
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "aGVsb!!!!!",
578ef2538ccf42e2a48234c24a8b709397101d88Timo Sirainen static const struct test_base64_decode_output output[] = {
d6b3cfd855c0eebed68be50d3111de1b5a6afeb0Timo Sirainen unsigned int i;
8a0a8c982a6ffc75a4b1c8717b6180a811655794Timo Sirainen ret = base64_decode(input[i], strlen(input[i]), &src_pos, str);
e0740628f6ca05f4bc79a9d8a90b650f4d38d4d0Timo Sirainen test_out(t_strdup_printf("base64_decode(%d)", i), success);
3f603ef00e35fca21605afa0ad8d76e94fee2b96Timo Sirainenstatic int cmp_uint(const void *p1, const void *p2)
ccf50662cc02b5e703039a4ff7f91a4470e25b71Timo Sirainen static const unsigned int input[] = {
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen const unsigned int *cur;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen if (bsearch_insert_pos(&key, cur, len, sizeof(*cur),
1ffb2afe6d7e8860a2231a4827078cf2ef9c22cdTimo Sirainen else if (idx == 0)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen test_out(t_strdup_printf("bsearch_insert_pos(%d,%d)", i, key),
7f1b897201d80c83c96b0d663f2a14c517d48f14Timo Sirainenstatic void test_buffer(void)
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned char *p, testdata[BUF_TEST_SIZE], shadowbuf[BUF_TEST_SIZE];
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int i, shadowbuf_size;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen for (i = 0; i < BUF_TEST_SIZE; i++)
275385a2ecc58e41dc7df3ce3cd943caaa58c4d1Timo Sirainen for (i = 0; i < BUF_TEST_COUNT; i++) {
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen size = shadowbuf_size = rand() % (buf->used - 1);
if (!zero) {
if (shadowbuf_size == 0)
if (i == BUF_TEST_COUNT)
unsigned int n, i, count;
if (i == deleted_n)
return FALSE;
return TRUE;
return NULL;
static void test_aqueue(void)
T_BEGIN {
} T_END;
for (i = 0; i < size; i++) {
if (bytes[i] != b)
return FALSE;
return TRUE;
static void test_mempool_alloconly(void)
struct test_net_is_in_network_input {
const char *ip;
const char *net;
unsigned int bits;
bool ret;
static void test_net_is_in_network(void)
#ifdef HAVE_IPV6
bool success;
struct pq_test_item {
int num;
static void test_primes(void)
unsigned int i, j, num;
bool success;
static void test_priorityq(void)
static const int input[] = {
static const int output[] = {
int prev;
for (j = 0; j < PQ_MAX_ITEMS; j++) {
for (j = 0; j < PQ_MAX_ITEMS; j++) {
prev = 0;
for (j = 0; j < PQ_MAX_ITEMS; j++) {
static void test_seq_range_array_random(void)
for (i = 0; i < SEQ_RANGE_TEST_COUNT; i++) {
switch (test) {
ret2++;
goto fail;
goto fail;
goto fail;
goto fail;
fail:
if (i == SEQ_RANGE_TEST_COUNT)
static void test_seq_range_array_invert(void)
static const unsigned int input[] = {
start = i;
for (j = start; j < i; j++) {
success);
static void test_seq_range_array_have_common(void)
ret2 = (i & j) != 0;
static void test_seq_range_array(void)
bool ret;
for (i = 0; i < max; i++) {
for (j = 0; j < text_len; j++) {
if (expected_pos < 0) {
if (ret)
return FALSE;
if (!ret)
return FALSE;
return FALSE;
return TRUE;
struct str_find_input {
const char *str;
int pos;
static void test_str_find(void)
static const char *fail_input[] = {
const char *key, *p;
T_BEGIN {
} T_END;
if (!success)
struct str_sanitize_input {
const char *str;
unsigned int max_len;
static void test_str_sanitize(void)
static const char *output[] = {
NULL,
const char *str;
bool success;
struct test_message_date_output {
int tz_offset;
bool ret;
struct test_utc_mktime_input {
static void test_utc_mktime(void)
#ifdef TIME_T_SIGNED
#ifdef TIME_T_SIGNED
time_t t;
bool success;
(long)t, (long)output[i]));
int main(void)
static void (*test_functions[])(void) = {
test_init();
T_BEGIN {
test_functions[i]();
} T_END;
return test_deinit();