aboutsummaryrefslogtreecommitdiff
path: root/jsonloader/parser2v2/parse_package_test.go
blob: 9217836f5d1f1bb1503529c160b73c8d16b3838f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package parser2v2

import (
	"encoding/json"
	"reflect"
	"testing"

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

func TestJSONSpdxDocument_parseJsonPackages2_2(t *testing.T) {

	data := []byte(`{
		"packages" : [ {
			"SPDXID" : "SPDXRef-Package",
			"annotations" : [ {
			  "annotationDate" : "2011-01-29T18:30:22Z",
			  "annotationType" : "OTHER",
			  "annotator" : "Person: Package Commenter",
			  "comment" : "Package level annotation"
			} ],
			"attributionTexts" : [ "The GNU C Library is free software.  See the file COPYING.LIB for copying conditions, and LICENSES for notices about a few contributions that require these additional notices to be distributed.  License copyright years may be listed using range notation, e.g., 1996-2015, indicating that every year in the range, inclusive, is a copyrightable year that would otherwise be listed individually." ],
			"checksums" : [ {
			  "algorithm" : "SHA256",
			  "checksumValue" : "11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd"
			}, {
			  "algorithm" : "SHA1",
			  "checksumValue" : "85ed0817af83a24ad8da68c2b5094de69833983c"
			}, {
			  "algorithm" : "MD5",
			  "checksumValue" : "624c1abb3664f4b35547e7c73864ad24"
			} ],
			"copyrightText" : "Copyright 2008-2010 John Smith",
			"description" : "The GNU C Library defines functions that are specified by the ISO C standard, as well as additional features specific to POSIX and other derivatives of the Unix operating system, and extensions specific to GNU systems.",
			"downloadLocation" : "http://ftp.gnu.org/gnu/glibc/glibc-ports-2.15.tar.gz",
			"externalRefs" : [ {
			  "comment" : "This is the external ref for Acme",
			  "referenceCategory" : "OTHER",
			  "referenceLocator" : "acmecorp/acmenator/4.1.3-alpha",
			  "referenceType" : "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#LocationRef-acmeforge"
			}, {
			  "referenceCategory" : "SECURITY",
			  "referenceLocator" : "cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*",
			  "referenceType" : "http://spdx.org/rdf/references/cpe23Type"
			} ],
			"filesAnalyzed" : true,
			"hasFiles" : [ "SPDXRef-JenaLib", "SPDXRef-DoapSource", "SPDXRef-CommonsLangSrc" ],
			"homepage" : "http://ftp.gnu.org/gnu/glibc",
			"licenseComments" : "The license for this project changed with the release of version x.y.  The version of the project included here post-dates the license change.",
			"licenseConcluded" : "(LGPL-2.0-only OR LicenseRef-3)",
			"licenseDeclared" : "(LGPL-2.0-only AND LicenseRef-3)",
			"licenseInfoFromFiles" : [ "GPL-2.0-only", "LicenseRef-2", "LicenseRef-1" ],
			"name" : "glibc",
			"originator" : "Organization: ExampleCodeInspect (contact@example.com)",
			"packageFileName" : "glibc-2.11.1.tar.gz",
			"packageVerificationCode" : {
			  "packageVerificationCodeExcludedFiles" : [ "excludes: ./package.spdx" ],
			  "packageVerificationCodeValue" : "d6a770ba38583ed4bb4525bd96e50461655d2758"
			},
			"sourceInfo" : "uses glibc-2_11-branch from git://sourceware.org/git/glibc.git.",
			"summary" : "GNU C library.",
			"supplier" : "Person: Jane Doe (jane.doe@example.com)",
			"versionInfo" : "2.11.1"
		  }, {
			"SPDXID" : "SPDXRef-fromDoap-1",
			"comment" : "This package was converted from a DOAP Project by the same name",
			"copyrightText" : "NOASSERTION",
			"downloadLocation" : "NOASSERTION",
			"filesAnalyzed" : false,
			"homepage" : "http://commons.apache.org/proper/commons-lang/",
			"licenseConcluded" : "NOASSERTION",
			"licenseDeclared" : "NOASSERTION",
			"name" : "Apache Commons Lang"
		  }, {
			"SPDXID" : "SPDXRef-fromDoap-0",
			"comment" : "This package was converted from a DOAP Project by the same name",
			"copyrightText" : "NOASSERTION",
			"downloadLocation" : "NOASSERTION",
			"filesAnalyzed" : false,
			"homepage" : "http://www.openjena.org/",
			"licenseConcluded" : "NOASSERTION",
			"licenseDeclared" : "NOASSERTION",
			"name" : "Jena"
		  }, {
			"SPDXID" : "SPDXRef-Saxon",
			"checksums" : [ {
			  "algorithm" : "SHA1",
			  "checksumValue" : "85ed0817af83a24ad8da68c2b5094de69833983c"
			} ],
			"description" : "The Saxon package is a collection of tools for processing XML documents.",
			"downloadLocation" : "https://sourceforge.net/projects/saxon/files/Saxon-B/8.8.0.7/saxonb8-8-0-7j.zip/download",
			"filesAnalyzed" : false,
			"homepage" : "http://saxon.sourceforge.net/",
			"licenseComments" : "Other versions available for a commercial license",
			"licenseConcluded" : "MPL-1.0",
			"licenseDeclared" : "MPL-1.0",
			"name" : "Saxon",
			"packageFileName" : "saxonB-8.8.zip",
			"versionInfo" : "8.8"
		  } ]		
		}
  `)

	document := spdxDocument2_2{
		UnpackagedFiles: map[spdx.ElementID]*spdx.File2_2{
			"DoapSource": {
				FileSPDXIdentifier: "DoapSource",
				FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{
					"SHA1": {
						Algorithm: "SHA1",
						Value:     "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
					},
				},
				FileCopyrightText: "Copyright 2010, 2011 Source Auditor Inc.",
				FileContributor:   []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",
				FileType:          []string{"SOURCE"},
				LicenseConcluded:  "Apache-2.0",
				LicenseInfoInFile: []string{"Apache-2.0"},
			},
			"CommonsLangSrc": {
				FileSPDXIdentifier: "CommonsLangSrc",
				FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{
					"SHA1": {
						Algorithm: "SHA1",
						Value:     "c2b4e1c67a2d28fced849ee1bb76e7391b93f125",
					},
				},
				FileComment:       "This file is used by Jena",
				FileCopyrightText: "Copyright 2001-2011 The Apache Software Foundation",
				FileContributor:   []string{"Apache Software Foundation"},
				FileName:          "./lib-source/commons-lang3-3.1-sources.jar",
				FileType:          []string{"ARCHIVE"},
				LicenseConcluded:  "Apache-2.0",
				LicenseInfoInFile: []string{"Apache-2.0"},
				FileNotice:        "Apache Commons Lang\nCopyright 2001-2011 The Apache Software Foundation\n\nThis product includes software developed by\nThe Apache Software Foundation (http://www.apache.org/).\n\nThis product includes software from the Spring Framework,\nunder the Apache License 2.0 (see: StringUtils.containsWhitespace())",
			},
			"JenaLib": {
				FileSPDXIdentifier: "JenaLib",
				FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{
					"SHA1": {
						Algorithm: "SHA1",
						Value:     "3ab4e1c67a2d28fced849ee1bb76e7391b93f125",
					},
				},
				FileComment:       "This file belongs to Jena",
				FileCopyrightText: "(c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP",
				FileContributor:   []string{"Apache Software Foundation", "Hewlett Packard Inc."},
				FileDependencies:  []string{"SPDXRef-CommonsLangSrc"},
				FileName:          "./lib-source/jena-2.6.3-sources.jar",
				FileType:          []string{"ARCHIVE"},
				LicenseComments:   "This license is used by Jena",
				LicenseConcluded:  "LicenseRef-1",
				LicenseInfoInFile: []string{"LicenseRef-1"},
			},
			"File": {
				FileSPDXIdentifier: "File",
				FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{
					"SHA1": {
						Algorithm: "SHA1",
						Value:     "d6a770ba38583ed4bb4525bd96e50461655d2758",
					},
					"MD5": {
						Algorithm: "MD5",
						Value:     "624c1abb3664f4b35547e7c73864ad24",
					},
				},
				FileComment:       "The concluded license was taken from the package level that the file was included in.\nThis information was found in the COPYING.txt file in the xyz directory.",
				FileCopyrightText: "Copyright 2008-2010 John Smith",
				FileContributor:   []string{"The Regents of the University of California", "Modified by Paul Mundt lethal@linux-sh.org", "IBM Corporation"},
				FileName:          "./package/foo.c",
				FileType:          []string{"SOURCE"},
				LicenseComments:   "The concluded license was taken from the package level that the file was included in.",
				LicenseConcluded:  "(LGPL-2.0-only OR LicenseRef-2)",
				LicenseInfoInFile: []string{"GPL-2.0-only", "LicenseRef-2"},
				FileNotice:        "Copyright (c) 2001 Aaron Lehmann aaroni@vitelus.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the �Software�), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: \nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED �AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
			},
		},
	}

	packagetest1 := map[spdx.ElementID]*spdx.Package2_2{
		"Package": {
			PackageSPDXIdentifier:   "Package",
			PackageAttributionTexts: []string{"The GNU C Library is free software.  See the file COPYING.LIB for copying conditions, and LICENSES for notices about a few contributions that require these additional notices to be distributed.  License copyright years may be listed using range notation, e.g., 1996-2015, indicating that every year in the range, inclusive, is a copyrightable year that would otherwise be listed individually."},
			PackageChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{
				"SHA256": {
					Algorithm: "SHA256",
					Value:     "11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd",
				},
				"SHA1": {
					Algorithm: "SHA1",
					Value:     "85ed0817af83a24ad8da68c2b5094de69833983c",
				},
				"MD5": {
					Algorithm: "MD5",
					Value:     "624c1abb3664f4b35547e7c73864ad24",
				},
			},
			PackageCopyrightText:    "Copyright 2008-2010 John Smith",
			PackageDescription:      "The GNU C Library defines functions that are specified by the ISO C standard, as well as additional features specific to POSIX and other derivatives of the Unix operating system, and extensions specific to GNU systems.",
			PackageDownloadLocation: "http://ftp.gnu.org/gnu/glibc/glibc-ports-2.15.tar.gz",
			PackageExternalReferences: []*spdx.PackageExternalReference2_2{
				{
					RefType:            "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#LocationRef-acmeforge",
					ExternalRefComment: "This is the external ref for Acme",
					Category:           "OTHER",
					Locator:            "acmecorp/acmenator/4.1.3-alpha",
				},
				{
					RefType:  "http://spdx.org/rdf/references/cpe23Type",
					Category: "SECURITY",
					Locator:  "cpe:2.3:a:pivotal_software:spring_framework:4.1.0:*:*:*:*:*:*:*",
				},
			},
			FilesAnalyzed:             true,
			IsFilesAnalyzedTagPresent: true,
			Files: map[spdx.ElementID]*spdx.File2_2{
				"DoapSource": {
					FileSPDXIdentifier: "DoapSource",
					FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{
						"SHA1": {
							Algorithm: "SHA1",
							Value:     "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
						},
					},
					FileCopyrightText: "Copyright 2010, 2011 Source Auditor Inc.",
					FileContributor:   []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",
					FileType:          []string{"SOURCE"},
					LicenseConcluded:  "Apache-2.0",
					LicenseInfoInFile: []string{"Apache-2.0"},
				},
				"CommonsLangSrc": {
					FileSPDXIdentifier: "CommonsLangSrc",
					FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{
						"SHA1": {
							Algorithm: "SHA1",
							Value:     "c2b4e1c67a2d28fced849ee1bb76e7391b93f125",
						},
					},
					FileComment:       "This file is used by Jena",
					FileCopyrightText: "Copyright 2001-2011 The Apache Software Foundation",
					FileContributor:   []string{"Apache Software Foundation"},
					FileName:          "./lib-source/commons-lang3-3.1-sources.jar",
					FileType:          []string{"ARCHIVE"},
					LicenseConcluded:  "Apache-2.0",
					LicenseInfoInFile: []string{"Apache-2.0"},
					FileNotice:        "Apache Commons Lang\nCopyright 2001-2011 The Apache Software Foundation\n\nThis product includes software developed by\nThe Apache Software Foundation (http://www.apache.org/).\n\nThis product includes software from the Spring Framework,\nunder the Apache License 2.0 (see: StringUtils.containsWhitespace())",
				},
				"JenaLib": {
					FileSPDXIdentifier: "JenaLib",
					FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{
						"SHA1": {
							Algorithm: "SHA1",
							Value:     "3ab4e1c67a2d28fced849ee1bb76e7391b93f125",
						},
					},
					FileComment:       "This file belongs to Jena",
					FileCopyrightText: "(c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP",
					FileContributor:   []string{"Apache Software Foundation", "Hewlett Packard Inc."},
					FileDependencies:  []string{"SPDXRef-CommonsLangSrc"},
					FileName:          "./lib-source/jena-2.6.3-sources.jar",
					FileType:          []string{"ARCHIVE"},
					LicenseComments:   "This license is used by Jena",
					LicenseConcluded:  "LicenseRef-1",
					LicenseInfoInFile: []string{"LicenseRef-1"},
				},
			},
			PackageHomePage:                     "http://ftp.gnu.org/gnu/glibc",
			PackageLicenseComments:              "The license for this project changed with the release of version x.y.  The version of the project included here post-dates the license change.",
			PackageLicenseConcluded:             "(LGPL-2.0-only OR LicenseRef-3)",
			PackageLicenseDeclared:              "(LGPL-2.0-only AND LicenseRef-3)",
			PackageLicenseInfoFromFiles:         []string{"GPL-2.0-only", "LicenseRef-2", "LicenseRef-1"},
			PackageName:                         "glibc",
			PackageOriginatorOrganization:       "ExampleCodeInspect (contact@example.com)",
			PackageFileName:                     "glibc-2.11.1.tar.gz",
			PackageVerificationCodeExcludedFile: "./package.spdx",
			PackageVerificationCode:             "d6a770ba38583ed4bb4525bd96e50461655d2758",
			PackageSourceInfo:                   "uses glibc-2_11-branch from git://sourceware.org/git/glibc.git.",
			PackageSummary:                      "GNU C library.",
			PackageSupplierPerson:               "Jane Doe (jane.doe@example.com)",
			PackageVersion:                      "2.11.1",
		},
		"fromDoap-1": {
			PackageSPDXIdentifier:     "fromDoap-1",
			PackageComment:            "This package was converted from a DOAP Project by the same name",
			PackageCopyrightText:      "NOASSERTION",
			PackageDownloadLocation:   "NOASSERTION",
			FilesAnalyzed:             false,
			IsFilesAnalyzedTagPresent: true,
			PackageHomePage:           "http://commons.apache.org/proper/commons-lang/",
			PackageLicenseConcluded:   "NOASSERTION",
			PackageLicenseDeclared:    "NOASSERTION",
			PackageName:               "Apache Commons Lang",
		},
		"fromDoap-0": {
			PackageSPDXIdentifier:     "fromDoap-0",
			PackageComment:            "This package was converted from a DOAP Project by the same name",
			PackageCopyrightText:      "NOASSERTION",
			PackageDownloadLocation:   "NOASSERTION",
			FilesAnalyzed:             false,
			IsFilesAnalyzedTagPresent: true,
			PackageHomePage:           "http://www.openjena.org/",
			PackageLicenseConcluded:   "NOASSERTION",
			PackageLicenseDeclared:    "NOASSERTION",
			PackageName:               "Jena",
		},

		"Saxon": {
			PackageSPDXIdentifier: "Saxon",
			PackageChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{
				"SHA1": {
					Algorithm: "SHA1",
					Value:     "85ed0817af83a24ad8da68c2b5094de69833983c",
				},
			},
			PackageDescription:        "The Saxon package is a collection of tools for processing XML documents.",
			PackageDownloadLocation:   "https://sourceforge.net/projects/saxon/files/Saxon-B/8.8.0.7/saxonb8-8-0-7j.zip/download",
			FilesAnalyzed:             false,
			IsFilesAnalyzedTagPresent: true,
			PackageHomePage:           "http://saxon.sourceforge.net/",
			PackageLicenseComments:    "Other versions available for a commercial license",
			PackageLicenseConcluded:   "MPL-1.0",
			PackageLicenseDeclared:    "MPL-1.0",
			PackageName:               "Saxon",
			PackageFileName:           "saxonB-8.8.zip",
			PackageVersion:            "8.8",
		},
	}
	var specs JSONSpdxDocument
	json.Unmarshal(data, &specs)

	type args struct {
		key   string
		value interface{}
		doc   *spdxDocument2_2
	}
	tests := []struct {
		name    string
		spec    JSONSpdxDocument
		args    args
		want    map[spdx.ElementID]*spdx.Package2_2
		wantErr bool
	}{
		// TODO: Add test cases.
		{
			name: "successTest",
			spec: specs,
			args: args{
				key:   "packages",
				value: specs["packages"],
				doc:   &document,
			},
			want:    packagetest1,
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if err := tt.spec.parseJsonPackages2_2(tt.args.key, tt.args.value, tt.args.doc); (err != nil) != tt.wantErr {
				t.Errorf("JSONSpdxDocument.parseJsonPackages2_2() error = %v, wantErr %v", err, tt.wantErr)
			}

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

		})
	}
}