Skip to content

Commit

Permalink
fix: update test and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
chen-keinan committed Aug 25, 2021
1 parent dc4e889 commit d332cb8
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 157 deletions.
1 change: 0 additions & 1 deletion internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func Test_InvokeCli(t *testing.T) {
ab.AuditCommand = []string{"aaa"}
ab.EvalExpr = "'$0' != '';"
ab.CommandParams = map[int][]string{}
ab.CmdExprBuilder = utils.UpdateCmdExprParam
ctrl := gomock.NewController(t)
defer ctrl.Finish()
evalCmd := mocks.NewMockCmdEvaluator(ctrl)
Expand Down
1 change: 0 additions & 1 deletion internal/cli/commands/command-helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ func Test_executeTests(t *testing.T) {
ab.AuditCommand = []string{"aaa", "bbb"}
ab.EvalExpr = "'$0' == ''; && '$1' == '';"
ab.CommandParams = map[int][]string{}
ab.CmdExprBuilder = utils.UpdateCmdExprParam
ctrl := gomock.NewController(t)
defer ctrl.Finish()
evalcmd := mocks.NewMockCmdEvaluator(ctrl)
Expand Down
72 changes: 0 additions & 72 deletions internal/cli/commands/lxd-audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package commands

import (
"fmt"
"github.com/Knetic/govaluate"
"github.com/chen-keinan/go-command-eval/eval"
"github.com/chen-keinan/lxd-probe/internal/common"
"github.com/chen-keinan/lxd-probe/internal/logger"
"github.com/chen-keinan/lxd-probe/internal/models"
"github.com/chen-keinan/lxd-probe/internal/reports"
Expand All @@ -16,7 +14,6 @@ import (
"github.com/mitchellh/colorstring"
"github.com/olekukonko/tablewriter"
"os"
"strings"
)

//LxdAudit lxd benchmark object
Expand Down Expand Up @@ -176,75 +173,6 @@ func (ldx *LxdAudit) runAuditTest(at *models.AuditBench) []*models.AuditBench {
return auditRes
}

func (ldx *LxdAudit) addDummyCommandResponse(expr string, index int, n string) string {
if n == "[^\"]\\S*'\n" || n == "" || n == common.EmptyValue {
spExpr := utils.SeparateExpr(expr)
for _, expr := range spExpr {
if expr.Type == common.SingleValue {
if !strings.Contains(expr.Expr, fmt.Sprintf("'$%d'", index)) {
if strings.Contains(expr.Expr, fmt.Sprintf("$%d", index)) {
return common.NotValidNumber
}
}
}
}
return common.EmptyValue
}
return n
}

//IndexValue hold command index and result
type IndexValue struct {
index int
value string
}

//evalExpression expression eval as cartesian product
func (ldx *LxdAudit) evalExpression(at *models.AuditBench,
commandRes []string, commResSize int, permutationArr []string, testFailure int) int {
if len(commandRes) == 0 {
return ldx.evalCommand(at, permutationArr, testFailure)
}
outputs := strings.Split(utils.RemoveNewLineSuffix(commandRes[0]), "\n")
for _, o := range outputs {
permutationArr = append(permutationArr, o)
testFailure = ldx.evalExpression(at, commandRes[1:commResSize], commResSize-1, permutationArr, testFailure)
if testFailure > 0 {
return testFailure
}
permutationArr = permutationArr[:len(permutationArr)-1]
}
return testFailure
}

func (ldx *LxdAudit) evalCommand(at *models.AuditBench, permutationArr []string, testExec int) int {
// build command expression with params
expr := at.CmdExprBuilder(permutationArr, at.EvalExpr)
testExec++
// eval command expression
testSucceeded, err := evalCommandExpr(strings.ReplaceAll(expr, common.EmptyValue, ""))
if err != nil {
ldx.log.Console(fmt.Sprintf("failed to evaluate command expr %s for audit test %s : err %s", expr, at.Name, err.Error()))
}
return testExec - testSucceeded
}

func evalCommandExpr(expr string) (int, error) {
expression, err := govaluate.NewEvaluableExpression(expr)
if err != nil {
return 0, err
}
result, err := expression.Evaluate(nil)
if err != nil {
return 0, err
}
b, ok := result.(bool)
if ok && b {
return 1, nil
}
return 0, nil
}

//Synopsis for help
func (ldx *LxdAudit) Synopsis() string {
return ldx.Help()
Expand Down
83 changes: 0 additions & 83 deletions pkg/utils/stringutil_test.go
Original file line number Diff line number Diff line change
@@ -1,93 +1,10 @@
package utils

import (
"github.com/chen-keinan/lxd-probe/internal/common"
"github.com/stretchr/testify/assert"
"testing"
)

//Test_CheckType_Permission_OK test
func Test_CheckType_Permission_OK(t *testing.T) {
evalExpr := "$0 <= 644"
bench := UpdateCmdExprParam
ti := bench([]string{"700"}, evalExpr)
assert.Equal(t, ti, "700 <= 644")
}

//Test_CheckType_Owner_OK test
func Test_CheckType_Owner_OK(t *testing.T) {
evalExpr := "'$0' == 'root:root';"
bench := UpdateCmdExprParam
ti := bench([]string{"root:root"}, evalExpr)
assert.Equal(t, ti, "'root:root' == 'root:root'")
}

//Test_CheckType_ProcessParam_OK test
func Test_CheckType_ProcessParam_OK(t *testing.T) {
evalExpr := "'$0' == 'false';"
bench := UpdateCmdExprParam
ti := bench([]string{"false"}, evalExpr)
assert.Equal(t, ti, "'false' == 'false'")
}

//Test_CheckType_Multi_ProcessParam_OK test
func Test_CheckType_Multi_ProcessParam_OK(t *testing.T) {
evalExpr := "'RBAC' IN ($0);"
bench := UpdateCmdExprParam
ti := bench([]string{"RBAC,bbb"}, evalExpr)

assert.Equal(t, ti, "'RBAC' IN ('RBAC','bbb')")
}

//Test_CheckType_Multi_ProcessParam_OK test
func Test_CheckType_Multi_ProcessParam_RexOK(t *testing.T) {
evalExpr := "'RBAC' IN ($0);"
bench := UpdateCmdExprParam
ti := bench([]string{common.GrepRegex}, evalExpr)
assert.Equal(t, ti, "'RBAC' == ''")
}

//Test_CheckType_Owner_OK test
func Test_CheckType_Regex_OK(t *testing.T) {
evalExpr := "'$0' == 'root:root';"
bench := UpdateCmdExprParam
ti := bench([]string{common.GrepRegex}, evalExpr)
assert.Equal(t, ti, "'' == 'root:root'")
}

//Test_CheckType_Regex_MultiParamType test
func Test_CheckType_Regex_MultiParamType(t *testing.T) {
evalExpr := "'$0' != 'root:root'; && 'root:root' IN ($0);"
bench := UpdateCmdExprParam
ti := bench([]string{"root:root"}, evalExpr)
assert.Equal(t, ti, "'root:root' != 'root:root' && 'root:root' == 'root:root'")
}

//Test_CheckType_Regex_MultiParamTypeManyValues test
func Test_CheckType_Regex_MultiParamTypeManyValues(t *testing.T) {
evalExpr := "'$0' != 'root:root'; && 'root:root' IN ($0);"
bench := UpdateCmdExprParam
ti := bench([]string{"root:root,abc"}, evalExpr)
assert.Equal(t, ti, "'root:root,abc' != 'root:root' && 'root:root' IN ('root:root','abc')")
}

//Test_CheckType_Regex_DiffParamTypeManyValues test
func Test_CheckType_Regex_DiffParamTypeManyValues(t *testing.T) {
evalExpr := "'$1' == 'kkk'; && '$0' != 'root:root'; && 'root:root' IN ($0);"
bench := UpdateCmdExprParam
ti := bench([]string{"root:root,abc", "kkk"}, evalExpr)
assert.Equal(t, ti, "'kkk' == 'kkk' && 'root:root,abc' != 'root:root' && 'root:root' IN ('root:root','abc')")
}

func Test_ExcludeAuditTest(t *testing.T) {
et := ExcludeAuditTest([]string{"1.2.4"}, "1.2.5")
assert.True(t, et)
et = ExcludeAuditTest([]string{"1.2.4"}, "1.2.4")
assert.False(t, et)
et = ExcludeAuditTest([]string{}, "1.2.4")
assert.False(t, et)
}

//Test_GetSpecificTestsToExecute test
func Test_GetSpecificTestsToExecute(t *testing.T) {
l := GetAuditTestsList("i", "i=1.2.3,1.4.5")
Expand Down

0 comments on commit d332cb8

Please sign in to comment.