test-message-date.c revision 39025a2dabfcfaeee3790988b9ea00d19887a3d3
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "lib.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "message-date.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "test-common.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainenstruct test_message_date_output {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen time_t time;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen int tz_offset;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen bool ret;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen};
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainenstatic void test_message_date_parse(void)
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen{
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen static const char *input[] = {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#ifdef TIME_T_SIGNED
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen "Thu, 01 Jan 1970 01:59:59 +0200",
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen "Fri, 13 Dec 1901 20:45:53 +0000",
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#endif
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#if (TIME_T_MAX_BITS > 32 || !defined(TIME_T_SIGNED))
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen "Sun, 07 Feb 2106 06:28:15 +0000",
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#endif
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen "Wed, 07 Nov 2007 01:07:20 +0200",
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen "Wed, 07 Nov 2007 01:07:20",
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen "Thu, 01 Jan 1970 02:00:00 +0200",
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen "Tue, 19 Jan 2038 03:14:07 +0000",
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen "Tue, 19 Jan 2038"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen };
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen static struct test_message_date_output output[] = {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#ifdef TIME_T_SIGNED
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen { -1, 2*60, TRUE },
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen { -2147483647, 0, TRUE },
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#endif
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#if (TIME_T_MAX_BITS > 32 || !defined(TIME_T_SIGNED))
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen { 4294967295, 0, TRUE },
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#endif
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen { 1194390440, 2*60, TRUE },
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen { 1194397640, 0, TRUE },
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen { 0, 2*60, TRUE },
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen { 2147483647, 0, TRUE },
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen { 0, 0, FALSE }
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen };
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen unsigned int i;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen bool success;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen time_t t;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen int tz;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen bool ret;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen for (i = 0; i < N_ELEMENTS(input); i++) {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen ret = message_date_parse((const unsigned char *)input[i],
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen strlen(input[i]), &t, &tz);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen success = (!ret && !output[i].ret) ||
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen (ret == output[i].ret && t == output[i].time &&
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen tz == output[i].tz_offset);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen test_out(t_strdup_printf("message_date_parse(%d)", i), success);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen }
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen}
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainenint main(void)
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen{
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen static void (*test_functions[])(void) = {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen test_message_date_parse,
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen NULL
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen };
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen return test_run(test_functions);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen}