0fabd46cf11d2566d5ead2450b74124da20d81de
[obnox/samba-ctdb.git] / source3 / script / tests / test_net_registry.sh
1 #!/bin/sh
2
3 # Tests for the "net registry" and "net rpc registry" commands.
4 # rpc tests are chose by specifying "rpc" as commandline parameter.
5
6 RPC="$1"
7
8 NET="$VALGRIND ${NET:-$BINDIR/net} $CONFIGURATION"
9
10 if test "x${RPC}" = "xrpc" ; then
11         NETREG="${NET} -U${USERNAME}%${PASSWORD} -I ${SERVER_IP} rpc registry"
12 else
13         NETREG="${NET} registry"
14 fi
15
16 test x"$TEST_FUNCTIONS_SH" != x"INCLUDED" && {
17 incdir=`dirname $0`
18 . $incdir/test_functions.sh
19 }
20
21 failed=0
22
23 test_enumerate()
24 {
25         KEY="$1"
26
27         ${NETREG} enumerate ${KEY}
28 }
29
30 test_getsd()
31 {
32         KEY="$1"
33
34         ${NETREG} getsd ${KEY}
35 }
36
37 test_enumerate_nonexisting()
38 {
39         KEY="$1"
40         ${NETREG} enumerate ${KEY}
41
42         if test "x$?" = "x0" ; then
43                 echo "ERROR: enumerate succeeded with key '${KEY}'"
44                 false
45         else
46                 true
47         fi
48 }
49
50 test_enumerate_no_key()
51 {
52         ${NETREG} enumerate
53         if test "x$?" = "x0" ; then
54                 echo "ERROR: enumerate succeeded without any key spcified"
55                 false
56         else
57                 true
58         fi
59 }
60
61 test_create_existing()
62 {
63         KEY="HKLM"
64         EXPECTED="createkey opened existing ${KEY}"
65
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
73                 fi
74         else
75                 printf "%s\n" "$OUTPUT"
76                 false
77         fi
78 }
79
80 test_createkey()
81 {
82         KEY="$1"
83         BASEKEY=`dirname $KEY`
84         SUBKEY=`basename $KEY`
85
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
93         fi
94
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
103         fi
104
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
112         fi
113
114         # check enumerate of new key works:
115         ${NETREG} enumerate ${KEY}
116 }
117
118 test_deletekey()
119 {
120         KEY="$1"
121         BASEKEY=`dirname ${KEY}`
122         SUBKEY=`basename ${KEY}`
123
124         OUTPUT=`test_createkey "${KEY}"`
125         if test "x$?" != "x0" ; then
126                 printf "%s\n" "${OUTPUT}"
127                 false
128                 return
129         fi
130
131         OUTPUT=`${NETREG} deletekey ${KEY}`
132         if test "x$?" != "x0" ; then
133                 printf "%s\n" "${OUTPUT}"
134                 false
135                 return
136         fi
137
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
144         fi
145
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
153         fi
154
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
162         fi
163 }
164
165 test_deletekey_nonexisting()
166 {
167         KEY="$1"
168
169         OUTPUT=`test_deletekey "${KEY}"`
170         if test "x$?" != "x0" ; then
171                 printf "%s\n" "${OUTPUT}"
172                 false
173                 return
174         fi
175
176         ${NETREG} deletekey "${KEY}"
177         if test "x$?" = "x0" ; then
178                 echo "ERROR: delete after delete succeeded for key '${KEY}'"
179                 false
180         fi
181 }
182
183 test_createkey_with_subkey()
184 {
185         KEY="$1"
186         KEY2=`dirname ${KEY}`
187         SUBKEYNAME2=`basename ${KEY}`
188         BASENAME=`dirname ${KEY2}`
189         SUBKEYNAME1=`basename ${KEY2}`
190
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
197         fi
198
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
206         fi
207
208         # clear:
209         ${NETREG} deletekey ${KEY} && ${NETREG} deletekey ${KEY2}
210 }
211
212 test_deletekey_with_subkey()
213 {
214         KEY="$1"
215         KEY2=`dirname ${KEY}`
216
217         OUTPUT=`${NETREG} createkey ${KEY}`
218         if test "x$?" != "x0" ; then
219                 printf "%s\n" "${OUTPUT}"
220                 false
221                 return
222         fi
223
224         OUTPUT=`${NETREG} deletekey ${KEY2}`
225
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
232         fi
233
234         ${NETREG} deletekey ${KEY} && ${NETREG} deletekey ${KEY2}
235 }
236
237 test_setvalue()
238 {
239         KEY="$1"
240         VALNAME="$2"
241         VALTYPE="$3"
242         VALVALUE="$4"
243
244         OUTPUT=`test_createkey ${KEY}`
245         if test "x$?" != "x0" ; then
246                 printf "%s\n" "${OUTPUT}"
247                 false
248                 return
249         fi
250
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
257         fi
258
259         OUTPUT=`${NETREG} enumerate ${KEY}`
260         if test "x$?" != "x0" ; then
261                 echo "ERROR: failure calling enumerate for key ${KEY}"
262                 echo output:
263                 printf "%s\n" "${OUTPUT}"
264                 false
265                 return
266         fi
267
268         printf "%s\n" "$OUTPUT" | {
269         FOUND=0
270         while read LINE ; do
271                 SEARCH1=`echo $LINE | grep '^Valuename' | grep ${VALNAME}`
272                 if test "x$?" = "x0" ; then
273                         read LINE
274                         read LINE
275                         SEARCH2=`echo $LINE | grep '^Value ' | grep ${VALVALUE}`
276                         if test "x$?" = "x0" ; then
277                                 FOUND=1
278                                 break
279                         fi
280                 fi
281         done
282
283         if test "x$FOUND" != "x1" ; then
284                 echo "ERROR: did not find value '${VALNAME}' with enumerate"
285                 echo "enumerate output:"
286                 printf "%s\n" "$OUTPUT"
287                 false
288                 return
289         fi
290         }
291 }
292
293 test_deletevalue()
294 {
295         KEY="$1"
296         VALNAME="$2"
297
298         ${NETREG} deletevalue ${KEY} ${VALNAME}
299 }
300
301 test_deletevalue_nonexisting()
302 {
303         KEY="$1"
304         VALNAME="$2"
305
306         ${NETREG} deletevalue ${KEY} ${VALNAME}
307         if test "x$?" = "x0" ; then
308                 echo "ERROR: succeeded deleting value ${VALNAME}"
309                 false
310         else
311                 true
312         fi
313 }
314
315 test_setvalue_twice()
316 {
317         KEY="$1"
318         VALNAME="$2"
319         VALTYPE1="$3"
320         VALVALUE1="$4"
321         VALTYPE2="$5"
322         VALVALUE2="$6"
323
324         OUTPUT=`test_setvalue ${KEY} ${VALNAME} ${VALTYPE1} ${VALVALUE1}`
325         if test "x$?" != "x0" ; then
326                 echo "ERROR: first setvalue call failed"
327                 printf "%s\n" "$OUTPUT"
328                 false
329                 return
330         fi
331
332         ${NETREG} setvalue ${KEY} ${VALNAME} ${VALTYPE2} ${VALVALUE2}
333 }
334
335 give_administrative_rights()
336 {
337         bin/net -s $SERVERCONFFILE sam createbuiltingroup Administrators
338         if test "x$?" != "x0" ; then
339                 echo "ERROR: creating builtin group Administrators"
340                 false
341                 return
342         fi
343
344         bin/net -s $SERVERCONFFILE sam addmem BUILTIN\\Administrators $USERNAME
345         if test "x$?" != "x0" ; then
346                 echo "ERROR: adding user $USERNAME to BUILTIN\\Administrators"
347                 false
348         else
349                 true
350         fi
351 }
352
353 take_administrative_rights()
354 {
355         bin/net -s $SERVERCONFFILE sam delmem BUILTIN\\Administrators $USERNAME
356         if test "x$?" != "x0" ; then
357                 echo "ERROR: removing user $USERNAME from BUILTIN\\Administrators"
358                 false
359         else
360                 true
361         fi
362 }
363
364 SED_INVALID_PARAMS="{
365 s/lock directory/;&/g
366 s/modules dir/;&/g
367 s/logging/;&/g
368 s/status/;&/g
369 s/logdir/;&/g
370 s/read prediction/;&/g
371 s/mkprofile/;&/g
372 s/valid chars/;&/g
373 s/timesync/;&/g
374 s/sambaconf/;&/g
375 s/logtype/;&/g
376 s/servername/;&/g
377 }"
378
379 REGPATH="HKLM\Software\Samba"
380
381 conf_roundtrip_step() {
382     echo $* >>$LOG
383     $@ 2>>$LOG
384 #    echo -n .
385 }
386
387 conf_roundtrip()
388 {
389     local DIR=$(mktemp -d --tmpdir=$PREFIX conf_roundtrip_XXXX)
390     local LOG=$DIR/log
391
392     echo conf_roundtrip $1 > $LOG
393
394     sed -e "$SED_INVALID_PARAMS" $1 >$DIR/conf_in
395
396     conf_roundtrip_step $NET conf drop
397     test -z "$($NET conf list)" 2>>$LOG
398     if [ "$?" = "1" ]; then
399         echo "ERROR: conf drop failed" | tee -a $LOG
400         return 1
401     fi
402
403     conf_roundtrip_step $NET conf import $DIR/conf_in
404     conf_roundtrip_step $NET conf list > $DIR/conf_exp
405
406     grep "\[global\]" $DIR/conf_exp >/dev/null 2>>$LOG
407     if [ "$?" = "1" ]; then
408         echo "ERROR: conf import => conf export failed" | tee -a $LOG
409         return 1
410     fi
411
412     conf_roundtrip_step $NET -d10 registry export $REGPATH $DIR/conf_exp.reg
413
414     conf_roundtrip_step $NET conf drop
415     test -z "$($NET conf list)" 2>>$LOG
416     if [ "$?" = "1" ]; then
417         echo "ERROR: conf drop failed" | tee -a $LOG
418         return 1
419     fi
420
421     conf_roundtrip_step $NET registry import $DIR/conf_exp.reg
422
423     conf_roundtrip_step $NET conf list >$DIR/conf_out
424     diff -q $DIR/conf_out $DIR/conf_exp  >> $LOG
425     if [ "$?" = "1" ]; then
426         echo "ERROR: registry import => conf export failed"  | tee -a $LOG
427         return 1
428     fi
429
430     conf_roundtrip_step $NET registry export $REGPATH $DIR/conf_out.reg
431     diff -q $DIR/conf_out.reg $DIR/conf_exp.reg >>$LOG
432     if [ "$?" = "1" ]; then
433         echo "Error: registry import => registry export failed" | tee -a $LOG
434         return 1
435     fi
436     rm -r $DIR
437 }
438
439 CONF_FILES=${CONF_FILES:-$(find $SRCDIR/.. -name *.conf | xargs grep -l "\[global\]")}
440
441 for conf_file in $CONF_FILES
442 do
443     conf_file=${conf_file#$SRCDIR/}
444     testit "conf_roundtrip $conf_file" \
445         conf_roundtrip $conf_file \
446         || failed=`expr $failed + 1`
447 done
448
449
450
451 if test "x${RPC}" = "xrpc" ; then
452 testit "giving user ${USERNAME} administrative rights" \
453         give_administrative_rights
454         if [ "x$?" != "x0" ] ; then
455                 failed=`expr $failed + 1`
456                 testok $0 $failed
457         fi
458 fi
459
460 testit "enumerate HKLM" \
461         test_enumerate HKLM || \
462         failed=`expr $failed + 1`
463
464 testit "enumerate nonexisting hive" \
465         test_enumerate_nonexisting XYZ || \
466         failed=`expr $failed + 1`
467
468 testit "enumerate without key" \
469         test_enumerate_no_key || \
470         failed=`expr $failed + 1`
471
472 # skip getsd test for registry currently: it fails
473 if test "x${RPC}" != "xrpc" ; then
474 testit "getsd HKLM" \
475         test_getsd HKLM || \
476         failed=`expr $failed + 1`
477 fi
478
479 testit "create existing HKLM" \
480         test_create_existing || \
481         failed=`expr $failed + 1`
482
483 testit "create key" \
484         test_createkey HKLM/testkey || \
485         failed=`expr $failed + 1`
486
487 testit "delete key" \
488         test_deletekey HKLM/testkey || \
489         failed=`expr $failed + 1`
490
491 testit "delete^2 key" \
492         test_deletekey_nonexisting HKLM/testkey || \
493         failed=`expr $failed + 1`
494
495 testit "enumerate nonexisting key" \
496         test_enumerate_nonexisting HKLM/testkey || \
497         failed=`expr $failed + 1`
498
499 testit "create key with subkey" \
500         test_createkey_with_subkey HKLM/testkey/subkey || \
501         failed=`expr $failed + 1`
502
503 testit "delete key with subkey" \
504         test_deletekey_with_subkey HKLM/testkey/subkey || \
505         failed=`expr $failed + 1`
506
507 testit "set value" \
508         test_setvalue HKLM/testkey testval sz moin || \
509         failed=`expr $failed + 1`
510
511 testit "delete value" \
512         test_deletevalue HKLM/testkey testval || \
513         failed=`expr $failed + 1`
514
515 testit "delete nonexisting value" \
516         test_deletevalue_nonexisting HKLM/testkey testval || \
517         failed=`expr $failed + 1`
518
519 testit "set value to different type" \
520         test_setvalue_twice HKLM/testkey testval sz moin dword 42 || \
521         failed=`expr $failed + 1`
522
523 testit "delete key with value" \
524         test_deletekey HKLM/testkey || \
525         failed=`expr $failed + 1`
526
527 if test "x${RPC}" = "xrpc" ; then
528 testit "taking administrative rights from user ${USERNAME}" \
529         take_administrative_rights || \
530         failed=`expr $failed + 1`
531 fi
532
533
534 testok $0 $failed
535