2020-07-28 13:38:48 +00:00
|
|
|
package exprhelpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-05-12 07:43:01 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-03-28 08:49:01 +00:00
|
|
|
"github.com/antonmedv/expr"
|
2020-07-28 13:38:48 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-01-05 14:26:13 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-28 13:38:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestJsonExtract(t *testing.T) {
|
2022-06-22 09:29:52 +00:00
|
|
|
if err := Init(nil); err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
log.Fatal(err)
|
2020-07-28 13:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := FileInit(TestFolder, "test_data_re.txt", "regex")
|
|
|
|
if err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
log.Fatal(err)
|
2020-07-28 13:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
jsonBlob string
|
|
|
|
targetField string
|
|
|
|
expectResult string
|
2023-03-28 08:49:01 +00:00
|
|
|
expr string
|
2020-07-28 13:38:48 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic json extract",
|
|
|
|
jsonBlob: `{"test" : "1234"}`,
|
|
|
|
targetField: "test",
|
|
|
|
expectResult: "1234",
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "JsonExtract(blob, target)",
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic json extract with non existing field",
|
|
|
|
jsonBlob: `{"test" : "1234"}`,
|
|
|
|
targetField: "non_existing_field",
|
|
|
|
expectResult: "",
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "JsonExtract(blob, target)",
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
2022-06-08 10:15:29 +00:00
|
|
|
{
|
|
|
|
name: "extract subfield",
|
|
|
|
jsonBlob: `{"test" : {"a": "b"}}`,
|
|
|
|
targetField: "test.a",
|
|
|
|
expectResult: "b",
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "JsonExtract(blob, target)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
2020-07-28 13:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2023-03-28 08:49:01 +00:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
env := map[string]interface{}{
|
|
|
|
"blob": test.jsonBlob,
|
|
|
|
"target": test.targetField,
|
|
|
|
}
|
|
|
|
vm, err := expr.Compile(test.expr, GetExprOptions(env)...)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
out, err := expr.Run(vm, env)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
assert.Equal(t, test.expectResult, out)
|
|
|
|
})
|
2020-07-28 13:38:48 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-09-10 10:43:11 +00:00
|
|
|
func TestJsonExtractUnescape(t *testing.T) {
|
2022-06-22 09:29:52 +00:00
|
|
|
if err := Init(nil); err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
log.Fatal(err)
|
2021-09-10 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := FileInit(TestFolder, "test_data_re.txt", "regex")
|
|
|
|
if err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
log.Fatal(err)
|
2021-09-10 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
jsonBlob string
|
|
|
|
targetField string
|
|
|
|
expectResult string
|
2023-03-28 08:49:01 +00:00
|
|
|
expr string
|
2021-09-10 10:43:11 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic json extract",
|
|
|
|
jsonBlob: `{"log" : "\"GET /JBNwtQ6i.blt HTTP/1.1\" 200 13 \"-\" \"Craftbot\""}`,
|
|
|
|
targetField: "log",
|
|
|
|
expectResult: "\"GET /JBNwtQ6i.blt HTTP/1.1\" 200 13 \"-\" \"Craftbot\"",
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "JsonExtractUnescape(blob, target)",
|
2021-09-10 10:43:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic json extract with non existing field",
|
|
|
|
jsonBlob: `{"test" : "1234"}`,
|
|
|
|
targetField: "non_existing_field",
|
|
|
|
expectResult: "",
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "JsonExtractUnescape(blob, target)",
|
2021-09-10 10:43:11 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2023-03-28 08:49:01 +00:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
env := map[string]interface{}{
|
|
|
|
"blob": test.jsonBlob,
|
|
|
|
"target": test.targetField,
|
|
|
|
}
|
|
|
|
vm, err := expr.Compile(test.expr, GetExprOptions(env)...)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
out, err := expr.Run(vm, env)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
assert.Equal(t, test.expectResult, out)
|
|
|
|
})
|
2021-09-10 10:43:11 +00:00
|
|
|
}
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonExtractSlice(t *testing.T) {
|
2022-06-22 09:29:52 +00:00
|
|
|
if err := Init(nil); err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
log.Fatal(err)
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := FileInit(TestFolder, "test_data_re.txt", "regex")
|
|
|
|
if err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
log.Fatal(err)
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
jsonBlob string
|
|
|
|
targetField string
|
|
|
|
expectResult []interface{}
|
2023-03-28 08:49:01 +00:00
|
|
|
expr string
|
2022-06-08 10:15:29 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "try to extract a string as a slice",
|
|
|
|
jsonBlob: `{"test" : "1234"}`,
|
|
|
|
targetField: "test",
|
|
|
|
expectResult: nil,
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "JsonExtractSlice(blob, target)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic json slice extract",
|
|
|
|
jsonBlob: `{"test" : ["1234"]}`,
|
|
|
|
targetField: "test",
|
|
|
|
expectResult: []interface{}{"1234"},
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "JsonExtractSlice(blob, target)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "extract with complex expression",
|
|
|
|
jsonBlob: `{"test": {"foo": [{"a":"b"}]}}`,
|
|
|
|
targetField: "test.foo",
|
|
|
|
expectResult: []interface{}{map[string]interface{}{"a": "b"}},
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "JsonExtractSlice(blob, target)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "extract non-existing key",
|
|
|
|
jsonBlob: `{"test: "11234"}`,
|
|
|
|
targetField: "foo",
|
|
|
|
expectResult: nil,
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "JsonExtractSlice(blob, target)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2022-10-10 08:48:26 +00:00
|
|
|
test := test
|
2022-06-08 10:15:29 +00:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-03-28 08:49:01 +00:00
|
|
|
env := map[string]interface{}{
|
|
|
|
"blob": test.jsonBlob,
|
|
|
|
"target": test.targetField,
|
|
|
|
}
|
|
|
|
vm, err := expr.Compile(test.expr, GetExprOptions(env)...)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
out, err := expr.Run(vm, env)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
assert.Equal(t, test.expectResult, out)
|
2022-06-08 10:15:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonExtractObject(t *testing.T) {
|
2022-06-22 09:29:52 +00:00
|
|
|
if err := Init(nil); err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
log.Fatal(err)
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := FileInit(TestFolder, "test_data_re.txt", "regex")
|
|
|
|
if err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
log.Fatal(err)
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
jsonBlob string
|
|
|
|
targetField string
|
|
|
|
expectResult map[string]interface{}
|
2023-03-28 08:49:01 +00:00
|
|
|
expr string
|
2022-06-08 10:15:29 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "try to extract a string as an object",
|
|
|
|
jsonBlob: `{"test" : "1234"}`,
|
|
|
|
targetField: "test",
|
|
|
|
expectResult: nil,
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "JsonExtractObject(blob, target)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic json object extract",
|
|
|
|
jsonBlob: `{"test" : {"1234": {"foo": "bar"}}}`,
|
|
|
|
targetField: "test",
|
|
|
|
expectResult: map[string]interface{}{"1234": map[string]interface{}{"foo": "bar"}},
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "JsonExtractObject(blob, target)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "extract with complex expression",
|
|
|
|
jsonBlob: `{"test": {"foo": [{"a":"b"}]}}`,
|
|
|
|
targetField: "test.foo[0]",
|
|
|
|
expectResult: map[string]interface{}{"a": "b"},
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "JsonExtractObject(blob, target)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2022-10-10 08:48:26 +00:00
|
|
|
test := test
|
2022-06-08 10:15:29 +00:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-03-28 08:49:01 +00:00
|
|
|
env := map[string]interface{}{
|
|
|
|
"blob": test.jsonBlob,
|
|
|
|
"target": test.targetField,
|
|
|
|
}
|
|
|
|
vm, err := expr.Compile(test.expr, GetExprOptions(env)...)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
out, err := expr.Run(vm, env)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
assert.Equal(t, test.expectResult, out)
|
2022-06-08 10:15:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToJson(t *testing.T) {
|
2023-03-28 08:49:01 +00:00
|
|
|
err := Init(nil)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-08 10:15:29 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
obj interface{}
|
|
|
|
expectResult string
|
2023-03-28 08:49:01 +00:00
|
|
|
expr string
|
2022-06-08 10:15:29 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "convert int",
|
|
|
|
obj: 42,
|
|
|
|
expectResult: "42",
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "ToJsonString(obj)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "convert slice",
|
|
|
|
obj: []string{"foo", "bar"},
|
|
|
|
expectResult: `["foo","bar"]`,
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "ToJsonString(obj)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "convert map",
|
|
|
|
obj: map[string]string{"foo": "bar"},
|
|
|
|
expectResult: `{"foo":"bar"}`,
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "ToJsonString(obj)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "convert struct",
|
|
|
|
obj: struct{ Foo string }{"bar"},
|
|
|
|
expectResult: `{"Foo":"bar"}`,
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "ToJsonString(obj)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "convert complex struct",
|
|
|
|
obj: struct {
|
|
|
|
Foo string
|
|
|
|
Bar struct {
|
|
|
|
Baz string
|
|
|
|
}
|
|
|
|
Bla []string
|
|
|
|
}{
|
|
|
|
Foo: "bar",
|
|
|
|
Bar: struct {
|
|
|
|
Baz string
|
|
|
|
}{
|
|
|
|
Baz: "baz",
|
|
|
|
},
|
|
|
|
Bla: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
expectResult: `{"Foo":"bar","Bar":{"Baz":"baz"},"Bla":["foo","bar"]}`,
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "ToJsonString(obj)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "convert invalid type",
|
|
|
|
obj: func() {},
|
|
|
|
expectResult: "",
|
2023-03-28 08:49:01 +00:00
|
|
|
expr: "ToJsonString(obj)",
|
2022-06-08 10:15:29 +00:00
|
|
|
},
|
|
|
|
}
|
2021-09-10 10:43:11 +00:00
|
|
|
|
2022-06-08 10:15:29 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-03-28 08:49:01 +00:00
|
|
|
env := map[string]interface{}{
|
|
|
|
"obj": test.obj,
|
|
|
|
}
|
|
|
|
vm, err := expr.Compile(test.expr, GetExprOptions(env)...)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
out, err := expr.Run(vm, env)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
assert.Equal(t, test.expectResult, out)
|
2022-06-08 10:15:29 +00:00
|
|
|
})
|
|
|
|
}
|
2021-09-10 10:43:11 +00:00
|
|
|
}
|
2023-05-12 07:43:01 +00:00
|
|
|
|
|
|
|
func TestUnmarshalJSON(t *testing.T) {
|
|
|
|
err := Init(nil)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-12 07:43:01 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
json string
|
|
|
|
expectResult interface{}
|
|
|
|
expr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "convert int",
|
|
|
|
json: "42",
|
|
|
|
expectResult: float64(42),
|
|
|
|
expr: "UnmarshalJSON(json, out, 'a')",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "convert slice",
|
|
|
|
json: `["foo","bar"]`,
|
|
|
|
expectResult: []interface{}{"foo", "bar"},
|
|
|
|
expr: "UnmarshalJSON(json, out, 'a')",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "convert map",
|
|
|
|
json: `{"foo":"bar"}`,
|
|
|
|
expectResult: map[string]interface{}{"foo": "bar"},
|
|
|
|
expr: "UnmarshalJSON(json, out, 'a')",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "convert struct",
|
|
|
|
json: `{"Foo":"bar"}`,
|
|
|
|
expectResult: map[string]interface{}{"Foo": "bar"},
|
|
|
|
expr: "UnmarshalJSON(json, out, 'a')",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "convert complex struct",
|
|
|
|
json: `{"Foo":"bar","Bar":{"Baz":"baz"},"Bla":["foo","bar"]}`,
|
|
|
|
expectResult: map[string]interface{}{
|
|
|
|
"Foo": "bar",
|
|
|
|
"Bar": map[string]interface{}{
|
|
|
|
"Baz": "baz",
|
|
|
|
},
|
|
|
|
"Bla": []interface{}{"foo", "bar"},
|
|
|
|
},
|
|
|
|
expr: "UnmarshalJSON(json, out, 'a')",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
outMap := make(map[string]interface{})
|
|
|
|
env := map[string]interface{}{
|
|
|
|
"json": test.json,
|
|
|
|
"out": outMap,
|
|
|
|
}
|
|
|
|
vm, err := expr.Compile(test.expr, GetExprOptions(env)...)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-12 07:43:01 +00:00
|
|
|
_, err = expr.Run(vm, env)
|
2024-01-05 14:26:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-12 07:43:01 +00:00
|
|
|
assert.Equal(t, test.expectResult, outMap["a"])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|