summaryrefslogtreecommitdiffstats
path: root/pw/tests/pw_usermod.sh
blob: 236fd27661ebcf3b388ff165a03e85481b77d1cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# $FreeBSD$

# Import helper functions
. $(atf_get_srcdir)/helper_functions.shin

# Test modifying a user
atf_test_case user_mod
user_mod_body() {
	populate_etc_skel

	atf_check -s exit:67 -e match:"no such user" ${PW} usermod test
	atf_check -s exit:0 ${PW} useradd test
	atf_check -s exit:0 ${PW} usermod test
	atf_check -s exit:0 -o match:"^test:.*" \
		grep "^test:.*" $HOME/master.passwd
}

# Test modifying a user with option -N
atf_test_case user_mod_noupdate
user_mod_noupdate_body() {
	populate_etc_skel

	atf_check -s exit:67 -e match:"no such user" ${PW} usermod test -N
	atf_check -s exit:0 ${PW} useradd test
	atf_check -s exit:0 -o match:"^test:.*" ${PW} usermod test -N
	atf_check -s exit:0 -o match:"^test:.*" \
		grep "^test:.*" $HOME/master.passwd
}

# Test modifying a user with comments
atf_test_case user_mod_comments
user_mod_comments_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} useradd test -c "Test User,home,123,456"
	atf_check -s exit:0 ${PW} usermod test -c "Test User,work,123,456"
	atf_check -s exit:0 -o match:"^test:.*:Test User,work,123,456:" \
		grep "^test:.*:Test User,work,123,456:" $HOME/master.passwd
}

# Test modifying a user with comments with option -N
atf_test_case user_mod_comments_noupdate
user_mod_comments_noupdate_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} useradd test -c "Test User,home,123,456"
	atf_check -s exit:0 -o match:"^test:.*:Test User,work,123,456:" \
		${PW} usermod test -c "Test User,work,123,456" -N
	atf_check -s exit:0 -o match:"^test:.*:Test User,home,123,456:" \
		grep "^test:.*:Test User,home,123,456:" $HOME/master.passwd
}

# Test modifying a user with invalid comments
atf_test_case user_mod_comments_invalid
user_mod_comments_invalid_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} useradd test
	atf_check -s exit:65 -e match:"invalid character" \
		${PW} usermod test -c "Test User,work,123:456,456"
	atf_check -s exit:1 -o empty \
		grep "^test:.*:Test User,work,123:456,456:" $HOME/master.passwd
	atf_check -s exit:0 -o match:"^test:\*" \
		grep "^test:\*" $HOME/master.passwd
}

# Test modifying a user with invalid comments with option -N
atf_test_case user_mod_comments_invalid_noupdate
user_mod_comments_invalid_noupdate_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} useradd test
	atf_check -s exit:65 -e match:"invalid character" \
		${PW} usermod test -c "Test User,work,123:456,456" -N
	atf_check -s exit:1 -o empty \
		grep "^test:.*:Test User,work,123:456,456:" $HOME/master.passwd
	atf_check -s exit:0 -o match:"^test:\*" \
		grep "^test:\*" $HOME/master.passwd
}

# Test modifying a user name with -l
atf_test_case user_mod_name
user_mod_name_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} useradd foo
	atf_check -s exit:0 ${PW} usermod foo -l "bar"
	atf_check -s exit:0 -o match:"^bar:.*" \
		grep "^bar:.*" $HOME/master.passwd
}

# Test modifying a user name with -l with option -N
atf_test_case user_mod_name_noupdate
user_mod_name_noupdate_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} useradd foo
	atf_check -s exit:0 -o match:"^bar:.*" ${PW} usermod foo -l "bar" -N
	atf_check -s exit:0 -o match:"^foo:.*" \
		grep "^foo:.*" $HOME/master.passwd
}

atf_test_case user_mod_rename_multigroups
user_mod_rename_multigroups_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} groupadd test1
	atf_check -s exit:0 ${PW} groupadd test2
	atf_check -s exit:0 ${PW} useradd foo -G test1,test2
	atf_check -o match:"foo" -s exit:0 ${PW} groupshow test1
	atf_check -o match:"foo" -s exit:0 ${PW} groupshow test2
	atf_check -s exit:0 ${PW} usermod foo -l bar
	atf_check -o match:"bar" -s exit:0 ${PW} groupshow test1
	atf_check -o match:"bar" -s exit:0 ${PW} groupshow test2
}

atf_test_case user_mod_nogroups
user_mod_nogroups_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} groupadd test1
	atf_check -s exit:0 ${PW} groupadd test2
	atf_check -s exit:0 ${PW} groupadd test3
	atf_check -s exit:0 ${PW} groupadd test4
	atf_check -s exit:0 ${PW} useradd foo -G test1,test2
	atf_check -o match:"foo" -s exit:0 ${PW} groupshow test1
	atf_check -o match:"foo" -s exit:0 ${PW} groupshow test2
	atf_check -s exit:0 ${PW} usermod foo -G test3,test4
	atf_check -s exit:0 -o inline:"test3\ntest4\n" \
		awk -F\: '$4 == "foo" { print $1 }' ${HOME}/group
}

atf_test_case user_mod_rename
user_mod_rename_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} useradd foo
	atf_check -s exit:0 ${PW} usermod foo -l bar
	atf_check -s exit:0 -o match:"^bar:.*" \
		grep "^bar:.*" ${HOME}/master.passwd
}

atf_test_case user_mod_rename_too_long
user_mod_rename_too_long_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} useradd foo
	atf_check -s exit:64 -e match:"too long" ${PW} usermod foo \
		-l name_very_very_very_very_very_long
}

atf_test_case user_mod_h
user_mod_h_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} useradd foo
	atf_check -s exit:0 ${PW} usermod foo -h 0 <<- EOF
	$(echo a)
	EOF
	atf_check -s exit:0 -o not-match:"^foo:\*:.*" \
		grep "^foo" ${HOME}/master.passwd
	atf_check -s exit:0 ${PW} usermod foo -h - <<- EOF
	$(echo b)
	EOF
	atf_check -s exit:0 -o match:"^foo:\*:.*" \
		grep "^foo" ${HOME}/master.passwd
	atf_check -e inline:"pw: Bad file descriptor 'a': invalid\n" \
		-s exit:64 ${PW} usermod foo -h a <<- EOF
	$(echo a)
	EOF
}

atf_test_case user_mod_H
user_mod_H_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} useradd foo
	atf_check -s exit:0 ${PW} usermod foo -H 0 <<- EOF
	$(echo a)
	EOF
	atf_check -s exit:0 -o match:"^foo:a:.*" \
		grep "^foo" ${HOME}/master.passwd
	atf_check -s exit:64 -e inline:"pw: -H expects a file descriptor\n" \
		${PW} usermod foo -H -
}

atf_test_case user_mod_renamehome
user_mod_renamehome_body() {
	populate_root_etc_skel

	mkdir -p ${HOME}/home
	atf_check -s exit:0 ${RPW} useradd foo -m
	test -d ${HOME}/home/foo || atf_fail "Directory not created"
	atf_check -s exit:0 ${RPW} usermod foo -l bar -d /home/bar -m
	test -d ${HOME}/home/bar || atf_fail "Directory not created"
}

atf_test_case user_mod_uid
user_mod_uid_body() {
	populate_etc_skel

	atf_check -s exit:0 ${PW} useradd foo
	atf_check -s exit:0 ${PW} usermod foo -u 5000
}

atf_init_test_cases() {
	atf_add_test_case user_mod
	atf_add_test_case user_mod_noupdate
	atf_add_test_case user_mod_comments
	atf_add_test_case user_mod_comments_noupdate
	atf_add_test_case user_mod_comments_invalid
	atf_add_test_case user_mod_comments_invalid_noupdate
	atf_add_test_case user_mod_nogroups
	atf_add_test_case user_mod_rename
	atf_add_test_case user_mod_name_noupdate
	atf_add_test_case user_mod_rename_too_long
	atf_add_test_case user_mod_rename_multigroups
	atf_add_test_case user_mod_h
	atf_add_test_case user_mod_H
	atf_add_test_case user_mod_renamehome
	atf_add_test_case user_mod_uid
}