Linux / verify (pull_request) Successful in 3m5s
Linux / keyless-cuda (pull_request) Skipped
Linux / hosted-cuda-package (pull_request) Skipped
Linux / linux-cli-container (pull_request) Successful in 20s
Linux / linux-static-cli (pull_request) Successful in 7m28s
Linux / release-cli (pull_request) Skipped
106 lines
6.7 KiB
Python
106 lines
6.7 KiB
Python
#!/usr/bin/env python3
|
|
import importlib.util
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import tarfile
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
spec=importlib.util.spec_from_file_location('bootstrap',Path(__file__).with_name('bootstrap-cuda-toolkit.py'))
|
|
m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
|
|
|
|
class Tests(unittest.TestCase):
|
|
def test_safe_tree_and_internal_link(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=Path(tmp);archive=root/'test.tar.xz'
|
|
with tarfile.open(archive,'w:xz') as t:
|
|
item=tarfile.TarInfo('component/lib/a');item.size=1;t.addfile(item,io.BytesIO(b'a'))
|
|
item=tarfile.TarInfo('component/lib/b');item.type=tarfile.SYMTYPE;item.linkname='a';t.addfile(item)
|
|
out=m.extract(archive,root/'out','component')
|
|
self.assertEqual((out/'lib/b').read_bytes(),b'a')
|
|
def test_python311_style_data_filter_fallback(self):
|
|
# Python 3.11 has data_filter but extractall(filter=...) is unavailable.
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=Path(tmp);archive=root/'test.tar.xz';dest=root/'out'
|
|
with tarfile.open(archive,'w:xz') as t:
|
|
item=tarfile.TarInfo('component/lib/a');item.size=1;t.addfile(item,io.BytesIO(b'a'))
|
|
item=tarfile.TarInfo('component/lib/b');item.type=tarfile.SYMTYPE;item.linkname='a';t.addfile(item)
|
|
with tarfile.open(archive,'r:xz') as source:
|
|
m.extract_members(source,dest,source.getmembers(),supports_filter=False)
|
|
self.assertEqual((dest/'component/lib/b').read_bytes(),b'a')
|
|
def test_python311_fallback_accepts_members_without_mode(self):
|
|
# NVIDIA component archives can contain directory/file entries without
|
|
# a mode. The legacy tarfile chmod path must never receive None.
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=Path(tmp);archive=root/'test.tar.xz';dest=root/'out'
|
|
with tarfile.open(archive,'w:xz') as t:
|
|
directory=tarfile.TarInfo('component/include');directory.type=tarfile.DIRTYPE;t.addfile(directory)
|
|
item=tarfile.TarInfo('component/include/header.h');item.size=1;t.addfile(item,io.BytesIO(b'h'))
|
|
with tarfile.open(archive,'r:xz') as source:
|
|
members=source.getmembers()
|
|
for member in members:member.mode=None
|
|
# Emulate old extractall()'s fully-trusted default after the
|
|
# caller's explicit data-policy validation has run.
|
|
with patch.object(source,'extraction_filter',lambda member,path: member,create=True):
|
|
m.extract_members(source,dest,members,supports_filter=False)
|
|
self.assertEqual((dest/'component/include/header.h').read_bytes(),b'h')
|
|
self.assertEqual((dest/'component/include').stat().st_mode & 0o777,0o700)
|
|
self.assertEqual((dest/'component/include/header.h').stat().st_mode & 0o777,0o600)
|
|
def test_python311_style_fallback_rejects_escaping_link(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=Path(tmp);archive=root/'test.tar.xz';dest=root/'out'
|
|
with tarfile.open(archive,'w:xz') as t:
|
|
item=tarfile.TarInfo('component/b');item.type=tarfile.SYMTYPE;item.linkname='../../escape';t.addfile(item)
|
|
with tarfile.open(archive,'r:xz') as source:
|
|
with self.assertRaises(ValueError):
|
|
m.extract_members(source,dest,source.getmembers(),supports_filter=False)
|
|
self.assertFalse((root/'escape').exists())
|
|
def test_rejects_member_nested_under_archive_symlink(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=Path(tmp);archive=root/'test.tar.xz'
|
|
with tarfile.open(archive,'w:xz') as t:
|
|
item=tarfile.TarInfo('component/a');item.type=tarfile.SYMTYPE;item.linkname='..';t.addfile(item)
|
|
# Lexically this link is still inside the root, but after the
|
|
# parent symlink is followed it would escape the extraction tree.
|
|
item=tarfile.TarInfo('component/a/x');item.type=tarfile.SYMTYPE;item.linkname='../..';t.addfile(item)
|
|
with self.assertRaises(ValueError):m.extract(archive,root/'out','component')
|
|
def test_bad_archive_paths_and_links(self):
|
|
for name,link in [('../escape',None),('/escape',None),('other/file',None),('component/b','../../escape')]:
|
|
with self.subTest(name=name),tempfile.TemporaryDirectory() as tmp:
|
|
root=Path(tmp);archive=root/'test.tar.xz'
|
|
with tarfile.open(archive,'w:xz') as t:
|
|
item=tarfile.TarInfo(name)
|
|
if link:item.type=tarfile.SYMTYPE;item.linkname=link
|
|
t.addfile(item)
|
|
with self.assertRaises((ValueError,tarfile.TarError)):m.extract(archive,root/'out','component')
|
|
self.assertFalse((root/'escape').exists())
|
|
def test_component_merge_rejects_conflict(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=Path(tmp);a=root/'a';b=root/'b';a.mkdir();b.mkdir()
|
|
(a/'x').write_text('a');(b/'x').write_text('b')
|
|
with self.assertRaises(ValueError):m.merge(a,b)
|
|
self.assertEqual((b/'x').read_text(),'b')
|
|
def test_identical_merge_and_moving(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=Path(tmp);a=root/'a';b=root/'b';a.mkdir();b.mkdir()
|
|
(a/'x').write_text('a');(b/'x').write_text('a');(a/'y').write_text('y')
|
|
m.merge(a,b);self.assertEqual((b/'y').read_text(),'y');self.assertFalse((a/'y').exists())
|
|
def test_download_mismatch_size_and_redirect(self):
|
|
for data,url,sha,limit in [(b'bad',m.BASE+'x','0'*64,4),(b'large',m.BASE+'x','0'*64,2),(b'x','https://other.invalid/x','0'*64,2)]:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
response=io.BytesIO(data);response.url=url
|
|
with patch.object(m.urllib.request,'urlopen',return_value=response),self.assertRaises(ValueError):
|
|
m.download(m.BASE+'x',Path(tmp)/'file',sha,limit)
|
|
def test_lock_pins_match_reference(self):
|
|
lock=json.loads((Path(__file__).resolve().parents[1]/'deploy/cuda-toolkit.lock.json').read_text())
|
|
manifest={x['component']:{'version':x['version'],'linux-x86_64':x.copy()} for x in lock['components']}
|
|
m.validate_lock(lock,manifest)
|
|
lock['components'][0]['sha256']='0'*64
|
|
with self.assertRaises(ValueError):m.validate_lock(lock,manifest)
|
|
def test_resource_gate_before_download(self):
|
|
with patch.object(m.Path,'read_text',return_value='MemTotal: 1024 kB\n'),self.assertRaises(ValueError):m.resource_gate(Path('/tmp'))
|
|
|
|
if __name__=='__main__':unittest.main()
|