[tor-commits] [tor/master] Add unit tests for the unitparse.c module.
dgoulet at torproject.org
dgoulet at torproject.org
Tue Jul 23 13:50:59 UTC 2019
commit 705bda859ed8bbdd753d587b9814de4af3afee80
Author: Nick Mathewson <nickm at torproject.org>
Date: Wed Jun 19 08:15:13 2019 -0400
Add unit tests for the unitparse.c module.
---
src/lib/confmgt/unitparse.c | 6 ++++-
src/test/test_confparse.c | 62 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 67 insertions(+), 1 deletion(-)
diff --git a/src/lib/confmgt/unitparse.c b/src/lib/confmgt/unitparse.c
index 906195259..c3ed8285a 100644
--- a/src/lib/confmgt/unitparse.c
+++ b/src/lib/confmgt/unitparse.c
@@ -128,10 +128,14 @@ config_parse_units(const char *val, const unit_table_t *u, int *ok)
use_float = 1;
}
- if (!cp) {
+ if (BUG(!cp)) {
+ // cp should always be non-NULL if the parse operation succeeds.
+
+ // LCOV_EXCL_START
*ok = 1;
v = use_float ? ((uint64_t)d) : v;
goto done;
+ // LCOV_EXCL_STOP
}
cp = (char*) eat_whitespace(cp);
diff --git a/src/test/test_confparse.c b/src/test/test_confparse.c
index fef361ef6..dde61b1c8 100644
--- a/src/test/test_confparse.c
+++ b/src/test/test_confparse.c
@@ -18,6 +18,8 @@
#include "test/test.h"
#include "test/log_test_helpers.h"
+#include "lib/confmgt/unitparse.h"
+
typedef struct test_struct_t {
uint32_t magic;
char *s;
@@ -805,6 +807,65 @@ test_confparse_extra_lines(void *arg)
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) \
{ #name, test_confparse_ ## name, flags, NULL, NULL }
@@ -838,5 +899,6 @@ struct testcase_t confparse_tests[] = {
CONFPARSE_TEST(reassign_extend, 0),
CONFPARSE_TEST(get_assigned, 0),
CONFPARSE_TEST(extra_lines, 0),
+ CONFPARSE_TEST(unitparse, 0),
END_OF_TESTCASES
};
More information about the tor-commits
mailing list