mirror of
https://github.com/aquasecurity/kube-bench.git
synced 2025-01-31 01:51:00 +00:00
* issue #363: starts unit tests for Test Comparison. * issue #363: Adds tests for "eq" operation * changes test result message * issue #363: Adds tests for "noteq" operation * issue #363: Adds tests for "gt" operation * issue #363: Adds tests for "lt" operation * issue #363: Adds tests for "gte" operation * issue #363: Adds tests for "lte" operation * issue #363: Adds tests for "has" operation * issue #363: Adds tests for "nothave" operation * issue #363: Adds tests for "regex" operation
This commit is contained in:
parent
7c97f6a490
commit
dab5e92bb5
125
check/test.go
125
check/test.go
@ -122,63 +122,7 @@ func (t *testItem) execute(s string) *testOutput {
|
||||
}
|
||||
}
|
||||
|
||||
expectedResultPattern := ""
|
||||
switch t.Compare.Op {
|
||||
case "eq":
|
||||
expectedResultPattern = "'%s' is equal to '%s'"
|
||||
value := strings.ToLower(flagVal)
|
||||
// Do case insensitive comparaison for booleans ...
|
||||
if value == "false" || value == "true" {
|
||||
result.testResult = value == t.Compare.Value
|
||||
} else {
|
||||
result.testResult = flagVal == t.Compare.Value
|
||||
}
|
||||
|
||||
case "noteq":
|
||||
expectedResultPattern = "'%s' is not equal to '%s'"
|
||||
value := strings.ToLower(flagVal)
|
||||
// Do case insensitive comparaison for booleans ...
|
||||
if value == "false" || value == "true" {
|
||||
result.testResult = !(value == t.Compare.Value)
|
||||
} else {
|
||||
result.testResult = !(flagVal == t.Compare.Value)
|
||||
}
|
||||
|
||||
case "gt":
|
||||
expectedResultPattern = "%s is greater then %s"
|
||||
a, b := toNumeric(flagVal, t.Compare.Value)
|
||||
result.testResult = a > b
|
||||
|
||||
case "gte":
|
||||
expectedResultPattern = "%s is greater or equal to %s"
|
||||
a, b := toNumeric(flagVal, t.Compare.Value)
|
||||
result.testResult = a >= b
|
||||
|
||||
case "lt":
|
||||
expectedResultPattern = "%s is lower then %s"
|
||||
a, b := toNumeric(flagVal, t.Compare.Value)
|
||||
result.testResult = a < b
|
||||
|
||||
case "lte":
|
||||
expectedResultPattern = "%s is lower or equal to %s"
|
||||
a, b := toNumeric(flagVal, t.Compare.Value)
|
||||
result.testResult = a <= b
|
||||
|
||||
case "has":
|
||||
expectedResultPattern = "'%s' has '%s'"
|
||||
result.testResult = strings.Contains(flagVal, t.Compare.Value)
|
||||
|
||||
case "nothave":
|
||||
expectedResultPattern = " '%s' not have '%s'"
|
||||
result.testResult = !strings.Contains(flagVal, t.Compare.Value)
|
||||
|
||||
case "regex":
|
||||
expectedResultPattern = " '%s' matched by '%s'"
|
||||
opRe := regexp.MustCompile(t.Compare.Value)
|
||||
result.testResult = opRe.MatchString(flagVal)
|
||||
}
|
||||
|
||||
result.ExpectedResult = fmt.Sprintf(expectedResultPattern, t.Flag, t.Compare.Value)
|
||||
result.ExpectedResult, result.testResult = compareOp(t.Compare.Op, flagVal, t.Compare.Value)
|
||||
} else {
|
||||
result.ExpectedResult = fmt.Sprintf("'%s' is present", t.Flag)
|
||||
result.testResult = isset
|
||||
@ -191,6 +135,73 @@ func (t *testItem) execute(s string) *testOutput {
|
||||
return result
|
||||
}
|
||||
|
||||
func compareOp(tCompareOp string, flagVal string, tCompareValue string) (string, bool) {
|
||||
|
||||
expectedResultPattern := ""
|
||||
testResult := false
|
||||
|
||||
switch tCompareOp {
|
||||
case "eq":
|
||||
expectedResultPattern = "'%s' is equal to '%s'"
|
||||
value := strings.ToLower(flagVal)
|
||||
// Do case insensitive comparaison for booleans ...
|
||||
if value == "false" || value == "true" {
|
||||
testResult = value == tCompareValue
|
||||
} else {
|
||||
testResult = flagVal == tCompareValue
|
||||
}
|
||||
|
||||
case "noteq":
|
||||
expectedResultPattern = "'%s' is not equal to '%s'"
|
||||
value := strings.ToLower(flagVal)
|
||||
// Do case insensitive comparaison for booleans ...
|
||||
if value == "false" || value == "true" {
|
||||
testResult = !(value == tCompareValue)
|
||||
} else {
|
||||
testResult = !(flagVal == tCompareValue)
|
||||
}
|
||||
|
||||
case "gt":
|
||||
expectedResultPattern = "%s is greater than %s"
|
||||
a, b := toNumeric(flagVal, tCompareValue)
|
||||
testResult = a > b
|
||||
|
||||
case "gte":
|
||||
expectedResultPattern = "%s is greater or equal to %s"
|
||||
a, b := toNumeric(flagVal, tCompareValue)
|
||||
testResult = a >= b
|
||||
|
||||
case "lt":
|
||||
expectedResultPattern = "%s is lower than %s"
|
||||
a, b := toNumeric(flagVal, tCompareValue)
|
||||
testResult = a < b
|
||||
|
||||
case "lte":
|
||||
expectedResultPattern = "%s is lower or equal to %s"
|
||||
a, b := toNumeric(flagVal, tCompareValue)
|
||||
testResult = a <= b
|
||||
|
||||
case "has":
|
||||
expectedResultPattern = "'%s' has '%s'"
|
||||
testResult = strings.Contains(flagVal, tCompareValue)
|
||||
|
||||
case "nothave":
|
||||
expectedResultPattern = " '%s' not have '%s'"
|
||||
testResult = !strings.Contains(flagVal, tCompareValue)
|
||||
|
||||
case "regex":
|
||||
expectedResultPattern = " '%s' matched by '%s'"
|
||||
opRe := regexp.MustCompile(tCompareValue)
|
||||
testResult = opRe.MatchString(flagVal)
|
||||
}
|
||||
|
||||
if expectedResultPattern == "" {
|
||||
return expectedResultPattern, testResult
|
||||
}
|
||||
|
||||
return fmt.Sprintf(expectedResultPattern, flagVal, tCompareValue), testResult
|
||||
}
|
||||
|
||||
func unmarshal(s string, jsonInterface *interface{}) error {
|
||||
data := []byte(s)
|
||||
err := json.Unmarshal(data, jsonInterface)
|
||||
|
@ -322,3 +322,222 @@ func TestExecuteJSONPath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareOp(t *testing.T) {
|
||||
cases := []struct {
|
||||
label string
|
||||
op string
|
||||
flagVal string
|
||||
compareValue string
|
||||
expectedResultPattern string
|
||||
testResult bool
|
||||
}{
|
||||
// Test Op not matching
|
||||
{label: "empty - op", op: "", flagVal: "", compareValue: "", expectedResultPattern: "", testResult: false},
|
||||
{label: "op=blah", op: "blah", flagVal: "foo", compareValue: "bar", expectedResultPattern: "", testResult: false},
|
||||
|
||||
// Test Op "eq"
|
||||
{label: "op=eq, both empty", op: "eq", flagVal: "", compareValue: "", expectedResultPattern: "'' is equal to ''", testResult: true},
|
||||
|
||||
{label: "op=eq, true==true", op: "eq", flagVal: "true",
|
||||
compareValue: "true",
|
||||
expectedResultPattern: "'true' is equal to 'true'",
|
||||
testResult: true},
|
||||
|
||||
{label: "op=eq, false==false", op: "eq", flagVal: "false",
|
||||
compareValue: "false",
|
||||
expectedResultPattern: "'false' is equal to 'false'",
|
||||
testResult: true},
|
||||
|
||||
{label: "op=eq, false==true", op: "eq", flagVal: "false",
|
||||
compareValue: "true",
|
||||
expectedResultPattern: "'false' is equal to 'true'",
|
||||
testResult: false},
|
||||
|
||||
{label: "op=eq, strings match", op: "eq", flagVal: "KubeletConfiguration",
|
||||
compareValue: "KubeletConfiguration",
|
||||
expectedResultPattern: "'KubeletConfiguration' is equal to 'KubeletConfiguration'",
|
||||
testResult: true},
|
||||
|
||||
{label: "op=eq, flagVal=empty", op: "eq", flagVal: "",
|
||||
compareValue: "KubeletConfiguration",
|
||||
expectedResultPattern: "'' is equal to 'KubeletConfiguration'",
|
||||
testResult: false},
|
||||
|
||||
{label: "op=eq, compareValue=empty", op: "eq", flagVal: "KubeletConfiguration",
|
||||
compareValue: "",
|
||||
expectedResultPattern: "'KubeletConfiguration' is equal to ''",
|
||||
testResult: false},
|
||||
|
||||
// Test Op "noteq"
|
||||
{label: "op=noteq, both empty", op: "noteq", flagVal: "",
|
||||
compareValue: "", expectedResultPattern: "'' is not equal to ''",
|
||||
testResult: false},
|
||||
|
||||
{label: "op=noteq, true!=true", op: "noteq", flagVal: "true",
|
||||
compareValue: "true",
|
||||
expectedResultPattern: "'true' is not equal to 'true'",
|
||||
testResult: false},
|
||||
|
||||
{label: "op=noteq, false!=false", op: "noteq", flagVal: "false",
|
||||
compareValue: "false",
|
||||
expectedResultPattern: "'false' is not equal to 'false'",
|
||||
testResult: false},
|
||||
|
||||
{label: "op=noteq, false!=true", op: "noteq", flagVal: "false",
|
||||
compareValue: "true",
|
||||
expectedResultPattern: "'false' is not equal to 'true'",
|
||||
testResult: true},
|
||||
|
||||
{label: "op=noteq, strings match", op: "noteq", flagVal: "KubeletConfiguration",
|
||||
compareValue: "KubeletConfiguration",
|
||||
expectedResultPattern: "'KubeletConfiguration' is not equal to 'KubeletConfiguration'",
|
||||
testResult: false},
|
||||
|
||||
{label: "op=noteq, flagVal=empty", op: "noteq", flagVal: "",
|
||||
compareValue: "KubeletConfiguration",
|
||||
expectedResultPattern: "'' is not equal to 'KubeletConfiguration'",
|
||||
testResult: true},
|
||||
|
||||
{label: "op=noteq, compareValue=empty", op: "noteq", flagVal: "KubeletConfiguration",
|
||||
compareValue: "",
|
||||
expectedResultPattern: "'KubeletConfiguration' is not equal to ''",
|
||||
testResult: true},
|
||||
|
||||
// Test Op "gt"
|
||||
// TODO: test for non-numeric values.
|
||||
// toNumeric function currently uses os.Exit, which stops tests.
|
||||
// {label: "op=gt, both empty", op: "gt", flagVal: "",
|
||||
// compareValue: "", expectedResultPattern: "'' is greater than ''",
|
||||
// testResult: true},
|
||||
{label: "op=gt, 0 > 0", op: "gt", flagVal: "0",
|
||||
compareValue: "0", expectedResultPattern: "0 is greater than 0",
|
||||
testResult: false},
|
||||
{label: "op=gt, 4 > 5", op: "gt", flagVal: "4",
|
||||
compareValue: "5", expectedResultPattern: "4 is greater than 5",
|
||||
testResult: false},
|
||||
{label: "op=gt, 5 > 4", op: "gt", flagVal: "5",
|
||||
compareValue: "4", expectedResultPattern: "5 is greater than 4",
|
||||
testResult: true},
|
||||
{label: "op=gt, 5 > 5", op: "gt", flagVal: "5",
|
||||
compareValue: "5", expectedResultPattern: "5 is greater than 5",
|
||||
testResult: false},
|
||||
|
||||
// Test Op "lt"
|
||||
// TODO: test for non-numeric values.
|
||||
// toNumeric function currently uses os.Exit, which stops tests.
|
||||
// {label: "op=lt, both empty", op: "lt", flagVal: "",
|
||||
// compareValue: "", expectedResultPattern: "'' is lower than ''",
|
||||
// testResult: true},
|
||||
{label: "op=gt, 0 < 0", op: "lt", flagVal: "0",
|
||||
compareValue: "0", expectedResultPattern: "0 is lower than 0",
|
||||
testResult: false},
|
||||
{label: "op=gt, 4 < 5", op: "lt", flagVal: "4",
|
||||
compareValue: "5", expectedResultPattern: "4 is lower than 5",
|
||||
testResult: true},
|
||||
{label: "op=gt, 5 < 4", op: "lt", flagVal: "5",
|
||||
compareValue: "4", expectedResultPattern: "5 is lower than 4",
|
||||
testResult: false},
|
||||
{label: "op=gt, 5 < 5", op: "lt", flagVal: "5",
|
||||
compareValue: "5", expectedResultPattern: "5 is lower than 5",
|
||||
testResult: false},
|
||||
|
||||
// Test Op "gte"
|
||||
// TODO: test for non-numeric values.
|
||||
// toNumeric function currently uses os.Exit, which stops tests.
|
||||
// {label: "op=gt, both empty", op: "gte", flagVal: "",
|
||||
// compareValue: "", expectedResultPattern: "'' is greater or equal to ''",
|
||||
// testResult: true},
|
||||
{label: "op=gt, 0 >= 0", op: "gte", flagVal: "0",
|
||||
compareValue: "0", expectedResultPattern: "0 is greater or equal to 0",
|
||||
testResult: true},
|
||||
{label: "op=gt, 4 >= 5", op: "gte", flagVal: "4",
|
||||
compareValue: "5", expectedResultPattern: "4 is greater or equal to 5",
|
||||
testResult: false},
|
||||
{label: "op=gt, 5 >= 4", op: "gte", flagVal: "5",
|
||||
compareValue: "4", expectedResultPattern: "5 is greater or equal to 4",
|
||||
testResult: true},
|
||||
{label: "op=gt, 5 >= 5", op: "gte", flagVal: "5",
|
||||
compareValue: "5", expectedResultPattern: "5 is greater or equal to 5",
|
||||
testResult: true},
|
||||
|
||||
// Test Op "lte"
|
||||
// TODO: test for non-numeric values.
|
||||
// toNumeric function currently uses os.Exit, which stops tests.
|
||||
// {label: "op=gt, both empty", op: "lte", flagVal: "",
|
||||
// compareValue: "", expectedResultPattern: "'' is lower or equal to ''",
|
||||
// testResult: true},
|
||||
{label: "op=gt, 0 <= 0", op: "lte", flagVal: "0",
|
||||
compareValue: "0", expectedResultPattern: "0 is lower or equal to 0",
|
||||
testResult: true},
|
||||
{label: "op=gt, 4 <= 5", op: "lte", flagVal: "4",
|
||||
compareValue: "5", expectedResultPattern: "4 is lower or equal to 5",
|
||||
testResult: true},
|
||||
{label: "op=gt, 5 <= 4", op: "lte", flagVal: "5",
|
||||
compareValue: "4", expectedResultPattern: "5 is lower or equal to 4",
|
||||
testResult: false},
|
||||
{label: "op=gt, 5 <= 5", op: "lte", flagVal: "5",
|
||||
compareValue: "5", expectedResultPattern: "5 is lower or equal to 5",
|
||||
testResult: true},
|
||||
|
||||
// Test Op "has"
|
||||
{label: "op=gt, both empty", op: "has", flagVal: "",
|
||||
compareValue: "", expectedResultPattern: "'' has ''",
|
||||
testResult: true},
|
||||
{label: "op=gt, flagVal=empty", op: "has", flagVal: "",
|
||||
compareValue: "blah", expectedResultPattern: "'' has 'blah'",
|
||||
testResult: false},
|
||||
{label: "op=gt, compareValue=empty", op: "has", flagVal: "blah",
|
||||
compareValue: "", expectedResultPattern: "'blah' has ''",
|
||||
testResult: true},
|
||||
{label: "op=gt, 'blah' has 'la'", op: "has", flagVal: "blah",
|
||||
compareValue: "la", expectedResultPattern: "'blah' has 'la'",
|
||||
testResult: true},
|
||||
{label: "op=gt, 'blah' has 'LA'", op: "has", flagVal: "blah",
|
||||
compareValue: "LA", expectedResultPattern: "'blah' has 'LA'",
|
||||
testResult: false},
|
||||
{label: "op=gt, 'blah' has 'lo'", op: "has", flagVal: "blah",
|
||||
compareValue: "lo", expectedResultPattern: "'blah' has 'lo'",
|
||||
testResult: false},
|
||||
|
||||
// Test Op "nothave"
|
||||
{label: "op=gt, both empty", op: "nothave", flagVal: "",
|
||||
compareValue: "", expectedResultPattern: " '' not have ''",
|
||||
testResult: false},
|
||||
{label: "op=gt, flagVal=empty", op: "nothave", flagVal: "",
|
||||
compareValue: "blah", expectedResultPattern: " '' not have 'blah'",
|
||||
testResult: true},
|
||||
{label: "op=gt, compareValue=empty", op: "nothave", flagVal: "blah",
|
||||
compareValue: "", expectedResultPattern: " 'blah' not have ''",
|
||||
testResult: false},
|
||||
{label: "op=gt, 'blah' not have 'la'", op: "nothave", flagVal: "blah",
|
||||
compareValue: "la", expectedResultPattern: " 'blah' not have 'la'",
|
||||
testResult: false},
|
||||
{label: "op=gt, 'blah' not have 'LA'", op: "nothave", flagVal: "blah",
|
||||
compareValue: "LA", expectedResultPattern: " 'blah' not have 'LA'",
|
||||
testResult: true},
|
||||
{label: "op=gt, 'blah' not have 'lo'", op: "nothave", flagVal: "blah",
|
||||
compareValue: "lo", expectedResultPattern: " 'blah' not have 'lo'",
|
||||
testResult: true},
|
||||
|
||||
// Test Op "regex"
|
||||
{label: "op=gt, both empty", op: "regex", flagVal: "",
|
||||
compareValue: "", expectedResultPattern: " '' matched by ''",
|
||||
testResult: true},
|
||||
{label: "op=gt, flagVal=empty", op: "regex", flagVal: "",
|
||||
compareValue: "blah", expectedResultPattern: " '' matched by 'blah'",
|
||||
testResult: false},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
expectedResultPattern, testResult := compareOp(c.op, c.flagVal, c.compareValue)
|
||||
|
||||
if expectedResultPattern != c.expectedResultPattern {
|
||||
t.Errorf("'expectedResultPattern' did not match - label: %q op: %q expected 'expectedResultPattern':%q got:%q\n", c.label, c.op, c.expectedResultPattern, expectedResultPattern)
|
||||
}
|
||||
|
||||
if testResult != c.testResult {
|
||||
t.Errorf("'testResult' did not match - label: %q op: %q expected 'testResult':%t got:%t\n", c.label, c.op, c.testResult, testResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user