forked from ChelseaKR/obligation-receipts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_paths.py
More file actions
64 lines (50 loc) · 2.04 KB
/
Copy pathtest_paths.py
File metadata and controls
64 lines (50 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from pathlib import Path
import pytest
from obligation_receipts.paths import (
BoundedPathError,
read_regular_file,
resolve_bounded_file,
)
def test_resolve_bounded_file_accepts_local_file(tmp_path: Path) -> None:
artifact = tmp_path / "evidence.json"
artifact.write_text("{}", encoding="utf-8")
assert resolve_bounded_file(tmp_path, "evidence.json") == artifact
def test_resolve_bounded_file_rejects_escape(tmp_path: Path) -> None:
outside = tmp_path.parent / "outside.json"
outside.write_text("{}", encoding="utf-8")
with pytest.raises(BoundedPathError, match="escapes"):
resolve_bounded_file(tmp_path, "../outside.json")
def test_resolve_bounded_file_rejects_absolute(tmp_path: Path) -> None:
with pytest.raises(BoundedPathError, match="relative"):
resolve_bounded_file(tmp_path, str((tmp_path / "x").resolve()))
@pytest.mark.parametrize(
"requested",
[
r"C:\evidence\artifact.json",
r"C:evidence\artifact.json",
r"\\server\share\artifact.json",
r"dir\artifact.json",
"https:artifact.json",
"https://example.invalid/artifact.json",
"dir//artifact.json",
"./artifact.json",
],
)
def test_resolve_bounded_file_rejects_windows_rooted_or_drive_paths(
tmp_path: Path,
requested: str,
) -> None:
with pytest.raises(BoundedPathError, match="portable and relative"):
resolve_bounded_file(tmp_path, requested)
@pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="FIFO creation is unavailable")
def test_regular_file_reader_rejects_fifo_without_blocking(tmp_path: Path) -> None:
fifo = tmp_path / "manifest.toml"
os.mkfifo(fifo)
with pytest.raises(BoundedPathError, match="regular file"):
read_regular_file(fifo, max_bytes=1024, no_follow=True)
def test_resolve_bounded_file_rejects_directory(tmp_path: Path) -> None:
directory = tmp_path / "directory"
directory.mkdir()
with pytest.raises(BoundedPathError, match="regular file"):
resolve_bounded_file(tmp_path, "directory")