51 lines
2.1 KiB
Python
51 lines
2.1 KiB
Python
import hashlib
|
|
import io
|
|
from pathlib import Path
|
|
import tarfile
|
|
import tempfile
|
|
import unittest
|
|
|
|
import bundle
|
|
|
|
|
|
class BundleTest(unittest.TestCase):
|
|
def test_notice_superset_is_inert(self):
|
|
p = bundle.NoticeText()
|
|
p.feed('<style>hidden</style><h1>Licenses</h1><p>Copyright & authors</p><script>bad()</script><a href="https://example.org/license">Terms</a>')
|
|
result = ''.join(p.parts)
|
|
self.assertIn('Copyright & authors', result)
|
|
self.assertIn('https://example.org/license', result)
|
|
self.assertNotIn('bad()', result)
|
|
self.assertNotIn('hidden', result)
|
|
|
|
def test_hash_rejects_changed_cached_source(self):
|
|
with tempfile.TemporaryDirectory() as name:
|
|
cache = Path(name)
|
|
(cache / 'source').write_bytes(b'changed')
|
|
with self.assertRaisesRegex(ValueError, 'checksum'):
|
|
bundle.obtain(cache, 'source', 'https://invalid.test', '0' * 64, True)
|
|
self.assertEqual(bundle.obtain(cache, 'source', '', hashlib.sha256(b'changed').hexdigest(), True), cache / 'source')
|
|
|
|
def test_archive_is_read_not_extracted(self):
|
|
with tempfile.TemporaryDirectory() as name:
|
|
path = Path(name) / 'source.tar.gz'
|
|
with tarfile.open(path, 'w:gz') as archive:
|
|
member = tarfile.TarInfo('../../LICENSE')
|
|
data = b'Copyright owner. Permission notice.'
|
|
member.size = len(data)
|
|
archive.addfile(member, io.BytesIO(data))
|
|
member = tarfile.TarInfo('src/license.html')
|
|
data = b'#ifdef OPTIONAL\n<p>Optional terms</p>\n#else\n<p>Other terms</p>\n#endif'
|
|
member.size = len(data)
|
|
archive.addfile(member, io.BytesIO(data))
|
|
notice = bundle.notices(path)
|
|
self.assertIn('Copyright owner', notice)
|
|
self.assertIn('Optional terms', notice)
|
|
self.assertIn('Other terms', notice)
|
|
self.assertNotIn('#ifdef', notice)
|
|
self.assertEqual(list(Path(name).iterdir()), [path])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|