msd: allow any printable characters in image name

This commit is contained in:
Devaev Maxim
2019-10-15 08:21:04 +03:00
parent e58ad66da9
commit 7b4818ed15
4 changed files with 70 additions and 38 deletions

View File

@@ -20,17 +20,16 @@
# ========================================================================== #
import re
from typing import Any
from .. import keymap
from . import check_not_none_string
from . import check_string_in_list
from .basic import valid_number
from .os import valid_printable_filename
# =====
def valid_atx_power_action(arg: Any) -> str:
@@ -42,15 +41,7 @@ def valid_atx_button(arg: Any) -> str:
def valid_msd_image_name(arg: Any) -> str:
if len(str(arg).strip()) == 0:
arg = None
arg = check_not_none_string(arg, "MSD image name", strip=True)
arg = re.sub(r"[^\w\.+@()\[\]-]", "_", arg)
if arg == ".":
arg = "_"
if arg == "..":
arg = "__"
return arg[:255]
return valid_printable_filename(arg, name="MSD image name") # pragma: nocover
def valid_log_seek(arg: Any) -> int:

View File

@@ -51,6 +51,25 @@ def valid_abs_path_exists(arg: Any, name: str="") -> str:
return valid_abs_path(arg, exists=True, name=name)
def valid_printable_filename(arg: Any, name: str="") -> str:
if not name:
name = "printable filename"
if len(str(arg).strip()) == 0:
arg = None
arg = check_not_none_string(arg, name)
if "/" in arg or "\0" in arg or arg in [".", ".."]:
raise_error(arg, name)
arg = "".join(
(ch if ch.isprintable() else "_")
for ch in arg[:255]
)
return arg
# =====
def valid_unix_mode(arg: Any) -> int:
return int(valid_number(arg, min=0, name="UNIX mode"))

View File

@@ -32,7 +32,6 @@ from kvmd.validators.kvm import valid_atx_button
from kvmd.validators.kvm import valid_log_seek
from kvmd.validators.kvm import valid_stream_quality
from kvmd.validators.kvm import valid_stream_fps
from kvmd.validators.kvm import valid_msd_image_name
from kvmd.validators.kvm import valid_hid_key
from kvmd.validators.kvm import valid_hid_mouse_move
from kvmd.validators.kvm import valid_hid_mouse_button
@@ -105,31 +104,6 @@ def test_fail__valid_stream_fps(arg: Any) -> None:
print(valid_stream_fps(arg))
# =====
@pytest.mark.parametrize("arg, retval", [
("archlinux-2018.07.01-i686.iso", "archlinux-2018.07.01-i686.iso"),
("archlinux-2018.07.01-x86_64.iso", "archlinux-2018.07.01-x86_64.iso"),
("dsl-4.11.rc1.iso", "dsl-4.11.rc1.iso"),
("systemrescuecd-x86-5.3.1.iso", "systemrescuecd-x86-5.3.1.iso"),
("ubuntu-16.04.5-desktop-i386.iso", "ubuntu-16.04.5-desktop-i386.iso"),
(".", "_"),
("..", "__"),
("/..", "_.."),
("/..\0", "_.._"),
("/root/..", "_root_.."),
(" тест(){}[ \t].iso\t", "тест()__[__].iso"),
("?" * 1000, "_" * 255),
])
def test_ok__valid_msd_image_name(arg: Any, retval: str) -> None:
assert valid_msd_image_name(arg) == retval
@pytest.mark.parametrize("arg", ["", None])
def test_fail__valid_msd_image_name(arg: Any) -> None:
with pytest.raises(ValidatorError):
print(valid_msd_image_name(arg))
# =====
def test_ok__valid_hid_key() -> None:
for key in KEYMAP:

View File

@@ -30,6 +30,7 @@ import pytest
from kvmd.validators import ValidatorError
from kvmd.validators.os import valid_abs_path
from kvmd.validators.os import valid_abs_path_exists
from kvmd.validators.os import valid_printable_filename
from kvmd.validators.os import valid_unix_mode
from kvmd.validators.os import valid_command
@@ -79,6 +80,53 @@ def test_fail__valid_abs_path_exists(arg: Any) -> None:
print(valid_abs_path_exists(arg))
# =====
@pytest.mark.parametrize("arg, retval", [
("archlinux-2018.07.01-i686.iso", "archlinux-2018.07.01-i686.iso"),
("archlinux-2018.07.01-x86_64.iso", "archlinux-2018.07.01-x86_64.iso"),
("dsl-4.11.rc1.iso", "dsl-4.11.rc1.iso"),
("systemrescuecd-x86-5.3.1.iso", "systemrescuecd-x86-5.3.1.iso"),
("ubuntu-16.04.5-desktop-i386.iso", "ubuntu-16.04.5-desktop-i386.iso"),
(" тест(){}[ \t].iso\t", "тест(){}[ _].iso"),
("\n" + "x" * 1000, "x" * 255),
("test", "test"),
("test test [test] #test$", "test test [test] #test$"),
(".test", ".test"),
("..test", "..test"),
("..тест..", "..тест.."),
("..те\\ст..", "..те\\ст.."),
(".....", "....."),
(".....txt", ".....txt"),
(" .. .", ".. ."),
("..\n.", ".._."),
])
def test_ok__valid_printable_filename(arg: Any, retval: str) -> None:
assert valid_printable_filename(arg) == retval
@pytest.mark.parametrize("arg", [
".",
"..",
" ..",
"test/",
"/test",
"../test",
"./.",
"../.",
"./..",
"../..",
"/ ..",
".. /",
"/.. /",
"",
" ",
None,
])
def test_fail__valid_printable_filename(arg: Any) -> None:
with pytest.raises(ValidatorError):
valid_printable_filename(arg)
# =====
@pytest.mark.parametrize("arg", [0, 5, "1000"])
def test_ok__valid_unix_mode(arg: Any) -> None: