|
1 | 1 | import pytest |
2 | 2 |
|
3 | | -from codegen.sdk.codebase.io.file_io import FileIO |
| 3 | +from codegen.sdk.codebase.io.file_io import BadWriteError, FileIO |
4 | 4 |
|
5 | 5 |
|
6 | 6 | @pytest.fixture |
@@ -61,3 +61,102 @@ def test_delete_file(file_io, tmp_path): |
61 | 61 |
|
62 | 62 | assert not test_file.exists() |
63 | 63 | assert test_file not in file_io.files |
| 64 | + |
| 65 | + |
| 66 | +def test_read_and_write_bounded(file_io, tmp_path): |
| 67 | + allowed_dir = tmp_path / "allowed" |
| 68 | + file_io.allowed_paths = [allowed_dir] |
| 69 | + |
| 70 | + allowed_file = allowed_dir / "test.txt" |
| 71 | + content = b"test content" |
| 72 | + |
| 73 | + file_io.write_bytes(allowed_file, content) |
| 74 | + assert file_io.read_bytes(allowed_file) == content |
| 75 | + |
| 76 | + with pytest.raises(BadWriteError) as exc_info: |
| 77 | + bad_file = tmp_path / "test.txt" |
| 78 | + file_io.write_bytes(bad_file, content) |
| 79 | + |
| 80 | + assert "is not within allowed paths" in str(exc_info.value) |
| 81 | + |
| 82 | + with pytest.raises(BadWriteError) as exc_info: |
| 83 | + bad_file_2 = allowed_dir / ".." / "test2.txt" |
| 84 | + file_io.write_bytes(bad_file_2, content) |
| 85 | + |
| 86 | + assert "is not within allowed paths" in str(exc_info.value) |
| 87 | + |
| 88 | + |
| 89 | +def test_read_bounded(file_io, tmp_path): |
| 90 | + allowed_dir = tmp_path / "allowed" |
| 91 | + allowed_dir.mkdir(exist_ok=True) |
| 92 | + file_io.allowed_paths = [allowed_dir] |
| 93 | + |
| 94 | + allowed_file = allowed_dir / "test.txt" |
| 95 | + content = b"test content" |
| 96 | + allowed_file.write_bytes(content) |
| 97 | + |
| 98 | + assert file_io.read_bytes(allowed_file) == content |
| 99 | + |
| 100 | + with pytest.raises(BadWriteError) as exc_info: |
| 101 | + bad_file = tmp_path / "test.txt" |
| 102 | + bad_file.write_bytes(content) |
| 103 | + file_io.read_bytes(bad_file) |
| 104 | + |
| 105 | + assert "is not within allowed paths" in str(exc_info.value) |
| 106 | + |
| 107 | + with pytest.raises(BadWriteError) as exc_info: |
| 108 | + bad_file_2 = allowed_dir / ".." / "test2.txt" |
| 109 | + bad_file_2.write_bytes(content) |
| 110 | + file_io.read_bytes(bad_file_2) |
| 111 | + |
| 112 | + assert "is not within allowed paths" in str(exc_info.value) |
| 113 | + |
| 114 | + |
| 115 | +def test_delete_file_bounded(file_io, tmp_path): |
| 116 | + allowed_dir = tmp_path / "allowed" |
| 117 | + allowed_dir.mkdir(exist_ok=True) |
| 118 | + file_io.allowed_paths = [allowed_dir] |
| 119 | + |
| 120 | + allowed_file = allowed_dir / "test.txt" |
| 121 | + allowed_file.write_bytes(b"test content") |
| 122 | + |
| 123 | + file_io.delete_file(allowed_file) |
| 124 | + |
| 125 | + with pytest.raises(BadWriteError) as exc_info: |
| 126 | + bad_file = tmp_path / "test.txt" |
| 127 | + bad_file.write_bytes(b"test content") |
| 128 | + file_io.delete_file(bad_file) |
| 129 | + |
| 130 | + assert "is not within allowed paths" in str(exc_info.value) |
| 131 | + |
| 132 | + with pytest.raises(BadWriteError) as exc_info: |
| 133 | + bad_file_2 = allowed_dir / ".." / "test2.txt" |
| 134 | + bad_file_2.write_bytes(b"test content") |
| 135 | + file_io.delete_file(bad_file_2) |
| 136 | + |
| 137 | + assert "is not within allowed paths" in str(exc_info.value) |
| 138 | + |
| 139 | + |
| 140 | +def test_file_exists_bounded(file_io, tmp_path): |
| 141 | + allowed_dir = tmp_path / "allowed" |
| 142 | + allowed_dir.mkdir(exist_ok=True) |
| 143 | + file_io.allowed_paths = [allowed_dir] |
| 144 | + |
| 145 | + allowed_file = allowed_dir / "test.txt" |
| 146 | + allowed_file.write_bytes(b"test content") |
| 147 | + |
| 148 | + assert file_io.file_exists(allowed_file) |
| 149 | + |
| 150 | + with pytest.raises(BadWriteError) as exc_info: |
| 151 | + bad_file = tmp_path / "test.txt" |
| 152 | + bad_file.write_bytes(b"test content") |
| 153 | + file_io.file_exists(bad_file) |
| 154 | + |
| 155 | + assert "is not within allowed paths" in str(exc_info.value) |
| 156 | + |
| 157 | + with pytest.raises(BadWriteError) as exc_info: |
| 158 | + bad_file_2 = allowed_dir / ".." / "test2.txt" |
| 159 | + bad_file_2.write_bytes(b"test content") |
| 160 | + file_io.file_exists(bad_file_2) |
| 161 | + |
| 162 | + assert "is not within allowed paths" in str(exc_info.value) |
0 commit comments