diff options
author | Nikos Mavrogiannopoulos <nmav@redhat.com> | 2019-05-20 15:47:41 +0200 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2019-05-20 21:37:05 +0200 |
commit | 45e2c62020c272ec9ef44057edcc4261969b5256 (patch) | |
tree | f64bf135ab56d8722fe2c098704a0e48af92c2e3 /tests | |
parent | 51c16c255b9153ffc8e22d23616456fa269b5e98 (diff) | |
download | gnutls-45e2c62020c272ec9ef44057edcc4261969b5256.tar.gz |
tests: added unit tests of utc and generalTime convertor
Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/time.c | 94 |
2 files changed, 95 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index eb65e94858..551c029c1f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -150,7 +150,7 @@ ctests += mini-record-2 simple gnutls_hmac_fast set_pkcs12_cred cert certuniquei mini-termination mini-x509-cas mini-x509-2 pkcs12_simple tls-pthread \ mini-emsgsize-dtls chainverify-unsorted mini-overhead tls12-ffdhe \ mini-dtls-heartbeat mini-x509-callbacks key-openssl priorities priorities-groups \ - gnutls_x509_privkey_import gnutls_x509_crt_list_import \ + gnutls_x509_privkey_import gnutls_x509_crt_list_import time \ sign-verify-ext4 tls-neg-ext4-key resume-lifetime memset0 memset1 \ mini-dtls-srtp rsa-encrypt-decrypt mini-loss-time gnutls-strcodes \ mini-record mini-dtls-record handshake-timeout mini-record-range \ diff --git a/tests/time.c b/tests/time.c new file mode 100644 index 0000000000..7f5240d026 --- /dev/null +++ b/tests/time.c @@ -0,0 +1,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); + } + } +} + |