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

[GSoC'21] Pr:7 . JSON saver bug fix #97

Merged
merged 2 commits into from Aug 13, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions jsonloader/parser2v2/parse_files_test.go
Expand Up @@ -197,13 +197,11 @@ func TestJSONSpdxDocument_parseJsonFiles2_2(t *testing.T) {
t.Errorf("JSONSpdxDocument.parseJsonFiles2_2() error = %v, wantErr %v", err, tt.wantErr)
}

// if !reflect.DeepEqual(tt.args.doc.UnpackagedFiles, tt.want) {
// t.Errorf("Load2_2() = %v, want %v", tt.args.doc.UnpackagedFiles, tt.want)
// }

for k, v := range tt.want {
if !reflect.DeepEqual(tt.args.doc.UnpackagedFiles[k], v) {
t.Errorf("Load2_2() = %v, want %v", tt.args.doc.UnpackagedFiles[k], v)
if !tt.wantErr {
for k, v := range tt.want {
if !reflect.DeepEqual(tt.args.doc.UnpackagedFiles[k], v) {
t.Errorf("Load2_2() = %v, want %v", tt.args.doc.UnpackagedFiles[k], v)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion jsonloader/parser2v2/parse_other_license.go
Expand Up @@ -34,7 +34,7 @@ func (spec JSONSpdxDocument) parseJsonOtherLicenses2_2(key string, value interfa
}
}
default:
return fmt.Errorf("received unknown tag %v in Annotation section", k)
return fmt.Errorf("received unknown tag %v in Licenses section", k)
}
}
doc.OtherLicenses = append(doc.OtherLicenses, &license)
Expand Down
31 changes: 27 additions & 4 deletions jsonloader/parser2v2/parse_other_license_test.go
Expand Up @@ -45,9 +45,20 @@ func TestJSONSpdxDocument_parseJsonOtherLicenses2_2(t *testing.T) {
},
}

otherLicensestest2 := []byte(`{
"hasExtractedLicensingInfos":[{
"extractedText" : "\"THE BEER-WARE LICENSE\" (Revision 42):\nphk@FreeBSD.ORG wrote this file. As long as you retain this notice you\ncan do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp </\nLicenseName: Beer-Ware License (Version 42)\nLicenseCrossReference: http://people.freebsd.org/~phk/\nLicenseComment: \nThe beerware license has a couple of other standard variants.",
"licensd" : "LicenseRef-Beerware-4.2"
swinslow marked this conversation as resolved.
Show resolved Hide resolved
}]
}
`)

var specs JSONSpdxDocument
json.Unmarshal(jsonData, &specs)

var specs2 JSONSpdxDocument
json.Unmarshal(otherLicensestest2, &specs2)

type args struct {
key string
value interface{}
Expand All @@ -72,19 +83,31 @@ func TestJSONSpdxDocument_parseJsonOtherLicenses2_2(t *testing.T) {
want: otherLicensestest1,
wantErr: false,
},
{
name: "failure test --> unknown tag",
spec: specs2,
args: args{
key: "hasExtractedLicensingInfos",
value: specs2["hasExtractedLicensingInfos"],
doc: &spdxDocument2_2{},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.spec.parseJsonOtherLicenses2_2(tt.args.key, tt.args.value, tt.args.doc); (err != nil) != tt.wantErr {
t.Errorf("JSONSpdxDocument.parseJsonOtherLicenses2_2() error = %v, wantErr %v", err, tt.wantErr)
}

for i := 0; i < len(tt.want); i++ {
if !reflect.DeepEqual(tt.args.doc.OtherLicenses[i], tt.want[i]) {
t.Errorf("Load2_2() = %v, want %v", tt.args.doc.OtherLicenses[i], tt.want[i])
if !tt.wantErr {
for i := 0; i < len(tt.want); i++ {
if !reflect.DeepEqual(tt.args.doc.OtherLicenses[i], tt.want[i]) {
t.Errorf("Load2_2() = %v, want %v", tt.args.doc.OtherLicenses[i], tt.want[i])
}
}
}

})
}
}
49 changes: 25 additions & 24 deletions jsonsaver/saver2v2/save_document_test.go
Expand Up @@ -177,14 +177,14 @@ func TestRenderDocument2_2(t *testing.T) {
"File": {
FileSPDXIdentifier: "File",
FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{
"MD5": {
Algorithm: "MD5",
Value: "624c1abb3664f4b35547e7c73864ad24",
},
"SHA1": {
Algorithm: "SHA1",
Value: "d6a770ba38583ed4bb4525bd96e50461655d2758",
},
// "MD5": {
// Algorithm: "MD5",
// Value: "624c1abb3664f4b35547e7c73864ad24",
// },
},
FileComment: "The concluded license was taken from the package level that the file was .\nThis information was found in the COPYING.txt file in the xyz directory.",
FileCopyrightText: "Copyright 2008-2010 John Smith",
Expand Down Expand Up @@ -317,6 +317,22 @@ func TestRenderDocument2_2(t *testing.T) {
},
},
"files": []interface{}{
map[string]interface{}{
"SPDXID": "SPDXRef-DoapSource",
"checksums": []interface{}{
map[string]interface{}{
"algorithm": "SHA1",
"checksumValue": "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
},
},
"copyrightText": "Copyright 2010, 2011 Source Auditor Inc.",
"fileContributors": []string{"Protecode Inc.", "SPDX Technical Team Members", "Open Logic Inc.", "Source Auditor Inc.", "Black Duck Software In.c"},
"fileDependencies": []string{"SPDXRef-JenaLib", "SPDXRef-CommonsLangSrc"},
"fileName": "./src/org/spdx/parser/DOAPProject.java",
"fileTypes": []string{"SOURCE"},
"licenseConcluded": "Apache-2.0",
"licenseInfoInFiles": []string{"Apache-2.0"},
},
map[string]interface{}{
"SPDXID": "SPDXRef-File",
"annotations": []interface{}{
Expand All @@ -328,14 +344,15 @@ func TestRenderDocument2_2(t *testing.T) {
},
},
"checksums": []interface{}{
map[string]interface{}{
"algorithm": "MD5",
"checksumValue": "624c1abb3664f4b35547e7c73864ad24",
},

map[string]interface{}{
"algorithm": "SHA1",
"checksumValue": "d6a770ba38583ed4bb4525bd96e50461655d2758",
},
// map[string]interface{}{
// "algorithm": "MD5",
// "checksumValue": "624c1abb3664f4b35547e7c73864ad24",
// },
},
"comment": "The concluded license was taken from the package level that the file was .\nThis information was found in the COPYING.txt file in the xyz directory.",
"copyrightText": "Copyright 2008-2010 John Smith",
Expand All @@ -347,22 +364,6 @@ func TestRenderDocument2_2(t *testing.T) {
"licenseInfoInFiles": []string{"GPL-2.0-only", "LicenseRef-2"},
"noticeText": "Copyright (c) 2001 Aaron Lehmann aaroni@vitelus.",
},
map[string]interface{}{
"SPDXID": "SPDXRef-DoapSource",
"checksums": []interface{}{
map[string]interface{}{
"algorithm": "SHA1",
"checksumValue": "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
},
},
"copyrightText": "Copyright 2010, 2011 Source Auditor Inc.",
"fileContributors": []string{"Protecode Inc.", "SPDX Technical Team Members", "Open Logic Inc.", "Source Auditor Inc.", "Black Duck Software In.c"},
"fileDependencies": []string{"SPDXRef-JenaLib", "SPDXRef-CommonsLangSrc"},
"fileName": "./src/org/spdx/parser/DOAPProject.java",
"fileTypes": []string{"SOURCE"},
"licenseConcluded": "Apache-2.0",
"licenseInfoInFiles": []string{"Apache-2.0"},
},
},
"snippets": []interface{}{
map[string]interface{}{
Expand Down
25 changes: 22 additions & 3 deletions jsonsaver/saver2v2/save_files.go
Expand Up @@ -3,15 +3,25 @@
package saver2v2

import (
"sort"

"github.com/spdx/tools-golang/spdx"
)

func renderFiles2_2(doc *spdx.Document2_2, jsondocument map[string]interface{}) ([]interface{}, error) {

var keys []string
for ke := range doc.UnpackagedFiles {
keys = append(keys, string(ke))
}
sort.Strings(keys)

var files []interface{}
for k, v := range doc.UnpackagedFiles {
// for k, v := range doc.UnpackagedFiles {
for _, k := range keys {
v := doc.UnpackagedFiles[spdx.ElementID(k)]
file := make(map[string]interface{})
file["SPDXID"] = spdx.RenderElementID(k)
file["SPDXID"] = spdx.RenderElementID(spdx.ElementID(k))
ann, _ := renderAnnotations2_2(doc.Annotations, spdx.MakeDocElementID("", string(v.FileSPDXIdentifier)))
if ann != nil {
file["annotations"] = ann
Expand All @@ -26,7 +36,16 @@ func renderFiles2_2(doc *spdx.Document2_2, jsondocument map[string]interface{})
// save package checksums
if v.FileChecksums != nil {
var checksums []interface{}
for _, value := range v.FileChecksums {

var algos []string
for alg := range v.FileChecksums {
algos = append(algos, string(alg))
}
sort.Strings(algos)

// for _, value := range v.FileChecksums {
for _, algo := range algos {
value := v.FileChecksums[spdx.ChecksumAlgorithm(algo)]
checksum := make(map[string]interface{})
checksum["algorithm"] = string(value.Algorithm)
checksum["checksumValue"] = value.Value
Expand Down
16 changes: 8 additions & 8 deletions jsonsaver/saver2v2/save_files_test.go
Expand Up @@ -75,10 +75,10 @@ func Test_renderFiles2_2(t *testing.T) {
Algorithm: "SHA1",
Value: "d6a770ba38583ed4bb4525bd96e50461655d2758",
},
// "MD5": {
// Algorithm: "MD5",
// Value: "624c1abb3664f4b35547e7c73864ad24",
// },
"MD5": {
Algorithm: "MD5",
Value: "624c1abb3664f4b35547e7c73864ad24",
},
},
FileComment: "The concluded license was taken from the package level that the file was .",
FileCopyrightText: "Copyright 2008-2010 John Smith",
Expand Down Expand Up @@ -107,14 +107,14 @@ func Test_renderFiles2_2(t *testing.T) {
},
},
"checksums": []interface{}{
map[string]interface{}{
"algorithm": "MD5",
"checksumValue": "624c1abb3664f4b35547e7c73864ad24",
},
map[string]interface{}{
"algorithm": "SHA1",
"checksumValue": "d6a770ba38583ed4bb4525bd96e50461655d2758",
},
// map[string]interface{}{
// "algorithm": "MD5",
// "checksumValue": "624c1abb3664f4b35547e7c73864ad24",
// },
},
"comment": "The concluded license was taken from the package level that the file was .",
"copyrightText": "Copyright 2008-2010 John Smith",
Expand Down
24 changes: 21 additions & 3 deletions jsonsaver/saver2v2/save_package.go
Expand Up @@ -4,16 +4,26 @@ package saver2v2

import (
"fmt"
"sort"

"github.com/spdx/tools-golang/spdx"
)

func renderPackage2_2(doc *spdx.Document2_2, jsondocument map[string]interface{}) ([]interface{}, error) {

var packages []interface{}
for k, v := range doc.Packages {

var keys []string
for ke := range doc.Packages {
keys = append(keys, string(ke))
}
sort.Strings(keys)

// for k, v := range doc.Packages {
for _, k := range keys {
v := doc.Packages[spdx.ElementID(k)]
pkg := make(map[string]interface{})
pkg["SPDXID"] = spdx.RenderElementID(k)
pkg["SPDXID"] = spdx.RenderElementID(spdx.ElementID(k))
ann, _ := renderAnnotations2_2(doc.Annotations, spdx.MakeDocElementID("", string(v.PackageSPDXIdentifier)))
if ann != nil {
pkg["annotations"] = ann
Expand All @@ -24,7 +34,15 @@ func renderPackage2_2(doc *spdx.Document2_2, jsondocument map[string]interface{}
// save package checksums
if v.PackageChecksums != nil {
var checksums []interface{}
for _, value := range v.PackageChecksums {

var algos []string
for alg := range v.PackageChecksums {
algos = append(algos, string(alg))
}
sort.Strings(algos)

for _, algo := range algos {
value := v.PackageChecksums[spdx.ChecksumAlgorithm(algo)]
checksum := make(map[string]interface{})
checksum["algorithm"] = string(value.Algorithm)
checksum["checksumValue"] = value.Value
Expand Down