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