diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/util/parmlist.c | 26 | ||||
-rw-r--r-- | lib/util/parmlist.h | 3 | ||||
-rw-r--r-- | lib/util/tests/parmlist.c | 66 |
3 files changed, 92 insertions, 3 deletions
diff --git a/lib/util/parmlist.c b/lib/util/parmlist.c index ffcd19a4ab..6658fa7e33 100644 --- a/lib/util/parmlist.c +++ b/lib/util/parmlist.c @@ -78,3 +78,29 @@ const char **parmlist_get_string_list(struct parmlist *ctx, const char *name, return (const char **)str_list_make(ctx, p->value, separator); } + +static struct parmlist_entry *parmlist_get_add(struct parmlist *ctx, const char *name) +{ + struct parmlist_entry *e = parmlist_get(ctx, name); + + if (e != NULL) + return e; + + e = talloc(ctx, struct parmlist_entry); + if (e == NULL) + return NULL; + e->key = talloc_strdup(e, name); + DLIST_ADD(ctx->entries, e); + return e; +} + +int parmlist_set_string(struct parmlist *ctx, const char *name, + const char *value) +{ + struct parmlist_entry *e = parmlist_get_add(ctx, name); + if (e == NULL) + return -1; + + e->value = talloc_strdup(e, value); + return 0; +} diff --git a/lib/util/parmlist.h b/lib/util/parmlist.h index 47d2f89d63..b320afee47 100644 --- a/lib/util/parmlist.h +++ b/lib/util/parmlist.h @@ -50,4 +50,7 @@ const char **parmlist_get_string_list(struct parmlist *ctx, const char *name, /** Retrieve boolean from a parameter list. If not set, return default_v. */ bool parmlist_get_bool(struct parmlist *ctx, const char *name, bool default_v); +/** Set a parameter. */ +int parmlist_set_string(struct parmlist *ctx, const char *name, const char *value); + #endif /* _PARMLIST_H */ diff --git a/lib/util/tests/parmlist.c b/lib/util/tests/parmlist.c index 6323fdc599..4b1d875715 100644 --- a/lib/util/tests/parmlist.c +++ b/lib/util/tests/parmlist.c @@ -28,19 +28,79 @@ static bool test_get_int(struct torture_context *tctx) struct parmlist *pctx = talloc_zero(tctx, struct parmlist); parmlist_set_string(pctx, "bar", "3"); parmlist_set_string(pctx, "notint", "bla"); - torture_assert_int_equal(3, parmlist_get_int(pctx, "bar", 42)); - torture_assert_int_equal(42, parmlist_get_int(pctx, "foo", 42), + torture_assert_int_equal(tctx, 3, parmlist_get_int(pctx, "bar", 42), + "existing"); + torture_assert_int_equal(tctx, 42, parmlist_get_int(pctx, "foo", 42), "default"); - torture_assert_int_equal(0, parmlist_get_int(pctx, "notint", 42) + torture_assert_int_equal(tctx, 0, parmlist_get_int(pctx, "notint", 42), "Not an integer"); return true; } +static bool test_get_string(struct torture_context *tctx) +{ + struct parmlist *pctx = talloc_zero(tctx, struct parmlist); + parmlist_set_string(pctx, "bar", "mystring"); + torture_assert_str_equal(tctx, "mystring", + parmlist_get_string(pctx, "bar", "bla"), "existing"); + torture_assert_str_equal(tctx, "bla", + parmlist_get_string(pctx, "foo", "bla"), "default"); + return true; +} + +static bool test_get(struct torture_context *tctx) +{ + struct parmlist *pctx = talloc_zero(tctx, struct parmlist); + struct parmlist_entry *e; + parmlist_set_string(pctx, "bar", "mystring"); + + e = parmlist_get(pctx, "bar"); + torture_assert(tctx, e != NULL, "entry"); + torture_assert_str_equal(tctx, e->key, "bar", "key"); + torture_assert_str_equal(tctx, e->value, "mystring", "value"); + + e = parmlist_get(pctx, "nonexistant"); + torture_assert(tctx, e == NULL, "nonexistant"); + return true; +} + +static bool test_get_bool(struct torture_context *tctx) +{ + struct parmlist *pctx = talloc_zero(tctx, struct parmlist); + parmlist_set_string(pctx, "bar", "true"); + parmlist_set_string(pctx, "gasoline", "invalid"); + + torture_assert(tctx, parmlist_get_bool(pctx, "bar", false), "set"); + torture_assert(tctx, !parmlist_get_bool(pctx, "foo", false), "default"); + torture_assert(tctx, !parmlist_get_bool(pctx, "gasoline", false), + "invalid"); + return true; +} + +static bool test_get_string_list(struct torture_context *tctx) +{ + struct parmlist *pctx = talloc_zero(tctx, struct parmlist); + const char **ret; + parmlist_set_string(pctx, "bar", "true, false"); + + ret = parmlist_get_string_list(pctx, "bar", NULL); + torture_assert_int_equal(tctx, str_list_length(ret), 2, "length"); + torture_assert_str_equal(tctx, "true", ret[0], "ret[0]"); + torture_assert_str_equal(tctx, "false", ret[1], "ret[1]"); + torture_assert(tctx, NULL == parmlist_get_string_list(pctx, "nonexistant", NULL), "nonexistant"); + + return true; +} + struct torture_suite *torture_local_util_parmlist(TALLOC_CTX *mem_ctx) { struct torture_suite *suite = torture_suite_create(mem_ctx, "PARMLIST"); torture_suite_add_simple_test(suite, "get_int", test_get_int); + torture_suite_add_simple_test(suite, "get_string", test_get_string); + torture_suite_add_simple_test(suite, "get", test_get); + torture_suite_add_simple_test(suite, "get_bool", test_get_bool); + torture_suite_add_simple_test(suite, "get_string_list", test_get_string_list); return suite; } |