Add unit tests for the unitparse.c module.

This commit is contained in:
Nick Mathewson 2019-06-19 08:15:13 -04:00
parent f007437292
commit 705bda859e
2 changed files with 67 additions and 1 deletions

View File

@ -128,10 +128,14 @@ config_parse_units(const char *val, const unit_table_t *u, int *ok)
use_float = 1; use_float = 1;
} }
if (!cp) { if (BUG(!cp)) {
// cp should always be non-NULL if the parse operation succeeds.
// LCOV_EXCL_START
*ok = 1; *ok = 1;
v = use_float ? ((uint64_t)d) : v; v = use_float ? ((uint64_t)d) : v;
goto done; goto done;
// LCOV_EXCL_STOP
} }
cp = (char*) eat_whitespace(cp); cp = (char*) eat_whitespace(cp);

View File

@ -18,6 +18,8 @@
#include "test/test.h" #include "test/test.h"
#include "test/log_test_helpers.h" #include "test/log_test_helpers.h"
#include "lib/confmgt/unitparse.h"
typedef struct test_struct_t { typedef struct test_struct_t {
uint32_t magic; uint32_t magic;
char *s; char *s;
@ -805,6 +807,65 @@ test_confparse_extra_lines(void *arg)
config_free(&etest_fmt, tst); config_free(&etest_fmt, tst);
} }
static void
test_confparse_unitparse(void *args)
{
(void)args;
/* spot-check a few memunit values. */
int ok = 3;
tt_u64_op(config_parse_memunit("100 MB", &ok), OP_EQ, 100<<20);
tt_assert(ok);
tt_u64_op(config_parse_memunit("100 TB", &ok), OP_EQ, UINT64_C(100)<<40);
tt_assert(ok);
// This is a floating-point value, but note that 1.5 can be represented
// precisely.
tt_u64_op(config_parse_memunit("1.5 MB", &ok), OP_EQ, 3<<19);
tt_assert(ok);
/* Try some good intervals and msec intervals */
tt_int_op(config_parse_interval("2 days", &ok), OP_EQ, 48*3600);
tt_assert(ok);
tt_int_op(config_parse_interval("1.5 hour", &ok), OP_EQ, 5400);
tt_assert(ok);
tt_u64_op(config_parse_interval("1 minute", &ok), OP_EQ, 60);
tt_assert(ok);
tt_int_op(config_parse_msec_interval("2 days", &ok), OP_EQ, 48*3600*1000);
tt_assert(ok);
tt_int_op(config_parse_msec_interval("10 msec", &ok), OP_EQ, 10);
tt_assert(ok);
/* Try a couple of unitless values. */
tt_int_op(config_parse_interval("10", &ok), OP_EQ, 10);
tt_assert(ok);
tt_u64_op(config_parse_interval("15.0", &ok), OP_EQ, 15);
tt_assert(ok);
/* u64 overflow */
/* XXXX our implementation does not currently detect this. See bug 30920. */
/*
tt_u64_op(config_parse_memunit("20000000 TB", &ok), OP_EQ, 0);
tt_assert(!ok);
*/
/* i32 overflow */
tt_int_op(config_parse_interval("1000 months", &ok), OP_EQ, -1);
tt_assert(!ok);
tt_int_op(config_parse_msec_interval("4 weeks", &ok), OP_EQ, -1);
tt_assert(!ok);
/* bad units */
tt_u64_op(config_parse_memunit("7 nybbles", &ok), OP_EQ, 0);
tt_assert(!ok);
// XXXX these next two should return -1 according to the documentation.
tt_int_op(config_parse_interval("7 cowznofski", &ok), OP_EQ, 0);
tt_assert(!ok);
tt_int_op(config_parse_msec_interval("1 kalpa", &ok), OP_EQ, 0);
tt_assert(!ok);
done:
;
}
#define CONFPARSE_TEST(name, flags) \ #define CONFPARSE_TEST(name, flags) \
{ #name, test_confparse_ ## name, flags, NULL, NULL } { #name, test_confparse_ ## name, flags, NULL, NULL }
@ -838,5 +899,6 @@ struct testcase_t confparse_tests[] = {
CONFPARSE_TEST(reassign_extend, 0), CONFPARSE_TEST(reassign_extend, 0),
CONFPARSE_TEST(get_assigned, 0), CONFPARSE_TEST(get_assigned, 0),
CONFPARSE_TEST(extra_lines, 0), CONFPARSE_TEST(extra_lines, 0),
CONFPARSE_TEST(unitparse, 0),
END_OF_TESTCASES END_OF_TESTCASES
}; };