Blame tests/virt-time.h

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