87edc06beb5c4811dffc8d7cc77b83de2b607657
[obnox/samba/samba-obnox.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 #include "torture/local/proto.h"
25
26 static bool test_create(struct torture_context *tctx)
27 {
28         struct loadparm_context *lp_ctx = loadparm_init(tctx);
29         torture_assert(tctx, lp_ctx != NULL, "lp_ctx");
30         return true;
31 }
32
33 static bool test_set_option(struct torture_context *tctx)
34 {
35         struct loadparm_context *lp_ctx = loadparm_init(tctx);
36         torture_assert(tctx, lpcfg_set_option(lp_ctx, "workgroup=werkgroep"), "lpcfg_set_option failed");
37         torture_assert_str_equal(tctx, "WERKGROEP", lpcfg_workgroup(lp_ctx), "workgroup");
38         return true;
39 }
40
41 static bool test_set_cmdline(struct torture_context *tctx)
42 {
43         struct loadparm_context *lp_ctx = loadparm_init(tctx);
44         torture_assert(tctx, lpcfg_set_cmdline(lp_ctx, "workgroup", "werkgroep"), "lpcfg_set_cmdline failed");
45         torture_assert(tctx, lpcfg_do_global_parameter(lp_ctx, "workgroup", "barbla"), "lpcfg_set_option failed");
46         torture_assert_str_equal(tctx, "WERKGROEP", lpcfg_workgroup(lp_ctx), "workgroup");
47         return true;
48 }
49
50 static bool test_do_global_parameter(struct torture_context *tctx)
51 {
52         struct loadparm_context *lp_ctx = loadparm_init(tctx);
53         torture_assert(tctx, lpcfg_do_global_parameter(lp_ctx, "workgroup", "werkgroep42"),
54                        "lpcfg_set_cmdline failed");
55         torture_assert_str_equal(tctx, lpcfg_workgroup(lp_ctx), "WERKGROEP42", "workgroup");
56         return true;
57 }
58
59
60 static bool test_do_global_parameter_var(struct torture_context *tctx)
61 {
62         struct loadparm_context *lp_ctx = loadparm_init(tctx);
63         torture_assert(tctx, lpcfg_do_global_parameter_var(lp_ctx, "workgroup", "werk%s%d", "groep", 42),
64                        "lpcfg_set_cmdline failed");
65         torture_assert_str_equal(tctx, lpcfg_workgroup(lp_ctx), "WERKGROEP42", "workgroup");
66         return true;
67 }
68
69
70 static bool test_set_option_invalid(struct torture_context *tctx)
71 {
72         struct loadparm_context *lp_ctx = loadparm_init(tctx);
73         torture_assert(tctx, !lpcfg_set_option(lp_ctx, "workgroup"), "lpcfg_set_option succeeded");
74         return true;
75 }
76
77 static bool test_set_option_parametric(struct torture_context *tctx)
78 {
79         struct loadparm_context *lp_ctx = loadparm_init(tctx);
80         torture_assert(tctx, lpcfg_set_option(lp_ctx, "some:thing=blaat"), "lpcfg_set_option failed");
81         torture_assert_str_equal(tctx, lpcfg_parm_string(lp_ctx, NULL, "some", "thing"), "blaat",
82                                  "invalid parametric option");
83         return true;
84 }
85
86 static bool test_lp_parm_double(struct torture_context *tctx)
87 {
88         struct loadparm_context *lp_ctx = loadparm_init(tctx);
89         torture_assert(tctx, lpcfg_set_option(lp_ctx, "some:thing=3.4"), "lpcfg_set_option failed");
90         torture_assert(tctx, lpcfg_parm_double(lp_ctx, NULL, "some", "thing", 2.0) == 3.4,
91                                  "invalid parametric option");
92         torture_assert(tctx, lpcfg_parm_double(lp_ctx, NULL, "some", "bla", 2.0) == 2.0,
93                                  "invalid parametric option");
94         return true;
95 }
96
97 static bool test_lp_parm_bool(struct torture_context *tctx)
98 {
99         struct loadparm_context *lp_ctx = loadparm_init(tctx);
100         torture_assert(tctx, lpcfg_set_option(lp_ctx, "some:thing=true"), "lpcfg_set_option failed");
101         torture_assert(tctx, lpcfg_parm_bool(lp_ctx, NULL, "some", "thing", false) == true,
102                                  "invalid parametric option");
103         torture_assert(tctx, lpcfg_parm_bool(lp_ctx, NULL, "some", "bla", true) == true,
104                                  "invalid parametric option");
105         return true;
106 }
107
108 static bool test_lp_parm_int(struct torture_context *tctx)
109 {
110         struct loadparm_context *lp_ctx = loadparm_init(tctx);
111         torture_assert(tctx, lpcfg_set_option(lp_ctx, "some:thing=34"), "lpcfg_set_option failed");
112         torture_assert_int_equal(tctx, lpcfg_parm_int(lp_ctx, NULL, "some", "thing", 20), 34,
113                                  "invalid parametric option");
114         torture_assert_int_equal(tctx, lpcfg_parm_int(lp_ctx, NULL, "some", "bla", 42), 42,
115                                  "invalid parametric option");
116         return true;
117 }
118
119 static bool test_lp_parm_bytes(struct torture_context *tctx)
120 {
121         struct loadparm_context *lp_ctx = loadparm_init(tctx);
122         torture_assert(tctx, lpcfg_set_option(lp_ctx, "some:thing=16K"), "lpcfg_set_option failed");
123         torture_assert_int_equal(tctx, lpcfg_parm_bytes(lp_ctx, NULL, "some", "thing", 20), 16 * 1024,
124                                  "invalid parametric option");
125         torture_assert_int_equal(tctx, lpcfg_parm_bytes(lp_ctx, NULL, "some", "bla", 42), 42,
126                                  "invalid parametric option");
127         return true;
128 }
129
130 static bool test_lp_do_service_parameter(struct torture_context *tctx)
131 {
132         struct loadparm_context *lp_ctx = loadparm_init(tctx);
133         struct loadparm_service *service = lpcfg_add_service(lp_ctx, lpcfg_default_service(lp_ctx), "foo");
134         torture_assert(tctx, lpcfg_do_service_parameter(lp_ctx, service,
135                                                      "some:thing", "foo"), "lpcfg_set_option failed");
136         torture_assert_str_equal(tctx, lpcfg_parm_string(lp_ctx, service, "some", "thing"), "foo",
137                                  "invalid parametric option");
138         return true;
139 }
140
141 static bool test_lp_service(struct torture_context *tctx)
142 {
143         struct loadparm_context *lp_ctx = loadparm_init(tctx);
144         struct loadparm_service *service = lpcfg_add_service(lp_ctx, lpcfg_default_service(lp_ctx), "foo");
145         torture_assert(tctx, service == lpcfg_service(lp_ctx, "foo"), "invalid service");
146         return true;
147 }
148
149 static bool test_server_role_default(struct torture_context *tctx)
150 {
151         struct loadparm_context *lp_ctx = loadparm_init(tctx);
152         torture_assert_int_equal(tctx, lpcfg_server_role(lp_ctx), ROLE_STANDALONE, "ROLE should be standalone by default");
153         torture_assert_int_equal(tctx, lpcfg_security(lp_ctx), SEC_USER, "security should be user");
154         return true;
155 }
156
157 static bool test_server_role_dc_specified(struct torture_context *tctx)
158 {
159         struct loadparm_context *lp_ctx = loadparm_init(tctx);
160         torture_assert(tctx, lpcfg_set_option(lp_ctx, "server role=domain controller"), "lpcfg_set_option failed");
161         torture_assert_int_equal(tctx, lpcfg_server_role(lp_ctx), ROLE_ACTIVE_DIRECTORY_DC, "ROLE should be DC");
162         torture_assert_int_equal(tctx, lpcfg_security(lp_ctx), SEC_USER, "security should be USER");
163         return true;
164 }
165
166 static bool test_server_role_member_specified(struct torture_context *tctx)
167 {
168         struct loadparm_context *lp_ctx = loadparm_init(tctx);
169         torture_assert(tctx, lpcfg_set_option(lp_ctx, "server role=member"), "lpcfg_set_option failed");
170         torture_assert_int_equal(tctx, lpcfg_server_role(lp_ctx), ROLE_DOMAIN_MEMBER, "ROLE should be member");
171         torture_assert_int_equal(tctx, lpcfg_security(lp_ctx), SEC_ADS, "security should be ADS");
172         return true;
173 }
174
175 static bool test_server_role_member_specified2(struct torture_context *tctx)
176 {
177         struct loadparm_context *lp_ctx = loadparm_init(tctx);
178         torture_assert(tctx, lpcfg_set_option(lp_ctx, "server role=member"), "lpcfg_set_option failed");
179         torture_assert(tctx, lpcfg_set_option(lp_ctx, "security=domain"), "lpcfg_set_option failed");
180         torture_assert_int_equal(tctx, lpcfg_server_role(lp_ctx), ROLE_DOMAIN_MEMBER, "ROLE should be member");
181         torture_assert_int_equal(tctx, lpcfg_security(lp_ctx), SEC_DOMAIN, "security should be domain");
182         return true;
183 }
184
185 static bool test_server_role_member_specified3(struct torture_context *tctx)
186 {
187         struct loadparm_context *lp_ctx = loadparm_init(tctx);
188         torture_assert(tctx, lpcfg_set_option(lp_ctx, "server role=member"), "lpcfg_set_option failed");
189         torture_assert(tctx, lpcfg_set_option(lp_ctx, "security=ads"), "lpcfg_set_option failed");
190         torture_assert_int_equal(tctx, lpcfg_server_role(lp_ctx), ROLE_DOMAIN_MEMBER, "ROLE should be member");
191         torture_assert_int_equal(tctx, lpcfg_security(lp_ctx), SEC_ADS, "security should be ads");
192         return true;
193 }
194
195 static bool test_server_role_standalone_specified(struct torture_context *tctx)
196 {
197         struct loadparm_context *lp_ctx = loadparm_init(tctx);
198         torture_assert(tctx, lpcfg_set_option(lp_ctx, "server role=standalone"), "lpcfg_set_option failed");
199         torture_assert_int_equal(tctx, lpcfg_server_role(lp_ctx), ROLE_STANDALONE, "ROLE should be standalone");
200         torture_assert_int_equal(tctx, lpcfg_security(lp_ctx), SEC_USER, "security should be USER");
201         return true;
202 }
203
204 static bool test_server_role_dc_domain_logons(struct torture_context *tctx)
205 {
206         struct loadparm_context *lp_ctx = loadparm_init(tctx);
207         torture_assert(tctx, lpcfg_set_option(lp_ctx, "domain logons=true"), "lpcfg_set_option failed");
208         torture_assert_int_equal(tctx, lpcfg_server_role(lp_ctx), ROLE_DOMAIN_PDC, "ROLE should be PDC");
209         torture_assert_int_equal(tctx, lpcfg_security(lp_ctx), SEC_USER, "security should be user");
210         return true;
211 }
212
213 static bool test_server_role_dc_domain_logons_and_not_master(struct torture_context *tctx)
214 {
215         struct loadparm_context *lp_ctx = loadparm_init(tctx);
216         torture_assert(tctx, lpcfg_set_option(lp_ctx, "domain logons=true"), "lpcfg_set_option failed");
217         torture_assert(tctx, lpcfg_set_option(lp_ctx, "domain master=false"), "lpcfg_set_option failed");
218         torture_assert_int_equal(tctx, lpcfg_server_role(lp_ctx), ROLE_DOMAIN_BDC, "ROLE should be BDC");
219         torture_assert_int_equal(tctx, lpcfg_security(lp_ctx), SEC_USER, "security should be user");
220         return true;
221 }
222
223 static bool test_server_role_security_ads(struct torture_context *tctx)
224 {
225         struct loadparm_context *lp_ctx = loadparm_init(tctx);
226         torture_assert(tctx, lpcfg_set_option(lp_ctx, "security=ads"), "lpcfg_set_option failed");
227         torture_assert_int_equal(tctx, lpcfg_server_role(lp_ctx), ROLE_DOMAIN_MEMBER, "ROLE should be MEMBER");
228         torture_assert_int_equal(tctx, lpcfg_security(lp_ctx), SEC_ADS, "security should be ads");
229         return true;
230 }
231
232 static bool test_server_role_security_domain(struct torture_context *tctx)
233 {
234         struct loadparm_context *lp_ctx = loadparm_init(tctx);
235         torture_assert(tctx, lpcfg_set_option(lp_ctx, "security=domain"), "lpcfg_set_option failed");
236         torture_assert_int_equal(tctx, lpcfg_server_role(lp_ctx), ROLE_DOMAIN_MEMBER, "ROLE should be MEMBER");
237         torture_assert_int_equal(tctx, lpcfg_security(lp_ctx), SEC_DOMAIN, "security should be domain");
238         return true;
239 }
240
241 struct torture_suite *torture_local_loadparm(TALLOC_CTX *mem_ctx)
242 {
243         struct torture_suite *suite = torture_suite_create(mem_ctx, "loadparm");
244
245         torture_suite_add_simple_test(suite, "create", test_create);
246         torture_suite_add_simple_test(suite, "set_option", test_set_option);
247         torture_suite_add_simple_test(suite, "set_cmdline", test_set_cmdline);
248         torture_suite_add_simple_test(suite, "set_option_invalid", test_set_option_invalid);
249         torture_suite_add_simple_test(suite, "set_option_parametric", test_set_option_parametric);
250         torture_suite_add_simple_test(suite, "set_lp_parm_double", test_lp_parm_double);
251         torture_suite_add_simple_test(suite, "set_lp_parm_bool", test_lp_parm_bool);
252         torture_suite_add_simple_test(suite, "set_lp_parm_int", test_lp_parm_int);
253         torture_suite_add_simple_test(suite, "set_lp_parm_bytes", test_lp_parm_bytes);
254         torture_suite_add_simple_test(suite, "service_parameter", test_lp_do_service_parameter);
255         torture_suite_add_simple_test(suite, "lpcfg_service", test_lp_service);
256         torture_suite_add_simple_test(suite, "do_global_parameter_var", test_do_global_parameter_var);
257         torture_suite_add_simple_test(suite, "do_global_parameter", test_do_global_parameter);
258         torture_suite_add_simple_test(suite, "test_server_role_default", test_server_role_default);
259         torture_suite_add_simple_test(suite, "test_server_role_dc_specified", test_server_role_dc_specified);
260         torture_suite_add_simple_test(suite, "test_server_role_member_specified", test_server_role_member_specified);
261         torture_suite_add_simple_test(suite, "test_server_role_member_specified2", test_server_role_member_specified2);
262         torture_suite_add_simple_test(suite, "test_server_role_member_specified3", test_server_role_member_specified3);
263         torture_suite_add_simple_test(suite, "test_server_role_standalone_specified", test_server_role_standalone_specified);
264         torture_suite_add_simple_test(suite, "test_server_role_dc_domain_logons", test_server_role_dc_domain_logons);
265         torture_suite_add_simple_test(suite, "test_server_role_dc_domain_logons_and_not_master", test_server_role_dc_domain_logons_and_not_master);
266         torture_suite_add_simple_test(suite, "test_server_role_security_ads", test_server_role_security_ads);
267         torture_suite_add_simple_test(suite, "test_server_role_security_domain", test_server_role_security_domain);
268
269         return suite;
270 }