mirror of
https://github.com/evilsocket/opensnitch.git
synced 2025-03-04 00:24:40 +01:00
deserialize rules operator list correctly
Inb93051026e
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 commit2fc9ed276b
)
This commit is contained in:
parent
987a227a42
commit
ae9aa40c37
2 changed files with 74 additions and 1 deletions
|
@ -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(
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue