1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2 3package spdxlib 4 5import ( 6 "testing" 7 8 "github.com/spdx/tools-golang/spdx/common" 9 "github.com/spdx/tools-golang/spdx/v2_1" 10 "github.com/spdx/tools-golang/spdx/v2_2" 11) 12 13// ===== 2.1 tests ===== 14 15func Test2_1FilterForDependencies(t *testing.T) { 16 // set up document and some packages and relationships 17 doc := &v2_1.Document{ 18 SPDXVersion: "SPDX-2.1", 19 DataLicense: "CC0-1.0", 20 SPDXIdentifier: common.ElementID("DOCUMENT"), 21 CreationInfo: &v2_1.CreationInfo{}, 22 Packages: []*v2_1.Package{ 23 {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, 24 {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, 25 {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, 26 {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, 27 {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, 28 }, 29 Relationships: []*v2_1.Relationship{ 30 { 31 RefA: common.MakeDocElementID("", "DOCUMENT"), 32 RefB: common.MakeDocElementID("", "p1"), 33 Relationship: "DESCRIBES", 34 }, 35 { 36 RefA: common.MakeDocElementID("", "DOCUMENT"), 37 RefB: common.MakeDocElementID("", "p5"), 38 Relationship: "DESCRIBES", 39 }, 40 { 41 RefA: common.MakeDocElementID("", "p4"), 42 RefB: common.MakeDocElementID("", "DOCUMENT"), 43 Relationship: "DESCRIBED_BY", 44 }, 45 { 46 RefA: common.MakeDocElementID("", "p1"), 47 RefB: common.MakeDocElementID("", "p2"), 48 Relationship: "DEPENDS_ON", 49 }, 50 { 51 RefA: common.MakeDocElementID("", "p3"), 52 RefB: common.MakeDocElementID("", "p4"), 53 Relationship: "DEPENDENCY_OF", 54 }, 55 }, 56 } 57 58 eIDs, err := FilterRelationships2_1(doc, func(relationship *v2_1.Relationship) *common.ElementID { 59 p1EID := common.MakeDocElementID("", "p1") 60 if relationship.Relationship == "DEPENDS_ON" && relationship.RefA == p1EID { 61 return &relationship.RefB.ElementRefID 62 } else if relationship.Relationship == "DEPENDENCY_OF" && relationship.RefB == p1EID { 63 return &relationship.RefA.ElementRefID 64 } 65 66 return nil 67 }) 68 if err != nil { 69 t.Fatalf("expected non-nil err, got: %s", err.Error()) 70 } 71 72 if len(eIDs) != 1 { 73 t.Fatalf("expected 1 ElementID, got: %v", eIDs) 74 } 75 76 if eIDs[0] != common.MakeDocElementID("", "p2").ElementRefID { 77 t.Fatalf("received unexpected relationship: %v", eIDs[0]) 78 } 79} 80 81// ===== 2.2 tests ===== 82 83func Test2_2FindsDependsOnRelationships(t *testing.T) { 84 // set up document and some packages and relationships 85 doc := &v2_2.Document{ 86 SPDXVersion: "SPDX-2.2", 87 DataLicense: "CC0-1.0", 88 SPDXIdentifier: common.ElementID("DOCUMENT"), 89 CreationInfo: &v2_2.CreationInfo{}, 90 Packages: []*v2_2.Package{ 91 {PackageName: "pkg1", PackageSPDXIdentifier: "p1"}, 92 {PackageName: "pkg2", PackageSPDXIdentifier: "p2"}, 93 {PackageName: "pkg3", PackageSPDXIdentifier: "p3"}, 94 {PackageName: "pkg4", PackageSPDXIdentifier: "p4"}, 95 {PackageName: "pkg5", PackageSPDXIdentifier: "p5"}, 96 }, 97 Relationships: []*v2_2.Relationship{ 98 { 99 RefA: common.MakeDocElementID("", "DOCUMENT"), 100 RefB: common.MakeDocElementID("", "p1"), 101 Relationship: "DESCRIBES", 102 }, 103 { 104 RefA: common.MakeDocElementID("", "DOCUMENT"), 105 RefB: common.MakeDocElementID("", "p5"), 106 Relationship: "DESCRIBES", 107 }, 108 // inverse relationship -- should also get detected 109 { 110 RefA: common.MakeDocElementID("", "p4"), 111 RefB: common.MakeDocElementID("", "DOCUMENT"), 112 Relationship: "DESCRIBED_BY", 113 }, 114 // different relationship 115 { 116 RefA: common.MakeDocElementID("", "p1"), 117 RefB: common.MakeDocElementID("", "p2"), 118 Relationship: "DEPENDS_ON", 119 }, 120 }, 121 } 122 123 eIDs, err := FilterRelationships2_2(doc, func(relationship *v2_2.Relationship) *common.ElementID { 124 p1EID := common.MakeDocElementID("", "p1") 125 if relationship.Relationship == "DEPENDS_ON" && relationship.RefA == p1EID { 126 return &relationship.RefB.ElementRefID 127 } else if relationship.Relationship == "DEPENDENCY_OF" && relationship.RefB == p1EID { 128 return &relationship.RefA.ElementRefID 129 } 130 131 return nil 132 }) 133 if err != nil { 134 t.Fatalf("expected non-nil err, got: %s", err.Error()) 135 } 136 137 if len(eIDs) != 1 { 138 t.Fatalf("expected 1 ElementID, got: %v", eIDs) 139 } 140 141 if eIDs[0] != common.MakeDocElementID("", "p2").ElementRefID { 142 t.Fatalf("received unexpected relationship: %v", eIDs[0]) 143 } 144} 145