aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_confparse.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2019-07-19 06:45:54 -0400
committerNick Mathewson <nickm@torproject.org>2019-07-24 15:21:56 -0400
commite8dc513bd012c9d7e1a5908c72056d5be52e760e (patch)
treee2ce3e075be6786118b6e8200741e14b595bdca0 /src/test/test_confparse.c
parentc32d485942e766eeea70cab468cc7c727a5be270 (diff)
downloadtor-e8dc513bd012c9d7e1a5908c72056d5be52e760e.tar.gz
tor-e8dc513bd012c9d7e1a5908c72056d5be52e760e.zip
Add a config_mgr_t type to wrap config_format_t
Remember that our goal in the present refactoring is to allow each subsystem to declare its own configuration structure and variables. To do this, each module will get its own config_format_t, and so we'll want a different structure that wraps several config_format_t objects. This is a "config_mgr_t".
Diffstat (limited to 'src/test/test_confparse.c')
-rw-r--r--src/test/test_confparse.c183
1 files changed, 107 insertions, 76 deletions
diff --git a/src/test/test_confparse.c b/src/test/test_confparse.c
index ec018f0c52..8a921531fa 100644
--- a/src/test/test_confparse.c
+++ b/src/test/test_confparse.c
@@ -119,7 +119,7 @@ test_validate_cb(void *old_options, void *options, void *default_options,
return 0;
}
-static void test_free_cb(void *options);
+static void test_free_cb(const config_mgr_t *mgr, void *options);
#define TEST_MAGIC 0x1337
@@ -139,12 +139,12 @@ static const config_format_t test_fmt = {
};
static void
-test_free_cb(void *options)
+test_free_cb(const config_mgr_t *mgr, void *options)
{
if (!options)
return;
- config_free(&test_fmt, options);
+ config_free(mgr, options);
}
/* Make sure that config_init sets everything to the right defaults. */
@@ -152,8 +152,9 @@ static void
test_confparse_init(void *arg)
{
(void)arg;
- test_struct_t *tst = config_new(&test_fmt);
- config_init(&test_fmt, tst);
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = config_new(mgr);
+ config_init(mgr, tst);
// Make sure that options are initialized right. */
tt_uint_op(tst->magic, OP_EQ, TEST_MAGIC);
@@ -178,7 +179,8 @@ test_confparse_init(void *arg)
tt_int_op(tst->hidden_int, OP_EQ, 0);
done:
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
+ config_mgr_free(mgr);
}
static const char simple_settings[] =
@@ -207,18 +209,18 @@ static const char simple_settings[] =
/* Return a configuration object set up from simple_settings above. */
static test_struct_t *
-get_simple_config(void)
+get_simple_config(const config_mgr_t *mgr)
{
test_struct_t *result = NULL;
- test_struct_t *tst = config_new(&test_fmt);
+ test_struct_t *tst = config_new(mgr);
config_line_t *lines = NULL;
char *msg = NULL;
- config_init(&test_fmt, tst);
+ config_init(mgr, tst);
int r = config_get_lines(simple_settings, &lines, 0);
tt_int_op(r, OP_EQ, 0);
- r = config_assign(&test_fmt, tst, lines, 0, &msg);
+ r = config_assign(mgr, tst, lines, 0, &msg);
tt_int_op(r, OP_EQ, 0);
tt_ptr_op(msg, OP_EQ, NULL);
@@ -227,7 +229,7 @@ get_simple_config(void)
done:
tor_free(msg);
config_free_lines(lines);
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
return result;
}
@@ -236,7 +238,8 @@ static void
test_confparse_assign_simple(void *arg)
{
(void)arg;
- test_struct_t *tst = get_simple_config();
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = get_simple_config(mgr);
tt_str_op(tst->s, OP_EQ, "this is a");
tt_str_op(tst->fn, OP_EQ, "/simple/test of the");
@@ -284,10 +287,11 @@ test_confparse_assign_simple(void *arg)
tt_str_op(tst->mixed_hidden_lines->next->value, OP_EQ, "ABC");
tt_assert(!tst->mixed_hidden_lines->next->next);
- tt_assert(config_check_ok(&test_fmt, tst, LOG_ERR));
+ tt_assert(config_check_ok(mgr, tst, LOG_ERR));
done:
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
+ config_mgr_free(mgr);
}
/* Try to assign to an obsolete option, and make sure we get a warning. */
@@ -295,26 +299,28 @@ static void
test_confparse_assign_obsolete(void *arg)
{
(void)arg;
- test_struct_t *tst = config_new(&test_fmt);
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = get_simple_config(mgr);
config_line_t *lines = NULL;
char *msg = NULL;
- config_init(&test_fmt, tst);
+ config_init(mgr, tst);
int r = config_get_lines("obsolete option here",
&lines, 0);
tt_int_op(r, OP_EQ, 0);
setup_capture_of_logs(LOG_WARN);
- r = config_assign(&test_fmt, tst, lines, 0, &msg);
+ r = config_assign(mgr, tst, lines, 0, &msg);
tt_int_op(r, OP_EQ, 0);
tt_ptr_op(msg, OP_EQ, NULL);
expect_single_log_msg_containing("Skipping obsolete configuration option");
done:
teardown_capture_of_logs();
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
config_free_lines(lines);
tor_free(msg);
+ config_mgr_free(mgr);
}
/* Try to assign to an deprecated option, and make sure we get a warning
@@ -323,30 +329,32 @@ static void
test_confparse_assign_deprecated(void *arg)
{
(void)arg;
- test_struct_t *tst = config_new(&test_fmt);
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = get_simple_config(mgr);
config_line_t *lines = NULL;
char *msg = NULL;
- config_init(&test_fmt, tst);
+ config_init(mgr, tst);
int r = config_get_lines("deprecated_int 7",
&lines, 0);
tt_int_op(r, OP_EQ, 0);
setup_capture_of_logs(LOG_WARN);
- r = config_assign(&test_fmt, tst, lines, CAL_WARN_DEPRECATIONS, &msg);
+ r = config_assign(mgr, tst, lines, CAL_WARN_DEPRECATIONS, &msg);
tt_int_op(r, OP_EQ, 0);
tt_ptr_op(msg, OP_EQ, NULL);
expect_single_log_msg_containing("This integer is deprecated.");
tt_int_op(tst->deprecated_int, OP_EQ, 7);
- tt_assert(config_check_ok(&test_fmt, tst, LOG_ERR));
+ tt_assert(config_check_ok(mgr, tst, LOG_ERR));
done:
teardown_capture_of_logs();
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
config_free_lines(lines);
tor_free(msg);
+ config_mgr_free(mgr);
}
/* Try to re-assign an option name that has been depreacted in favor of
@@ -355,16 +363,17 @@ static void
test_confparse_assign_replaced(void *arg)
{
(void)arg;
- test_struct_t *tst = config_new(&test_fmt);
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = get_simple_config(mgr);
config_line_t *lines = NULL;
char *msg = NULL;
- config_init(&test_fmt, tst);
+ config_init(mgr, tst);
int r = config_get_lines("float 1000\n", &lines, 0);
tt_int_op(r, OP_EQ, 0);
setup_capture_of_logs(LOG_WARN);
- r = config_assign(&test_fmt, tst, lines, CAL_WARN_DEPRECATIONS, &msg);
+ r = config_assign(mgr, tst, lines, CAL_WARN_DEPRECATIONS, &msg);
tt_int_op(r, OP_EQ, 0);
tt_ptr_op(msg, OP_EQ, NULL);
expect_single_log_msg_containing("use 'dbl' instead.");
@@ -374,9 +383,10 @@ test_confparse_assign_replaced(void *arg)
done:
teardown_capture_of_logs();
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
config_free_lines(lines);
tor_free(msg);
+ config_mgr_free(mgr);
}
/* Try to set a linelist value with no option. */
@@ -384,25 +394,27 @@ static void
test_confparse_assign_emptystring(void *arg)
{
(void)arg;
- test_struct_t *tst = config_new(&test_fmt);
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = get_simple_config(mgr);
config_line_t *lines = NULL;
char *msg = NULL;
- config_init(&test_fmt, tst);
+ config_init(mgr, tst);
int r = config_get_lines("lines\n", &lines, 0);
tt_int_op(r, OP_EQ, 0);
setup_capture_of_logs(LOG_WARN);
- r = config_assign(&test_fmt, tst, lines, 0, &msg);
+ r = config_assign(mgr, tst, lines, 0, &msg);
tt_int_op(r, OP_EQ, 0);
tt_ptr_op(msg, OP_EQ, NULL);
expect_single_log_msg_containing("has no value");
done:
teardown_capture_of_logs();
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
config_free_lines(lines);
tor_free(msg);
+ config_mgr_free(mgr);
}
/* Try to set a the same option twice; make sure we get a warning. */
@@ -410,26 +422,28 @@ static void
test_confparse_assign_twice(void *arg)
{
(void)arg;
- test_struct_t *tst = config_new(&test_fmt);
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = get_simple_config(mgr);
config_line_t *lines = NULL;
char *msg = NULL;
- config_init(&test_fmt, tst);
+ config_init(mgr, tst);
int r = config_get_lines("pos 10\n"
"pos 99\n", &lines, 0);
tt_int_op(r, OP_EQ, 0);
setup_capture_of_logs(LOG_WARN);
- r = config_assign(&test_fmt, tst, lines, 0, &msg);
+ r = config_assign(mgr, tst, lines, 0, &msg);
tt_int_op(r, OP_EQ, 0);
tt_ptr_op(msg, OP_EQ, NULL);
expect_single_log_msg_containing("used more than once");
done:
teardown_capture_of_logs();
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
config_free_lines(lines);
tor_free(msg);
+ config_mgr_free(mgr);
}
typedef struct badval_test_t {
@@ -443,16 +457,17 @@ static void
test_confparse_assign_badval(void *arg)
{
const badval_test_t *bt = arg;
- test_struct_t *tst = config_new(&test_fmt);
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = get_simple_config(mgr);
config_line_t *lines = NULL;
char *msg = NULL;
- config_init(&test_fmt, tst);
+ config_init(mgr, tst);
int r = config_get_lines(bt->cfg, &lines, 0);
tt_int_op(r, OP_EQ, 0);
setup_capture_of_logs(LOG_WARN);
- r = config_assign(&test_fmt, tst, lines, 0, &msg);
+ r = config_assign(mgr, tst, lines, 0, &msg);
tt_int_op(r, OP_LT, 0);
tt_ptr_op(msg, OP_NE, NULL);
if (! strstr(msg, bt->expect_msg)) {
@@ -461,9 +476,10 @@ test_confparse_assign_badval(void *arg)
done:
teardown_capture_of_logs();
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
config_free_lines(lines);
tor_free(msg);
+ config_mgr_free(mgr);
}
/* Various arguments for badval test.
@@ -495,11 +511,12 @@ static void
test_confparse_dump(void *arg)
{
(void)arg;
- test_struct_t *tst = get_simple_config();
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = get_simple_config(mgr);
char *dumped = NULL;
/* Minimal version. */
- dumped = config_dump(&test_fmt, NULL, tst, 1, 0);
+ dumped = config_dump(mgr, NULL, tst, 1, 0);
tt_str_op(dumped, OP_EQ,
"s this is a\n"
"fn /simple/test of the\n"
@@ -524,7 +541,7 @@ test_confparse_dump(void *arg)
/* Maximal */
tor_free(dumped);
- dumped = config_dump(&test_fmt, NULL, tst, 0, 0);
+ dumped = config_dump(mgr, NULL, tst, 0, 0);
tt_str_op(dumped, OP_EQ,
"s this is a\n"
"fn /simple/test of the\n"
@@ -550,7 +567,7 @@ test_confparse_dump(void *arg)
/* commented */
tor_free(dumped);
- dumped = config_dump(&test_fmt, NULL, tst, 0, 1);
+ dumped = config_dump(mgr, NULL, tst, 0, 1);
tt_str_op(dumped, OP_EQ,
"s this is a\n"
"fn /simple/test of the\n"
@@ -575,8 +592,9 @@ test_confparse_dump(void *arg)
"VisibleLineB ABC\n");
done:
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
tor_free(dumped);
+ config_mgr_free(mgr);
}
/* Try confparse_reset_line(), and make sure it behaves correctly */
@@ -584,16 +602,18 @@ static void
test_confparse_reset(void *arg)
{
(void)arg;
- test_struct_t *tst = get_simple_config();
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = get_simple_config(mgr);
- config_reset_line(&test_fmt, tst, "interval", 0);
+ config_reset_line(mgr, tst, "interval", 0);
tt_int_op(tst->interval, OP_EQ, 0);
- config_reset_line(&test_fmt, tst, "interval", 1);
+ config_reset_line(mgr, tst, "interval", 1);
tt_int_op(tst->interval, OP_EQ, 10);
done:
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
+ config_mgr_free(mgr);
}
/* Try setting options a second time on a config object, and make sure
@@ -602,7 +622,8 @@ static void
test_confparse_reassign(void *arg)
{
(void)arg;
- test_struct_t *tst = get_simple_config();
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = get_simple_config(mgr);
config_line_t *lines = NULL;
char *msg = NULL, *rs = NULL;
@@ -613,7 +634,7 @@ test_confparse_reassign(void *arg)
"csv 14,15\n"
"routerset 127.0.0.1\n",
&lines, 0);
- r = config_assign(&test_fmt, tst,lines, 0, &msg);
+ r = config_assign(mgr, tst,lines, 0, &msg);
tt_int_op(r, OP_EQ, 0);
tt_ptr_op(msg, OP_EQ, NULL);
@@ -633,7 +654,7 @@ test_confparse_reassign(void *arg)
tt_str_op(rs, OP_EQ, "127.0.0.1");
// Try again with the CLEAR_FIRST and USE_DEFAULTS flags
- r = config_assign(&test_fmt, tst, lines,
+ r = config_assign(mgr, tst, lines,
CAL_CLEAR_FIRST|CAL_USE_DEFAULTS, &msg);
tt_int_op(r, OP_EQ, 0);
@@ -644,10 +665,11 @@ test_confparse_reassign(void *arg)
tt_int_op(tst->i, OP_EQ, 12);
done:
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
config_free_lines(lines);
tor_free(msg);
tor_free(rs);
+ config_mgr_free(mgr);
}
/* Try setting options a second time on a config object, using the +foo
@@ -656,7 +678,8 @@ static void
test_confparse_reassign_extend(void *arg)
{
(void)arg;
- test_struct_t *tst = get_simple_config();
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = get_simple_config(mgr);
config_line_t *lines = NULL;
char *msg = NULL;
@@ -664,7 +687,7 @@ test_confparse_reassign_extend(void *arg)
"+lines 13\n",
&lines, 1); // allow extended format.
tt_int_op(r, OP_EQ, 0);
- r = config_assign(&test_fmt, tst,lines, 0, &msg);
+ r = config_assign(mgr, tst,lines, 0, &msg);
tt_int_op(r, OP_EQ, 0);
tt_ptr_op(msg, OP_EQ, NULL);
@@ -684,27 +707,28 @@ test_confparse_reassign_extend(void *arg)
"/lines\n",
&lines, 1); // allow extended format.
tt_int_op(r, OP_EQ, 0);
- r = config_assign(&test_fmt, tst, lines, 0, &msg);
+ r = config_assign(mgr, tst, lines, 0, &msg);
tt_int_op(r, OP_EQ, 0);
tt_ptr_op(msg, OP_EQ, NULL);
tt_assert(tst->lines == NULL);
config_free_lines(lines);
- config_free(&test_fmt, tst);
- tst = get_simple_config();
+ config_free(mgr, tst);
+ tst = get_simple_config(mgr);
r = config_get_lines(
"/lines away!\n",
&lines, 1); // allow extended format.
tt_int_op(r, OP_EQ, 0);
- r = config_assign(&test_fmt, tst, lines, 0, &msg);
+ r = config_assign(mgr, tst, lines, 0, &msg);
tt_int_op(r, OP_EQ, 0);
tt_ptr_op(msg, OP_EQ, NULL);
tt_assert(tst->lines == NULL);
done:
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
config_free_lines(lines);
tor_free(msg);
+ config_mgr_free(mgr);
}
/* Test out confparse_get_assigned(). */
@@ -712,30 +736,32 @@ static void
test_confparse_get_assigned(void *arg)
{
(void)arg;
- test_struct_t *tst = get_simple_config();
+
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = get_simple_config(mgr);
config_line_t *lines = NULL;
- lines = config_get_assigned_option(&test_fmt, tst, "I", 1);
+ lines = config_get_assigned_option(mgr, tst, "I", 1);
tt_assert(lines);
tt_str_op(lines->key, OP_EQ, "i");
tt_str_op(lines->value, OP_EQ, "3");
tt_assert(lines->next == NULL);
config_free_lines(lines);
- lines = config_get_assigned_option(&test_fmt, tst, "s", 1);
+ lines = config_get_assigned_option(mgr, tst, "s", 1);
tt_assert(lines);
tt_str_op(lines->key, OP_EQ, "s");
tt_str_op(lines->value, OP_EQ, "this is a");
tt_assert(lines->next == NULL);
config_free_lines(lines);
- lines = config_get_assigned_option(&test_fmt, tst, "obsolete", 1);
+ lines = config_get_assigned_option(mgr, tst, "obsolete", 1);
tt_assert(!lines);
- lines = config_get_assigned_option(&test_fmt, tst, "nonesuch", 1);
+ lines = config_get_assigned_option(mgr, tst, "nonesuch", 1);
tt_assert(!lines);
- lines = config_get_assigned_option(&test_fmt, tst, "mixedlines", 1);
+ lines = config_get_assigned_option(mgr, tst, "mixedlines", 1);
tt_assert(lines);
tt_str_op(lines->key, OP_EQ, "LineTypeA");
tt_str_op(lines->value, OP_EQ, "i d");
@@ -745,7 +771,7 @@ test_confparse_get_assigned(void *arg)
tt_assert(lines->next->next == NULL);
config_free_lines(lines);
- lines = config_get_assigned_option(&test_fmt, tst, "linetypeb", 1);
+ lines = config_get_assigned_option(mgr, tst, "linetypeb", 1);
tt_assert(lines);
tt_str_op(lines->key, OP_EQ, "LineTypeB");
tt_str_op(lines->value, OP_EQ, "i c");
@@ -754,7 +780,7 @@ test_confparse_get_assigned(void *arg)
tor_free(tst->s);
tst->s = tor_strdup("Hello\nWorld");
- lines = config_get_assigned_option(&test_fmt, tst, "s", 1);
+ lines = config_get_assigned_option(mgr, tst, "s", 1);
tt_assert(lines);
tt_str_op(lines->key, OP_EQ, "s");
tt_str_op(lines->value, OP_EQ, "\"Hello\\nWorld\"");
@@ -762,8 +788,9 @@ test_confparse_get_assigned(void *arg)
config_free_lines(lines);
done:
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
config_free_lines(lines);
+ config_mgr_free(mgr);
}
/* Another variant, which accepts and stores unrecognized lines.*/
@@ -796,24 +823,25 @@ static void
test_confparse_extra_lines(void *arg)
{
(void)arg;
- test_struct_t *tst = config_new(&etest_fmt);
+ config_mgr_t *mgr = config_mgr_new(&etest_fmt);
+ test_struct_t *tst = config_new(mgr);
config_line_t *lines = NULL;
char *msg = NULL, *dump = NULL;
- config_init(&etest_fmt, tst);
+ config_init(mgr, tst);
int r = config_get_lines(
"unknotty addita\n"
"pos 99\n"
"wombat knish\n", &lines, 0);
tt_int_op(r, OP_EQ, 0);
- r = config_assign(&etest_fmt, tst, lines, 0, &msg);
+ r = config_assign(mgr, tst, lines, 0, &msg);
tt_int_op(r, OP_EQ, 0);
tt_ptr_op(msg, OP_EQ, NULL);
tt_assert(tst->extra_lines);
- dump = config_dump(&etest_fmt, NULL, tst, 1, 0);
+ dump = config_dump(mgr, NULL, tst, 1, 0);
tt_str_op(dump, OP_EQ,
"pos 99\n"
"unknotty addita\n"
@@ -823,7 +851,8 @@ test_confparse_extra_lines(void *arg)
tor_free(msg);
tor_free(dump);
config_free_lines(lines);
- config_free(&etest_fmt, tst);
+ config_free(mgr, tst);
+ config_mgr_free(mgr);
}
static void
@@ -889,12 +918,14 @@ static void
test_confparse_check_ok_fail(void *arg)
{
(void)arg;
- test_struct_t *tst = config_new(&test_fmt);
+ config_mgr_t *mgr = config_mgr_new(&test_fmt);
+ test_struct_t *tst = config_new(mgr);
tst->pos = -10;
- tt_assert(! config_check_ok(&test_fmt, tst, LOG_INFO));
+ tt_assert(! config_check_ok(mgr, tst, LOG_INFO));
done:
- config_free(&test_fmt, tst);
+ config_free(mgr, tst);
+ config_mgr_free(mgr);
}
#define CONFPARSE_TEST(name, flags) \