From 737a28b33da2d2eab19ec913feece6eab1f238bf Mon Sep 17 00:00:00 2001 From: Heitor Pascoal de Bittencourt Date: Wed, 6 Sep 2023 13:34:26 -0300 Subject: [PATCH 1/6] Fix parsing hexadecimal integers in conditions Remove the assumption that JSON contains only decimal integers and instead identify the base from the prefix of the data. `strconv.ParseInt` can identify the base of the integer based on its "prefix": - 0x -> hexadecimal - 0o -> octal - 0b -> binary - decimal otherwise. --- core/condition.go | 2 +- core/condition_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/core/condition.go b/core/condition.go index 6b11d8e9b..c3f42153b 100644 --- a/core/condition.go +++ b/core/condition.go @@ -304,7 +304,7 @@ func sanitizeAndResolveNumerical(list []string, result *Result) (parameters []st if duration, err := time.ParseDuration(element); duration != 0 && err == nil { // If the string is a duration, convert it to milliseconds resolvedNumericalParameters = append(resolvedNumericalParameters, duration.Milliseconds()) - } else if number, err := strconv.ParseInt(element, 10, 64); err != nil { + } else if number, err := strconv.ParseInt(element, 0, 64); err != nil { // It's not an int, so we'll check if it's a float if f, err := strconv.ParseFloat(element, 64); err == nil { // It's a float, but we'll convert it to an int. We're losing precision here, but it's better than diff --git a/core/condition_test.go b/core/condition_test.go index b3cdad569..47b4b22f4 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -287,6 +287,27 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: true, ExpectedOutput: "[BODY].data.id > 0", }, + { + Name: "body-jsonpath-hexadecimal-int-using-greater-than", + Condition: Condition("[BODY].data > 0"), + Result: &Result{Body: []byte("{\"data\": \"0x1\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data > 0", + }, + { + Name: "body-jsonpath-octal-int-using-greater-than", + Condition: Condition("[BODY].data > 0"), + Result: &Result{Body: []byte("{\"data\": \"0o1\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data > 0", + }, + { + Name: "body-jsonpath-binary-int-using-greater-than", + Condition: Condition("[BODY].data > 0"), + Result: &Result{Body: []byte("{\"data\": \"0b1\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data > 0", + }, { Name: "body-jsonpath-complex-int-using-greater-than-failure", Condition: Condition("[BODY].data.id > 5"), From b69058c3f2d59d011744a54c313f90b8916e443a Mon Sep 17 00:00:00 2001 From: Heitor Pascoal de Bittencourt Date: Sun, 24 Sep 2023 17:10:02 -0300 Subject: [PATCH 2/6] core: add more tests for condiction.evaluate --- core/condition_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/core/condition_test.go b/core/condition_test.go index 47b4b22f4..5267cdaad 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -294,6 +294,27 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: true, ExpectedOutput: "[BODY].data > 0", }, + { + Name: "body-jsonpath-hexadecimal-int-len", + Condition: Condition("len([BODY].data) == 3"), + Result: &Result{Body: []byte("{\"data\": \"0x1\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY].data) == 3", + }, + { + Name: "body-jsonpath-hexadecimal-int-greater", + Condition: Condition("[BODY].data >= 1"), + Result: &Result{Body: []byte("{\"data\": \"0x01\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data >= 1", + }, + { + Name: "body-jsonpath-hexadecimal-int-0x01-len", + Condition: Condition("len([BODY].data) == 4"), + Result: &Result{Body: []byte("{\"data\": \"0x01\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY].data) == 4", + }, { Name: "body-jsonpath-octal-int-using-greater-than", Condition: Condition("[BODY].data > 0"), From a8c44ca429a2e658a1843cdb67ea96271a73cbb3 Mon Sep 17 00:00:00 2001 From: Heitor Pascoal de Bittencourt Date: Sat, 30 Sep 2023 17:11:28 -0300 Subject: [PATCH 3/6] fix isEqual parsing integers as strings --- core/condition.go | 8 ++++++++ core/condition_test.go | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/core/condition.go b/core/condition.go index c3f42153b..f5a26cd8f 100644 --- a/core/condition.go +++ b/core/condition.go @@ -224,6 +224,14 @@ func isEqual(first, second string) bool { return false } } + + // test if inputs are integers + firstInt, err1 := strconv.ParseInt(first, 0, 64) + secondInt, err2 := strconv.ParseInt(second, 0, 64) + if err1 == nil && err2 == nil { + return firstInt == secondInt + } + return first == second } diff --git a/core/condition_test.go b/core/condition_test.go index 5267cdaad..fc38c8823 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -294,6 +294,13 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: true, ExpectedOutput: "[BODY].data > 0", }, + { + Name: "body-jsonpath-hexadecimal-int-using-greater-than-without-strings", + Condition: Condition("[BODY].data == 2"), + Result: &Result{Body: []byte("{\"data\": \"0x2\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 2", + }, { Name: "body-jsonpath-hexadecimal-int-len", Condition: Condition("len([BODY].data) == 3"), From 9e636f81013eb133cbdaab8056adf34a11cd3119 Mon Sep 17 00:00:00 2001 From: Heitor Pascoal de Bittencourt Date: Sat, 30 Sep 2023 17:16:15 -0300 Subject: [PATCH 4/6] tests: extend conditions --- core/condition_test.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/core/condition_test.go b/core/condition_test.go index fc38c8823..482a48ba3 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -295,7 +295,7 @@ func TestCondition_evaluate(t *testing.T) { ExpectedOutput: "[BODY].data > 0", }, { - Name: "body-jsonpath-hexadecimal-int-using-greater-than-without-strings", + Name: "body-jsonpath-hexadecimal-int-using-equal", Condition: Condition("[BODY].data == 2"), Result: &Result{Body: []byte("{\"data\": \"0x2\"}")}, ExpectedSuccess: true, @@ -329,6 +329,13 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: true, ExpectedOutput: "[BODY].data > 0", }, + { + Name: "body-jsonpath-octal-int-using-equal", + Condition: Condition("[BODY].data == 2"), + Result: &Result{Body: []byte("{\"data\": \"0o2\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 2", + }, { Name: "body-jsonpath-binary-int-using-greater-than", Condition: Condition("[BODY].data > 0"), @@ -336,6 +343,13 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: true, ExpectedOutput: "[BODY].data > 0", }, + { + Name: "body-jsonpath-binary-int-using-equal", + Condition: Condition("[BODY].data == 2"), + Result: &Result{Body: []byte("{\"data\": \"0b0010\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 2", + }, { Name: "body-jsonpath-complex-int-using-greater-than-failure", Condition: Condition("[BODY].data.id > 5"), From 95f513fc755fe6b8d1190b5a1cf721acb0538014 Mon Sep 17 00:00:00 2001 From: Heitor Pascoal de Bittencourt Date: Sun, 1 Oct 2023 16:02:35 -0300 Subject: [PATCH 5/6] Test if we can compare equality of hex in JSON --- core/condition_test.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/core/condition_test.go b/core/condition_test.go index 482a48ba3..198e11e5b 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -295,12 +295,33 @@ func TestCondition_evaluate(t *testing.T) { ExpectedOutput: "[BODY].data > 0", }, { - Name: "body-jsonpath-hexadecimal-int-using-equal", + Name: "body-jsonpath-hexadecimal-int-using-equal-to-0x1", + Condition: Condition("[BODY].data == 1"), + Result: &Result{Body: []byte("{\"data\": \"0x1\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 1", + }, + { + Name: "body-jsonpath-hexadecimal-int-using-equal-to-0x2", Condition: Condition("[BODY].data == 2"), Result: &Result{Body: []byte("{\"data\": \"0x2\"}")}, ExpectedSuccess: true, ExpectedOutput: "[BODY].data == 2", }, + { + Name: "body-jsonpath-hexadecimal-int-using-equal-to-0xF", + Condition: Condition("[BODY].data == 15"), + Result: &Result{Body: []byte("{\"data\": \"0xF\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 15", + }, + { + Name: "body-jsonpath-hexadecimal-int-using-equal-to-0xC0ff33", + Condition: Condition("[BODY].data == 12648243"), + Result: &Result{Body: []byte("{\"data\": \"0xC0ff33\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 12648243", + }, { Name: "body-jsonpath-hexadecimal-int-len", Condition: Condition("len([BODY].data) == 3"), From d3dcce8e00325b6f1fe8c507e7cd5010e2a80179 Mon Sep 17 00:00:00 2001 From: Heitor Pascoal de Bittencourt Date: Tue, 3 Oct 2023 09:53:50 -0300 Subject: [PATCH 6/6] Test if we can have hex/oct/bin in conditions --- core/condition_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/core/condition_test.go b/core/condition_test.go index 198e11e5b..e4daf2377 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -301,6 +301,13 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: true, ExpectedOutput: "[BODY].data == 1", }, + { + Name: "body-jsonpath-hexadecimal-int-using-equals", + Condition: Condition("[BODY].data == 0x1"), + Result: &Result{Body: []byte("{\"data\": \"0x1\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 0x1", + }, { Name: "body-jsonpath-hexadecimal-int-using-equal-to-0x2", Condition: Condition("[BODY].data == 2"), @@ -357,6 +364,13 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: true, ExpectedOutput: "[BODY].data == 2", }, + { + Name: "body-jsonpath-octal-int-using-equals", + Condition: Condition("[BODY].data == 0o2"), + Result: &Result{Body: []byte("{\"data\": \"0o2\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 0o2", + }, { Name: "body-jsonpath-binary-int-using-greater-than", Condition: Condition("[BODY].data > 0"), @@ -371,6 +385,13 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: true, ExpectedOutput: "[BODY].data == 2", }, + { + Name: "body-jsonpath-binary-int-using-equals", + Condition: Condition("[BODY].data == 0b10"), + Result: &Result{Body: []byte("{\"data\": \"0b0010\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 0b10", + }, { Name: "body-jsonpath-complex-int-using-greater-than-failure", Condition: Condition("[BODY].data.id > 5"),