Skip to content

Commit

Permalink
tests: Fix failing after year 2038
Browse files Browse the repository at this point in the history
For #439
  • Loading branch information
andy5995 committed Mar 26, 2024
1 parent eea0b68 commit 04ddcc2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 26 deletions.
55 changes: 30 additions & 25 deletions src/purging_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,54 @@ bool
is_time_to_purge(st_time *st_time_var, const char *file)
{
const int BUF_TIME = 80;
char time_prev[BUF_TIME];
bool purge_needed = true;

// Open the file for reading
FILE *fp = fopen(file, "r");
bool init = (fp);
if (fp)
{
char time_prev[BUF_TIME];
// Read the previous time from the file
if (fgets(time_prev, sizeof(time_prev), fp) != NULL)
{
trim_whitespace(time_prev);
long prev_time = atol(time_prev);
// fprintf(stderr, "%ld\n", prev_time);

if (fgets(time_prev, sizeof time_prev, fp) == NULL)
// Check if the difference is less than a day
purge_needed = ((st_time_var->now - prev_time) < SECONDS_IN_A_DAY) ? false : true;
}
else
{
print_msg_error();
printf("while getting line from %s\n", file);
perror(__func__);
close_file(&fp, file, __func__);
exit(EXIT_FAILURE);
}

trim_whitespace(time_prev);
close_file(&fp, file, __func__);

if ((st_time_var->now - atoi(time_prev)) < SECONDS_IN_A_DAY)
return false;
fclose(fp); // Close the file after reading
}
else if (errno == ENOENT)
purge_needed = false;

// Open the file for writing to update the time
fp = fopen(file, "w");
if (fp)
{
fprintf(fp, "%ld\n", st_time_var->now);
close_file(&fp, file, __func__);

/*
* if this is the first time the file got created, it's very likely
* indeed that purge does not need to run. Only return FALSE if the
* file didn't previously exist.
*/
return init;
// Check for errors when closing the file after writing
if (fclose(fp) != 0)
{
perror("Error closing file after write");
exit(EXIT_FAILURE);
}
}
else
{
open_err(file, __func__);
exit(errno); // Exit if unable to write the file
}

/*
* if we can't even write this file to the config directory, something
* is not right. Make it fatal.
*/
open_err(file, __func__);
exit(errno);
return purge_needed;
}


Expand Down Expand Up @@ -195,7 +200,7 @@ do_file_purge(char *purge_target, const rmw_options *cli_user_options,
// sleep(1);
}
else
printf("-%s\n", pt_basename);
printf("-%s\n", pt_basename);
}
else
msg_err_remove(trashinfo_entry_realpath, __func__);
Expand Down
9 changes: 8 additions & 1 deletion src/time_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ set_time_string(char *tm_str, const size_t len, const char *format,
time_t time_t_now)
{
struct tm result;
localtime_r(&time_t_now, &result);
if (localtime_r(&time_t_now, &result) == NULL)
{
fputs("Error: localtime_r() failed for time_t value beyond 32-bit limit.\n", stderr);
exit(EXIT_FAILURE);
}
strftime(tm_str, len, format, &result);
trim_whitespace(tm_str);

return;
}

/*!
Expand Down Expand Up @@ -64,6 +70,7 @@ void
init_time_vars(st_time *x)
{
x->now = time(NULL);
// x->now = 0x80000000;

set_which_deletion_date(x->t_fmt);

Expand Down
23 changes: 23 additions & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@ add_test_setup(
env: 'RMW_FAKE_MEDIA_ROOT=true'
)

#
# This does not work
#
#faketime_lib = cc.find_library(
# 'faketime',
# dirs: [
# '/usr/lib/faketime',
# '/lib/x86_64-linux-gnu/faketime',
# '/lib/aarch64-linux-gnu/faketime'
# ],
# required: false
# )

# TODO: Detect whether ubsan is required or not, determine the path
add_test_setup(
'epochalypse',
env: [
#'LD_PRELOAD=' + faketime_lib.full_path(),
'LD_PRELOAD=/usr/lib/faketime/libfaketime.so.1',
'FAKETIME=+14y'
]
)

test_cases = [
'strings_rmw',
'utils_rmw',
Expand Down

0 comments on commit 04ddcc2

Please sign in to comment.