| 1 |
import os |
| 2 |
import stat |
| 3 |
import sys |
| 4 |
import tempfile |
| 5 |
import unittest |
| 6 |
from pathlib import Path |
| 7 |
|
| 8 |
from django.core.exceptions import SuspiciousFileOperation |
| 9 |
from django.utils._os import safe_join, safe_makedirs, to_path |
| 10 |
|
| 11 |
|
| 12 |
class SafeMakeDirsTests(unittest.TestCase): |
| 13 |
def setUp(self): |
| 14 |
tmp = tempfile.TemporaryDirectory() |
| 15 |
self.base = tmp.name |
| 16 |
self.addCleanup(tmp.cleanup) |
| 17 |
|
| 18 |
def assertDirMode(self, path, expected): |
| 19 |
self.assertIs(os.path.isdir(path), True) |
| 20 |
if sys.platform == "win32": |
| 21 |
# Windows partially supports chmod: dirs always end up with 0o777. |
| 22 |
expected = 0o777 |
| 23 |
|
| 24 |
# These tests assume a typical process umask (0o022 or similar): they |
| 25 |
# create directories with modes like 0o755 and 0o700, which don't have |
| 26 |
# group/world write bits, so a typical umask doesn't change the final |
| 27 |
# permissions. On unexpected failures, check whether umask has changed. |
| 28 |
self.assertEqual(stat.S_IMODE(os.stat(path).st_mode), expected) |
| 29 |
|
| 30 |
def test_creates_directory_hierarchy_with_permissions(self): |
| 31 |
path = os.path.join(self.base, "a", "b", "c") |
| 32 |
safe_makedirs(path, mode=0o755) |
| 33 |
|
| 34 |
self.assertDirMode(os.path.join(self.base, "a"), 0o755) |
| 35 |
self.assertDirMode(os.path.join(self.base, "a", "b"), 0o755) |
| 36 |
self.assertDirMode(path, 0o755) |
| 37 |
|
| 38 |
def test_existing_directory_exist_ok(self): |
| 39 |
path = os.path.join(self.base, "a") |
| 40 |
os.mkdir(path, 0o700) |
| 41 |
|
| 42 |
safe_makedirs(path, mode=0o755, exist_ok=True) |
| 43 |
|
| 44 |
self.assertDirMode(path, 0o700) |
| 45 |
|
| 46 |
def test_existing_directory_exist_ok_false_raises(self): |
| 47 |
path = os.path.join(self.base, "a") |
| 48 |
os.mkdir(path) |
| 49 |
|
| 50 |
with self.assertRaises(FileExistsError): |
| 51 |
safe_makedirs(path, mode=0o755, exist_ok=False) |
| 52 |
|
| 53 |
def test_existing_file_at_target_raises(self): |
| 54 |
path = os.path.join(self.base, "a") |
| 55 |
with open(path, "w") as f: |
| 56 |
f.write("x") |
| 57 |
|
| 58 |
with self.assertRaises(FileExistsError): |
| 59 |
safe_makedirs(path, mode=0o755, exist_ok=False) |
| 60 |
|
| 61 |
with self.assertRaises(FileExistsError): |
| 62 |
safe_makedirs(path, mode=0o755, exist_ok=True) |
| 63 |
|
| 64 |
def test_file_in_intermediate_path_raises(self): |
| 65 |
file_path = os.path.join(self.base, "a") |
| 66 |
with open(file_path, "w") as f: |
| 67 |
f.write("x") |
| 68 |
|
| 69 |
path = os.path.join(file_path, "b") |
| 70 |
|
| 71 |
expected = FileNotFoundError if sys.platform == "win32" else NotADirectoryError |
| 72 |
|
| 73 |
with self.assertRaises(expected): |
| 74 |
safe_makedirs(path, mode=0o755, exist_ok=False) |
| 75 |
|
| 76 |
with self.assertRaises(expected): |
| 77 |
safe_makedirs(path, mode=0o755, exist_ok=True) |
| 78 |
|
| 79 |
def test_existing_parent_preserves_permissions(self): |
| 80 |
a = os.path.join(self.base, "a") |
| 81 |
b = os.path.join(a, "b") |
| 82 |
|
| 83 |
os.mkdir(a, 0o700) |
| 84 |
|
| 85 |
safe_makedirs(b, mode=0o755, exist_ok=False) |
| 86 |
|
| 87 |
self.assertDirMode(a, 0o700) |
| 88 |
self.assertDirMode(b, 0o755) |
| 89 |
|
| 90 |
c = os.path.join(a, "c") |
| 91 |
safe_makedirs(c, mode=0o750, exist_ok=True) |
| 92 |
|
| 93 |
self.assertDirMode(a, 0o700) |
| 94 |
self.assertDirMode(c, 0o750) |
| 95 |
|
| 96 |
def test_path_is_normalized(self): |
| 97 |
path = os.path.join(self.base, "a", "b", "..", "c") |
| 98 |
safe_makedirs(path, mode=0o755) |
| 99 |
|
| 100 |
self.assertDirMode(os.path.normpath(path), 0o755) |
| 101 |
self.assertIs(os.path.isdir(os.path.join(self.base, "a", "c")), True) |
| 102 |
|
| 103 |
def test_permissions_unaffected_by_process_umask(self): |
| 104 |
path = os.path.join(self.base, "a", "b", "c") |
| 105 |
# `umask()` returns the current mask, so it'll be restored on cleanup. |
| 106 |
self.addCleanup(os.umask, os.umask(0o077)) |
| 107 |
|
| 108 |
safe_makedirs(path, mode=0o755) |
| 109 |
|
| 110 |
self.assertDirMode(os.path.join(self.base, "a"), 0o755) |
| 111 |
self.assertDirMode(os.path.join(self.base, "a", "b"), 0o755) |
| 112 |
self.assertDirMode(path, 0o755) |
| 113 |
|
| 114 |
def test_permissions_correct_despite_concurrent_umask_change(self): |
| 115 |
path = os.path.join(self.base, "a", "b", "c") |
| 116 |
original_mkdir = os.mkdir |
| 117 |
# `umask()` returns the current mask, so it'll be restored on cleanup. |
| 118 |
self.addCleanup(os.umask, os.umask(0o000)) |
| 119 |
|
| 120 |
def mkdir_changing_umask(p, mode): |
| 121 |
# Simulate a concurrent thread changing the process umask. |
| 122 |
os.umask(0o077) |
| 123 |
original_mkdir(p, mode) |
| 124 |
|
| 125 |
with unittest.mock.patch("os.mkdir", side_effect=mkdir_changing_umask): |
| 126 |
safe_makedirs(path, mode=0o755) |
| 127 |
|
| 128 |
self.assertDirMode(os.path.join(self.base, "a"), 0o755) |
| 129 |
self.assertDirMode(os.path.join(self.base, "a", "b"), 0o755) |
| 130 |
self.assertDirMode(path, 0o755) |
| 131 |
|
| 132 |
def test_race_condition_exist_ok_false(self): |
| 133 |
path = os.path.join(self.base, "a", "b") |
| 134 |
|
| 135 |
original_mkdir = os.mkdir |
| 136 |
call_count = [0] |
| 137 |
|
| 138 |
# `safe_makedirs()` calls `os.mkdir()` for each level in the path. |
| 139 |
# For path "a/b", mkdir is called twice: once for "a", once for "b". |
| 140 |
def mkdir_with_race(p, mode): |
| 141 |
call_count[0] += 1 |
| 142 |
if call_count[0] == 1: |
| 143 |
original_mkdir(p, mode) |
| 144 |
else: |
| 145 |
raise FileExistsError(f"Directory exists: '{p}'") |
| 146 |
|
| 147 |
with unittest.mock.patch("os.mkdir", side_effect=mkdir_with_race): |
| 148 |
with self.assertRaises(FileExistsError): |
| 149 |
safe_makedirs(path, mode=0o755, exist_ok=False) |
| 150 |
|
| 151 |
def test_race_condition_exist_ok_true(self): |
| 152 |
path = os.path.join(self.base, "a", "b") |
| 153 |
|
| 154 |
original_mkdir = os.mkdir |
| 155 |
call_count = [0] |
| 156 |
|
| 157 |
def mkdir_with_race(p, mode): |
| 158 |
call_count[0] += 1 |
| 159 |
if call_count[0] == 1: |
| 160 |
original_mkdir(p, mode) |
| 161 |
else: |
| 162 |
# Simulate other thread creating the directory during the race. |
| 163 |
# The directory needs to exist for `exist_ok=True` to succeed. |
| 164 |
original_mkdir(p, mode) |
| 165 |
raise FileExistsError(f"Directory exists: '{p}'") |
| 166 |
|
| 167 |
with unittest.mock.patch("os.mkdir", side_effect=mkdir_with_race): |
| 168 |
safe_makedirs(path, mode=0o755, exist_ok=True) |
| 169 |
|
| 170 |
self.assertIs(os.path.isdir(path), True) |
| 171 |
|
| 172 |
|
| 173 |
class SafeJoinTests(unittest.TestCase): |
| 174 |
def test_base_path_ends_with_sep(self): |
| 175 |
drive, path = os.path.splitdrive(safe_join("/abc/", "abc")) |
| 176 |
self.assertEqual(path, "{0}abc{0}abc".format(os.path.sep)) |
| 177 |
|
| 178 |
def test_root_path(self): |
| 179 |
drive, path = os.path.splitdrive(safe_join("/", "path")) |
| 180 |
self.assertEqual( |
| 181 |
path, |
| 182 |
"{}path".format(os.path.sep), |
| 183 |
) |
| 184 |
|
| 185 |
drive, path = os.path.splitdrive(safe_join("/", "")) |
| 186 |
self.assertEqual( |
| 187 |
path, |
| 188 |
os.path.sep, |
| 189 |
) |
| 190 |
|
| 191 |
def test_parent_path(self): |
| 192 |
with self.assertRaises(SuspiciousFileOperation): |
| 193 |
safe_join("/abc/", "../def") |
| 194 |
|
| 195 |
|
| 196 |
class ToPathTests(unittest.TestCase): |
| 197 |
def test_to_path(self): |
| 198 |
for path in ("/tmp/some_file.txt", Path("/tmp/some_file.txt")): |
| 199 |
with self.subTest(path): |
| 200 |
self.assertEqual(to_path(path), Path("/tmp/some_file.txt")) |
| 201 |
|
| 202 |
def test_to_path_invalid_value(self): |
| 203 |
with self.assertRaises(TypeError): |
| 204 |
to_path(42) |