1717from vulnerabilities import models
1818from vulnerabilities .import_runner import ImportRunner
1919from vulnerabilities .importers import archlinux
20+ from vulnerabilities .tests import util_tests
2021
2122BASE_DIR = os .path .dirname (os .path .abspath (__file__ ))
22- TEST_DATA = os .path .join (BASE_DIR , "test_data/" )
23-
24-
25- class ArchlinuxImportTest (TestCase ):
26- @classmethod
27- def setUpClass (cls ) -> None :
28- fixture_path = os .path .join (TEST_DATA , "archlinux.json" )
29- with open (fixture_path ) as f :
30- cls .mock_response = json .load (f )
31-
32- cls .importer = models .Importer .objects .create (
33- name = "archlinux_unittests" ,
34- license = "" ,
35- last_run = None ,
36- data_source = "ArchlinuxImporter" ,
37- data_source_cfg = {
38- "archlinux_tracker_url" : "https://security.example.com/json" ,
39- },
40- )
41-
42- @classmethod
43- def tearDownClass (cls ) -> None :
44- pass
45-
46- def test_import (self ):
47- runner = ImportRunner (self .importer , 5 )
48-
49- with patch (
50- "vulnerabilities.importers.ArchlinuxImporter._fetch" , return_value = self .mock_response
51- ):
52- runner .run ()
53- assert models .Vulnerability .objects .count () == 6
54- assert models .VulnerabilityReference .objects .count () == 10
55- assert models .PackageRelatedVulnerability .objects .all ().count () == 12
56- assert (
57- models .PackageRelatedVulnerability .objects .filter (patched_package__isnull = False ).count ()
58- == 8
59- )
60- assert models .Package .objects .count () == 10
61-
62- self .assert_for_package (
63- "squid" ,
64- "4.10-2" ,
65- cve_ids = {"CVE-2020-11945" , "CVE-2019-12521" , "CVE-2019-12519" },
66- )
67- self .assert_for_package ("openconnect" , "1:8.05-1" , cve_ids = {"CVE-2020-12823" })
68- self .assert_for_package (
69- "wireshark-common" ,
70- "2.6.0-1" ,
71- cve_ids = {"CVE-2018-11362" , "CVE-2018-11361" },
72- )
73- self .assert_for_package (
74- "wireshark-gtk" ,
75- "2.6.0-1" ,
76- cve_ids = {"CVE-2018-11362" , "CVE-2018-11361" },
77- )
78- self .assert_for_package (
79- "wireshark-cli" ,
80- "2.6.0-1" ,
81- cve_ids = {"CVE-2018-11362" , "CVE-2018-11361" },
82- )
83- self .assert_for_package (
84- "wireshark-qt" ,
85- "2.6.0-1" ,
86- cve_ids = {"CVE-2018-11362" , "CVE-2018-11361" },
87- )
88- self .assert_for_package ("wireshark-common" , "2.6.1-1" )
89- self .assert_for_package ("wireshark-gtk" , "2.6.1-1" )
90- self .assert_for_package ("wireshark-cli" , "2.6.1-1" )
91- self .assert_for_package ("wireshark-qt" , "2.6.1-1" )
92-
93- def assert_for_package (self , name , version , cve_ids = None ):
94- qs = models .Package .objects .filter (
95- name = name ,
96- version = version ,
97- type = "pacman" ,
98- namespace = "archlinux" ,
99- )
100- assert qs
101-
102- if cve_ids :
103- assert cve_ids == {v .vulnerability_id for v in qs [0 ].vulnerabilities .all ()}
23+ TEST_DATA = os .path .join (BASE_DIR , "test_data/archlinux" )
10424
10525
10626def test_parse_advisory_single ():
@@ -117,48 +37,19 @@ def test_parse_advisory_single():
11737 "advisories" : [],
11838 }
11939
120- assert archlinux .ArchlinuxImporter ().parse_advisory (record )
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 )
12144
12245
123- def test_parse_advisory_multi ():
124- record_list = [
125- {
126- "name" : "AVG-2781" ,
127- "packages" : ["python-pyjwt" ],
128- "status" : "Unknown" ,
129- "severity" : "Unknown" ,
130- "type" : "unknown" ,
131- "affected" : "2.3.0-1" ,
132- "fixed" : "2.4.0-1" ,
133- "ticket" : None ,
134- "issues" : ["CVE-2022-29217" ],
135- "advisories" : [],
136- },
137- {
138- "name" : "AVG-2780" ,
139- "packages" : ["wpewebkit" ],
140- "status" : "Unknown" ,
141- "severity" : "Unknown" ,
142- "type" : "unknown" ,
143- "affected" : "2.36.3-1" ,
144- "fixed" : "2.36.4-1" ,
145- "ticket" : None ,
146- "issues" : ["CVE-2022-26710" , "CVE-2022-22677" , "CVE-2022-22662" ],
147- "advisories" : [],
148- },
149- {
150- "name" : "AVG-4" ,
151- "packages" : ["bzip2" ],
152- "status" : "Fixed" ,
153- "severity" : "Low" ,
154- "type" : "denial of service" ,
155- "affected" : "1.0.6-5" ,
156- "fixed" : "1.0.6-6" ,
157- "ticket" : None ,
158- "issues" : ["CVE-2016-3189" ],
159- "advisories" : ["ASA-201702-19" ],
160- },
161- ]
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 )
16250
163- for record in record_list :
164- assert archlinux .ArchlinuxImporter ().parse_advisory (record )
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+ # result = [data.to_dict() for data in archlinux.ArchlinuxImporter().advisory_data()]
54+ # result = archlinux.ArchlinuxImporter().advisory_data()
55+ util_tests .check_results_against_json (result , expected_file )
0 commit comments