|
| 1 | +// Copyright 2021 Security Scorecard Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package checks |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/golang/mock/gomock" |
| 23 | + "go.uber.org/zap/zapcore" |
| 24 | + |
| 25 | + "github.com/ossf/scorecard/v4/checker" |
| 26 | + "github.com/ossf/scorecard/v4/clients/githubrepo" |
| 27 | + "github.com/ossf/scorecard/v4/clients/localdir" |
| 28 | + scut "github.com/ossf/scorecard/v4/utests" |
| 29 | +) |
| 30 | + |
| 31 | +func TestBinaryArtifacts(t *testing.T) { |
| 32 | + t.Parallel() |
| 33 | + tests := []struct { |
| 34 | + name string |
| 35 | + inputFolder string |
| 36 | + err error |
| 37 | + expected checker.CheckResult |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "Jar file", |
| 41 | + inputFolder: "file://testdata/binaryartifacts/jars", |
| 42 | + err: nil, |
| 43 | + expected: checker.CheckResult{ |
| 44 | + Score: 9, |
| 45 | + Pass: true, |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "non binary file", |
| 50 | + inputFolder: "file://testdata/licensedir/withlicense", |
| 51 | + err: nil, |
| 52 | + expected: checker.CheckResult{ |
| 53 | + Score: 10, |
| 54 | + Pass: true, |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | + for _, tt := range tests { |
| 59 | + tt := tt // Re-initializing variable so it is not changed while executing the closure below |
| 60 | + t.Run(tt.name, func(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + |
| 63 | + logger, err := githubrepo.NewLogger(zapcore.DebugLevel) |
| 64 | + if err != nil { |
| 65 | + t.Errorf("githubrepo.NewLogger: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + // nolint |
| 69 | + defer logger.Sync() |
| 70 | + |
| 71 | + ctrl := gomock.NewController(t) |
| 72 | + repo, err := localdir.MakeLocalDirRepo(tt.inputFolder) |
| 73 | + |
| 74 | + if !errors.Is(err, tt.err) { |
| 75 | + t.Errorf("MakeLocalDirRepo: %v, expected %v", err, tt.err) |
| 76 | + } |
| 77 | + |
| 78 | + ctx := context.Background() |
| 79 | + |
| 80 | + client := localdir.CreateLocalDirClient(ctx, logger) |
| 81 | + if err := client.InitRepo(repo); err != nil { |
| 82 | + t.Errorf("InitRepo: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + dl := scut.TestDetailLogger{} |
| 86 | + |
| 87 | + req := checker.CheckRequest{ |
| 88 | + Ctx: ctx, |
| 89 | + RepoClient: client, |
| 90 | + Dlogger: &dl, |
| 91 | + } |
| 92 | + |
| 93 | + result := BinaryArtifacts(&req) |
| 94 | + if result.Score != tt.expected.Score { |
| 95 | + t.Errorf("BinaryArtifacts: %v, expected %v for tests %v", result.Score, tt.expected.Score, tt.name) |
| 96 | + } |
| 97 | + if result.Pass != tt.expected.Pass { |
| 98 | + t.Errorf("BinaryArtifacts: %v, expected %v for tests %v", result.Pass, tt.expected.Pass, tt.name) |
| 99 | + } |
| 100 | + |
| 101 | + ctrl.Finish() |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
0 commit comments