forked from Checkmarx/kics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit tests to increase current code coverage Checkmarx#1790 (Ch…
- Loading branch information
1 parent
7c74c13
commit dbb55c8
Showing
34 changed files
with
3,080 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package console | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/Checkmarx/kics/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestGenerateIDCommand tests kics generate ID command | ||
func TestGenerateIDCommand(t *testing.T) { | ||
t.Run("Tests if generates a valid uuid", func(t *testing.T) { | ||
validUUID := regexp.MustCompile(test.ValidUUIDRegex) | ||
|
||
out, err := test.CaptureCommandOutput(generateIDCmd, nil) | ||
|
||
require.NoError(t, err) | ||
require.True(t, validUUID.MatchString(strings.TrimSpace(out))) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package console | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/Checkmarx/kics/pkg/model" | ||
"github.com/Checkmarx/kics/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var summary = model.Summary{ | ||
Counters: model.Counters{ | ||
ScannedFiles: 1, | ||
ParsedFiles: 1, | ||
FailedToScanFiles: 0, | ||
TotalQueries: 1, | ||
FailedToExecuteQueries: 0, | ||
}, | ||
Queries: []model.VulnerableQuery{ | ||
{ | ||
QueryName: "ALB protocol is HTTP", | ||
QueryID: "de7f5e83-da88-4046-871f-ea18504b1d43", | ||
Severity: "HIGH", | ||
Files: []model.VulnerableFile{ | ||
{ | ||
FileName: "positive.tf", | ||
Line: 25, | ||
IssueType: "MissingAttribute", | ||
SearchKey: "aws_alb_listener[front_end].default_action.redirect", | ||
KeyExpectedValue: "'default_action.redirect.protocol' is equal 'HTTPS'", | ||
KeyActualValue: "'default_action.redirect.protocol' is missing", | ||
Value: nil, | ||
}, | ||
{ | ||
FileName: "positive.tf", | ||
Line: 19, | ||
IssueType: "IncorrectValue", | ||
SearchKey: "aws_alb_listener[front_end].default_action.redirect", | ||
KeyExpectedValue: "'default_action.redirect.protocol' is equal 'HTTPS'", | ||
KeyActualValue: "'default_action.redirect.protocol' is equal 'HTTP'", | ||
Value: nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
SeveritySummary: model.SeveritySummary{ | ||
ScanID: "console", | ||
SeverityCounters: map[model.Severity]int{ | ||
"INFO": 0, | ||
"LOW": 0, | ||
"MEDIUM": 0, | ||
"HIGH": 2, | ||
}, | ||
TotalCounter: 2, | ||
}, | ||
} | ||
|
||
var printTests = []struct { | ||
caseTest model.Summary | ||
expectedResult string | ||
}{ | ||
{ | ||
caseTest: summary, | ||
expectedResult: "Files scanned: 1\n" + | ||
"Parsed files: 1\n" + | ||
"Queries loaded: 1\n" + | ||
"Queries failed to execute: 0\n\n" + | ||
"ALB protocol is HTTP, Severity: HIGH, Results: 2\n" + | ||
"\tpositive.tf:25\n" + | ||
"\tpositive.tf:19\n\n" + | ||
"Results Summary:\n" + | ||
"HIGH: 2\n" + | ||
"MEDIUM: 0\n" + | ||
"LOW: 0\n" + | ||
"INFO: 0\n" + | ||
"TOTAL: 2\n\n", | ||
}, | ||
} | ||
|
||
type jsonCaseTest struct { | ||
summary model.Summary | ||
path string | ||
} | ||
|
||
var jsonTests = []struct { | ||
caseTest jsonCaseTest | ||
expectedResult model.Summary | ||
}{ | ||
{ | ||
caseTest: jsonCaseTest{ | ||
summary: summary, | ||
path: "./testout.json", | ||
}, | ||
expectedResult: summary, | ||
}, | ||
} | ||
|
||
// TestPrintResult tests the functions [printResult()] and all the methods called by them | ||
func TestPrintResult(t *testing.T) { | ||
for idx, testCase := range printTests { | ||
t.Run(fmt.Sprintf("Print test case %d", idx), func(t *testing.T) { | ||
out, err := test.CaptureOutput(func() error { return printResult(&testCase.caseTest) }) | ||
require.NoError(t, err) | ||
require.Equal(t, testCase.expectedResult, out) | ||
}) | ||
} | ||
} | ||
|
||
// TestPrintToJSONFile tests the functions [printToJSONFile()] and all the methods called by them | ||
func TestPrintToJSONFile(t *testing.T) { | ||
for idx, test := range jsonTests { | ||
t.Run(fmt.Sprintf("JSON File test case %d", idx), func(t *testing.T) { | ||
var err error | ||
err = printToJSONFile(test.caseTest.path, test.caseTest.summary) | ||
require.NoError(t, err) | ||
require.FileExists(t, test.caseTest.path) | ||
var jsonResult []byte | ||
jsonResult, err = ioutil.ReadFile(test.caseTest.path) | ||
require.NoError(t, err) | ||
var resultSummary model.Summary | ||
err = json.Unmarshal(jsonResult, &resultSummary) | ||
require.NoError(t, err) | ||
require.Equal(t, test.expectedResult, resultSummary) | ||
os.Remove(test.caseTest.path) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package console | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/Checkmarx/kics/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestVersionCommand tests kics version command | ||
func TestVersionCommand(t *testing.T) { | ||
t.Run("Tests if prints current version", func(t *testing.T) { | ||
out, err := test.CaptureCommandOutput(versionCmd, nil) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, fmt.Sprintf("%s\n", currentVersion), out) | ||
}) | ||
} |
Oops, something went wrong.