diff --git a/dnsutils/message.go b/dnsutils/message.go index 995b9e43..3fdd36fe 100644 --- a/dnsutils/message.go +++ b/dnsutils/message.go @@ -1487,7 +1487,7 @@ func matchUserMap(realValue, expectedValue reflect.Value) (bool, error) { } if !isFloat && !isInt { - return false, fmt.Errorf("integer is expected for greater-than operator") + return false, fmt.Errorf("integer or float is expected for greater-than operator") } // If realValue is a slice @@ -1508,13 +1508,13 @@ func matchUserMap(realValue, expectedValue reflect.Value) (bool, error) { return false, nil } - if realValue.Kind() == reflect.Float64 { + if isFloat && realValue.Kind() == reflect.Float64 { if realValue.Interface().(float64) > opValue.Interface().(float64) { return true, nil } } - if realValue.Kind() == reflect.Int { + if isInt && realValue.Kind() == reflect.Int { if realValue.Interface().(int) > opValue.Interface().(int) { return true, nil } diff --git a/dnsutils/message_test.go b/dnsutils/message_test.go index 3670bcfa..e32722be 100644 --- a/dnsutils/message_test.go +++ b/dnsutils/message_test.go @@ -1733,7 +1733,7 @@ func TestDNSMessage_Matching(t *testing.T) { wantMatch: true, }, { - name: "Test greater than operator matching", + name: "Test integer greater than operator matching", dm: &DNSMessage{DNS: DNS{Opcode: 5}}, matching: map[string]interface{}{ "dns.opcode": map[string]interface{}{ @@ -1743,6 +1743,68 @@ func TestDNSMessage_Matching(t *testing.T) { wantError: false, wantMatch: true, }, + { + name: "Test integer with invalid greater than operator", + dm: &DNSMessage{DNS: DNS{Opcode: 1}}, + matching: map[string]interface{}{ + "dns.opcode": map[string]interface{}{ + "greater-than": "0", + }, + }, + wantError: true, + wantMatch: false, + }, + { + name: "Test float greater than operator matching", + dm: &DNSMessage{DNSTap: DNSTap{Latency: 0.5}}, + matching: map[string]interface{}{ + "dnstap.latency": map[string]interface{}{ + "greater-than": 0.3, + }, + }, + wantError: false, + wantMatch: true, + }, + { + name: "Test lower than operator matching", + dm: &DNSMessage{DNS: DNS{Opcode: 9}}, + matching: map[string]interface{}{ + "dns.opcode": map[string]interface{}{ + "lower-than": 10, + }, + }, + wantError: false, + wantMatch: true, + }, + { + name: "Test lower than operator no match", + dm: &DNSMessage{DNS: DNS{Opcode: 1}}, + matching: map[string]interface{}{ + "dns.opcode": map[string]interface{}{ + "lower-than": 1, + }, + }, + wantError: false, + wantMatch: false, + }, + { + name: "Test match with list of string", + dm: &DNSMessage{DNS: DNS{Qname: "www.example.com"}}, + matching: map[string]interface{}{ + "dns.qname": []interface{}{"www.test.com", "www.example.com"}, + }, + wantError: false, + wantMatch: true, + }, + { + name: "Test no match with list of string", + dm: &DNSMessage{DNS: DNS{Qname: "www.notexample.com"}}, + matching: map[string]interface{}{ + "dns.qname": []interface{}{"www.test.com", "www.example.com"}, + }, + wantError: false, + wantMatch: false, + }, } for _, tt := range tests {