xref: /freebsd/usr.sbin/pw/tests/pw_usermod_test.sh (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1# $FreeBSD$
2
3# Import helper functions
4. $(atf_get_srcdir)/helper_functions.shin
5
6# Test modifying a user
7atf_test_case user_mod
8user_mod_body() {
9	populate_etc_skel
10
11	atf_check -s exit:67 -e match:"no such user" ${PW} usermod test
12	atf_check -s exit:0 ${PW} useradd test
13	atf_check -s exit:0 ${PW} usermod test
14	atf_check -s exit:0 -o match:"^test:.*" \
15		grep "^test:.*" $HOME/master.passwd
16}
17
18# Test modifying a user with option -N
19atf_test_case user_mod_noupdate
20user_mod_noupdate_body() {
21	populate_etc_skel
22
23	atf_check -s exit:67 -e match:"no such user" ${PW} usermod test -N
24	atf_check -s exit:0 ${PW} useradd test
25	atf_check -s exit:0 -o match:"^test:.*" ${PW} usermod test -N
26	atf_check -s exit:0 -o match:"^test:.*" \
27		grep "^test:.*" $HOME/master.passwd
28}
29
30# Test modifying a user with comments
31atf_test_case user_mod_comments
32user_mod_comments_body() {
33	populate_etc_skel
34
35	atf_check -s exit:0 ${PW} useradd test -c "Test User,home,123,456"
36	atf_check -s exit:0 ${PW} usermod test -c "Test User,work,123,456"
37	atf_check -s exit:0 -o match:"^test:.*:Test User,work,123,456:" \
38		grep "^test:.*:Test User,work,123,456:" $HOME/master.passwd
39}
40
41# Test modifying a user with comments with option -N
42atf_test_case user_mod_comments_noupdate
43user_mod_comments_noupdate_body() {
44	populate_etc_skel
45
46	atf_check -s exit:0 ${PW} useradd test -c "Test User,home,123,456"
47	atf_check -s exit:0 -o match:"^test:.*:Test User,work,123,456:" \
48		${PW} usermod test -c "Test User,work,123,456" -N
49	atf_check -s exit:0 -o match:"^test:.*:Test User,home,123,456:" \
50		grep "^test:.*:Test User,home,123,456:" $HOME/master.passwd
51}
52
53# Test modifying a user with invalid comments
54atf_test_case user_mod_comments_invalid
55user_mod_comments_invalid_body() {
56	populate_etc_skel
57
58	atf_check -s exit:0 ${PW} useradd test
59	atf_check -s exit:65 -e match:"invalid character" \
60		${PW} usermod test -c "Test User,work,123:456,456"
61	atf_check -s exit:1 -o empty \
62		grep "^test:.*:Test User,work,123:456,456:" $HOME/master.passwd
63	atf_check -s exit:0 -o match:"^test:\*" \
64		grep "^test:\*" $HOME/master.passwd
65}
66
67# Test modifying a user with invalid comments with option -N
68atf_test_case user_mod_comments_invalid_noupdate
69user_mod_comments_invalid_noupdate_body() {
70	populate_etc_skel
71
72	atf_check -s exit:0 ${PW} useradd test
73	atf_check -s exit:65 -e match:"invalid character" \
74		${PW} usermod test -c "Test User,work,123:456,456" -N
75	atf_check -s exit:1 -o empty \
76		grep "^test:.*:Test User,work,123:456,456:" $HOME/master.passwd
77	atf_check -s exit:0 -o match:"^test:\*" \
78		grep "^test:\*" $HOME/master.passwd
79}
80
81# Test modifying a user name with -l
82atf_test_case user_mod_name
83user_mod_name_body() {
84	populate_etc_skel
85
86	atf_check -s exit:0 ${PW} useradd foo
87	atf_check -s exit:0 ${PW} usermod foo -l "bar"
88	atf_check -s exit:0 -o match:"^bar:.*" \
89		grep "^bar:.*" $HOME/master.passwd
90}
91
92# Test modifying a user name with -l with option -N
93atf_test_case user_mod_name_noupdate
94user_mod_name_noupdate_body() {
95	populate_etc_skel
96
97	atf_check -s exit:0 ${PW} useradd foo
98	atf_check -s exit:0 -o match:"^bar:.*" ${PW} usermod foo -l "bar" -N
99	atf_check -s exit:0 -o match:"^foo:.*" \
100		grep "^foo:.*" $HOME/master.passwd
101}
102
103atf_test_case user_mod_rename_multigroups
104user_mod_rename_multigroups_body() {
105	populate_etc_skel
106
107	atf_check -s exit:0 ${PW} groupadd test1
108	atf_check -s exit:0 ${PW} groupadd test2
109	atf_check -s exit:0 ${PW} useradd foo -G test1,test2
110	atf_check -o match:"foo" -s exit:0 ${PW} groupshow test1
111	atf_check -o match:"foo" -s exit:0 ${PW} groupshow test2
112	atf_check -s exit:0 ${PW} usermod foo -l bar
113	atf_check -o match:"bar" -s exit:0 ${PW} groupshow test1
114	atf_check -o match:"bar" -s exit:0 ${PW} groupshow test2
115}
116
117atf_test_case user_mod_nogroups
118user_mod_nogroups_body() {
119	populate_etc_skel
120
121	atf_check -s exit:0 ${PW} groupadd test1
122	atf_check -s exit:0 ${PW} groupadd test2
123	atf_check -s exit:0 ${PW} groupadd test3
124	atf_check -s exit:0 ${PW} groupadd test4
125	atf_check -s exit:0 ${PW} useradd foo -G test1,test2
126	atf_check -o match:"foo" -s exit:0 ${PW} groupshow test1
127	atf_check -o match:"foo" -s exit:0 ${PW} groupshow test2
128	atf_check -s exit:0 ${PW} usermod foo -G test3,test4
129	atf_check -s exit:0 -o inline:"test3\ntest4\n" \
130		awk -F\: '$4 == "foo" { print $1 }' ${HOME}/group
131	atf_check -s exit:0 ${PW} usermod foo -G ""
132	atf_check -s exit:0 -o empty \
133		awk -F\: '$4 == "foo" { print $1 }' ${HOME}/group
134}
135
136atf_test_case user_mod_rename
137user_mod_rename_body() {
138	populate_etc_skel
139
140	atf_check -s exit:0 ${PW} useradd foo
141	atf_check -s exit:0 ${PW} usermod foo -l bar
142	atf_check -s exit:0 -o match:"^bar:.*" \
143		grep "^bar:.*" ${HOME}/master.passwd
144}
145
146atf_test_case user_mod_rename_too_long
147user_mod_rename_too_long_body() {
148	populate_etc_skel
149
150	atf_check -s exit:0 ${PW} useradd foo
151	atf_check -s exit:64 -e match:"too long" ${PW} usermod foo \
152		-l name_very_very_very_very_very_long
153}
154
155atf_test_case user_mod_h
156user_mod_h_body() {
157	populate_etc_skel
158
159	atf_check -s exit:0 ${PW} useradd foo
160	atf_check -s exit:0 ${PW} usermod foo -h 0 <<- EOF
161	$(echo a)
162	EOF
163	passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
164	atf_check -s exit:0 -o inline:$passhash \
165		$(atf_get_srcdir)/crypt $passhash "a"
166	atf_check -s exit:0 ${PW} usermod foo -h - <<- EOF
167	$(echo b)
168	EOF
169	atf_check -s exit:0 -o match:"^foo:\*:.*" \
170		grep "^foo" ${HOME}/master.passwd
171	atf_check -e inline:"pw: Bad file descriptor 'a': invalid\n" \
172		-s exit:64 ${PW} usermod foo -h a <<- EOF
173	$(echo a)
174	EOF
175}
176
177atf_test_case user_mod_H
178user_mod_H_body() {
179	populate_etc_skel
180
181	atf_check -s exit:0 ${PW} useradd foo
182	atf_check -s exit:0 ${PW} usermod foo -H 0 <<- EOF
183	$(echo a)
184	EOF
185	atf_check -s exit:0 -o match:"^foo:a:.*" \
186		grep "^foo" ${HOME}/master.passwd
187	atf_check -s exit:64 -e inline:"pw: -H expects a file descriptor\n" \
188		${PW} usermod foo -H -
189}
190
191atf_test_case user_mod_renamehome
192user_mod_renamehome_body() {
193	populate_root_etc_skel
194
195	mkdir -p ${HOME}/home
196	atf_check -s exit:0 ${RPW} useradd foo -m
197	test -d ${HOME}/home/foo || atf_fail "Directory not created"
198	atf_check -s exit:0 ${RPW} usermod foo -l bar -d /home/bar -m
199	test -d ${HOME}/home/bar || atf_fail "Directory not created"
200}
201
202atf_test_case user_mod_uid
203user_mod_uid_body() {
204	populate_etc_skel
205
206	atf_check -s exit:0 ${PW} useradd foo
207	atf_check -s exit:0 ${PW} usermod foo -u 5000
208}
209
210atf_test_case user_mod_w_error
211user_mod_w_error_body() {
212	populate_etc_skel
213
214	atf_check -s exit:0 ${PW} useradd foo
215	atf_check -s exit:1 -e match:"pw: Invalid value for default password" \
216		${PW} usermod foo -w invalid_value
217}
218
219atf_test_case user_mod_w_no
220user_mod_w_no_body() {
221	populate_etc_skel
222
223	atf_check -s exit:0 ${PW} useradd foo
224	atf_check -s exit:0 ${PW} usermod foo -w no
225	atf_check -s exit:0 -o match:"^foo:\*" grep "^foo:" $HOME/master.passwd
226}
227
228atf_test_case user_mod_w_none
229user_mod_w_none_body() {
230	populate_etc_skel
231
232	atf_check -s exit:0 ${PW} useradd foo
233	atf_check -s exit:0 ${PW} usermod foo -w none
234	atf_check -s exit:0 -o match:"^foo::" grep "^foo:" $HOME/master.passwd
235}
236
237atf_test_case user_mod_w_random
238user_mod_w_random_body() {
239	populate_etc_skel
240
241	atf_check -s exit:0 ${PW} useradd foo
242	password=`${PW} usermod foo -w random | cat`
243	passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
244	atf_check -s exit:0 -o inline:$passhash \
245		$(atf_get_srcdir)/crypt $passhash "$password"
246}
247
248atf_test_case user_mod_w_yes
249user_mod_w_yes_body() {
250	populate_etc_skel
251
252	atf_check -s exit:0 ${PW} useradd foo
253	atf_check -s exit:0 ${PW} usermod foo -w yes
254	passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
255	atf_check -s exit:0 -o inline:$passhash \
256		$(atf_get_srcdir)/crypt $passhash "foo"
257}
258
259atf_test_case user_mod_m
260user_mod_m_body() {
261	populate_root_etc_skel
262
263	mkdir -p ${HOME}/home
264	mkdir -p ${HOME}/skel
265	echo "entry" > ${HOME}/skel/.file
266	atf_check -s exit:0 ${RPW} useradd foo
267	! test -d ${HOME}/home/foo || atf_fail "Directory should not have been created"
268	atf_check -s exit:0 ${RPW} usermod foo -m -k /skel
269	test -d ${HOME}/home/foo || atf_fail "Directory should have been created"
270	test -f ${HOME}/home/foo/.file || atf_fail "Skell files not added"
271	echo "entry" > ${HOME}/skel/.file2
272	atf_check -s exit:0 ${RPW} usermod foo -m -k /skel
273	test -f ${HOME}/home/foo/.file2 || atf_fail "Skell files not added"
274	echo > ${HOME}/home/foo/.file2
275	atf_check -s exit:0 ${RPW} usermod foo -m -k /skel
276	atf_check -s exit:0 -o inline:"\n" cat ${HOME}/home/foo/.file2
277}
278
279
280atf_init_test_cases() {
281	atf_add_test_case user_mod
282	atf_add_test_case user_mod_noupdate
283	atf_add_test_case user_mod_comments
284	atf_add_test_case user_mod_comments_noupdate
285	atf_add_test_case user_mod_comments_invalid
286	atf_add_test_case user_mod_comments_invalid_noupdate
287	atf_add_test_case user_mod_nogroups
288	atf_add_test_case user_mod_rename
289	atf_add_test_case user_mod_name_noupdate
290	atf_add_test_case user_mod_rename_too_long
291	atf_add_test_case user_mod_rename_multigroups
292	atf_add_test_case user_mod_h
293	atf_add_test_case user_mod_H
294	atf_add_test_case user_mod_renamehome
295	atf_add_test_case user_mod_uid
296	atf_add_test_case user_mod_w_error
297	atf_add_test_case user_mod_w_no
298	atf_add_test_case user_mod_w_none
299	atf_add_test_case user_mod_w_random
300	atf_add_test_case user_mod_w_yes
301	atf_add_test_case user_mod_m
302}
303