diff options
author | Steve Winslow <steve@swinslow.net> | 2021-03-20 17:16:31 -0400 |
---|---|---|
committer | Steve Winslow <steve@swinslow.net> | 2021-03-20 17:16:31 -0400 |
commit | 6234aa66fed08ee03a2be31ec177152cefeedfd2 (patch) | |
tree | 8f62b5948cffe465927cc06a6eb83c815ac39d04 /tvsaver | |
parent | ad870ed6f31eb4918018af143611a5a2dc6aa798 (diff) | |
download | spdx-tools-6234aa66fed08ee03a2be31ec177152cefeedfd2.tar.gz |
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 <steve@swinslow.net>
Diffstat (limited to 'tvsaver')
-rw-r--r-- | tvsaver/saver2v2/save_relationship_test.go | 50 |
1 files changed, 50 insertions, 0 deletions
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()) + } +} |