mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-21 23:43:27 +01:00
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:
parent
c1cdf50851
commit
744d63abac
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
@ -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"),
|
||||
|
Loading…
Reference in New Issue
Block a user