Archived
48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
import io
|
|
from pathlib import Path
|
|
import runpy
|
|
import tarfile
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def helper():
|
|
pytest.importorskip('fcntl')
|
|
return runpy.run_path(str(Path(__file__).resolve().parents[1] / 'scripts/wgtools-helper'))
|
|
|
|
|
|
def archive(path, entries):
|
|
with tarfile.open(path, 'w:gz') as tar:
|
|
for name, content in entries:
|
|
item = tarfile.TarInfo(name)
|
|
if content is None:
|
|
item.type = tarfile.SYMTYPE
|
|
item.linkname = '/etc/passwd'
|
|
tar.addfile(item)
|
|
else:
|
|
item.size = len(content)
|
|
tar.addfile(item, io.BytesIO(content))
|
|
|
|
|
|
def test_uploaded_package_rejects_paths_and_links(helper, tmp_path):
|
|
for name, content in [('../escape', b'bad'), ('link', None), ('/etc/passwd', b'bad')]:
|
|
package = tmp_path / 'unsafe.bin'
|
|
archive(package, [(name, content)])
|
|
with pytest.raises(ValueError):
|
|
helper['_extract_uploaded_update'](package, tmp_path / 'dest')
|
|
assert not (tmp_path / 'escape').exists()
|
|
|
|
|
|
def test_uploaded_package_requires_newer_version(helper, tmp_path, monkeypatch):
|
|
gl = helper['_uploaded_update_validate'].__globals__
|
|
monkeypatch.setitem(gl, '_validate_update_source', lambda *args: None)
|
|
monkeypatch.setitem(gl, '_current_wgtools_version', lambda: '1.2.3')
|
|
package = tmp_path / 'update.bin'
|
|
archive(package, [('VERSION', b'1.2.4\n')])
|
|
_, version = helper['_uploaded_update_validate'](package, tmp_path / 'good')
|
|
assert version == '1.2.4'
|
|
archive(package, [('VERSION', b'1.2.3\n')])
|
|
with pytest.raises(ValueError, match='nowsza'):
|
|
helper['_uploaded_update_validate'](package, tmp_path / 'old')
|