UserAccountTest.groovy revision ad39334d4c363c6ada5863d0bb3184f5f4699d69
1/*
2 * Copyright 2008 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.fake
17
18import org.mockftpserver.fake.CustomUserAccount
19import org.mockftpserver.fake.UserAccount
20import org.mockftpserver.fake.filesystem.FileEntry
21import org.mockftpserver.fake.filesystem.FileSystemEntry
22import org.mockftpserver.fake.filesystem.Permissions
23import org.mockftpserver.test.AbstractGroovyTest
24
25
26
27/**
28 * Tests for UserAccount
29 *
30 * @version $Revision$ - $Date$
31 *
32 * @author Chris Mair
33 */
34class UserAccountTest extends AbstractGroovyTest {
35
36    private static final USERNAME = "user123"
37    private static final PASSWORD = "password123"
38    private static final HOME_DIR = "/usr/user123"
39    private static final GROUP = 'group'
40
41    private UserAccount userAccount
42
43    void testConstructor() {
44        def acct = new UserAccount(USERNAME, PASSWORD, HOME_DIR)
45        assert acct.username == USERNAME
46        assert acct.password == PASSWORD
47        assert acct.homeDirectory == HOME_DIR
48    }
49
50    void testGetPrimaryGroup() {
51        assert userAccount.primaryGroup == UserAccount.DEFAULT_GROUP
52
53        userAccount.groups = ['abc']
54        assert userAccount.primaryGroup == 'abc'
55
56        userAccount.groups.add('def')
57        assert userAccount.primaryGroup == 'abc'
58
59        userAccount.groups = []
60        assert userAccount.primaryGroup == UserAccount.DEFAULT_GROUP
61    }
62
63    void testIsValidPassword() {
64        userAccount.username = USERNAME
65        userAccount.password = PASSWORD
66        assert userAccount.isValidPassword(PASSWORD)
67
68        assert !userAccount.isValidPassword("")
69        assert !userAccount.isValidPassword("wrong")
70        assert !userAccount.isValidPassword(null)
71    }
72
73    void testIsValidPassword_UsernameNullOrEmpty() {
74        userAccount.password = PASSWORD
75        shouldFailWithMessageContaining('username') { userAccount.isValidPassword(PASSWORD) }
76
77        userAccount.username = ''
78        shouldFailWithMessageContaining('username') { userAccount.isValidPassword(PASSWORD) }
79    }
80
81    void testIsValidPassword_OverrideComparePassword() {
82        def customUserAccount = new CustomUserAccount()
83        customUserAccount.username = USERNAME
84        customUserAccount.password = PASSWORD
85        println customUserAccount
86        assert customUserAccount.isValidPassword(PASSWORD) == false
87        assert customUserAccount.isValidPassword(PASSWORD + "123")
88    }
89
90    void testIsValidPassword_PasswordNotCheckedDuringValidation() {
91        userAccount.username = USERNAME
92        userAccount.password = PASSWORD
93        userAccount.passwordCheckedDuringValidation = false
94        assert userAccount.isValidPassword("wrong")
95    }
96
97    void testIsValid() {
98        assert !userAccount.valid
99        userAccount.homeDirectory = ""
100        assert !userAccount.valid
101        userAccount.homeDirectory = "/abc"
102        assert userAccount.valid
103    }
104
105    void testCanRead() {
106        // No file permissions - readable by all
107        testCanRead(USERNAME, GROUP, null, true)
108
109        // UserAccount has no username or group; use World permissions
110        testCanRead(USERNAME, GROUP, '------r--', true)
111        testCanRead(USERNAME, GROUP, 'rwxrwx-wx', false)
112
113        userAccount.username = USERNAME
114        userAccount.groups = [GROUP]
115
116        testCanRead(USERNAME, GROUP, 'rwxrwxrwx', true)     // ALL
117        testCanRead(USERNAME, GROUP, '---------', false)    // NONE
118
119        testCanRead(USERNAME, null, 'r--------', true)      // User
120        testCanRead(USERNAME, null, '-wxrwxrwx', false)
121
122        testCanRead(null, GROUP, '---r-----', true)         // Group
123        testCanRead(null, GROUP, 'rwx-wxrwx', false)
124
125        testCanRead(null, null, '------r--', true)          // World
126        testCanRead(null, null, 'rwxrwx-wx', false)
127    }
128
129    void testCanWrite() {
130        // No file permissions - writable by all
131        testCanWrite(USERNAME, GROUP, null, true)
132
133        // UserAccount has no username or group; use World permissions
134        testCanWrite(USERNAME, GROUP, '-------w-', true)
135        testCanWrite(USERNAME, GROUP, 'rwxrwxr-x', false)
136
137        userAccount.username = USERNAME
138        userAccount.groups = [GROUP]
139
140        testCanWrite(USERNAME, GROUP, 'rwxrwxrwx', true)     // ALL
141        testCanWrite(USERNAME, GROUP, '---------', false)    // NONE
142
143        testCanWrite(USERNAME, null, '-w-------', true)      // User
144        testCanWrite(USERNAME, null, 'r-xrwxrwx', false)
145
146        testCanWrite(null, GROUP, '----w----', true)         // Group
147        testCanWrite(null, GROUP, 'rwxr-xrwx', false)
148
149        testCanWrite(null, null, '-------w-', true)          // World
150        testCanWrite(null, null, 'rwxrwxr-x', false)
151    }
152
153    void testCanExecute() {
154        // No file permissions - executable by all
155        testCanExecute(USERNAME, GROUP, null, true)
156
157        // UserAccount has no username or group; use World permissions
158        testCanExecute(USERNAME, GROUP, '--------x', true)
159        testCanExecute(USERNAME, GROUP, 'rwxrwxrw-', false)
160
161        userAccount.username = USERNAME
162        userAccount.groups = [GROUP]
163
164        testCanExecute(USERNAME, GROUP, 'rwxrwxrwx', true)     // ALL
165        testCanExecute(USERNAME, GROUP, '---------', false)    // NONE
166
167        testCanExecute(USERNAME, null, '--x------', true)      // User
168        testCanExecute(USERNAME, null, 'rw-rwxrwx', false)
169
170        testCanExecute(null, GROUP, '-----x---', true)         // Group
171        testCanExecute(null, GROUP, 'rwxrw-rwx', false)
172
173        testCanExecute(null, null, '--------x', true)          // World
174        testCanExecute(null, null, 'rwxrwxrw-', false)
175    }
176
177    void testDefaultPermissions() {
178        assert userAccount.defaultPermissionsForNewFile == new Permissions('rw-rw-rw-')
179        assert userAccount.defaultPermissionsForNewDirectory == Permissions.ALL
180    }
181
182    //--------------------------------------------------------------------------
183    // Helper Methods
184    //--------------------------------------------------------------------------
185
186    private void testCanRead(owner, group, permissionsString, expectedResult) {
187        def file = createFileEntry(owner, permissionsString, group)
188        assert userAccount.canRead(file) == expectedResult, file
189    }
190
191    private void testCanWrite(owner, group, permissionsString, expectedResult) {
192        def file = createFileEntry(owner, permissionsString, group)
193        assert userAccount.canWrite(file) == expectedResult, file
194    }
195
196    private void testCanExecute(owner, group, permissionsString, expectedResult) {
197        def file = createFileEntry(owner, permissionsString, group)
198        assert userAccount.canExecute(file) == expectedResult, file
199    }
200
201    private FileSystemEntry createFileEntry(owner, permissionsString, group) {
202        def permissions = permissionsString ? new Permissions(permissionsString) : null
203        return new FileEntry(path: '', owner: owner, group: group, permissions: permissions)
204    }
205
206    void setUp() {
207        super.setUp()
208        userAccount = new UserAccount()
209    }
210}