root/regress/usr.bin/ssh/unittests/conversion/tests.c
/*      $OpenBSD: tests.c,v 1.5 2025/04/15 04:00:42 djm Exp $ */
/*
 * Regress test for conversions
 *
 * Placed in the public domain
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

#include "test_helper.h"

#include "misc.h"

void
tests(void)
{
        char buf[1024];

        TEST_START("conversion_convtime");
        ASSERT_INT_EQ(convtime("0"), 0);
        ASSERT_INT_EQ(convtime("1"), 1);
        ASSERT_INT_EQ(convtime("1S"), 1);
        /* from the examples in the comment above the function */
        ASSERT_INT_EQ(convtime("90m"), 5400);
        ASSERT_INT_EQ(convtime("1h30m"), 5400);
        ASSERT_INT_EQ(convtime("2d"), 172800);
        ASSERT_INT_EQ(convtime("1w"), 604800);

        /* negative time is not allowed */
        ASSERT_INT_EQ(convtime("-7"), -1);
        ASSERT_INT_EQ(convtime("-9d"), -1);
        
        /* overflow */
        snprintf(buf, sizeof buf, "%llu", (unsigned long long)INT_MAX);
        ASSERT_INT_EQ(convtime(buf), INT_MAX);
        snprintf(buf, sizeof buf, "%llu", (unsigned long long)INT_MAX + 1);
        ASSERT_INT_EQ(convtime(buf), -1);

        /* overflow with multiplier */
        snprintf(buf, sizeof buf, "%lluM", (unsigned long long)INT_MAX/60 + 1);
        ASSERT_INT_EQ(convtime(buf), -1);
        ASSERT_INT_EQ(convtime("1000000000000000000000w"), -1);
        TEST_DONE();
}

void
benchmarks(void)
{
        printf("no benchmarks\n");
}