Skip to content

Commit

Permalink
added unit tests to increase current code coverage Checkmarx#1790 (Ch…
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoReigota1 authored Jan 27, 2021
1 parent 7c74c13 commit dbb55c8
Show file tree
Hide file tree
Showing 34 changed files with 3,080 additions and 150 deletions.
4 changes: 4 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ ignore:
- "*/**/*_test.go"
- "**/*_test.go"
- "**/mock/*.go"
- "**/*_easyjson.go"
- "cmd/"
- "internal/console/kics.go"
- "internal/console/scan.go"
22 changes: 22 additions & 0 deletions internal/console/generate_id_test.go
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)))
})
}
131 changes: 131 additions & 0 deletions internal/console/helpers_test.go
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)
})
}
}
4 changes: 3 additions & 1 deletion internal/console/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/spf13/cobra"
)

const currentVersion = "Keeping Infrastructure as Code Secure v1.1.1"

var versionCmd = &cobra.Command{
Use: "version",
Short: "Displays the current version",
Expand All @@ -16,5 +18,5 @@ var versionCmd = &cobra.Command{
}

func getVersion() string {
return "Keeping Infrastructure as Code Secure v1.1.1"
return currentVersion
}
19 changes: 19 additions & 0 deletions internal/console/version_test.go
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)
})
}
Loading

0 comments on commit dbb55c8

Please sign in to comment.