3 # Import helper functions
4 . $(atf_get_srcdir)/helper_functions.shin
11 atf_check -s exit:0 ${PW} useradd test
12 atf_check -s exit:0 -o match:"^test:.*" \
13 grep "^test:.*" $HOME/master.passwd
16 # Test add user with option -N
17 atf_test_case user_add_noupdate
18 user_add_noupdate_body() {
21 atf_check -s exit:0 -o match:"^test:.*" ${PW} useradd test -N
22 atf_check -s exit:1 -o empty grep "^test:.*" $HOME/master.passwd
25 # Test add user with comments
26 atf_test_case user_add_comments
27 user_add_comments_body() {
30 atf_check -s exit:0 ${PW} useradd test -c 'Test User,work!,123,user@example.com'
31 atf_check -s exit:0 -o match:'^test:.*:Test User,work!,123,user@example.com:' \
32 grep '^test:.*:Test User,work!,123,user@example.com:' $HOME/master.passwd
35 # Test add user with comments and option -N
36 atf_test_case user_add_comments_noupdate
37 user_add_comments_noupdate_body() {
40 atf_check -s exit:0 -o match:"^test:.*:Test User,work,123,456:" \
41 ${PW} useradd test -c "Test User,work,123,456" -N
42 atf_check -s exit:1 -o empty grep "^test:.*" $HOME/master.passwd
45 # Test add user with invalid comments
46 atf_test_case user_add_comments_invalid
47 user_add_comments_invalid_body() {
50 atf_check -s exit:65 -e match:"invalid character" \
51 ${PW} useradd test -c "Test User,work,123:456,456"
52 atf_check -s exit:1 -o empty \
53 grep "^test:.*:Test User,work,123:456,456:" $HOME/master.passwd
56 # Test add user with invalid comments and option -N
57 atf_test_case user_add_comments_invalid_noupdate
58 user_add_comments_invalid_noupdate_body() {
61 atf_check -s exit:65 -e match:"invalid character" \
62 ${PW} useradd test -c "Test User,work,123:456,456" -N
63 atf_check -s exit:1 -o empty grep "^test:.*" $HOME/master.passwd
66 # Test add user with alternate homedir
67 atf_test_case user_add_homedir
68 user_add_homedir_body() {
71 atf_check -s exit:0 ${PW} useradd test -d /foo/bar
72 atf_check -s exit:0 -o match:"^test:\*:.*::0:0:User &:/foo/bar:.*" \
76 # Test add user with account expiration as an epoch date
77 atf_test_case user_add_account_expiration_epoch
78 user_add_account_expiration_epoch_body() {
81 DATE=`date -j -v+1d "+%s"`
82 atf_check -s exit:0 ${PW} useradd test -e ${DATE}
83 atf_check -s exit:0 -o match:"^test:\*:.*::0:${DATE}:.*" \
87 # Test add user with account expiration as a DD-MM-YYYY date
88 atf_test_case user_add_account_expiration_date_numeric
89 user_add_account_expiration_date_numeric_body() {
92 DATE=`date -j -v+1d "+%d-%m-%Y"`
93 EPOCH=`date -j -f "%d-%m-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
94 atf_check -s exit:0 ${PW} useradd test -e ${DATE}
95 atf_check -s exit:0 -o match:"^test:\*:.*::0:${EPOCH}:User &:.*" \
99 # Test add user with account expiration as a DD-MM-YYYY date
100 atf_test_case user_add_account_expiration_date_month
101 user_add_account_expiration_date_month_body() {
104 DATE=`date -j -v+1d "+%d-%b-%Y"`
105 EPOCH=`date -j -f "%d-%b-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
106 atf_check -s exit:0 ${PW} useradd test -e ${DATE}
107 atf_check -s exit:0 -o match:"^test:\*:.*::0:${EPOCH}:User &:.*" \
111 # Test add user with account expiration as a relative date
112 atf_test_case user_add_account_expiration_date_relative
113 user_add_account_expiration_date_relative_body() {
116 EPOCH=`date -j -v+13m "+%s"`
117 BUF=`expr $EPOCH + 5`
118 atf_check -s exit:0 ${PW} useradd test -e +13o
119 TIME=`${PW} usershow test | awk -F ':' '{print $7}'`
120 [ ! -z $TIME -a $TIME -ge $EPOCH -a $TIME -lt $BUF ] || \
121 atf_fail "Expiration time($TIME) was not within $EPOCH - $BUF seconds."
124 # Test add user with password expiration as an epoch date
125 atf_test_case user_add_password_expiration_epoch
126 user_add_password_expiration_epoch_body() {
129 DATE=`date -j -v+1d "+%s"`
130 atf_check -s exit:0 ${PW} useradd test -p ${DATE}
131 atf_check -s exit:0 -o match:"^test:\*:.*::${DATE}:0:.*" \
135 # Test add user with password expiration as a DD-MM-YYYY date
136 atf_test_case user_add_password_expiration_date_numeric
137 user_add_password_expiration_date_numeric_body() {
140 DATE=`date -j -v+1d "+%d-%m-%Y"`
141 EPOCH=`date -j -f "%d-%m-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
142 atf_check -s exit:0 ${PW} useradd test -p ${DATE}
143 atf_check -s exit:0 -o match:"^test:\*:.*::${EPOCH}:0:User &:.*" \
147 # Test add user with password expiration as a DD-MMM-YYYY date
148 atf_test_case user_add_password_expiration_date_month
149 user_add_password_expiration_date_month_body() {
152 DATE=`date -j -v+1d "+%d-%b-%Y"`
153 EPOCH=`date -j -f "%d-%b-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
154 atf_check -s exit:0 ${PW} useradd test -p ${DATE}
155 atf_check -s exit:0 -o match:"^test:\*:.*::${EPOCH}:0:User &:.*" \
159 # Test add user with password expiration as a relative date
160 atf_test_case user_add_password_expiration_date_relative
161 user_add_password_expiration_date_relative_body() {
164 EPOCH=`date -j -v+13m "+%s"`
165 BUF=`expr $EPOCH + 5`
166 atf_check -s exit:0 ${PW} useradd test -p +13o
167 TIME=`${PW} usershow test | awk -F ':' '{print $6}'`
168 [ ! -z $TIME -a $TIME -ge $EPOCH -a $TIME -lt $BUF ] || \
169 atf_fail "Expiration time($TIME) was not within $EPOCH - $BUF seconds."
172 atf_test_case user_add_name_too_long
173 user_add_name_too_long_body() {
175 atf_check -e match:"too long" -s exit:64 \
176 ${PW} useradd name_very_vert_very_very_very_long
179 atf_test_case user_add_name_with_spaces
180 user_add_name_with_spaces_body() {
182 atf_check -s exit:65 -e match:"invalid character" \
183 ${PW} useradd 'test user'
184 atf_check -s exit:1 -o empty grep "^test user:.*" $HOME/master.passwd
185 # Try again with -n which uses a slightly different code path.
186 atf_check -s exit:65 -e match:"invalid character" \
187 ${PW} useradd -n 'test user'
188 atf_check -s exit:1 -o empty grep "^test user:.*" $HOME/master.passwd
191 atf_test_case user_add_name_with_spaces_and_gid_specified
192 user_add_name_with_spaces_and_gid_specified_body() {
195 user_name="test user"
196 # pw useradd should fail because of the space in the user
197 # name, not because the group doesn't exist.
198 atf_check -s exit:65 -e match:"invalid character" \
199 ${PW} useradd "${user_name}" -g ${gid}
200 atf_check -s exit:1 -o empty grep "^${user_name}:.*" $HOME/master.passwd
201 # Try again with -n which uses a slightly different code path.
202 atf_check -s exit:65 -e match:"invalid character" \
203 ${PW} useradd -n "${user_name}" -g ${gid}
204 atf_check -s exit:1 -o empty grep "^${user_name}:.*" $HOME/master.passwd
205 # Make sure the user isn't added even if the group exists
206 atf_check -s exit:0 ${PW} groupadd blafasel -g ${gid}
207 atf_check -s exit:65 -e match:"invalid character" \
208 ${PW} useradd "${user_name}" -g ${gid}
209 atf_check -s exit:1 -o empty grep "^${user_name}:.*" $HOME/master.passwd
210 # Try again with the -n option.
211 atf_check -s exit:65 -e match:"invalid character" \
212 ${PW} useradd -n "${user_name}" -g ${gid}
213 atf_check -s exit:1 -o empty grep "^${user_name}:.*" $HOME/master.passwd
216 atf_test_case user_add_expiration
217 user_add_expiration_body() {
220 atf_check -s exit:0 \
221 ${PW} useradd foo -e 20-03-2037
222 atf_check -o inline:"foo:*:1001:1001::0:2121120000:User &:/home/foo:/bin/sh\n" \
223 -s exit:0 grep "^foo" ${HOME}/master.passwd
224 atf_check -s exit:0 ${PW} userdel foo
225 atf_check -s exit:0 \
226 ${PW} useradd foo -e 20-03-37
227 atf_check -o inline:"foo:*:1001:1001::0:2121120000:User &:/home/foo:/bin/sh\n" \
228 -s exit:0 grep "^foo" ${HOME}/master.passwd
229 atf_check -s exit:0 ${PW} userdel foo
230 atf_check -s exit:0 \
231 ${PW} useradd foo -e 20-Mar-2037
232 atf_check -o inline:"foo:*:1001:1001::0:2121120000:User &:/home/foo:/bin/sh\n" \
233 -s exit:0 grep "^foo" ${HOME}/master.passwd
234 atf_check -s exit:0 ${PW} userdel foo
235 atf_check -e inline:"pw: Invalid date\n" -s exit:1 \
236 ${PW} useradd foo -e 20-Foo-2037
237 atf_check -e inline:"pw: Invalid date\n" -s exit:1 \
238 ${PW} useradd foo -e 20-13-2037
239 atf_check -s exit:0 ${PW} useradd foo -e "12:00 20-03-2037"
240 atf_check -s exit:0 ${PW} userdel foo
241 atf_check -e inline:"pw: Invalid date\n" -s exit:1 \
242 ${PW} useradd foo -e "12 20-03-2037"
243 atf_check -s exit:0 ${PW} useradd foo -e "20-03-2037 12:00"
244 atf_check -s exit:0 ${PW} userdel foo
247 atf_test_case user_add_invalid_user_entry
248 user_add_invalid_user_entry_body() {
249 touch ${HOME}/master.passwd
252 pwd_mkdb -p -d ${HOME} ${HOME}/master.passwd || \
253 atf_fail "generate passwd from master.passwd"
254 atf_check -s exit:0 ${PW} useradd foo
255 echo "foo1:*:1002" >> ${HOME}/master.passwd
256 atf_check -s exit:1 -e match:"Invalid user entry" ${PW} useradd foo2
259 atf_test_case user_add_invalid_group_entry
260 user_add_invalid_group_entry_body() {
261 touch ${HOME}/master.passwd
264 pwd_mkdb -p -d ${HOME} ${HOME}/master.passwd || \
265 atf_fail "generate passwd from master.passwd"
266 atf_check -s exit:0 ${PW} useradd foo
267 echo 'foo1:*:1002' >> group
268 atf_check -s exit:1 -e match:"Invalid group entry" ${PW} useradd foo2
271 atf_test_case user_add_password_from_h
272 user_add_password_from_h_body() {
275 atf_check -s exit:0 ${PW} useradd foo -h 0 <<-EOF
278 passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
279 atf_check -s exit:0 -o inline:$passhash \
280 $(atf_get_srcdir)/crypt $passhash "mypassword"
283 atf_test_case user_add_R
285 populate_root_etc_skel
287 atf_check -s exit:0 ${RPW} useradd foo
288 atf_check -s exit:0 ${RPW} useradd bar -m
289 test -d ${HOME}/home || atf_fail "Home parent directory not created"
290 test -d ${HOME}/home/bar || atf_fail "Directory not created"
291 atf_check -s exit:0 ${RPW} userdel bar
292 test -d ${HOME}/home/bar || atf_fail "Directory removed"
293 atf_check -s exit:0 ${RPW} useradd bar
294 atf_check -s exit:0 ${RPW} userdel bar -r
295 [ ! -d ${HOME}/home/bar ] || atf_fail "Directory not removed"
298 atf_test_case user_add_R_symlink
299 user_add_R_symlink_body() {
300 populate_root_etc_skel
303 atf_check -s exit:0 ${RPW} useradd foo -m
304 test -d ${HOME}/usr/home || atf_fail "Home parent directory not created"
305 test -h ${HOME}/home || atf_fail "/home directory is not a symlink"
306 atf_check -s exit:0 -o inline:"usr/home\n" readlink ${HOME}/home
309 atf_test_case user_add_skel
310 user_add_skel_body() {
311 populate_root_etc_skel
314 echo "a" > ${HOME}/skel/.a
315 echo "b" > ${HOME}/skel/b
317 mkdir ${HOME}/skel/c/d
318 mkdir ${HOME}/skel/dot.plop
319 echo "c" > ${HOME}/skel/c/d/dot.c
321 ln -sf /nonexistent ${HOME}/skel/c/foo
322 atf_check -s exit:0 ${RPW} useradd foo -k /skel -m
323 test -d ${HOME}/home/foo || atf_fail "Directory not created"
324 test -f ${HOME}/home/foo/.a || atf_fail "File not created"
325 atf_check -o file:${HOME}/skel/.a -s exit:0 cat ${HOME}/home/foo/.a
326 atf_check -o file:${HOME}/skel/b -s exit:0 cat ${HOME}/home/foo/b
327 test -d ${HOME}/home/foo/c || atf_fail "Dotted directory in skel not copied"
328 test -d ${HOME}/home/foo/.plop || atf_fail "Directory in skell not created"
329 atf_check -o inline:"/nonexistent\n" -s ignore readlink -f ${HOME}/home/foo/c/foo
330 atf_check -o file:${HOME}/skel/c/d/dot.c -s exit:0 cat ${HOME}/home/foo/c/d/.c
333 atf_test_case user_add_uid0
334 user_add_uid0_body() {
336 atf_check -e inline:"pw: WARNING: new account \`foo' has a uid of 0 (superuser access!)\n" \
337 -s exit:0 ${PW} useradd foo -u 0 -g 0 -d /root -s /bin/sh -c "Bourne-again Superuser" -o
339 -o inline:"foo:*:0:0::0:0:Bourne-again Superuser:/root:/bin/sh\n" \
340 -s exit:0 ${PW} usershow foo
343 atf_test_case user_add_uid_too_large
344 user_add_uid_too_large_body() {
346 atf_check -s exit:64 -e inline:"pw: Bad id '9999999999999': too large\n" \
347 ${PW} useradd -n test1 -u 9999999999999
350 atf_test_case user_add_bad_shell
351 user_add_bad_shell_body() {
354 atf_check -s exit:0 ${PW} useradd foo -s sh
355 atf_check -s exit:78 -e ignore ${PW} useradd bar -s badshell
358 atf_test_case user_add_already_exists
359 user_add_already_exists_body() {
362 atf_check -s exit:0 ${PW} useradd foo
363 atf_check -s exit:65 \
364 -e inline:"pw: login name \`foo' already exists\n" \
368 atf_test_case user_add_w_error
369 user_add_w_error_body() {
372 atf_check -s exit:1 -e match:"pw: Invalid value for default password" \
373 ${PW} useradd foo -w invalid_value
376 atf_test_case user_add_w_no
377 user_add_w_no_body() {
380 atf_check -s exit:0 ${PW} useradd foo -w no
381 atf_check -s exit:0 -o match:"^foo:\*" grep "^foo:" $HOME/master.passwd
384 atf_test_case user_add_w_none
385 user_add_w_none_body() {
388 atf_check -s exit:0 ${PW} useradd foo -w none
389 atf_check -s exit:0 -o match:"^foo::" grep "^foo:" $HOME/master.passwd
392 atf_test_case user_add_w_random
393 user_add_w_random_body() {
396 password=`${PW} useradd foo -w random | cat`
397 passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
398 atf_check -s exit:0 -o inline:$passhash \
399 $(atf_get_srcdir)/crypt $passhash "$password"
402 atf_test_case user_add_w_yes
403 user_add_w_yes_body() {
405 password=`${PW} useradd foo -w random | cat`
406 passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
407 atf_check -s exit:0 -o inline:$passhash \
408 $(atf_get_srcdir)/crypt $passhash "$password"
411 atf_test_case user_add_with_pw_conf
412 user_add_with_pw_conf_body()
415 atf_check -s exit:0 \
416 ${PW} useradd -D -C ${HOME}/pw.conf \
417 -u 2000,32767 -i 2000,32767
418 atf_check -s exit:0 \
419 -o inline:"minuid = 2000\nmaxuid = 32767\nmingid = 2000\nmaxgid = 32767\n" \
420 grep "^m.*id =" ${HOME}/pw.conf
421 atf_check -s exit:0 \
422 ${PW} useradd foo -C ${HOME}/pw.conf
424 atf_test_case user_add_defaultgroup
425 user_add_defaultgroup_body()
428 echo 'defaultgroup = "plop"' > ${HOME}/pw.conf
429 atf_check -s exit:0 \
430 ${PW} groupadd plop -g 442
431 atf_check -s exit:0 \
432 ${PW} useradd foo -C ${HOME}/pw.conf
433 atf_check -s exit:0 \
434 -o inline:"foo:*:1001:442::0:0:User &:/home/foo:/bin/sh\n" \
438 atf_init_test_cases() {
439 atf_add_test_case user_add
440 atf_add_test_case user_add_noupdate
441 atf_add_test_case user_add_comments
442 atf_add_test_case user_add_comments_noupdate
443 atf_add_test_case user_add_comments_invalid
444 atf_add_test_case user_add_comments_invalid_noupdate
445 atf_add_test_case user_add_homedir
446 atf_add_test_case user_add_account_expiration_epoch
447 atf_add_test_case user_add_account_expiration_date_numeric
448 atf_add_test_case user_add_account_expiration_date_month
449 atf_add_test_case user_add_account_expiration_date_relative
450 atf_add_test_case user_add_password_expiration_epoch
451 atf_add_test_case user_add_password_expiration_date_numeric
452 atf_add_test_case user_add_password_expiration_date_month
453 atf_add_test_case user_add_password_expiration_date_relative
454 atf_add_test_case user_add_name_too_long
455 atf_add_test_case user_add_name_with_spaces
456 atf_add_test_case user_add_name_with_spaces_and_gid_specified
457 atf_add_test_case user_add_expiration
458 atf_add_test_case user_add_invalid_user_entry
459 atf_add_test_case user_add_invalid_group_entry
460 atf_add_test_case user_add_password_from_h
461 atf_add_test_case user_add_R
462 atf_add_test_case user_add_R_symlink
463 atf_add_test_case user_add_skel
464 atf_add_test_case user_add_uid0
465 atf_add_test_case user_add_uid_too_large
466 atf_add_test_case user_add_bad_shell
467 atf_add_test_case user_add_already_exists
468 atf_add_test_case user_add_w_error
469 atf_add_test_case user_add_w_no
470 atf_add_test_case user_add_w_none
471 atf_add_test_case user_add_w_random
472 atf_add_test_case user_add_w_yes
473 atf_add_test_case user_add_with_pw_conf
474 atf_add_test_case user_add_defaultgroup