aboutsummaryrefslogtreecommitdiff
path: root/spdxlib/relationships_test.go
blob: 577288773760810b96cd68dfbaa95ae136a9f4c4 (plain)
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
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package spdxlib

import (
	"testing"

	"github.com/spdx/tools-golang/spdx/common"
	"github.com/spdx/tools-golang/spdx/v2_1"
	"github.com/spdx/tools-golang/spdx/v2_2"
)

// ===== 2.1 tests =====

func Test2_1FilterForDependencies(t *testing.T) {
	// set up document and some packages and relationships
	doc := &v2_1.Document{
		SPDXVersion:    "SPDX-2.1",
		DataLicense:    "CC0-1.0",
		SPDXIdentifier: common.ElementID("DOCUMENT"),
		CreationInfo:   &v2_1.CreationInfo{},
		Packages: []*v2_1.Package{
			{PackageName: "pkg1", PackageSPDXIdentifier: "p1"},
			{PackageName: "pkg2", PackageSPDXIdentifier: "p2"},
			{PackageName: "pkg3", PackageSPDXIdentifier: "p3"},
			{PackageName: "pkg4", PackageSPDXIdentifier: "p4"},
			{PackageName: "pkg5", PackageSPDXIdentifier: "p5"},
		},
		Relationships: []*v2_1.Relationship{
			{
				RefA:         common.MakeDocElementID("", "DOCUMENT"),
				RefB:         common.MakeDocElementID("", "p1"),
				Relationship: "DESCRIBES",
			},
			{
				RefA:         common.MakeDocElementID("", "DOCUMENT"),
				RefB:         common.MakeDocElementID("", "p5"),
				Relationship: "DESCRIBES",
			},
			{
				RefA:         common.MakeDocElementID("", "p4"),
				RefB:         common.MakeDocElementID("", "DOCUMENT"),
				Relationship: "DESCRIBED_BY",
			},
			{
				RefA:         common.MakeDocElementID("", "p1"),
				RefB:         common.MakeDocElementID("", "p2"),
				Relationship: "DEPENDS_ON",
			},
			{
				RefA:         common.MakeDocElementID("", "p3"),
				RefB:         common.MakeDocElementID("", "p4"),
				Relationship: "DEPENDENCY_OF",
			},
		},
	}

	eIDs, err := FilterRelationships2_1(doc, func(relationship *v2_1.Relationship) *common.ElementID {
		p1EID := common.MakeDocElementID("", "p1")
		if relationship.Relationship == "DEPENDS_ON" && relationship.RefA == p1EID {
			return &relationship.RefB.ElementRefID
		} else if relationship.Relationship == "DEPENDENCY_OF" && relationship.RefB == p1EID {
			return &relationship.RefA.ElementRefID
		}

		return nil
	})
	if err != nil {
		t.Fatalf("expected non-nil err, got: %s", err.Error())
	}

	if len(eIDs) != 1 {
		t.Fatalf("expected 1 ElementID, got: %v", eIDs)
	}

	if eIDs[0] != common.MakeDocElementID("", "p2").ElementRefID {
		t.Fatalf("received unexpected relationship: %v", eIDs[0])
	}
}

// ===== 2.2 tests =====

func Test2_2FindsDependsOnRelationships(t *testing.T) {
	// set up document and some packages and relationships
	doc := &v2_2.Document{
		SPDXVersion:    "SPDX-2.2",
		DataLicense:    "CC0-1.0",
		SPDXIdentifier: common.ElementID("DOCUMENT"),
		CreationInfo:   &v2_2.CreationInfo{},
		Packages: []*v2_2.Package{
			{PackageName: "pkg1", PackageSPDXIdentifier: "p1"},
			{PackageName: "pkg2", PackageSPDXIdentifier: "p2"},
			{PackageName: "pkg3", PackageSPDXIdentifier: "p3"},
			{PackageName: "pkg4", PackageSPDXIdentifier: "p4"},
			{PackageName: "pkg5", PackageSPDXIdentifier: "p5"},
		},
		Relationships: []*v2_2.Relationship{
			{
				RefA:         common.MakeDocElementID("", "DOCUMENT"),
				RefB:         common.MakeDocElementID("", "p1"),
				Relationship: "DESCRIBES",
			},
			{
				RefA:         common.MakeDocElementID("", "DOCUMENT"),
				RefB:         common.MakeDocElementID("", "p5"),
				Relationship: "DESCRIBES",
			},
			// inverse relationship -- should also get detected
			{
				RefA:         common.MakeDocElementID("", "p4"),
				RefB:         common.MakeDocElementID("", "DOCUMENT"),
				Relationship: "DESCRIBED_BY",
			},
			// different relationship
			{
				RefA:         common.MakeDocElementID("", "p1"),
				RefB:         common.MakeDocElementID("", "p2"),
				Relationship: "DEPENDS_ON",
			},
		},
	}

	eIDs, err := FilterRelationships2_2(doc, func(relationship *v2_2.Relationship) *common.ElementID {
		p1EID := common.MakeDocElementID("", "p1")
		if relationship.Relationship == "DEPENDS_ON" && relationship.RefA == p1EID {
			return &relationship.RefB.ElementRefID
		} else if relationship.Relationship == "DEPENDENCY_OF" && relationship.RefB == p1EID {
			return &relationship.RefA.ElementRefID
		}

		return nil
	})
	if err != nil {
		t.Fatalf("expected non-nil err, got: %s", err.Error())
	}

	if len(eIDs) != 1 {
		t.Fatalf("expected 1 ElementID, got: %v", eIDs)
	}

	if eIDs[0] != common.MakeDocElementID("", "p2").ElementRefID {
		t.Fatalf("received unexpected relationship: %v", eIDs[0])
	}
}