|
1 | 1 | package evalutation |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "github.com/gobwas/glob" |
| 5 | + "github.com/google/go-cmp/cmp" |
4 | 6 | "testing" |
5 | 7 |
|
6 | 8 | "github.com/anchore/grant/grant" |
@@ -43,3 +45,154 @@ func Test_NewLicenseEvaluations(t *testing.T) { |
43 | 45 | func fixtureCase(fixturePath string) []grant.Case { |
44 | 46 | return grant.NewCases(fixturePath) |
45 | 47 | } |
| 48 | + |
| 49 | +func Test_checkLicense(t *testing.T) { |
| 50 | + tests := []struct { |
| 51 | + name string |
| 52 | + config EvaluationConfig |
| 53 | + license grant.License |
| 54 | + wants struct { |
| 55 | + Pass bool |
| 56 | + Reasons []Reason |
| 57 | + } |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "should reject denied licenses when SPDX expressions and CheckNON SPDX is False", |
| 61 | + license: grant.License{ID: "MIT", SPDXExpression: "MIT", LicenseID: "MIT"}, |
| 62 | + // Only allow OSI licenses. |
| 63 | + config: EvaluationConfig{CheckNonSPDX: false, Policy: grant.DefaultPolicy().SetMatchNonSPDX(false)}, |
| 64 | + wants: struct { |
| 65 | + Pass bool |
| 66 | + Reasons []Reason |
| 67 | + }{ |
| 68 | + Pass: false, |
| 69 | + Reasons: []Reason{{ |
| 70 | + Detail: ReasonLicenseDeniedPolicy, |
| 71 | + RuleName: "default-deny-all", |
| 72 | + }}, |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "should reject denied licenses when CheckNonSPDX is also true", |
| 77 | + license: grant.License{Name: "foobar"}, |
| 78 | + // Only allow OSI licenses. |
| 79 | + config: EvaluationConfig{CheckNonSPDX: true, Policy: grant.DefaultPolicy().SetMatchNonSPDX(true)}, |
| 80 | + wants: struct { |
| 81 | + Pass bool |
| 82 | + Reasons []Reason |
| 83 | + }{ |
| 84 | + Pass: false, |
| 85 | + Reasons: []Reason{{ |
| 86 | + Detail: ReasonLicenseDeniedPolicy, |
| 87 | + RuleName: "default-deny-all", |
| 88 | + }}, |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "non-OSI approved licenses should be denied when EvaluationConfig.OsiApproved is true", |
| 93 | + license: grant.License{ |
| 94 | + IsOsiApproved: false, |
| 95 | + LicenseID: "AGPL-1.0-only", |
| 96 | + SPDXExpression: "AGPL-1.0-only", |
| 97 | + }, |
| 98 | + // Only allow OSI licenses. |
| 99 | + config: EvaluationConfig{OsiApproved: true}, |
| 100 | + wants: struct { |
| 101 | + Pass bool |
| 102 | + Reasons []Reason |
| 103 | + }{ |
| 104 | + Pass: false, |
| 105 | + Reasons: []Reason{{ |
| 106 | + Detail: ReasonLicenseDeniedOSI, |
| 107 | + RuleName: RuleNameNotOSIApproved, |
| 108 | + }}, |
| 109 | + }, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "non-OSI approved licenses should be allowed when it's not an SPDX expression", |
| 113 | + license: grant.License{ |
| 114 | + IsOsiApproved: false, |
| 115 | + // Non-SPDX license |
| 116 | + Name: "AGPL-1.0-only", |
| 117 | + }, |
| 118 | + // Only allow OSI licenses. |
| 119 | + config: EvaluationConfig{OsiApproved: true}, |
| 120 | + wants: struct { |
| 121 | + Pass bool |
| 122 | + Reasons []Reason |
| 123 | + }{ |
| 124 | + Pass: true, |
| 125 | + Reasons: []Reason{{Detail: ReasonLicenseAllowed}}, |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "non-OSI approved licenses should be allowed when EvaluationConfig.OsiApproved is false", |
| 130 | + license: grant.License{ |
| 131 | + IsOsiApproved: false, |
| 132 | + LicenseID: "AGPL-1.0-only", |
| 133 | + SPDXExpression: "AGPL-1.0-only", |
| 134 | + }, |
| 135 | + config: EvaluationConfig{}, |
| 136 | + wants: struct { |
| 137 | + Pass bool |
| 138 | + Reasons []Reason |
| 139 | + }{ |
| 140 | + Pass: true, |
| 141 | + Reasons: []Reason{{Detail: ReasonLicenseAllowed}}, |
| 142 | + }, |
| 143 | + }, |
| 144 | + { |
| 145 | + // Verifies rules are evaluated from first to last. |
| 146 | + name: "A 'Deny' rule preceding a 'Deny' rule should always take precedence", |
| 147 | + license: grant.License{LicenseID: "BSD-3-Clause", SPDXExpression: "BSD-3-Clause"}, |
| 148 | + config: EvaluationConfig{ |
| 149 | + Policy: grant.Policy{ |
| 150 | + Rules: []grant.Rule{ |
| 151 | + { |
| 152 | + Name: "allow-bsd-licenses", |
| 153 | + Glob: glob.MustCompile("bsd-*"), |
| 154 | + Exceptions: []glob.Glob{}, |
| 155 | + Mode: grant.Allow, |
| 156 | + Reason: "BSD licenses are allowed", |
| 157 | + }, |
| 158 | + { |
| 159 | + Name: "deny-all", |
| 160 | + Glob: glob.MustCompile("*"), |
| 161 | + Exceptions: []glob.Glob{}, |
| 162 | + Mode: grant.Deny, |
| 163 | + Reason: "No 'Allow' rule matched, unknown licenses are not allowed.", |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + wants: struct { |
| 169 | + Pass bool |
| 170 | + Reasons []Reason |
| 171 | + }{ |
| 172 | + Pass: true, |
| 173 | + Reasons: []Reason{{ |
| 174 | + Detail: ReasonLicenseAllowed, |
| 175 | + RuleName: "allow-bsd-licenses", |
| 176 | + }}, |
| 177 | + }, |
| 178 | + }, |
| 179 | + } |
| 180 | + |
| 181 | + for _, tc := range tests { |
| 182 | + t.Run(tc.name, func(t *testing.T) { |
| 183 | + result := checkLicense(tc.config, &grant.Package{}, tc.license) |
| 184 | + if tc.wants.Pass != result.Pass { |
| 185 | + t.Errorf("Expected Pass to be %t, got %t", tc.wants.Pass, result.Pass) |
| 186 | + } |
| 187 | + if diff := cmp.Diff(tc.license, result.License); diff != "" { |
| 188 | + t.Errorf("Mismatched 'License' field (-want +got):\n%s", diff) |
| 189 | + } |
| 190 | + if diff := cmp.Diff(tc.config.Policy, result.Policy); diff != "" { |
| 191 | + t.Errorf("Mismatched 'Policy' field (-want +got):\n%s", diff) |
| 192 | + } |
| 193 | + if diff := cmp.Diff(tc.wants.Reasons, result.Reason); diff != "" { |
| 194 | + t.Errorf("Mismatched 'Reasons' field (-want +got):\n%s", diff) |
| 195 | + } |
| 196 | + }) |
| 197 | + } |
| 198 | +} |
0 commit comments