Skip to content

Commit

Permalink
customrules: add new tests to cover all supported languages
Browse files Browse the repository at this point in the history
Update #718

Signed-off-by: Matheus Alcantara <matheus.alcantara@zup.com.br>
  • Loading branch information
matheusalcantarazup committed Nov 16, 2021
1 parent 77c8d12 commit fcedbaf
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 26 deletions.
30 changes: 15 additions & 15 deletions internal/entities/custom_rules/custom_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/ZupIT/horusec-devkit/pkg/enums/severities"
"github.com/ZupIT/horusec-devkit/pkg/utils/logger"
"github.com/ZupIT/horusec-engine/text"
customRulesEnums "github.com/ZupIT/horusec/internal/enums/custom_rules"
customrules "github.com/ZupIT/horusec/internal/enums/custom_rules"
"github.com/ZupIT/horusec/internal/services/engines/csharp"
"github.com/ZupIT/horusec/internal/services/engines/dart"
"github.com/ZupIT/horusec/internal/services/engines/java"
Expand All @@ -41,14 +41,14 @@ import (
)

type CustomRule struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Language languages.Language `json:"language"`
Severity severities.Severity `json:"severity"`
Confidence confidence.Confidence `json:"confidence"`
Type customRulesEnums.MatchType `json:"type"`
Expressions []string `json:"expressions"`
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Language languages.Language `json:"language"`
Severity severities.Severity `json:"severity"`
Confidence confidence.Confidence `json:"confidence"`
Type customrules.MatchType `json:"type"`
Expressions []string `json:"expressions"`
}

func (c *CustomRule) Validate() error {
Expand All @@ -62,20 +62,20 @@ func (c *CustomRule) Validate() error {
severities.Low, severities.Medium, severities.High, severities.Critical)),
validation.Field(&c.Confidence, validation.Required, validation.In(confidence.Low,
confidence.Medium, confidence.High)),
validation.Field(&c.Type, validation.Required, validation.In(customRulesEnums.Regular,
customRulesEnums.OrMatch, customRulesEnums.AndMatch, customRulesEnums.NotMatch)),
validation.Field(&c.Type, validation.Required, validation.In(customrules.Regular,
customrules.OrMatch, customrules.AndMatch, customrules.NotMatch)),
)
}

func (c *CustomRule) GetRuleType() text.MatchType {
switch c.Type {
case customRulesEnums.Regular:
case customrules.Regular:
return text.Regular
case customRulesEnums.OrMatch:
case customrules.OrMatch:
return text.OrMatch
case customRulesEnums.AndMatch:
case customrules.AndMatch:
return text.AndMatch
case customRulesEnums.NotMatch:
case customrules.NotMatch:
return text.NotMatch
}

Expand Down
88 changes: 77 additions & 11 deletions internal/entities/custom_rules/custom_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/ZupIT/horusec-devkit/pkg/enums/confidence"
"github.com/ZupIT/horusec-devkit/pkg/enums/severities"
"github.com/ZupIT/horusec-engine/text"
customRulesEnums "github.com/ZupIT/horusec/internal/enums/custom_rules"
customrulesenum "github.com/ZupIT/horusec/internal/enums/custom_rules"
)

func TestValidate(t *testing.T) {
Expand All @@ -35,15 +35,15 @@ func TestValidate(t *testing.T) {
Description: "test",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customRulesEnums.OrMatch,
Type: customrulesenum.OrMatch,
Expressions: []string{""},
Language: languages.Leaks,
}

assert.NoError(t, customRule.Validate())
})

t.Run("should return error when invalid custom", func(t *testing.T) {
t.Run("should return error when empty custom rule", func(t *testing.T) {
customRule := CustomRule{}
assert.Error(t, customRule.Validate())
})
Expand All @@ -55,7 +55,7 @@ func TestValidate(t *testing.T) {
Description: "test",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customRulesEnums.Regular,
Type: customrulesenum.Regular,
Expressions: []string{""},
Language: languages.Java,
}
Expand All @@ -68,9 +68,9 @@ func TestValidate(t *testing.T) {
Description: "test",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customRulesEnums.Regular,
Type: customrulesenum.Regular,
Expressions: []string{""},
Language: languages.Java,
Language: languages.Leaks,
}
assert.Error(t, customRule.Validate())
})
Expand All @@ -81,18 +81,84 @@ func TestValidate(t *testing.T) {
Description: "test",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customRulesEnums.Regular,
Type: customrulesenum.Regular,
Expressions: []string{""},
Language: languages.Python,
}
assert.Error(t, customRule.Validate())
})
}

func TestValidateAllLanguages(t *testing.T) {
rules := []CustomRule{
{
ID: "HS-CSHARP-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Language: languages.CSharp,
},
{
ID: "HS-DART-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Language: languages.Dart,
},
{
ID: "HS-JAVA-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Language: languages.Java,
},
{
ID: "HS-KOTLIN-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Language: languages.Kotlin,
},
{
ID: "HS-YAML-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Language: languages.Yaml,
},
{
ID: "HS-LEAKS-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Language: languages.Leaks,
},
{
ID: "HS-JAVASCRIPT-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Language: languages.Javascript,
},
{
ID: "HS-NGINX-10000",
Severity: severities.Low,
Confidence: confidence.Low,
Type: customrulesenum.Regular,
Language: languages.Nginx,
},
}

for _, rule := range rules {
err := rule.Validate()
assert.NoError(t, err, "Expected no error for custom rule of language %s: %v", rule.Language, err)
}
}

func TestGetRuleType(t *testing.T) {
t.Run("should return regular type", func(t *testing.T) {
customRule := CustomRule{
Type: customRulesEnums.Regular,
Type: customrulesenum.Regular,
}

assert.Equal(t, text.Regular, customRule.GetRuleType())
Expand All @@ -106,23 +172,23 @@ func TestGetRuleType(t *testing.T) {

t.Run("should return or type", func(t *testing.T) {
customRule := CustomRule{
Type: customRulesEnums.OrMatch,
Type: customrulesenum.OrMatch,
}

assert.Equal(t, text.OrMatch, customRule.GetRuleType())
})

t.Run("should return and type", func(t *testing.T) {
customRule := CustomRule{
Type: customRulesEnums.AndMatch,
Type: customrulesenum.AndMatch,
}

assert.Equal(t, text.AndMatch, customRule.GetRuleType())
})

t.Run("should return not type", func(t *testing.T) {
customRule := CustomRule{
Type: customRulesEnums.NotMatch,
Type: customrulesenum.NotMatch,
}

assert.Equal(t, text.NotMatch, customRule.GetRuleType())
Expand Down

0 comments on commit fcedbaf

Please sign in to comment.