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/bar || atf_fail "Directory not created"
250 atf_check -s exit:0 ${RPW} userdel bar
251 test -d ${HOME}/home/bar || atf_fail "Directory removed"
252 atf_check -s exit:0 ${RPW} useradd bar
253 atf_check -s exit:0 ${RPW} userdel bar -r
254 [ ! -d ${HOME}/home/bar ] || atf_fail "Directory not removed"
257 atf_test_case user_add_skel
258 user_add_skel_body() {
259 populate_root_etc_skel
262 echo "a" > ${HOME}/skel/.ae
263 echo "b" > ${HOME}/skel/b
265 mkdir ${HOME}/skel/c/d
266 mkdir ${HOME}/skel/dot.plop
267 echo "c" > ${HOME}/skel/c/d/dot.c
269 ln -sf /nonexistent ${HOME}/skel/c/foo
270 atf_check -s exit:0 ${RPW} useradd foo -k /skel -m
271 test -d ${HOME}/home/foo || atf_fail "Directory not created"
272 test -f ${HOME}/home/foo/.a || atf_fail "File not created"
273 atf_check -o file:${HOME}/skel/.a -s exit:0 cat ${HOME}/home/foo/.a
274 atf_check -o file:${HOME}/skel/b -s exit:0 cat ${HOME}/home/foo/b
275 test -d ${HOME}/home/foo/c || atf_fail "Dotted directory in skel not copied"
276 test -d ${HOME}/home/foo/.plop || atf_fail "Directory in skell not created"
277 atf_check -o inline:"/nonexistent\n" -s ignore readlink -f ${HOME}/home/foo/c/foo
278 atf_check -o file:${HOME}/skel/c/d/dot.c -s exit:0 cat ${HOME}/home/foo/c/d/.c
281 atf_test_case user_add_uid0
282 user_add_uid0_body() {
284 atf_check -e inline:"pw: WARNING: new account \`foo' has a uid of 0 (superuser access!)\n" \
285 -s exit:0 ${PW} useradd foo -u 0 -g 0 -d /root -s /bin/sh -c "Bourne-again Superuser" -o
287 -o inline:"foo:*:0:0::0:0:Bourne-again Superuser:/root:/bin/sh\n" \
288 -s exit:0 ${PW} usershow foo
291 atf_test_case user_add_uid_too_large
292 user_add_uid_too_large_body() {
294 atf_check -s exit:64 -e inline:"pw: Bad id '9999999999999': too large\n" \
295 ${PW} useradd -n test1 -u 9999999999999
298 atf_test_case user_add_bad_shell
299 user_add_bad_shell_body() {
302 atf_check -s exit:0 ${PW} useradd foo -s sh
303 atf_check -s exit:78 -e ignore ${PW} useradd bar -s badshell
306 atf_test_case user_add_already_exists
307 user_add_already_exists_body() {
310 atf_check -s exit:0 ${PW} useradd foo
311 atf_check -s exit:65 \
312 -e inline:"pw: login name \`foo' already exists\n" \
316 atf_test_case user_add_w_yes
317 user_add_w_yes_body() {
319 atf_check -s exit:0 ${PW} useradd foo -w yes
320 atf_check -s exit:0 \
321 -o match:'^foo:\$.*' \
322 grep "^foo" ${HOME}/master.passwd
323 atf_check -s exit:0 ${PW} usermod foo -w yes
324 atf_check -s exit:0 \
325 -o match:'^foo:\$.*' \
326 grep "^foo" ${HOME}/master.passwd
329 atf_init_test_cases() {
330 atf_add_test_case user_add
331 atf_add_test_case user_add_noupdate
332 atf_add_test_case user_add_comments
333 atf_add_test_case user_add_comments_noupdate
334 atf_add_test_case user_add_comments_invalid
335 atf_add_test_case user_add_comments_invalid_noupdate
336 atf_add_test_case user_add_homedir
337 atf_add_test_case user_add_account_expiration_epoch
338 atf_add_test_case user_add_account_expiration_date_numeric
339 atf_add_test_case user_add_account_expiration_date_month
340 atf_add_test_case user_add_account_expiration_date_relative
341 atf_add_test_case user_add_password_expiration_epoch
342 atf_add_test_case user_add_password_expiration_date_numeric
343 atf_add_test_case user_add_password_expiration_date_month
344 atf_add_test_case user_add_password_expiration_date_relative
345 atf_add_test_case user_add_name_too_long
346 atf_add_test_case user_add_expiration
347 atf_add_test_case user_add_invalid_user_entry
348 atf_add_test_case user_add_invalid_group_entry
349 atf_add_test_case user_add_password_from_h
350 atf_add_test_case user_add_R
351 atf_add_test_case user_add_skel
352 atf_add_test_case user_add_uid0
353 atf_add_test_case user_add_uid_too_large
354 atf_add_test_case user_add_bad_shell
355 atf_add_test_case user_add_already_exists
356 atf_add_test_case user_add_w_yes