-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath_test.go
136 lines (123 loc) · 2.38 KB
/
path_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
129
130
131
132
133
134
135
136
// Copyright (c) Seth Hoenig
// SPDX-License-Identifier: MPL-2.0
package landlock
import (
"testing"
"github.com/shoenig/test/must"
)
func TestPath_Dir(t *testing.T) {
cases := []struct {
mode string
path string
exp *Path
}{
{
mode: "r",
path: "/etc",
exp: &Path{mode: "r", path: "/etc", dir: true},
},
{
mode: "rx",
path: "/opt/bin",
exp: &Path{mode: "rx", path: "/opt/bin", dir: true},
},
}
for _, tc := range cases {
result := Dir(tc.path, tc.mode)
must.Equal(t, tc.exp, result)
}
}
func TestPath_ParsePath(t *testing.T) {
cases := []struct {
input string
exp *Path
}{
{
input: "d:r:/etc",
exp: &Path{mode: "r", path: "/etc", dir: true},
},
{
input: "d:rw:/etc/system",
exp: &Path{mode: "rw", path: "/etc/system", dir: true},
},
}
for _, tc := range cases {
result, err := ParsePath(tc.input)
must.NoError(t, err)
must.Equal(t, tc.exp, result)
}
}
func TestPath_ParsePath_error(t *testing.T) {
cases := []struct {
input string
exp error
}{
{
input: "",
exp: ErrImproperPath,
},
{
input: "rw:",
exp: ErrImproperPath,
},
{
input: "z:/etc",
exp: ErrImproperPath,
},
{
input: "rw:./foo/..",
exp: ErrImproperPath,
},
}
for _, tc := range cases {
t.Run(tc.input, func(t *testing.T) {
result, err := ParsePath(tc.input)
must.Nil(t, result)
must.ErrorIs(t, err, tc.exp,
must.Sprintf("err: %#v", err),
must.Sprintf("exp: %#v", tc.exp),
)
})
}
}
func TestPath_IsProperMode(t *testing.T) {
cases := []struct {
input string
exp bool
}{
{input: "r", exp: true},
{input: "w", exp: true},
{input: "c", exp: true},
{input: "x", exp: true},
{input: "rw", exp: true},
{input: "wrc", exp: true},
{input: "xc", exp: true},
{input: "", exp: false},
{input: "a", exp: false},
{input: "rwa", exp: false},
{input: "xar", exp: false},
{input: "RW", exp: false},
{input: "r w c x", exp: false},
}
for _, tc := range cases {
t.Run(tc.input, func(t *testing.T) {
result := IsProperMode(tc.input)
must.EqOp(t, tc.exp, result)
})
}
}
func TestPath_IsProperPath(t *testing.T) {
cases := []struct {
input string
exp bool
}{
{input: "/", exp: true},
{input: "", exp: false},
}
for _, tc := range cases {
t.Run(tc.input, func(t *testing.T) {
result := IsProperPath(tc.input)
must.EqOp(t, tc.exp, result)
})
}
}