|
16 | 16 |
|
17 | 17 | from vulnerabilities import models |
18 | 18 | from vulnerabilities.import_runner import ImportRunner |
| 19 | +from vulnerabilities.importers import archlinux |
| 20 | +from vulnerabilities.tests import util_tests |
19 | 21 |
|
20 | 22 | BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
21 | | -TEST_DATA = os.path.join(BASE_DIR, "test_data/") |
22 | | - |
23 | | - |
24 | | -class ArchlinuxImportTest(TestCase): |
25 | | - @classmethod |
26 | | - def setUpClass(cls) -> None: |
27 | | - fixture_path = os.path.join(TEST_DATA, "archlinux.json") |
28 | | - with open(fixture_path) as f: |
29 | | - cls.mock_response = json.load(f) |
30 | | - |
31 | | - cls.importer = models.Importer.objects.create( |
32 | | - name="archlinux_unittests", |
33 | | - license="", |
34 | | - last_run=None, |
35 | | - data_source="ArchlinuxImporter", |
36 | | - data_source_cfg={ |
37 | | - "archlinux_tracker_url": "https://security.example.com/json", |
38 | | - }, |
39 | | - ) |
40 | | - |
41 | | - @classmethod |
42 | | - def tearDownClass(cls) -> None: |
43 | | - pass |
44 | | - |
45 | | - def test_import(self): |
46 | | - runner = ImportRunner(self.importer, 5) |
47 | | - |
48 | | - with patch( |
49 | | - "vulnerabilities.importers.ArchlinuxImporter._fetch", return_value=self.mock_response |
50 | | - ): |
51 | | - runner.run() |
52 | | - assert models.Vulnerability.objects.count() == 6 |
53 | | - assert models.VulnerabilityReference.objects.count() == 10 |
54 | | - assert models.PackageRelatedVulnerability.objects.all().count() == 12 |
55 | | - assert ( |
56 | | - models.PackageRelatedVulnerability.objects.filter(patched_package__isnull=False).count() |
57 | | - == 8 |
58 | | - ) |
59 | | - assert models.Package.objects.count() == 10 |
60 | | - |
61 | | - self.assert_for_package( |
62 | | - "squid", |
63 | | - "4.10-2", |
64 | | - cve_ids={"CVE-2020-11945", "CVE-2019-12521", "CVE-2019-12519"}, |
65 | | - ) |
66 | | - self.assert_for_package("openconnect", "1:8.05-1", cve_ids={"CVE-2020-12823"}) |
67 | | - self.assert_for_package( |
68 | | - "wireshark-common", |
69 | | - "2.6.0-1", |
70 | | - cve_ids={"CVE-2018-11362", "CVE-2018-11361"}, |
71 | | - ) |
72 | | - self.assert_for_package( |
73 | | - "wireshark-gtk", |
74 | | - "2.6.0-1", |
75 | | - cve_ids={"CVE-2018-11362", "CVE-2018-11361"}, |
76 | | - ) |
77 | | - self.assert_for_package( |
78 | | - "wireshark-cli", |
79 | | - "2.6.0-1", |
80 | | - cve_ids={"CVE-2018-11362", "CVE-2018-11361"}, |
81 | | - ) |
82 | | - self.assert_for_package( |
83 | | - "wireshark-qt", |
84 | | - "2.6.0-1", |
85 | | - cve_ids={"CVE-2018-11362", "CVE-2018-11361"}, |
86 | | - ) |
87 | | - self.assert_for_package("wireshark-common", "2.6.1-1") |
88 | | - self.assert_for_package("wireshark-gtk", "2.6.1-1") |
89 | | - self.assert_for_package("wireshark-cli", "2.6.1-1") |
90 | | - self.assert_for_package("wireshark-qt", "2.6.1-1") |
91 | | - |
92 | | - def assert_for_package(self, name, version, cve_ids=None): |
93 | | - qs = models.Package.objects.filter( |
94 | | - name=name, |
95 | | - version=version, |
96 | | - type="pacman", |
97 | | - namespace="archlinux", |
98 | | - ) |
99 | | - assert qs |
100 | | - |
101 | | - if cve_ids: |
102 | | - assert cve_ids == {v.vulnerability_id for v in qs[0].vulnerabilities.all()} |
| 23 | +TEST_DATA = os.path.join(BASE_DIR, "test_data/archlinux") |
| 24 | + |
| 25 | + |
| 26 | +def test_parse_advisory_single(): |
| 27 | + record = { |
| 28 | + "name": "AVG-2781", |
| 29 | + "packages": ["python-pyjwt"], |
| 30 | + "status": "Unknown", |
| 31 | + "severity": "Unknown", |
| 32 | + "type": "unknown", |
| 33 | + "affected": "2.3.0-1", |
| 34 | + "fixed": "2.4.0-1", |
| 35 | + "ticket": None, |
| 36 | + "issues": ["CVE-2022-29217"], |
| 37 | + "advisories": [], |
| 38 | + } |
| 39 | + |
| 40 | + advisory_data = archlinux.ArchlinuxImporter().parse_advisory(record) |
| 41 | + result = [data.to_dict() for data in advisory_data] |
| 42 | + expected_file = os.path.join(TEST_DATA, f"parse-advisory-archlinux-expected.json") |
| 43 | + util_tests.check_results_against_json(result, expected_file) |
| 44 | + |
| 45 | + |
| 46 | +@patch("vulnerabilities.importers.archlinux.ArchlinuxImporter.fetch") |
| 47 | +def test_archlinux_importer(mock_response): |
| 48 | + with open(os.path.join(TEST_DATA, "archlinux-multi.json")) as f: |
| 49 | + mock_response.return_value = json.load(f) |
| 50 | + |
| 51 | + expected_file = os.path.join(TEST_DATA, f"archlinux-multi-expected.json") |
| 52 | + result = [data.to_dict() for data in list(archlinux.ArchlinuxImporter().advisory_data())] |
| 53 | + util_tests.check_results_against_json(result, expected_file) |
0 commit comments