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,456"
31 atf_check -s exit:0 -o match:"^test:.*:Test User,work,123,456:" \
32 grep "^test:.*:Test User,work,123,456:" $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_expiration
180 user_add_expiration_body() {
183 atf_check -s exit:0 \
184 ${PW} useradd foo -e 20-03-2037
185 atf_check -o inline:"foo:*:1001:1001::0:2121120000:User &:/home/foo:/bin/sh\n" \
186 -s exit:0 grep "^foo" ${HOME}/master.passwd
187 atf_check -s exit:0 ${PW} userdel foo
188 atf_check -s exit:0 \
189 ${PW} useradd foo -e 20-03-37
190 atf_check -o inline:"foo:*:1001:1001::0:2121120000:User &:/home/foo:/bin/sh\n" \
191 -s exit:0 grep "^foo" ${HOME}/master.passwd
192 atf_check -s exit:0 ${PW} userdel foo
193 atf_check -s exit:0 \
194 ${PW} useradd foo -e 20-Mar-2037
195 atf_check -o inline:"foo:*:1001:1001::0:2121120000:User &:/home/foo:/bin/sh\n" \
196 -s exit:0 grep "^foo" ${HOME}/master.passwd
197 atf_check -s exit:0 ${PW} userdel foo
198 atf_check -e inline:"pw: Invalid date\n" -s exit:1 \
199 ${PW} useradd foo -e 20-Foo-2037
200 atf_check -e inline:"pw: Invalid date\n" -s exit:1 \
201 ${PW} useradd foo -e 20-13-2037
202 atf_check -s exit:0 ${PW} useradd foo -e "12:00 20-03-2037"
203 atf_check -s exit:0 ${PW} userdel foo
204 atf_check -e inline:"pw: Invalid date\n" -s exit:1 \
205 ${PW} useradd foo -e "12 20-03-2037"
206 atf_check -s exit:0 ${PW} useradd foo -e "20-03-2037 12:00"
207 atf_check -s exit:0 ${PW} userdel foo
210 atf_test_case user_add_invalid_user_entry
211 user_add_invalid_user_entry_body() {
212 touch ${HOME}/master.passwd
215 pwd_mkdb -p -d ${HOME} ${HOME}/master.passwd || \
216 atf_fail "generate passwd from master.passwd"
217 atf_check -s exit:0 ${PW} useradd foo
218 echo "foo1:*:1002" >> ${HOME}/master.passwd
219 atf_check -s exit:1 -e match:"Invalid user entry" ${PW} useradd foo2
222 atf_test_case user_add_invalid_group_entry
223 user_add_invalid_group_entry_body() {
224 touch ${HOME}/master.passwd
227 pwd_mkdb -p -d ${HOME} ${HOME}/master.passwd || \
228 atf_fail "generate passwd from master.passwd"
229 atf_check -s exit:0 ${PW} useradd foo
230 echo 'foo1:*:1002' >> group
231 atf_check -s exit:1 -e match:"Invalid group entry" ${PW} useradd foo2
234 atf_test_case user_add_password_from_h
235 user_add_password_from_h_body() {
238 atf_check -s exit:0 ${PW} useradd test -h 0 <<-EOF
243 atf_test_case user_add_R
245 populate_root_etc_skel
247 atf_check -s exit:0 ${RPW} useradd foo
248 atf_check -s exit:0 ${RPW} useradd bar -m
249 test -d ${HOME}/home || atf_fail "Home parent directory not created"
250 test -d ${HOME}/home/bar || atf_fail "Directory not created"
251 atf_check -s exit:0 ${RPW} userdel bar
252 test -d ${HOME}/home/bar || atf_fail "Directory removed"
253 atf_check -s exit:0 ${RPW} useradd bar
254 atf_check -s exit:0 ${RPW} userdel bar -r
255 [ ! -d ${HOME}/home/bar ] || atf_fail "Directory not removed"
258 atf_test_case user_add_R_symlink
259 user_add_R_symlink_body() {
260 populate_root_etc_skel
263 atf_check -s exit:0 ${RPW} useradd foo -m
264 test -d ${HOME}/usr/home || atf_fail "Home parent directory not created"
265 test -h ${HOME}/home || atf_fail "/home directory is not a symlink"
266 atf_check -s exit:0 -o inline:"usr/home\n" readlink ${HOME}/home
269 atf_test_case user_add_skel
270 user_add_skel_body() {
271 populate_root_etc_skel
274 echo "a" > ${HOME}/skel/.a
275 echo "b" > ${HOME}/skel/b
277 mkdir ${HOME}/skel/c/d
278 mkdir ${HOME}/skel/dot.plop
279 echo "c" > ${HOME}/skel/c/d/dot.c
281 ln -sf /nonexistent ${HOME}/skel/c/foo
282 atf_check -s exit:0 ${RPW} useradd foo -k /skel -m
283 test -d ${HOME}/home/foo || atf_fail "Directory not created"
284 test -f ${HOME}/home/foo/.a || atf_fail "File not created"
285 atf_check -o file:${HOME}/skel/.a -s exit:0 cat ${HOME}/home/foo/.a
286 atf_check -o file:${HOME}/skel/b -s exit:0 cat ${HOME}/home/foo/b
287 test -d ${HOME}/home/foo/c || atf_fail "Dotted directory in skel not copied"
288 test -d ${HOME}/home/foo/.plop || atf_fail "Directory in skell not created"
289 atf_check -o inline:"/nonexistent\n" -s ignore readlink -f ${HOME}/home/foo/c/foo
290 atf_check -o file:${HOME}/skel/c/d/dot.c -s exit:0 cat ${HOME}/home/foo/c/d/.c
293 atf_test_case user_add_uid0
294 user_add_uid0_body() {
296 atf_check -e inline:"pw: WARNING: new account \`foo' has a uid of 0 (superuser access!)\n" \
297 -s exit:0 ${PW} useradd foo -u 0 -g 0 -d /root -s /bin/sh -c "Bourne-again Superuser" -o
299 -o inline:"foo:*:0:0::0:0:Bourne-again Superuser:/root:/bin/sh\n" \
300 -s exit:0 ${PW} usershow foo
303 atf_test_case user_add_uid_too_large
304 user_add_uid_too_large_body() {
306 atf_check -s exit:64 -e inline:"pw: Bad id '9999999999999': too large\n" \
307 ${PW} useradd -n test1 -u 9999999999999
310 atf_test_case user_add_bad_shell
311 user_add_bad_shell_body() {
314 atf_check -s exit:0 ${PW} useradd foo -s sh
315 atf_check -s exit:78 -e ignore ${PW} useradd bar -s badshell
318 atf_test_case user_add_already_exists
319 user_add_already_exists_body() {
322 atf_check -s exit:0 ${PW} useradd foo
323 atf_check -s exit:65 \
324 -e inline:"pw: login name \`foo' already exists\n" \
328 atf_test_case user_add_w_yes
329 user_add_w_yes_body() {
331 atf_check -s exit:0 ${PW} useradd foo -w yes
332 atf_check -s exit:0 \
333 -o match:'^foo:\$.*' \
334 grep "^foo" ${HOME}/master.passwd
335 atf_check -s exit:0 ${PW} usermod foo -w yes
336 atf_check -s exit:0 \
337 -o match:'^foo:\$.*' \
338 grep "^foo" ${HOME}/master.passwd
341 atf_test_case user_add_with_pw_conf
342 user_add_with_pw_conf_body()
345 atf_check -s exit:0 \
346 ${PW} useradd -D -C ${HOME}/pw.conf \
347 -u 2000,32767 -i 2000,32767
348 atf_check -s exit:0 \
349 -o inline:"minuid = 2000\nmaxuid = 32767\nmingid = 2000\nmaxgid = 32767\n" \
350 grep "^m.*id =" ${HOME}/pw.conf
351 atf_check -s exit:0 \
352 ${PW} useradd foo -C ${HOME}/pw.conf
355 atf_init_test_cases() {
356 atf_add_test_case user_add
357 atf_add_test_case user_add_noupdate
358 atf_add_test_case user_add_comments
359 atf_add_test_case user_add_comments_noupdate
360 atf_add_test_case user_add_comments_invalid
361 atf_add_test_case user_add_comments_invalid_noupdate
362 atf_add_test_case user_add_homedir
363 atf_add_test_case user_add_account_expiration_epoch
364 atf_add_test_case user_add_account_expiration_date_numeric
365 atf_add_test_case user_add_account_expiration_date_month
366 atf_add_test_case user_add_account_expiration_date_relative
367 atf_add_test_case user_add_password_expiration_epoch
368 atf_add_test_case user_add_password_expiration_date_numeric
369 atf_add_test_case user_add_password_expiration_date_month
370 atf_add_test_case user_add_password_expiration_date_relative
371 atf_add_test_case user_add_name_too_long
372 atf_add_test_case user_add_expiration
373 atf_add_test_case user_add_invalid_user_entry
374 atf_add_test_case user_add_invalid_group_entry
375 atf_add_test_case user_add_password_from_h
376 atf_add_test_case user_add_R
377 atf_add_test_case user_add_R_symlink
378 atf_add_test_case user_add_skel
379 atf_add_test_case user_add_uid0
380 atf_add_test_case user_add_uid_too_large
381 atf_add_test_case user_add_bad_shell
382 atf_add_test_case user_add_already_exists
383 atf_add_test_case user_add_w_yes
384 atf_add_test_case user_add_with_pw_conf