Test some edge cases of valid_temp_time

This commit is contained in:
Ed Kellett 2022-03-18 18:13:24 +00:00
parent 2f596395fa
commit 1fcdacb424
2 changed files with 35 additions and 0 deletions

View file

@ -697,6 +697,8 @@ valid_temp_time(const char *p)
return -1; return -1;
if (endp == p) if (endp == p)
return -1; return -1;
if (current < 0)
return -1;
switch (*endp) { switch (*endp) {
case '\0': /* No unit was given so send it back as minutes */ case '\0': /* No unit was given so send it back as minutes */

View file

@ -42,11 +42,44 @@ static void valid_temp_time1(void)
is_int(52 * WEEK, t, MSG); is_int(52 * WEEK, t, MSG);
} }
static void valid_temp_time_invalid(void)
{
time_t t;
t = valid_temp_time("-2w");
is_int(-1, t, MSG);
t = valid_temp_time("hello");
is_int(-1, t, MSG);
t = valid_temp_time("m");
is_int(-1, t, MSG);
t = valid_temp_time("1w-1w");
is_int(-1, t, MSG);
}
static void valid_temp_time_overflow(void)
{
time_t max_time = (uintmax_t) (~(time_t)0) >> 1;
char s[100];
time_t t;
snprintf(s, sizeof s, "%" PRIuMAX "m", (uintmax_t) max_time / 60 + 2);
t = valid_temp_time(s);
is_int(52 * WEEK, t, MSG);
snprintf(s, sizeof s, "%" PRIuMAX "m%" PRIuMAX "m", (uintmax_t) max_time / 60 - 1, (uintmax_t) max_time / 60 - 1);
t = valid_temp_time(s);
is_int(52 * WEEK, t, MSG);
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
plan_lazy(); plan_lazy();
valid_temp_time1(); valid_temp_time1();
valid_temp_time_invalid();
valid_temp_time_overflow();
return 0; return 0;
} }