xref: /freebsd/usr.sbin/pw/tests/pw_groupmod_test.sh (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1
2# Import helper functions
3. $(atf_get_srcdir)/helper_functions.shin
4
5
6# Test adding & removing a user from a group
7atf_test_case groupmod_user
8groupmod_user_body() {
9	populate_etc_skel
10	atf_check -s exit:0 ${PW} addgroup test
11	atf_check -s exit:0 ${PW} groupmod test -m root
12	atf_check -s exit:0 -o match:"^test:\*:1001:root$" \
13		grep "^test:\*:.*:root$" $HOME/group
14	atf_check -s exit:0 ${PW} groupmod test -d root
15	atf_check -s exit:0 -o match:"^test:\*:1001:$" \
16		grep "^test:\*:.*:$" $HOME/group
17}
18
19
20# Test adding and removing a user that does not exist
21atf_test_case groupmod_invalid_user
22groupmod_invalid_user_body() {
23	populate_etc_skel
24	atf_check -s exit:0 ${PW} addgroup test
25	atf_check -s exit:67 -e match:"does not exist" ${PW} groupmod test -m foo
26	atf_check -s exit:0  ${PW} groupmod test -d foo
27}
28
29atf_test_case groupmod_bug_193704
30groupmod_bug_193704_head() {
31	atf_set "descr" "Regression test for the #193704 bug"
32}
33groupmod_bug_193704_body() {
34	populate_etc_skel
35	atf_check -s exit:0 -x ${PW} groupadd test
36	atf_check -s exit:0 -x ${PW} groupmod test -l newgroupname
37	atf_check -s exit:65 -e match:"^pw: unknown group" -x ${PW} groupshow test
38}
39
40atf_test_case usermod_bug_185666
41usermod_bug_185666_head() {
42	atf_set "descr" "Regression test for the #185666 bug"
43}
44
45usermod_bug_185666_body() {
46	populate_etc_skel
47	atf_check -s exit:0 -x ${PW} useradd testuser
48	atf_check -s exit:0 -x ${PW} groupadd testgroup
49	atf_check -s exit:0 -x ${PW} groupadd testgroup2
50	atf_check -s exit:0 -x ${PW} usermod testuser -G testgroup
51	atf_check -o inline:"testuser:*:1001:\n" -x ${PW} groupshow testuser
52	atf_check -o inline:"testgroup:*:1002:testuser\n" -x ${PW} groupshow testgroup
53	atf_check -o inline:"testgroup2:*:1003:\n" -x ${PW} groupshow testgroup2
54	atf_check -s exit:0 -x ${PW} usermod testuser -G testgroup2
55	atf_check -o inline:"testuser:*:1001:\n" -x ${PW} groupshow testuser
56	atf_check -o inline:"testgroup:*:1002:\n" -x ${PW} groupshow testgroup
57	atf_check -o inline:"testgroup2:*:1003:testuser\n" -x ${PW} groupshow testgroup2
58}
59
60atf_test_case do_not_duplicate_group_on_gid_change
61do_not_duplicate_group_on_gid_change_head() {
62	atf_set "descr" "Do not duplicate group on gid change"
63}
64
65do_not_duplicate_group_on_gid_change_body() {
66	populate_etc_skel
67	atf_check -s exit:0 -x ${PW} groupadd testgroup
68	atf_check -s exit:0 -x ${PW} groupmod testgroup -g 12345
69	# use grep to see if the entry has not be duplicated
70	atf_check -o inline:"testgroup:*:12345:\n" -s exit:0 -x grep "^testgroup" ${HOME}/group
71}
72
73atf_test_case groupmod_rename
74groupmod_rename_body() {
75	populate_etc_skel
76
77	atf_check -s exit:0 ${PW} groupadd foo
78	atf_check -s exit:0 ${PW} groupmod foo -l bar
79	atf_check -s exit:0 -o match:"^bar:.*" \
80		grep "^bar:.*" ${HOME}/group
81}
82
83atf_test_case groupmod_members
84groupmod_members_body() {
85	populate_etc_skel
86
87	for i in user1 user2 user3 user4; do
88		atf_check -s exit:0 ${PW} useradd $i
89	done
90
91	atf_check -s exit:0 ${PW} groupadd foo -M "user1, user2"
92	atf_check -o inline:"foo:*:1005:user1,user2\n" -s exit:0 \
93		${PW} groupshow foo
94	atf_check -s exit:0 ${PW} groupmod foo -m "user3, user4"
95	atf_check -o inline:"foo:*:1005:user1,user2,user3,user4\n" -s exit:0 \
96		${PW} groupshow foo
97	atf_check -s exit:0 ${PW} groupmod foo -M "user1, user4"
98	atf_check -o inline:"foo:*:1005:user1,user4\n" -s exit:0 \
99		${PW} groupshow foo
100	# what about duplicates
101	atf_check -s exit:0 ${PW} groupmod foo -m "user1, user2, user3, user4"
102	atf_check -o inline:"foo:*:1005:user1,user4,user2,user3\n" -s exit:0 \
103		${PW} groupshow foo
104	atf_check -s exit:0 ${PW} groupmod foo -d "user1, user3"
105	atf_check -o inline:"foo:*:1005:user4,user2\n" -s exit:0 \
106		${PW} groupshow foo
107}
108
109atf_init_test_cases() {
110	atf_add_test_case groupmod_user
111	atf_add_test_case groupmod_invalid_user
112	atf_add_test_case groupmod_bug_193704
113	atf_add_test_case usermod_bug_185666
114	atf_add_test_case do_not_duplicate_group_on_gid_change
115	atf_add_test_case groupmod_rename
116	atf_add_test_case groupmod_members
117}
118