1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2 3package parser2v2 4 5import ( 6 "testing" 7) 8 9func TestNewParser2_2(t *testing.T) { 10 // testing if the attributes are initialised well and no top-level is left uninitialized. 11 // primarily, checking if all the maps are initialized because 12 // uninitialized slices are by default slices of length 0 13 p, _ := parserFromBodyContent(``) 14 parser := NewParser2_2(p.gordfParserObj, p.nodeStringToTriples) 15 if parser.files == nil { 16 t.Errorf("files should've been initialised, got %v", parser.files) 17 } 18 if parser.assocWithPackage == nil { 19 t.Errorf("assocWithPackage should've been initialised, got %v", parser.assocWithPackage) 20 } 21 if parser.doc.CreationInfo == nil { 22 t.Errorf("doc.CreationInfo should've been initialised, got %v", parser.doc.CreationInfo) 23 } 24 if parser.doc.Packages == nil { 25 t.Errorf("doc.Packages should've been initialised, got %v", parser.doc.Packages) 26 } 27 if parser.doc.Files == nil { 28 t.Errorf("doc.Files should've been initialised, got %v", parser.doc.Files) 29 } 30} 31 32func TestLoadFromGoRDFParser(t *testing.T) { 33 var parser *rdfParser2_2 34 var err error 35 36 // TestCase 1: gordfparser without a SpdxDocument node triple: 37 parser, _ = parserFromBodyContent("") 38 _, err = LoadFromGoRDFParser(parser.gordfParserObj) 39 if err == nil { 40 t.Errorf("expected an error because of absence of SpdxDocument node, got %v", err) 41 } 42 43 // TestCase 2: invalid SpdxDocumentNode 44 parser, _ = parserFromBodyContent(` 45 <spdx:SpdxDocument rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301/Document"> 46 <spdx:invalidTag /> 47 </spdx:SpdxDocument> 48 `) 49 _, err = LoadFromGoRDFParser(parser.gordfParserObj) 50 if err == nil { 51 t.Errorf("expected an error because of absence of SpdxDocument node, got %v", err) 52 } 53 54 // TestCase 3: >1 type triples for subnode of a SpdxDocument: 55 parser, _ = parserFromBodyContent(` 56 <spdx:SpdxDocument rdf:about="#SPDXRef-Document"/> 57 <spdx:Snippet rdf:about="#Snippet"/> 58 <spdx:CreationInfo rdf:about="#Snippet"/> 59 `) 60 _, err = LoadFromGoRDFParser(parser.gordfParserObj) 61 if err == nil { 62 t.Errorf("expected an error due to more than one type triples, got %v", err) 63 } 64 65 // TestCase 4: invalid snippet must raise an error. 66 parser, _ = parserFromBodyContent(` 67 <spdx:SpdxDocument rdf:about="#SPDXRef-Document"/> 68 <spdx:Snippet rdf:about="#Snippet"/> 69 `) 70 _, err = LoadFromGoRDFParser(parser.gordfParserObj) 71 if err == nil { 72 t.Errorf("expected an error due to invalid Snippet, got %v", err) 73 } 74 75 // TestCase 5: invalid snippet not associated with any File must raise an error. 76 parser, _ = parserFromBodyContent(` 77 <spdx:SpdxDocument rdf:about="#SPDXRef-Document"/> 78 <spdx:Snippet rdf:about="#SPDXRef-Snippet"/> 79 `) 80 _, err = LoadFromGoRDFParser(parser.gordfParserObj) 81 if err == nil { 82 t.Errorf("expected an error due to invalid Snippet File, got %v", err) 83 } 84 85 // TestCase 6: other Tag alongwith the SpdxDocument node mustn't raise any error. 86 parser, _ = parserFromBodyContent(` 87 <spdx:SpdxDocument rdf:about="#SPDXRef-Document"/> 88 <spdx:review/> 89 `) 90 _, err = LoadFromGoRDFParser(parser.gordfParserObj) 91 if err != nil { 92 t.Errorf("unexpected error: %v", err) 93 } 94 95 // TestCase 5: everything valid: 96 parser, _ = parserFromBodyContent(` 97 <spdx:SpdxDocument rdf:about="#SPDXRef-Document"/> 98 <spdx:Snippet rdf:about="#SPDXRef-Snippet"> 99 <spdx:name>from linux kernel</spdx:name> 100 <spdx:copyrightText>Copyright 2008-2010 John Smith</spdx:copyrightText> 101 <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> 102 <spdx:snippetFromFile> 103 <spdx:File rdf:about="#SPDXRef-DoapSource"> 104 <spdx:copyrightText>Copyright 2010, 2011 Source Auditor Inc.</spdx:copyrightText> 105 <spdx:fileContributor>Open Logic Inc.</spdx:fileContributor> 106 <spdx:fileName>./src/org/spdx/parser/DOAPProject.java</spdx:fileName> 107 <spdx:fileContributor>Black Duck Software In.c</spdx:fileContributor> 108 <spdx:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> 109 <spdx:licenseInfoInFile rdf:resource="http://spdx.org/licenses/Apache-2.0"/> 110 </spdx:File> 111 </spdx:snippetFromFile> 112 </spdx:Snippet> 113 `) 114 _, err = LoadFromGoRDFParser(parser.gordfParserObj) 115 if err != nil { 116 t.Errorf("error parsing a valid example: %v", err) 117 } 118} 119 120func Test_rdfParser2_2_getSpdxDocNode(t *testing.T) { 121 var parser *rdfParser2_2 122 var err error 123 124 // TestCase 1: more than one association type for a single node. 125 parser, _ = parserFromBodyContent(` 126 <spdx:SpdxDocument rdf:about="#SPDXRef-Document"/> 127 <spdx:Snippet rdf:about="#SPDXRef-Document"/> 128 `) 129 _, err = parser.getSpdxDocNode() 130 t.Log(err) 131 if err == nil { 132 t.Errorf("expected and error due to more than one type triples for the SpdxDocument Node, got %v", err) 133 } 134 135 // TestCase 2: must be associated with exactly one rdf:type. 136 parser, _ = parserFromBodyContent(` 137 <spdx:SpdxDocument rdf:about="#SPDXRef-Document"/> 138 <spdx:Snippet rdf:about="#SPDXRef-Document"/> 139 <spdx:File rdf:about="#SPDXRef-DoapSource"/> 140 `) 141 _, err = parser.getSpdxDocNode() 142 t.Log(err) 143 if err == nil { 144 t.Errorf("rootNode must be associated with exactly one triple of predicate rdf:type, got %v", err) 145 } 146 147 // TestCase 3: two different spdx nodes found in a single document. 148 parser, _ = parserFromBodyContent(` 149 <spdx:SpdxDocument rdf:about="#SPDXRef-Document-1"/> 150 <spdx:SpdxDocument rdf:about="#SPDXRef-Document-2"/> 151 `) 152 _, err = parser.getSpdxDocNode() 153 if err == nil { 154 t.Errorf("expected and error due to more than one type SpdxDocument Node, got %v", err) 155 } 156 157 // TestCase 4: no spdx document 158 parser, _ = parserFromBodyContent(``) 159 _, err = parser.getSpdxDocNode() 160 if err == nil { 161 t.Errorf("expected and error due to no SpdxDocument Node, got %v", err) 162 } 163 164 // TestCase 5: valid spdxDocument node 165 parser, _ = parserFromBodyContent(` 166 <spdx:SpdxDocument rdf:about="#SPDXRef-Document-1"/> 167 `) 168 _, err = parser.getSpdxDocNode() 169 if err != nil { 170 t.Errorf("unexpected error: %v", err) 171 } 172} 173