xref: /freebsd/usr.sbin/pw/tests/pw_useradd_test.sh (revision e12b7446bddcb91b869abef6048910cc15185a33)
1
2# Import helper functions
3. $(atf_get_srcdir)/helper_functions.shin
4
5# Test add user
6atf_test_case user_add
7user_add_body() {
8	populate_etc_skel
9
10	atf_check -s exit:0 ${PW} useradd test
11	atf_check -s exit:0 -o match:"^test:.*" \
12		grep "^test:.*" $HOME/master.passwd
13}
14
15# Test add user with option -N
16atf_test_case user_add_noupdate
17user_add_noupdate_body() {
18	populate_etc_skel
19
20	atf_check -s exit:0 -o match:"^test:.*" ${PW} useradd test -N
21	atf_check -s exit:1 -o empty grep "^test:.*" $HOME/master.passwd
22}
23
24# Test add user with comments
25atf_test_case user_add_comments
26user_add_comments_body() {
27	populate_etc_skel
28
29	atf_check -s exit:0 ${PW} useradd test -c 'Test User,work!,123,user@example.com'
30	atf_check -s exit:0 -o match:'^test:.*:Test User,work!,123,user@example.com:' \
31		grep '^test:.*:Test User,work!,123,user@example.com:' $HOME/master.passwd
32}
33
34# Test add user with comments and option -N
35atf_test_case user_add_comments_noupdate
36user_add_comments_noupdate_body() {
37	populate_etc_skel
38
39	atf_check -s exit:0 -o match:"^test:.*:Test User,work,123,456:" \
40		${PW} useradd test -c "Test User,work,123,456" -N
41	atf_check -s exit:1 -o empty grep "^test:.*" $HOME/master.passwd
42}
43
44# Test add user with invalid comments
45atf_test_case user_add_comments_invalid
46user_add_comments_invalid_body() {
47	populate_etc_skel
48
49	atf_check -s exit:65 -e match:"invalid character" \
50		${PW} useradd test -c "Test User,work,123:456,456"
51	atf_check -s exit:1 -o empty \
52		grep "^test:.*:Test User,work,123:456,456:" $HOME/master.passwd
53}
54
55# Test add user with invalid comments and option -N
56atf_test_case user_add_comments_invalid_noupdate
57user_add_comments_invalid_noupdate_body() {
58	populate_etc_skel
59
60	atf_check -s exit:65 -e match:"invalid character" \
61		${PW} useradd test -c "Test User,work,123:456,456" -N
62	atf_check -s exit:1 -o empty grep "^test:.*" $HOME/master.passwd
63}
64
65# Test add user with alternate homedir
66atf_test_case user_add_homedir
67user_add_homedir_body() {
68	populate_etc_skel
69
70	atf_check -s exit:0 ${PW} useradd test -d /foo/bar
71	atf_check -s exit:0 -o match:"^test:\*:.*::0:0:User &:/foo/bar:.*" \
72		${PW} usershow test
73}
74
75# Test add user with account expiration as an epoch date
76atf_test_case user_add_account_expiration_epoch
77user_add_account_expiration_epoch_body() {
78	populate_etc_skel
79
80	DATE=`date -j -v+1d "+%s"`
81	atf_check -s exit:0 ${PW} useradd test -e ${DATE}
82	atf_check -s exit:0 -o match:"^test:\*:.*::0:${DATE}:.*" \
83		${PW} usershow test
84}
85
86# Test add user with account expiration as a DD-MM-YYYY date
87atf_test_case user_add_account_expiration_date_numeric
88user_add_account_expiration_date_numeric_body() {
89	populate_etc_skel
90
91	DATE=`date -j -v+1d "+%d-%m-%Y"`
92	EPOCH=`date -j -f "%d-%m-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
93	atf_check -s exit:0 ${PW} useradd test -e ${DATE}
94	atf_check -s exit:0 -o match:"^test:\*:.*::0:${EPOCH}:User &:.*" \
95		${PW} usershow test
96}
97
98# Test add user with account expiration as a DD-MM-YYYY date
99atf_test_case user_add_account_expiration_date_month
100user_add_account_expiration_date_month_body() {
101	populate_etc_skel
102
103	DATE=`date -j -v+1d "+%d-%b-%Y"`
104	EPOCH=`date -j -f "%d-%b-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
105	atf_check -s exit:0 ${PW} useradd test -e ${DATE}
106	atf_check -s exit:0 -o match:"^test:\*:.*::0:${EPOCH}:User &:.*" \
107		${PW} usershow test
108}
109
110# Test add user with account expiration as a relative date
111atf_test_case user_add_account_expiration_date_relative
112user_add_account_expiration_date_relative_body() {
113	populate_etc_skel
114
115	EPOCH=`date -j -v+13m "+%s"`
116	BUF=`expr $EPOCH + 5`
117	atf_check -s exit:0 ${PW} useradd test -e +13o
118	TIME=`${PW} usershow test | awk -F ':' '{print $7}'`
119	[ ! -z $TIME -a $TIME -ge $EPOCH -a $TIME -lt $BUF ] || \
120		atf_fail "Expiration time($TIME) was not within $EPOCH - $BUF seconds."
121}
122
123# Test add user with password expiration as an epoch date
124atf_test_case user_add_password_expiration_epoch
125user_add_password_expiration_epoch_body() {
126	populate_etc_skel
127
128	DATE=`date -j -v+1d "+%s"`
129	atf_check -s exit:0 ${PW} useradd test -p ${DATE}
130	atf_check -s exit:0 -o match:"^test:\*:.*::${DATE}:0:.*" \
131		${PW} usershow test
132}
133
134# Test add user with password expiration as a DD-MM-YYYY date
135atf_test_case user_add_password_expiration_date_numeric
136user_add_password_expiration_date_numeric_body() {
137	populate_etc_skel
138
139	DATE=`date -j -v+1d "+%d-%m-%Y"`
140	EPOCH=`date -j -f "%d-%m-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
141	atf_check -s exit:0 ${PW} useradd test -p ${DATE}
142	atf_check -s exit:0 -o match:"^test:\*:.*::${EPOCH}:0:User &:.*" \
143		${PW} usershow test
144}
145
146# Test add user with password expiration as a DD-MMM-YYYY date
147atf_test_case user_add_password_expiration_date_month
148user_add_password_expiration_date_month_body() {
149	populate_etc_skel
150
151	DATE=`date -j -v+1d "+%d-%b-%Y"`
152	EPOCH=`date -j -f "%d-%b-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
153	atf_check -s exit:0 ${PW} useradd test -p ${DATE}
154	atf_check -s exit:0 -o match:"^test:\*:.*::${EPOCH}:0:User &:.*" \
155		${PW} usershow test
156}
157
158# Test add user with password expiration as a relative date
159atf_test_case user_add_password_expiration_date_relative
160user_add_password_expiration_date_relative_body() {
161	populate_etc_skel
162
163	EPOCH=`date -j -v+13m "+%s"`
164	BUF=`expr $EPOCH + 5`
165	atf_check -s exit:0 ${PW} useradd test -p +13o
166	TIME=`${PW} usershow test | awk -F ':' '{print $6}'`
167	[ ! -z $TIME -a $TIME -ge $EPOCH -a $TIME -lt $BUF ] || \
168		atf_fail "Expiration time($TIME) was not within $EPOCH - $BUF seconds."
169}
170
171atf_test_case user_add_name_too_long
172user_add_name_too_long_body() {
173	populate_etc_skel
174	atf_check -e match:"too long" -s exit:64 \
175		${PW} useradd name_very_vert_very_very_very_long
176}
177
178atf_test_case user_add_name_with_spaces
179user_add_name_with_spaces_body() {
180	populate_etc_skel
181	atf_check -s exit:65 -e match:"invalid character" \
182		  ${PW} useradd 'test user'
183	atf_check -s exit:1 -o empty grep "^test user:.*" $HOME/master.passwd
184	# Try again with -n which uses a slightly different code path.
185	atf_check -s exit:65 -e match:"invalid character" \
186		  ${PW} useradd -n 'test user'
187	atf_check -s exit:1 -o empty grep "^test user:.*" $HOME/master.passwd
188}
189
190atf_test_case user_add_name_with_spaces_and_gid_specified
191user_add_name_with_spaces_and_gid_specified_body() {
192	populate_etc_skel
193	gid=12345
194	user_name="test user"
195	# pw useradd should fail because of the space in the user
196	# name, not because the group doesn't exist.
197	atf_check -s exit:65 -e match:"invalid character" \
198		  ${PW} useradd "${user_name}" -g ${gid}
199	atf_check -s exit:1 -o empty grep "^${user_name}:.*" $HOME/master.passwd
200	# Try again with -n which uses a slightly different code path.
201	atf_check -s exit:65 -e match:"invalid character" \
202		  ${PW} useradd -n "${user_name}" -g ${gid}
203	atf_check -s exit:1 -o empty grep "^${user_name}:.*" $HOME/master.passwd
204	# Make sure the user isn't added even if the group exists
205	atf_check -s exit:0 ${PW} groupadd blafasel -g ${gid}
206	atf_check -s exit:65 -e match:"invalid character" \
207		  ${PW} useradd "${user_name}" -g ${gid}
208	atf_check -s exit:1 -o empty grep "^${user_name}:.*" $HOME/master.passwd
209	# Try again with the -n option.
210	atf_check -s exit:65 -e match:"invalid character" \
211		  ${PW} useradd -n "${user_name}" -g ${gid}
212	atf_check -s exit:1 -o empty grep "^${user_name}:.*" $HOME/master.passwd
213}
214
215atf_test_case user_add_expiration
216user_add_expiration_body() {
217	populate_etc_skel
218
219	atf_check -s exit:0 \
220		${PW} useradd foo -e 20-03-2037
221	atf_check -o inline:"foo:*:1001:1001::0:2121120000:User &:/home/foo:/bin/sh\n" \
222		-s exit:0 grep "^foo" ${HOME}/master.passwd
223	atf_check -s exit:0 ${PW} userdel foo
224	atf_check -s exit:0 \
225		${PW} useradd foo -e 20-03-37
226	atf_check -o inline:"foo:*:1001:1001::0:2121120000:User &:/home/foo:/bin/sh\n" \
227		-s exit:0 grep "^foo" ${HOME}/master.passwd
228	atf_check -s exit:0 ${PW} userdel foo
229	atf_check -s exit:0 \
230		${PW} useradd foo -e 20-Mar-2037
231	atf_check -o inline:"foo:*:1001:1001::0:2121120000:User &:/home/foo:/bin/sh\n" \
232		-s exit:0 grep "^foo" ${HOME}/master.passwd
233	atf_check -s exit:0 ${PW} userdel foo
234	atf_check -e inline:"pw: Invalid date\n" -s exit:1 \
235		${PW} useradd foo -e 20-Foo-2037
236	atf_check -e inline:"pw: Invalid date\n" -s exit:1 \
237		${PW} useradd foo -e 20-13-2037
238	atf_check -s exit:0 ${PW} useradd foo -e "12:00 20-03-2037"
239	atf_check -s exit:0 ${PW} userdel foo
240	atf_check -e inline:"pw: Invalid date\n" -s exit:1 \
241		${PW} useradd foo -e "12 20-03-2037"
242	atf_check -s exit:0 ${PW} useradd foo -e "20-03-2037	12:00"
243	atf_check -s exit:0 ${PW} userdel foo
244}
245
246atf_test_case user_add_invalid_user_entry
247user_add_invalid_user_entry_body() {
248	touch ${HOME}/master.passwd
249	touch ${HOME}/group
250
251	pwd_mkdb -p -d ${HOME} ${HOME}/master.passwd || \
252		atf_fail "generate passwd from master.passwd"
253	atf_check -s exit:0 ${PW} useradd foo
254	echo "foo1:*:1002" >> ${HOME}/master.passwd
255	atf_check -s exit:1 -e match:"Invalid user entry" ${PW} useradd foo2
256}
257
258atf_test_case user_add_invalid_group_entry
259user_add_invalid_group_entry_body() {
260	touch ${HOME}/master.passwd
261	touch ${HOME}/group
262
263	pwd_mkdb -p -d ${HOME} ${HOME}/master.passwd || \
264		atf_fail "generate passwd from master.passwd"
265	atf_check -s exit:0 ${PW} useradd foo
266	echo 'foo1:*:1002' >> group
267	atf_check -s exit:1 -e match:"Invalid group entry" ${PW} useradd foo2
268}
269
270atf_test_case user_add_password_from_h
271user_add_password_from_h_body() {
272	populate_etc_skel
273
274	atf_check -s exit:0 ${PW} useradd foo -h 0 <<-EOF
275	$(echo mypassword)
276	EOF
277	passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
278	atf_check -s exit:0 -o inline:$passhash \
279		$(atf_get_srcdir)/crypt $passhash "mypassword"
280}
281
282atf_test_case user_add_R
283user_add_R_body() {
284	populate_root_etc_skel
285
286	atf_check -s exit:0 ${RPW} useradd foo
287	atf_check -s exit:0 ${RPW} useradd bar -m
288	test -d ${HOME}/home || atf_fail "Home parent directory not created"
289	test -d ${HOME}/home/bar || atf_fail "Directory not created"
290	atf_check -s exit:0 ${RPW} userdel bar
291	test -d ${HOME}/home/bar || atf_fail "Directory removed"
292	atf_check -s exit:0 ${RPW} useradd bar
293	atf_check -s exit:0 ${RPW} userdel bar -r
294	[ ! -d ${HOME}/home/bar ] || atf_fail "Directory not removed"
295}
296
297atf_test_case user_add_R_no_symlink
298user_add_R_no_symlink_body() {
299	populate_root_etc_skel
300
301	mkdir ${HOME}/usr
302	atf_check -s exit:0 ${RPW} useradd foo -m
303	[ ! -d ${HOME}/usr/home ] || atf_fail "/usr/home created"
304	test -d ${HOME}/home || atf_fail "/home directory not created"
305}
306
307atf_test_case user_add_R_intermed
308user_add_R_intermed_body() {
309	populate_root_etc_skel
310
311	atf_check -s exit:0 ${RPW} useradd foo -m -d /a/b/c/foo
312	test -d ${HOME}/a/b/c || atf_fail "intermediate directories not created"
313	test -d ${HOME}/a/b/c/foo || atf_fail "user directory not created"
314}
315
316atf_test_case user_add_dir
317user_add_dir_body() {
318	populate_root_etc_skel
319
320	atf_check -s exit:0 ${RPW} useradd foo -M 0705 -m
321	atf_check grep -q '^foo:' $HOME/etc/master.passwd
322	atf_check test -d ${HOME}/home/foo
323	atf_check -o save:ugid \
324	      awk -F: '$1 == "foo" { print $3, $4 }' \
325	      $HOME/etc/master.passwd
326	atf_check -o file:ugid \
327	    stat -f '%u %g' ${HOME}/home/foo
328	atf_check -o inline:"40705\n" \
329	    stat -f '%p' ${HOME}/home/foo
330}
331
332atf_test_case user_add_skel
333user_add_skel_body() {
334	populate_root_etc_skel
335
336	mkdir ${HOME}/skel
337	echo "a" > ${HOME}/skel/.a
338	echo "b" > ${HOME}/skel/b
339	mkdir ${HOME}/skel/c
340	mkdir ${HOME}/skel/c/d
341	mkdir ${HOME}/skel/dot.plop
342	echo "c" > ${HOME}/skel/c/d/dot.c
343	mkdir ${HOME}/home
344	ln -sf /nonexistent ${HOME}/skel/c/foo
345	atf_check -s exit:0 ${RPW} useradd foo -k /skel -m
346	test -d ${HOME}/home/foo || atf_fail "Directory not created"
347	test -f ${HOME}/home/foo/.a || atf_fail "File not created"
348	atf_check -o file:${HOME}/skel/.a -s exit:0 cat ${HOME}/home/foo/.a
349	atf_check -o file:${HOME}/skel/b -s exit:0 cat ${HOME}/home/foo/b
350	test -d ${HOME}/home/foo/c || atf_fail "Dotted directory in skel not copied"
351	test -d ${HOME}/home/foo/.plop || atf_fail "Directory in skell not created"
352	atf_check -o inline:"/nonexistent\n" -s ignore readlink -f ${HOME}/home/foo/c/foo
353	atf_check -o file:${HOME}/skel/c/d/dot.c -s exit:0 cat ${HOME}/home/foo/c/d/.c
354}
355
356atf_test_case user_add_uid0
357user_add_uid0_body() {
358	populate_etc_skel
359	atf_check -e inline:"pw: WARNING: new account \`foo' has a uid of 0 (superuser access!)\n" \
360		-s exit:0 ${PW} useradd foo -u 0 -g 0 -d /root -s /bin/sh -c "Bourne-again Superuser" -o
361	atf_check \
362		-o inline:"foo:*:0:0::0:0:Bourne-again Superuser:/root:/bin/sh\n" \
363		-s exit:0 ${PW} usershow foo
364}
365
366atf_test_case user_add_uid_too_large
367user_add_uid_too_large_body() {
368	populate_etc_skel
369	atf_check -s exit:64 -e inline:"pw: Bad id '9999999999999': too large\n" \
370		${PW} useradd -n test1 -u 9999999999999
371}
372
373atf_test_case user_add_bad_shell
374user_add_bad_shell_body() {
375	populate_etc_skel
376
377	atf_check -s exit:0 ${PW} useradd foo -s sh
378	atf_check -s exit:78 -e ignore ${PW} useradd bar -s badshell
379}
380
381atf_test_case user_add_already_exists
382user_add_already_exists_body() {
383	populate_etc_skel
384
385	atf_check -s exit:0 ${PW} useradd foo
386	atf_check -s exit:65 \
387		-e inline:"pw: login name \`foo' already exists\n" \
388		${PW} useradd foo
389}
390
391atf_test_case user_add_w_error
392user_add_w_error_body() {
393	populate_etc_skel
394
395	atf_check -s exit:1 -e match:"pw: Invalid value for default password" \
396		${PW} useradd foo -w invalid_value
397}
398
399atf_test_case user_add_w_no
400user_add_w_no_body() {
401	populate_etc_skel
402
403	atf_check -s exit:0 ${PW} useradd foo -w no
404	atf_check -s exit:0 -o match:"^foo:\*" grep "^foo:" $HOME/master.passwd
405}
406
407atf_test_case user_add_w_none
408user_add_w_none_body() {
409	populate_etc_skel
410
411	atf_check -s exit:0 ${PW} useradd foo -w none
412	atf_check -s exit:0 -o match:"^foo::" grep "^foo:" $HOME/master.passwd
413}
414
415atf_test_case user_add_w_random
416user_add_w_random_body() {
417	populate_etc_skel
418
419	password=`${PW} useradd foo -w random | cat`
420	passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
421	atf_check -s exit:0 -o inline:$passhash \
422		$(atf_get_srcdir)/crypt $passhash "$password"
423}
424
425atf_test_case user_add_w_yes
426user_add_w_yes_body() {
427	populate_etc_skel
428	password=`${PW} useradd foo -w random | cat`
429	passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
430	atf_check -s exit:0 -o inline:$passhash \
431		$(atf_get_srcdir)/crypt $passhash "$password"
432}
433
434atf_test_case user_add_with_pw_conf
435user_add_with_pw_conf_body()
436{
437	populate_etc_skel
438	atf_check -s exit:0 \
439		${PW} useradd -D -C ${HOME}/pw.conf \
440		-u 2000,32767 -i 2000,32767
441	atf_check -s exit:0 \
442		-o inline:"minuid = 2000\nmaxuid = 32767\nmingid = 2000\nmaxgid = 32767\n" \
443		grep "^m.*id =" ${HOME}/pw.conf
444	atf_check -s exit:0 \
445		${PW} useradd foo -C ${HOME}/pw.conf
446}
447
448atf_test_case user_add_defaultgroup
449user_add_defaultgroup_body()
450{
451	populate_etc_skel
452	echo 'defaultgroup = "plop"' > ${HOME}/pw.conf
453	atf_check -s exit:0 \
454		${PW} groupadd plop -g 442
455	atf_check -s exit:0 \
456		${PW} useradd foo -C ${HOME}/pw.conf
457	atf_check -s exit:0 \
458		-o inline:"foo:*:1001:442::0:0:User &:/home/foo:/bin/sh\n" \
459		${PW} usershow foo
460}
461
462atf_test_case user_add_conf_defaultpasswd
463user_add_conf_defaultpasswd_body()
464{
465	populate_etc_skel
466
467	atf_check -s exit:0 ${PW} useradd -D -w no
468	atf_check -o inline:"defaultpasswd = \"no\"\n" \
469	    grep defaultpasswd ${HOME}/pw.conf
470	atf_check -s exit:0 ${PW} useradd -D -w none
471	atf_check -o inline:"defaultpasswd = \"none\"\n" \
472	    grep defaultpasswd ${HOME}/pw.conf
473	atf_check -s exit:0 ${PW} useradd -D -w random
474	atf_check -o inline:"defaultpasswd = \"random\"\n" \
475	    grep defaultpasswd ${HOME}/pw.conf
476	atf_check -s exit:0 ${PW} useradd -D -w yes
477	atf_check -o inline:"defaultpasswd = \"yes\"\n" \
478	    grep defaultpasswd ${HOME}/pw.conf
479}
480
481atf_test_case user_add_existing_login_group
482user_add_existing_login_group_body()
483{
484	populate_etc_skel
485
486	atf_check -s exit:0 ${PW} groupadd testuser
487	atf_check -s exit:0 ${PW} useradd user1 -G testuser
488	atf_check -s exit:0 ${PW} useradd testuser
489	atf_check -o match:"1" \
490	    sh -c "grep testuser ${HOME}/group | wc -l"
491}
492
493atf_test_case user_add_already_in_group
494user_add_already_in_group_body()
495{
496	populate_etc_skel
497
498	echo "testgroup:*:4242:testuser" >> ${HOME}/group
499	atf_check -s exit:0 ${PW} useradd testuser -G testgroup
500	atf_check -o not-match:"testuser,testuser" \
501		grep testuser ${HOME}/group
502}
503
504atf_init_test_cases() {
505	atf_add_test_case user_add
506	atf_add_test_case user_add_noupdate
507	atf_add_test_case user_add_comments
508	atf_add_test_case user_add_comments_noupdate
509	atf_add_test_case user_add_comments_invalid
510	atf_add_test_case user_add_comments_invalid_noupdate
511	atf_add_test_case user_add_homedir
512	atf_add_test_case user_add_account_expiration_epoch
513	atf_add_test_case user_add_account_expiration_date_numeric
514	atf_add_test_case user_add_account_expiration_date_month
515	atf_add_test_case user_add_account_expiration_date_relative
516	atf_add_test_case user_add_password_expiration_epoch
517	atf_add_test_case user_add_password_expiration_date_numeric
518	atf_add_test_case user_add_password_expiration_date_month
519	atf_add_test_case user_add_password_expiration_date_relative
520	atf_add_test_case user_add_name_too_long
521	atf_add_test_case user_add_name_with_spaces
522	atf_add_test_case user_add_name_with_spaces_and_gid_specified
523	atf_add_test_case user_add_expiration
524	atf_add_test_case user_add_invalid_user_entry
525	atf_add_test_case user_add_invalid_group_entry
526	atf_add_test_case user_add_password_from_h
527	atf_add_test_case user_add_R
528	atf_add_test_case user_add_R_no_symlink
529	atf_add_test_case user_add_R_intermed
530	atf_add_test_case user_add_dir
531	atf_add_test_case user_add_skel
532	atf_add_test_case user_add_uid0
533	atf_add_test_case user_add_uid_too_large
534	atf_add_test_case user_add_bad_shell
535	atf_add_test_case user_add_already_exists
536	atf_add_test_case user_add_w_error
537	atf_add_test_case user_add_w_no
538	atf_add_test_case user_add_w_none
539	atf_add_test_case user_add_w_random
540	atf_add_test_case user_add_w_yes
541	atf_add_test_case user_add_with_pw_conf
542	atf_add_test_case user_add_defaultgroup
543
544	atf_add_test_case user_add_conf_defaultpasswd
545	atf_add_test_case user_add_existing_login_group
546	atf_add_test_case user_add_already_in_group
547}
548