github.com/CycloneDX/sbom-utility@v0.16.0/schema/license_expression_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 /* 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package schema 20 21 import ( 22 "reflect" 23 "testing" 24 ) 25 26 // TODO: Need tests that include unary operators (e.g., AFL-2.0+) 27 // which is an outdated concept and replaced by newer approach as seen 28 // with GPL (e.g., GPL-2.0-or-later) using suffixes "or-later" or "only" (restrict) 29 30 func TestLicenseExpressionTokenizerWhitespaceRemoval(t *testing.T) { 31 EXP := " Apache-2.0 AND ( MIT OR GPL-2.0-only ) " 32 VALID := []string{"Apache-2.0", "AND", "(", "MIT", "OR", "GPL-2.0-only", ")"} 33 34 tokens := tokenizeExpression(EXP) 35 36 if !reflect.DeepEqual(tokens, VALID) { 37 t.Errorf("tokenizeExpression(): returned: %v; expected: %v", tokens, VALID) 38 } else { 39 getLogger().Tracef("tokenizeExpression(): returned: %v; matched expected", tokens) 40 } 41 } 42 43 func TestLicenseExpressionTokenizerWhitespaceNewlineTabRemoval(t *testing.T) { 44 EXP := "\n\tApache-2.0 \tAND (\n MIT OR GPL-2.0-only )\t\n" 45 VALID := []string{"Apache-2.0", "AND", "(", "MIT", "OR", "GPL-2.0-only", ")"} 46 47 tokens := tokenizeExpression(EXP) 48 49 if !reflect.DeepEqual(tokens, VALID) { 50 t.Errorf("tokenizeExpression(): returned: %v; expected: %v", tokens, VALID) 51 } else { 52 getLogger().Tracef("tokenizeExpression(): returned: %v; matched expected", tokens) 53 } 54 } 55 56 // ------------------------------------------- 57 // Test SPDX ID (validity) 58 // ------------------------------------------- 59 60 func TestLicenseSpdxIdSimple(t *testing.T) { 61 ID := "MIT" 62 if !IsValidSpdxId(ID) { 63 t.Errorf("IsValidSpdxId(`%s`) == `false`: Expected `true`.", ID) 64 } 65 } 66 67 func TestLicenseSpdxIdComplex(t *testing.T) { 68 ID := "AGPL-3.0-or-later" 69 if !IsValidSpdxId(ID) { 70 t.Errorf("IsValidSpdxId(`%s`) == `false`: Expected `true`.", ID) 71 } 72 } 73 74 func TestLicenseSpdxIdFailEmptyString(t *testing.T) { 75 ID := "" 76 if IsValidSpdxId(ID) { 77 t.Errorf("IsValidSpdxId(`%s`) == `true`: Expected `false`.", ID) 78 } 79 } 80 81 func TestLicenseSpdxIdFailBadCharacter1(t *testing.T) { 82 ID := "?" 83 if IsValidSpdxId(ID) { 84 t.Errorf("IsValidSpdxId(`%s`) == `true`: Expected `false`.", ID) 85 } 86 } 87 88 func TestLicenseSpdxIdFailBadCharacter2(t *testing.T) { 89 ID := "MIT+Apache-2.0" 90 if IsValidSpdxId(ID) { 91 t.Errorf("IsValidSpdxId(`%s`) == `true`: Expected `false`.", ID) 92 } 93 } 94 95 func TestLicenseSpdxIdFailWhiteSpace(t *testing.T) { 96 ID := "Apache 2.0" 97 if IsValidSpdxId(ID) { 98 t.Errorf("IsValidSpdxId(`%s`) == `true`: Expected `false`.", ID) 99 } 100 } 101 102 // ----------------------------------- 103 // Usage Policy: allowed value tests 104 // ----------------------------------- 105 func TestLicensePolicyUsageValueAllow(t *testing.T) { 106 value := POLICY_ALLOW 107 if !IsValidUsagePolicy(value) { 108 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", false, true) 109 } 110 } 111 112 func TestLicensePolicyUsageValueDeny(t *testing.T) { 113 value := POLICY_DENY 114 if !IsValidUsagePolicy(value) { 115 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", false, true) 116 } 117 } 118 119 func TestLicensePolicyUsageValueNeedsReview(t *testing.T) { 120 value := POLICY_NEEDS_REVIEW 121 if !IsValidUsagePolicy(value) { 122 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", false, true) 123 } 124 } 125 126 func TestLicensePolicyUsageValueUndefined(t *testing.T) { 127 value := POLICY_UNDEFINED 128 if IsValidUsagePolicy(value) { 129 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", false, true) 130 } 131 } 132 133 func TestLicensePolicyUsageInvalidValue(t *testing.T) { 134 value := POLICY_CONFLICT 135 if IsValidUsagePolicy(value) { 136 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", true, false) 137 } 138 } 139 140 //------------------------------------ 141 // Policy Family: allowed value tests 142 //------------------------------------ 143 144 func TestLicensePolicyInvalidFamily1(t *testing.T) { 145 value := "CONFLICT" 146 if IsValidFamilyKey(value) { 147 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", true, false) 148 } 149 150 value = "conflict" 151 if IsValidFamilyKey(value) { 152 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", true, false) 153 } 154 155 value = "Conflict" 156 if IsValidFamilyKey(value) { 157 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", true, false) 158 } 159 } 160 161 func TestLicensePolicyInvalidFamilyKeywords1(t *testing.T) { 162 value := "CONFLICT" 163 if IsValidFamilyKey(value) { 164 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", true, false) 165 } 166 167 value = "conflict" 168 if IsValidFamilyKey(value) { 169 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", true, false) 170 } 171 172 value = "Conflict" 173 if IsValidFamilyKey(value) { 174 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", true, false) 175 } 176 177 value = "Foo-Conflict-2.0-Bar" 178 if IsValidFamilyKey(value) { 179 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", true, false) 180 } 181 } 182 183 func TestLicensePolicyInvalidFamilyKeywords2(t *testing.T) { 184 value := "UNKNOWN" 185 if IsValidFamilyKey(value) { 186 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", true, false) 187 } 188 189 value = "unknown" 190 if IsValidFamilyKey(value) { 191 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", true, false) 192 } 193 194 value = "Unknown" 195 if IsValidFamilyKey(value) { 196 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", true, false) 197 } 198 199 value = "Foo-Unknown-1.1-Bar" 200 if IsValidFamilyKey(value) { 201 t.Errorf("isValidUsagePolicy(): returned: %t; expected: %t", true, false) 202 } 203 }