Skip to content

Commit

Permalink
fix: matcher function lost (#818)
Browse files Browse the repository at this point in the history
Signed-off-by: closetool <c299999999@qq.com>
  • Loading branch information
kilosonc committed Jun 19, 2021
1 parent 0f894b3 commit 2c4ba4a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion log/mocks/mock_logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion model/model.go
Expand Up @@ -248,10 +248,21 @@ func (model Model) SortPoliciesByPriority() error {
}

func (model Model) ToText() string {
tokenPatterns := make(map[string]string)

for _, ptype := range []string{"r", "p"} {
for _, token := range model[ptype][ptype].Tokens {
tokenPatterns[token] = strings.Replace(token, "_", ".", -1)
}
}
s := strings.Builder{}
writeString := func(sec string) {
for ptype := range model[sec] {
s.WriteString(fmt.Sprintf("%s = %s\n", ptype, strings.Replace(model[sec][ptype].Value, "_", ".", -1)))
value := model[sec][ptype].Value
for tokenPattern, newToken := range tokenPatterns {
value = strings.Replace(value, tokenPattern, newToken, -1)
}
s.WriteString(fmt.Sprintf("%s = %s\n", sec, value))
}
}
s.WriteString("[request_definition]\n")
Expand Down
35 changes: 35 additions & 0 deletions model/model_test.go
Expand Up @@ -148,3 +148,38 @@ func TestModel_CopyTo(t *testing.T) {
t.Fatal(`the a["p"]["p"] and b["p"]["p"] should not be equal`)
}
}

func TestModelToTest(t *testing.T) {
testModelToText(t, "r.sub == p.sub && r.obj == p.obj && r_func(r.act, p.act) && testr_func(r.act, p.act)", "r_sub == p_sub && r_obj == p_obj && r_func(r_act, p_act) && testr_func(r_act, p_act)")
testModelToText(t, "r.sub == p.sub && r.obj == p.obj && p_func(r.act, p.act) && testp_func(r.act, p.act)", "r_sub == p_sub && r_obj == p_obj && p_func(r_act, p_act) && testp_func(r_act, p_act)")
}

func testModelToText(t *testing.T, mData, mExpected string) {
m := NewModel()
data := map[string]string{
"r": "sub, obj, act",
"p": "sub, obj, act",
"e": "some(where (p.eft == allow))",
"m": mData,
}
expected := map[string]string{
"r": "sub, obj, act",
"p": "sub, obj, act",
"e": "some(where (p_eft == allow))",
"m": mExpected,
}
addData := func(ptype string) {
m.AddDef(ptype, ptype, data[ptype])
}
for ptype := range data {
addData(ptype)
}
newM := NewModel()
print(m.ToText())
_ = newM.LoadModelFromText(m.ToText())
for ptype := range data {
if newM[ptype][ptype].Value != expected[ptype] {
t.Errorf("\"%s\" assertion value changed, current value: %s, it should be: %s", ptype, newM[ptype][ptype].Value, expected[ptype])
}
}
}

0 comments on commit 2c4ba4a

Please sign in to comment.