xref: /freebsd/usr.sbin/pw/tests/pw_useradd_test.sh (revision 02e9120893770924227138ba49df1edb3896112a)
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_skel
317user_add_skel_body() {
318	populate_root_etc_skel
319
320	mkdir ${HOME}/skel
321	echo "a" > ${HOME}/skel/.a
322	echo "b" > ${HOME}/skel/b
323	mkdir ${HOME}/skel/c
324	mkdir ${HOME}/skel/c/d
325	mkdir ${HOME}/skel/dot.plop
326	echo "c" > ${HOME}/skel/c/d/dot.c
327	mkdir ${HOME}/home
328	ln -sf /nonexistent ${HOME}/skel/c/foo
329	atf_check -s exit:0 ${RPW} useradd foo -k /skel -m
330	test -d ${HOME}/home/foo || atf_fail "Directory not created"
331	test -f ${HOME}/home/foo/.a || atf_fail "File not created"
332	atf_check -o file:${HOME}/skel/.a -s exit:0 cat ${HOME}/home/foo/.a
333	atf_check -o file:${HOME}/skel/b -s exit:0 cat ${HOME}/home/foo/b
334	test -d ${HOME}/home/foo/c || atf_fail "Dotted directory in skel not copied"
335	test -d ${HOME}/home/foo/.plop || atf_fail "Directory in skell not created"
336	atf_check -o inline:"/nonexistent\n" -s ignore readlink -f ${HOME}/home/foo/c/foo
337	atf_check -o file:${HOME}/skel/c/d/dot.c -s exit:0 cat ${HOME}/home/foo/c/d/.c
338}
339
340atf_test_case user_add_uid0
341user_add_uid0_body() {
342	populate_etc_skel
343	atf_check -e inline:"pw: WARNING: new account \`foo' has a uid of 0 (superuser access!)\n" \
344		-s exit:0 ${PW} useradd foo -u 0 -g 0 -d /root -s /bin/sh -c "Bourne-again Superuser" -o
345	atf_check \
346		-o inline:"foo:*:0:0::0:0:Bourne-again Superuser:/root:/bin/sh\n" \
347		-s exit:0 ${PW} usershow foo
348}
349
350atf_test_case user_add_uid_too_large
351user_add_uid_too_large_body() {
352	populate_etc_skel
353	atf_check -s exit:64 -e inline:"pw: Bad id '9999999999999': too large\n" \
354		${PW} useradd -n test1 -u 9999999999999
355}
356
357atf_test_case user_add_bad_shell
358user_add_bad_shell_body() {
359	populate_etc_skel
360
361	atf_check -s exit:0 ${PW} useradd foo -s sh
362	atf_check -s exit:78 -e ignore ${PW} useradd bar -s badshell
363}
364
365atf_test_case user_add_already_exists
366user_add_already_exists_body() {
367	populate_etc_skel
368
369	atf_check -s exit:0 ${PW} useradd foo
370	atf_check -s exit:65 \
371		-e inline:"pw: login name \`foo' already exists\n" \
372		${PW} useradd foo
373}
374
375atf_test_case user_add_w_error
376user_add_w_error_body() {
377	populate_etc_skel
378
379	atf_check -s exit:1 -e match:"pw: Invalid value for default password" \
380		${PW} useradd foo -w invalid_value
381}
382
383atf_test_case user_add_w_no
384user_add_w_no_body() {
385	populate_etc_skel
386
387	atf_check -s exit:0 ${PW} useradd foo -w no
388	atf_check -s exit:0 -o match:"^foo:\*" grep "^foo:" $HOME/master.passwd
389}
390
391atf_test_case user_add_w_none
392user_add_w_none_body() {
393	populate_etc_skel
394
395	atf_check -s exit:0 ${PW} useradd foo -w none
396	atf_check -s exit:0 -o match:"^foo::" grep "^foo:" $HOME/master.passwd
397}
398
399atf_test_case user_add_w_random
400user_add_w_random_body() {
401	populate_etc_skel
402
403	password=`${PW} useradd foo -w random | cat`
404	passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
405	atf_check -s exit:0 -o inline:$passhash \
406		$(atf_get_srcdir)/crypt $passhash "$password"
407}
408
409atf_test_case user_add_w_yes
410user_add_w_yes_body() {
411	populate_etc_skel
412	password=`${PW} useradd foo -w random | cat`
413	passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
414	atf_check -s exit:0 -o inline:$passhash \
415		$(atf_get_srcdir)/crypt $passhash "$password"
416}
417
418atf_test_case user_add_with_pw_conf
419user_add_with_pw_conf_body()
420{
421	populate_etc_skel
422	atf_check -s exit:0 \
423		${PW} useradd -D -C ${HOME}/pw.conf \
424		-u 2000,32767 -i 2000,32767
425	atf_check -s exit:0 \
426		-o inline:"minuid = 2000\nmaxuid = 32767\nmingid = 2000\nmaxgid = 32767\n" \
427		grep "^m.*id =" ${HOME}/pw.conf
428	atf_check -s exit:0 \
429		${PW} useradd foo -C ${HOME}/pw.conf
430}
431
432atf_test_case user_add_defaultgroup
433user_add_defaultgroup_body()
434{
435	populate_etc_skel
436	echo 'defaultgroup = "plop"' > ${HOME}/pw.conf
437	atf_check -s exit:0 \
438		${PW} groupadd plop -g 442
439	atf_check -s exit:0 \
440		${PW} useradd foo -C ${HOME}/pw.conf
441	atf_check -s exit:0 \
442		-o inline:"foo:*:1001:442::0:0:User &:/home/foo:/bin/sh\n" \
443		${PW} usershow foo
444}
445
446atf_test_case user_add_conf_defaultpasswd
447user_add_conf_defaultpasswd_body()
448{
449	populate_etc_skel
450
451	atf_check -s exit:0 ${PW} useradd -D -w no
452	atf_check -o inline:"defaultpasswd = \"no\"\n" \
453	    grep defaultpasswd ${HOME}/pw.conf
454	atf_check -s exit:0 ${PW} useradd -D -w none
455	atf_check -o inline:"defaultpasswd = \"none\"\n" \
456	    grep defaultpasswd ${HOME}/pw.conf
457	atf_check -s exit:0 ${PW} useradd -D -w random
458	atf_check -o inline:"defaultpasswd = \"random\"\n" \
459	    grep defaultpasswd ${HOME}/pw.conf
460	atf_check -s exit:0 ${PW} useradd -D -w yes
461	atf_check -o inline:"defaultpasswd = \"yes\"\n" \
462	    grep defaultpasswd ${HOME}/pw.conf
463}
464
465atf_test_case user_add_existing_login_group
466user_add_existing_login_group_body()
467{
468	populate_etc_skel
469
470	atf_check -s exit:0 ${PW} groupadd testuser
471	atf_check -s exit:0 ${PW} useradd user1 -G testuser
472	atf_check -s exit:0 ${PW} useradd testuser
473	atf_check -o match:"1" \
474	    sh -c "grep testuser ${HOME}/group | wc -l"
475}
476
477atf_test_case user_add_already_in_group
478user_add_already_in_group_body()
479{
480	populate_etc_skel
481
482	echo "testgroup:*:4242:testuser" >> ${HOME}/group
483	atf_check -s exit:0 ${PW} useradd testuser -G testgroup
484	atf_check -o not-match:"testuser,testuser" \
485		grep testuser ${HOME}/group
486}
487
488atf_init_test_cases() {
489	atf_add_test_case user_add
490	atf_add_test_case user_add_noupdate
491	atf_add_test_case user_add_comments
492	atf_add_test_case user_add_comments_noupdate
493	atf_add_test_case user_add_comments_invalid
494	atf_add_test_case user_add_comments_invalid_noupdate
495	atf_add_test_case user_add_homedir
496	atf_add_test_case user_add_account_expiration_epoch
497	atf_add_test_case user_add_account_expiration_date_numeric
498	atf_add_test_case user_add_account_expiration_date_month
499	atf_add_test_case user_add_account_expiration_date_relative
500	atf_add_test_case user_add_password_expiration_epoch
501	atf_add_test_case user_add_password_expiration_date_numeric
502	atf_add_test_case user_add_password_expiration_date_month
503	atf_add_test_case user_add_password_expiration_date_relative
504	atf_add_test_case user_add_name_too_long
505	atf_add_test_case user_add_name_with_spaces
506	atf_add_test_case user_add_name_with_spaces_and_gid_specified
507	atf_add_test_case user_add_expiration
508	atf_add_test_case user_add_invalid_user_entry
509	atf_add_test_case user_add_invalid_group_entry
510	atf_add_test_case user_add_password_from_h
511	atf_add_test_case user_add_R
512	atf_add_test_case user_add_R_no_symlink
513	atf_add_test_case user_add_R_intermed
514	atf_add_test_case user_add_skel
515	atf_add_test_case user_add_uid0
516	atf_add_test_case user_add_uid_too_large
517	atf_add_test_case user_add_bad_shell
518	atf_add_test_case user_add_already_exists
519	atf_add_test_case user_add_w_error
520	atf_add_test_case user_add_w_no
521	atf_add_test_case user_add_w_none
522	atf_add_test_case user_add_w_random
523	atf_add_test_case user_add_w_yes
524	atf_add_test_case user_add_with_pw_conf
525	atf_add_test_case user_add_defaultgroup
526
527	atf_add_test_case user_add_conf_defaultpasswd
528	atf_add_test_case user_add_existing_login_group
529	atf_add_test_case user_add_already_in_group
530}
531