|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# ScanCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/nexB/scancode-toolkit for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import json |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +from scancode import plugin_lkm |
| 14 | +from scancode.cli_test_utils import run_scan_click |
| 15 | + |
| 16 | + |
| 17 | +class TestLinuxKernelModuleScanner: |
| 18 | + |
| 19 | + test_data_dir = ( |
| 20 | + Path(__file__).resolve().parents[2] |
| 21 | + / 'tests' |
| 22 | + / 'licensedcode' |
| 23 | + / 'data' |
| 24 | + / 'query' |
| 25 | + ) |
| 26 | + |
| 27 | + def test_extract_modinfo(self): |
| 28 | + test_file = self.test_data_dir / 'eeepc_acpi.ko' |
| 29 | + |
| 30 | + metadata = plugin_lkm.extract_modinfo(str(test_file)) |
| 31 | + |
| 32 | + assert 'license' in metadata |
| 33 | + assert 'description' in metadata |
| 34 | + assert 'author' in metadata |
| 35 | + |
| 36 | + def test_get_dependency_names(self): |
| 37 | + metadata = { |
| 38 | + 'depends': [ |
| 39 | + 'usbcore,cfg80211,mac80211', |
| 40 | + ] |
| 41 | + } |
| 42 | + |
| 43 | + result = plugin_lkm.get_dependency_names(metadata) |
| 44 | + |
| 45 | + assert result == [ |
| 46 | + 'usbcore', |
| 47 | + 'cfg80211', |
| 48 | + 'mac80211', |
| 49 | + ] |
| 50 | + |
| 51 | + def test_get_dependency_names_handles_multiple_entries(self): |
| 52 | + metadata = { |
| 53 | + 'depends': [ |
| 54 | + 'usbcore,cfg80211', |
| 55 | + 'mac80211,netdev', |
| 56 | + ] |
| 57 | + } |
| 58 | + |
| 59 | + result = plugin_lkm.get_dependency_names(metadata) |
| 60 | + |
| 61 | + assert result == [ |
| 62 | + 'usbcore', |
| 63 | + 'cfg80211', |
| 64 | + 'mac80211', |
| 65 | + 'netdev', |
| 66 | + ] |
| 67 | + |
| 68 | + def test_scan_unrelated_file_returns_empty_mapping(self): |
| 69 | + test_file = self.test_data_dir / 'apache-2.0.LICENSE' |
| 70 | + |
| 71 | + result = plugin_lkm.scan_linux_kernel_module(str(test_file)) |
| 72 | + |
| 73 | + assert result == {} |
| 74 | + |
| 75 | + def test_scan_invalid_lkm_returns_empty_mapping(self, tmp_path): |
| 76 | + test_file = tmp_path / 'fake.ko' |
| 77 | + test_file.write_bytes(b'not an elf file') |
| 78 | + |
| 79 | + result = plugin_lkm.scan_linux_kernel_module(str(test_file)) |
| 80 | + |
| 81 | + assert result == {} |
| 82 | + |
| 83 | + def test_scan_linux_kernel_module(self): |
| 84 | + test_file = self.test_data_dir / 'eeepc_acpi.ko' |
| 85 | + |
| 86 | + result = plugin_lkm.scan_linux_kernel_module(str(test_file)) |
| 87 | + |
| 88 | + assert list(result) == ['linux_kernel_module'] |
| 89 | + |
| 90 | + metadata = result['linux_kernel_module'] |
| 91 | + assert metadata['license'] == ['GPL'] |
| 92 | + assert metadata['description'] == ['Asus EeePC Hotkey Driver'] |
| 93 | + assert metadata['author'] == [ |
| 94 | + 'Julien Lerouge, Karol Kozimor, Eric Cooper', |
| 95 | + ] |
| 96 | + assert metadata['depends'] == [] |
| 97 | + assert metadata['srcversion'] == ['7FD8A46D43685ADB0819A28'] |
| 98 | + assert metadata['vermagic'] == [ |
| 99 | + '2.6.24-19-generic SMP mod_unload 586 ', |
| 100 | + ] |
| 101 | + |
| 102 | + def test_plugin_supplies_file_scanner(self): |
| 103 | + scanner = plugin_lkm.LinuxKernelModuleScanner() |
| 104 | + |
| 105 | + assert scanner.get_scanner() is plugin_lkm.scan_linux_kernel_module |
| 106 | + assert scanner.is_enabled(lkm=True) |
| 107 | + assert not scanner.is_enabled(lkm=False) |
| 108 | + assert list(scanner.resource_attributes) == ['linux_kernel_module'] |
| 109 | + |
| 110 | + def test_lkm_scan_adds_file_metadata_without_package_data(self, tmp_path): |
| 111 | + test_file = self.test_data_dir / 'eeepc_acpi.ko' |
| 112 | + result_file = tmp_path / 'lkm-scan.json' |
| 113 | + |
| 114 | + run_scan_click([ |
| 115 | + '--lkm', |
| 116 | + '--json-pp', |
| 117 | + str(result_file), |
| 118 | + str(test_file), |
| 119 | + ]) |
| 120 | + |
| 121 | + with result_file.open(encoding='utf-8') as results: |
| 122 | + scanned_file = json.load(results)['files'][0] |
| 123 | + |
| 124 | + metadata = scanned_file['linux_kernel_module'] |
| 125 | + assert metadata['license'] == ['GPL'] |
| 126 | + assert metadata['depends'] == [] |
| 127 | + assert 'package_data' not in scanned_file |
0 commit comments