Skip to content

Commit

Permalink
🌱 Unit tests for binary_artifact (#1512)
Browse files Browse the repository at this point in the history
  • Loading branch information
naveensrinivasan authored Jan 27, 2022
1 parent 15a204f commit 17467c1
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions checks/raw/binary_artifact_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2021 Security Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package raw

import (
"fmt"
"log"
"os"
"testing"

"github.com/golang/mock/gomock"

mockrepo "github.com/ossf/scorecard/v4/clients/mockclients"
)

// TestBinaryArtifact tests the BinaryArtifact checker.
func TestBinaryArtifacts(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inputFile string
err error
files []string
expect int
}{
{
name: "Jar file",
inputFile: "../testdata/binaryartifacts/jars/aws-java-sdk-core-1.11.571.jar",
err: nil,
files: []string{
"../testdata/binaryartifacts/jars/aws-java-sdk-core-1.11.571.jar",
},
expect: 1,
},
{
name: "non binary file",
inputFile: "../testdata/licensedir/withlicense/LICENSE",
err: nil,
files: []string{
"../testdata/licensedir/withlicense/LICENSE",
},
},
{
name: "non binary file",
inputFile: "../doesnotexist",
err: nil,
files: []string{
"../doesnotexist",
},
},
}
for _, tt := range tests {
tt := tt // Re-initializing variable so it is not changed while executing the closure below
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
mockRepoClient := mockrepo.NewMockRepoClient(ctrl)
mockRepoClient.EXPECT().ListFiles(gomock.Any()).Return(tt.files, nil)
mockRepoClient.EXPECT().GetFileContent(gomock.Any()).DoAndReturn(func(file string) ([]byte, error) {
// This will read the file and return the content
content, err := os.ReadFile(file)
log.Println(os.Getwd())
if err != nil {
return content, fmt.Errorf("%w", err)
}
return content, nil
})

f, err := BinaryArtifacts(mockRepoClient)

if tt.err != nil {
// If we expect an error, make sure it is the same
if err == nil {
t.Errorf("expected error, got nil")
}
} else {
// Check that the expected number of files are returned
if len(f.Files) != tt.expect {
t.Errorf("expected %d files, got %d test %v", tt.expect, len(f.Files), tt.name)
}
}
})
}
}

0 comments on commit 17467c1

Please sign in to comment.