From d9ada600cc81603300a0cfce75179c6aa1ac94cc Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 27 Sep 2009 17:37:53 +0200 Subject: parmlist: Add more tests. --- lib/util/parmlist.c | 26 +++++++++++++++++++ lib/util/parmlist.h | 3 +++ lib/util/tests/parmlist.c | 66 ++++++++++++++++++++++++++++++++++++++++++++--- source4/param/generic.c | 10 +++---- source4/param/loadparm.c | 24 ++++++++--------- 5 files changed, 108 insertions(+), 21 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; } diff --git a/source4/param/generic.c b/source4/param/generic.c index 802f90764c..41d01de9d3 100644 --- a/source4/param/generic.c +++ b/source4/param/generic.c @@ -92,16 +92,14 @@ const char *param_get_string(struct param_context *ctx, const char *param, const return parmlist_get_string(section->parameters, param, NULL); } -int param_set_string(struct param_context *ctx, const char *param, const char *value, const char *section) +int param_set_string(struct param_context *ctx, const char *param, const char *value, const char *section_name) { - struct parmlist_entry *p = param_get_add(ctx, param, section); + struct param_section *section = param_get_section(ctx, section_name); - if (p == NULL) + if (section == NULL) return -1; - p->value = talloc_strdup(p, value); - - return 0; + return parmlist_set_string(section->parameters, param, value); } const char **param_get_string_list(struct param_context *ctx, const char *param, const char *separator, const char *section_name) diff --git a/source4/param/loadparm.c b/source4/param/loadparm.c index 21e01b74fd..f7cd95bf4c 100644 --- a/source4/param/loadparm.c +++ b/source4/param/loadparm.c @@ -182,7 +182,7 @@ struct loadparm_global int bDisableNetbios; int bRpcBigEndian; char *szNTPSignDSocketDirectory; - struct param_opt *param_opt; + struct parmlist_entry *param_opt; }; @@ -222,7 +222,7 @@ struct loadparm_service int bMSDfsRoot; int bStrictSync; int bCIFileSystem; - struct param_opt *param_opt; + struct parmlist_entry *param_opt; char dummy[3]; /* for alignment */ }; @@ -749,7 +749,7 @@ const char *lp_get_parametric(struct loadparm_context *lp_ctx, const char *type, const char *option) { char *vfskey; - struct param_opt *data; + struct parmlist_entry *data; if (lp_ctx == NULL) return NULL; @@ -1020,7 +1020,7 @@ struct loadparm_service *lp_add_service(struct loadparm_context *lp_ctx, int i; struct loadparm_service tservice; int num_to_alloc = lp_ctx->iNumServices + 1; - struct param_opt *data, *pdata; + struct parmlist_entry *data, *pdata; tservice = *pservice; @@ -1260,7 +1260,7 @@ static void copy_service(struct loadparm_service *pserviceDest, { int i; bool bcopyall = (pcopymapDest == NULL); - struct param_opt *data, *pdata, *paramo; + struct parmlist_entry *data, *pdata, *paramo; bool not_added; for (i = 0; parm_table[i].label; i++) @@ -1328,7 +1328,7 @@ static void copy_service(struct loadparm_service *pserviceDest, pdata = pdata->next; } if (not_added) { - paramo = talloc(pserviceDest, struct param_opt); + paramo = talloc(pserviceDest, struct parmlist_entry); if (paramo == NULL) smb_panic("OOM"); paramo->key = talloc_reference(paramo, data->key); @@ -1544,7 +1544,7 @@ static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx, const char *pszParmName, const char *pszParmValue, int flags) { - struct param_opt *paramo, *data; + struct parmlist_entry *paramo, *data; char *name; TALLOC_CTX *mem_ctx; @@ -1583,7 +1583,7 @@ static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx, } } - paramo = talloc(mem_ctx, struct param_opt); + paramo = talloc(mem_ctx, struct parmlist_entry); if (!paramo) smb_panic("OOM"); paramo->key = talloc_strdup(paramo, name); @@ -2048,7 +2048,7 @@ static void dump_globals(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults) { int i; - struct param_opt *data; + struct parmlist_entry *data; fprintf(f, "# Global parameters\n[global]\n"); @@ -2078,7 +2078,7 @@ static void dump_globals(struct loadparm_context *lp_ctx, FILE *f, static void dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f) { int i; - struct param_opt *data; + struct parmlist_entry *data; if (pService != sDefault) fprintf(f, "\n[%s]\n", pService->szService); @@ -2217,10 +2217,10 @@ void lp_killunused(struct loadparm_context *lp_ctx, static int lp_destructor(struct loadparm_context *lp_ctx) { - struct param_opt *data; + struct parmlist_entry *data; if (lp_ctx->globals->param_opt != NULL) { - struct param_opt *next; + struct parmlist_entry *next; for (data = lp_ctx->globals->param_opt; data; data=next) { next = data->next; if (data->priority & FLAG_CMDLINE) continue; -- cgit