|
1 | 1 | import os |
| 2 | +from pathlib import PosixPath |
2 | 3 | import sys |
3 | 4 |
|
| 5 | +from hypothesis import given |
| 6 | +from hypothesis.strategies import text |
4 | 7 | import pytest |
5 | 8 |
|
6 | 9 | from ddtrace.appsec._iast._taint_tracking import OriginType |
@@ -176,10 +179,130 @@ def test_ospathjoin_last_slash_tainted(): |
176 | 179 | assert get_tainted_ranges(res) == [TaintRange(0, 4, Source("test_ospath", "/bar", OriginType.PARAMETER))] |
177 | 180 |
|
178 | 181 |
|
| 182 | +@given(text()) |
| 183 | +def test_ospathbasename_no_exceptions(string_): |
| 184 | + assert os.path.basename(PosixPath(string_)) == ospathbasename_aspect(PosixPath(string_)) |
| 185 | + assert os.path.basename(string_) == ospathbasename_aspect(string_) |
| 186 | + |
| 187 | + |
| 188 | +def test_ospathbasename_wrong_arg(): |
| 189 | + with pytest.raises(TypeError): |
| 190 | + _ = os.path.basename(42, "333") |
| 191 | + |
| 192 | + with pytest.raises(TypeError): |
| 193 | + _ = os.path.basename(42) |
| 194 | + |
| 195 | + with pytest.raises(TypeError): |
| 196 | + _ = os.path.basename(["a", "b", "c"]) |
| 197 | + |
| 198 | + with pytest.raises(TypeError): |
| 199 | + _ = ospathbasename_aspect(42, "333") |
| 200 | + |
| 201 | + with pytest.raises(TypeError): |
| 202 | + _ = ospathbasename_aspect(42) |
| 203 | + |
| 204 | + with pytest.raises(TypeError): |
| 205 | + _ = ospathbasename_aspect(["a", "b", "c"]) |
| 206 | + |
| 207 | + |
| 208 | +@given(text()) |
| 209 | +def test_ospathdirname_no_exceptions(string_): |
| 210 | + assert os.path.dirname(PosixPath(string_)) == ospathdirname_aspect(PosixPath(string_)) |
| 211 | + assert os.path.dirname(string_) == ospathdirname_aspect(string_) |
| 212 | + |
| 213 | + |
| 214 | +def test_ospathdirname_wrong_arg(): |
| 215 | + with pytest.raises(TypeError): |
| 216 | + _ = os.path.dirname(42, "333") |
| 217 | + |
| 218 | + with pytest.raises(TypeError): |
| 219 | + _ = os.path.dirname(42) |
| 220 | + |
| 221 | + with pytest.raises(TypeError): |
| 222 | + _ = os.path.dirname(["a", "b", "c"]) |
| 223 | + |
| 224 | + with pytest.raises(TypeError): |
| 225 | + _ = ospathdirname_aspect(42, "333") |
| 226 | + |
| 227 | + with pytest.raises(TypeError): |
| 228 | + _ = ospathdirname_aspect(42) |
| 229 | + |
| 230 | + with pytest.raises(TypeError): |
| 231 | + _ = ospathdirname_aspect(["a", "b", "c"]) |
| 232 | + |
| 233 | + |
| 234 | +@given(text()) |
| 235 | +def test_ospathjoin_no_exceptions(string_): |
| 236 | + assert os.path.join(PosixPath(string_), string_) == ospathjoin_aspect(PosixPath(string_), string_) |
| 237 | + assert os.path.join(PosixPath(string_)) == ospathjoin_aspect(PosixPath(string_)) |
| 238 | + assert os.path.join(string_) == ospathjoin_aspect(string_) |
| 239 | + |
| 240 | + |
179 | 241 | def test_ospathjoin_wrong_arg(): |
| 242 | + def iterator_with_exception(): |
| 243 | + for i in range(5): |
| 244 | + yield i |
| 245 | + raise ValueError("there is a problem in iterator_with_exception") |
| 246 | + |
| 247 | + with pytest.raises(TypeError): |
| 248 | + _ = os.path.join("root", 42, "foobar") |
| 249 | + |
| 250 | + with pytest.raises(TypeError): |
| 251 | + _ = os.path.join(("a", "b")) |
| 252 | + |
| 253 | + with pytest.raises(TypeError): |
| 254 | + _ = os.path.join([PosixPath("a"), PosixPath("b")]) |
| 255 | + |
| 256 | + with pytest.raises(ValueError): |
| 257 | + _ = os.path.join("".join(iterator_with_exception())) |
| 258 | + |
| 259 | + with pytest.raises(TypeError): |
| 260 | + _ = os.path.join([PosixPath("a"), PosixPath("b")]) |
| 261 | + |
180 | 262 | with pytest.raises(TypeError): |
181 | 263 | _ = ospathjoin_aspect("root", 42, "foobar") |
182 | 264 |
|
| 265 | + with pytest.raises(TypeError): |
| 266 | + _ = ospathjoin_aspect(("a", "b")) |
| 267 | + |
| 268 | + with pytest.raises(TypeError): |
| 269 | + _ = ospathjoin_aspect([PosixPath("a"), PosixPath("b")]) |
| 270 | + |
| 271 | + with pytest.raises(ValueError): |
| 272 | + _ = ospathjoin_aspect("".join(iterator_with_exception())) |
| 273 | + |
| 274 | + |
| 275 | +@given(text()) |
| 276 | +def test_ospathnormcase_no_exceptions(string_): |
| 277 | + assert os.path.normcase(PosixPath(string_)) == ospathnormcase_aspect(PosixPath(string_)) |
| 278 | + assert os.path.normcase(string_) == ospathnormcase_aspect(string_) |
| 279 | + |
| 280 | + |
| 281 | +@given(text()) |
| 282 | +def test_ospathsplit_no_exceptions(string_): |
| 283 | + assert os.path.split(PosixPath(string_)) == ospathsplit_aspect(PosixPath(string_)) |
| 284 | + assert os.path.split(string_) == ospathsplit_aspect(string_) |
| 285 | + |
| 286 | + |
| 287 | +@pytest.mark.skipif(sys.version_info < (3, 12) and os.name != "nt", reason="Requires Python 3.12 or Windows") |
| 288 | +@given(text()) |
| 289 | +def test_ospathsplitdrive_no_exceptions(string_): |
| 290 | + assert os.path.splitdrive(PosixPath(string_)) == ospathsplitdrive_aspect(PosixPath(string_)) |
| 291 | + assert os.path.splitdrive(string_) == ospathsplitdrive_aspect(string_) |
| 292 | + |
| 293 | + |
| 294 | +@given(text()) |
| 295 | +def test_ospathsplitext_no_exceptions(string_): |
| 296 | + assert os.path.splitext(PosixPath(string_)) == ospathsplitext_aspect(PosixPath(string_)) |
| 297 | + assert os.path.splitext(string_) == ospathsplitext_aspect(string_) |
| 298 | + |
| 299 | + |
| 300 | +@pytest.mark.skipif(sys.version_info < (3, 12), reason="Requires Python 3.12") |
| 301 | +@given(text()) |
| 302 | +def test_ospathsplitroot_no_exceptions(string_): |
| 303 | + assert os.path.splitroot(PosixPath(string_)) == ospathsplitroot_aspect(PosixPath(string_)) |
| 304 | + assert os.path.splitroot(string_) == ospathsplitroot_aspect(string_) |
| 305 | + |
183 | 306 |
|
184 | 307 | def test_ospathjoin_bytes_nottainted(): |
185 | 308 | res = ospathjoin_aspect(b"nottainted", b"alsonottainted") |
@@ -255,11 +378,6 @@ def test_ospathbasename_nottainted(): |
255 | 378 | assert not get_tainted_ranges(res) |
256 | 379 |
|
257 | 380 |
|
258 | | -def test_ospathbasename_wrong_arg(): |
259 | | - with pytest.raises(TypeError): |
260 | | - _ = ospathbasename_aspect(42) |
261 | | - |
262 | | - |
263 | 381 | def test_ospathbasename_bytes_tainted(): |
264 | 382 | tainted_foobarbaz = taint_pyobject( |
265 | 383 | pyobject=b"/foo/bar/baz", |
@@ -403,11 +521,6 @@ def test_ospathdirname_nottainted(): |
403 | 521 | assert not get_tainted_ranges(res) |
404 | 522 |
|
405 | 523 |
|
406 | | -def test_ospathdirname_wrong_arg(): |
407 | | - with pytest.raises(TypeError): |
408 | | - _ = ospathdirname_aspect(42) |
409 | | - |
410 | | - |
411 | 524 | def test_ospathdirname_bytes_tainted(): |
412 | 525 | tainted_foobarbaz = taint_pyobject( |
413 | 526 | pyobject=b"/foo/bar/baz", |
|
0 commit comments