Skip to content

Commit f704bbf

Browse files
committed
fix tests on linux
1 parent e0f7f65 commit f704bbf

1 file changed

Lines changed: 39 additions & 17 deletions

File tree

tests/test_restricted_path.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,60 @@
2121

2222
class TestRestrictedPath(unittest.TestCase):
2323
def setUp(self):
24-
# Setup restricted directories for testing if they are global
25-
# server.RESTRICTED_DIRECTORIES is used.
26-
# We should back it up and restore it.
24+
# Setup restricted directories for testing based on OS
2725
self.original_dirs = server.RESTRICTED_DIRECTORIES
28-
server.RESTRICTED_DIRECTORIES = [r"C:\Restricted", r"C:\Windows"]
26+
27+
if os.name == 'nt':
28+
self.restricted_root = r"C:\Restricted"
29+
self.windows_system = r"C:\Windows"
30+
self.allowed_root = r"C:\Allowed"
31+
self.public_docs = r"C:\Users\Public\Documents"
32+
server.RESTRICTED_DIRECTORIES = [self.restricted_root, self.windows_system]
33+
else:
34+
# POSIX paths for Linux execution (GitHub Actions)
35+
self.restricted_root = "/tmp/Restricted"
36+
self.windows_system = "/etc" # acting as a system dir
37+
self.allowed_root = "/tmp/Allowed"
38+
self.public_docs = "/home/user/public"
39+
server.RESTRICTED_DIRECTORIES = [self.restricted_root, self.windows_system]
2940

3041
def tearDown(self):
3142
server.RESTRICTED_DIRECTORIES = self.original_dirs
3243

3344
def test_restricted_absolute_path(self):
34-
self.assertTrue(_is_restricted_path(r"C:\Restricted\secret.txt", pathlib.Path(".")))
35-
self.assertTrue(_is_restricted_path(r"C:\Windows\System32\drivers", pathlib.Path(".")))
36-
self.assertTrue(_is_restricted_path(r"C:\Restricted", pathlib.Path(".")))
45+
# Construct paths using the roots defined in setUp
46+
secret_txt = os.path.join(self.restricted_root, "secret.txt")
47+
system_file = os.path.join(self.windows_system, "System32", "drivers")
48+
49+
self.assertTrue(_is_restricted_path(secret_txt, pathlib.Path(".")))
50+
self.assertTrue(_is_restricted_path(system_file, pathlib.Path(".")))
51+
self.assertTrue(_is_restricted_path(self.restricted_root, pathlib.Path(".")))
3752

3853
def test_allowed_absolute_path(self):
39-
self.assertFalse(_is_restricted_path(r"C:\Allowed\file.txt", pathlib.Path(".")))
40-
self.assertFalse(_is_restricted_path(r"C:\Users\Public\Documents", pathlib.Path(".")))
54+
allowed_file = os.path.join(self.allowed_root, "file.txt")
55+
56+
self.assertFalse(_is_restricted_path(allowed_file, pathlib.Path(".")))
57+
self.assertFalse(_is_restricted_path(self.public_docs, pathlib.Path(".")))
4158

4259
def test_relative_path_skipped(self):
43-
# Relative paths should return False (not restricted check skipped -> not restricted?)
44-
# Logic says: if not absolute return False.
60+
# Relative paths should return False (checks against restricted dirs are for absolute paths)
4561
self.assertFalse(_is_restricted_path("file.txt", pathlib.Path(".")))
46-
self.assertFalse(_is_restricted_path(r"subdir\file.txt", pathlib.Path(".")))
62+
self.assertFalse(_is_restricted_path(os.path.join("subdir", "file.txt"), pathlib.Path(".")))
4763

4864
def test_pathlib_object(self):
49-
self.assertTrue(_is_restricted_path(pathlib.Path(r"C:\Restricted\file.txt"), pathlib.Path(".")))
50-
self.assertFalse(_is_restricted_path(pathlib.Path(r"C:\Allowed\file.txt"), pathlib.Path(".")))
65+
restricted_file = pathlib.Path(self.restricted_root) / "file.txt"
66+
allowed_file = pathlib.Path(self.allowed_root) / "file.txt"
67+
68+
self.assertTrue(_is_restricted_path(restricted_file, pathlib.Path(".")))
69+
self.assertFalse(_is_restricted_path(allowed_file, pathlib.Path(".")))
5170

5271
def test_case_insensitivity_if_windows(self):
53-
# pathlib resolution handles case on Windows usually.
54-
# But we constructed paths.
55-
pass
72+
if os.name == 'nt':
73+
# Check mixed case on Windows
74+
mixed_case = self.restricted_root.lower()
75+
self.assertTrue(_is_restricted_path(mixed_case, pathlib.Path(".")))
76+
else:
77+
pass
5678

5779
if __name__ == '__main__':
5880
unittest.main()

0 commit comments

Comments
 (0)