pw_useradd_test.sh revision 316257
1# $FreeBSD: stable/10/usr.sbin/pw/tests/pw_useradd_test.sh 316257 2017-03-30 06:20:58Z ngie $
2
3# Import helper functions
4. $(atf_get_srcdir)/helper_functions.shin
5
6# Test add user
7atf_test_case user_add
8user_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
17atf_test_case user_add_noupdate
18user_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
26atf_test_case user_add_comments
27user_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
36atf_test_case user_add_comments_noupdate
37user_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
46atf_test_case user_add_comments_invalid
47user_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
57atf_test_case user_add_comments_invalid_noupdate
58user_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
67atf_test_case user_add_homedir
68user_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
77atf_test_case user_add_account_expiration_epoch
78user_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
88atf_test_case user_add_account_expiration_date_numeric
89user_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
100atf_test_case user_add_account_expiration_date_month
101user_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
112atf_test_case user_add_account_expiration_date_relative
113user_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
125atf_test_case user_add_password_expiration_epoch
126user_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
136atf_test_case user_add_password_expiration_date_numeric
137user_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
148atf_test_case user_add_password_expiration_date_month
149user_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
160atf_test_case user_add_password_expiration_date_relative
161user_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
172atf_test_case user_add_name_too_long
173user_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
179atf_test_case user_add_expiration
180user_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
210atf_test_case user_add_invalid_user_entry
211user_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
222atf_test_case user_add_invalid_group_entry
223user_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
234atf_test_case user_add_password_from_h
235user_add_password_from_h_body() {
236	populate_etc_skel
237
238	atf_check -s exit:0 ${PW} useradd foo -h 0 <<-EOF
239	$(echo mypassword)
240	EOF
241	passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
242	atf_check -s exit:0 -o inline:$passhash \
243		$(atf_get_srcdir)/crypt $passhash "mypassword"
244}
245
246atf_test_case user_add_R
247user_add_R_body() {
248	populate_root_etc_skel
249
250	atf_check -s exit:0 ${RPW} useradd foo
251	atf_check -s exit:0 ${RPW} useradd bar -m
252	test -d ${HOME}/home || atf_fail "Home parent directory not created"
253	test -d ${HOME}/home/bar || atf_fail "Directory not created"
254	atf_check -s exit:0 ${RPW} userdel bar
255	test -d ${HOME}/home/bar || atf_fail "Directory removed"
256	atf_check -s exit:0 ${RPW} useradd bar
257	atf_check -s exit:0 ${RPW} userdel bar -r
258	[ ! -d ${HOME}/home/bar ] || atf_fail "Directory not removed"
259}
260
261atf_test_case user_add_R_symlink
262user_add_R_symlink_body() {
263	populate_root_etc_skel
264
265	mkdir ${HOME}/usr
266	atf_check -s exit:0 ${RPW} useradd foo -m
267	test -d ${HOME}/usr/home || atf_fail "Home parent directory not created"
268	test -h ${HOME}/home || atf_fail "/home directory is not a symlink"
269	atf_check -s exit:0 -o inline:"usr/home\n" readlink ${HOME}/home
270}
271
272atf_test_case user_add_skel
273user_add_skel_body() {
274	populate_root_etc_skel
275
276	mkdir ${HOME}/skel
277	echo "a" > ${HOME}/skel/.a
278	echo "b" > ${HOME}/skel/b
279	mkdir ${HOME}/skel/c
280	mkdir ${HOME}/skel/c/d
281	mkdir ${HOME}/skel/dot.plop
282	echo "c" > ${HOME}/skel/c/d/dot.c
283	mkdir ${HOME}/home
284	ln -sf /nonexistent ${HOME}/skel/c/foo
285	atf_check -s exit:0 ${RPW} useradd foo -k /skel -m
286	test -d ${HOME}/home/foo || atf_fail "Directory not created"
287	test -f ${HOME}/home/foo/.a || atf_fail "File not created"
288	atf_check -o file:${HOME}/skel/.a -s exit:0 cat ${HOME}/home/foo/.a
289	atf_check -o file:${HOME}/skel/b -s exit:0 cat ${HOME}/home/foo/b
290	test -d ${HOME}/home/foo/c || atf_fail "Dotted directory in skel not copied"
291	test -d ${HOME}/home/foo/.plop || atf_fail "Directory in skell not created"
292	atf_check -o inline:"/nonexistent\n" -s ignore readlink -f ${HOME}/home/foo/c/foo
293	atf_check -o file:${HOME}/skel/c/d/dot.c -s exit:0 cat ${HOME}/home/foo/c/d/.c
294}
295
296atf_test_case user_add_uid0
297user_add_uid0_body() {
298	populate_etc_skel
299	atf_check -e inline:"pw: WARNING: new account \`foo' has a uid of 0 (superuser access!)\n" \
300		-s exit:0 ${PW} useradd foo -u 0 -g 0 -d /root -s /bin/sh -c "Bourne-again Superuser" -o
301	atf_check \
302		-o inline:"foo:*:0:0::0:0:Bourne-again Superuser:/root:/bin/sh\n" \
303		-s exit:0 ${PW} usershow foo
304}
305
306atf_test_case user_add_uid_too_large
307user_add_uid_too_large_body() {
308	populate_etc_skel
309	atf_check -s exit:64 -e inline:"pw: Bad id '9999999999999': too large\n" \
310		${PW} useradd -n test1 -u 9999999999999
311}
312
313atf_test_case user_add_bad_shell
314user_add_bad_shell_body() {
315	populate_etc_skel
316
317	atf_check -s exit:0 ${PW} useradd foo -s sh
318	atf_check -s exit:78 -e ignore ${PW} useradd bar -s badshell
319}
320
321atf_test_case user_add_already_exists
322user_add_already_exists_body() {
323	populate_etc_skel
324
325	atf_check -s exit:0 ${PW} useradd foo
326	atf_check -s exit:65 \
327		-e inline:"pw: login name \`foo' already exists\n" \
328		${PW} useradd foo
329}
330
331atf_test_case user_add_w_error
332user_add_w_error_body() {
333	populate_etc_skel
334
335	atf_check -s exit:1 -e match:"pw: Invalid value for default password" \
336		${PW} useradd foo -w invalid_value
337}
338
339atf_test_case user_add_w_no
340user_add_w_no_body() {
341	populate_etc_skel
342
343	atf_check -s exit:0 ${PW} useradd foo -w no
344	atf_check -s exit:0 -o match:"^foo:\*" grep "^foo:" $HOME/master.passwd
345}
346
347atf_test_case user_add_w_none
348user_add_w_none_body() {
349	populate_etc_skel
350
351	atf_check -s exit:0 ${PW} useradd foo -w none
352	atf_check -s exit:0 -o match:"^foo::" grep "^foo:" $HOME/master.passwd
353}
354
355atf_test_case user_add_w_random
356user_add_w_random_body() {
357	populate_etc_skel
358
359	password=`${PW} useradd foo -w random | cat`
360	passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
361	atf_check -s exit:0 -o inline:$passhash \
362		$(atf_get_srcdir)/crypt $passhash "$password"
363}
364
365atf_test_case user_add_w_yes
366user_add_w_yes_body() {
367	populate_etc_skel
368	password=`${PW} useradd foo -w random | cat`
369	passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
370	atf_check -s exit:0 -o inline:$passhash \
371		$(atf_get_srcdir)/crypt $passhash "$password"
372}
373
374atf_test_case user_add_with_pw_conf
375user_add_with_pw_conf_body()
376{
377	populate_etc_skel
378	atf_check -s exit:0 \
379		${PW} useradd -D -C ${HOME}/pw.conf \
380		-u 2000,32767 -i 2000,32767
381	atf_check -s exit:0 \
382		-o inline:"minuid = 2000\nmaxuid = 32767\nmingid = 2000\nmaxgid = 32767\n" \
383		grep "^m.*id =" ${HOME}/pw.conf
384	atf_check -s exit:0 \
385		${PW} useradd foo -C ${HOME}/pw.conf
386}
387
388atf_init_test_cases() {
389	atf_add_test_case user_add
390	atf_add_test_case user_add_noupdate
391	atf_add_test_case user_add_comments
392	atf_add_test_case user_add_comments_noupdate
393	atf_add_test_case user_add_comments_invalid
394	atf_add_test_case user_add_comments_invalid_noupdate
395	atf_add_test_case user_add_homedir
396	atf_add_test_case user_add_account_expiration_epoch
397	atf_add_test_case user_add_account_expiration_date_numeric
398	atf_add_test_case user_add_account_expiration_date_month
399	atf_add_test_case user_add_account_expiration_date_relative
400	atf_add_test_case user_add_password_expiration_epoch
401	atf_add_test_case user_add_password_expiration_date_numeric
402	atf_add_test_case user_add_password_expiration_date_month
403	atf_add_test_case user_add_password_expiration_date_relative
404	atf_add_test_case user_add_name_too_long
405	atf_add_test_case user_add_expiration
406	atf_add_test_case user_add_invalid_user_entry
407	atf_add_test_case user_add_invalid_group_entry
408	atf_add_test_case user_add_password_from_h
409	atf_add_test_case user_add_R
410	atf_add_test_case user_add_R_symlink
411	atf_add_test_case user_add_skel
412	atf_add_test_case user_add_uid0
413	atf_add_test_case user_add_uid_too_large
414	atf_add_test_case user_add_bad_shell
415	atf_add_test_case user_add_already_exists
416	atf_add_test_case user_add_w_error
417	atf_add_test_case user_add_w_no
418	atf_add_test_case user_add_w_none
419	atf_add_test_case user_add_w_random
420	atf_add_test_case user_add_w_yes
421	atf_add_test_case user_add_with_pw_conf
422}
423