tests/test_sync.py aktualisiert

This commit is contained in:
2026-11-11 07:16:16 +01:00
parent 407cea6beb
commit de29f8ee49
+24 -5
View File
@@ -18,24 +18,43 @@ class SyncTests(unittest.TestCase):
with tarfile.open(archive,'w:bz2') as tf:
for name in ('csaf-vex/2025/cve-2025-12345.json','csaf-vex/2016/cve-2016-9999.json'):
info=tarfile.TarInfo(name);info.size=len(data);tf.addfile(info,io.BytesIO(data))
items=parse_archive(archive,2017)
self.assertEqual(list(items),['2025/CVE-2025-12345.json'])
count, digest=parse_archive(archive,2017,p/"staging")
self.assertEqual(count,1)
self.assertTrue((p/'staging/2025/CVE-2025-12345.json').exists())
import os
cwd=Path.cwd();os.chdir(p)
try:
result=reconcile(items,Path('data/vex'),Path('state/sync.json'),'test',2017)
result=reconcile(p/'staging',Path('data/vex'),Path('state/sync.json'),'test',2017,count,digest)
self.assertEqual(result['record_count'],1)
validate()
(Path('data/vex/2025')/'CVE-2025-12345.json').unlink()
reconcile(items,Path('data/vex'),Path('state/sync.json'),'test',2017)
reconcile(p/'staging',Path('data/vex'),Path('state/sync.json'),'test',2017,count,digest)
validate()
finally:os.chdir(cwd)
def test_document_without_vulnerabilities_is_preserved(self):
with tempfile.TemporaryDirectory() as tmp:
p=Path(tmp); archive=p/'document-only.tar.bz2'
data=json.dumps({'document': {'category': 'csaf_vex', 'csaf_version': '2.0'}}).encode()
with tarfile.open(archive,'w:bz2') as tf:
member=tarfile.TarInfo('csaf-vex/2024/cve-2018-15157.json')
member.size=len(data)
tf.addfile(member,io.BytesIO(data))
count, digest=parse_archive(archive,2017,p/"staging")
self.assertEqual(count,1)
self.assertTrue((p/'staging/2018/CVE-2018-15157.json').exists())
import os
cwd=Path.cwd();os.chdir(p)
try:
reconcile(p/'staging',Path('data/vex'),Path('state/sync.json'),'test',2017,count,digest)
validate()
finally:os.chdir(cwd)
def test_invalid_csaf_fails_before_write(self):
with tempfile.TemporaryDirectory() as tmp:
p=Path(tmp);archive=p/'bad.tar.bz2'
data=b'{bad'
with tarfile.open(archive,'w:bz2') as tf:
x=tarfile.TarInfo('cve-2026-1111.json');x.size=len(data);tf.addfile(x,io.BytesIO(data))
with self.assertRaises(json.JSONDecodeError):parse_archive(archive,2017)
with self.assertRaises(json.JSONDecodeError):parse_archive(archive,2017,p/"staging")
if __name__=='__main__':unittest.main()