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: if request == command:
# explicitly only calling a _hardcoded_ command # explicitly only calling a _hardcoded_ command
# ever # 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") raise ValueError("Oh no")
if get_test_mode(): if get_test_mode():
_spawn_shell(f'echo "{command}"') _spawn_shell(f'echo "{command}"')
else: else:
_spawn_shell(command) _spawn_shell(command)
else: else:
return "-1" return "not permitted"
def _root_loop(socket: socket_module.socket, allowed_commands): def _root_loop(socket: socket_module.socket, allowed_commands):
@ -120,7 +120,9 @@ def _root_loop(socket: socket_module.socket, allowed_commands):
try: try:
conn, addr = socket.accept() conn, addr = socket.accept()
except TimeoutError: except TimeoutError:
raise ValueError("nooo, there was a timeout!")
continue continue
pipe = conn.makefile("rw") pipe = conn.makefile("rw")
# We accept a single line per connection for simplicity and safety # We accept a single line per connection for simplicity and safety
line = pipe.readline() line = pipe.readline()

View file

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

View file

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