Skip to content

Commit 6a271aa

Browse files
authored
fix: SPDX originator decode error (#221)
Signed-off-by: Christopher Phillips <[email protected]>
1 parent 4b477e8 commit 6a271aa

File tree

4 files changed

+1304
-4
lines changed

4 files changed

+1304
-4
lines changed

json/reader_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package json
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
// TestRead tests that the SPDX Reader can still parse json documents correctly
9+
// this protects against any of the custom unmarshalling code breaking given a new change set
10+
func TestRead(t *testing.T) {
11+
tt := []struct {
12+
filename string
13+
}{
14+
{"test_fixtures/spdx2_3.json"},
15+
}
16+
17+
for _, tc := range tt {
18+
t.Run(tc.filename, func(t *testing.T) {
19+
file, err := os.Open(tc.filename)
20+
if err != nil {
21+
t.Errorf("error opening %s: %v", tc.filename, err)
22+
}
23+
defer file.Close()
24+
_, err = Read(file)
25+
if err != nil {
26+
t.Errorf("error reading %s: %v", tc.filename, err)
27+
}
28+
})
29+
}
30+
}

json/test_fixtures/spdx2_3.json

Lines changed: 1225 additions & 0 deletions
Large diffs are not rendered by default.

spdx/v2/common/package.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,13 @@ func (o *Originator) UnmarshalJSON(data []byte) error {
7070
return nil
7171
}
7272

73-
originatorFields := strings.SplitN(originatorStr, ": ", 2)
74-
73+
originatorFields := strings.SplitN(originatorStr, ":", 2)
7574
if len(originatorFields) != 2 {
7675
return fmt.Errorf("failed to parse Originator '%s'", originatorStr)
7776
}
7877

7978
o.OriginatorType = originatorFields[0]
80-
o.Originator = originatorFields[1]
81-
79+
o.Originator = strings.TrimLeft(originatorFields[1], " \t")
8280
return nil
8381
}
8482

spdx/v2/common/package_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package common
2+
3+
import "testing"
4+
5+
func TestOriginator_UnmarshalJSON(t *testing.T) {
6+
tt := []struct {
7+
name string
8+
data []byte
9+
wantErr bool
10+
}{
11+
{
12+
name: "valid originator",
13+
data: []byte("\"Person: John Doe\""),
14+
wantErr: false,
15+
},
16+
{
17+
name: "valid originator with no space",
18+
data: []byte("\"Person:John Doe\""),
19+
wantErr: false,
20+
},
21+
{
22+
name: "valid originator with no space - organization",
23+
data: []byte("\"Organization:SPDX\""),
24+
wantErr: false,
25+
},
26+
{
27+
name: "valid originator with email",
28+
data: []byte("\"Organization: ExampleCodeInspect ([email protected])\""),
29+
wantErr: false,
30+
},
31+
{
32+
name: "invalid originator with no type",
33+
data: []byte("\"John Doe\""),
34+
wantErr: true,
35+
},
36+
}
37+
38+
for _, tc := range tt {
39+
t.Run(tc.name, func(t *testing.T) {
40+
var o Originator
41+
err := o.UnmarshalJSON(tc.data)
42+
if (err != nil) != tc.wantErr {
43+
t.Errorf("Originator.UnmarshalJSON() error = %v, wantErr %v", err, tc.wantErr)
44+
}
45+
})
46+
}
47+
}

0 commit comments

Comments
 (0)