summaryrefslogtreecommitdiff
path: root/tests/time.c
blob: 7f5240d0264fa6409b76fb0aea1d7f69611ee990 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (C) 2019 Red Hat, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * GnuTLS is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuTLS is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 *
 */

/* That's a unit test of _gnutls_utcTime2gtime() and _gnutls_x509_generalTime2gtime()
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#include <string.h>
#include <gnutls/gnutls.h>

#include "utils.h"

time_t _gnutls_utcTime2gtime(const char *ttime);
time_t _gnutls_x509_generalTime2gtime(const char *ttime);

struct time_tests_st {
	const char *time_str;
	time_t utime;
};

struct time_tests_st general_time_tests[] = {
	{
		.time_str = "20190520133237Z",
		.utime = 1558359157
	},
	{
		.time_str = "20170101000000Z",
		.utime = 1483228800
	},
	{
		.time_str = "19700101000000Z",
		.utime = 0
	},
};

struct time_tests_st utc_time_tests[] = {
	{
		.time_str = "190520133237",
		.utime = 1558359157
	},
	{
		.time_str = "170101000000Z",
		.utime = 1483228800
	},
};


void doit(void)
{
	time_t t;
	unsigned i;

	for (i=0;i<sizeof(general_time_tests)/sizeof(general_time_tests[0]);i++) {
		t = _gnutls_x509_generalTime2gtime(general_time_tests[i].time_str);
		if (t != general_time_tests[i].utime) {
			fprintf(stderr, "%s: Error in GeneralTime conversion\n", general_time_tests[i].time_str);
			fprintf(stderr, "got: %lu, expected: %lu\n", (unsigned long)t, general_time_tests[i].utime);
		}
	}

	for (i=0;i<sizeof(utc_time_tests)/sizeof(utc_time_tests[0]);i++) {
		t = _gnutls_utcTime2gtime(utc_time_tests[i].time_str);
		if (t != utc_time_tests[i].utime) {
			fprintf(stderr, "%s: Error in utcTime conversion\n", utc_time_tests[i].time_str);
			fprintf(stderr, "got: %lu, expected: %lu\n", (unsigned long)t, utc_time_tests[i].utime);
		}
	}
}