Blame tests/virt-time.h

Packit Service 4684c1
/*
Packit Service 4684c1
 * Copyright (C) 2018 Red Hat, Inc.
Packit Service 4684c1
 *
Packit Service 4684c1
 * Author: Nikos Mavrogiannopoulos
Packit Service 4684c1
 *
Packit Service 4684c1
 * This file is part of GnuTLS.
Packit Service 4684c1
 *
Packit Service 4684c1
 * GnuTLS is free software; you can redistribute it and/or modify it
Packit Service 4684c1
 * under the terms of the GNU General Public License as published by
Packit Service 4684c1
 * the Free Software Foundation; either version 3 of the License, or
Packit Service 4684c1
 * (at your option) any later version.
Packit Service 4684c1
 *
Packit Service 4684c1
 * GnuTLS is distributed in the hope that it will be useful, but
Packit Service 4684c1
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 4684c1
 * General Public License for more details.
Packit Service 4684c1
 *
Packit Service 4684c1
 * You should have received a copy of the GNU Lesser General Public License
Packit Service 4684c1
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit Service 4684c1
 */
Packit Service 4684c1
Packit Service 4684c1
#ifndef GNUTLS_TESTS_VIRT_TIME_H
Packit Service 4684c1
#define GNUTLS_TESTS_VIRT_TIME_H
Packit Service 4684c1
Packit Service 4684c1
#ifdef HAVE_CONFIG_H
Packit Service 4684c1
#include <config.h>
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
#include <time.h>
Packit Service 4684c1
#include <gnutls/gnutls.h>
Packit Service 4684c1
Packit Service 4684c1
/* copied from ../lib/system.h so not to include that header from
Packit Service 4684c1
 * every test program */
Packit Service 4684c1
typedef void (*gnutls_gettime_func) (struct timespec *);
Packit Service 4684c1
extern void _gnutls_global_set_gettime_function(gnutls_gettime_func gettime_func);
Packit Service 4684c1
Packit Service 4684c1
/* virtualize time in a test. This freezes the time in the test, except for
Packit Service 4684c1
 * the advances due to calls to virt_sleep_sec(). This makes the test
Packit Service 4684c1
 * independent of the test system load, and avoids any long delays. */
Packit Service 4684c1
static time_t _now;
Packit Service 4684c1
static struct timespec _now_ts;
Packit Service 4684c1
Packit Service 4684c1
#define virt_sec_sleep(s) { \
Packit Service 4684c1
		_now += s; \
Packit Service 4684c1
		_now_ts.tv_sec += s; \
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
#define virt_time_init() { \
Packit Service 4684c1
		_now = time(0); \
Packit Service 4684c1
		gnutls_global_set_time_function(mytime); \
Packit Service 4684c1
		_now_ts.tv_sec = _now; \
Packit Service 4684c1
		_now_ts.tv_nsec = 0; \
Packit Service 4684c1
		_gnutls_global_set_gettime_function(mygettime); \
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
Packit Service 4684c1
static time_t mytime(time_t * t)
Packit Service 4684c1
{
Packit Service 4684c1
	if (t)
Packit Service 4684c1
		*t = _now;
Packit Service 4684c1
Packit Service 4684c1
	return _now;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static void mygettime(struct timespec * t)
Packit Service 4684c1
{
Packit Service 4684c1
	if (t)
Packit Service 4684c1
		*t = _now_ts;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
#endif /* GNUTLS_TESTS_VIRT_TIME_H */