-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermbits_test.go
128 lines (119 loc) · 3.36 KB
/
permbits_test.go
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
package permbits_test
import (
"fmt"
"os"
"testing"
"github.com/na4ma4/go-permbits"
)
const (
modeAll = os.FileMode(0o777)
modeNone = os.FileMode(0o000)
)
func TestIs_CompareSingleModeAll(t *testing.T) {
tests := []struct {
name string
mode os.FileMode
is os.FileMode
}{
{"Compare 0o777 to UserRead", modeAll, permbits.UserRead},
{"Compare 0o777 to UserWrite", modeAll, permbits.UserWrite},
{"Compare 0o777 to UserExecute", modeAll, permbits.UserExecute},
{"Compare 0o777 to GroupRead", modeAll, permbits.GroupRead},
{"Compare 0o777 to GroupWrite", modeAll, permbits.GroupWrite},
{"Compare 0o777 to GroupExecute", modeAll, permbits.GroupExecute},
{"Compare 0o777 to OtherRead", modeAll, permbits.OtherRead},
{"Compare 0o777 to OtherWrite", modeAll, permbits.OtherWrite},
{"Compare 0o777 to OtherExecute", modeAll, permbits.OtherExecute},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if v := permbits.Is(tt.mode, tt.is); !v {
t.Errorf("permbits.Is() returned %t for %04o matching %04o", v, tt.mode, tt.is)
}
})
}
}
func TestIs_CompareSingleModeNone(t *testing.T) {
tests := []struct {
name string
mode os.FileMode
is os.FileMode
}{
{"Compare 0o000 to UserRead", modeNone, permbits.UserRead},
{"Compare 0o000 to UserWrite", modeNone, permbits.UserWrite},
{"Compare 0o000 to UserExecute", modeNone, permbits.UserExecute},
{"Compare 0o000 to GroupRead", modeNone, permbits.GroupRead},
{"Compare 0o000 to GroupWrite", modeNone, permbits.GroupWrite},
{"Compare 0o000 to GroupExecute", modeNone, permbits.GroupExecute},
{"Compare 0o000 to OtherRead", modeNone, permbits.OtherRead},
{"Compare 0o000 to OtherWrite", modeNone, permbits.OtherWrite},
{"Compare 0o000 to OtherExecute", modeNone, permbits.OtherExecute},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if v := permbits.Is(tt.mode, tt.is); v {
t.Errorf("permbits.Is() returned %t for %04o matching %04o", v, tt.mode, tt.is)
}
})
}
}
func TestIs_CompareMultipleModes(t *testing.T) {
tests := []struct {
name string
mode os.FileMode
is os.FileMode
want bool
}{
{
"Compare 0o777 to UserAll+GroupAll+OtherAll, expect True",
0o777,
permbits.UserAll + permbits.GroupAll + permbits.OtherAll,
true,
},
{
"Compare 0o775 to UserAll+GroupAll+OtherAll, expect False",
0o775,
permbits.UserAll + permbits.GroupAll + permbits.OtherAll,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if v := permbits.Is(tt.mode, tt.is); v != tt.want {
t.Errorf("permbits.Is() returned %t, expected %t for %04o matching %04o", v, tt.want, tt.mode, tt.is)
}
})
}
}
func TestForce(t *testing.T) {
t.Parallel()
tests := []struct {
mode os.FileMode
expect os.FileMode
want []os.FileMode
}{
{
0o111, 0o711,
[]os.FileMode{permbits.UserReadWriteExecute},
},
{
0o111, 0o711,
[]os.FileMode{permbits.UserRead, permbits.UserWrite, permbits.UserExecute},
},
{
0o111, 0o171,
[]os.FileMode{permbits.GroupRead, permbits.GroupWrite, permbits.GroupExecute},
},
}
for _, tt := range tests {
t.Run(
fmt.Sprintf("%04o + %v = %04o", tt.mode, tt.want, tt.expect),
func(t *testing.T) {
t.Parallel()
if v := permbits.Force(tt.mode, tt.want...); v != tt.expect {
t.Errorf("permbits.Force() got '%04o', want '%04o'", v, tt.expect)
}
},
)
}
}