diff options
author | specter25 <ujjwalcoding012@gmail.com> | 2021-06-12 18:33:08 +0530 |
---|---|---|
committer | specter25 <ujjwalcoding012@gmail.com> | 2021-06-12 18:33:08 +0530 |
commit | a0cfd0ef09eaa526e9ff4ecceaeab9933ae15b07 (patch) | |
tree | 4b09a97a2bf4f4100d1c1668f8d7ab6c8d1ced01 | |
parent | 7831df4930566d5adc846ca5021676166f6b3d6c (diff) | |
download | spdx-tools-a0cfd0ef09eaa526e9ff4ecceaeab9933ae15b07.tar.gz |
Create test file for utility function
Signed-off-by: specter25 <ujjwalcoding012@gmail.com>
-rw-r--r-- | jsonloader2v2/parse_annotations.go | 2 | ||||
-rw-r--r-- | jsonloader2v2/parse_annotations_test.go | 2 | ||||
-rw-r--r-- | jsonloader2v2/parse_creation_info.go | 2 | ||||
-rw-r--r-- | jsonloader2v2/parse_creation_info_test.go | 2 | ||||
-rw-r--r-- | jsonloader2v2/parse_reationship.go | 2 | ||||
-rw-r--r-- | jsonloader2v2/parse_reationship_test.go | 2 | ||||
-rw-r--r-- | jsonloader2v2/types.go | 2 | ||||
-rw-r--r-- | jsonloader2v2/util.go | 19 | ||||
-rw-r--r-- | jsonloader2v2/util_test.go | 156 |
9 files changed, 187 insertions, 2 deletions
diff --git a/jsonloader2v2/parse_annotations.go b/jsonloader2v2/parse_annotations.go index 9126370..e233bc3 100644 --- a/jsonloader2v2/parse_annotations.go +++ b/jsonloader2v2/parse_annotations.go @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + package jsonloader2v2 import ( diff --git a/jsonloader2v2/parse_annotations_test.go b/jsonloader2v2/parse_annotations_test.go index 6e0690b..b29f29d 100644 --- a/jsonloader2v2/parse_annotations_test.go +++ b/jsonloader2v2/parse_annotations_test.go @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + package jsonloader2v2 import ( diff --git a/jsonloader2v2/parse_creation_info.go b/jsonloader2v2/parse_creation_info.go index 156be6d..95f5bc0 100644 --- a/jsonloader2v2/parse_creation_info.go +++ b/jsonloader2v2/parse_creation_info.go @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + package jsonloader2v2 import ( diff --git a/jsonloader2v2/parse_creation_info_test.go b/jsonloader2v2/parse_creation_info_test.go index 3580902..5391f04 100644 --- a/jsonloader2v2/parse_creation_info_test.go +++ b/jsonloader2v2/parse_creation_info_test.go @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + package jsonloader2v2 import ( diff --git a/jsonloader2v2/parse_reationship.go b/jsonloader2v2/parse_reationship.go index 64b1b12..915c802 100644 --- a/jsonloader2v2/parse_reationship.go +++ b/jsonloader2v2/parse_reationship.go @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + package jsonloader2v2 import ( diff --git a/jsonloader2v2/parse_reationship_test.go b/jsonloader2v2/parse_reationship_test.go index 57ea266..ac33e60 100644 --- a/jsonloader2v2/parse_reationship_test.go +++ b/jsonloader2v2/parse_reationship_test.go @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + package jsonloader2v2 import ( diff --git a/jsonloader2v2/types.go b/jsonloader2v2/types.go index 61c3941..87dd3b6 100644 --- a/jsonloader2v2/types.go +++ b/jsonloader2v2/types.go @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + package jsonloader2v2 import "github.com/spdx/tools-golang/spdx" diff --git a/jsonloader2v2/util.go b/jsonloader2v2/util.go index eeafa90..afbc1f8 100644 --- a/jsonloader2v2/util.go +++ b/jsonloader2v2/util.go @@ -9,8 +9,6 @@ import ( "github.com/spdx/tools-golang/spdx" ) -//TODO: inspect all functions - // used to extract key / value from embedded substrings // returns subkey, subvalue, nil if no error, or "", "", error otherwise func extractSubs(value string) (string, string, error) { @@ -75,6 +73,23 @@ func extractDocElementID(value string) (spdx.DocElementID, error) { return spdx.DocElementID{DocumentRefID: docRefID, ElementRefID: spdx.ElementID(eltRefID)}, nil } +// used to extract SPDXRef values from an SPDX Identifier, OR "special" strings +// from a specified set of permitted values. The primary use case for this is +// the right-hand side of Relationships, where beginning in SPDX 2.2 the values +// "NONE" and "NOASSERTION" are permitted. If the value does not match one of +// the specified permitted values, it will fall back to the ordinary +// DocElementID extractor. +func extractDocElementSpecial(value string, permittedSpecial []string) (spdx.DocElementID, error) { + // check value against special set first + for _, sp := range permittedSpecial { + if sp == value { + return spdx.DocElementID{SpecialID: sp}, nil + } + } + // not found, fall back to regular search + return extractDocElementID(value) +} + // used to extract SPDXRef values only from an SPDX Identifier which can point // to this document only. Use extractDocElementID for parsing IDs that can // refer either to this document or a different one. diff --git a/jsonloader2v2/util_test.go b/jsonloader2v2/util_test.go new file mode 100644 index 0000000..de5b843 --- /dev/null +++ b/jsonloader2v2/util_test.go @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +package jsonloader2v2 + +import ( + "testing" + + "github.com/spdx/tools-golang/spdx" +) + +// ===== Helper function tests ===== + +func TestCanExtractSubvalues(t *testing.T) { + subkey, subvalue, err := extractSubs("SHA1: abc123") + if err != nil { + t.Errorf("got error when calling extractSubs: %v", err) + } + if subkey != "SHA1" { + t.Errorf("got %v for subkey", subkey) + } + if subvalue != "abc123" { + t.Errorf("got %v for subvalue", subvalue) + } +} + +func TestReturnsErrorForInvalidSubvalueFormat(t *testing.T) { + _, _, err := extractSubs("blah") + if err == nil { + t.Errorf("expected error when calling extractSubs for invalid format (0 colons), got nil") + } +} + +func TestCanExtractDocumentAndElementRefsFromID(t *testing.T) { + // test with valid ID in this document + helperForExtractDocElementID(t, "SPDXRef-file1", false, "", "file1") + // test with valid ID in another document + helperForExtractDocElementID(t, "DocumentRef-doc2:SPDXRef-file2", false, "doc2", "file2") + // test with invalid ID in this document + helperForExtractDocElementID(t, "a:SPDXRef-file1", true, "", "") + helperForExtractDocElementID(t, "file1", true, "", "") + helperForExtractDocElementID(t, "SPDXRef-", true, "", "") + helperForExtractDocElementID(t, "SPDXRef-file1:", true, "", "") + // test with invalid ID in another document + helperForExtractDocElementID(t, "DocumentRef-doc2", true, "", "") + helperForExtractDocElementID(t, "DocumentRef-doc2:", true, "", "") + helperForExtractDocElementID(t, "DocumentRef-doc2:SPDXRef-", true, "", "") + helperForExtractDocElementID(t, "DocumentRef-doc2:a", true, "", "") + helperForExtractDocElementID(t, "DocumentRef-:", true, "", "") + helperForExtractDocElementID(t, "DocumentRef-:SPDXRef-file1", true, "", "") + // test with invalid formats + helperForExtractDocElementID(t, "DocumentRef-doc2:SPDXRef-file1:file2", true, "", "") +} + +func helperForExtractDocElementID(t *testing.T, tst string, wantErr bool, wantDoc string, wantElt string) { + deID, err := extractDocElementID(tst) + if err != nil && wantErr == false { + t.Errorf("testing %v: expected nil error, got %v", tst, err) + } + if err == nil && wantErr == true { + t.Errorf("testing %v: expected non-nil error, got nil", tst) + } + if deID.DocumentRefID != wantDoc { + if wantDoc == "" { + t.Errorf("testing %v: want empty string for DocumentRefID, got %v", tst, deID.DocumentRefID) + } else { + t.Errorf("testing %v: want %v for DocumentRefID, got %v", tst, wantDoc, deID.DocumentRefID) + } + } + if deID.ElementRefID != spdx.ElementID(wantElt) { + if wantElt == "" { + t.Errorf("testing %v: want empty string for ElementRefID, got %v", tst, deID.ElementRefID) + } else { + t.Errorf("testing %v: want %v for ElementRefID, got %v", tst, wantElt, deID.ElementRefID) + } + } +} + +func TestCanExtractSpecialDocumentIDs(t *testing.T) { + permittedSpecial := []string{"NONE", "NOASSERTION"} + // test with valid special values + helperForExtractDocElementSpecial(t, permittedSpecial, "NONE", false, "", "", "NONE") + helperForExtractDocElementSpecial(t, permittedSpecial, "NOASSERTION", false, "", "", "NOASSERTION") + // test with valid regular IDs + helperForExtractDocElementSpecial(t, permittedSpecial, "SPDXRef-file1", false, "", "file1", "") + helperForExtractDocElementSpecial(t, permittedSpecial, "DocumentRef-doc2:SPDXRef-file2", false, "doc2", "file2", "") + helperForExtractDocElementSpecial(t, permittedSpecial, "a:SPDXRef-file1", true, "", "", "") + helperForExtractDocElementSpecial(t, permittedSpecial, "DocumentRef-doc2", true, "", "", "") + // test with invalid other words not on permitted list + helperForExtractDocElementSpecial(t, permittedSpecial, "FOO", true, "", "", "") +} + +func helperForExtractDocElementSpecial(t *testing.T, permittedSpecial []string, tst string, wantErr bool, wantDoc string, wantElt string, wantSpecial string) { + deID, err := extractDocElementSpecial(tst, permittedSpecial) + if err != nil && wantErr == false { + t.Errorf("testing %v: expected nil error, got %v", tst, err) + } + if err == nil && wantErr == true { + t.Errorf("testing %v: expected non-nil error, got nil", tst) + } + if deID.DocumentRefID != wantDoc { + if wantDoc == "" { + t.Errorf("testing %v: want empty string for DocumentRefID, got %v", tst, deID.DocumentRefID) + } else { + t.Errorf("testing %v: want %v for DocumentRefID, got %v", tst, wantDoc, deID.DocumentRefID) + } + } + if deID.ElementRefID != spdx.ElementID(wantElt) { + if wantElt == "" { + t.Errorf("testing %v: want empty string for ElementRefID, got %v", tst, deID.ElementRefID) + } else { + t.Errorf("testing %v: want %v for ElementRefID, got %v", tst, wantElt, deID.ElementRefID) + } + } + if deID.SpecialID != wantSpecial { + if wantSpecial == "" { + t.Errorf("testing %v: want empty string for SpecialID, got %v", tst, deID.SpecialID) + } else { + t.Errorf("testing %v: want %v for SpecialID, got %v", tst, wantSpecial, deID.SpecialID) + } + } +} + +func TestCanExtractElementRefsOnlyFromID(t *testing.T) { + // test with valid ID in this document + helperForExtractElementID(t, "SPDXRef-file1", false, "file1") + // test with valid ID in another document + helperForExtractElementID(t, "DocumentRef-doc2:SPDXRef-file2", true, "") + // test with invalid ID in this document + helperForExtractElementID(t, "a:SPDXRef-file1", true, "") + helperForExtractElementID(t, "file1", true, "") + helperForExtractElementID(t, "SPDXRef-", true, "") + helperForExtractElementID(t, "SPDXRef-file1:", true, "") + // test with invalid ID in another document + helperForExtractElementID(t, "DocumentRef-doc2", true, "") + helperForExtractElementID(t, "DocumentRef-doc2:", true, "") + helperForExtractElementID(t, "DocumentRef-doc2:SPDXRef-", true, "") + helperForExtractElementID(t, "DocumentRef-doc2:a", true, "") + helperForExtractElementID(t, "DocumentRef-:", true, "") + helperForExtractElementID(t, "DocumentRef-:SPDXRef-file1", true, "") +} + +func helperForExtractElementID(t *testing.T, tst string, wantErr bool, wantElt string) { + eID, err := extractElementID(tst) + if err != nil && wantErr == false { + t.Errorf("testing %v: expected nil error, got %v", tst, err) + } + if err == nil && wantErr == true { + t.Errorf("testing %v: expected non-nil error, got nil", tst) + } + if eID != spdx.ElementID(wantElt) { + if wantElt == "" { + t.Errorf("testing %v: want emptyString for ElementRefID, got %v", tst, eID) + } else { + t.Errorf("testing %v: want %v for ElementRefID, got %v", tst, wantElt, eID) + } + } +} |