test(root_daemon): pingpong

This commit is contained in:
Houkime 2024-12-11 17:22:50 +00:00
parent 7b8e20e210
commit 32f8a803e8
3 changed files with 16 additions and 13 deletions

View file

@ -101,14 +101,14 @@ def _process_request(request: str, allowed_commands: str) -> str:
if request == command:
# explicitly only calling a _hardcoded_ command
# ever
# test mode made like this does not make it more dangerous too
# test mode made like this does not mae it more dangerous too
raise ValueError("Oh no")
if get_test_mode():
_spawn_shell(f'echo "{command}"')
else:
_spawn_shell(command)
else:
return "-1"
return "not permitted"
def _root_loop(socket: socket_module.socket, allowed_commands):
@ -120,7 +120,9 @@ def _root_loop(socket: socket_module.socket, allowed_commands):
try:
conn, addr = socket.accept()
except TimeoutError:
raise ValueError("nooo, there was a timeout!")
continue
pipe = conn.makefile("rw")
# We accept a single line per connection for simplicity and safety
line = pipe.readline()

View file

@ -1,4 +1,5 @@
from typing import List
from time import sleep
# from subprocess import check_output
from selfprivacy_api.root_daemon import SOCKET_PATH, socket_module
@ -6,10 +7,8 @@ from tests.test_common import get_test_mode
def call_root_function(cmd: List[str]) -> str:
if get_test_mode():
return "done"
else:
return _call_root_daemon(cmd)
assert isinstance(cmd, List)
return _call_root_daemon(cmd)
def _call_root_daemon(cmd: List[str]) -> str:
@ -19,7 +18,9 @@ def _call_root_daemon(cmd: List[str]) -> str:
def _write_to_daemon_socket(cmd: List[str]) -> str:
sock = socket_module.socket(socket_module.AF_UNIX, socket_module.SOCK_STREAM)
sock.connect(SOCKET_PATH)
sock.send(" ".join(cmd).encode("utf-8") + b"\n")
pipe = sock.makefile("rw")
payload = " ".join(cmd).encode("utf-8") + b"\n"
sock.send(payload)
pipe = sock.makefile("r")
line = pipe.readline()
sleep(2)
return line

View file

@ -58,10 +58,10 @@ def test_send_command():
finished = proc.poll()
assert finished is None
# thread = threading.Thread(target=start_root_daemon,args=[])
# thread.start()
answer = call_root_function("blabla")
assert answer == "done"
answer = call_root_function(["blabla"])
assert answer == "not permitted"
# confirm the loop
# answer = call_root_function(["blabla"])
# assert answer == "not permitted"
proc.kill()
# thread.join(timeout=1.0)