1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
package reader
import (
"reflect"
"testing"
)
func Test_getLicenseStringFromURI(t *testing.T) {
// TestCase 1: NONE license
input := SPDX_NONE_CAPS
output := getLicenseStringFromURI(input)
expectedOutput := "NONE"
if output != expectedOutput {
t.Errorf("expected: %s, found %s", expectedOutput, output)
}
// TestCase 2: NOASSERTION license
input = SPDX_NOASSERTION_SMALL
output = getLicenseStringFromURI(input)
expectedOutput = "NOASSERTION"
if output != expectedOutput {
t.Errorf("expected: %s, found %s", expectedOutput, output)
}
// TestCase 3: Other license
input = NS_SPDX + "LicenseRef-1"
output = getLicenseStringFromURI(input)
expectedOutput = "LicenseRef-1"
if output != expectedOutput {
t.Errorf("expected: %s, found %s", expectedOutput, output)
}
}
func Test_rdfParser2_2_getChecksumFromNode(t *testing.T) {
var parser *rdfParser2_2
var err error
// TestCase 1: invalid checksum algorithm
parser, _ = parserFromBodyContent(`
<spdx:Checksum>
<spdx:checksumValue>2fd4e1c67a2d28fced849ee1bb76e7391b93eb12</spdx:checksumValue>
<spdx:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha999"/>
</spdx:Checksum>
`)
checksumNode := parser.gordfParserObj.Triples[0].Subject
_, _, err = parser.getChecksumFromNode(checksumNode)
if err == nil {
t.Errorf("expected an error saying invalid checksum algorithm")
}
// TestCase 2: invalid predicate
parser, _ = parserFromBodyContent(`
<spdx:Checksum>
<spdx:checksumValue>2fd4e1c67a2d28fced849ee1bb76e7391b93eb12</spdx:checksumValue>
<spdx:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/>
<spdx:invalidPredicate />
</spdx:Checksum>
`)
checksumNode = parser.gordfParserObj.Triples[0].Subject
_, _, err = parser.getChecksumFromNode(checksumNode)
if err == nil {
t.Errorf("expected an error saying invalid predicate")
}
// TestCase 3: valid input
parser, _ = parserFromBodyContent(`
<spdx:Checksum>
<spdx:checksumValue>2fd4e1c67a2d28fced849ee1bb76e7391b93eb12</spdx:checksumValue>
<spdx:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/>
</spdx:Checksum>
`)
checksumNode = parser.gordfParserObj.Triples[0].Subject
algorithm, value, err := parser.getChecksumFromNode(checksumNode)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if algorithm != "SHA1" {
t.Errorf("expected checksum algorithm to be sha1, found %s", algorithm)
}
expectedValue := "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"
if value != expectedValue {
t.Errorf("expected checksumValue to be %s, found %s", expectedValue, value)
}
}
func Test_rdfParser2_2_getAlgorithmFromURI(t *testing.T) {
var algorithmURI string
var err error
// TestCase 1: checksumAlgorithm uri doesn't start with checksumAlgorithm_
algorithmURI = NS_SPDX + "sha1"
_, err = getAlgorithmFromURI(algorithmURI)
if err == nil {
t.Errorf("should've raised an error for algorithmURI that doesn't start with checksumAlgorithm_")
}
// TestCase 2: unknown checksum algorithm
algorithmURI = NS_SPDX + "checksumAlgorithm_sha999"
_, err = getAlgorithmFromURI(algorithmURI)
if err == nil {
t.Errorf("should've raised an error for invalid algorithm")
}
// TestCase 3: valid input
algorithmURI = NS_SPDX + "checksumAlgorithm_sha256"
algorithm, err := getAlgorithmFromURI(algorithmURI)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if algorithm != "SHA256" {
t.Errorf("expected: SHA256, found: %s", algorithm)
}
}
func Test_mapLicensesToStrings(t *testing.T) {
// nothing much to test here.
// just a dummy dry run.
licenses := []AnyLicenseInfo{
SpecialLicense{
value: NONE,
},
SpecialLicense{
value: NOASSERTION,
},
}
licenseStrings := mapLicensesToStrings(licenses)
expectedLicenseStrings := []string{"NONE", "NOASSERTION"}
if !reflect.DeepEqual(licenseStrings, expectedLicenseStrings) {
t.Errorf("expected: %+v\nfound %+v", expectedLicenseStrings, licenseStrings)
}
}
func TestConjunctiveLicenseSet_ToLicenseString(t *testing.T) {
var lic ConjunctiveLicenseSet
var output, expectedOutput string
// TestCase 1: no license in the set
lic = ConjunctiveLicenseSet{
members: nil,
}
output = lic.ToLicenseString()
expectedOutput = ""
if output != expectedOutput {
t.Errorf("expected: %s, found %s", output, expectedOutput)
}
// TestCase 2: single license in the set
lic = ConjunctiveLicenseSet{
members: []AnyLicenseInfo{
SpecialLicense{value: NOASSERTION},
},
}
output = lic.ToLicenseString()
expectedOutput = "NOASSERTION"
if output != expectedOutput {
t.Errorf("expected: %s, found %s", output, expectedOutput)
}
// TestCase 3: more than one license in the set.
lic = ConjunctiveLicenseSet{
members: []AnyLicenseInfo{
SpecialLicense{value: NOASSERTION},
SpecialLicense{value: NONE},
},
}
output = lic.ToLicenseString()
expectedOutput = "NOASSERTION AND NONE"
if output != expectedOutput {
t.Errorf("expected: %s, found %s", output, expectedOutput)
}
// TestCase 4: nested conjunctive license.
lic = ConjunctiveLicenseSet{
members: []AnyLicenseInfo{
SpecialLicense{value: NOASSERTION},
ConjunctiveLicenseSet{
members: []AnyLicenseInfo{
SpecialLicense{value: "LicenseRef-1"},
SpecialLicense{value: NONE},
},
},
},
}
output = lic.ToLicenseString()
expectedOutput = "NOASSERTION AND LicenseRef-1 AND NONE"
if output != expectedOutput {
t.Errorf("expected: %s, found %s", output, expectedOutput)
}
}
func TestDisjunctiveLicenseSet_ToLicenseString(t *testing.T) {
var lic DisjunctiveLicenseSet
var output, expectedOutput string
// TestCase 1: no license in the set
lic = DisjunctiveLicenseSet{
members: nil,
}
output = lic.ToLicenseString()
expectedOutput = ""
if output != expectedOutput {
t.Errorf("expected: %s, found %s", output, expectedOutput)
}
// TestCase 2: single license in the set
lic = DisjunctiveLicenseSet{
members: []AnyLicenseInfo{
SpecialLicense{value: NOASSERTION},
},
}
output = lic.ToLicenseString()
expectedOutput = "NOASSERTION"
if output != expectedOutput {
t.Errorf("expected: %s, found %s", output, expectedOutput)
}
// TestCase 3: more than one license in the set.
lic = DisjunctiveLicenseSet{
members: []AnyLicenseInfo{
SpecialLicense{value: NOASSERTION},
SpecialLicense{value: NONE},
},
}
output = lic.ToLicenseString()
expectedOutput = "NOASSERTION OR NONE"
if output != expectedOutput {
t.Errorf("expected: %s, found %s", output, expectedOutput)
}
// TestCase 4: nested conjunctive license.
lic = DisjunctiveLicenseSet{
members: []AnyLicenseInfo{
SpecialLicense{value: NOASSERTION},
DisjunctiveLicenseSet{
members: []AnyLicenseInfo{
SpecialLicense{value: "LicenseRef-1"},
SpecialLicense{value: NONE},
},
},
},
}
output = lic.ToLicenseString()
expectedOutput = "NOASSERTION OR LicenseRef-1 OR NONE"
if output != expectedOutput {
t.Errorf("expected: %s, found %s", output, expectedOutput)
}
}
func TestExtractedLicensingInfo_ToLicenseString(t *testing.T) {
// nothing to test (just a dry run)
extractedLicense := ExtractedLicensingInfo{
SimpleLicensingInfo: SimpleLicensingInfo{
licenseID: "license",
},
extractedText: "extracted Text",
}
expectedOutput := "license"
output := extractedLicense.ToLicenseString()
if output != expectedOutput {
t.Errorf("expected: %s, found: %s", expectedOutput, output)
}
}
func TestOrLaterOperator_ToLicenseString(t *testing.T) {
// nothing to test (just a dry run)
orLater := OrLaterOperator{
member: SimpleLicensingInfo{
licenseID: "license",
},
}
expectedOutput := "license"
output := orLater.ToLicenseString()
if output != expectedOutput {
t.Errorf("expected: %s, found: %s", expectedOutput, output)
}
}
func TestLicense_ToLicenseString(t *testing.T) {
// nothing to test (just a dry run)
license := License{
SimpleLicensingInfo: SimpleLicensingInfo{
licenseID: "license",
},
}
expectedOutput := "license"
output := license.ToLicenseString()
if output != expectedOutput {
t.Errorf("expected: %s, found: %s", expectedOutput, output)
}
}
func TestListedLicense_ToLicenseString(t *testing.T) {
// nothing to test (just a dry run)
ll := ListedLicense{License{
SimpleLicensingInfo: SimpleLicensingInfo{
licenseID: "license",
},
},
}
expectedOutput := "license"
output := ll.ToLicenseString()
if output != expectedOutput {
t.Errorf("expected: %s, found: %s", expectedOutput, output)
}
}
func TestWithExceptionOperator_ToLicenseString(t *testing.T) {
// nothing to test (just a dry run)
withException := WithExceptionOperator{
member: SimpleLicensingInfo{
licenseID: "license",
},
licenseException: LicenseException{},
}
expectedOutput := "license"
output := withException.ToLicenseString()
if output != expectedOutput {
t.Errorf("expected: %s, found: %s", expectedOutput, output)
}
}
func TestSpecialLicense_ToLicenseString(t *testing.T) {
// nothing to test (just a dry run)
specialLicense := SpecialLicense{
value: "license",
}
expectedOutput := "license"
output := specialLicense.ToLicenseString()
if output != expectedOutput {
t.Errorf("expected: %s, found: %s", expectedOutput, output)
}
}
func TestSimpleLicensingInfo_ToLicenseString(t *testing.T) {
// nothing to test (just a dry run)
sli := SimpleLicensingInfo{
licenseID: "license",
}
expectedOutput := "license"
output := sli.ToLicenseString()
if output != expectedOutput {
t.Errorf("expected: %s, found: %s", expectedOutput, output)
}
}
|