xref: /freebsd/usr.sbin/pw/tests/pw_userdel_test.sh (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1# $FreeBSD$
2
3# Import helper functions
4. $(atf_get_srcdir)/helper_functions.shin
5
6
7# Test that a user can be deleted when another user is part of this
8# user's default group and does not go into an infinate loop.
9# PR: 191427
10atf_test_case rmuser_seperate_group cleanup
11rmuser_seperate_group_head() {
12	atf_set "timeout" "30"
13}
14rmuser_seperate_group_body() {
15	populate_etc_skel
16	${PW} useradd test || atf_fail "Creating test user"
17	${PW} groupmod test -M 'test,root' || \
18		atf_fail "Modifying the group"
19	${PW} userdel test || atf_fail "Delete the test user"
20}
21
22
23atf_test_case user_do_not_try_to_delete_root_if_user_unknown
24user_do_not_try_to_delete_root_if_user_unknown_head() {
25	atf_set "descr" \
26		"Make sure not to try to remove root if deleting an unknown user"
27}
28user_do_not_try_to_delete_root_if_user_unknown_body() {
29	populate_etc_skel
30	atf_check -e inline:"pw: Bad id 'plop': invalid\n" -s exit:64 -x \
31		${PW} userdel -u plop
32}
33
34atf_test_case delete_files
35delete_files_body() {
36	populate_root_etc_skel
37
38	mkdir -p ${HOME}/skel
39	touch ${HOME}/skel/a
40	mkdir -p ${HOME}/home
41	mkdir -p ${HOME}/var/mail
42	atf_check -s exit:0 ${RPW} useradd foo -k /skel -m
43	test -d ${HOME}/home || atf_fail "Fail to create home directory"
44	test -f ${HOME}/var/mail/foo || atf_fail "Mail file not created"
45	atf_check -s exit:0 ${RPW} userdel foo -r
46	if test -f ${HOME}/var/mail/foo; then
47		atf_fail "Mail file not removed"
48	fi
49}
50
51atf_test_case delete_numeric_name
52delete_numeric_name_body() {
53	populate_etc_skel
54
55	atf_check ${PW} useradd -n foo -u 4001
56	atf_check -e inline:"pw: no such user \`4001'\n" -s exit:67 \
57		${PW} userdel -n 4001
58}
59
60atf_test_case home_not_a_dir
61home_not_a_dir_body() {
62	populate_root_etc_skel
63	touch ${HOME}/foo
64	atf_check ${RPW} useradd foo -d /foo
65	atf_check ${RPW} userdel foo -r
66}
67
68atf_test_case home_shared
69home_shared_body() {
70	populate_root_etc_skel
71	mkdir ${HOME}/shared
72	atf_check ${RPW} useradd -n testuser1 -d /shared
73	atf_check ${RPW} useradd -n testuser2 -d /shared
74	atf_check ${RPW} userdel -n testuser1 -r
75	test -d ${HOME}/shared || atf_fail "Shared home has been removed"
76}
77
78atf_test_case home_regular_dir
79home_regular_dir_body() {
80	populate_root_etc_skel
81	atf_check ${RPW} useradd -n foo -d /foo
82	atf_check ${RPW} userdel -n foo -r
83	[ ! -d ${HOME}/foo ] || atf_fail "Home has not been removed"
84}
85
86atf_init_test_cases() {
87	atf_add_test_case rmuser_seperate_group
88	atf_add_test_case user_do_not_try_to_delete_root_if_user_unknown
89	atf_add_test_case delete_files
90	atf_add_test_case delete_numeric_name
91	atf_add_test_case home_not_a_dir
92	atf_add_test_case home_shared
93	atf_add_test_case home_regular_dir
94}
95