deserialize rules operator list correctly

In b93051026e we disabled sending/parsing
list operators as JSON strings. Instead, now it's sent/parsed as
protobuf Rule, and saved to disk as JSON array, which ease the task of
manually creating new rules if needed.

This change was missing in the previous commit.

(cherry picked from commit 2fc9ed276b)
This commit is contained in:
Gustavo Iñiguez Goia 2024-06-21 14:35:15 +02:00
parent 987a227a42
commit ae9aa40c37
Failed to generate hash of commit
2 changed files with 74 additions and 1 deletions

View file

@ -106,6 +106,7 @@ func Deserialize(reply *protocol.Rule) (*Rule, error) {
) )
if Type(reply.Operator.Type) == List { if Type(reply.Operator.Type) == List {
newRule.Operator.Data = ""
reply.Operator.Data = "" reply.Operator.Data = ""
for i := 0; i < len(reply.Operator.List); i++ { for i := 0; i < len(reply.Operator.List); i++ {
newRule.Operator.List = append( newRule.Operator.List = append(

View file

@ -1,6 +1,8 @@
package rule package rule
import "testing" import (
"testing"
)
func TestCreate(t *testing.T) { func TestCreate(t *testing.T) {
t.Log("Test: Create rule") t.Log("Test: Create rule")
@ -45,3 +47,73 @@ func TestCreate(t *testing.T) {
} }
}) })
} }
func TestRuleSerializers(t *testing.T) {
t.Log("Test: Serializers()")
var opList []Operator
opList = append(opList, Operator{
Type: Simple,
Operand: OpProcessPath,
Data: "/path/x",
})
opList = append(opList, Operator{
Type: Simple,
Operand: OpDstPort,
Data: "23",
})
op, _ := NewOperator(List, false, OpTrue, "", opList)
// this string must be erased after Deserialized
op.Data = "[\"test\": true]"
r := Create("000-test-serializer-list", "rule description 000", true, false, false, Allow, Once, op)
rSerialized := r.Serialize()
t.Run("Serialize() must not return nil", func(t *testing.T) {
if rSerialized == nil {
t.Error("rule.Serialize() returned nil")
t.Fail()
}
})
rDeser, err := Deserialize(rSerialized)
t.Run("Deserialize must not return error", func(t *testing.T) {
if err != nil {
t.Error("rule.Serialize() returned error:", err)
t.Fail()
}
})
// commit: b93051026e6a82ba07a5ac2f072880e69f04c238
t.Run("Deserialize. Operator.Data must be empty", func(t *testing.T) {
if rDeser.Operator.Data != "" {
t.Error("rule.Deserialize() Operator.Data not emptied:", rDeser.Operator.Data)
t.Fail()
}
})
t.Run("Deserialize. Operator.List must be expanded", func(t *testing.T) {
if len(rDeser.Operator.List) != 2 {
t.Error("rule.Deserialize() invalid Operator.List:", rDeser.Operator.List)
t.Fail()
}
if rDeser.Operator.List[0].Operand != OpProcessPath {
t.Error("rule.Deserialize() invalid Operator.List 1:", rDeser.Operator.List)
t.Fail()
}
if rDeser.Operator.List[1].Operand != OpDstPort {
t.Error("rule.Deserialize() invalid Operator.List 2:", rDeser.Operator.List)
t.Fail()
}
if rDeser.Operator.List[0].Type != Simple || rDeser.Operator.List[1].Type != Simple {
t.Error("rule.Deserialize() invalid Operator.List 3:", rDeser.Operator.List)
t.Fail()
}
if rDeser.Operator.List[0].Data != "/path/x" || rDeser.Operator.List[1].Data != "23" {
t.Error("rule.Deserialize() invalid Operator.List 4:", rDeser.Operator.List)
t.Fail()
}
})
}