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