Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support hexadecimal integers in conditions #563

Merged
merged 8 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion core/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -304,7 +312,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
Expand Down
105 changes: 105 additions & 0 deletions core/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}")},
TwiN marked this conversation as resolved.
Show resolved Hide resolved
ExpectedSuccess: true,
ExpectedOutput: "[BODY].data > 0",
},
TwiN marked this conversation as resolved.
Show resolved Hide resolved
{
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"),
Expand Down