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
|
/*
* Copyright 2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mockftpserver.fake
import org.mockftpserver.fake.CustomUserAccount
import org.mockftpserver.fake.UserAccount
import org.mockftpserver.fake.filesystem.FileEntry
import org.mockftpserver.fake.filesystem.FileSystemEntry
import org.mockftpserver.fake.filesystem.Permissions
import org.mockftpserver.test.AbstractGroovyTest
/**
* Tests for UserAccount
*
* @version $Revision$ - $Date$
*
* @author Chris Mair
*/
class UserAccountTest extends AbstractGroovyTest {
private static final USERNAME = "user123"
private static final PASSWORD = "password123"
private static final HOME_DIR = "/usr/user123"
private static final GROUP = 'group'
private UserAccount userAccount
void testConstructor() {
def acct = new UserAccount(USERNAME, PASSWORD, HOME_DIR)
assert acct.username == USERNAME
assert acct.password == PASSWORD
assert acct.homeDirectory == HOME_DIR
}
void testGetPrimaryGroup() {
assert userAccount.primaryGroup == UserAccount.DEFAULT_GROUP
userAccount.groups = ['abc']
assert userAccount.primaryGroup == 'abc'
userAccount.groups.add('def')
assert userAccount.primaryGroup == 'abc'
userAccount.groups = []
assert userAccount.primaryGroup == UserAccount.DEFAULT_GROUP
}
void testIsValidPassword() {
userAccount.username = USERNAME
userAccount.password = PASSWORD
assert userAccount.isValidPassword(PASSWORD)
assert !userAccount.isValidPassword("")
assert !userAccount.isValidPassword("wrong")
assert !userAccount.isValidPassword(null)
}
void testIsValidPassword_UsernameNullOrEmpty() {
userAccount.password = PASSWORD
shouldFailWithMessageContaining('username') { userAccount.isValidPassword(PASSWORD) }
userAccount.username = ''
shouldFailWithMessageContaining('username') { userAccount.isValidPassword(PASSWORD) }
}
void testIsValidPassword_OverrideComparePassword() {
def customUserAccount = new CustomUserAccount()
customUserAccount.username = USERNAME
customUserAccount.password = PASSWORD
println customUserAccount
assert customUserAccount.isValidPassword(PASSWORD) == false
assert customUserAccount.isValidPassword(PASSWORD + "123")
}
void testIsValidPassword_PasswordNotCheckedDuringValidation() {
userAccount.username = USERNAME
userAccount.password = PASSWORD
userAccount.passwordCheckedDuringValidation = false
assert userAccount.isValidPassword("wrong")
}
void testIsValid() {
assert !userAccount.valid
userAccount.homeDirectory = ""
assert !userAccount.valid
userAccount.homeDirectory = "/abc"
assert userAccount.valid
}
void testCanRead() {
// No file permissions - readable by all
testCanRead(USERNAME, GROUP, null, true)
// UserAccount has no username or group; use World permissions
testCanRead(USERNAME, GROUP, '------r--', true)
testCanRead(USERNAME, GROUP, 'rwxrwx-wx', false)
userAccount.username = USERNAME
userAccount.groups = [GROUP]
testCanRead(USERNAME, GROUP, 'rwxrwxrwx', true) // ALL
testCanRead(USERNAME, GROUP, '---------', false) // NONE
testCanRead(USERNAME, null, 'r--------', true) // User
testCanRead(USERNAME, null, '-wxrwxrwx', false)
testCanRead(null, GROUP, '---r-----', true) // Group
testCanRead(null, GROUP, 'rwx-wxrwx', false)
testCanRead(null, null, '------r--', true) // World
testCanRead(null, null, 'rwxrwx-wx', false)
}
void testCanWrite() {
// No file permissions - writable by all
testCanWrite(USERNAME, GROUP, null, true)
// UserAccount has no username or group; use World permissions
testCanWrite(USERNAME, GROUP, '-------w-', true)
testCanWrite(USERNAME, GROUP, 'rwxrwxr-x', false)
userAccount.username = USERNAME
userAccount.groups = [GROUP]
testCanWrite(USERNAME, GROUP, 'rwxrwxrwx', true) // ALL
testCanWrite(USERNAME, GROUP, '---------', false) // NONE
testCanWrite(USERNAME, null, '-w-------', true) // User
testCanWrite(USERNAME, null, 'r-xrwxrwx', false)
testCanWrite(null, GROUP, '----w----', true) // Group
testCanWrite(null, GROUP, 'rwxr-xrwx', false)
testCanWrite(null, null, '-------w-', true) // World
testCanWrite(null, null, 'rwxrwxr-x', false)
}
void testCanExecute() {
// No file permissions - executable by all
testCanExecute(USERNAME, GROUP, null, true)
// UserAccount has no username or group; use World permissions
testCanExecute(USERNAME, GROUP, '--------x', true)
testCanExecute(USERNAME, GROUP, 'rwxrwxrw-', false)
userAccount.username = USERNAME
userAccount.groups = [GROUP]
testCanExecute(USERNAME, GROUP, 'rwxrwxrwx', true) // ALL
testCanExecute(USERNAME, GROUP, '---------', false) // NONE
testCanExecute(USERNAME, null, '--x------', true) // User
testCanExecute(USERNAME, null, 'rw-rwxrwx', false)
testCanExecute(null, GROUP, '-----x---', true) // Group
testCanExecute(null, GROUP, 'rwxrw-rwx', false)
testCanExecute(null, null, '--------x', true) // World
testCanExecute(null, null, 'rwxrwxrw-', false)
}
void testDefaultPermissions() {
assert userAccount.defaultPermissionsForNewFile == new Permissions('rw-rw-rw-')
assert userAccount.defaultPermissionsForNewDirectory == Permissions.ALL
}
//--------------------------------------------------------------------------
// Helper Methods
//--------------------------------------------------------------------------
private void testCanRead(owner, group, permissionsString, expectedResult) {
def file = createFileEntry(owner, permissionsString, group)
assert userAccount.canRead(file) == expectedResult, file
}
private void testCanWrite(owner, group, permissionsString, expectedResult) {
def file = createFileEntry(owner, permissionsString, group)
assert userAccount.canWrite(file) == expectedResult, file
}
private void testCanExecute(owner, group, permissionsString, expectedResult) {
def file = createFileEntry(owner, permissionsString, group)
assert userAccount.canExecute(file) == expectedResult, file
}
private FileSystemEntry createFileEntry(owner, permissionsString, group) {
def permissions = permissionsString ? new Permissions(permissionsString) : null
return new FileEntry(path: '', owner: owner, group: group, permissions: permissions)
}
void setUp() {
super.setUp()
userAccount = new UserAccount()
}
}
|