// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later package saver2v2 import ( "bytes" "testing" "github.com/spdx/tools-golang/spdx" ) // ===== File section Saver tests ===== func TestSaver2_2FileSavesText(t *testing.T) { f := &spdx.File2_2{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: spdx.ElementID("File123"), FileType: []string{ "TEXT", "DOCUMENTATION", }, FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{ spdx.SHA1: spdx.Checksum{ Algorithm: spdx.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c", }, spdx.SHA256: spdx.Checksum{ Algorithm: spdx.SHA256, Value: "11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd", }, spdx.MD5: spdx.Checksum{ Algorithm: spdx.MD5, Value: "624c1abb3664f4b35547e7c73864ad24", }, }, LicenseConcluded: "Apache-2.0", LicenseInfoInFile: []string{ "Apache-2.0", "Apache-1.1", }, LicenseComments: "this is a license comment(s)", FileCopyrightText: "Copyright (c) Jane Doe", ArtifactOfProjects: []*spdx.ArtifactOfProject2_2{ &spdx.ArtifactOfProject2_2{ Name: "project1", HomePage: "http://example.com/1/", URI: "http://example.com/1/uri.whatever", }, &spdx.ArtifactOfProject2_2{ Name: "project2", }, &spdx.ArtifactOfProject2_2{ Name: "project3", HomePage: "http://example.com/3/", }, &spdx.ArtifactOfProject2_2{ Name: "project4", URI: "http://example.com/4/uri.whatever", }, }, FileComment: "this is a file comment", FileNotice: "This file may be used under either Apache-2.0 or Apache-1.1.", FileContributor: []string{ "John Doe jdoe@example.com", "EvilCorp", }, FileAttributionTexts: []string{ "attributions", `multi-line attribution`, }, FileDependencies: []string{ "f-1.txt", "g.txt", }, } // what we want to get, as a buffer of bytes want := bytes.NewBufferString(`FileName: /tmp/whatever.txt SPDXID: SPDXRef-File123 FileType: TEXT FileType: DOCUMENTATION FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c FileChecksum: SHA256: 11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd FileChecksum: MD5: 624c1abb3664f4b35547e7c73864ad24 LicenseConcluded: Apache-2.0 LicenseInfoInFile: Apache-2.0 LicenseInfoInFile: Apache-1.1 LicenseComments: this is a license comment(s) FileCopyrightText: Copyright (c) Jane Doe ArtifactOfProjectName: project1 ArtifactOfProjectHomePage: http://example.com/1/ ArtifactOfProjectURI: http://example.com/1/uri.whatever ArtifactOfProjectName: project2 ArtifactOfProjectName: project3 ArtifactOfProjectHomePage: http://example.com/3/ ArtifactOfProjectName: project4 ArtifactOfProjectURI: http://example.com/4/uri.whatever FileComment: this is a file comment FileNotice: This file may be used under either Apache-2.0 or Apache-1.1. FileContributor: John Doe jdoe@example.com FileContributor: EvilCorp FileAttributionText: attributions FileAttributionText: multi-line attribution FileDependency: f-1.txt FileDependency: g.txt `) // render as buffer of bytes var got bytes.Buffer err := renderFile2_2(f, &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_2FileSavesSnippetsAlso(t *testing.T) { sn1 := &spdx.Snippet2_2{ SnippetSPDXIdentifier: spdx.ElementID("Snippet19"), SnippetFromFileSPDXIdentifier: spdx.MakeDocElementID("", "File123"), SnippetByteRangeStart: 17, SnippetByteRangeEnd: 209, SnippetLicenseConcluded: "GPL-2.0-or-later", SnippetCopyrightText: "Copyright (c) John Doe 20x6", } sn2 := &spdx.Snippet2_2{ SnippetSPDXIdentifier: spdx.ElementID("Snippet20"), SnippetFromFileSPDXIdentifier: spdx.MakeDocElementID("", "File123"), SnippetByteRangeStart: 268, SnippetByteRangeEnd: 309, SnippetLicenseConcluded: "WTFPL", SnippetCopyrightText: "NOASSERTION", } sns := map[spdx.ElementID]*spdx.Snippet2_2{ spdx.ElementID("Snippet19"): sn1, spdx.ElementID("Snippet20"): sn2, } f := &spdx.File2_2{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: spdx.ElementID("File123"), FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{ spdx.SHA1: spdx.Checksum{ Algorithm: spdx.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c", }, }, LicenseConcluded: "Apache-2.0", LicenseInfoInFile: []string{ "Apache-2.0", }, FileCopyrightText: "Copyright (c) Jane Doe", Snippets: sns, } // what we want to get, as a buffer of bytes want := bytes.NewBufferString(`FileName: /tmp/whatever.txt SPDXID: SPDXRef-File123 FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c LicenseConcluded: Apache-2.0 LicenseInfoInFile: Apache-2.0 FileCopyrightText: Copyright (c) Jane Doe SnippetSPDXIdentifier: SPDXRef-Snippet19 SnippetFromFileSPDXID: SPDXRef-File123 SnippetByteRange: 17:209 SnippetLicenseConcluded: GPL-2.0-or-later SnippetCopyrightText: Copyright (c) John Doe 20x6 SnippetSPDXIdentifier: SPDXRef-Snippet20 SnippetFromFileSPDXID: SPDXRef-File123 SnippetByteRange: 268:309 SnippetLicenseConcluded: WTFPL SnippetCopyrightText: NOASSERTION `) // render as buffer of bytes var got bytes.Buffer err := renderFile2_2(f, &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_2FileOmitsOptionalFieldsIfEmpty(t *testing.T) { f := &spdx.File2_2{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: spdx.ElementID("File123"), FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{ spdx.SHA1: spdx.Checksum{ Algorithm: spdx.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c", }, }, LicenseConcluded: "Apache-2.0", LicenseInfoInFile: []string{ "Apache-2.0", }, FileCopyrightText: "Copyright (c) Jane Doe", } // what we want to get, as a buffer of bytes want := bytes.NewBufferString(`FileName: /tmp/whatever.txt SPDXID: SPDXRef-File123 FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c LicenseConcluded: Apache-2.0 LicenseInfoInFile: Apache-2.0 FileCopyrightText: Copyright (c) Jane Doe `) // render as buffer of bytes var got bytes.Buffer err := renderFile2_2(f, &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_2FileWrapsCopyrightMultiLine(t *testing.T) { f := &spdx.File2_2{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: spdx.ElementID("File123"), FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{ spdx.SHA1: spdx.Checksum{ Algorithm: spdx.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c", }, }, LicenseConcluded: "Apache-2.0", LicenseInfoInFile: []string{ "Apache-2.0", }, FileCopyrightText: `Copyright (c) Jane Doe Copyright (c) John Doe`, } // what we want to get, as a buffer of bytes want := bytes.NewBufferString(`FileName: /tmp/whatever.txt SPDXID: SPDXRef-File123 FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c LicenseConcluded: Apache-2.0 LicenseInfoInFile: Apache-2.0 FileCopyrightText: Copyright (c) Jane Doe Copyright (c) John Doe `) // render as buffer of bytes var got bytes.Buffer err := renderFile2_2(f, &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_2FileWrapsCommentsAndNoticesMultiLine(t *testing.T) { f := &spdx.File2_2{ FileName: "/tmp/whatever.txt", FileSPDXIdentifier: spdx.ElementID("File123"), FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{ spdx.SHA1: spdx.Checksum{ Algorithm: spdx.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c", }, }, LicenseComments: `this is a multi-line license comment`, LicenseConcluded: "Apache-2.0", LicenseInfoInFile: []string{ "Apache-2.0", }, FileCopyrightText: "Copyright (c) Jane Doe", FileComment: `this is a multi-line file comment`, FileNotice: `This file may be used under either Apache-2.0 or Apache-1.1.`, } // what we want to get, as a buffer of bytes want := bytes.NewBufferString(`FileName: /tmp/whatever.txt SPDXID: SPDXRef-File123 FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c LicenseConcluded: Apache-2.0 LicenseInfoInFile: Apache-2.0 LicenseComments: this is a multi-line license comment FileCopyrightText: Copyright (c) Jane Doe FileComment: this is a multi-line file comment FileNotice: This file may be used under either Apache-2.0 or Apache-1.1. `) // render as buffer of bytes var got bytes.Buffer err := renderFile2_2(f, &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()) } }