1
+ import os
2
+ import pytest
3
+ from mcp_shell_server .shell_executor import ShellExecutor
4
+
5
+ @pytest .fixture
6
+ def executor ():
7
+ return ShellExecutor ()
8
+
9
+ @pytest .mark .asyncio
10
+ async def test_basic_command_execution (executor , monkeypatch ):
11
+ monkeypatch .setenv ("ALLOW_COMMANDS" , "echo,ls" )
12
+ result = await executor .execute (["echo" , "hello" ])
13
+ assert result ["stdout" ].strip () == "hello"
14
+ assert result ["status" ] == 0
15
+ assert result ["stderr" ] == ""
16
+ assert "execution_time" in result
17
+
18
+ @pytest .mark .asyncio
19
+ async def test_stdin_input (executor , monkeypatch ):
20
+ monkeypatch .setenv ("ALLOW_COMMANDS" , "cat" )
21
+ result = await executor .execute (["cat" ], stdin = "hello world" )
22
+ assert result ["stdout" ].strip () == "hello world"
23
+ assert result ["status" ] == 0
24
+
25
+ @pytest .mark .asyncio
26
+ async def test_command_not_allowed (executor , monkeypatch ):
27
+ monkeypatch .setenv ("ALLOW_COMMANDS" , "ls" )
28
+ result = await executor .execute (["rm" , "-rf" , "/" ])
29
+ assert result ["error" ] == "Command not allowed: rm"
30
+ assert result ["status" ] == 1
31
+
32
+ @pytest .mark .asyncio
33
+ async def test_empty_command (executor ):
34
+ result = await executor .execute ([])
35
+ assert result ["error" ] == "Empty command"
36
+ assert result ["status" ] == 1
37
+
38
+ @pytest .mark .asyncio
39
+ async def test_command_with_space_in_allow_commands (executor , monkeypatch ):
40
+ monkeypatch .setenv ("ALLOW_COMMANDS" , "ls, echo ,cat" )
41
+ result = await executor .execute (["echo" , "test" ])
42
+ assert result ["stdout" ].strip () == "test"
43
+ assert result ["status" ] == 0
44
+
45
+ @pytest .mark .asyncio
46
+ async def test_multiple_commands_with_operator (executor , monkeypatch ):
47
+ monkeypatch .setenv ("ALLOW_COMMANDS" , "echo,ls" )
48
+ result = await executor .execute (["echo" , "hello" , ";" , "ls" , "-l" ])
49
+ assert "Command not allowed: ls" in result ["error" ]
50
+ assert result ["status" ] == 1
51
+
52
+ @pytest .mark .asyncio
53
+ async def test_command_with_error_output (executor , monkeypatch ):
54
+ monkeypatch .setenv ("ALLOW_COMMANDS" , "ls" )
55
+ result = await executor .execute (["ls" , "/nonexistent" ])
56
+ assert result ["stderr" ] != ""
57
+ assert result ["status" ] != 0
0 commit comments