2022-12-02 02:08:48 +03:00
|
|
|
// Copyright 2021 OpenSSF Scorecard Authors
|
2022-01-27 21:25:50 +03:00
|
|
|
//
|
|
|
|
// 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 (
|
2024-03-05 04:37:50 +03:00
|
|
|
"io"
|
2022-01-27 21:25:50 +03:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
|
2024-04-13 00:51:50 +03:00
|
|
|
"github.com/ossf/scorecard/v5/checker"
|
|
|
|
"github.com/ossf/scorecard/v5/clients"
|
|
|
|
mockrepo "github.com/ossf/scorecard/v5/clients/mockclients"
|
|
|
|
scut "github.com/ossf/scorecard/v5/utests"
|
2022-01-27 21:25:50 +03:00
|
|
|
)
|
|
|
|
|
2022-07-18 21:42:32 +03:00
|
|
|
func strptr(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:25:50 +03:00
|
|
|
// TestBinaryArtifact tests the BinaryArtifact checker.
|
|
|
|
func TestBinaryArtifacts(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
2022-07-18 21:42:32 +03:00
|
|
|
name string
|
|
|
|
err error
|
|
|
|
files [][]string
|
|
|
|
successfulWorkflowRuns []clients.WorkflowRun
|
|
|
|
commits []clients.Commit
|
|
|
|
getFileContentCount int
|
|
|
|
expect int
|
2022-01-27 21:25:50 +03:00
|
|
|
}{
|
2023-01-06 22:16:30 +03:00
|
|
|
{
|
|
|
|
name: "Wasm file",
|
|
|
|
err: nil,
|
|
|
|
files: [][]string{
|
|
|
|
{"../testdata/binaryartifacts/wasms/simple.wasm"},
|
|
|
|
},
|
|
|
|
getFileContentCount: 1,
|
|
|
|
expect: 1,
|
|
|
|
},
|
2022-01-27 21:25:50 +03:00
|
|
|
{
|
2022-05-03 23:31:51 +03:00
|
|
|
name: "Jar file",
|
|
|
|
err: nil,
|
2022-07-18 21:42:32 +03:00
|
|
|
files: [][]string{
|
|
|
|
{"../testdata/binaryartifacts/jars/aws-java-sdk-core-1.11.571.jar"},
|
2022-01-27 21:25:50 +03:00
|
|
|
},
|
2022-07-18 21:42:32 +03:00
|
|
|
getFileContentCount: 1,
|
|
|
|
expect: 1,
|
2022-01-27 21:25:50 +03:00
|
|
|
},
|
2022-06-23 18:54:55 +03:00
|
|
|
{
|
|
|
|
name: "Mach-O ARM64 executable",
|
|
|
|
err: nil,
|
2022-07-18 21:42:32 +03:00
|
|
|
files: [][]string{
|
|
|
|
{"../testdata/binaryartifacts/executables/darwin-arm64-bt"},
|
2022-06-23 18:54:55 +03:00
|
|
|
},
|
2022-07-18 21:42:32 +03:00
|
|
|
getFileContentCount: 1,
|
|
|
|
expect: 1,
|
2022-06-23 18:54:55 +03:00
|
|
|
},
|
2022-01-27 21:25:50 +03:00
|
|
|
{
|
2022-05-03 23:31:51 +03:00
|
|
|
name: "non binary file",
|
|
|
|
err: nil,
|
2022-07-18 21:42:32 +03:00
|
|
|
files: [][]string{
|
|
|
|
{"../testdata/licensedir/withlicense/LICENSE"},
|
2022-01-27 21:25:50 +03:00
|
|
|
},
|
2022-07-18 21:42:32 +03:00
|
|
|
getFileContentCount: 1,
|
2022-01-27 21:25:50 +03:00
|
|
|
},
|
|
|
|
{
|
2022-05-03 23:31:51 +03:00
|
|
|
name: "non binary file",
|
|
|
|
err: nil,
|
2022-07-18 21:42:32 +03:00
|
|
|
files: [][]string{
|
2024-01-27 02:08:26 +03:00
|
|
|
{"../nonexistent"},
|
2022-01-27 21:25:50 +03:00
|
|
|
},
|
2022-07-18 21:42:32 +03:00
|
|
|
getFileContentCount: 1,
|
2022-01-27 21:25:50 +03:00
|
|
|
},
|
2022-05-03 23:31:51 +03:00
|
|
|
{
|
|
|
|
name: "printable character .lib",
|
|
|
|
err: nil,
|
2022-07-18 21:42:32 +03:00
|
|
|
files: [][]string{
|
|
|
|
{"../testdata/binaryartifacts/printable.lib"},
|
|
|
|
},
|
|
|
|
getFileContentCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gradle-wrapper.jar without verification action",
|
|
|
|
err: nil,
|
|
|
|
files: [][]string{
|
|
|
|
{"../testdata/binaryartifacts/jars/gradle-wrapper.jar"},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
getFileContentCount: 1,
|
|
|
|
expect: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gradle-wrapper.jar with verification action",
|
|
|
|
err: nil,
|
|
|
|
files: [][]string{
|
|
|
|
{"../testdata/binaryartifacts/jars/gradle-wrapper.jar"},
|
|
|
|
{
|
2022-08-19 06:44:18 +03:00
|
|
|
"../testdata/binaryartifacts/workflows/nonverify.yaml",
|
|
|
|
"../testdata/binaryartifacts/workflows/verify.yaml",
|
2022-07-18 21:42:32 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
successfulWorkflowRuns: []clients.WorkflowRun{
|
|
|
|
{
|
|
|
|
HeadSHA: strptr("sha-a"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
commits: []clients.Commit{
|
|
|
|
{
|
|
|
|
SHA: "sha-a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SHA: "sha-old",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getFileContentCount: 3,
|
2023-12-05 11:24:16 +03:00
|
|
|
expect: 1,
|
2022-07-18 21:42:32 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gradle-wrapper.jar with non-verification action",
|
|
|
|
err: nil,
|
|
|
|
files: [][]string{
|
|
|
|
{"../testdata/binaryartifacts/jars/gradle-wrapper.jar"},
|
2022-08-19 06:44:18 +03:00
|
|
|
{"../testdata/binaryartifacts/workflows/nonverify.yaml"},
|
2022-05-03 23:31:51 +03:00
|
|
|
},
|
2022-07-18 21:42:32 +03:00
|
|
|
getFileContentCount: 2,
|
|
|
|
expect: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gradle-wrapper.jar with verification-failing commit",
|
|
|
|
err: nil,
|
|
|
|
files: [][]string{
|
|
|
|
{"../testdata/binaryartifacts/jars/gradle-wrapper.jar"},
|
2022-08-19 06:44:18 +03:00
|
|
|
{"../testdata/binaryartifacts/workflows/verify.yaml"},
|
2022-07-18 21:42:32 +03:00
|
|
|
},
|
|
|
|
successfulWorkflowRuns: []clients.WorkflowRun{
|
|
|
|
{
|
|
|
|
HeadSHA: strptr("sha-old"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
commits: []clients.Commit{
|
|
|
|
{
|
|
|
|
SHA: "sha-a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SHA: "sha-old",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getFileContentCount: 2,
|
|
|
|
expect: 1,
|
|
|
|
},
|
|
|
|
{
|
2024-05-09 21:27:34 +03:00
|
|
|
name: "gradle-wrapper.jar with new verification action",
|
2022-07-18 21:42:32 +03:00
|
|
|
err: nil,
|
|
|
|
files: [][]string{
|
|
|
|
{"../testdata/binaryartifacts/jars/gradle-wrapper.jar"},
|
|
|
|
{
|
2022-08-19 06:44:18 +03:00
|
|
|
"../testdata/binaryartifacts/workflows/nonverify.yaml",
|
2024-05-09 21:27:34 +03:00
|
|
|
"../testdata/binaryartifacts/workflows/verify-new-gradle-name.yaml",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
successfulWorkflowRuns: []clients.WorkflowRun{
|
|
|
|
{
|
|
|
|
HeadSHA: strptr("sha-a"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
commits: []clients.Commit{
|
|
|
|
{
|
|
|
|
SHA: "sha-a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SHA: "sha-old",
|
2022-07-18 21:42:32 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
getFileContentCount: 3,
|
|
|
|
expect: 1,
|
2022-05-03 23:31:51 +03:00
|
|
|
},
|
2022-08-19 06:44:18 +03:00
|
|
|
{
|
|
|
|
name: "gradle-wrapper.jar with no verification action due to bad workflow",
|
|
|
|
err: nil,
|
|
|
|
files: [][]string{
|
|
|
|
{"../testdata/binaryartifacts/jars/gradle-wrapper.jar"},
|
|
|
|
{
|
|
|
|
"../testdata/binaryartifacts/workflows/invalid.yaml",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getFileContentCount: 2,
|
|
|
|
expect: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gradle-wrapper.jar with verification and bad workflow",
|
|
|
|
err: nil,
|
|
|
|
files: [][]string{
|
|
|
|
{"../testdata/binaryartifacts/jars/gradle-wrapper.jar"},
|
|
|
|
{
|
|
|
|
"../testdata/binaryartifacts/workflows/invalid.yaml",
|
|
|
|
"../testdata/binaryartifacts/workflows/verify.yaml",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
successfulWorkflowRuns: []clients.WorkflowRun{
|
|
|
|
{
|
|
|
|
HeadSHA: strptr("sha-a"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
commits: []clients.Commit{
|
|
|
|
{
|
|
|
|
SHA: "sha-a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SHA: "sha-old",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getFileContentCount: 3,
|
2023-12-05 11:24:16 +03:00
|
|
|
expect: 1,
|
2022-08-19 06:44:18 +03:00
|
|
|
},
|
2022-01-27 21:25:50 +03:00
|
|
|
}
|
|
|
|
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)
|
2023-12-05 11:24:16 +03:00
|
|
|
mockRepo := mockrepo.NewMockRepo(ctrl)
|
2022-07-18 21:42:32 +03:00
|
|
|
for _, files := range tt.files {
|
|
|
|
mockRepoClient.EXPECT().ListFiles(gomock.Any()).Return(files, nil)
|
|
|
|
}
|
|
|
|
for i := 0; i < tt.getFileContentCount; i++ {
|
2024-03-05 04:37:50 +03:00
|
|
|
mockRepoClient.EXPECT().GetFileReader(gomock.Any()).DoAndReturn(func(file string) (io.ReadCloser, error) {
|
|
|
|
return os.Open(file)
|
2022-07-18 21:42:32 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if tt.successfulWorkflowRuns != nil {
|
|
|
|
mockRepoClient.EXPECT().ListSuccessfulWorkflowRuns(gomock.Any()).Return(tt.successfulWorkflowRuns, nil)
|
|
|
|
}
|
|
|
|
if tt.commits != nil {
|
|
|
|
mockRepoClient.EXPECT().ListCommits().Return(tt.commits, nil)
|
|
|
|
}
|
2022-01-27 21:25:50 +03:00
|
|
|
|
2023-12-05 11:24:16 +03:00
|
|
|
dl := scut.TestDetailLogger{}
|
|
|
|
c := &checker.CheckRequest{
|
|
|
|
RepoClient: mockRepoClient,
|
|
|
|
Repo: mockRepo,
|
|
|
|
Dlogger: &dl,
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := BinaryArtifacts(c)
|
2022-01-27 21:25:50 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 20:25:26 +03:00
|
|
|
|
|
|
|
func TestBinaryArtifacts_workflow_runs_unsupported(t *testing.T) {
|
2023-12-04 05:25:03 +03:00
|
|
|
t.Parallel()
|
2023-08-23 20:25:26 +03:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
mockRepoClient := mockrepo.NewMockRepoClient(ctrl)
|
2023-12-05 11:24:16 +03:00
|
|
|
mockRepo := mockrepo.NewMockRepo(ctrl)
|
2023-08-23 20:25:26 +03:00
|
|
|
const jarFile = "gradle-wrapper.jar"
|
|
|
|
const verifyWorkflow = ".github/workflows/verify.yaml"
|
|
|
|
files := []string{jarFile, verifyWorkflow}
|
|
|
|
mockRepoClient.EXPECT().ListFiles(gomock.Any()).Return(files, nil).AnyTimes()
|
2024-03-05 04:37:50 +03:00
|
|
|
mockRepoClient.EXPECT().GetFileReader(jarFile).DoAndReturn(func(file string) (io.ReadCloser, error) {
|
|
|
|
return os.Open("../testdata/binaryartifacts/jars/gradle-wrapper.jar")
|
2023-08-23 20:25:26 +03:00
|
|
|
}).AnyTimes()
|
2024-03-05 04:37:50 +03:00
|
|
|
mockRepoClient.EXPECT().GetFileReader(verifyWorkflow).DoAndReturn(func(file string) (io.ReadCloser, error) {
|
|
|
|
return os.Open("../testdata/binaryartifacts/workflows/verify.yaml")
|
2023-08-23 20:25:26 +03:00
|
|
|
}).AnyTimes()
|
|
|
|
|
|
|
|
mockRepoClient.EXPECT().ListSuccessfulWorkflowRuns(gomock.Any()).Return(nil, clients.ErrUnsupportedFeature).AnyTimes()
|
2023-12-05 11:24:16 +03:00
|
|
|
dl := scut.TestDetailLogger{}
|
|
|
|
c := &checker.CheckRequest{
|
|
|
|
RepoClient: mockRepoClient,
|
|
|
|
Repo: mockRepo,
|
|
|
|
Dlogger: &dl,
|
|
|
|
}
|
|
|
|
got, err := BinaryArtifacts(c)
|
2023-08-23 20:25:26 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if len(got.Files) != 1 {
|
|
|
|
t.Errorf("expected 1 file, got %d", len(got.Files))
|
|
|
|
}
|
|
|
|
}
|