From 6234aa66fed08ee03a2be31ec177152cefeedfd2 Mon Sep 17 00:00:00 2001 From: Steve Winslow Date: Sat, 20 Mar 2021 17:16:31 -0400 Subject: Fix special IDs for right-side 2.2 Relationships In SPDX 2.2, the right-hand side of Relationships are not limited to SPDX IDs; they can also include the special values NONE and NOASSERTION. To handle these, since Golang doesn't (to my knowledge) have a concept of union types, and since I don't want to use interface{}, this commit instead adds a new SpecialID field to DocElementID. When SpecialID is non-empty, it should be treated as being a "special" ID value, and DocumentRefID / ElementRefID should be ignored. (Unfortunately, we can't just use ElementRefID == "NONE", etc. for this purpose, because in theory an SPDX document could define the identifier SPDXRef-NONE to mean something. Even though they really, really shouldn't do that.) This commit updates tvloader and tvsaver to appropriately handle the possibility of NONE and NOASSERTION for this field. Signed-off-by: Steve Winslow --- tvsaver/saver2v2/save_relationship_test.go | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'tvsaver') diff --git a/tvsaver/saver2v2/save_relationship_test.go b/tvsaver/saver2v2/save_relationship_test.go index eecbb6c..ab98184 100644 --- a/tvsaver/saver2v2/save_relationship_test.go +++ b/tvsaver/saver2v2/save_relationship_test.go @@ -62,3 +62,53 @@ func TestSaver2_2RelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { t.Errorf("Expected %v, got %v", want.String(), got.String()) } } + +func TestSaver2_2RelationshipCanHaveNONEOnRight(t *testing.T) { + rln := &spdx.Relationship2_2{ + RefA: spdx.MakeDocElementID("", "PackageA"), + RefB: spdx.MakeDocElementSpecial("NONE"), + Relationship: "DEPENDS_ON", + } + + // what we want to get, as a buffer of bytes + // no trailing blank newline + want := bytes.NewBufferString("Relationship: SPDXRef-PackageA DEPENDS_ON NONE\n") + + // render as buffer of bytes + var got bytes.Buffer + err := renderRelationship2_2(rln, &got) + if err != nil { + t.Errorf("Expected nil error, got %v", err) + } + + // check that they match + c := bytes.Compare(want.Bytes(), got.Bytes()) + if c != 0 { + t.Errorf("Expected %v, got %v", want.String(), got.String()) + } +} + +func TestSaver2_2RelationshipCanHaveNOASSERTIONOnRight(t *testing.T) { + rln := &spdx.Relationship2_2{ + RefA: spdx.MakeDocElementID("", "PackageA"), + RefB: spdx.MakeDocElementSpecial("NOASSERTION"), + Relationship: "DEPENDS_ON", + } + + // what we want to get, as a buffer of bytes + // no trailing blank newline + want := bytes.NewBufferString("Relationship: SPDXRef-PackageA DEPENDS_ON NOASSERTION\n") + + // render as buffer of bytes + var got bytes.Buffer + err := renderRelationship2_2(rln, &got) + if err != nil { + t.Errorf("Expected nil error, got %v", err) + } + + // check that they match + c := bytes.Compare(want.Bytes(), got.Bytes()) + if c != 0 { + t.Errorf("Expected %v, got %v", want.String(), got.String()) + } +} -- cgit v1.2.3