265 lines
6.6 KiB
Python
265 lines
6.6 KiB
Python
from backend.agent.servers import mcp_server_code_execution as server
|
||
|
||
|
||
# =========================================================
|
||
# BASIC TESTS (1–10)
|
||
# =========================================================
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 1. Erlaubter Code besteht Safety Check
|
||
# ---------------------------------------------------------
|
||
|
||
def test_check_code_safety_valid():
|
||
code = "print('hello')"
|
||
|
||
result = server.check_code_safety(code)
|
||
|
||
assert result is None
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 2. Blockierter Import wird erkannt
|
||
# ---------------------------------------------------------
|
||
|
||
def test_check_code_safety_blocked_import():
|
||
code = "import os"
|
||
|
||
result = server.check_code_safety(code)
|
||
|
||
assert "Blocked import" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 3. Blockierter Builtin wird erkannt
|
||
# ---------------------------------------------------------
|
||
|
||
def test_check_code_safety_blocked_builtin():
|
||
code = "eval('2+2')"
|
||
|
||
result = server.check_code_safety(code)
|
||
|
||
assert "Blocked builtin" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 4. analyse_structure erkennt Funktionen
|
||
# ---------------------------------------------------------
|
||
|
||
def test_analyse_structure_function():
|
||
code = """
|
||
def hello(name):
|
||
return name
|
||
"""
|
||
|
||
result = server.analyse_structure(code)
|
||
|
||
assert "def hello(name)" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 5. analyse_structure erkennt Klassen
|
||
# ---------------------------------------------------------
|
||
|
||
def test_analyse_structure_class():
|
||
code = """
|
||
class User:
|
||
def login(self):
|
||
pass
|
||
"""
|
||
|
||
result = server.analyse_structure(code)
|
||
|
||
assert "class User" in result
|
||
assert "method: login" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 6. lint_code erkennt Undefined Variable
|
||
# ---------------------------------------------------------
|
||
|
||
def test_lint_code_undefined_variable():
|
||
code = "print(x)"
|
||
|
||
result = server.lint_code(code)
|
||
|
||
assert "undefined name 'x'" in result.lower()
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 7. lint_code erkennt sauberen Code
|
||
# ---------------------------------------------------------
|
||
|
||
def test_lint_code_clean():
|
||
code = """
|
||
x = 1
|
||
print(x)
|
||
"""
|
||
|
||
result = server.lint_code(code)
|
||
|
||
assert "No issues found" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 8. python_code_validation validiert sicheren Code
|
||
# ---------------------------------------------------------
|
||
|
||
def test_python_code_validation_safe():
|
||
code = "print('safe')"
|
||
|
||
result = server.python_code_validation(code)
|
||
|
||
assert "can be executed" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 9. run_python_sandboxed führt Code aus
|
||
# ---------------------------------------------------------
|
||
|
||
def test_run_python_sandboxed_success():
|
||
code = "print('hello world')"
|
||
|
||
result = server.run_python_sandboxed(code)
|
||
|
||
assert "hello world" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 10. run_python_sandboxed ohne Output
|
||
# ---------------------------------------------------------
|
||
|
||
def test_run_python_sandboxed_no_output():
|
||
code = "x = 5"
|
||
|
||
result = server.run_python_sandboxed(code)
|
||
|
||
assert "no output" in result.lower()
|
||
|
||
|
||
# =========================================================
|
||
# EDGE CASE TESTS (11–20)
|
||
# =========================================================
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 11. Syntaxfehler erkennen
|
||
# ---------------------------------------------------------
|
||
|
||
def test_check_code_safety_syntax_error():
|
||
code = "def broken("
|
||
|
||
result = server.check_code_safety(code)
|
||
|
||
assert "SyntaxError" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 12. ImportFrom blockieren
|
||
# ---------------------------------------------------------
|
||
|
||
def test_check_code_safety_import_from():
|
||
code = "from os import path"
|
||
|
||
result = server.check_code_safety(code)
|
||
|
||
assert "Blocked import" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 13. Gefährliche Path-Sequenzen erkennen
|
||
# ---------------------------------------------------------
|
||
|
||
def test_check_code_safety_path_traversal():
|
||
code = "print('../etc/passwd')"
|
||
|
||
result = server.check_code_safety(code)
|
||
|
||
assert "Suspect path sequence" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 14. __import__ erkennen
|
||
# ---------------------------------------------------------
|
||
|
||
def test_check_code_safety_import_escape():
|
||
code = "__import__('os')"
|
||
|
||
result = server.check_code_safety(code)
|
||
|
||
assert "Blocked" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 15. subprocess Escape erkennen
|
||
# ---------------------------------------------------------
|
||
|
||
def test_check_code_safety_subprocess_escape():
|
||
code = "subprocess.run(['ls'])"
|
||
|
||
result = server.check_code_safety(code)
|
||
|
||
assert "Suspect path sequence" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 16. Endlosschleife Timeout
|
||
# ---------------------------------------------------------
|
||
|
||
def test_run_python_sandboxed_timeout():
|
||
code = """
|
||
while True:
|
||
pass
|
||
"""
|
||
|
||
result = server.run_python_sandboxed(code)
|
||
|
||
assert "time limit" in result.lower()
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 17. Sehr großer Output wird gekürzt
|
||
# ---------------------------------------------------------
|
||
|
||
def test_run_python_sandboxed_large_output():
|
||
code = "print('A' * 10000)"
|
||
|
||
result = server.run_python_sandboxed(code)
|
||
|
||
assert "truncated" in result.lower()
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 18. Unicode Output funktioniert
|
||
# ---------------------------------------------------------
|
||
|
||
def test_run_python_sandboxed_unicode():
|
||
code = "print('🔥 Grüezi 世界')"
|
||
|
||
result = server.run_python_sandboxed(code)
|
||
|
||
assert "🔥 Grüezi 世界" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 19. analyse_structure bei leerem Code
|
||
# ---------------------------------------------------------
|
||
|
||
def test_analyse_structure_empty():
|
||
code = ""
|
||
|
||
result = server.analyse_structure(code)
|
||
|
||
assert "No top-level imports" in result
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# 20. Sandbox behandelt Runtime Errors
|
||
# ---------------------------------------------------------
|
||
|
||
def test_run_python_sandboxed_runtime_error():
|
||
code = "1 / 0"
|
||
|
||
result = server.run_python_sandboxed(code)
|
||
|
||
assert "ZeroDivisionError" in result |