|
21 | 21 |
|
22 | 22 | class TestRestrictedPath(unittest.TestCase): |
23 | 23 | 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 |
27 | 25 | 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] |
29 | 40 |
|
30 | 41 | def tearDown(self): |
31 | 42 | server.RESTRICTED_DIRECTORIES = self.original_dirs |
32 | 43 |
|
33 | 44 | 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("."))) |
37 | 52 |
|
38 | 53 | 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("."))) |
41 | 58 |
|
42 | 59 | 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) |
45 | 61 | 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("."))) |
47 | 63 |
|
48 | 64 | 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("."))) |
51 | 70 |
|
52 | 71 | 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 |
56 | 78 |
|
57 | 79 | if __name__ == '__main__': |
58 | 80 | unittest.main() |
0 commit comments