about summary refs log tree commit diff stats
path: root/tests/test_utils.py
blob: 5c1b205936f8e194440d8d9c5a0921e61d7e375c (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Tests for pure utility functions in fungus.utils_pure."""

from fungus.utils_pure import format_title, is_executable_in_whitelist


class TestFormatTitle:
    def test_basic_title(self):
        assert format_title("My Game") == "my-game"

    def test_removes_special_chars(self):
        assert format_title("Game! @#$% Title") == "game-title"

    def test_lowercases(self):
        assert format_title("UPPERCASE GAME") == "uppercase-game"

    def test_multiple_spaces_to_hyphens(self):
        assert format_title("game   with   spaces") == "game-with-spaces"

    def test_empty_string(self):
        assert format_title("") == ""

    def test_whitespace_only(self):
        assert format_title("   ") == ""

    def test_leading_trailing_whitespace(self):
        assert format_title("  My Game  ") == "my-game"

    def test_numbers_preserved(self):
        assert format_title("Game 2024") == "game-2024"

    def test_unicode_removal(self):
        # Non-ASCII characters are stripped by the regex
        result = format_title("Game café")
        assert result == "game-caf"  # é is removed, not normalized


class TestIsExecutableInWhitelist:
    def test_empty_whitelist_allows_everything(self):
        assert is_executable_in_whitelist("/some/path/game", []) is True

    def test_exact_dir_match(self):
        game_dirs = ["/home/user/Games"]
        assert is_executable_in_whitelist("/home/user/Games/game.exe", game_dirs) is True

    def test_subdir_match(self):
        game_dirs = ["/home/user/Games"]
        assert (
            is_executable_in_whitelist(
                "/home/user/Games/subfolder/game.exe", game_dirs
            )
            is True
        )

    def test_no_match_outside_whitelist(self):
        game_dirs = ["/home/user/Games"]
        assert (
            is_executable_in_whitelist("/opt/other/game.exe", game_dirs) is False
        )

    def test_multiple_whitelisted_dirs(self):
        game_dirs = ["/home/user/Games", "/mnt/steam/steamapps"]
        assert (
            is_executable_in_whitelist("/mnt/steam/steamapps/game.exe", game_dirs)
            is True
        )
        assert (
            is_executable_in_whitelist("/home/user/Games/another/game.exe", game_dirs)
            is True
        )
        assert (
            is_executable_in_whitelist("/tmp/hacker/game.exe", game_dirs) is False
        )

    def test_resolves_symlinks(self):
        """Should use realpath to resolve symlinks before checking."""
        game_dirs = ["/home/user/Games"]
        # Even with a symlink-like path, realpath normalization matters
        # We can't easily create symlinks in tests, but we verify the
        # function uses os.path.realpath internally.
        assert is_executable_in_whitelist("/home/user/Games/game.exe", game_dirs) is True

    def test_prefix_match_prevents_escape(self):
        """Game dirs starting with same prefix should not match."""
        game_dirs = ["/home/user/Games"]
        # /home/user/GamesExtra should NOT match /home/user/Games
        assert (
            is_executable_in_whitelist("/home/user/GamesExtra/game.exe", game_dirs)
            is False
        )