s3-net: add roundtrip tests for registry import/export
[Samba.git] / source3 / script / tests / test_net_registry.sh
blob525e13a81118f0198c3af1c36034b4e46296923a
1 #!/bin/sh
3 # Tests for the "net registry" and "net rpc registry" commands.
4 # rpc tests are chose by specifying "rpc" as commandline parameter.
6 RPC="$1"
8 NET="$VALGRIND ${NET:-$BINDIR/net} $CONFIGURATION"
10 if test "x${RPC}" = "xrpc" ; then
11 NETREG="${NET} -U${USERNAME}%${PASSWORD} -I ${SERVER_IP} rpc registry"
12 else
13 NETREG="${NET} registry"
16 test x"$TEST_FUNCTIONS_SH" != x"INCLUDED" && {
17 incdir=`dirname $0`
18 . $incdir/test_functions.sh
21 failed=0
23 test_enumerate()
25 KEY="$1"
27 ${NETREG} enumerate ${KEY}
30 test_getsd()
32 KEY="$1"
34 ${NETREG} getsd ${KEY}
37 test_enumerate_nonexisting()
39 KEY="$1"
40 ${NETREG} enumerate ${KEY}
42 if test "x$?" = "x0" ; then
43 echo "ERROR: enumerate succeeded with key '${KEY}'"
44 false
45 else
46 true
50 test_enumerate_no_key()
52 ${NETREG} enumerate
53 if test "x$?" = "x0" ; then
54 echo "ERROR: enumerate succeeded without any key spcified"
55 false
56 else
57 true
61 test_create_existing()
63 KEY="HKLM"
64 EXPECTED="createkey opened existing ${KEY}"
66 OUTPUT=`${NETREG} createkey ${KEY}`
67 if test "x$?" = "x0" ; then
68 if test "$OUTPUT" = "$EXPECTED" ; then
69 true
70 else
71 echo "got '$OUTPUT', expected '$EXPECTED'"
72 false
74 else
75 printf "%s\n" "$OUTPUT"
76 false
80 test_createkey()
82 KEY="$1"
83 BASEKEY=`dirname $KEY`
84 SUBKEY=`basename $KEY`
86 OUTPUT=`${NETREG} createkey ${KEY}`
87 if test "x$?" != "x0" ; then
88 echo "ERROR: createkey ${KEY} failed"
89 echo "output:"
90 printf "%s\n" "$OUTPUT"
91 false
92 return
95 # check enumerate of basekey lists new key:
96 OUTPUT=`${NETREG} enumerate ${BASEKEY}`
97 if test "x$?" != "x0" ; then
98 echo "ERROR: failed to enumerate key '${BASEKEY}'"
99 echo "output:"
100 printf "%s\n" "$OUTPUT"
101 false
102 return
105 EXPECTED="Keyname = ${SUBKEY}"
106 printf "%s\n" "$OUTPUT" | grep '^Keyname' | grep ${SUBKEY}
107 if test "x$?" != "x0" ; then
108 echo "ERROR: did not find expexted '$EXPECTED' in output"
109 echo "output:"
110 printf "%s\n" "$OUTPUT"
111 false
114 # check enumerate of new key works:
115 ${NETREG} enumerate ${KEY}
118 test_deletekey()
120 KEY="$1"
121 BASEKEY=`dirname ${KEY}`
122 SUBKEY=`basename ${KEY}`
124 OUTPUT=`test_createkey "${KEY}"`
125 if test "x$?" != "x0" ; then
126 printf "%s\n" "${OUTPUT}"
127 false
128 return
131 OUTPUT=`${NETREG} deletekey ${KEY}`
132 if test "x$?" != "x0" ; then
133 printf "%s\n" "${OUTPUT}"
134 false
135 return
138 # check enumerate of basekey does not show key anymore:
139 OUTPUT=`${NETREG} enumerate ${BASEKEY}`
140 if test "x$?" != "x0" ; then
141 printf "%s\n" "$OUTPUT"
142 false
143 return
146 UNEXPECTED="Keyname = ${SUBKEY}"
147 printf "%s\n" "$OUTPUT" | grep '^Keyname' | grep ${SUBKEY}
148 if test "x$?" = "x0" ; then
149 echo "ERROR: found '$UNEXPECTED' after delete in output"
150 echo "output:"
151 printf "%s\n" "$OUTPUT"
152 false
155 # check enumerate of key itself does not work anymore:
156 ${NETREG} enumerate ${KEY}
157 if test "x$?" = "x0" ; then
158 echo "ERROR: 'enumerate ${KEY}' works after 'deletekey ${KEY}'"
159 false
160 else
161 true
165 test_deletekey_nonexisting()
167 KEY="$1"
169 OUTPUT=`test_deletekey "${KEY}"`
170 if test "x$?" != "x0" ; then
171 printf "%s\n" "${OUTPUT}"
172 false
173 return
176 ${NETREG} deletekey "${KEY}"
177 if test "x$?" = "x0" ; then
178 echo "ERROR: delete after delete succeeded for key '${KEY}'"
179 false
183 test_createkey_with_subkey()
185 KEY="$1"
186 KEY2=`dirname ${KEY}`
187 SUBKEYNAME2=`basename ${KEY}`
188 BASENAME=`dirname ${KEY2}`
189 SUBKEYNAME1=`basename ${KEY2}`
191 OUTPUT=`${NETREG} createkey ${KEY}`
192 if test "x$?" != "x0" ; then
193 echo "ERROR: createkey ${KEY} failed"
194 printf "%s\n" "${OUTPUT}"
195 false
196 return
199 # check we can enumerate to level key
200 OUTPUT=`${NETREG} enumerate ${KEY}`
201 if test "x$?" != "x0" ; then
202 echo "ERROR: failed to enumerate '${KEY}' after creation"
203 printf "%s\n" "${OUTPUT}"
204 false
205 return
208 # clear:
209 ${NETREG} deletekey ${KEY} && ${NETREG} deletekey ${KEY2}
212 test_deletekey_with_subkey()
214 KEY="$1"
215 KEY2=`dirname ${KEY}`
217 OUTPUT=`${NETREG} createkey ${KEY}`
218 if test "x$?" != "x0" ; then
219 printf "%s\n" "${OUTPUT}"
220 false
221 return
224 OUTPUT=`${NETREG} deletekey ${KEY2}`
226 if test "x$?" = "x0" ; then
227 echo "ERROR: delete of key with subkey succeeded"
228 echo "output:"
229 printf "%s\n" "$OUTPUT"
230 false
231 return
234 ${NETREG} deletekey ${KEY} && ${NETREG} deletekey ${KEY2}
237 test_setvalue()
239 KEY="$1"
240 VALNAME="${2#_}"
241 VALTYPE="$3"
242 VALVALUE="$4"
244 OUTPUT=`test_createkey ${KEY}`
245 if test "x$?" != "x0" ; then
246 printf "%s\n" "${OUTPUT}"
247 false
248 return
251 OUTPUT=`${NETREG} setvalue ${KEY} "${VALNAME}" ${VALTYPE} ${VALVALUE}`
252 if test "x$?" != "x0" ; then
253 echo "ERROR: failed to set value testval in key ${KEY}"
254 printf "%s\n" "${OUTPUT}"
255 false
256 return
259 OUTPUT=`${NETREG} getvalueraw ${KEY} "${VALNAME}"`
260 if test "x$?" != "x0" ; then
261 echo "ERROR: failure calling getvalueraw for key ${KEY}"
262 echo output:
263 printf "%s\n" "${OUTPUT}"
264 false
265 return
268 if test "x${OUTPUT}" != "x${VALVALUE}" ; then
269 echo "ERROR: failure retrieving value ${VALNAME} for key ${KEY}"
270 printf "expected: %s\ngot: %s\n" "${VALVALUE}" "${OUTPUT}"
271 false
272 return
277 test_deletevalue()
279 KEY="$1"
280 VALNAME="${2#_}"
282 ${NETREG} deletevalue ${KEY} "${VALNAME}"
285 test_deletevalue_nonexisting()
287 KEY="$1"
288 VALNAME="${2#_}"
290 ${NETREG} deletevalue ${KEY} "${VALNAME}"
291 if test "x$?" = "x0" ; then
292 echo "ERROR: succeeded deleting value ${VALNAME}"
293 false
294 else
295 true
299 test_setvalue_twice()
301 KEY="$1"
302 VALNAME="${2#_}"
303 VALTYPE1="$3"
304 VALVALUE1="$4"
305 VALTYPE2="$5"
306 VALVALUE2="$6"
308 OUTPUT=`test_setvalue ${KEY} _"${VALNAME}" ${VALTYPE1} ${VALVALUE1}`
309 if test "x$?" != "x0" ; then
310 echo "ERROR: first setvalue call failed"
311 printf "%s\n" "$OUTPUT"
312 false
313 return
316 ${NETREG} setvalue ${KEY} "${VALNAME}" ${VALTYPE2} ${VALVALUE2}
319 give_administrative_rights()
321 bin/net -s $SERVERCONFFILE sam createbuiltingroup Administrators
322 if test "x$?" != "x0" ; then
323 echo "ERROR: creating builtin group Administrators"
324 false
325 return
328 bin/net -s $SERVERCONFFILE sam addmem BUILTIN\\Administrators $USERNAME
329 if test "x$?" != "x0" ; then
330 echo "ERROR: adding user $USERNAME to BUILTIN\\Administrators"
331 false
332 else
333 true
337 take_administrative_rights()
339 bin/net -s $SERVERCONFFILE sam delmem BUILTIN\\Administrators $USERNAME
340 if test "x$?" != "x0" ; then
341 echo "ERROR: removing user $USERNAME from BUILTIN\\Administrators"
342 false
343 else
344 true
348 SED_INVALID_PARAMS="{
349 s/lock directory/;&/g
350 s/modules dir/;&/g
351 s/logging/;&/g
352 s/status/;&/g
353 s/logdir/;&/g
354 s/read prediction/;&/g
355 s/mkprofile/;&/g
356 s/valid chars/;&/g
357 s/timesync/;&/g
358 s/sambaconf/;&/g
359 s/logtype/;&/g
360 s/servername/;&/g
363 REGPATH="HKLM\Software\Samba"
365 conf_roundtrip_step() {
366 echo $* >>$LOG
367 $@ 2>>$LOG
368 # echo -n .
371 conf_roundtrip()
373 local DIR=$(mktemp -d --tmpdir=$PREFIX conf_roundtrip_XXXX)
374 local LOG=$DIR/log
376 echo conf_roundtrip $1 > $LOG
378 sed -e "$SED_INVALID_PARAMS" $1 >$DIR/conf_in
380 conf_roundtrip_step $NET conf drop
381 test -z "$($NET conf list)" 2>>$LOG
382 if [ "$?" = "1" ]; then
383 echo "ERROR: conf drop failed" | tee -a $LOG
384 return 1
387 conf_roundtrip_step $NET conf import $DIR/conf_in
388 conf_roundtrip_step $NET conf list > $DIR/conf_exp
390 grep "\[global\]" $DIR/conf_exp >/dev/null 2>>$LOG
391 if [ "$?" = "1" ]; then
392 echo "ERROR: conf import => conf export failed" | tee -a $LOG
393 return 1
396 conf_roundtrip_step $NET -d10 registry export $REGPATH $DIR/conf_exp.reg
398 conf_roundtrip_step $NET conf drop
399 test -z "$($NET conf list)" 2>>$LOG
400 if [ "$?" = "1" ]; then
401 echo "ERROR: conf drop failed" | tee -a $LOG
402 return 1
405 conf_roundtrip_step $NET registry import $DIR/conf_exp.reg
407 conf_roundtrip_step $NET conf list >$DIR/conf_out
408 diff -q $DIR/conf_out $DIR/conf_exp >> $LOG
409 if [ "$?" = "1" ]; then
410 echo "ERROR: registry import => conf export failed" | tee -a $LOG
411 return 1
414 conf_roundtrip_step $NET registry export $REGPATH $DIR/conf_out.reg
415 diff -q $DIR/conf_out.reg $DIR/conf_exp.reg >>$LOG
416 if [ "$?" = "1" ]; then
417 echo "Error: registry import => registry export failed" | tee -a $LOG
418 return 1
420 rm -r $DIR
423 CONF_FILES=${CONF_FILES:-$(find $SRCDIR/.. -name *.conf | xargs grep -l "\[global\]")}
425 for conf_file in $CONF_FILES
427 conf_file=${conf_file#$SRCDIR/}
428 testit "conf_roundtrip $conf_file" \
429 conf_roundtrip $conf_file \
430 || failed=`expr $failed + 1`
431 done
435 if test "x${RPC}" = "xrpc" ; then
436 testit "giving user ${USERNAME} administrative rights" \
437 give_administrative_rights
438 if [ "x$?" != "x0" ] ; then
439 failed=`expr $failed + 1`
440 testok $0 $failed
444 testit "enumerate HKLM" \
445 test_enumerate HKLM || \
446 failed=`expr $failed + 1`
448 testit "enumerate nonexisting hive" \
449 test_enumerate_nonexisting XYZ || \
450 failed=`expr $failed + 1`
452 testit "enumerate without key" \
453 test_enumerate_no_key || \
454 failed=`expr $failed + 1`
456 # skip getsd test for registry currently: it fails
457 if test "x${RPC}" != "xrpc" ; then
458 testit "getsd HKLM" \
459 test_getsd HKLM || \
460 failed=`expr $failed + 1`
463 testit "create existing HKLM" \
464 test_create_existing || \
465 failed=`expr $failed + 1`
467 testit "create key" \
468 test_createkey HKLM/testkey || \
469 failed=`expr $failed + 1`
471 testit "delete key" \
472 test_deletekey HKLM/testkey || \
473 failed=`expr $failed + 1`
475 testit "delete^2 key" \
476 test_deletekey_nonexisting HKLM/testkey || \
477 failed=`expr $failed + 1`
479 testit "enumerate nonexisting key" \
480 test_enumerate_nonexisting HKLM/testkey || \
481 failed=`expr $failed + 1`
483 testit "create key with subkey" \
484 test_createkey_with_subkey HKLM/testkey/subkey || \
485 failed=`expr $failed + 1`
487 testit "delete key with subkey" \
488 test_deletekey_with_subkey HKLM/testkey/subkey || \
489 failed=`expr $failed + 1`
491 testit "set value" \
492 test_setvalue HKLM/testkey _testval sz moin || \
493 failed=`expr $failed + 1`
495 testit "delete value" \
496 test_deletevalue HKLM/testkey _testval || \
497 failed=`expr $failed + 1`
499 testit "delete nonexisting value" \
500 test_deletevalue_nonexisting HKLM/testkey _testval || \
501 failed=`expr $failed + 1`
503 testit "set value to different type" \
504 test_setvalue_twice HKLM/testkey testval sz moin dword 42 || \
505 failed=`expr $failed + 1`
507 testit "set default value" \
508 test_setvalue HKLM/testkey _"" sz 42 || \
509 failed=`expr $failed + 1`
511 testit "delete default value" \
512 test_deletevalue HKLM/testkey _"" || \
513 failed=`expr $failed + 1`
515 testit "delete nonexisting default value" \
516 test_deletevalue_nonexisting HKLM/testkey _"" || \
517 failed=`expr $failed + 1`
519 testit "delete key with value" \
520 test_deletekey HKLM/testkey || \
521 failed=`expr $failed + 1`
523 if test "x${RPC}" = "xrpc" ; then
524 testit "taking administrative rights from user ${USERNAME}" \
525 take_administrative_rights || \
526 failed=`expr $failed + 1`
530 testok $0 $failed