def test_get_victims_notation(self): """Test `utils.get_victims_notation` function.""" victims_pattern = r"^(?P<condition>[><=]=)" \ r"(?P<version>[^, ]+)" \ r"(?:,(?P<series>[^, ]+)){0,1}$" # versions in tuple format: # (versionExact, versionEndExcluding, versionEndIncluding, # versionStartIncluding, versionEndExcluding) # empty version_tuple = (None, None, None, None, None) victims_notation = utils.get_victims_notation(version_tuple) self.assertIsNone(victims_notation) # exact version_tuple = ('1.0', None, None, None, None) victims_notation = utils.get_victims_notation(version_tuple) self.assertTrue( all([re.fullmatch(victims_pattern, vn) for vn in victims_notation])) self.assertEqual(victims_notation, ["==1.0"]) # including-excluding version_tuple = (None, None, '2.0', None, '1.0') victims_notation = utils.get_victims_notation(version_tuple) # TODO: should we solve this? # self.assertTrue( # all([re.fullmatch(victims_pattern, vn) for vn in victims_notation]) # ) self.assertEqual(victims_notation, ['<=2.0', '>1.0']) # excluding-excluding version_tuple = (None, '2.0', None, None, '1.0') victims_notation = utils.get_victims_notation(version_tuple) # TODO: should we solve this? # self.assertTrue( # all([re.fullmatch(victims_pattern, vn) for vn in victims_notation]) # ) self.assertEqual(victims_notation, ['<2.0', '>1.0']) # including-including version_tuple = (None, None, '2.0', '1.0', None) victims_notation = utils.get_victims_notation(version_tuple) self.assertTrue( all([re.fullmatch(victims_pattern, vn) for vn in victims_notation])) self.assertEqual(victims_notation, ['<=2.0', '>=1.0'])
def parse(self, entry: typing.Any): try: version_exact = CPE(entry['cpe23Uri']).get_version()[0] or None except NotImplementedError: # workaround for invalid CPE string entry, see [#6] # [#6]: https://github.com/fabric8-analytics/nvdlib/pull/6 version_exact = None if version_exact in ['-', '*']: # same as missing entry version_exact = None version_end_excl = entry.get('versionEndExcluding', None) version_end_incl = entry.get('versionEndIncluding', None) version_start_incl = entry.get('versionStartIncluding', None) version_start_excl = entry.get('versionStartExcluding', None) version_range = utils.get_victims_notation( (version_exact, version_end_excl, version_end_incl, version_start_incl, version_start_excl)) return self.ConfigurationsNode(vulnerable=entry['vulnerable'], cpe=entry['cpe23Uri'], version_range=version_range)