summaryrefslogtreecommitdiffstats
path: root/commands/account/test/test_user.sh
blob: fbfeddd648492164dfb2cbe2273563c0b54efe2b (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/bin/bash
#
# Copyright (C) 2014 Red Hat, Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of the FreeBSD Project.
#
# Authors: Jan Safranek <jsafrane@redhat.com>

. ./base.sh

USER=$LMI_ACCOUNT_USER
USER_GID=1 # 'bin' group
USER_UID=9998

RESERVED_THRESHOLD=1000

rlJournalStart

rlPhaseStartSetup
rlPhaseEnd


rlPhaseStartTest "Test creation of non-reserved user"
    rlLogInfo "Create non-reserved user"
    rlRun "$LMI user create $USER"

    rlLogInfo "Check that '/etc/passwd' shows it"
    rlAssertGrep "^$USER:" /etc/passwd

    rlLogInfo "Check that '/etc/group' shows it"
    rlAssertGrep "^$USER:" /etc/group

    rlLogInfo "Check that 'lmi user list' shows it"
    rlRun -s "$LMI -L csv -N user list"
    rlAssertGrep "^\"$USER\"" $rlRun_LOG
    rm $rlRun_LOG

    rlLogInfo "Check that 'lmi user show' shows it"
    rlRun -s "$LMI -L csv -N user show"
    rlAssertGrep "^\"$USER\"" $rlRun_LOG
    rm $rlRun_LOG

    rlLogInfo "Check that 'lmi user show $USER' shows it"
    rlRun -s "$LMI -L csv -N user show $USER"
    rlAssertGrep "^\"$USER\"" $rlRun_LOG
    rm $rlRun_LOG

    rlLogInfo "Check that 'lmi user show root' does not show it"
    rlRun -s "$LMI -L csv -N user show root"
    rlAssertNotGrep "^\"$USER\"" $rlRun_LOG
    rm $rlRun_LOG

    rlLogInfo "Check that home exists"
    rlAssertExists /home/$USER

    rlLogInfo "Check that GID is non-reserved"
    gid=`grep </etc/group "^$USER:" | cut -f 3 -d ':'`
    rlAssertGreaterOrEqual "group is non-reserved" "$gid" $RESERVED_THRESHOLD

    rlLogInfo "Check that UID is non-reserved"
    uid=`grep </etc/passwd "^$USER:" | cut -f 3 -d ':'`
    rlAssertGreaterOrEqual "userid non-reserved" "$uid" $RESERVED_THRESHOLD

    rlLogInfo "Remove the user"
    rlRun "$LMI user delete $USER"

    rlLogInfo "Check that 'lmi user show $USER' does not show it"
    rlRun -s "$LMI -L csv -N user show $USER" 1
    rlAssertNotGrep "\"$USER\"" $rlRun_LOG
    rm $rlRun_LOG

    rlLogInfo "Check that '/etc/passwd' does not show it"
    rlAssertNotGrep "^$GROUP:" /etc/passwd
    rlLogInfo "Check that '/etc/group' does not show it"
    rlAssertNotGrep "^$GROUP:" /etc/group
rlPhaseEnd

rlPhaseStartTest "Test creation of reserved user"
    rlLogInfo "Create reserved user"
    rlRun "$LMI user create -r $USER"

    rlLogInfo "Check that GID is non-reserved"
    gid=`grep </etc/group "^$USER:" | cut -f 3 -d ':'`
    rlAssertGreaterOrEqual "group is non-reserved" "$gid" $RESERVED_THRESHOLD

    rlLogInfo "Check that UID is reserved"
    uid=`grep </etc/passwd "^$USER:" | cut -f 3 -d ':'`
    rlAssertGreater "userid non-reserved" $RESERVED_THRESHOLD "$uid"

    rlLogInfo "Remove the user"
    rlRun "$LMI user delete $USER"
rlPhaseEnd

rlPhaseStartTest "Test creation of user with specific UID"
    rlLogInfo "Create user with UID"
    rlRun "$LMI user create --uid=$USER_UID $USER"

    rlLogInfo "Check that GID is non-reserved"
    gid=`grep </etc/group "^$USER:" | cut -f 3 -d ':'`
    rlAssertGreaterOrEqual "group is non-reserved" "$gid" $RESERVED_THRESHOLD

    rlLogInfo "Check that UID is $USER_UID"
    uid=`grep </etc/passwd "^$USER:" | cut -f 3 -d ':'`
    rlAssertEquals "userid" $USER_UID "$uid"

    rlLogInfo "Remove the user"
    rlRun "$LMI user delete $USER"
rlPhaseEnd

rlPhaseStartTest "Test creation of user with specific GID"
    rlLogInfo "Create user with GID"
    rlRun "$LMI user create --gid=$USER_GID $USER"

    rlLogInfo "Check that GID is non-reserved"
    gid=`grep </etc/passwd "^$USER:" | cut -f 4 -d ':'`
    rlAssertEquals "group" "$gid" $USER_GID

    rlLogInfo "Check that UID is non-reserved"
    uid=`grep </etc/passwd "^$USER:" | cut -f 3 -d ':'`
    rlAssertGreaterOrEqual "userid non-reserved" "$uid" $RESERVED_THRESHOLD

    rlLogInfo "Remove the user"
    rlRun "$LMI user delete $USER"
rlPhaseEnd

rlPhaseStartTest "Test creation of user with specific UID and GID"
    rlLogInfo "Create user with UID and GID"
    rlRun "$LMI user create --gid=$USER_GID --uid=$USER_UID $USER"

    rlLogInfo "Check that GID is non-reserved"
    gid=`grep </etc/passwd "^$USER:" | cut -f 4 -d ':'`
    rlAssertEquals "group" "$gid" $USER_GID

    rlLogInfo "Check that UID is $USER_UID"
    uid=`grep </etc/passwd "^$USER:" | cut -f 3 -d ':'`
    rlAssertEquals "userid" $USER_UID "$uid"

    rlLogInfo "Remove the user"
    rlRun "$LMI user delete $USER"
rlPhaseEnd

rlPhaseStartTest "Test creation of user with --directory, --gecos and --shell args"
    rlLogInfo "Create user with --dir, --gecos and --shell args"
    rlRun "$LMI user create -d /home/blabla -s /bin/bleble -c mygecos $USER"

    rlLogInfo "Check home"
    home=`grep </etc/passwd "^$USER:" | cut -f 6 -d ':'`
    rlAssertEquals "home" "$home" "/home/blabla"

    rlLogInfo "Check shell"
    shell=`grep </etc/passwd "^$USER:" | cut -f 7 -d ':'`
    rlAssertEquals "shell" "$shell" "/bin/bleble"

    rlLogInfo "Check gecos"
    shell=`grep </etc/passwd "^$USER:" | cut -f 5 -d ':'`
    rlAssertEquals "shell" "$shell" "mygecos"

    rlLogInfo "Remove the user"
    rlRun "$LMI user delete $USER"
rlPhaseEnd

rlPhaseStartTest "Test creation of user without home and group"
    rlLogInfo "Create user without home and group"
    rlRun "$LMI user create -M -n $USER"

    rlLogInfo "Check home"
    home=`grep </etc/passwd "^$USER:" | cut -f 6 -d ':'`
    rlAssertEquals "home" "$home" "/home/$USER"

    rlLogInfo "Check that home does not exist"
    rlAssertNotExists /home/$USER

    rlLogInfo "Check the group does not exist"
    rlAssertNotGrep "^$USER" /etc/group

    rlLogInfo "Remove the user"
    rlRun "$LMI user delete $USER"
rlPhaseEnd

rlPhaseStartTest "Test deletion of user without home and group"
    rlLogInfo "Create user"
    rlRun "$LMI user create $USER"

    rlLogInfo "Check home"
    home=`grep </etc/passwd "^$USER:" | cut -f 6 -d ':'`
    rlAssertEquals "home" "$home" "/home/$USER"

    rlLogInfo "Check that home exists"
    rlAssertExists /home/$USER

    rlLogInfo "Check the group exists"
    rlAssertGrep "^$USER" /etc/group

    rlLogInfo "Remove the user"
    rlRun "$LMI user delete --no-delete-home --no-delete-group $USER"

    rlLogInfo "Check that home still exists"
    rlAssertExists /home/$USER

    rlLogInfo "Check the group still exists"
    rlAssertGrep "^$USER" /etc/group

    rm -rf /home/$USER
    groupdel $USER
rlPhaseEnd

rlPhaseStartCleanup
    # remove any stray user
    userdel $USER
rlPhaseEnd

rlJournalPrintText
rlJournalEnd