fix: Support hexadecimal integers in conditions (#563)
* 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: add more tests for condiction.evaluate * fix isEqual parsing integers as strings * tests: extend conditions * Test if we can compare equality of hex in JSON * Test if we can have hex/oct/bin in conditions --------- Co-authored-by: TwiN <twin@linux.com>
This commit is contained in:
@ -287,6 +287,111 @@ 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-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-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"),
|
||||
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"),
|
||||
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"),
|
||||
Result: &Result{Body: []byte("{\"data\": \"0o1\"}")},
|
||||
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-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"),
|
||||
Result: &Result{Body: []byte("{\"data\": \"0b1\"}")},
|
||||
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-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"),
|
||||
|
Reference in New Issue
Block a user