1/*
2   Unix SMB/CIFS implementation.
3
4   parmlist testing
5
6   Copyright (C) Jelmer Vernooij 2009
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 3 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program.  If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "includes.h"
23#include "torture/torture.h"
24#include "../lib/util/parmlist.h"
25
26static bool test_get_int(struct torture_context *tctx)
27{
28	struct parmlist *pctx = talloc_zero(tctx, struct parmlist);
29	parmlist_set_string(pctx, "bar", "3");
30	parmlist_set_string(pctx, "notint", "bla");
31	torture_assert_int_equal(tctx, 3, parmlist_get_int(pctx, "bar", 42),
32							 "existing");
33	torture_assert_int_equal(tctx, 42, parmlist_get_int(pctx, "foo", 42),
34							 "default");
35	torture_assert_int_equal(tctx, 0, parmlist_get_int(pctx, "notint", 42),
36							 "Not an integer");
37	return true;
38}
39
40static bool test_get_string(struct torture_context *tctx)
41{
42	struct parmlist *pctx = talloc_zero(tctx, struct parmlist);
43	parmlist_set_string(pctx, "bar", "mystring");
44	torture_assert_str_equal(tctx, "mystring",
45		parmlist_get_string(pctx, "bar", "bla"), "existing");
46	torture_assert_str_equal(tctx, "bla",
47		parmlist_get_string(pctx, "foo", "bla"), "default");
48	return true;
49}
50
51static bool test_get(struct torture_context *tctx)
52{
53	struct parmlist *pctx = talloc_zero(tctx, struct parmlist);
54	struct parmlist_entry *e;
55	parmlist_set_string(pctx, "bar", "mystring");
56
57	e = parmlist_get(pctx, "bar");
58	torture_assert(tctx, e != NULL, "entry");
59	torture_assert_str_equal(tctx, e->key, "bar", "key");
60	torture_assert_str_equal(tctx, e->value, "mystring", "value");
61
62	e = parmlist_get(pctx, "nonexistant");
63	torture_assert(tctx, e == NULL, "nonexistant");
64	return true;
65}
66
67static bool test_get_bool(struct torture_context *tctx)
68{
69	struct parmlist *pctx = talloc_zero(tctx, struct parmlist);
70	parmlist_set_string(pctx, "bar", "true");
71	parmlist_set_string(pctx, "gasoline", "invalid");
72
73	torture_assert(tctx, parmlist_get_bool(pctx, "bar", false), "set");
74	torture_assert(tctx, !parmlist_get_bool(pctx, "foo", false), "default");
75	torture_assert(tctx, !parmlist_get_bool(pctx, "gasoline", false),
76				   "invalid");
77	return true;
78}
79
80static bool test_get_string_list(struct torture_context *tctx)
81{
82	struct parmlist *pctx = talloc_zero(tctx, struct parmlist);
83	const char **ret;
84	parmlist_set_string(pctx, "bar", "true, false");
85
86	ret = parmlist_get_string_list(pctx, "bar", NULL);
87	torture_assert_int_equal(tctx, str_list_length(ret), 2, "length");
88	torture_assert_str_equal(tctx, "true", ret[0], "ret[0]");
89	torture_assert_str_equal(tctx, "false", ret[1], "ret[1]");
90	torture_assert(tctx, NULL == parmlist_get_string_list(pctx, "nonexistant", NULL), "nonexistant");
91
92	return true;
93}
94
95struct torture_suite *torture_local_util_parmlist(TALLOC_CTX *mem_ctx)
96{
97	struct torture_suite *suite = torture_suite_create(mem_ctx, "PARMLIST");
98
99	torture_suite_add_simple_test(suite, "get_int", test_get_int);
100	torture_suite_add_simple_test(suite, "get_string", test_get_string);
101	torture_suite_add_simple_test(suite, "get", test_get);
102	torture_suite_add_simple_test(suite, "get_bool", test_get_bool);
103	torture_suite_add_simple_test(suite, "get_string_list", test_get_string_list);
104
105	return suite;
106}
107