Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow usage of wildcards in talismarc file (#196)
* Check scan required based on wildcard in talismanrc * Introduce a interfaces for utility and checksumcalculator * Inject checksumCalculator to the checksum comparator * Inject ChecksumCompare to Detectors
- Loading branch information
Showing
21 changed files
with
291 additions
and
108 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
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 |
---|---|---|
@@ -1,20 +1,52 @@ | ||
package detector | ||
|
||
import ( | ||
"talisman/utility" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
"talisman/gitrepo" | ||
mockchecksumcalculator "talisman/internal/mock/checksumcalculator" | ||
mockutility "talisman/internal/mock/utility" | ||
"talisman/talismanrc" | ||
"testing" | ||
) | ||
|
||
func TestShouldReturnCorrectFileHash(t *testing.T) { | ||
checksumSomeFile := utility.CollectiveSHA256Hash([]string{"some_file.pem"}) | ||
checksumTestSomeFile := utility.CollectiveSHA256Hash([]string{"test/some_file.pem"}) | ||
assert.Equal(t, checksumSomeFile, "87139cc4d975333b25b6275f97680604add51b84eb8f4a3b9dcbbc652e6f27ac", "Should be equal to some_file.pem hash value") | ||
assert.Equal(t, checksumTestSomeFile, "25bd31a28bf9d4e06327f1c4a5cab2260574ae508803f66adcc393350e994866", "Should be equal to test/some_file.pem hash value") | ||
} | ||
func TestChecksumCompare_IsScanNotRequired(t *testing.T) { | ||
|
||
t.Run("should return false if talismanrc is empty", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
mockSHA256Hasher := mockutility.NewMockSHA256Hasher(ctrl) | ||
ignoreConfig := talismanrc.NewTalismanRC(nil) | ||
cc := NewChecksumCompare(nil, mockSHA256Hasher, ignoreConfig) | ||
addition := gitrepo.Addition{Path: "some.txt"} | ||
mockSHA256Hasher.EXPECT().CollectiveSHA256Hash([]string{string(addition.Path)}).Return("somesha") | ||
|
||
required := cc.IsScanNotRequired(addition) | ||
|
||
assert.False(t, required) | ||
}) | ||
|
||
t.Run("should loop through talismanrc configs", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
mockSHA256Hasher := mockutility.NewMockSHA256Hasher(ctrl) | ||
checksumCalculator := mockchecksumcalculator.NewMockChecksumCalculator(ctrl) | ||
ignoreConfig := talismanrc.TalismanRC{ | ||
FileIgnoreConfig: []talismanrc.FileIgnoreConfig{ | ||
{ | ||
FileName: "some.txt", | ||
Checksum: "sha1", | ||
}, | ||
}, | ||
} | ||
cc := NewChecksumCompare(checksumCalculator, mockSHA256Hasher, &ignoreConfig) | ||
addition := gitrepo.Addition{Name: "some.txt",} | ||
mockSHA256Hasher.EXPECT().CollectiveSHA256Hash([]string{string(addition.Path)}).Return("somesha") | ||
checksumCalculator.EXPECT().CalculateCollectiveChecksumForPattern("some.txt").Return("sha1") | ||
|
||
required := cc.IsScanNotRequired(addition) | ||
|
||
assert.True(t, required) | ||
}) | ||
|
||
func TestShouldReturnEmptyFileHashWhenNoPathsPassed(t *testing.T) { | ||
checksum := utility.CollectiveSHA256Hash([]string{}) | ||
assert.Equal(t, checksum, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "Should be equal to empty hash value when no paths passed") | ||
} |
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
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
Oops, something went wrong.