28 lines
1002 B
Python
28 lines
1002 B
Python
import unittest
|
|
import os
|
|
|
|
from src.gui.components.explorer import _is_valid_entry_name, _is_within_root
|
|
|
|
|
|
class TestExplorerSafety(unittest.TestCase):
|
|
def test_is_valid_entry_name_rejects_path_segments(self):
|
|
self.assertFalse(_is_valid_entry_name("../outside.txt"))
|
|
self.assertFalse(_is_valid_entry_name("nested/file.txt"))
|
|
self.assertFalse(_is_valid_entry_name(r"nested\file.txt"))
|
|
self.assertFalse(_is_valid_entry_name(".."))
|
|
|
|
def test_is_valid_entry_name_accepts_simple_name(self):
|
|
self.assertTrue(_is_valid_entry_name("song.lmd"))
|
|
self.assertTrue(_is_valid_entry_name("verse_01"))
|
|
|
|
def test_is_within_root(self):
|
|
root = os.path.join("tmp", "project")
|
|
inside = os.path.join(root, "lyrics", "song.lmd")
|
|
outside = os.path.join("tmp", "other", "song.lmd")
|
|
self.assertTrue(_is_within_root(root, inside))
|
|
self.assertFalse(_is_within_root(root, outside))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|