Imported Upstream version 3.3.3
[debian/amanda] / packaging / common / test_sh_libs.sh
1 #!/bin/sh
2 # This script is uses ShUnit to test packaging/common/*.  Currently
3 # it only tests the functions as they perform for rpm and deb building.
4 # As support is added for other styles of package, these tests should be
5 # updated.
6
7 #######################
8 # If EXECUTING WITH ROOT ACCESS SCRIPT DELETES AND CREATES USERS.
9 # EXECUTE WITH CAUTION.
10 #######################
11
12 #set -x
13
14 # Note on conflicting variable names:
15 # SHUNIT_* and SHU_* are shunit2 variables
16 # test_* should only be used by test functions.
17 # All other variables will probably be used by pre/post/common functions.
18 case $1 in
19     DEBUG) DEBUG=True
20         shift
21         ;;
22     *) DEBUG= ;;
23 esac
24 export DEBUG
25
26 ex_from=`dirname $0`
27
28 # shunit2 assumes shunit_inc, but we might run from a separate
29 # builddir. So try to use srcdir, as defined by automake, or
30 # otherwise `pwd`.
31 SHUNIT_INC="${srcdir=`pwd`}/packaging/common"; export SHUNIT_INC
32 TMPDIR=`pwd`/shunit-test; export TMPDIR
33 amanda_user=test_amandabackup; export amanda_user
34 amanda_group=test_disk; export amanda_group
35 sup_group="test_tape"; export sup_group
36 AMANDAHOMEDIR=$TMPDIR/amanda; export AMANDAHOMEDIR
37 AMANDATES=$AMANDAHOMEDIR/amandates; export AMANDATES
38 os=`uname`; export os
39 wanted_shell='/bin/false'; export wanted_shell
40 dist=Fedora; export dist
41 SYSCONFDIR=$TMPDIR/etc; export SYSCONFDIR
42 SBINDIR=$TMPDIR/sbin; export SBINDIR
43 encoder=`{ command -v base64 2>/dev/null; } || { command -v uuencode 2>/dev/null; }`
44 # Don't potentially conflict with a real value...
45 deb_uid=63000; export deb_uid
46
47
48 # Append stuff to this when you make temp files if they are outside TMPDIR
49 test_cleanup_files="$TMPDIR"; export test_cleanup_files
50
51 # This can't be part of one-time setup, because TMPDIR must exist before
52 # shunit2 is sourced.
53 mkdir -p ${TMPDIR} || exit 1
54 {
55     LOGFILE="$TMPDIR/test_sh_libs.log"
56     (umask 077 && touch $LOGFILE)
57 } || {
58     echo "Unable to create log file!"
59     exit 1
60 }
61 export LOGFILE
62
63 oneTimeSetUp() {
64     msg_prefix="oneTimeSetUp: "
65     if [ "$IAmRoot" ]; then
66         # Delete an existing user.
67         echo "$msg_prefix attempting to delete an existing user."
68         userdel -f -r ${amanda_user}
69         # If the user exists, our tests will act wrong, so exit
70         grep "${amanda_user}" ${SYSCONFDIR}/passwd &> /dev/null || { \
71             echo "$msg_prefix ${amanda_user} still exists, or no ${SYSCONFDIR}/passwd.";
72             exit 1; }
73         groupdel ${amanda_group} || exit 1
74         groupdel ${sup_group} || exit 1
75     else
76         echo "Not root, cleanup skipped"
77     fi
78     # Make all directories which might be needed.
79     mkdir -p ${AMANDAHOMEDIR}/example
80     if [ ! "$IAmRoot" ]; then
81         mkdir -p ${SYSCONFDIR}/xinetd.d
82         mkdir -p ${SYSCONFDIR}/init.d
83     fi
84 }
85
86 oneTimeTearDown() {
87     if [ ${__shunit_assertsFailed} -eq 0 ] && [ ! "${DEBUG}" ]; then
88         rm -rf $test_cleanup_files
89     else
90         echo "Check ${test_cleanup_files} for logs and error info."
91     fi
92 }
93
94 test_cleanup_files="$LOGFILE ${test_cleanup_files}"
95
96 # shows syntax errors before the tests are run.
97 . ${SHUNIT_INC}/common_functions.sh
98 . ${SHUNIT_INC}/pre_inst_functions.sh
99 . ${SHUNIT_INC}/post_inst_functions.sh
100 . ${SHUNIT_INC}/post_rm_functions.sh
101
102 # Use this to skip root requiring tests.
103 # id -u is not portable to solaris.
104 tester_id=`id | sed 's/gid=\([0-9]*\).*/\1/'`
105 if [ "$tester_id" = "0" ]; then
106     # Same as SHUNIT_TRUE, but we can't use that yet...
107     IAmRoot=0
108 else
109     IAmRoot=
110 fi
111
112 # CAUTION: using real values if we are root.
113 if [ "$IAmRoot" ]; then
114     amanda_group=disk; export amanda_group
115     SYSCONFDIR=/etc; export SYSCONFDIR
116     SBINDDIR=/sbin; export SBINDDIR
117 fi
118
119 # Source our mock utils.
120 . ${SHUNIT_INC}/mock_utils.sh
121
122 log_tail_no_stamp() {
123     # This strips the date off of a log line so only the message is
124     # compared.
125     LOG_TAIL=`tail -1 ${LOGFILE}|cut -d " " -f 5-`
126 }
127
128 ######################################
129 # Common functions
130
131 # shUnit reorders tests alphabetically.  We need the logger and cleanup tests
132 # to run first.
133 test___logger() {
134     # Write a line to the log, test that it got there.
135     TEST_MSG="test___logger message"
136     LOG_LINE="`date +'%b %d %Y %T'`: ${TEST_MSG}"
137     # It's important for the log messages to be quoted, or funny stuff happens.
138     logger "${TEST_MSG}"
139     assertEquals "logger() return code" 0 $?
140     log_tail_no_stamp
141     assertEquals "logger() did not write <${LOG_LINE}> " \
142         "${TEST_MSG}" "${LOG_TAIL}"
143     # Leave this outside the unit test framework.  if the logger is
144     # broken we must exit.
145     if [ ! `grep -c "${LOG_LINE}" ${LOGFILE}` = "1" ]; then
146         echo "error: logger(): Incorrect content in ${LOGFILE}: " `cat ${LOGFILE}`
147         exit 1
148     fi
149 }
150
151 test__log_output_of() {
152     # Use log_output_of to append to the log
153     TEST_MSG="test__log_output_of message"
154     log_output_of echo "${TEST_MSG}"
155     assertEquals "log_output_of()" 0 $?
156     LOG_LINE="echo: ${TEST_MSG}"
157     log_tail_no_stamp
158     assertEquals "log_output_of() message should be: " \
159         "${LOG_LINE}" "${LOG_TAIL}"
160     COUNT=`grep -c "${TEST_MSG}" ${LOGFILE}`
161     assertEquals "log_output_of(): incorrect content in log" \
162         1 ${COUNT}
163     # Leave this outside the unit test framework.  if the logger is
164     # broken we must exit.
165     if [ ! ${COUNT} = '1' ]; then
166         echo "error: log_output_of(): Incorrect content in ${LOGFILE}: " `cat ${LOGFILE}`
167         exit 1
168     fi
169 }
170
171
172 # NOTE: check_xinetd and check_inetd tests end up simply duplicating the
173 # code of the function itself
174
175 test_check_smf() {
176     logger "test_check_smf"
177     [ "$os" = "SunOS" ] || { startSkipping; echo "test_check_smf: skipped"; }
178     # Test existing service
179     touch ${MOCKDIR}/prexisting_service
180     check_smf
181     assertEquals "check_smf preexisting services" 0 $?
182     rm ${MOCKDIR}/prexisting_service
183     check_smf
184     assertEquals "check_smf no amanda service" 1 $?
185 }
186
187 test_check_superserver_running() {
188     logger "test_check_superserver_running"
189     [ "$IAmRoot" ] && { startSkipping; echo "test_check_superserver_running: skipped"; }
190     # Test the positive cases
191     # running changes the output of mocked ps
192     touch ${MOCKDIR}/running
193     check_superserver_running inetd
194     assertEquals "check_superserver_running inetd" 0 $?
195     assertSame "ps args: -e" "`cat $mock_ps_flags`"
196     if [ `uname` = 'Darwin' ]; then
197         [ "$IAmRoot" ] && startSkipping
198         check_superserver_running launchd
199         assertEquals "check_superserver_running launchd" 0 $?
200         assertSame "ps args: aux" "`cat $mock_ps_flags`"
201         endSkipping
202     else
203         endSkipping
204         check_superserver_running launchd
205         assertEquals 2 $?
206     fi
207     # Test the negative case, skip if root
208     [ "$IAmRoot" ] && startSkipping
209     rm ${MOCKDIR}/running
210     check_superserver_running inetd
211     assertEquals "check_superserver_running inetd returned 0 when inetd was not running" 1 $?
212     assertSame "ps args: -e" "`cat $mock_ps_flags`"
213     check_superserver_running xinetd
214     assertNotEquals "check_superserver_running xinetd incorrectly returned 0" \
215          0 $?
216     assertSame "ps args: -e" "`cat $mock_ps_flags`"
217 }
218
219 test_backup_xinetd() {
220     logger "test_backup_xinetd"
221     touch ${SYSCONFDIR}/xinetd.d/amandaserver
222     backup_xinetd "amandaserver"
223     assertEquals "backup_xinetd returns 0" 0 $?
224     assertTrue "[ -f ${AMANDAHOMEDIR}/example/*.amandaserver.orig ]"
225 }
226
227 test_backup_inetd() {
228     logger "test_backup_inetd"
229     case $os in
230         SunOS) inetd_dir=${SYSCONFDIR}/inet ;;
231         *) inetd_dir=${SYSCONFDIR} ;;
232     esac
233     [ -d "${inetd_dir}" ] || mkdir -p ${inetd_dir}
234     touch ${inetd_dir}/inetd.conf
235     echo "amanda foo/bar/baz  amandad" >> ${inetd_dir}/inetd.conf
236     backup_inetd
237     assertEquals "backup_inetd returns 0" 0 $?
238     assertTrue "[ -f ${AMANDAHOMEDIR}/example/inetd.orig ]"
239 }
240
241 test_backup_smf() {
242     logger "test_backup_smf"
243     :
244     # TODO: how to mock this?
245 }
246
247 test_install_xinetd() {
248     logger "test_install_xinetd"
249     if [ "$os" = "SunOS" ] ; then
250         # Solaris has install_xinetd_sol
251         startSkipping
252         echo "test_install_xinetd: skipped"
253         return
254     fi
255     # Test success:
256     touch ${MOCKDIR}/success
257     touch ${AMANDAHOMEDIR}/example/xinetd.amandaserver
258     install_xinetd "amandaserver"
259     assertEquals "install_xinetd returns 0" 0 $?
260     # Test "install" failure
261     rm ${MOCKDIR}/success
262     install_xinetd "amandaserver"
263     assertEquals "install_xinetd returns 1" 1 $?
264 }
265
266 test_install_inetd() {
267     logger "test_install_inetd"
268     case $os in
269         SunOS) inetd_dir=${BASEDIR}/${SYSCONFDIR}/inet ;;
270         *) inetd_dir=${SYSCONFDIR} ;;
271     esac
272     [  -f ${inetd_dir}/inetd.conf ] || touch ${inetd_dir}/inetd.conf
273     test_inetd_entry='amanda foo/bar/baz  amandad'
274     if [ ! -f ${AMANDAHOMEDIR}/example/inetd.conf ]; then
275         echo "${test_inetd_entry}" > ${AMANDAHOMEDIR}/example/inetd.conf.amandaserver
276     fi
277     install_inetd amandaserver
278     assertEquals "install_inetd returns 0" 0 $?
279     assertSame "${test_inetd_entry}" "`tail -1 ${inetd_dir}/inetd.conf`"
280 }
281
282 # TODO: test_install_smf() {
283 # Needs mocks for: inetconv, inetadm, svcadm.
284
285 test_reload_xinetd() {
286     logger "test_reload_xinetd"
287     # Might need init script.
288     if [ "$IAmRoot" ]; then
289         startSkipping
290         echo "test_reload_xinetd: skipped"
291         return
292     elif [ ! -f "${SYSCONFDIR}/init.d/xinetd" ]; then
293         mv ${MOCKDIR}/xinetd ${SYSCONFDIR}/init.d
294     fi
295     # Test bad argument
296     reload_xinetd foo
297     assertEquals "reload_xinetd should reject argument 'foo'" 1 $?
298     # Test success
299     touch ${MOCKDIR}/success
300     reload_xinetd "reload"
301     assertEquals "reload_xinetd" 0 $?
302     # Test no argument
303     reload_xinetd
304     assertEquals "reload_xinetd should attempt to reload" 0 $?
305     flags=`cat ${mock_xinetd_flags}`
306     assertEquals "xinetd_flags should contain: " \
307         "xinetd args: reload" "${flags}"
308     # Test failure
309     rm ${MOCKDIR}/success
310     reload_xinetd "reload"
311     assertEquals "reload_xinetd" 1 $?
312     tail -4 ${LOGFILE}|grep "\<xinetd.*Attempting restart" >/dev/null
313     assertEquals "reload_xinetd should try to restart." 0 $?
314     reload_xinetd "restart"
315     assertEquals "restart should fail." 1 $?
316     tail -3 ${LOGFILE}|grep "restarting xinetd" >/dev/null
317     assertEquals "Should log attempt to restart" 0 $?
318 }
319
320 test_reload_inetd() {
321     logger "test_reload_inetd"
322     # Test bad argument
323     # Might need init script.
324     if [ ! "$IAmRoot" ]; then
325         if [ ! -f "${SYSCONFDIR}/init.d/inetd" ]; then
326             mv ${MOCKDIR}/inetd ${SYSCONFDIR}/init.d
327         fi
328     fi
329     # Test bad argument
330     reload_inetd foo
331     assertEquals "reload_inetd should reject argument 'foo' (return 1):" 1 $?
332     # Test success
333     touch ${MOCKDIR}/success
334     reload_inetd
335     assertEquals "reload_inetd" 0 $?
336     # Test failure
337     rm ${MOCKDIR}/success
338     reload_inetd "reload"
339     assertEquals "reload_inetd" 1 $?
340     tail -4 ${LOGFILE}|grep "\<inetd.*Attempting restart" >/dev/null
341     assertEquals "reload_inetd should try to restart." 0 $?
342     reload_inetd "restart"
343     assertEquals "restart should fail." 1 $?
344     tail -3 ${LOGFILE}|grep "Restarting inetd" >/dev/null
345     assertEquals "Should log attempt to restart" 0 $?
346 }
347
348
349 ######################################
350 # pre_install_functions
351
352 test_check_user_group_missing() {
353     logger "test_check_user_group_missing no param"
354     check_user_group
355     assertNotEquals "'check_user_group' should fail" 0 $?
356     logger "test_check_user_group_missing missing group"
357     [ ! "$IAmRoot" ] && rm -f ${SYSCONFDIR}/group
358     touch ${SYSCONFDIR}/group
359     for os in linux osx solaris; do
360         echo $os > ${MOCKDIR}/id_os
361         check_user_group "abracadabra"
362         assertNotEquals "'check_user group abracadabra' should not be found:" 0 $?
363         log_tail_no_stamp
364         assertEquals "check_user_group should write" \
365             "User's primary group 'abracadabra' does not exist" \
366             "${LOG_TAIL}"
367     done
368 }
369
370 good_group_entry="${amanda_group}:x:100:"
371 export good_group_entry
372 test_check_user_group_exists() {
373     logger "test_check_user_group user and group exist"
374     touch ${MOCKDIR}/id_exists
375     touch ${SYSCONFDIR}/group
376     # Non-root adds and entry to the mock group file
377     [ ! "$IAmRoot" ] && echo $good_group_entry > ${SYSCONFDIR}/group
378     for os in linux osx solaris; do
379         echo $os > ${MOCKDIR}/id_os
380
381         # Case 1: Amanda_user is correct.
382         echo ${amanda_group} > ${MOCKDIR}/id_group
383         check_user_group "${amanda_group}"
384         assertEquals "'check_user_group ${amanda_group}': id returns correct groupname" \
385             0 $?
386
387         # Case 2: Amanda_user is not a member of the the correct primary group.
388         rm ${MOCKDIR}/id_group
389         check_user_group "${amanda_group}"
390         assertEquals "'check_user_group ${amanda_group}' when not a member" 1 $?
391     done
392 }
393
394 test_check_user_supplemental_group_missing() {
395     logger "test_check_user_supplemental_group missing"
396     [ ! "$IAmRoot" ] && echo $good_group_entry > ${SYSCONFDIR}/group
397     for os in linux osx solaris; do
398         echo $os > ${MOCKDIR}/id_os
399         check_user_supplemental_group ${sup_group}
400         assertEquals "'check_user supplemental-group ${sup_group}' when group missing" \
401         1 $?
402     done
403 }
404
405 missing_group_member="${sup_group}:x:105:nobody"
406 export missing_group_member
407 good_sup_group_entry="${missing_group_member},${amanda_user}"
408 export good_sup_group_entry
409
410 test_check_user_supplemental_group_exists() {
411     logger "test_check_user_supplemental_group exists"
412     [ ! "$IAmRoot" ] && echo $missing_group_member > ${SYSCONFDIR}/group
413     check_user_supplemental_group ${sup_group}
414     assertEquals "'check_user_supplemental_group ${sup_group}' when amanda_user is not a member" \
415         1 $?
416
417     [ ! "$IAmRoot" ] && echo ${good_sup_group_entry} > ${SYSCONFDIR}/group
418     check_user_supplemental_group ${sup_group}
419     assertEquals "'check_user_supplemental_group ${sup_group}' with correct membership" \
420         0 $?
421 }
422 test_check_user_shell() {
423     logger "test_check_user_shell"
424     if [ ! "$IAmRoot" ]; then
425         echo "${good_passwd_entry}" > ${SYSCONFDIR}/passwd
426     fi
427     # Case 1: Provide a matching shell
428     check_user_shell "/bin/bash"
429     assertEquals "check_user_shell /bin/bash (matching)" 0 $?
430     # Case 2: Provide a non-matching shell. 
431     check_user_shell "/bin/ksh"
432     assertEquals "check_user_shell /bin/ksh (not matching)" 1 $?
433 }
434
435 test_check_user_homedir() {
436     logger 'test_check_user_homedir'
437     if [ ! "$IAmRoot" ]; then
438         echo "${good_passwd_entry}" > ${SYSCONFDIR}/passwd
439     fi
440     # Case 1: Assume amanda_user is correct.
441     check_user_homedir "${AMANDAHOMEDIR}"
442     assertEquals "check_user_homedir ${AMANDAHOMEDIR}" 0 $?
443     # Case 2: Provide an incorrect homedir
444     check_user_homedir "/tmp"
445     assertEquals "check_user_homedir /tmp" 1 $?
446 }
447
448 test_check_user_uid() {
449     echo "${amanda_group}" > ${MOCKDIR}/id_group
450     touch ${MOCKDIR}/id_exists
451     logger 'test_check_user_uid'
452     for os in linux osx solaris; do
453         echo $os > ${MOCKDIR}/id_os
454         check_user_uid
455         assertEquals "check_user_uid without a uid" 1 $?
456         logger 'test_check_user_uid wrong id'
457         check_user_uid 123
458         assertEquals "check_user_uid uids don't match" 1 $?
459         logger 'test_check_user_uid correct id'
460         check_user_uid ${deb_uid}
461     done
462
463 }
464 test_check_homedir_dir_missing() {
465     logger "test_check_homedir_dir_missing"
466     # First make sure the dir is missing
467     rm -rf ${AMANDAHOMEDIR}
468     check_homedir
469     assertNotEquals "check_homedir returned 0, but homedir did not exist" 0 $?
470 }
471
472 # passwd file entry for Linux systems, maybe others.  UID is correct
473 # for Debian as well.
474 good_passwd_entry="${amanda_user}:x:${mock_deb_uid}:6::${AMANDAHOMEDIR}:/bin/bash"
475 export good_passwd_entry
476 test_create_user() {
477     logger "test_create_user"
478     if [ ! "$IAmRoot" ]; then
479         startSkipping
480         echo "test_create_user: Creating mock passwd file."
481         echo "$good_passwd_entry" > ${SYSCONFDIR}/passwd
482         echo "test_create_user: tests skipped."
483         #TODO: mock useradd.
484         return
485     fi
486     # Case 1: create_user should succeed.
487     create_user
488     assertEquals "create_user()" 0 $?
489 }
490
491 test_add_profiles() {
492     # Solaris only, but testing using mock usermod will run if not root
493     # on any system.
494     logger "test_add_profiles"
495     [ "$IAmRoot" ] && startSkipping
496     add_profiles "Profile foo,Profile bar"
497     assertEquals "add_profiles should succeed" 0 $?
498     flags=`cat ${mock_usermod_flags}`
499     assertEquals "usermod_flags should contain:" \
500         "usermod args: -P \"Profile foo,Profile bar\" ${amanda_user}" \
501         "${flags}"
502
503 }
504
505 test_add_group_check_parameters_logs() {
506     rm -f ${MOCKDIR}/groupadd_rc ${MOCKDIR}/num_groups
507     # Return codes are integers.
508     printf '%i' 0 > ${MOCKDIR}/groupadd_rc
509     # Test that first parameter is required.
510     add_group
511     assertEquals "add_group without a group should fail." 1 $?
512     log_tail_no_stamp
513     assertEquals "add_group should write" \
514         "Error: first argument was not a group to add." \
515         "${LOG_TAIL}"
516 }
517
518 test_add_group_group_ok() {
519     # groupadd created group
520     printf '%i' 0 > ${MOCKDIR}/groupadd_rc
521     echo '${amanda_user} : prev_grp1' > ${MOCKDIR}/groups_output
522     add_group twinkle
523     assertEquals "add_group group ok" 0 $?
524     flags=`cat ${mock_usermod_flags}`
525     assertEquals "usermod_flags" \
526         "usermod args: -G prev_grp1,twinkle ${amanda_user}" \
527         "${flags}"
528
529     # Make sure supplemental groups are preserved when adding groups to an
530     # existing account
531     echo '${amanda_user} : prev_grp1 prev_grp2' > ${MOCKDIR}/groups_output
532     printf '%i' 1 > ${MOCKDIR}/num_groups
533     add_group twinkle
534     assertEquals "add_group group ok" 0 $?
535     flags=`cat ${mock_usermod_flags}`
536     assertEquals "usermod_flags should contain:" \
537         "usermod args: -G prev_grp1,prev_grp2,twinkle ${amanda_user}" \
538         "${flags}"
539 }
540
541 test_create_homedir() {
542     logger "test_create_homedir"
543     rm -rf ${AMANDAHOMEDIR}
544     create_homedir
545     assertEquals "create_homedir returns 0" 0 $?
546     assertTrue "${AMANDAHOMEDIR} did not get created" "[ -d ${AMANDAHOMEDIR} ]"
547     if [ "$IAmRoot" ]; then
548         # Check real owner
549         real_owner=`ls -ld $AMANDAHOMEDIR | awk '{ print $3":"$4; }'`
550         assertSame "${amanda_user}:${amanda_group}" "$real_owner"
551     else
552         assertSame \
553             "chown args: -R ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}" \
554             "`cat $mock_chown_flags`"
555     fi
556     # A second run should succeed too.
557     create_homedir    
558     assertEquals "When homedir exists, create_homedir returns 0:" 0 $?
559     # Later tests use ${AMANDAHOMEDIR}, so leave the dir there.
560 }
561
562 test_check_homedir_dir_existing() {
563     logger "test_check_homedir_dir_existing"
564     # Create the dir (example needed for other tests), then check again.
565     mkdir -p ${AMANDAHOMEDIR}/example
566     check_homedir
567     assertEquals "Homedir exists; check_homedir" 0 $?
568 }
569
570 test_create_logdir() {
571     logger "test_create_logdir"
572     # The logdir variable for the shell libs, not shunit2.
573     LOGDIR=$TMPDIR/amanda_log; export LOGDIR
574     rm -rf ${LOGDIR}
575     rm -rf ${LOGDIR}.save
576     create_logdir
577     assertEquals "create_logdir clean system" 0 $?
578     if [ -n "$IAmRoot" ]; then
579         real_owner=`ls -ld $LOGDIR | awk '{ print $3":"$4; }'`
580         assertSame "${amanda_user}:${amanda_group}" "$real_owner"
581     else
582         assertSame \
583             "chown args: -R ${amanda_user}:${amanda_group} ${LOGDIR}"\
584             "`cat $mock_chown_flags`"
585     fi
586     assertTrue "${LOGDIR} exists" "[ -d ${LOGDIR} ]"
587     # What happens if logdir is a file?
588     rm -rf ${LOGDIR}
589     touch ${LOGDIR}
590     create_logdir
591     assertEquals "create_logdir" 0 $?
592     assertTrue "${LOGDIR} exists" "[ -d ${LOGDIR} ]"
593     assertTrue "${LOGDIR}/amanda_log.save backup exists" \
594         "[ -f ${LOGDIR}/amanda_log.save ]"
595 }
596
597 test_create_amandates() {
598     logger "test_create_amandates"
599     rm -f ${AMANDATES}
600     create_amandates
601     assertEquals "create_amandates" 0 $?
602     assertTrue "[ -f ${AMANDATES} ]"
603 }
604
605 test_check_amandates() {
606     logger "test_check_amandates"
607     touch $mock_chown_flags
608     touch $mock_chmod_flags
609     check_amandates
610     assertEquals "check_amandates" 0 $?
611     [ "$IAmRoot" ] && { startSkipping; echo "test_check_amandates: skipped"; }
612     assertSame \
613         "chown args: ${amanda_user}:${amanda_group} ${AMANDATES}" \
614         "`cat $mock_chown_flags`"
615     assertSame \
616         "chmod args: 0640 ${AMANDATES}" \
617         "`cat $mock_chmod_flags`"
618 }
619
620 test_a_create_gnupg() {
621     # We need to impose some order on a few tests because some functions
622     # rely on others.  Tests are sorted alphabetically.  Insert a letter after
623     # "test_" to impose order on particular tests
624     logger "test_create_gnupg"
625     create_gnupg
626     assertEquals "create_gnupg" 0 $?
627     assertTrue "[ -d ${AMANDAHOMEDIR}/.gnupg ]"
628     # Dir exists
629     create_gnupg
630     assertEquals "create_gnupg dir existing" 0 $?
631 }
632
633 test_a_get_random_lines() {
634     logger "test_get_random_lines"
635     get_random_lines > ${TMPDIR}/lines
636     assertEquals "get_random_lines" 1 $?
637     get_random_lines 1 > ${TMPDIR}/lines
638     assertEquals "get_random_lines 1" 0 $?
639     assertEquals "get_random_lines 1 output" 1 "`sed -n '$=' ${TMPDIR}/lines`"
640     get_random_lines 20 > ${TMPDIR}/lines
641     assertEquals "get_random_lines 20 output" 20 "`sed -n '$=' ${TMPDIR}/lines`"
642 }
643
644 test_b_create_ampassphrase() {
645     rm -f ${AMANDAHOMEDIR}/.am_passphrase
646     logger "test_create_ampassphrase"
647     create_ampassphrase
648     assertEquals "create_ampassphrase" 0 $?
649     assertSame \
650         "chown args: ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.am_passphrase" \
651         "`cat $mock_chown_flags`"
652     assertSame \
653         "chmod args: 0600 ${AMANDAHOMEDIR}/.am_passphrase" \
654         "`cat $mock_chmod_flags`"
655     # When .am_passphrase exists.
656     create_ampassphrase
657     log_tail_no_stamp
658     assertSame \
659         "Info: ${AMANDAHOMEDIR}/.am_passphrase already exists." \
660         "${LOG_TAIL}"
661     rm ${AMANDAHOMEDIR}/.am_passphrase
662 }
663
664 test_b_create_amkey() {
665     logger "test_create_amkey"
666     # Missing .am_passphrase
667     [ -f ${AMANDAHOMEDIR}/.am_passphrase ] && rm ${AMANDAHOMEDIR}/.am_passphrase
668     create_amkey
669     assertEquals "create_amkey" 1 $?
670     log_tail_no_stamp
671     assertSame \
672         "Error: ${AMANDAHOMEDIR}/.am_passphrase is missing, can't create amcrypt key." \
673         "${LOG_TAIL}"
674     # Need .am_passphrase. Ignore these test errors if get_random_lines or
675     # create_gnupg tests failed.
676     get_random_lines 1 > ${AMANDAHOMEDIR}/.am_passphrase
677     create_amkey
678     assertEquals "create_amkey" 0 $?
679     # Test with existing key
680     create_amkey
681     assertEquals "create_amkey" 0 $?
682     log_tail_no_stamp
683     assertSame \
684         "Info: Encryption key '${AMANDAHOMEDIR}/.gnupg/am_key.gpg' already exists." \
685         "${LOG_TAIL}"
686     # make sure unencrypted am_key is not hanging around
687     assertFalse "[ -f ${AMANDAHOMEDIR}/.gnupg/am_key ]"
688
689 }
690
691 test_check_gnupg() {
692     logger "test_check_gnupg"
693     check_gnupg
694     assertEquals "check_gnupg" 0 $?
695     assertSame \
696         "chown args: -R ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.gnupg" \
697         "`cat $mock_chown_flags`"
698     assertSame \
699         "chmod args: -R u=rwX,go= ${AMANDAHOMEDIR}/.gnupg" \
700         "`cat $mock_chmod_flags`"
701 }
702
703 test_create_amandahosts() {
704     logger "test_create_amandahosts"
705     create_amandahosts
706     assertEquals "create_amandahosts:" 0 $?
707     assertEquals "${AMANDAHOMEDIR}/.amandahosts exists:" 0 $?
708     assertEquals "create_amandahosts" 0 $?
709     assertTrue "[ -f ${AMANDAHOMEDIR}/.amandahosts ]"
710 }
711
712 test_check_amandahosts_entry() {
713     logger "test_check_amandahosts_entry"
714     if [ -f ${AMANDAHOMEDIR}/.amandahosts ]; then
715         check_amandahosts_entry root amindexd amidxtaped
716         assertEquals "check_amandahosts_entry root amindexd amidxtaped" \
717             0 $?
718     else
719         echo "test_check_amandahosts_entry: ${AMANDAHOMEDIR}/.amandahosts missing.  test skipped"
720         startSkipping
721     fi
722 }
723
724 test_check_amandahosts_perm() {
725     logger "test_check_amandahosts_perm"
726     check_amandahosts_perms
727     assertEquals "check_amandahosts_perms" 0 $?
728     [ "$IAmRoot" ] && { startSkipping; echo "test_check_amandahosts_perm: skipped"; }
729     assertSame \
730         "chown args: ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.amandahosts" \
731         "`cat $mock_chown_flags`"
732     assertSame \
733         "chmod args: 0600 ${AMANDAHOMEDIR}/.amandahosts" \
734         "`cat $mock_chmod_flags`"
735 }
736
737 test_create_ssh_key() {
738     logger "test_create_ssh_key"
739     keydir=${AMANDAHOMEDIR}/.ssh
740     rm -rf ${keydir}
741     create_ssh_key server
742     assertEquals "create_ssh_key" 0 $?
743     assertTrue "[ -f ${keydir}/id_rsa_amdump ]"
744     [ "$IAmRoot" ] && { startSkipping; echo "test_create_ssh_key: skipped"; }
745     assertSame \
746         "chown args: ${amanda_user}:${amanda_group} ${keydir} ${keydir}/id_rsa_amdump ${keydir}/id_rsa_amdump.pub" \
747         "`cat $mock_chown_flags`"
748     # Chmod is called twice, but we only get the 2nd invocation.
749     assertSame \
750         "chmod args: 0600 ${keydir}/id_rsa_amdump ${keydir}/id_rsa_amdump.pub" \
751         "`cat $mock_chmod_flags`"
752     endSkipping
753
754     rm -rf ${keydir}
755     # What happens if .ssh is a file?
756     touch ${AMANDAHOMEDIR}/.ssh
757     create_ssh_key client
758     assertEquals "create_ssh_key" 0 $?
759     assertTrue "[ -f ${keydir}.save ]"
760     assertTrue "[ -f ${keydir}/id_rsa_amrecover ]"
761 }
762
763 test_create_profile() {
764     logger "test_create_profile"
765     rm -f ${AMANDAHOMEDIR}/.profile
766     create_profile
767     assertEquals "create_profile" 0 $?
768     assertTrue "[ -f ${AMANDAHOMEDIR}/.profile ]"
769 }
770
771 test_check_profile() {
772     logger "test_check_profile"
773     [ -f "${AMANDAHOMEDIR}/.profile" ] || touch ${AMANDAHOMEDIR}/.profile
774     check_profile
775     assertEquals "check_profile" 0 $?
776     assertTrue "[ -s ${AMANDAHOMEDIR}/.profile ]"
777     [ "$IAmRoot" ] && { startSkipping; echo "test_check_profile: skipped"; }
778     assertSame \
779         "chown args: ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.profile" \
780         "`cat $mock_chown_flags`"
781     assertSame \
782         "chmod args: 0640 ${AMANDAHOMEDIR}/.profile" \
783         "`cat $mock_chmod_flags`"
784 }
785
786 test_install_client_conf() {
787     logger "test_install_client_conf"
788     # Test success
789     touch ${MOCKDIR}/success
790     install_client_conf
791     assertEquals "install_client_conf" 0 $?
792     prefix="install args:"
793     inst_files="${AMANDAHOMEDIR}/example/amanda-client.conf ${SYSCONFDIR}/amanda/"
794     case $os in
795       SunOS) assertSame \
796         "`cat $mock_install_flags`" \
797         "${prefix} -m 0600 -u ${amanda_user} -g ${amanda_group} ${inst_files}"
798       ;;
799       *) assertSame \
800         "`cat $mock_install_flags`" \
801         "${prefix} -m 0600 -o ${amanda_user} -g ${amanda_group} ${inst_files}"
802     esac
803 }
804
805 #TODO: create_amtmp
806
807 ######################################
808 #TODO: post_rm_functions
809
810 # Run a single test, or let shunit run all tests
811 if [ $# -gt 0 ]; then
812     echo $1
813     SPECIFIC_TESTS="$*"
814     suite() {
815         suite_addTest test___logger
816         suite_addTest test__log_output_of
817         for t in $SPECIFIC_TESTS; do
818             suite_addTest $t
819         done
820     }
821 fi
822
823 # Importing shunit2 triggers test enumeration, so must happen after
824 # all tests are defined.
825 . ${SHUNIT_INC}/shunit2
826
827 echo "shunit2 log is: ${LOGFILE}"
828 echo "mockdir is: ${MOCKDIR}"