Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: SPDX decode error originator #221

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions json/reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package json

import (
"os"
"testing"
)

// TestRead tests that the SPDX Reader can still parse json documents correctly
// this protects against any of the custom unmarshalling code breaking given a new change set
func TestRead(t *testing.T) {
tt := []struct {
filename string
}{
{"test_fixtures/spdx2_3.json"},
}

for _, tc := range tt {
t.Run(tc.filename, func(t *testing.T) {
file, err := os.Open(tc.filename)
if err != nil {
t.Errorf("error opening %s: %v", tc.filename, err)
}
defer file.Close()
_, err = Read(file)
if err != nil {
t.Errorf("error reading %s: %v", tc.filename, err)
}
})
}
}
1,225 changes: 1,225 additions & 0 deletions json/test_fixtures/spdx2_3.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions spdx/v2/common/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,13 @@ func (o *Originator) UnmarshalJSON(data []byte) error {
return nil
}

originatorFields := strings.SplitN(originatorStr, ": ", 2)

originatorFields := strings.SplitN(originatorStr, ":", 2)
if len(originatorFields) != 2 {
return fmt.Errorf("failed to parse Originator '%s'", originatorStr)
}

o.OriginatorType = originatorFields[0]
o.Originator = originatorFields[1]

o.Originator = strings.TrimLeft(originatorFields[1], " \t")
return nil
}

Expand Down
47 changes: 47 additions & 0 deletions spdx/v2/common/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package common

import "testing"

func TestOriginator_UnmarshalJSON(t *testing.T) {
tt := []struct {
name string
data []byte
wantErr bool
}{
{
name: "valid originator",
data: []byte("\"Person: John Doe\""),
wantErr: false,
},
{
name: "valid originator with no space",
data: []byte("\"Person:John Doe\""),
wantErr: false,
},
{
name: "valid originator with no space - organization",
data: []byte("\"Organization:SPDX\""),
wantErr: false,
},
{
name: "valid originator with email",
data: []byte("\"Organization: ExampleCodeInspect ([email protected])\""),
wantErr: false,
},
{
name: "invalid originator with no type",
data: []byte("\"John Doe\""),
wantErr: true,
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
var o Originator
err := o.UnmarshalJSON(tc.data)
if (err != nil) != tc.wantErr {
t.Errorf("Originator.UnmarshalJSON() error = %v, wantErr %v", err, tc.wantErr)
}
})
}
}