FOSSology  4.7.1
Open Source License Compliance by Open Source Software
test_report_output.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2026 Krrish Biswas <krrishbiswas175@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
13 namespace Fossology\Lib\Data {
14  class LicenseRef
15  {
16  const SPDXREF_PREFIX = "LicenseRef-";
17  }
18 }
19 
20 namespace {
21 
22  if (!function_exists('uuid_create')) {
23  define('UUID_TYPE_TIME', 1);
24  function uuid_create($type = UUID_TYPE_TIME)
25  {
26  return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
27  mt_rand(0, 0xffff), mt_rand(0, 0xffff),
28  mt_rand(0, 0xffff),
29  mt_rand(0, 0x0fff) | 0x4000,
30  mt_rand(0, 0x3fff) | 0x8000,
31  mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
32  );
33  }
34  }
35 
36  require_once __DIR__ . '/../../lib/php/Data/Report/CycloneDxSettings.php';
37  require_once __DIR__ . '/../agent/reportgenerator.php';
38 
41 
42  $generator = new BomReportGenerator();
43  $passed = 0;
44  $failed = 0;
45 
46  function assert_test($name, $condition, $detail = '')
47  {
48  global $passed, $failed;
49  if ($condition) {
50  echo " PASS: $name\n";
51  $passed++;
52  } else {
53  echo " FAIL: $name" . ($detail ? " -- $detail" : "") . "\n";
54  $failed++;
55  }
56  }
57 
58  echo "\n=== CycloneDX BomReportGenerator Tests ===\n\n";
59 
60  echo "Test 1: License with text content\n";
61  $licenseData = array(
62  'id' => 'MIT',
63  'name' => 'MIT License',
64  'url' => 'https://opensource.org/licenses/MIT',
65  'textContent' => base64_encode('Permission is hereby granted, free of charge...'),
66  'textContentType' => 'text/plain'
67  );
68  $result = $generator->createLicense($licenseData);
69 
70  assert_test('License has license key', isset($result['license']));
71  assert_test('License id is MIT', $result['license']['id'] === 'MIT');
72  assert_test('License has url', $result['license']['url'] === 'https://opensource.org/licenses/MIT');
73  assert_test('License has text block', isset($result['license']['text']));
74  assert_test('License text has content', isset($result['license']['text']['content']));
75  assert_test('License text contentType is text/plain', $result['license']['text']['contentType'] === 'text/plain');
76  assert_test('License text encoding is base64', $result['license']['text']['encoding'] === 'base64');
77  assert_test('License text decodes correctly',
78  base64_decode($result['license']['text']['content']) === 'Permission is hereby granted, free of charge...');
79 
80  echo "\n";
81 
82  echo "Test 2: License without text content\n";
83  $licenseDataNoText = array(
84  'id' => 'Apache-2.0',
85  'name' => 'Apache License 2.0',
86  'url' => 'https://www.apache.org/licenses/LICENSE-2.0'
87  );
88  $result2 = $generator->createLicense($licenseDataNoText);
89 
90  assert_test('License has license key', isset($result2['license']));
91  assert_test('License id is Apache-2.0', $result2['license']['id'] === 'Apache-2.0');
92  assert_test('License has NO text block', !isset($result2['license']['text']));
93 
94  echo "\n";
95 
96  echo "Test 3: LicenseRef expression\n";
97  $licenseRef = array(
98  'id' => 'LicenseRef-fossology-custom',
99  'name' => 'Custom License'
100  );
101  $result3 = $generator->createLicense($licenseRef);
102 
103  assert_test('LicenseRef returns expression', isset($result3['expression']));
104  assert_test('Expression value correct', $result3['expression'] === 'LicenseRef-fossology-custom');
105  assert_test('LicenseRef has NO license key', !isset($result3['license']));
106 
107  echo "\n";
108 
109  echo "Test 4: Component with copyright\n";
110  $componentData = array(
111  'type' => 'library',
112  'name' => 'openssl-3.0.12.tar.gz',
113  'bomref' => '42',
114  'scope' => 'required',
115  'mimeType' => 'application/gzip',
116  'copyright' => "Copyright 2000-2023 The OpenSSL Project Authors\nCopyright 1995-2023 Eric A. Young, Tim J. Hudson",
117  'hashes' => array(
118  $generator->createHash('SHA-1', 'abc123'),
119  $generator->createHash('MD5', 'def456')
120  ),
121  'licenses' => array($result)
122  );
123  $comp = $generator->createComponent($componentData);
124 
125  assert_test('Component has type', $comp['type'] === 'library');
126  assert_test('Component has name', $comp['name'] === 'openssl-3.0.12.tar.gz');
127  assert_test('Component has bom-ref', $comp['bom-ref'] === '42');
128  assert_test('Component has copyright', isset($comp['copyright']));
129  assert_test('Copyright contains OpenSSL', strpos($comp['copyright'], 'OpenSSL') !== false);
130  assert_test('Copyright contains both entries', strpos($comp['copyright'], 'Eric A. Young') !== false);
131  assert_test('Component has hashes', count($comp['hashes']) === 2);
132  assert_test('Component has licenses', count($comp['licenses']) === 1);
133 
134  echo "\n";
135 
136  echo "Test 5: Component without copyright\n";
137  $compNoCopyright = $generator->createComponent(array(
138  'type' => 'file',
139  'name' => 'README.md'
140  ));
141 
142  assert_test('Component without copyright has no copyright key', !isset($compNoCopyright['copyright']));
143 
144  echo "\n";
145 
146  echo "Test 6: Full report structure\n";
147  $fileComponent = $generator->createComponent(array(
148  'type' => 'file',
149  'name' => 'src/main.c',
150  'bomref' => '42-100',
151  'copyright' => 'Copyright 2023 Test Author',
152  'hashes' => array($generator->createHash('SHA-1', 'aaa')),
153  'licenses' => array($result)
154  ));
155 
156  $report = $generator->generateReport(array(
157  'tool-version' => '4.5.0',
158  'maincomponent' => $comp,
159  'components' => array($fileComponent)
160  ));
161 
162  assert_test('Report bomFormat is CycloneDX', $report['bomFormat'] === 'CycloneDX');
163  assert_test('Report specVersion is 1.7', $report['specVersion'] === '1.7');
164  assert_test('Report version is integer 1', $report['version'] === 1);
165  assert_test('Report has serialNumber', !empty($report['serialNumber']));
166  assert_test('Report has metadata', isset($report['metadata']));
167  assert_test('Report metadata has tools', isset($report['metadata']['tools']));
168  assert_test('Tool manufacturer is FOSSology',
169  $report['metadata']['tools']['components'][0]['manufacturer']['name'] === 'FOSSology');
170  assert_test('Tool has no vendor', !isset($report['metadata']['tools']['components'][0]['vendor']));
171  assert_test('Tool version is 4.5.0', $report['metadata']['tools']['components'][0]['version'] === '4.5.0');
172  assert_test('Metadata has main component', isset($report['metadata']['component']));
173  assert_test('Main component has copyright', isset($report['metadata']['component']['copyright']));
174  assert_test('Report has components array', is_array($report['components']));
175  assert_test('Components count is 1', count($report['components']) === 1);
176  assert_test('File component has copyright', isset($report['components'][0]['copyright']));
177  assert_test('Report has no externalReferences when not provided', !isset($report['externalReferences']));
178 
179  echo "\n";
180 
181  echo "Test 7: JSON validity\n";
182  $json = json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
183  assert_test('JSON encodes without error', json_last_error() === JSON_ERROR_NONE);
184  assert_test('JSON is not empty', strlen($json) > 100);
185 
186  $decoded = json_decode($json, true);
187  assert_test('JSON round-trip preserves bomFormat', $decoded['bomFormat'] === 'CycloneDX');
188  assert_test('JSON round-trip preserves license text',
189  isset($decoded['metadata']['component']['licenses'][0]['license']['text']['content']));
190 
191  echo "\n";
192 
193  echo "Test 8: Component with version\n";
194  $compWithVersion = $generator->createComponent(array(
195  'type' => 'library',
196  'name' => 'busybox',
197  'version' => '1.36.1'
198  ));
199 
200  assert_test('Component has version', isset($compWithVersion['version']));
201  assert_test('Version value is correct', $compWithVersion['version'] === '1.36.1');
202 
203  $compNoVersion = $generator->createComponent(array(
204  'type' => 'library',
205  'name' => 'busybox',
206  'version' => ''
207  ));
208  assert_test('Empty version is not included', !isset($compNoVersion['version']));
209 
210  echo "\n";
211 
212  echo "Test 9: Component with purl\n";
213  $compWithPurl = $generator->createComponent(array(
214  'type' => 'library',
215  'name' => 'busybox',
216  'purl' => 'pkg:deb/debian/busybox@1.36.1'
217  ));
218 
219  assert_test('Component has purl', isset($compWithPurl['purl']));
220  assert_test('PURL value is correct', $compWithPurl['purl'] === 'pkg:deb/debian/busybox@1.36.1');
221 
222  $compNoPurl = $generator->createComponent(array(
223  'type' => 'library',
224  'name' => 'busybox',
225  'purl' => ''
226  ));
227  assert_test('Empty purl is not included', !isset($compNoPurl['purl']));
228 
229  echo "\n";
230 
231  echo "Test 10: Component with description\n";
232  $compWithDesc = $generator->createComponent(array(
233  'type' => 'library',
234  'name' => 'openssl',
235  'description' => 'Open source SSL/TLS toolkit'
236  ));
237 
238  assert_test('Component has description', isset($compWithDesc['description']));
239  assert_test('Description value is correct', $compWithDesc['description'] === 'Open source SSL/TLS toolkit');
240 
241  $compNoDesc = $generator->createComponent(array(
242  'type' => 'library',
243  'name' => 'openssl',
244  'description' => ''
245  ));
246  assert_test('Empty description is not included', !isset($compNoDesc['description']));
247 
248  echo "\n";
249 
250  echo "Test 11: Component with properties (acknowledgements + comments)\n";
251  $compWithProps = $generator->createComponent(array(
252  'type' => 'file',
253  'name' => 'src/main.c',
254  'acknowledgements' => "Thanks to the OpenSSL team\nThanks to contributors",
255  'comments' => 'Reviewed and approved by legal team'
256  ));
257 
258  assert_test('Component has properties', isset($compWithProps['properties']));
259  assert_test('Properties has 2 entries', count($compWithProps['properties']) === 2);
260  assert_test('First property is acknowledgement',
261  $compWithProps['properties'][0]['name'] === 'fossology:acknowledgement');
262  assert_test('Acknowledgement value is correct',
263  strpos($compWithProps['properties'][0]['value'], 'OpenSSL') !== false);
264  assert_test('Second property is comment',
265  $compWithProps['properties'][1]['name'] === 'fossology:comment');
266  assert_test('Comment value is correct',
267  $compWithProps['properties'][1]['value'] === 'Reviewed and approved by legal team');
268 
269  $compOnlyAck = $generator->createComponent(array(
270  'type' => 'file',
271  'name' => 'src/util.c',
272  'acknowledgements' => 'Thanks to maintainers',
273  'comments' => ''
274  ));
275  assert_test('Only acknowledgement property when comments empty', count($compOnlyAck['properties']) === 1);
276  assert_test('Single property is acknowledgement',
277  $compOnlyAck['properties'][0]['name'] === 'fossology:acknowledgement');
278 
279  $compNoProps = $generator->createComponent(array(
280  'type' => 'file',
281  'name' => 'src/test.c',
282  'acknowledgements' => '',
283  'comments' => ''
284  ));
285  assert_test('No properties when both empty', !isset($compNoProps['properties']));
286 
287  echo "\n";
288 
289  echo "Test 12: Component with externalReferences\n";
290  $compWithExtRef = $generator->createComponent(array(
291  'type' => 'library',
292  'name' => 'openssl',
293  'externalReferences' => [
294  ['type' => 'distribution', 'url' => 'https://www.openssl.org/source/openssl-3.0.12.tar.gz'],
295  ['type' => 'vcs', 'url' => 'https://github.com/openssl/openssl']
296  ]
297  ));
298 
299  assert_test('Component has externalReferences', isset($compWithExtRef['externalReferences']));
300  assert_test('ExternalReferences has 2 entries', count($compWithExtRef['externalReferences']) === 2);
301  assert_test('First ref type is distribution',
302  $compWithExtRef['externalReferences'][0]['type'] === 'distribution');
303  assert_test('First ref url is correct',
304  $compWithExtRef['externalReferences'][0]['url'] === 'https://www.openssl.org/source/openssl-3.0.12.tar.gz');
305 
306  $compNoExtRef = $generator->createComponent(array(
307  'type' => 'library',
308  'name' => 'openssl',
309  'externalReferences' => []
310  ));
311  assert_test('Empty externalReferences is not included', !isset($compNoExtRef['externalReferences']));
312 
313  echo "\n";
314 
315  echo "Test 13: Full report with externalReferences\n";
316  $reportWithExtRef = $generator->generateReport(array(
317  'tool-version' => '4.5.0',
318  'maincomponent' => $comp,
319  'components' => array($fileComponent),
320  'externalReferences' => [
321  ['type' => 'website', 'url' => 'https://www.fossology.org']
322  ]
323  ));
324 
325  assert_test('Report has externalReferences', isset($reportWithExtRef['externalReferences']));
326  assert_test('Report externalReferences has 1 entry', count($reportWithExtRef['externalReferences']) === 1);
327  assert_test('Report externalRef type is website',
328  $reportWithExtRef['externalReferences'][0]['type'] === 'website');
329 
330  echo "\n";
331 
332  echo "Test 14: Full component with all new fields\n";
333  $fullComp = $generator->createComponent(array(
334  'type' => 'library',
335  'name' => 'busybox',
336  'version' => '1.36.1',
337  'bomref' => '99',
338  'scope' => 'required',
339  'mimeType' => 'application/x-tar',
340  'copyright' => 'Copyright 2023 BusyBox Authors',
341  'description' => 'Tiny utilities for small and embedded systems',
342  'purl' => 'pkg:generic/busybox@1.36.1',
343  'hashes' => array($generator->createHash('SHA-256', 'abc123def456')),
344  'licenses' => array($result),
345  'externalReferences' => [
346  ['type' => 'vcs', 'url' => 'https://git.busybox.net/busybox/']
347  ],
348  'acknowledgements' => 'Thanks to Denys Vlasenko',
349  'comments' => 'Primary build dependency'
350  ));
351 
352  assert_test('Full component has type', $fullComp['type'] === 'library');
353  assert_test('Full component has name', $fullComp['name'] === 'busybox');
354  assert_test('Full component has version', $fullComp['version'] === '1.36.1');
355  assert_test('Full component has bom-ref', $fullComp['bom-ref'] === '99');
356  assert_test('Full component has purl', $fullComp['purl'] === 'pkg:generic/busybox@1.36.1');
357  assert_test('Full component has description',
358  $fullComp['description'] === 'Tiny utilities for small and embedded systems');
359  assert_test('Full component has copyright', isset($fullComp['copyright']));
360  assert_test('Full component has hashes', count($fullComp['hashes']) === 1);
361  assert_test('Full component has licenses', count($fullComp['licenses']) === 1);
362  assert_test('Full component has externalReferences', count($fullComp['externalReferences']) === 1);
363  assert_test('Full component has properties', count($fullComp['properties']) === 2);
364 
365  echo "\n";
366 
367  echo "Test 15: LicenseRef expression with bom-ref and acknowledgement\n";
368  $licenseRefWithRef = $generator->createLicense(array(
369  'id' => 'LicenseRef-fossology-BSD-style',
370  'bom-ref' => 'lic-scanner-320-42',
371  'acknowledgement' => 'declared'
372  ));
373 
374  assert_test('Expression value correct',
375  $licenseRefWithRef['expression'] === 'LicenseRef-fossology-BSD-style');
376  assert_test('Expression has bom-ref', $licenseRefWithRef['bom-ref'] === 'lic-scanner-320-42');
377  assert_test('Expression has acknowledgement', $licenseRefWithRef['acknowledgement'] === 'declared');
378  assert_test('Expression has only schema keys',
379  array_keys($licenseRefWithRef) === array('expression', 'bom-ref', 'acknowledgement'));
380 
381  echo "\n";
382 
383  echo "Test 16: Spec version gates fields the older schemas reject\n";
384  foreach (CycloneDxSettings::SUPPORTED_VERSIONS as $version) {
385  $versioned = new BomReportGenerator();
386  $versioned->applySettings(new CycloneDxSettings(false, false, false, 'acme:', $version));
387  $lic = $versioned->createLicense(['id' => 'MIT', 'bom-ref' => 'lic-1', 'acknowledgement' => 'concluded']);
388  $ref = $versioned->createLicense(['id' => 'LicenseRef-x', 'bom-ref' => 'lic-2', 'acknowledgement' => 'declared']);
389  $comp = $versioned->createComponent(['type' => 'file', 'name' => 'f', 'licenses' => [$lic, $ref], 'comments' => 'c']);
390  $bom = $versioned->generateReport([
391  'tool-version' => '1.0', 'maincomponent' => $comp, 'components' => [],
392  'citations' => [['attributedTo' => 'tool-fossology', 'expressions' => ['$']]]
393  ]);
394  $atLeast = function ($v) use ($version) {
395  return version_compare($version, $v, '>=');
396  };
397  assert_test("$version: specVersion and \$schema",
398  $bom['specVersion'] === $version && strpos($bom['$schema'], "bom-$version.schema.json") !== false);
399  assert_test("$version: tools form",
400  $atLeast('1.5') ? isset($bom['metadata']['tools']['components'])
401  : isset($bom['metadata']['tools'][0]['vendor']));
402  assert_test("$version: tool manufacturer only from 1.6",
403  $atLeast('1.6') === isset($bom['metadata']['tools']['components'][0]['manufacturer']));
404  assert_test("$version: author bom-ref only from 1.5",
405  $atLeast('1.5') === isset($bom['metadata']['authors'][0]['bom-ref']));
406  assert_test("$version: license bom-ref only from 1.5",
407  $atLeast('1.5') === isset($lic['license']['bom-ref']));
408  assert_test("$version: license acknowledgement only from 1.6",
409  $atLeast('1.6') === isset($lic['license']['acknowledgement']));
410  assert_test("$version: LicenseRef as expression only from 1.7",
411  $atLeast('1.7') ? $ref['expression'] === 'LicenseRef-x'
412  : $ref['license']['name'] === 'LicenseRef-x');
413  assert_test("$version: citations only from 1.7",
414  $atLeast('1.7') === isset($bom['citations']));
415  assert_test("$version: property names use the namespace",
416  $comp['properties'][0]['name'] === 'acme:comment');
417  }
418 
419  echo "\n";
420 
421  echo "=== Results ===\n";
422  echo "Passed: $passed | Failed: $failed\n";
423 
424  if ($failed > 0) {
425  echo "\nSome tests FAILED!\n";
426  exit(1);
427  } else {
428  echo "\nAll tests passed!\n";
429  exit(0);
430  }
431 
432 } // end namespace
CycloneDX export settings of a user (defaults) or of an upload.