summaryrefslogtreecommitdiff
path: root/tags/2.3/src/test/groovy/org/mockftpserver/fake/filesystem/PermissionsTest.groovy
blob: cf75e1cd8f77eb8b103cd14dfbf53a4709350c69 (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
/*
 * 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.filesystem

import org.mockftpserver.test.AbstractGroovyTestCase

/**
 * Tests for the Permissions class
 *
 * @version $Revision$ - $Date$
 *
 * @author Chris Mair
 */
class PermissionsTest extends AbstractGroovyTestCase {

    void testConstructor() {
        testConstructorWithValidString('rwxrwxrwx')
        testConstructorWithValidString('rwxr--r--')
        testConstructorWithValidString('---------')
    }

    void testConstructor_InvalidString() {
        testConstructorWithInvalidString('')
        testConstructorWithInvalidString('------')
        testConstructorWithInvalidString('-')
        testConstructorWithInvalidString('r')
        testConstructorWithInvalidString('rwx')
        testConstructorWithInvalidString('rwxrwxrw')
        testConstructorWithInvalidString('123456789')
        testConstructorWithInvalidString('rwxrZxrwx')
        testConstructorWithInvalidString('--------Z')
    }

    void testCanReadWriteExecute() {
        testCanReadWriteExecute('rwxrwxrwx', true, true, true, true, true, true, true, true, true)
        testCanReadWriteExecute('r--r--r--', true, false, false, true, false, false, true, false, false)
        testCanReadWriteExecute('-w-r----x', false, true, false, true, false, false, false, false, true)
        testCanReadWriteExecute('---------', false, false, false, false, false, false, false, false, false)
    }

    void testHashCode() {
        assert new Permissions('rwxrwxrwx').hashCode() == Permissions.DEFAULT.hashCode()
        assert new Permissions('---------').hashCode() == Permissions.NONE.hashCode()
    }

    void testEquals() {
        assert new Permissions('rwxrwxrwx').equals(Permissions.DEFAULT)
        assert new Permissions('---------').equals(Permissions.NONE)
        assert Permissions.NONE.equals(Permissions.NONE)

        assert !(new Permissions('------rwx').equals(Permissions.NONE))
        assert !Permissions.NONE.equals(null)
        assert !Permissions.NONE.equals(123)
    }

    //--------------------------------------------------------------------------
    // Helper Methods
    //--------------------------------------------------------------------------

    private testCanReadWriteExecute(rwxString,
                                    canUserRead, canUserWrite, canUserExecute,
                                    canGroupRead, canGroupWrite, canGroupExecute,
                                    canWorldRead, canWorldWrite, canWorldExecute) {

        def permissions = new Permissions(rwxString)
        LOG.info("Testing can read/write/execute for $permissions")
        assert permissions.canUserRead() == canUserRead
        assert permissions.canUserWrite() == canUserWrite
        assert permissions.canUserExecute() == canUserExecute
        assert permissions.canGroupRead() == canGroupRead
        assert permissions.canGroupWrite() == canGroupWrite
        assert permissions.canGroupExecute() == canGroupExecute
        assert permissions.canWorldRead() == canWorldRead
        assert permissions.canWorldWrite() == canWorldWrite
        assert permissions.canWorldExecute() == canWorldExecute
    }

    private testConstructorWithInvalidString(String string) {
        LOG.info("Verifying invalid: [$string]")
        shouldFail { new Permissions(string) }
    }

    private testConstructorWithValidString(String string) {
        LOG.info("Verifying valid: [$string]")
        def permissions = new Permissions(string)
        LOG.info(permissions)
        assert permissions.asRwxString() == string
    }
}