]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - usr.sbin/pw/tests/pw_useradd.sh
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / usr.sbin / pw / tests / pw_useradd.sh
1 # $FreeBSD$
2
3 # Import helper functions
4 . $(atf_get_srcdir)/helper_functions.shin
5
6 # Test add user
7 atf_test_case user_add
8 user_add_body() {
9         populate_etc_skel
10
11         atf_check -s exit:0 ${PW} useradd test
12         atf_check -s exit:0 -o match:"^test:.*" \
13                 grep "^test:.*" $HOME/master.passwd
14 }
15
16 # Test add user with option -N
17 atf_test_case user_add_noupdate
18 user_add_noupdate_body() {
19         populate_etc_skel
20
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
23 }
24
25 # Test add user with comments
26 atf_test_case user_add_comments
27 user_add_comments_body() {
28         populate_etc_skel
29
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
33 }
34
35 # Test add user with comments and option -N
36 atf_test_case user_add_comments_noupdate
37 user_add_comments_noupdate_body() {
38         populate_etc_skel
39
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
43 }
44
45 # Test add user with invalid comments
46 atf_test_case user_add_comments_invalid
47 user_add_comments_invalid_body() {
48         populate_etc_skel
49
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
54 }
55
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() {
59         populate_etc_skel
60
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
64 }
65
66 # Test add user with alternate homedir
67 atf_test_case user_add_homedir
68 user_add_homedir_body() {
69         populate_etc_skel
70
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:.*" \
73                 ${PW} usershow test
74 }
75
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() {
79         populate_etc_skel
80
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}:.*" \
84                 ${PW} usershow test
85 }
86
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() {
90         populate_etc_skel
91
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 &:.*" \
96                 ${PW} usershow test
97 }
98
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() {
102         populate_etc_skel
103
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 &:.*" \
108                 ${PW} usershow test
109 }
110
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() {
114         populate_etc_skel
115
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."
122 }
123
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() {
127         populate_etc_skel
128
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:.*" \
132                 ${PW} usershow test
133 }
134
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() {
138         populate_etc_skel
139
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 &:.*" \
144                 ${PW} usershow test
145 }
146
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() {
150         populate_etc_skel
151
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 &:.*" \
156                 ${PW} usershow test
157 }
158
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() {
162         populate_etc_skel
163
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."
170 }
171
172 atf_test_case user_add_name_too_long
173 user_add_name_too_long_body() {
174         populate_etc_skel
175         atf_check -e match:"too long" -s exit:64 \
176                 ${PW} useradd name_very_vert_very_very_very_long
177 }
178
179 atf_test_case user_add_expiration
180 user_add_expiration_body() {
181         populate_etc_skel
182
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
208 }
209
210 atf_test_case user_add_invalid_user_entry
211 user_add_invalid_user_entry_body() {
212         touch ${HOME}/master.passwd
213         touch ${HOME}/group
214
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
220 }
221
222 atf_test_case user_add_invalid_group_entry
223 user_add_invalid_group_entry_body() {
224         touch ${HOME}/master.passwd
225         touch ${HOME}/group
226
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
232 }
233
234 atf_test_case user_add_password_from_h
235 user_add_password_from_h_body() {
236         populate_etc_skel
237
238         atf_check -s exit:0 ${PW} useradd test -h 0 <<-EOF
239         $(echo test)
240         EOF
241 }
242
243 atf_test_case user_add_R
244 user_add_R_body() {
245         populate_root_etc_skel
246
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"
256 }
257
258 atf_test_case user_add_R_symlink
259 user_add_R_symlink_body() {
260         populate_root_etc_skel
261
262         mkdir ${HOME}/usr
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
267 }
268
269 atf_test_case user_add_skel
270 user_add_skel_body() {
271         populate_root_etc_skel
272
273         mkdir ${HOME}/skel
274         echo "a" > ${HOME}/skel/.a
275         echo "b" > ${HOME}/skel/b
276         mkdir ${HOME}/skel/c
277         mkdir ${HOME}/skel/c/d
278         mkdir ${HOME}/skel/dot.plop
279         echo "c" > ${HOME}/skel/c/d/dot.c
280         mkdir ${HOME}/home
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
291 }
292
293 atf_test_case user_add_uid0
294 user_add_uid0_body() {
295         populate_etc_skel
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
298         atf_check \
299                 -o inline:"foo:*:0:0::0:0:Bourne-again Superuser:/root:/bin/sh\n" \
300                 -s exit:0 ${PW} usershow foo
301 }
302
303 atf_test_case user_add_uid_too_large
304 user_add_uid_too_large_body() {
305         populate_etc_skel
306         atf_check -s exit:64 -e inline:"pw: Bad id '9999999999999': too large\n" \
307                 ${PW} useradd -n test1 -u 9999999999999
308 }
309
310 atf_test_case user_add_bad_shell
311 user_add_bad_shell_body() {
312         populate_etc_skel
313
314         atf_check -s exit:0 ${PW} useradd foo -s sh
315         atf_check -s exit:78 -e ignore ${PW} useradd bar -s badshell
316 }
317
318 atf_test_case user_add_already_exists
319 user_add_already_exists_body() {
320         populate_etc_skel
321
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" \
325                 ${PW} useradd foo
326 }
327
328 atf_test_case user_add_w_yes
329 user_add_w_yes_body() {
330         populate_etc_skel
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
339 }
340
341 atf_test_case user_add_with_pw_conf
342 user_add_with_pw_conf_body()
343 {
344         populate_etc_skel
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
353 }
354
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
385 }