// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
package parser2v2
import (
"testing"
gordfParser "github.com/spdx/gordf/rdfloader/parser"
)
func Test_rdfParser2_2_getExternalDocumentRefFromNode(t *testing.T) {
var parser *rdfParser2_2
var node *gordfParser.Node
var err error
// TestCase 1: invalid checksum
parser, _ = parserFromBodyContent(`
DocumentRef-spdx-tool-1.2
d6a770ba38583ed4bb4525bd96e50461655d2759
`)
node = parser.gordfParserObj.Triples[0].Subject
_, err = parser.getExternalDocumentRefFromNode(node)
if err == nil {
t.Errorf("expected an error due to invalid checksum, found %v", err)
}
// TestCase 2: unknown predicate
parser, _ = parserFromBodyContent(`
`)
node = parser.gordfParserObj.Triples[0].Subject
_, err = parser.getExternalDocumentRefFromNode(node)
if err == nil {
t.Errorf("expected an error due to invalid predicate, found %v", err)
}
// TestCase 3: valid example
parser, _ = parserFromBodyContent(`
DocumentRef-spdx-tool-1.2
d6a770ba38583ed4bb4525bd96e50461655d2759
`)
node = parser.gordfParserObj.Triples[0].Subject
_, err = parser.getExternalDocumentRefFromNode(node)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
func Test_rdfParser2_2_parseSpdxDocumentNode(t *testing.T) {
var parser *rdfParser2_2
var node *gordfParser.Node
var err error
// TestCase 1: invalid spdx id of the document
parser, _ = parserFromBodyContent(`
`)
node = parser.gordfParserObj.Triples[0].Subject
err = parser.parseSpdxDocumentNode(node)
if err == nil {
t.Errorf("expected an error due to invalid document id, got %v", err)
}
// TestCase 2: erroneous dataLicense
parser, _ = parserFromBodyContent(`
`)
node = parser.gordfParserObj.Triples[0].Subject
err = parser.parseSpdxDocumentNode(node)
if err == nil {
t.Errorf("expected an error due to invalid dataLicense, got %v", err)
}
// TestCase 3: invalid external document ref
parser, _ = parserFromBodyContent(`
DocumentRef-spdx-tool-1.2
d6a770ba38583ed4bb4525bd96e50461655d2759
`)
node = parser.gordfParserObj.Triples[0].Subject
err = parser.parseSpdxDocumentNode(node)
if err == nil {
t.Errorf("expected an error due to invalid externalDocumentRef, got %v", err)
}
// TestCase 4: invalid package
parser, _ = parserFromBodyContent(`
`)
node = parser.gordfParserObj.Triples[0].Subject
err = parser.parseSpdxDocumentNode(node)
if err == nil {
t.Errorf("expected an error due to invalid externalDocumentRef, got %v", err)
}
// TestCase 5: error in extractedLicensingInfo
parser, _ = parserFromBodyContent(`
LicenseRef-Freeware
freeware
`)
node = parser.gordfParserObj.Triples[0].Subject
err = parser.parseSpdxDocumentNode(node)
if err == nil {
t.Errorf("expected an error due to invalid extractedLicensingInfo, got %v", err)
}
// TestCase 6: error in annotation
parser, _ = parserFromBodyContent(`
`)
node = parser.gordfParserObj.Triples[0].Subject
err = parser.parseSpdxDocumentNode(node)
if err == nil {
t.Errorf("expected an error due to invalid extractedLicensingInfo, got %v", err)
}
// TestCase 7: invalid predicate
parser, _ = parserFromBodyContent(`
`)
node = parser.gordfParserObj.Triples[0].Subject
err = parser.parseSpdxDocumentNode(node)
if err == nil {
t.Errorf("expected an error due to unknown predicate, got %v", err)
}
// TestCase 7: everything valid
parser, _ = parserFromBodyContent(`
SPDX-2.1
/test/example
DocumentRef-spdx-tool-1.2
d6a770ba38583ed4bb4525bd96e50461655d2759
2.6
Person: spdx (y)
Organization:
Tool: spdx2
2018-08-24T19:55:34Z
test
Another example reviewer.
2011-03-13T00:00:00Z
Person: Suzanne Reviewer
2011-01-29T18:30:22Z
test annotation
Person: Rishabh Bhatnagar
`)
node = parser.gordfParserObj.Triples[0].Subject
err = parser.parseSpdxDocumentNode(node)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}