mirror of
https://github.com/ossf/scorecard.git
synced 2024-11-05 05:17:00 +03:00
98 lines
2.6 KiB
Go
98 lines
2.6 KiB
Go
// 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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|