1# $FreeBSD: releng/10.2/usr.sbin/pw/tests/pw_useradd.sh 285092 2015-07-03 14:22:44Z bapt $
2
3# Import helper functions
4. $(atf_get_srcdir)/helper_functions.shin
5
6# Test add user
7atf_test_case user_add
8user_add_body() {
9	populate_etc_skel
10
11	atf_check -s exit:0 ${PW} useradd test
12	atf_check -s exit:0 -o match:"^test:.*" \
13		grep "^test:.*" $HOME/master.passwd
14}
15
16# Test add user with option -N
17atf_test_case user_add_noupdate
18user_add_noupdate_body() {
19	populate_etc_skel
20
21	atf_check -s exit:0 -o match:"^test:.*" ${PW} useradd test -N
22	atf_check -s exit:1 -o empty grep "^test:.*" $HOME/master.passwd
23}
24
25# Test add user with comments
26atf_test_case user_add_comments
27user_add_comments_body() {
28	populate_etc_skel
29
30	atf_check -s exit:0 ${PW} useradd test -c "Test User,work,123,456"
31	atf_check -s exit:0 -o match:"^test:.*:Test User,work,123,456:" \
32		grep "^test:.*:Test User,work,123,456:" $HOME/master.passwd
33}
34
35# Test add user with comments and option -N
36atf_test_case user_add_comments_noupdate
37user_add_comments_noupdate_body() {
38	populate_etc_skel
39
40	atf_check -s exit:0 -o match:"^test:.*:Test User,work,123,456:" \
41		${PW} useradd test -c "Test User,work,123,456" -N
42	atf_check -s exit:1 -o empty grep "^test:.*" $HOME/master.passwd
43}
44
45# Test add user with invalid comments
46atf_test_case user_add_comments_invalid
47user_add_comments_invalid_body() {
48	populate_etc_skel
49
50	atf_check -s exit:65 -e match:"invalid character" \
51		${PW} useradd test -c "Test User,work,123:456,456"
52	atf_check -s exit:1 -o empty \
53		grep "^test:.*:Test User,work,123:456,456:" $HOME/master.passwd
54}
55
56# Test add user with invalid comments and option -N
57atf_test_case user_add_comments_invalid_noupdate
58user_add_comments_invalid_noupdate_body() {
59	populate_etc_skel
60
61	atf_check -s exit:65 -e match:"invalid character" \
62		${PW} useradd test -c "Test User,work,123:456,456" -N
63	atf_check -s exit:1 -o empty grep "^test:.*" $HOME/master.passwd
64}
65
66# Test add user with alternate homedir
67atf_test_case user_add_homedir
68user_add_homedir_body() {
69	populate_etc_skel
70
71	atf_check -s exit:0 ${PW} useradd test -d /foo/bar
72	atf_check -s exit:0 -o match:"^test:\*:.*::0:0:User &:/foo/bar:.*" \
73		${PW} usershow test
74}
75
76# Test add user with account expiration as an epoch date
77atf_test_case user_add_account_expiration_epoch
78user_add_account_expiration_epoch_body() {
79	populate_etc_skel
80
81	DATE=`date -j -v+1d "+%s"`
82	atf_check -s exit:0 ${PW} useradd test -e ${DATE}
83	atf_check -s exit:0 -o match:"^test:\*:.*::0:${DATE}:.*" \
84		${PW} usershow test
85}
86
87# Test add user with account expiration as a DD-MM-YYYY date
88atf_test_case user_add_account_expiration_date_numeric
89user_add_account_expiration_date_numeric_body() {
90	populate_etc_skel
91
92	DATE=`date -j -v+1d "+%d-%m-%Y"`
93	EPOCH=`date -j -f "%d-%m-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
94	atf_check -s exit:0 ${PW} useradd test -e ${DATE}
95	atf_check -s exit:0 -o match:"^test:\*:.*::0:${EPOCH}:User &:.*" \
96		${PW} usershow test
97}
98
99# Test add user with account expiration as a DD-MM-YYYY date
100atf_test_case user_add_account_expiration_date_month
101user_add_account_expiration_date_month_body() {
102	populate_etc_skel
103
104	DATE=`date -j -v+1d "+%d-%b-%Y"`
105	EPOCH=`date -j -f "%d-%b-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
106	atf_check -s exit:0 ${PW} useradd test -e ${DATE}
107	atf_check -s exit:0 -o match:"^test:\*:.*::0:${EPOCH}:User &:.*" \
108		${PW} usershow test
109}
110
111# Test add user with account expiration as a relative date
112atf_test_case user_add_account_expiration_date_relative
113user_add_account_expiration_date_relative_body() {
114	populate_etc_skel
115
116	EPOCH=`date -j -v+13m "+%s"`
117	BUF=`expr $EPOCH + 5`
118	atf_check -s exit:0 ${PW} useradd test -e +13o
119	TIME=`${PW} usershow test | awk -F ':' '{print $7}'`
120	[ ! -z $TIME -a $TIME -ge $EPOCH -a $TIME -lt $BUF ] || \
121		atf_fail "Expiration time($TIME) was not within $EPOCH - $BUF seconds."
122}
123
124# Test add user with password expiration as an epoch date
125atf_test_case user_add_password_expiration_epoch
126user_add_password_expiration_epoch_body() {
127	populate_etc_skel
128
129	DATE=`date -j -v+1d "+%s"`
130	atf_check -s exit:0 ${PW} useradd test -p ${DATE}
131	atf_check -s exit:0 -o match:"^test:\*:.*::${DATE}:0:.*" \
132		${PW} usershow test
133}
134
135# Test add user with password expiration as a DD-MM-YYYY date
136atf_test_case user_add_password_expiration_date_numeric
137user_add_password_expiration_date_numeric_body() {
138	populate_etc_skel
139
140	DATE=`date -j -v+1d "+%d-%m-%Y"`
141	EPOCH=`date -j -f "%d-%m-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
142	atf_check -s exit:0 ${PW} useradd test -p ${DATE}
143	atf_check -s exit:0 -o match:"^test:\*:.*::${EPOCH}:0:User &:.*" \
144		${PW} usershow test
145}
146
147# Test add user with password expiration as a DD-MMM-YYYY date
148atf_test_case user_add_password_expiration_date_month
149user_add_password_expiration_date_month_body() {
150	populate_etc_skel
151
152	DATE=`date -j -v+1d "+%d-%b-%Y"`
153	EPOCH=`date -j -f "%d-%b-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
154	atf_check -s exit:0 ${PW} useradd test -p ${DATE}
155	atf_check -s exit:0 -o match:"^test:\*:.*::${EPOCH}:0:User &:.*" \
156		${PW} usershow test
157}
158
159# Test add user with password expiration as a relative date
160atf_test_case user_add_password_expiration_date_relative
161user_add_password_expiration_date_relative_body() {
162	populate_etc_skel
163
164	EPOCH=`date -j -v+13m "+%s"`
165	BUF=`expr $EPOCH + 5`
166	atf_check -s exit:0 ${PW} useradd test -p +13o
167	TIME=`${PW} usershow test | awk -F ':' '{print $6}'`
168	[ ! -z $TIME -a $TIME -ge $EPOCH -a $TIME -lt $BUF ] || \
169		atf_fail "Expiration time($TIME) was not within $EPOCH - $BUF seconds."
170}
171
172atf_test_case user_add_name_too_long
173user_add_name_too_long_body() {
174	populate_etc_skel
175	atf_check -e match:"too long" -s exit:64 \
176		${PW} useradd name_very_vert_very_very_very_long
177}
178
179atf_init_test_cases() {
180	atf_add_test_case user_add
181	atf_add_test_case user_add_noupdate
182	atf_add_test_case user_add_comments
183	atf_add_test_case user_add_comments_noupdate
184	atf_add_test_case user_add_comments_invalid
185	atf_add_test_case user_add_comments_invalid_noupdate
186	atf_add_test_case user_add_homedir
187	atf_add_test_case user_add_account_expiration_epoch
188	atf_add_test_case user_add_account_expiration_date_numeric
189	atf_add_test_case user_add_account_expiration_date_month
190	atf_add_test_case user_add_account_expiration_date_relative
191	atf_add_test_case user_add_password_expiration_epoch
192	atf_add_test_case user_add_password_expiration_date_numeric
193	atf_add_test_case user_add_password_expiration_date_month
194	atf_add_test_case user_add_password_expiration_date_relative
195	atf_add_test_case user_add_name_too_long
196}
197