File: test_files.py

package info (click to toggle)
posting 2.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,556 kB
  • sloc: python: 9,719; makefile: 15
file content (58 lines) | stat: -rw-r--r-- 1,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pytest
from posting.files import is_valid_filename


@pytest.mark.parametrize(
    "filename, expected",
    [
        ("valid_filename.txt", True),
        ("", False),
        ("   ", False),
        ("file/with/path.txt", False),
        ("a" * 255, True),
        ("a" * 256, False),
        ("CON", False),
        ("PRN.txt", False),
        ("AUX.log", False),
        ("NUL.dat", False),
        ("COM1.bin", False),
        ("LPT1.tmp", False),
        ("file..with..dots.txt", False),
        (".hidden_file.txt", False),
        ("normal.file.txt", True),
        ("file-with-dashes.txt", True),
        ("file_with_underscores.txt", True),
        ("file with spaces.txt", True),
        ("file.with.multiple.extensions.txt", True),
        # Path traversal attack tests
        ("../filename.txt", False),
        ("filename/../something.txt", False),
        ("foo/../bar/baz.txt", False),
        ("foo/./bar/baz.txt", False),
        # Absolute path tests
        ("/etc/passwd", False),
        ("/var/log/system.log", False),
        ("/home/user/file.txt", False),
        ("/file.txt", False),
        ("C:/Program Files/file.txt", False),
    ],
)
def test_is_valid_filename(filename, expected):
    assert is_valid_filename(filename) == expected


def test_is_valid_filename_with_none():
    assert is_valid_filename(None) is False


@pytest.mark.parametrize(
    "os_specific_filename, expected",
    [
        ("COM0", True),  # COM0 is not in the reserved list
        ("LPT0", True),  # LPT0 is not in the reserved list
        ("CON.txt", False),
        ("AUX.log", False),
    ],
)
def test_is_valid_filename_os_specific(os_specific_filename, expected):
    assert is_valid_filename(os_specific_filename) == expected