initial commit heh
This commit is contained in:
78 files changed
+11181
No files matched your search
@@ -0,0 +1,94 @@
|
||||
"""Unit tests for the Safe Code Execution Sandbox extension."""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
import pytest
|
||||
|
||||
from extensions.sandbox.base import ExecutionResult
|
||||
from extensions.sandbox.manager import SandboxManager
|
||||
from extensions.sandbox.tools import (
|
||||
code_execute,
|
||||
sandbox_file_read,
|
||||
sandbox_file_write,
|
||||
sandbox_reset,
|
||||
sandbox_run_command,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_code_execute_python_arithmetic():
|
||||
code = "total = sum(range(1, 101))\nprint(f'Sum: {total}')"
|
||||
res = await code_execute(code=code, language="python")
|
||||
assert "Sum: 5050" in res
|
||||
assert "Sandbox:" in res
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_code_execute_syntax_error():
|
||||
bad_code = "def invalid_syntax(:\n pass"
|
||||
res = await code_execute(code=bad_code, language="python")
|
||||
assert "SyntaxError" in res or "Ошибка" in res
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_code_execute_timeout():
|
||||
# Loop that takes longer than 1 second
|
||||
infinite_code = "import time\ntime.sleep(5)"
|
||||
res = await code_execute(code=infinite_code, language="python", timeout=1)
|
||||
assert "Превышен лимит времени выполнения" in res or "таймаут" in res.lower()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sandbox_isolation_cannot_access_host():
|
||||
"""Verify that host home directory and secrets (.env) are completely invisible in sandbox."""
|
||||
check_code = """
|
||||
import os
|
||||
print("HOME_EXISTS:", os.path.exists("/home"))
|
||||
print("ENV_EXISTS:", os.path.exists("/home/timka/code/homeagent/.env"))
|
||||
"""
|
||||
res = await code_execute(code=check_code, language="python")
|
||||
assert "HOME_EXISTS: False" in res
|
||||
assert "ENV_EXISTS: False" in res
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sandbox_file_write_and_read():
|
||||
await sandbox_reset()
|
||||
write_res = await sandbox_file_write("sample.txt", "Привет из изолированной песочницы!")
|
||||
assert "успешно создан" in write_res
|
||||
|
||||
read_res = await sandbox_file_read("sample.txt")
|
||||
assert "Привет из изолированной песочницы!" in read_res
|
||||
|
||||
# Also test that Python script can read the written file
|
||||
py_code = """
|
||||
with open("sample.txt", "r", encoding="utf-8") as f:
|
||||
print("Content:", f.read())
|
||||
"""
|
||||
run_res = await code_execute(code=py_code, language="python")
|
||||
assert "Content: Привет из изолированной песочницы!" in run_res
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sandbox_path_traversal_blocked():
|
||||
write_res = await sandbox_file_write("../../evil.sh", "echo evil")
|
||||
assert "Access denied" in write_res or "Ошибка" in write_res
|
||||
|
||||
read_res = await sandbox_file_read("../../.env")
|
||||
assert "Access denied" in read_res or "Ошибка" in read_res
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sandbox_run_command():
|
||||
res = await sandbox_run_command("echo 'hello sandbox' | tr 'a-z' 'A-Z'")
|
||||
assert "HELLO SANDBOX" in res
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sandbox_disabled():
|
||||
with patch("extensions.sandbox.tools.get_settings") as mock_settings:
|
||||
cfg = MagicMock()
|
||||
cfg.SANDBOX_ENABLED = False
|
||||
mock_settings.return_value = cfg
|
||||
|
||||
res = await code_execute("print(1)")
|
||||
assert "отключена в настройках" in res
|
||||
Reference in new issue
Block a user