diff --git a/daemon/rule/rule.go b/daemon/rule/rule.go index 034cdd828a..794f27cbc1 100644 --- a/daemon/rule/rule.go +++ b/daemon/rule/rule.go @@ -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( diff --git a/daemon/rule/rule_test.go b/daemon/rule/rule_test.go index cc54a4c87e..bde6f19323 100644 --- a/daemon/rule/rule_test.go +++ b/daemon/rule/rule_test.go @@ -1,6 +1,8 @@ package rule -import "testing" +import ( + "testing" +) func TestCreate(t *testing.T) { 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() + } + }) + +}