Skip to content

Commit

Permalink
deserialize rules operator list correctly
Browse files Browse the repository at this point in the history
In b930510 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 2fc9ed2)
  • Loading branch information
gustavo-iniguez-goya committed Jun 21, 2024
1 parent 987a227 commit ae9aa40
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions daemon/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func Deserialize(reply *protocol.Rule) (*Rule, error) {
)

if Type(reply.Operator.Type) == List {
newRule.Operator.Data = ""
reply.Operator.Data = ""
for i := 0; i < len(reply.Operator.List); i++ {
newRule.Operator.List = append(
Expand Down
74 changes: 73 additions & 1 deletion daemon/rule/rule_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package rule

import "testing"
import (
"testing"
)

func TestCreate(t *testing.T) {
t.Log("Test: Create rule")
Expand Down Expand Up @@ -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()
}
})

}

0 comments on commit ae9aa40

Please sign in to comment.