1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2package parser2v1 3 4import ( 5 "testing" 6 7 "github.com/spdx/tools-golang/spdx/v2_1" 8) 9 10// ===== Relationship section tests ===== 11func TestParser2_1FailsIfRelationshipNotSet(t *testing.T) { 12 parser := tvParser2_1{ 13 doc: &v2_1.Document{}, 14 st: psCreationInfo2_1, 15 } 16 err := parser.parsePairForRelationship2_1("Relationship", "SPDXRef-A CONTAINS SPDXRef-B") 17 if err == nil { 18 t.Errorf("expected error when calling parsePairFromRelationship2_1 without setting rln pointer") 19 } 20} 21 22func TestParser2_1FailsIfRelationshipCommentWithoutRelationship(t *testing.T) { 23 parser := tvParser2_1{ 24 doc: &v2_1.Document{}, 25 st: psCreationInfo2_1, 26 } 27 err := parser.parsePair2_1("RelationshipComment", "comment whatever") 28 if err == nil { 29 t.Errorf("expected error when calling parsePair2_1 for RelationshipComment without Relationship first") 30 } 31} 32 33func TestParser2_1CanParseRelationshipTags(t *testing.T) { 34 parser := tvParser2_1{ 35 doc: &v2_1.Document{}, 36 st: psCreationInfo2_1, 37 } 38 39 // Relationship 40 err := parser.parsePair2_1("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") 41 if err != nil { 42 t.Errorf("expected nil error, got %v", err) 43 } 44 if parser.rln.RefA.DocumentRefID != "" || parser.rln.RefA.ElementRefID != "something" { 45 t.Errorf("got %v for first part of Relationship, expected something", parser.rln.RefA) 46 } 47 if parser.rln.RefB.DocumentRefID != "otherdoc" || parser.rln.RefB.ElementRefID != "something-else" { 48 t.Errorf("got %v for second part of Relationship, expected otherdoc:something-else", parser.rln.RefB) 49 } 50 if parser.rln.Relationship != "CONTAINS" { 51 t.Errorf("got %v for Relationship type, expected CONTAINS", parser.rln.Relationship) 52 } 53 54 // Relationship Comment 55 cmt := "this is a comment" 56 err = parser.parsePair2_1("RelationshipComment", cmt) 57 if err != nil { 58 t.Errorf("expected nil error, got %v", err) 59 } 60 if parser.rln.RelationshipComment != cmt { 61 t.Errorf("got %v for RelationshipComment, expected %v", parser.rln.RelationshipComment, cmt) 62 } 63} 64 65func TestParser2_1InvalidRelationshipTagsNoValueFail(t *testing.T) { 66 parser := tvParser2_1{ 67 doc: &v2_1.Document{}, 68 st: psCreationInfo2_1, 69 } 70 71 // no items 72 parser.rln = nil 73 err := parser.parsePair2_1("Relationship", "") 74 if err == nil { 75 t.Errorf("expected error for empty items in relationship, got nil") 76 } 77} 78 79func TestParser2_1InvalidRelationshipTagsOneValueFail(t *testing.T) { 80 parser := tvParser2_1{ 81 doc: &v2_1.Document{}, 82 st: psCreationInfo2_1, 83 } 84 85 // one item 86 parser.rln = nil 87 err := parser.parsePair2_1("Relationship", "DESCRIBES") 88 if err == nil { 89 t.Errorf("expected error for only one item in relationship, got nil") 90 } 91} 92 93func TestParser2_1InvalidRelationshipTagsTwoValuesFail(t *testing.T) { 94 parser := tvParser2_1{ 95 doc: &v2_1.Document{}, 96 st: psCreationInfo2_1, 97 } 98 99 // two items 100 parser.rln = nil 101 err := parser.parsePair2_1("Relationship", "SPDXRef-DOCUMENT DESCRIBES") 102 if err == nil { 103 t.Errorf("expected error for only two items in relationship, got nil") 104 } 105} 106 107func TestParser2_1InvalidRelationshipTagsThreeValuesSucceed(t *testing.T) { 108 parser := tvParser2_1{ 109 doc: &v2_1.Document{}, 110 st: psCreationInfo2_1, 111 } 112 113 // three items but with interspersed additional whitespace 114 parser.rln = nil 115 err := parser.parsePair2_1("Relationship", " SPDXRef-DOCUMENT \t DESCRIBES SPDXRef-something-else ") 116 if err != nil { 117 t.Errorf("expected pass for three items in relationship w/ extra whitespace, got: %v", err) 118 } 119} 120 121func TestParser2_1InvalidRelationshipTagsFourValuesFail(t *testing.T) { 122 parser := tvParser2_1{ 123 doc: &v2_1.Document{}, 124 st: psCreationInfo2_1, 125 } 126 127 // four items 128 parser.rln = nil 129 err := parser.parsePair2_1("Relationship", "SPDXRef-a DESCRIBES SPDXRef-b SPDXRef-c") 130 if err == nil { 131 t.Errorf("expected error for more than three items in relationship, got nil") 132 } 133} 134 135func TestParser2_1InvalidRelationshipTagsInvalidRefIDs(t *testing.T) { 136 parser := tvParser2_1{ 137 doc: &v2_1.Document{}, 138 st: psCreationInfo2_1, 139 } 140 141 // four items 142 parser.rln = nil 143 err := parser.parsePair2_1("Relationship", "SPDXRef-a DESCRIBES b") 144 if err == nil { 145 t.Errorf("expected error for missing SPDXRef- prefix, got nil") 146 } 147 148 parser.rln = nil 149 err = parser.parsePair2_1("Relationship", "a DESCRIBES SPDXRef-b") 150 if err == nil { 151 t.Errorf("expected error for missing SPDXRef- prefix, got nil") 152 } 153} 154 155func TestParser2_1FailsToParseUnknownTagInRelationshipSection(t *testing.T) { 156 parser := tvParser2_1{ 157 doc: &v2_1.Document{}, 158 st: psCreationInfo2_1, 159 } 160 161 // Relationship 162 err := parser.parsePair2_1("Relationship", "SPDXRef-something CONTAINS DocumentRef-otherdoc:SPDXRef-something-else") 163 if err != nil { 164 t.Errorf("expected nil error, got %v", err) 165 } 166 // invalid tag 167 err = parser.parsePairForRelationship2_1("blah", "whoops") 168 if err == nil { 169 t.Errorf("expected non-nil error, got nil") 170 } 171} 172