Call tzset() after setenv("TZ", ...) in unit tests

POSIX does not require localtime_r() to behave as if tzset() was called,
so the TZ environment change isn't picked up if some library has already
primed libc's tz cache.  Loading pkcs11-provider during OpenSSL init
does exactly that, causing the time and dnstap cmocka tests to format
timestamps in UTC instead of the requested zone.

Assisted-by: Claude:claude-opus-4-7
(cherry picked from commit c14f7881f2)
This commit is contained in:
Michal Nowak 2026-05-14 12:28:06 +00:00
parent 1805817c3c
commit e574119a19
3 changed files with 12 additions and 0 deletions

View file

@ -18,6 +18,7 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define UNIT_TESTING
@ -61,6 +62,7 @@ setup(void **state) {
* the testdata was originally generated.
*/
setenv("TZ", "PDT8", 1);
tzset();
setup_loopmgr(state);

View file

@ -48,6 +48,7 @@ setup_test(void **state) {
UNUSED(state);
setenv("TZ", "", 1);
tzset();
return 0;
}

View file

@ -19,6 +19,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define UNIT_TESTING
#include <cmocka.h>
@ -144,6 +145,7 @@ ISC_RUN_TEST_IMPL(isc_time_parsehttptimestamp_test) {
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
tzset();
t = isc_time_now();
isc_time_formathttptimestamp(&t, buf, sizeof(buf));
@ -161,6 +163,7 @@ ISC_RUN_TEST_IMPL(isc_time_formatISO8601_test) {
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
tzset();
t = isc_time_now();
/* check formatting: yyyy-mm-ddThh:mm:ssZ */
@ -195,6 +198,7 @@ ISC_RUN_TEST_IMPL(isc_time_formatISO8601ms_test) {
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
tzset();
t = isc_time_now();
/* check formatting: yyyy-mm-ddThh:mm:ss.sssZ */
@ -230,6 +234,7 @@ ISC_RUN_TEST_IMPL(isc_time_formatISO8601us_test) {
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
tzset();
t = isc_time_now_hires();
/* check formatting: yyyy-mm-ddThh:mm:ss.ssssssZ */
@ -265,6 +270,7 @@ ISC_RUN_TEST_IMPL(isc_time_formatISO8601L_test) {
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
tzset();
t = isc_time_now();
/* check formatting: yyyy-mm-ddThh:mm:ss */
@ -298,6 +304,7 @@ ISC_RUN_TEST_IMPL(isc_time_formatISO8601Lms_test) {
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
tzset();
t = isc_time_now();
/* check formatting: yyyy-mm-ddThh:mm:ss.sss */
@ -332,6 +339,7 @@ ISC_RUN_TEST_IMPL(isc_time_formatISO8601Lus_test) {
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
tzset();
t = isc_time_now_hires();
/* check formatting: yyyy-mm-ddThh:mm:ss.ssssss */
@ -366,6 +374,7 @@ ISC_RUN_TEST_IMPL(isc_time_formatshorttimestamp_test) {
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
tzset();
t = isc_time_now();
/* check formatting: yyyymmddhhmmsssss */