|
| 1 | +// Copyright 2022 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 evaluation |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/ossf/scorecard/v4/checker" |
| 21 | + sce "github.com/ossf/scorecard/v4/errors" |
| 22 | + scut "github.com/ossf/scorecard/v4/utests" |
| 23 | +) |
| 24 | + |
| 25 | +func TestDependencyUpdateTool(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + //nolint |
| 28 | + type args struct { |
| 29 | + name string |
| 30 | + dl checker.DetailLogger |
| 31 | + r *checker.DependencyUpdateToolData |
| 32 | + } |
| 33 | + //nolint |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + args args |
| 37 | + want checker.CheckResult |
| 38 | + err bool |
| 39 | + expected scut.TestReturn |
| 40 | + }{ |
| 41 | + { |
| 42 | + name: "DependencyUpdateTool", |
| 43 | + args: args{ |
| 44 | + name: "DependencyUpdateTool", |
| 45 | + dl: &scut.TestDetailLogger{}, |
| 46 | + r: &checker.DependencyUpdateToolData{ |
| 47 | + Tools: []checker.Tool{ |
| 48 | + { |
| 49 | + Name: "DependencyUpdateTool", |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + want: checker.CheckResult{ |
| 55 | + Score: -1, |
| 56 | + }, |
| 57 | + err: false, |
| 58 | + expected: scut.TestReturn{ |
| 59 | + Error: sce.ErrScorecardInternal, |
| 60 | + Score: -1, |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "empty tool list", |
| 65 | + args: args{ |
| 66 | + name: "DependencyUpdateTool", |
| 67 | + dl: &scut.TestDetailLogger{}, |
| 68 | + r: &checker.DependencyUpdateToolData{ |
| 69 | + Tools: []checker.Tool{}, |
| 70 | + }, |
| 71 | + }, |
| 72 | + want: checker.CheckResult{ |
| 73 | + Score: 0, |
| 74 | + Error2: nil, |
| 75 | + }, |
| 76 | + err: false, |
| 77 | + expected: scut.TestReturn{ |
| 78 | + Score: 0, |
| 79 | + NumberOfWarn: 2, |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "Valid tool", |
| 84 | + args: args{ |
| 85 | + name: "DependencyUpdateTool", |
| 86 | + dl: &scut.TestDetailLogger{}, |
| 87 | + r: &checker.DependencyUpdateToolData{ |
| 88 | + Tools: []checker.Tool{ |
| 89 | + { |
| 90 | + Name: "DependencyUpdateTool", |
| 91 | + ConfigFiles: []checker.File{ |
| 92 | + { |
| 93 | + Path: "/etc/dependency-update-tool.conf", |
| 94 | + Snippet: ` |
| 95 | + [dependency-update-tool] |
| 96 | + enabled = true |
| 97 | + `, |
| 98 | + Offset: 0, |
| 99 | + Type: 0, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + want: checker.CheckResult{ |
| 107 | + Score: 10, |
| 108 | + Error2: nil, |
| 109 | + }, |
| 110 | + expected: scut.TestReturn{ |
| 111 | + Error: nil, |
| 112 | + Score: 10, |
| 113 | + NumberOfInfo: 1, |
| 114 | + }, |
| 115 | + err: false, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "more than one tool in the list", |
| 119 | + args: args{ |
| 120 | + name: "DependencyUpdateTool", |
| 121 | + dl: &scut.TestDetailLogger{}, |
| 122 | + r: &checker.DependencyUpdateToolData{ |
| 123 | + Tools: []checker.Tool{ |
| 124 | + { |
| 125 | + Name: "DependencyUpdateTool", |
| 126 | + }, |
| 127 | + { |
| 128 | + Name: "DependencyUpdateTool", |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + want: checker.CheckResult{ |
| 134 | + Score: -1, |
| 135 | + Error2: nil, |
| 136 | + }, |
| 137 | + expected: scut.TestReturn{ |
| 138 | + Error: sce.ErrScorecardInternal, |
| 139 | + Score: -1, |
| 140 | + }, |
| 141 | + err: false, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "Nil r", |
| 145 | + args: args{ |
| 146 | + name: "nil r", |
| 147 | + dl: &scut.TestDetailLogger{}, |
| 148 | + }, |
| 149 | + want: checker.CheckResult{ |
| 150 | + Score: -1, |
| 151 | + Error2: nil, |
| 152 | + }, |
| 153 | + expected: scut.TestReturn{ |
| 154 | + Error: sce.ErrScorecardInternal, |
| 155 | + Score: -1, |
| 156 | + }, |
| 157 | + err: false, |
| 158 | + }, |
| 159 | + } |
| 160 | + for _, tt := range tests { |
| 161 | + tt := tt |
| 162 | + t.Run(tt.name, func(t *testing.T) { |
| 163 | + t.Parallel() |
| 164 | + |
| 165 | + dl := scut.TestDetailLogger{} |
| 166 | + got := DependencyUpdateTool(tt.args.name, &dl, tt.args.r) |
| 167 | + if tt.want.Score != got.Score { |
| 168 | + t.Errorf("DependencyUpdateTool() got Score = %v, want %v for %v", got.Score, tt.want.Score, tt.name) |
| 169 | + } |
| 170 | + if tt.err && got.Error2 == nil { |
| 171 | + t.Errorf("DependencyUpdateTool() error = %v, want %v for %v", got.Error2, tt.want.Error2, tt.name) |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + if !scut.ValidateTestReturn(t, tt.name, &tt.expected, &got, &dl) { |
| 176 | + t.Fatalf(tt.name) |
| 177 | + } |
| 178 | + }) |
| 179 | + } |
| 180 | +} |
0 commit comments