1d689c0a9a18357e55b29a2a2654096ff29822c9
[garming/samba-autobuild/.git] / source4 / param / tests / loadparm.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "param/share.h"
22 #include "param/param.h"
23 #include "torture/torture.h"
24
25 static bool test_create(struct torture_context *tctx)
26 {
27         struct loadparm_context *lp_ctx = loadparm_init(tctx);
28         torture_assert(tctx, lp_ctx != NULL, "lp_ctx");
29         return true;
30 }
31
32 static bool test_set_option(struct torture_context *tctx)
33 {
34         struct loadparm_context *lp_ctx = loadparm_init(tctx);
35         torture_assert(tctx, lp_set_option(lp_ctx, "workgroup=werkgroep"), "lp_set_option failed");
36         torture_assert_str_equal(tctx, "WERKGROEP", lp_workgroup(lp_ctx), "workgroup");
37         return true;
38 }
39
40 static bool test_set_option_invalid(struct torture_context *tctx)
41 {
42         struct loadparm_context *lp_ctx = loadparm_init(tctx);
43         torture_assert(tctx, !lp_set_option(lp_ctx, "workgroup"), "lp_set_option succeeded");
44         return true;
45 }
46
47 static bool test_set_option_parametric(struct torture_context *tctx)
48 {
49         struct loadparm_context *lp_ctx = loadparm_init(tctx);
50         torture_assert(tctx, lp_set_option(lp_ctx, "some:thing=blaat"), "lp_set_option failed");
51         torture_assert_str_equal(tctx, lp_parm_string(lp_ctx, NULL, "some", "thing"), "blaat", 
52                                  "invalid parametric option");
53         return true;
54 }
55
56 static bool test_lp_parm_double(struct torture_context *tctx)
57 {
58         struct loadparm_context *lp_ctx = loadparm_init(tctx);
59         torture_assert(tctx, lp_set_option(lp_ctx, "some:thing=3.4"), "lp_set_option failed");
60         torture_assert(tctx, lp_parm_double(lp_ctx, NULL, "some", "thing", 2.0) == 3.4, 
61                                  "invalid parametric option");
62         torture_assert(tctx, lp_parm_double(lp_ctx, NULL, "some", "bla", 2.0) == 2.0, 
63                                  "invalid parametric option");
64         return true;
65 }
66
67 static bool test_lp_parm_bool(struct torture_context *tctx)
68 {
69         struct loadparm_context *lp_ctx = loadparm_init(tctx);
70         torture_assert(tctx, lp_set_option(lp_ctx, "some:thing=true"), "lp_set_option failed");
71         torture_assert(tctx, lp_parm_bool(lp_ctx, NULL, "some", "thing", false) == true, 
72                                  "invalid parametric option");
73         torture_assert(tctx, lp_parm_bool(lp_ctx, NULL, "some", "bla", true) == true, 
74                                  "invalid parametric option");
75         return true;
76 }
77
78 static bool test_lp_parm_int(struct torture_context *tctx)
79 {
80         struct loadparm_context *lp_ctx = loadparm_init(tctx);
81         torture_assert(tctx, lp_set_option(lp_ctx, "some:thing=34"), "lp_set_option failed");
82         torture_assert_int_equal(tctx, lp_parm_int(lp_ctx, NULL, "some", "thing", 20), 34, 
83                                  "invalid parametric option");
84         torture_assert_int_equal(tctx, lp_parm_int(lp_ctx, NULL, "some", "bla", 42), 42, 
85                                  "invalid parametric option");
86         return true;
87 }
88
89 static bool test_lp_parm_bytes(struct torture_context *tctx)
90 {
91         struct loadparm_context *lp_ctx = loadparm_init(tctx);
92         torture_assert(tctx, lp_set_option(lp_ctx, "some:thing=16K"), "lp_set_option failed");
93         torture_assert_int_equal(tctx, lp_parm_bytes(lp_ctx, NULL, "some", "thing", 20), 16 * 1024, 
94                                  "invalid parametric option");
95         torture_assert_int_equal(tctx, lp_parm_bytes(lp_ctx, NULL, "some", "bla", 42), 42, 
96                                  "invalid parametric option");
97         return true;
98 }
99
100 static bool test_lp_do_service_parameter(struct torture_context *tctx)
101 {
102         struct loadparm_context *lp_ctx = loadparm_init(tctx);
103         struct loadparm_service *service = lp_add_service(lp_ctx, &sDefault, "foo");
104         torture_assert(tctx, lp_do_service_parameter(lp_ctx, service, 
105                                                      "some:thing", "foo"), "lp_set_option failed");
106         torture_assert_str_equal(tctx, lp_parm_string(lp_ctx, service, "some", "thing"), "foo",
107                                  "invalid parametric option");
108         return true;
109 }
110
111 struct torture_suite *torture_local_loadparm(TALLOC_CTX *mem_ctx)
112 {
113         struct torture_suite *suite = torture_suite_create(mem_ctx, "LOADPARM");
114
115         torture_suite_add_simple_test(suite, "create", test_create);
116         torture_suite_add_simple_test(suite, "set_option", test_set_option);
117         torture_suite_add_simple_test(suite, "set_option_invalid", test_set_option_invalid);
118         torture_suite_add_simple_test(suite, "set_option_parametric", test_set_option_parametric);
119         torture_suite_add_simple_test(suite, "set_lp_parm_double", test_lp_parm_double);
120         torture_suite_add_simple_test(suite, "set_lp_parm_bool", test_lp_parm_bool);
121         torture_suite_add_simple_test(suite, "set_lp_parm_int", test_lp_parm_int);
122         torture_suite_add_simple_test(suite, "set_lp_parm_bytes", test_lp_parm_bytes);
123         torture_suite_add_simple_test(suite, "service_parameter", test_lp_do_service_parameter);
124
125         return suite;
126 }