Spaces:
Rsnarsna
/
Sleeping

File size: 3,142 Bytes
c857507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9a26c7
 
 
 
 
 
 
 
 
c857507
 
 
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
"""
Unit tests for custom script sandbox executor (Task 3).
Verifies AST static safety inspection, allowlisted execution, entrypoints, and error handling.
"""

import sys
import unittest
from pathlib import Path

# Add backend directory to sys.path
backend_dir = Path(__file__).resolve().parent.parent
if str(backend_dir) not in sys.path:
    sys.path.insert(0, str(backend_dir))

from language_core.executor.custom_script_executor import (
    WorkflowExecutionError,
    execute_custom_script_node,
)


class TestCustomScriptExecutor(unittest.TestCase):

    def test_basic_expression_execution(self):
        res = execute_custom_script_node(
            node_inputs={"x": 21},
            params={"code": "result = x * 2"},
            node_id="node_custom_1",
            label="Multiplier",
        )
        self.assertEqual(res["result"], 42)
        self.assertEqual(res["out"], 42)

    def test_entrypoint_function_execution(self):
        code = """
def run(a, b):
    return {"sum": a + b, "diff": a - b}
"""
        res = execute_custom_script_node(
            node_inputs={"a": 10, "b": 3},
            params={"code": code},
            node_id="node_custom_2",
            label="Adder",
        )
        self.assertEqual(res["sum"], 13)
        self.assertEqual(res["diff"], 7)

    def test_blocks_os_import(self):
        with self.assertRaises(WorkflowExecutionError) as ctx:
            execute_custom_script_node(
                node_inputs={},
                params={"code": "import os\nos.system('ls')"},
                node_id="node_bad_1",
            )
        self.assertIn("[ERR-SECURITY-UNSAFE-IMPORT]", str(ctx.exception))

    def test_blocks_eval_call(self):
        with self.assertRaises(WorkflowExecutionError) as ctx:
            execute_custom_script_node(
                node_inputs={},
                params={"code": "result = eval('1 + 1')"},
                node_id="node_bad_2",
            )
        self.assertIn("[ERR-SECURITY-DANGEROUS-CALL]", str(ctx.exception))

    def test_blocks_dunder_inspection(self):
        with self.assertRaises(WorkflowExecutionError) as ctx:
            execute_custom_script_node(
                node_inputs={},
                params={"code": "x = ().__class__.__bases__"},
                node_id="node_bad_3",
            )
        self.assertIn("[ERR-SECURITY-FORBIDDEN-ATTR]", str(ctx.exception))

    def test_syntax_error_reported(self):
        with self.assertRaises(WorkflowExecutionError) as ctx:
            execute_custom_script_node(
                node_inputs={},
                params={"code": "def broken(:"},
                node_id="node_bad_4",
            )
        self.assertIn("[ERR-SYNTAX]", str(ctx.exception))

    def test_timeout_enforcement(self):
        with self.assertRaises(WorkflowExecutionError) as ctx:
            execute_custom_script_node(
                node_inputs={},
                params={"code": "while True: pass", "timeout_seconds": 0.1},
                node_id="node_timeout",
            )
        self.assertIn("[ERR-TIMEOUT]", str(ctx.exception))


if __name__ == "__main__":
    unittest.main()