1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2 3package saver2v1 4 5import ( 6 "bytes" 7 "testing" 8 9 "github.com/spdx/tools-golang/spdx/common" 10 "github.com/spdx/tools-golang/spdx/v2_1" 11) 12 13// ===== File section Saver tests ===== 14func TestSaver2_1FileSavesText(t *testing.T) { 15 f := &v2_1.File{ 16 FileName: "/tmp/whatever.txt", 17 FileSPDXIdentifier: common.ElementID("File123"), 18 FileTypes: []string{ 19 "TEXT", 20 "DOCUMENTATION", 21 }, 22 Checksums: []common.Checksum{ 23 {Algorithm: common.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c"}, 24 {Algorithm: common.SHA256, Value: "11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd"}, 25 {Algorithm: common.MD5, Value: "624c1abb3664f4b35547e7c73864ad24"}, 26 }, 27 LicenseConcluded: "Apache-2.0", 28 LicenseInfoInFiles: []string{ 29 "Apache-2.0", 30 "Apache-1.1", 31 }, 32 LicenseComments: "this is a license comment(s)", 33 FileCopyrightText: "Copyright (c) Jane Doe", 34 ArtifactOfProjects: []*v2_1.ArtifactOfProject{ 35 &v2_1.ArtifactOfProject{ 36 Name: "project1", 37 HomePage: "http://example.com/1/", 38 URI: "http://example.com/1/uri.whatever", 39 }, 40 &v2_1.ArtifactOfProject{ 41 Name: "project2", 42 }, 43 &v2_1.ArtifactOfProject{ 44 Name: "project3", 45 HomePage: "http://example.com/3/", 46 }, 47 &v2_1.ArtifactOfProject{ 48 Name: "project4", 49 URI: "http://example.com/4/uri.whatever", 50 }, 51 }, 52 FileComment: "this is a file comment", 53 FileNotice: "This file may be used under either Apache-2.0 or Apache-1.1.", 54 FileContributors: []string{ 55 "John Doe [email protected]", 56 "EvilCorp", 57 }, 58 FileDependencies: []string{ 59 "f-1.txt", 60 "g.txt", 61 }, 62 } 63 64 // what we want to get, as a buffer of bytes 65 want := bytes.NewBufferString(`FileName: /tmp/whatever.txt 66SPDXID: SPDXRef-File123 67FileType: TEXT 68FileType: DOCUMENTATION 69FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c 70FileChecksum: SHA256: 11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd 71FileChecksum: MD5: 624c1abb3664f4b35547e7c73864ad24 72LicenseConcluded: Apache-2.0 73LicenseInfoInFile: Apache-2.0 74LicenseInfoInFile: Apache-1.1 75LicenseComments: this is a license comment(s) 76FileCopyrightText: Copyright (c) Jane Doe 77ArtifactOfProjectName: project1 78ArtifactOfProjectHomePage: http://example.com/1/ 79ArtifactOfProjectURI: http://example.com/1/uri.whatever 80ArtifactOfProjectName: project2 81ArtifactOfProjectName: project3 82ArtifactOfProjectHomePage: http://example.com/3/ 83ArtifactOfProjectName: project4 84ArtifactOfProjectURI: http://example.com/4/uri.whatever 85FileComment: this is a file comment 86FileNotice: This file may be used under either Apache-2.0 or Apache-1.1. 87FileContributor: John Doe jdoe@example.com 88FileContributor: EvilCorp 89FileDependency: f-1.txt 90FileDependency: g.txt 91 92`) 93 94 // render as buffer of bytes 95 var got bytes.Buffer 96 err := renderFile2_1(f, &got) 97 if err != nil { 98 t.Errorf("Expected nil error, got %v", err) 99 } 100 101 // check that they match 102 c := bytes.Compare(want.Bytes(), got.Bytes()) 103 if c != 0 { 104 t.Errorf("Expected %v, got %v", want.String(), got.String()) 105 } 106} 107 108func TestSaver2_1FileSavesSnippetsAlso(t *testing.T) { 109 sn1 := &v2_1.Snippet{ 110 SnippetSPDXIdentifier: common.ElementID("Snippet19"), 111 SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File123").ElementRefID, 112 Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 17}, EndPointer: common.SnippetRangePointer{Offset: 209}}}, 113 SnippetLicenseConcluded: "GPL-2.0-or-later", 114 SnippetCopyrightText: "Copyright (c) John Doe 20x6", 115 } 116 117 sn2 := &v2_1.Snippet{ 118 SnippetSPDXIdentifier: common.ElementID("Snippet20"), 119 SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File123").ElementRefID, 120 Ranges: []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 268}, EndPointer: common.SnippetRangePointer{Offset: 309}}}, 121 SnippetLicenseConcluded: "WTFPL", 122 SnippetCopyrightText: "NOASSERTION", 123 } 124 125 sns := map[common.ElementID]*v2_1.Snippet{ 126 common.ElementID("Snippet19"): sn1, 127 common.ElementID("Snippet20"): sn2, 128 } 129 130 f := &v2_1.File{ 131 FileName: "/tmp/whatever.txt", 132 FileSPDXIdentifier: common.ElementID("File123"), 133 Checksums: []common.Checksum{ 134 {Algorithm: common.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c"}, 135 }, 136 LicenseConcluded: "Apache-2.0", 137 LicenseInfoInFiles: []string{ 138 "Apache-2.0", 139 }, 140 FileCopyrightText: "Copyright (c) Jane Doe", 141 Snippets: sns, 142 } 143 144 // what we want to get, as a buffer of bytes 145 want := bytes.NewBufferString(`FileName: /tmp/whatever.txt 146SPDXID: SPDXRef-File123 147FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c 148LicenseConcluded: Apache-2.0 149LicenseInfoInFile: Apache-2.0 150FileCopyrightText: Copyright (c) Jane Doe 151 152SnippetSPDXID: SPDXRef-Snippet19 153SnippetFromFileSPDXID: SPDXRef-File123 154SnippetByteRange: 17:209 155SnippetLicenseConcluded: GPL-2.0-or-later 156SnippetCopyrightText: Copyright (c) John Doe 20x6 157 158SnippetSPDXID: SPDXRef-Snippet20 159SnippetFromFileSPDXID: SPDXRef-File123 160SnippetByteRange: 268:309 161SnippetLicenseConcluded: WTFPL 162SnippetCopyrightText: NOASSERTION 163 164`) 165 166 // render as buffer of bytes 167 var got bytes.Buffer 168 err := renderFile2_1(f, &got) 169 if err != nil { 170 t.Errorf("Expected nil error, got %v", err) 171 } 172 173 // check that they match 174 c := bytes.Compare(want.Bytes(), got.Bytes()) 175 if c != 0 { 176 t.Errorf("Expected %v, got %v", want.String(), got.String()) 177 } 178} 179 180func TestSaver2_1FileOmitsOptionalFieldsIfEmpty(t *testing.T) { 181 f := &v2_1.File{ 182 FileName: "/tmp/whatever.txt", 183 FileSPDXIdentifier: common.ElementID("File123"), 184 Checksums: []common.Checksum{ 185 {Algorithm: common.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c"}, 186 }, 187 LicenseConcluded: "Apache-2.0", 188 LicenseInfoInFiles: []string{ 189 "Apache-2.0", 190 }, 191 FileCopyrightText: "Copyright (c) Jane Doe", 192 } 193 194 // what we want to get, as a buffer of bytes 195 want := bytes.NewBufferString(`FileName: /tmp/whatever.txt 196SPDXID: SPDXRef-File123 197FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c 198LicenseConcluded: Apache-2.0 199LicenseInfoInFile: Apache-2.0 200FileCopyrightText: Copyright (c) Jane Doe 201 202`) 203 204 // render as buffer of bytes 205 var got bytes.Buffer 206 err := renderFile2_1(f, &got) 207 if err != nil { 208 t.Errorf("Expected nil error, got %v", err) 209 } 210 211 // check that they match 212 c := bytes.Compare(want.Bytes(), got.Bytes()) 213 if c != 0 { 214 t.Errorf("Expected %v, got %v", want.String(), got.String()) 215 } 216} 217 218func TestSaver2_1FileWrapsCopyrightMultiLine(t *testing.T) { 219 f := &v2_1.File{ 220 FileName: "/tmp/whatever.txt", 221 FileSPDXIdentifier: common.ElementID("File123"), 222 Checksums: []common.Checksum{ 223 {Algorithm: common.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c"}, 224 }, 225 LicenseConcluded: "Apache-2.0", 226 LicenseInfoInFiles: []string{ 227 "Apache-2.0", 228 }, 229 FileCopyrightText: `Copyright (c) Jane Doe 230Copyright (c) John Doe`, 231 } 232 233 // what we want to get, as a buffer of bytes 234 want := bytes.NewBufferString(`FileName: /tmp/whatever.txt 235SPDXID: SPDXRef-File123 236FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c 237LicenseConcluded: Apache-2.0 238LicenseInfoInFile: Apache-2.0 239FileCopyrightText: <text>Copyright (c) Jane Doe 240Copyright (c) John Doe</text> 241 242`) 243 244 // render as buffer of bytes 245 var got bytes.Buffer 246 err := renderFile2_1(f, &got) 247 if err != nil { 248 t.Errorf("Expected nil error, got %v", err) 249 } 250 251 // check that they match 252 c := bytes.Compare(want.Bytes(), got.Bytes()) 253 if c != 0 { 254 t.Errorf("Expected %v, got %v", want.String(), got.String()) 255 } 256} 257 258func TestSaver2_1FileWrapsCommentsAndNoticesMultiLine(t *testing.T) { 259 f := &v2_1.File{ 260 FileName: "/tmp/whatever.txt", 261 FileSPDXIdentifier: common.ElementID("File123"), 262 Checksums: []common.Checksum{ 263 {Algorithm: common.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c"}, 264 }, 265 LicenseComments: `this is a 266multi-line license comment`, 267 LicenseConcluded: "Apache-2.0", 268 LicenseInfoInFiles: []string{ 269 "Apache-2.0", 270 }, 271 FileCopyrightText: "Copyright (c) Jane Doe", 272 FileComment: `this is a 273multi-line file comment`, 274 FileNotice: `This file may be used 275under either Apache-2.0 or Apache-1.1.`, 276 } 277 278 // what we want to get, as a buffer of bytes 279 want := bytes.NewBufferString(`FileName: /tmp/whatever.txt 280SPDXID: SPDXRef-File123 281FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c 282LicenseConcluded: Apache-2.0 283LicenseInfoInFile: Apache-2.0 284LicenseComments: <text>this is a 285multi-line license comment</text> 286FileCopyrightText: Copyright (c) Jane Doe 287FileComment: <text>this is a 288multi-line file comment</text> 289FileNotice: <text>This file may be used 290under either Apache-2.0 or Apache-1.1.</text> 291 292`) 293 294 // render as buffer of bytes 295 var got bytes.Buffer 296 err := renderFile2_1(f, &got) 297 if err != nil { 298 t.Errorf("Expected nil error, got %v", err) 299 } 300 301 // check that they match 302 c := bytes.Compare(want.Bytes(), got.Bytes()) 303 if c != 0 { 304 t.Errorf("Expected %v, got %v", want.String(), got.String()) 305 } 306} 307