aboutsummaryrefslogtreecommitdiff
path: root/acl/acl_test.go
blob: 46f8ada161a8c827131cbb0b58f4c985afd8c40b (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
package acl

import (
	"os"
	"testing"
)

func checkPerm(t *testing.T, cat rune, perms QualifiedPerms, k string, want Perm) {
	have, ok := perms[k]
	if ok {
		if have != want {
			t.Errorf("Perm %c:%s:%s != %c:%s:%s", cat, k, have, cat, k, want)
		}
	} else {
		t.Errorf("Perm %c:%s: not set", cat, k)
	}
}

func TestFromUnix(t *testing.T) {
	acl := ACLFromUnixPerms(0752)
	if len(acl.User) != 1 {
		t.Errorf("Expected exactly 1 user perm, got %d", len(acl.User))
	}
	if len(acl.Group) != 1 {
		t.Errorf("Expected exactly 1 group perm, got %d", len(acl.Group))
	}
	if len(acl.Other) != 1 {
		t.Errorf("Expected exactly 1 other perm, got %d", len(acl.Other))
	}
	if len(acl.Mask) != 0 {
		t.Errorf("Expected exactly 0 mask perm, got %d", len(acl.Mask))
	}

	checkPerm(t, 'u', acl.User, "", PermR|PermW|PermX)
	checkPerm(t, 'g', acl.Group, "", PermR|PermX)
	checkPerm(t, 'o', acl.Other, "", PermW)
}

func TestToUnix(t *testing.T) {
	acl := ACL{}
	acl.Init()

	acl.User[""] = PermR | PermW | PermX
	acl.Group[""] = PermR | PermX
	acl.Other[""] = PermR

	want := os.FileMode(0754)
	have := acl.ToUnixPerms()
	if have != want {
		t.Errorf("Unexpected ToUnixPerms result: have 0%o, want 0%o", have, want)
	}
}

func TestStringify(t *testing.T) {
	acl := ACL{}
	acl.Init()

	acl.User[""] = PermR | PermW | PermX
	acl.User["foo"] = PermR | PermW
	acl.User["bar"] = PermR | PermW
	acl.Group[""] = PermR | PermX
	acl.Group["baz"] = PermR | PermW | PermX
	acl.Other[""] = 0
	acl.Mask[""] = PermX

	want := "u::rwx,u:bar:rw-,u:foo:rw-,g::r-x,g:baz:rwx,o::---,m::--x"
	have := acl.String()
	if have != want {
		t.Errorf("Unexpected String result: have %s, want %s", have, want)
	}
}

func TestParsing(t *testing.T) {
	acl, err := ParseACL("u::rwx,u:bar:rw-,u:foo:rw-,g::r-x,g:baz:rwx,o::---,m::--x")
	if err != nil {
		t.Fatalf("unexpected parsing error: %s", err)
	}

	if len(acl.User) == 3 {
		checkPerm(t, 'u', acl.User, "", PermR|PermW|PermX)
		checkPerm(t, 'u', acl.User, "foo", PermR|PermW)
		checkPerm(t, 'u', acl.User, "bar", PermR|PermW)
	} else {
		t.Errorf("Expeced 3 user entries, got %d", len(acl.User))
	}

	if len(acl.Group) == 2 {
		checkPerm(t, 'u', acl.Group, "", PermR|PermX)
		checkPerm(t, 'u', acl.Group, "baz", PermR|PermW|PermX)
	} else {
		t.Errorf("Expeced 2 group entries, got %d", len(acl.Group))
	}

	if len(acl.Other) == 1 {
		checkPerm(t, 'u', acl.Other, "", 0)
	} else {
		t.Errorf("Expeced 1 other entries, got %d", len(acl.Other))
	}

	if len(acl.Mask) == 1 {
		checkPerm(t, 'u', acl.Mask, "", PermX)
	} else {
		t.Errorf("Expeced 1 mask entries, got %d", len(acl.Mask))
	}
}