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