82 lines
2.2 KiB
Python
82 lines
2.2 KiB
Python
import urllib.request, json
|
|
|
|
def test(label, data):
|
|
req = urllib.request.Request(
|
|
'http://localhost:18001/execute',
|
|
data=json.dumps(data).encode(),
|
|
headers={'Content-Type': 'application/json'}
|
|
)
|
|
try:
|
|
resp = urllib.request.urlopen(req, timeout=15)
|
|
result = json.loads(resp.read())
|
|
print(f'[PASS] {label}: success={result.get("success")}, exit_code={result.get("exit_code")}, time={result.get("execution_time_ms",0):.1f}ms')
|
|
if result.get('stdout'):
|
|
print(f' stdout: {result["stdout"][:200]}')
|
|
if result.get('stderr'):
|
|
print(f' stderr: {result["stderr"][:200]}')
|
|
if result.get('error'):
|
|
print(f' error: {result["error"]}')
|
|
except Exception as e:
|
|
print(f'[FAIL] {label}: {e}')
|
|
|
|
print('=== Sandbox Execution Tests ===')
|
|
|
|
# Test 1: Python hello
|
|
test('Python Hello', {
|
|
'language': 'python',
|
|
'code': 'print("Hello from sandbox!")\nimport sys\nprint(f"Python {sys.version.split()[0]}")',
|
|
'timeout': 10
|
|
})
|
|
|
|
# Test 2: Python with stdin
|
|
test('Python stdin', {
|
|
'language': 'python',
|
|
'code': 'name = input()\nage = input()\nprint(f"Hello {name}, age {age}")',
|
|
'stdin': 'Alice\n25\n',
|
|
'timeout': 10
|
|
})
|
|
|
|
# Test 3: JavaScript
|
|
test('JavaScript', {
|
|
'language': 'javascript',
|
|
'code': 'console.log("Hello from Node.js!");\nconsole.log("Version: " + process.version);',
|
|
'timeout': 10
|
|
})
|
|
|
|
# Test 4: Timeout
|
|
test('Timeout', {
|
|
'language': 'python',
|
|
'code': 'import time\ntime.sleep(10)\nprint("Done")',
|
|
'timeout': 2
|
|
})
|
|
|
|
# Test 5: Compute
|
|
test('Fibonacci', {
|
|
'language': 'python',
|
|
'code': 'def fib(n):\n a,b=0,1\n for _ in range(n): a,b=b,a+b\n return a\nprint(f"fib(30)={fib(30)}")',
|
|
'timeout': 10
|
|
})
|
|
|
|
# Test 6: Error code
|
|
test('Python Error', {
|
|
'language': 'python',
|
|
'code': 'print(1/0)',
|
|
'timeout': 10
|
|
})
|
|
|
|
# Test 7: JS Error
|
|
test('JS Error', {
|
|
'language': 'javascript',
|
|
'code': 'throw new Error("test error");',
|
|
'timeout': 10
|
|
})
|
|
|
|
# Test 8: Large output
|
|
test('Large Output', {
|
|
'language': 'python',
|
|
'code': 'for i in range(100): print(f"Line {i}: Hello World" * 5)',
|
|
'timeout': 10
|
|
})
|
|
|
|
print('\nDone!')
|