-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_test.go
214 lines (201 loc) · 4.22 KB
/
parser_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package polai_test
import (
"reflect"
"strings"
"testing"
"github.com/iann0036/polai"
)
// Ensure the parser can parse strings into Statement ASTs.
func TestParser_ParseStatement(t *testing.T) {
var tests = []struct {
s string
stmts *[]polai.PolicyStatement
err string
}{
// Basic permit
{
s: `
permit (
principal,
action,
resource
);`,
stmts: &[]polai.PolicyStatement{
{
Effect: polai.PERMIT,
AnyPrincipal: true,
AnyAction: true,
AnyResource: true,
},
},
},
// Basic forbid
{
s: `
forbid (
principal,
action,
resource
);`,
stmts: &[]polai.PolicyStatement{
{
Effect: polai.FORBID,
AnyPrincipal: true,
AnyAction: true,
AnyResource: true,
},
},
},
// Multiple statements
{
s: `
permit (
principal,
action,
resource
);
forbid (
principal,
action,
resource
);`,
stmts: &[]polai.PolicyStatement{
{
Effect: polai.PERMIT,
AnyPrincipal: true,
AnyAction: true,
AnyResource: true,
},
{
Effect: polai.FORBID,
AnyPrincipal: true,
AnyAction: true,
AnyResource: true,
},
},
},
// Comments
{
s: `
// comment stuff
permit (
// comment stuff
principal, // comment stuff
action,
resource // comment stuff
); // comment stuff`,
stmts: &[]polai.PolicyStatement{
{
Effect: polai.PERMIT,
AnyPrincipal: true,
AnyAction: true,
AnyResource: true,
},
},
},
// Scope with equality
{
s: `
permit (
principal == Namespace::"Identifier",
action == Namespace2::"Identifier2",
resource == Namespace3::"Identifier3"
);`,
stmts: &[]polai.PolicyStatement{
{
Effect: polai.PERMIT,
Principal: "Namespace::\"Identifier\"",
Action: "Namespace2::\"Identifier2\"",
Resource: "Namespace3::\"Identifier3\"",
AnyPrincipal: false,
AnyAction: false,
AnyResource: false,
},
},
},
// Scope with in set
{
s: `
permit (
principal,
action in [ Namespace::"Identifier", Namespace2::"Identifier2" ],
resource
);`,
stmts: &[]polai.PolicyStatement{
{
Effect: polai.PERMIT,
ActionParents: []string{"Namespace::\"Identifier\"", "Namespace2::\"Identifier2\""},
AnyPrincipal: true,
AnyAction: false,
AnyResource: true,
},
},
},
// Scope with in entity
{
s: `
permit (
principal in Namespace::"Identifier",
action in Namespace2::"Identifier2",
resource in Namespace3::"Identifier3"
);`,
stmts: &[]polai.PolicyStatement{
{
Effect: polai.PERMIT,
PrincipalParent: "Namespace::\"Identifier\"",
ActionParents: []string{"Namespace2::\"Identifier2\""},
ResourceParent: "Namespace3::\"Identifier3\"",
AnyPrincipal: false,
AnyAction: false,
AnyResource: false,
},
},
},
// Basic when with int equality
{
s: `
permit (
principal,
action,
resource
) when {
123 == 0123
};`,
stmts: &[]polai.PolicyStatement{
{
Effect: polai.PERMIT,
AnyPrincipal: true,
AnyAction: true,
AnyResource: true,
Conditions: []polai.ConditionClause{
{
Type: polai.WHEN,
Sequence: []polai.SequenceItem{
{Token: polai.LONG, Literal: "123", Normalized: "123"},
{Token: polai.EQUALITY, Literal: "==", Normalized: "=="},
{Token: polai.LONG, Literal: "0123", Normalized: "123"},
},
},
},
},
},
},
// Errors
{s: `foo`, err: `found "foo", expected permit or forbid`},
}
for i, tt := range tests {
stmts, err := polai.NewParser(strings.NewReader(tt.s)).Parse()
if !reflect.DeepEqual(tt.err, errstring(err)) {
t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
} else if tt.err == "" && !reflect.DeepEqual(tt.stmts, stmts) {
t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmts, stmts)
}
}
}
// errstring returns the string representation of an error.
func errstring(err error) string {
if err != nil {
return err.Error()
}
return ""
}