-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_storage.py
More file actions
361 lines (265 loc) · 12.6 KB
/
Copy pathtest_storage.py
File metadata and controls
361 lines (265 loc) · 12.6 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# Unit tests for NetFS storage operations
# ABOUTME: Tests for file/directory operations with path security and validation
import pytest
from netfs.common.config import StorageConfig
from netfs.common.errors import (
DirectoryNotEmptyError,
FileNotFoundError,
FileTooLargeError,
InvalidPathError,
PathAlreadyExistsError,
)
from netfs.server.storage import StorageManager
@pytest.fixture
def temp_storage(tmp_path):
"""Create temporary storage directory."""
storage_dir = tmp_path / "storage"
storage_dir.mkdir()
return storage_dir
@pytest.fixture
def storage_manager(temp_storage):
"""Create StorageManager with temp storage."""
config = StorageConfig(
root_path=str(temp_storage),
max_file_size_mb=1, # 1MB for tests
)
return StorageManager(config)
class TestResolvePath:
"""Test path resolution and security."""
def test_resolve_absolute_path(self, storage_manager, temp_storage):
"""Should resolve absolute path within storage root."""
resolved = storage_manager.resolve_path("/documents/file.txt")
expected = temp_storage / "documents" / "file.txt"
assert resolved == expected
def test_resolve_relative_path(self, storage_manager, temp_storage):
"""Should resolve relative path."""
resolved = storage_manager.resolve_path("documents/file.txt")
expected = temp_storage / "documents" / "file.txt"
assert resolved == expected
def test_resolve_root_path(self, storage_manager, temp_storage):
"""Should resolve root path."""
resolved = storage_manager.resolve_path("/")
assert resolved == temp_storage
def test_resolve_current_dir(self, storage_manager, temp_storage):
"""Should resolve current directory."""
resolved = storage_manager.resolve_path(".")
assert resolved == temp_storage
def test_reject_path_traversal_dotdot(self, storage_manager):
"""Should reject path traversal with .."""
with pytest.raises(InvalidPathError):
storage_manager.resolve_path("../etc/passwd")
def test_absolute_path_within_storage(self, storage_manager, temp_storage):
"""Should treat absolute paths as relative to storage root."""
# In our system, /etc/passwd means storage/etc/passwd, not system /etc/passwd
resolved = storage_manager.resolve_path("/etc/passwd")
expected = temp_storage / "etc" / "passwd"
assert resolved == expected
def test_reject_path_traversal_nested(self, storage_manager):
"""Should reject nested path traversal."""
with pytest.raises(InvalidPathError):
storage_manager.resolve_path("docs/../../etc/passwd")
def test_allow_dotdot_within_storage(self, storage_manager, temp_storage):
"""Should allow .. that stays within storage."""
resolved = storage_manager.resolve_path("docs/../files/test.txt")
expected = temp_storage / "files" / "test.txt"
assert resolved == expected
class TestExists:
"""Test path existence checking."""
def test_exists_file(self, storage_manager, temp_storage):
"""Should return True for existing file."""
test_file = temp_storage / "test.txt"
test_file.write_text("content")
assert storage_manager.exists("/test.txt") is True
def test_exists_directory(self, storage_manager, temp_storage):
"""Should return True for existing directory."""
test_dir = temp_storage / "testdir"
test_dir.mkdir()
assert storage_manager.exists("/testdir") is True
def test_not_exists(self, storage_manager):
"""Should return False for non-existent path."""
assert storage_manager.exists("/missing.txt") is False
class TestMakeDir:
"""Test directory creation."""
def test_make_dir_simple(self, storage_manager, temp_storage):
"""Should create directory."""
storage_manager.make_dir("/newdir")
created_dir = temp_storage / "newdir"
assert created_dir.exists()
assert created_dir.is_dir()
def test_make_dir_nested(self, storage_manager, temp_storage):
"""Should create nested directory with parents."""
storage_manager.make_dir("/parent/child/grandchild")
created_dir = temp_storage / "parent" / "child" / "grandchild"
assert created_dir.exists()
def test_make_dir_already_exists(self, storage_manager, temp_storage):
"""Should raise error if directory exists."""
test_dir = temp_storage / "existing"
test_dir.mkdir()
with pytest.raises(PathAlreadyExistsError):
storage_manager.make_dir("/existing")
def test_make_dir_file_exists(self, storage_manager, temp_storage):
"""Should raise error if file exists at path."""
test_file = temp_storage / "file.txt"
test_file.write_text("content")
with pytest.raises(PathAlreadyExistsError):
storage_manager.make_dir("/file.txt")
class TestRemoveDir:
"""Test directory removal."""
def test_remove_empty_dir(self, storage_manager, temp_storage):
"""Should remove empty directory."""
test_dir = temp_storage / "emptydir"
test_dir.mkdir()
storage_manager.remove_dir("/emptydir")
assert not test_dir.exists()
def test_remove_non_empty_dir_without_recursive(self, storage_manager, temp_storage):
"""Should raise error when removing non-empty directory without recursive flag."""
test_dir = temp_storage / "nonempty"
test_dir.mkdir()
(test_dir / "file.txt").write_text("content")
with pytest.raises(DirectoryNotEmptyError):
storage_manager.remove_dir("/nonempty", recursive=False)
def test_remove_non_empty_dir_recursive(self, storage_manager, temp_storage):
"""Should remove non-empty directory with recursive flag."""
test_dir = temp_storage / "nonempty"
test_dir.mkdir()
(test_dir / "file1.txt").write_text("content1")
subdir = test_dir / "subdir"
subdir.mkdir()
(subdir / "file2.txt").write_text("content2")
storage_manager.remove_dir("/nonempty", recursive=True)
assert not test_dir.exists()
def test_remove_dir_not_exists(self, storage_manager):
"""Should raise error if directory doesn't exist."""
with pytest.raises(FileNotFoundError):
storage_manager.remove_dir("/missing")
class TestListDir:
"""Test directory listing."""
def test_list_empty_dir(self, storage_manager, temp_storage):
"""Should list empty directory."""
test_dir = temp_storage / "emptydir"
test_dir.mkdir()
entries = storage_manager.list_dir("/emptydir")
assert entries == []
def test_list_dir_with_files(self, storage_manager, temp_storage):
"""Should list files in directory."""
test_dir = temp_storage / "testdir"
test_dir.mkdir()
(test_dir / "file1.txt").write_text("content1")
(test_dir / "file2.txt").write_text("content2")
entries = storage_manager.list_dir("/testdir")
assert len(entries) == 2
names = [e.name for e in entries]
assert "file1.txt" in names
assert "file2.txt" in names
def test_list_dir_with_mixed_content(self, storage_manager, temp_storage):
"""Should list both files and directories."""
test_dir = temp_storage / "testdir"
test_dir.mkdir()
(test_dir / "file.txt").write_text("content")
(test_dir / "subdir").mkdir()
entries = storage_manager.list_dir("/testdir")
assert len(entries) == 2
file_entry = next(e for e in entries if e.name == "file.txt")
dir_entry = next(e for e in entries if e.name == "subdir")
assert file_entry.is_dir is False
assert dir_entry.is_dir is True
def test_list_dir_not_exists(self, storage_manager):
"""Should raise error if directory doesn't exist."""
with pytest.raises(FileNotFoundError):
storage_manager.list_dir("/missing")
def test_list_file_not_dir(self, storage_manager, temp_storage):
"""Should raise error if path is a file, not directory."""
test_file = temp_storage / "file.txt"
test_file.write_text("content")
with pytest.raises(FileNotFoundError):
storage_manager.list_dir("/file.txt")
class TestReadFile:
"""Test file reading."""
def test_read_text_file(self, storage_manager, temp_storage):
"""Should read text file content."""
test_file = temp_storage / "test.txt"
content = "Hello, World!"
test_file.write_text(content)
result = storage_manager.read_file("/test.txt")
assert result["content"] == content
assert result["is_binary"] is False
def test_read_binary_file(self, storage_manager, temp_storage):
"""Should read binary file as base64."""
test_file = temp_storage / "test.bin"
binary_data = bytes([0, 1, 2, 3, 255])
test_file.write_bytes(binary_data)
result = storage_manager.read_file("/test.bin")
assert result["is_binary"] is True
# Decode base64 and verify
import base64
decoded = base64.b64decode(result["content"])
assert decoded == binary_data
def test_read_file_not_exists(self, storage_manager):
"""Should raise error if file doesn't exist."""
with pytest.raises(FileNotFoundError):
storage_manager.read_file("/missing.txt")
def test_read_directory_not_file(self, storage_manager, temp_storage):
"""Should raise error if path is a directory."""
test_dir = temp_storage / "testdir"
test_dir.mkdir()
with pytest.raises(FileNotFoundError):
storage_manager.read_file("/testdir")
class TestWriteFile:
"""Test file writing."""
def test_write_new_file(self, storage_manager, temp_storage):
"""Should create and write new file."""
storage_manager.write_file("/new.txt", "Hello, World!")
created_file = temp_storage / "new.txt"
assert created_file.exists()
assert created_file.read_text() == "Hello, World!"
def test_overwrite_existing_file(self, storage_manager, temp_storage):
"""Should overwrite existing file."""
test_file = temp_storage / "existing.txt"
test_file.write_text("old content")
storage_manager.write_file("/existing.txt", "new content")
assert test_file.read_text() == "new content"
def test_write_binary_file(self, storage_manager, temp_storage):
"""Should write binary file from base64."""
import base64
binary_data = bytes([0, 1, 2, 3, 255])
base64_content = base64.b64encode(binary_data).decode("utf-8")
storage_manager.write_file("/test.bin", base64_content, is_binary=True)
created_file = temp_storage / "test.bin"
assert created_file.read_bytes() == binary_data
def test_write_file_creates_parent_dirs(self, storage_manager, temp_storage):
"""Should create parent directories if they don't exist."""
storage_manager.write_file("/parent/child/file.txt", "content")
created_file = temp_storage / "parent" / "child" / "file.txt"
assert created_file.exists()
assert created_file.read_text() == "content"
def test_write_file_too_large(self, storage_manager):
"""Should raise error if file exceeds size limit."""
large_content = "x" * (2 * 1024 * 1024) # 2MB (limit is 1MB in fixture)
with pytest.raises(FileTooLargeError):
storage_manager.write_file("/large.txt", large_content)
class TestStat:
"""Test file/directory stat."""
def test_stat_file(self, storage_manager, temp_storage):
"""Should return file statistics."""
test_file = temp_storage / "test.txt"
content = "Hello, World!"
test_file.write_text(content)
stat = storage_manager.stat("/test.txt")
assert stat.name == "test.txt"
assert stat.path == "/test.txt"
assert stat.is_dir is False
assert stat.size == len(content)
assert stat.modified is not None
def test_stat_directory(self, storage_manager, temp_storage):
"""Should return directory statistics."""
test_dir = temp_storage / "testdir"
test_dir.mkdir()
stat = storage_manager.stat("/testdir")
assert stat.name == "testdir"
assert stat.path == "/testdir"
assert stat.is_dir is True
assert stat.modified is not None
def test_stat_not_exists(self, storage_manager):
"""Should raise error if path doesn't exist."""
with pytest.raises(FileNotFoundError):
storage_manager.stat("/missing.txt")