Files
supervisor/tests/utils/test_tarfile.py
T

64 lines
1.5 KiB
Python

"""Test Tarfile functions."""
from pathlib import PurePath
import attr
from supervisor.utils.tar import _is_excluded_by_filter, secure_path
@attr.s
class TarInfo:
"""Fake TarInfo."""
name: str = attr.ib()
def test_secure_path():
"""Test Secure Path."""
test_list = [
TarInfo("test.txt"),
TarInfo("data/xy.blob"),
TarInfo("bla/blu/ble"),
TarInfo("data/../xy.blob"),
]
assert test_list == list(secure_path(test_list))
def test_not_secure_path():
"""Test Not secure path."""
test_list = [
TarInfo("/test.txt"),
TarInfo("data/../../xy.blob"),
TarInfo("/bla/blu/ble"),
]
assert [] == list(secure_path(test_list))
def test_is_excluded_by_filter_good():
"""Test exclude filter."""
filter_list = ["not/match", "/dev/xy"]
test_list = [
PurePath("test.txt"),
PurePath("data/xy.blob"),
PurePath("bla/blu/ble"),
PurePath("data/../xy.blob"),
]
for path_object in test_list:
assert _is_excluded_by_filter(path_object, filter_list) is False
def test_is_exclude_by_filter_bad():
"""Test exclude filter."""
filter_list = ["*.txt", "data/*", "bla/blu/ble"]
test_list = [
PurePath("test.txt"),
PurePath("data/xy.blob"),
PurePath("bla/blu/ble"),
PurePath("data/test_files/kk.txt"),
]
for path_object in test_list:
assert _is_excluded_by_filter(path_object, filter_list) is True