Skip to content

Commit

Permalink
more coverage
Browse files Browse the repository at this point in the history
Signed-off-by: balteravishay <[email protected]>
  • Loading branch information
balteravishay committed Jan 22, 2025
1 parent fac9482 commit a047af7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
59 changes: 58 additions & 1 deletion probes/memorysafe/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ func Test_Run(t *testing.T) {
expected: []finding.Finding{},
err: nil,
},
// unimplemented languages
{
name: "unimplemented languages",
repoLanguages: []clients.Language{
{Name: clients.Erlang, NumLines: 0},
},
filenames: []string{},
expected: []finding.Finding{},
err: nil,
},
// golang
{
name: "golang - no files",
Expand Down Expand Up @@ -142,6 +152,29 @@ func Test_Run(t *testing.T) {
},
err: nil,
},
{
name: "golang - malformed file with unsafe",
repoLanguages: []clients.Language{
{Name: clients.Go, NumLines: 0},
},
filenames: []string{
"testdata/malformed.go",
"testdata/unsafe.go",
},
expected: []finding.Finding{
{
Probe: Probe,
Message: "Golang code uses the unsafe package",
Outcome: finding.OutcomeFalse,
Remediation: &finding.Remediation{
Text: "Visit the OpenSSF Memory Safety SIG guidance on how to make your project memory safe.\nGuidance for [Memory-Safe By Default Languages](https://github.com/ossf/Memory-Safety/blob/main/docs/best-practice-memory-safe-by-default-languages.md)\nGuidance for [Non Memory-Safe By Default Languages](https://github.com/ossf/Memory-Safety/blob/main/docs/best-practice-non-memory-safe-by-default-languages.md)",
Effort: 2,
},
Location: &finding.Location{Path: "testdata/unsafe.go"},
},
},
err: nil,
},
// csharp
{
name: "C# - no files",
Expand Down Expand Up @@ -237,6 +270,29 @@ func Test_Run(t *testing.T) {
},
err: nil,
},
{
name: "C# - malformed file with unsafe",
repoLanguages: []clients.Language{
{Name: clients.CSharp, NumLines: 0},
},
filenames: []string{
"testdata/malformed.csproj",
"testdata/unsafe.csproj",
},
expected: []finding.Finding{
{
Probe: Probe,
Message: "C# code allows the use of unsafe blocks",
Outcome: finding.OutcomeFalse,
Remediation: &finding.Remediation{
Text: "Visit the OpenSSF Memory Safety SIG guidance on how to make your project memory safe.\nGuidance for [Memory-Safe By Default Languages](https://github.com/ossf/Memory-Safety/blob/main/docs/best-practice-memory-safe-by-default-languages.md)\nGuidance for [Non Memory-Safe By Default Languages](https://github.com/ossf/Memory-Safety/blob/main/docs/best-practice-non-memory-safe-by-default-languages.md)",
Effort: 2,
},
Location: &finding.Location{Path: "testdata/unsafe.csproj"},
},
},
err: nil,
},

// all languages
{
Expand Down Expand Up @@ -394,7 +450,8 @@ func Test_Run(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if diff := cmp.Diff(findings, tt.expected, cmpopts.IgnoreUnexported(finding.Finding{})); diff != "" {
sortFindings := func(a, b finding.Finding) bool { return a.Message < b.Message }
if diff := cmp.Diff(findings, tt.expected, cmpopts.IgnoreUnexported(finding.Finding{}), cmpopts.SortSlices(sortFindings)); diff != "" {
t.Error(diff)
}
})
Expand Down
2 changes: 2 additions & 0 deletions probes/memorysafe/testdata/malformed.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<Project Sdk="Microsoft.NET.Sdk">
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
6 changes: 6 additions & 0 deletions probes/memorysafe/testdata/malformed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

imp ort "unsafe"

fu SafeFoo(inpu string {


0 comments on commit a047af7

Please sign in to comment.