|
| 1 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 2 | +# http://nexb.com and https://github.com/nexB/vulnerablecode/ |
| 3 | +# The VulnerableCode software is licensed under the Apache License version 2.0. |
| 4 | +# Data generated with VulnerableCode require an acknowledgment. |
| 5 | +# |
| 6 | +# You may not use this software except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 9 | +# under the License is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR |
| 10 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +# specific language governing permissions and limitations under the License. |
| 12 | +# |
| 13 | +# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode |
| 14 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 15 | +# |
| 16 | +# Generated with VulnerableCode and provided on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 17 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 18 | +# VulnerableCode should be considered or used as legal advice. Consult an Attorney |
| 19 | +# for any legal advice. |
| 20 | +# VulnerableCode is a free software code scanning tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/nexB/vulnerablecode/ for support and download. |
| 22 | + |
| 23 | +import bz2 |
| 24 | +from collections import OrderedDict |
| 25 | +import json |
| 26 | +import os |
| 27 | +from unittest import TestCase |
| 28 | +from unittest.mock import MagicMock |
| 29 | +from unittest.mock import patch |
| 30 | + |
| 31 | +from packageurl import PackageURL |
| 32 | + |
| 33 | +from vulnerabilities.data_source import Advisory |
| 34 | +import vulnerabilities.importers.ubuntu_usn as ubuntu_usn |
| 35 | + |
| 36 | +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 37 | +TEST_DATA = os.path.join(BASE_DIR, 'test_data/', 'ubuntu_usn_db', 'database-all.json.bz2') |
| 38 | + |
| 39 | + |
| 40 | +class TestUbuntuUSNDataSource(TestCase): |
| 41 | + @classmethod |
| 42 | + def setUpClass(cls): |
| 43 | + data_src_cfg = {'etags': {}, 'db_url': 'http://exampledb.com'} |
| 44 | + cls.data_src = ubuntu_usn.UbuntuUSNDataSource(batch_size=1, config=data_src_cfg) |
| 45 | + with open(TEST_DATA, 'rb') as f: |
| 46 | + cls.raw_data = f.read() |
| 47 | + cls.db = json.loads(bz2.decompress(cls.raw_data)) |
| 48 | + |
| 49 | + def test_get_usn_references(self): |
| 50 | + |
| 51 | + eg_usn = '435-1' |
| 52 | + expected_references = { |
| 53 | + 'reference_id': 'USN-435-1', |
| 54 | + 'reference_url': ['https://usn.ubuntu.com/435-1/'], |
| 55 | + } |
| 56 | + |
| 57 | + found_references = ubuntu_usn.get_usn_references(eg_usn) |
| 58 | + assert found_references == expected_references |
| 59 | + |
| 60 | + def test_fetch(self): |
| 61 | + |
| 62 | + mock_response = MagicMock() |
| 63 | + mock_response.content = self.raw_data |
| 64 | + with patch('vulnerabilities.importers.ubuntu_usn.requests.get', return_value=mock_response): |
| 65 | + assert ubuntu_usn.fetch('www.db.com') == self.db |
| 66 | + |
| 67 | + def test_get_purls(self): |
| 68 | + |
| 69 | + eg_pkg_dict_1 = self.db['763-1']['releases']['hardy'] |
| 70 | + eg_pkg_dict_2 = self.db['763-1']['releases']['dapper'] |
| 71 | + eg_pkg_dict_3 = self.db['763-1']['releases']['intrepid'] |
| 72 | + |
| 73 | + exp_pkgs_1 = { |
| 74 | + PackageURL( |
| 75 | + type='deb', |
| 76 | + namespace='ubuntu', |
| 77 | + name='xine-lib', |
| 78 | + version='1.1.11.1-1ubuntu3.4', |
| 79 | + qualifiers=OrderedDict(), |
| 80 | + subpath=None, |
| 81 | + ), |
| 82 | + PackageURL( |
| 83 | + type='deb', |
| 84 | + namespace='ubuntu', |
| 85 | + name='libxine1', |
| 86 | + version='1.1.11.1-1ubuntu3.4', |
| 87 | + qualifiers=OrderedDict(), |
| 88 | + subpath=None, |
| 89 | + ), |
| 90 | + } |
| 91 | + exp_pkgs_2 = { |
| 92 | + PackageURL( |
| 93 | + type='deb', |
| 94 | + namespace='ubuntu', |
| 95 | + name='libxine-main1', |
| 96 | + version='1.1.1+ubuntu2-7.12', |
| 97 | + qualifiers=OrderedDict(), |
| 98 | + subpath=None, |
| 99 | + ), |
| 100 | + PackageURL( |
| 101 | + type='deb', |
| 102 | + namespace='ubuntu', |
| 103 | + name='xine-lib', |
| 104 | + version='1.1.1+ubuntu2-7.12', |
| 105 | + qualifiers=OrderedDict(), |
| 106 | + subpath=None, |
| 107 | + ), |
| 108 | + } |
| 109 | + exp_pkgs_3 = { |
| 110 | + PackageURL( |
| 111 | + type='deb', |
| 112 | + namespace='ubuntu', |
| 113 | + name='xine-lib', |
| 114 | + version='1.1.15-0ubuntu3.3', |
| 115 | + qualifiers=OrderedDict(), |
| 116 | + subpath=None, |
| 117 | + ), |
| 118 | + PackageURL( |
| 119 | + type='deb', |
| 120 | + namespace='ubuntu', |
| 121 | + name='libxine1', |
| 122 | + version='1.1.15-0ubuntu3.3', |
| 123 | + qualifiers=OrderedDict(), |
| 124 | + subpath=None, |
| 125 | + ), |
| 126 | + } |
| 127 | + |
| 128 | + assert exp_pkgs_1 == ubuntu_usn.get_purls(eg_pkg_dict_1) |
| 129 | + assert exp_pkgs_2 == ubuntu_usn.get_purls(eg_pkg_dict_2) |
| 130 | + assert exp_pkgs_3 == ubuntu_usn.get_purls(eg_pkg_dict_3) |
| 131 | + |
| 132 | + def test_to_advisories(self): |
| 133 | + |
| 134 | + expected_advisories = { |
| 135 | + Advisory( |
| 136 | + summary='', |
| 137 | + impacted_package_urls=[], |
| 138 | + resolved_package_urls={ |
| 139 | + PackageURL( |
| 140 | + type='deb', |
| 141 | + namespace='ubuntu', |
| 142 | + name='xine-lib', |
| 143 | + version='1.1.15-0ubuntu3.3', |
| 144 | + qualifiers=OrderedDict(), |
| 145 | + subpath=None, |
| 146 | + ), |
| 147 | + PackageURL( |
| 148 | + type='deb', |
| 149 | + namespace='ubuntu', |
| 150 | + name='libxine1', |
| 151 | + version='1.1.15-0ubuntu3.3', |
| 152 | + qualifiers=OrderedDict(), |
| 153 | + subpath=None, |
| 154 | + ), |
| 155 | + }, |
| 156 | + reference_urls=['https://usn.ubuntu.com/763-1/'], |
| 157 | + reference_ids=['USN-763-1'], |
| 158 | + cve_id='CVE-2009-0698', |
| 159 | + ), |
| 160 | + Advisory( |
| 161 | + summary='', |
| 162 | + impacted_package_urls=[], |
| 163 | + resolved_package_urls={ |
| 164 | + PackageURL( |
| 165 | + type='deb', |
| 166 | + namespace='ubuntu', |
| 167 | + name='xine-lib', |
| 168 | + version='1.1.15-0ubuntu3.3', |
| 169 | + qualifiers=OrderedDict(), |
| 170 | + subpath=None, |
| 171 | + ), |
| 172 | + PackageURL( |
| 173 | + type='deb', |
| 174 | + namespace='ubuntu', |
| 175 | + name='libxine1', |
| 176 | + version='1.1.15-0ubuntu3.3', |
| 177 | + qualifiers=OrderedDict(), |
| 178 | + subpath=None, |
| 179 | + ), |
| 180 | + }, |
| 181 | + reference_urls=['https://usn.ubuntu.com/763-1/'], |
| 182 | + reference_ids=['USN-763-1'], |
| 183 | + cve_id='CVE-2009-1274', |
| 184 | + ), |
| 185 | + } |
| 186 | + found_advisories = set(self.data_src.to_advisories(self.db)) |
| 187 | + |
| 188 | + assert expected_advisories == found_advisories |
| 189 | + |
| 190 | + def test_create_etag(self): |
| 191 | + assert self.data_src.config.etags == {} |
| 192 | + |
| 193 | + mock_response = MagicMock() |
| 194 | + mock_response.headers = {'etag': '2131151243&2191'} |
| 195 | + |
| 196 | + with patch('vulnerabilities.importers.ubuntu.requests.head', return_value=mock_response): |
| 197 | + assert self.data_src.create_etag('https://example.org') |
| 198 | + assert self.data_src.config.etags == {'https://example.org': '2131151243&2191'} |
| 199 | + assert not self.data_src.create_etag('https://example.org') |
0 commit comments