|
9 | 9 | # |
10 | 10 |
|
11 | 11 | import io |
| 12 | +import hashlib |
12 | 13 | import os |
13 | 14 | import re |
14 | 15 |
|
|
26 | 27 | test_env.test_data_dir = os.path.join(os.path.dirname(__file__), 'data') |
27 | 28 |
|
28 | 29 |
|
| 30 | +def write_spdx_for_checksum_test(file_data, as_tagvalue, monkeypatch): |
| 31 | + from formattedcode.output_spdx import write_spdx |
| 32 | + from licensedcode import cache |
| 33 | + |
| 34 | + # These inputs contain no license detections and need no license database. |
| 35 | + monkeypatch.setattr(cache, 'get_licenses_db', lambda: {}) |
| 36 | + output = io.StringIO() |
| 37 | + write_spdx( |
| 38 | + codebase=None, |
| 39 | + output_file=output, |
| 40 | + files=[file_data], |
| 41 | + tool_name='ScanCode', |
| 42 | + tool_version='test', |
| 43 | + notice='test', |
| 44 | + package_name='sample', |
| 45 | + as_tagvalue=as_tagvalue, |
| 46 | + ) |
| 47 | + return output.getvalue() |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize('as_tagvalue', [True, False], ids=['tagvalue', 'rdf']) |
| 51 | +@pytest.mark.parametrize('checksum_data', [{}, {'sha1': None}, {'sha1': ''}]) |
| 52 | +def test_spdx_missing_sha1_rejected(as_tagvalue, checksum_data, monkeypatch): |
| 53 | + file_data = {'type': 'file', 'path': 'sample.txt', 'file_type': 'ASCII text'} |
| 54 | + file_data.update(checksum_data) |
| 55 | + with pytest.raises(ValueError, match='missing SHA1.*sample.txt'): |
| 56 | + write_spdx_for_checksum_test(file_data, as_tagvalue, monkeypatch) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize('as_tagvalue', [True, False], ids=['tagvalue', 'rdf']) |
| 60 | +def test_spdx_file_checksum_preserves_known_sha1(as_tagvalue, monkeypatch): |
| 61 | + checksum = hashlib.sha1(b'non-empty contents').hexdigest() |
| 62 | + result = write_spdx_for_checksum_test( |
| 63 | + {'type': 'file', 'path': 'sample.txt', 'sha1': checksum}, |
| 64 | + as_tagvalue, |
| 65 | + monkeypatch, |
| 66 | + ) |
| 67 | + assert checksum in result |
| 68 | + assert hashlib.sha1(b'').hexdigest() not in result |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize('as_tagvalue', [True, False], ids=['tagvalue', 'rdf']) |
| 72 | +def test_spdx_file_checksum_preserves_empty_file_hash(as_tagvalue, monkeypatch): |
| 73 | + result = write_spdx_for_checksum_test( |
| 74 | + {'type': 'file', 'path': 'empty.txt', 'file_type': 'empty'}, |
| 75 | + as_tagvalue, |
| 76 | + monkeypatch, |
| 77 | + ) |
| 78 | + assert hashlib.sha1(b'').hexdigest() in result |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize('as_tagvalue', [True, False], ids=['tagvalue', 'rdf']) |
| 82 | +def test_spdx_file_checksum_does_not_require_directory_hash(as_tagvalue, monkeypatch): |
| 83 | + result = write_spdx_for_checksum_test( |
| 84 | + {'type': 'directory', 'path': 'directory'}, |
| 85 | + as_tagvalue, |
| 86 | + monkeypatch, |
| 87 | + ) |
| 88 | + assert "No results for package 'sample'." in result |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize('output_option', ['--spdx-tv', '--spdx-rdf']) |
| 92 | +def test_spdx_missing_sha1_cli_error(output_option, tmp_path, monkeypatch): |
| 93 | + from licensedcode import cache |
| 94 | + |
| 95 | + monkeypatch.setattr(cache, 'get_licenses_db', lambda: {}) |
| 96 | + source = tmp_path / 'sample.txt' |
| 97 | + source.write_text('non-empty contents', encoding='utf-8') |
| 98 | + output = tmp_path / 'output.spdx' |
| 99 | + result = run_scan_click( |
| 100 | + ['--email', str(source), output_option, str(output)], |
| 101 | + expected_rc=1, |
| 102 | + retry=False, |
| 103 | + ) |
| 104 | + assert 'missing SHA1' in result.output |
| 105 | + assert 'sample.txt' in result.output |
| 106 | + assert '--info' in result.output |
| 107 | + if output.exists(): |
| 108 | + assert not output.read_text(encoding='utf-8') |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.parametrize('output_option', ['--spdx-tv', '--spdx-rdf']) |
| 112 | +@pytest.mark.parametrize('content', [b'non-empty contents', b'']) |
| 113 | +def test_spdx_file_checksum_cli_with_info(output_option, content, tmp_path, monkeypatch): |
| 114 | + from licensedcode import cache |
| 115 | + |
| 116 | + monkeypatch.setattr(cache, 'get_licenses_db', lambda: {}) |
| 117 | + source = tmp_path / 'sample.txt' |
| 118 | + source.write_bytes(content) |
| 119 | + output = tmp_path / 'output.spdx' |
| 120 | + run_scan_click(['--info', str(source), output_option, str(output)]) |
| 121 | + assert hashlib.sha1(content).hexdigest() in output.read_text(encoding='utf-8') |
| 122 | + |
| 123 | + |
29 | 124 | def strip_variable_text(rdf_text): |
30 | 125 | """ |
31 | 126 | Return rdf_text stripped from variable parts such as rdf nodeids |
@@ -339,15 +434,21 @@ def test_spdx_rdf_with_empty_scan(): |
339 | 434 |
|
340 | 435 |
|
341 | 436 | @pytest.mark.scanslow |
342 | | -def test_output_spdx_rdf_can_handle_non_ascii_paths(): |
| 437 | +def test_output_spdx_rdf_can_handle_non_ascii_paths(monkeypatch): |
| 438 | + from licensedcode import cache |
| 439 | + |
| 440 | + monkeypatch.setattr(cache, 'get_licenses_db', lambda: {}) |
343 | 441 | test_file = test_env.get_test_loc('spdx/unicode.json') |
344 | 442 | result_file = test_env.get_temp_file(extension='spdx', file_name='test_spdx') |
345 | 443 | run_scan_click(['--from-json', test_file, '--spdx-rdf', result_file]) |
346 | 444 | with io.open(result_file, encoding='utf-8') as res: |
347 | 445 | results = res.read() |
348 | 446 | assert 'han/据.svg' in results |
349 | 447 |
|
350 | | -def test_output_spdx_tv_can_handle_non_ascii_paths(): |
| 448 | +def test_output_spdx_tv_can_handle_non_ascii_paths(monkeypatch): |
| 449 | + from licensedcode import cache |
| 450 | + |
| 451 | + monkeypatch.setattr(cache, 'get_licenses_db', lambda: {}) |
351 | 452 | test_file = test_env.get_test_loc('spdx/unicode.json') |
352 | 453 | result_file = test_env.get_temp_file(extension='spdx', file_name='test_spdx') |
353 | 454 | run_scan_click(['--from-json', test_file, '--spdx-tv', result_file]) |
|
0 commit comments