From 87c80f0a99bad4d9b3ce5b4827b9668db8c8f7d7 Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Oct 11 2011 19:56:31 +0000 Subject: - wrap time() to get a predictable value when we're testing timeout values --- diff --git a/src/Makefile.am b/src/Makefile.am index 4087b58..9d3b431 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -97,26 +97,26 @@ libcm_a_LIBADD = $(libcm_o_a_OBJECTS) bin_PROGRAMS = getcert pkglibexec_PROGRAMS = certmonger-session pkglibexecdir = $(libexecdir)/$(PACKAGE) -getcert_SOURCES = getcert.c +getcert_SOURCES = getcert.c tm.c tm.h getcert_LDADD = libcm.a $(GETCERT_LIBS) $(KRB5_LIBS) if WITH_IPA bin_PROGRAMS += ipa-getcert -ipa_getcert_SOURCES = ipa-getcert.c +ipa_getcert_SOURCES = ipa-getcert.c tm.c tm.h ipa_getcert_LDADD = $(getcert_LDADD) endif if WITH_IPA bin_PROGRAMS += certmaster-getcert -certmaster_getcert_SOURCES = certmaster-getcert.c +certmaster_getcert_SOURCES = certmaster-getcert.c tm.c tm.h certmaster_getcert_LDADD = $(getcert_LDADD) endif bin_PROGRAMS += selfsign-getcert -selfsign_getcert_SOURCES = selfsign-getcert.c +selfsign_getcert_SOURCES = selfsign-getcert.c tm.c tm.h selfsign_getcert_LDADD = $(getcert_LDADD) sbin_PROGRAMS = certmonger -certmonger_SOURCES = main.c env-system.c +certmonger_SOURCES = main.c env-system.c tm.c tm.h certmonger_LDADD = libcm.a \ $(OPENSSL_LIBS) $(CERTMONGER_LIBS) $(KRB5_LIBS) $(UUID_LIBS) -certmonger_session_SOURCES = main.c env-session.c +certmonger_session_SOURCES = main.c env-session.c tm.c tm.h certmonger_session_LDADD = libcm.a \ $(OPENSSL_LIBS) $(CERTMONGER_LIBS) $(KRB5_LIBS) $(UUID_LIBS) noinst_PROGRAMS = tdbusm-check serial-check nl-check submit-x toklist @@ -124,7 +124,8 @@ tdbusm_check_LDADD = libcm.a $(CERTMONGER_LIBS) serial_check_LDADD = libcm.a $(CERTMONGER_LIBS) nl_check_LDADD = libcm.a $(CERTMONGER_LIBS) submit_x_CFLAGS = $(AM_CFLAGS) -DCM_SUBMIT_X_MAIN -submit_x_SOURCES = submit-x.c submit-x.h submit-u.c submit-u.h log.c log.h +submit_x_SOURCES = submit-x.c submit-x.h submit-u.c submit-u.h log.c log.h \ + tm.c tm.h submit_x_LDADD = $(XMLRPC_LIBS) $(KRB5_LIBS) $(TALLOC_LIBS) $(UUID_LIBS) toklist_CFLAGS = $(AM_CFLAGS) $(NSS_CFLAGS) toklist_LDADD = $(NSS_LIBS) @@ -139,20 +140,22 @@ EXTRA_PROGRAMS += dogtag-submit noinst_PROGRAMS += submit-h submit-d endif ipa_submit_SOURCES = ipa.c submit-x.c submit-x.h submit-u.c submit-u.h \ - submit-e.h util.c util.h log.c log.h + submit-e.h util.c util.h log.c log.h tm.c tm.h ipa_submit_LDADD = $(XMLRPC_LIBS) $(KRB5_LIBS) $(TALLOC_LIBS) $(UUID_LIBS) certmaster_submit_SOURCES = certmaster.c submit-x.c submit-x.h \ - submit-e.h submit-u.c submit-u.h util.c util.h log.c log.h + submit-e.h submit-u.c submit-u.h util.c util.h log.c log.h \ + tm.c tm.h certmaster_submit_LDADD = $(XMLRPC_LIBS) $(KRB5_LIBS) $(TALLOC_LIBS) \ $(UUID_LIBS) dogtag_submit_CFLAGS = $(AM_CFLAGS) $(XML_CFLAGS) $(CURL_LIBS) dogtag_submit_SOURCES = dogtag.c submit-d.c submit-d.h submit-h.c submit-h.h \ - submit-u.c submit-u.h submit-e.h util.c util.h log.c log.h + submit-u.c submit-u.h submit-e.h util.c util.h log.c log.h \ + tm.c tm.h dogtag_submit_LDADD = $(CURL_LIBS) $(XML_LIBS) $(TALLOC_LIBS) $(UUID_LIBS) submit_d_CFLAGS = $(AM_CFLAGS) $(CURL_CFLAGS) $(XML_CFLAGS) -DCM_SUBMIT_D_MAIN submit_d_SOURCES = submit-d.c submit-d.h submit-h.c submit-h.h \ - submit-u.c submit-u.h log.c log.h + submit-u.c submit-u.h log.c log.h tm.c tm.h submit_d_LDADD = $(CURL_LIBS) $(XML_LIBS) $(TALLOC_LIBS) $(UUID_LIBS) submit_h_CFLAGS = $(AM_CFLAGS) $(CURL_CFLAGS) $(XML_CFLAGS) -DCM_SUBMIT_H_MAIN -submit_h_SOURCES = submit-h.c submit-h.h log.c log.h +submit_h_SOURCES = submit-h.c submit-h.h log.c log.h tm.c tm.h submit_h_LDADD = $(CURL_LIBS) $(XML_LIBS) $(TALLOC_LIBS) diff --git a/src/cm.c b/src/cm.c index 4754a86..7b11a4e 100644 --- a/src/cm.c +++ b/src/cm.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009,2010 Red Hat, Inc. + * Copyright (C) 2009,2010,2011 Red Hat, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,6 +38,7 @@ #include "netlink.h" #include "store.h" #include "store-int.h" +#include "tm.h" struct cm_context { int n_entries, should_quit; @@ -397,7 +398,7 @@ cm_add_entry(struct cm_context *context, struct cm_store_entry *new_entry) } else { do { /* Try to assign a new ID. */ - now = time(NULL); + now = cm_time(NULL); new_entry->cm_id = cm_store_timestamp_from_time(now, timestamp); /* Check for duplicates. */ @@ -649,7 +650,7 @@ cm_add_ca(struct cm_context *context, struct cm_store_ca *new_ca) } else { do { /* Try to assign a new ID. */ - now = time(NULL); + now = cm_time(NULL); new_ca->cm_id = cm_store_timestamp_from_time(now, timestamp); /* Check for duplicates. */ diff --git a/src/iterate.c b/src/iterate.c index f33aac4..7ec3525 100644 --- a/src/iterate.c +++ b/src/iterate.c @@ -39,6 +39,7 @@ #include "store.h" #include "store-int.h" #include "submit.h" +#include "tm.h" struct cm_iterate_state { struct cm_keygen_state *cm_keygen_state; @@ -259,7 +260,7 @@ cm_check_expiration_is_noteworthy(struct cm_store_entry *entry) unsigned int i, n_ttls; time_t now, ttl, previous_ttl; const time_t *ttls; - now = time(NULL); + now = cm_time(NULL); /* Do we have validity information? */ if (entry->cm_cert_not_after == 0) { return -1; @@ -329,7 +330,7 @@ cm_iterate(struct cm_store_entry *entry, struct cm_store_ca *ca, old_entry_state = entry->cm_state; if (entry->cm_cert_not_after != 0) { - remaining = entry->cm_cert_not_after - time(NULL); + remaining = entry->cm_cert_not_after - cm_time(NULL); } else { remaining = -1; } @@ -610,7 +611,7 @@ cm_iterate(struct cm_store_entry *entry, struct cm_store_ca *ca, case CM_SUBMITTING: if (cm_submit_ready(entry, state->cm_submit_state) == 0) { - entry->cm_submitted = time(NULL); + entry->cm_submitted = cm_time(NULL); if (cm_submit_issued(entry, state->cm_submit_state) == 0) { /* We're all done. Save the certificate to its diff --git a/src/log.c b/src/log.c index 11ebe0b..4cc8368 100644 --- a/src/log.c +++ b/src/log.c @@ -31,6 +31,7 @@ #include #include "log.h" +#include "tm.h" static int cm_log_level = 0; static enum cm_log_method cm_log_method; @@ -71,9 +72,8 @@ cm_log(int level, const char *fmt, ...) stream = stdout; /* fall through */ case cm_log_stderr: - now = time(NULL); + now = cm_time(NULL); localtime_r(&now, <); - now = time(NULL); p = talloc_asprintf(NULL, "%04d-%02d-%02d %02d:%02d:%02d " "[%lu] %s", diff --git a/src/notify.c b/src/notify.c index a7f8503..37a1a63 100644 --- a/src/notify.c +++ b/src/notify.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Red Hat, Inc. + * Copyright (C) 2009,2011 Red Hat, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,6 +32,7 @@ #include "store.h" #include "store-int.h" #include "subproc.h" +#include "tm.h" struct cm_notify_state { struct cm_subproc_state *subproc; @@ -81,7 +82,7 @@ cm_notify_main(int fd, struct cm_store_ca *ca, struct cm_store_entry *entry, {"debug", LOG_DEBUG}, }; unsigned int i; - if (entry->cm_cert_not_after > time(NULL)) { + if (entry->cm_cert_not_after > cm_time(NULL)) { switch (entry->cm_cert_storage_type) { case cm_cert_storage_nssdb: if (entry->cm_cert_token != NULL) { diff --git a/src/prefs.c b/src/prefs.c index 8f4aae3..0d8d332 100644 --- a/src/prefs.c +++ b/src/prefs.c @@ -30,6 +30,7 @@ #include "store-int.h" #include "submit.h" #include "util.h" +#include "tm.h" static char * cm_prefs_read(void) @@ -167,7 +168,7 @@ cm_prefs_ttls(const time_t **ttls, unsigned int *n_ttls) q = p + strcspn(p, " \t,"); c = *q; *q = '\0'; - if (cm_submit_delta_from_string(p, time(NULL), + if (cm_submit_delta_from_string(p, cm_time(NULL), &config[i]) == 0) { i++; }; diff --git a/src/store-files.c b/src/store-files.c index bf052ea..0b12f26 100644 --- a/src/store-files.c +++ b/src/store-files.c @@ -37,6 +37,7 @@ #include "store.h" #include "store-int.h" #include "log.h" +#include "tm.h" enum cm_store_file_field { cm_store_file_field_invalid = 0, @@ -797,7 +798,7 @@ cm_store_entry_write(FILE *fp, struct cm_store_entry *entry) const char *p; if (entry->cm_id == NULL) { - p = cm_store_timestamp_from_time(time(NULL), timestamp); + p = cm_store_timestamp_from_time(cm_time(NULL), timestamp); } else { p = entry->cm_id; } @@ -1012,7 +1013,7 @@ cm_store_entry_save(struct cm_store_entry *entry) const char *directory; if (entry->cm_store_private == NULL) { - cm_store_timestamp_from_time(time(NULL), timestamp); + cm_store_timestamp_from_time(cm_time(NULL), timestamp); directory = cm_env_request_dir(); snprintf(path, sizeof(path), "%s/%s", directory, timestamp); fd = open(path, @@ -1140,7 +1141,7 @@ cm_store_ca_write(FILE *fp, struct cm_store_ca *ca) char timestamp[15]; if (ca->cm_id == NULL) { - p = cm_store_timestamp_from_time(time(NULL), timestamp); + p = cm_store_timestamp_from_time(cm_time(NULL), timestamp); } else { p = ca->cm_id; } @@ -1207,7 +1208,7 @@ cm_store_ca_save(struct cm_store_ca *ca) const char *directory; if (ca->cm_store_private == NULL) { - cm_store_timestamp_from_time(time(NULL), timestamp); + cm_store_timestamp_from_time(cm_time(NULL), timestamp); directory = cm_env_ca_dir(); snprintf(path, sizeof(path), "%s/%s", directory, timestamp); fd = open(path, diff --git a/src/submit-so.c b/src/submit-so.c index 0271682..2edef0c 100644 --- a/src/submit-so.c +++ b/src/submit-so.c @@ -47,6 +47,7 @@ #include "submit-int.h" #include "submit-u.h" #include "subproc.h" +#include "tm.h" #include "util-o.h" struct cm_submit_state { @@ -86,7 +87,7 @@ cm_submit_so_main(int fd, struct cm_store_ca *ca, struct cm_store_entry *entry, if (ca->cm_ca_internal_force_issue_time) { now = ca->cm_ca_internal_issue_time; } else { - now = time(NULL); + now = cm_time(NULL); } keyfp = fopen(entry->cm_key_storage_location, "r"); if (cm_submit_delta_from_string(cm_prefs_validity_period(), now, diff --git a/src/tm.c b/src/tm.c new file mode 100644 index 0000000..da248ba --- /dev/null +++ b/src/tm.c @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2011 Red Hat, Inc. + * + * This program 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. + * + * This program 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 General Public License + * along with this program. If not, see . + */ + +#include "config.h" + +#include + +#include "tm.h" + +time_t +cm_time(time_t *dest) +{ + return time(dest); +} diff --git a/src/tm.h b/src/tm.h new file mode 100644 index 0000000..a026c4f --- /dev/null +++ b/src/tm.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2011 Red Hat, Inc. + * + * This program 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. + * + * This program 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 General Public License + * along with this program. If not, see . + */ + +#ifndef cmtm_h +#define cmtm_h + +time_t cm_time(time_t *dest); + +#endif diff --git a/tests/010-iterate/run.sh b/tests/010-iterate/run.sh index f552dbb..f6fd5b1 100755 --- a/tests/010-iterate/run.sh +++ b/tests/010-iterate/run.sh @@ -286,17 +286,18 @@ $toolsdir/iterate ca3 entry3 "" # Note! The "iterate" harness rounds delay times up to the next multiple of 50. for interval in 0 30 1800 3600 7200 86000 86500 604800 1000000 2000000; do + now=`date +%s` + CM_FORCE_TIME=$now ; export CM_FORCE_TIME + when=`expr $now + $interval` + later=`env TZ=UTC date -d @$when +%Y%m%d%H%M%S` for ca in ca-unreachable ca-ask-again ca-unconfigured ; do echo echo '[CA poll timeout remaining='$interval'.]' - now=`date +%s` - when=`expr $now + $interval` - then=`env TZ=UTC date -d @$when +%Y%m%d%H%M%S` cat > entry4 <<- EOF id=Test ca_name=Lostie state=HAVE_CSR - cert_not_after=$then + cert_not_after=$later csr=AAAA EOF cat > ca4 <<- EOF @@ -308,14 +309,11 @@ for interval in 0 30 1800 3600 7200 86000 86500 604800 1000000 2000000; do done echo echo '[Monitor poll timeout remaining='$interval'.]' - now=`date +%s` - when=`expr $now + $interval` - then=`env TZ=UTC date -d @$when +%Y%m%d%H%M%S` cat > entry4 <<- EOF id=Test ca_name=Lostie state=MONITORING - cert_not_after=$then + cert_not_after=$later csr=AAAA EOF cat > ca4 <<- EOF diff --git a/tests/tools/Makefile.am b/tests/tools/Makefile.am index fe2d363..dd58283 100644 --- a/tests/tools/Makefile.am +++ b/tests/tools/Makefile.am @@ -2,4 +2,4 @@ AM_CFLAGS = $(TALLOC_CFLAGS) $(TEVENT_CFLAGS) $(DBUS_CFLAGS) \ -I$(builddir)/../../src listnicks_CFLAGS = $(AM_CFLAGS) $(NSS_CFLAGS) noinst_PROGRAMS = keyiread keygen csrgen submit certread certsave oid2name name2oid iterate prefs notty dates listnicks -LDADD = ../../src/libcm.a $(srcdir)/../../src/env-system.c @OPENSSL_LIBS@ @CERTMONGER_LIBS@ @KRB5_LIBS@ @UUID_LIBS@ +LDADD = ../../src/libcm.a $(srcdir)/../../src/env-system.c tm.c @OPENSSL_LIBS@ @CERTMONGER_LIBS@ @KRB5_LIBS@ @UUID_LIBS@ diff --git a/tests/tools/iterate.c b/tests/tools/iterate.c index 829528d..1ebb396 100644 --- a/tests/tools/iterate.c +++ b/tests/tools/iterate.c @@ -129,7 +129,7 @@ main(int argc, char **argv) talloc_free(tmp); } if (when == cm_time_delay) { - printf("delay=%ld\n", (long) howmany(delay, 50) * 50); + printf("delay=%ld\n", delay); } /* If we didn't find a match, stop here. */ if (*p == '\0') { diff --git a/tests/tools/tm.c b/tests/tools/tm.c new file mode 100644 index 0000000..d4e70bf --- /dev/null +++ b/tests/tools/tm.c @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2011 Red Hat, Inc. + * + * This program 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. + * + * This program 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 General Public License + * along with this program. If not, see . + */ + +#include "config.h" + +#include +#include + +#include "tm.h" + +time_t +cm_time(time_t *dest) +{ + long t; + if (getenv("CM_FORCE_TIME") != NULL) { + t = atol(getenv("CM_FORCE_TIME")); + if (dest != NULL) { + *dest = t; + } + return t; + } else { + return time(dest); + } +}