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
|
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
package reader
import (
"testing"
)
func TestNewParser2_2(t *testing.T) {
// testing if the attributes are initialised well and no top-level is left uninitialized.
// primarily, checking if all the maps are initialized because
// uninitialized slices are by default slices of length 0
p, _ := parserFromBodyContent(``)
parser := NewParser2_2(p.gordfParserObj, p.nodeStringToTriples)
if parser.files == nil {
t.Errorf("files should've been initialised, got %v", parser.files)
}
if parser.assocWithPackage == nil {
t.Errorf("assocWithPackage should've been initialised, got %v", parser.assocWithPackage)
}
if parser.doc.CreationInfo == nil {
t.Errorf("doc.CreationInfo should've been initialised, got %v", parser.doc.CreationInfo)
}
if parser.doc.Packages == nil {
t.Errorf("doc.Packages should've been initialised, got %v", parser.doc.Packages)
}
if parser.doc.Files == nil {
t.Errorf("doc.Files should've been initialised, got %v", parser.doc.Files)
}
}
func TestLoadFromGoRDFParser(t *testing.T) {
var parser *rdfParser2_2
var err error
// TestCase 1: gordfparser without a SpdxDocument node triple:
parser, _ = parserFromBodyContent("")
_, err = LoadFromGoRDFParser(parser.gordfParserObj)
if err == nil {
t.Errorf("expected an error because of absence of SpdxDocument node, got %v", err)
}
// TestCase 2: invalid SpdxDocumentNode
parser, _ = parserFromBodyContent(`
<spdx:SpdxDocument rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301/Document">
<spdx:invalidTag />
</spdx:SpdxDocument>
`)
_, err = LoadFromGoRDFParser(parser.gordfParserObj)
if err == nil {
t.Errorf("expected an error because of absence of SpdxDocument node, got %v", err)
}
// TestCase 3: >1 type triples for subnode of a SpdxDocument:
parser, _ = parserFromBodyContent(`
<spdx:SpdxDocument rdf:about="#SPDXRef-Document"/>
<spdx:Snippet rdf:about="#Snippet"/>
<spdx:CreationInfo rdf:about="#Snippet"/>
`)
_, err = LoadFromGoRDFParser(parser.gordfParserObj)
if err == nil {
t.Errorf("expected an error due to more than one type triples, got %v", err)
}
// TestCase 4: invalid snippet must raise an error.
parser, _ = parserFromBodyContent(`
<spdx:SpdxDocument rdf:about="#SPDXRef-Document"/>
<spdx:Snippet rdf:about="#Snippet"/>
`)
_, err = LoadFromGoRDFParser(parser.gordfParserObj)
if err == nil {
t.Errorf("expected an error due to invalid Snippet, got %v", err)
}
// TestCase 5: invalid snippet not associated with any File must raise an error.
parser, _ = parserFromBodyContent(`
<spdx:SpdxDocument rdf:about="#SPDXRef-Document"/>
<spdx:Snippet rdf:about="#SPDXRef-Snippet"/>
`)
_, err = LoadFromGoRDFParser(parser.gordfParserObj)
if err == nil {
t.Errorf("expected an error due to invalid Snippet File, got %v", err)
}
// TestCase 6: other Tag alongwith the SpdxDocument node mustn't raise any error.
parser, _ = parserFromBodyContent(`
<spdx:SpdxDocument rdf:about="#SPDXRef-Document"/>
<spdx:review/>
`)
_, err = LoadFromGoRDFParser(parser.gordfParserObj)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// TestCase 5: everything valid:
parser, _ = parserFromBodyContent(`
<spdx:SpdxDocument rdf:about="#SPDXRef-Document"/>
<spdx:Snippet rdf:about="#SPDXRef-Snippet">
<spdx:name>from linux kernel</spdx:name>
<spdx:copyrightText>Copyright 2008-2010 John Smith</spdx:copyrightText>
<spdx:licenseComments>The concluded license was taken from package xyz, from which the snippet was copied into the current file. The concluded license information was found in the COPYING.txt file in package xyz.</spdx:licenseComments>
<spdx:snippetFromFile>
<spdx:File rdf:about="#SPDXRef-DoapSource">
<spdx:copyrightText>Copyright 2010, 2011 Source Auditor Inc.</spdx:copyrightText>
<spdx:fileContributor>Open Logic Inc.</spdx:fileContributor>
<spdx:fileName>./src/org/spdx/parser/DOAPProject.java</spdx:fileName>
<spdx:fileContributor>Black Duck Software In.c</spdx:fileContributor>
<spdx:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/>
<spdx:licenseInfoInFile rdf:resource="http://spdx.org/licenses/Apache-2.0"/>
</spdx:File>
</spdx:snippetFromFile>
</spdx:Snippet>
`)
_, err = LoadFromGoRDFParser(parser.gordfParserObj)
if err != nil {
t.Errorf("error parsing a valid example: %v", err)
}
}
func Test_rdfParser2_2_getSpdxDocNode(t *testing.T) {
var parser *rdfParser2_2
var err error
// TestCase 1: more than one association type for a single node.
parser, _ = parserFromBodyContent(`
<spdx:SpdxDocument rdf:about="#SPDXRef-Document"/>
<spdx:Snippet rdf:about="#SPDXRef-Document"/>
`)
_, err = parser.getSpdxDocNode()
t.Log(err)
if err == nil {
t.Errorf("expected and error due to more than one type triples for the SpdxDocument Node, got %v", err)
}
// TestCase 2: must be associated with exactly one rdf:type.
parser, _ = parserFromBodyContent(`
<spdx:SpdxDocument rdf:about="#SPDXRef-Document"/>
<spdx:Snippet rdf:about="#SPDXRef-Document"/>
<spdx:File rdf:about="#SPDXRef-DoapSource"/>
`)
_, err = parser.getSpdxDocNode()
t.Log(err)
if err == nil {
t.Errorf("rootNode must be associated with exactly one triple of predicate rdf:type, got %v", err)
}
// TestCase 3: two different spdx nodes found in a single document.
parser, _ = parserFromBodyContent(`
<spdx:SpdxDocument rdf:about="#SPDXRef-Document-1"/>
<spdx:SpdxDocument rdf:about="#SPDXRef-Document-2"/>
`)
_, err = parser.getSpdxDocNode()
if err == nil {
t.Errorf("expected and error due to more than one type SpdxDocument Node, got %v", err)
}
// TestCase 4: no spdx document
parser, _ = parserFromBodyContent(``)
_, err = parser.getSpdxDocNode()
if err == nil {
t.Errorf("expected and error due to no SpdxDocument Node, got %v", err)
}
// TestCase 5: valid spdxDocument node
parser, _ = parserFromBodyContent(`
<spdx:SpdxDocument rdf:about="#SPDXRef-Document-1"/>
`)
_, err = parser.getSpdxDocNode()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
|