|
| 1 | +# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | + |
| 3 | +# Copyright (C) 2022 igo95862 |
| 4 | + |
| 5 | +# This file is part of python-sdbus |
| 6 | + |
| 7 | +# This library is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public |
| 9 | +# License as published by the Free Software Foundation; either |
| 10 | +# version 2.1 of the License, or (at your option) any later version. |
| 11 | + |
| 12 | +# This library is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# Lesser General Public License for more details. |
| 16 | + |
| 17 | +# You should have received a copy of the GNU Lesser General Public |
| 18 | +# License along with this library; if not, write to the Free Software |
| 19 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +from asyncio.subprocess import DEVNULL, create_subprocess_exec |
| 23 | +from os import environ, kill |
| 24 | +from pathlib import Path |
| 25 | +from signal import SIGTERM |
| 26 | +from tempfile import TemporaryDirectory |
| 27 | +from typing import ClassVar |
| 28 | +from unittest import IsolatedAsyncioTestCase |
| 29 | + |
| 30 | +from sdbus import sd_bus_open_user, set_default_bus |
| 31 | + |
| 32 | +dbus_config = ''' |
| 33 | +<busconfig> |
| 34 | + <type>session</type> |
| 35 | + <pidfile>{pidfile_path}</pidfile> |
| 36 | + <auth>EXTERNAL</auth> |
| 37 | + <listen>unix:path={socket_path}</listen> |
| 38 | + <policy context="default"> |
| 39 | + <allow send_destination="*" eavesdrop="true"/> |
| 40 | + <allow eavesdrop="true"/> |
| 41 | + <allow own="*"/> |
| 42 | + </policy> |
| 43 | +</busconfig> |
| 44 | +''' |
| 45 | + |
| 46 | + |
| 47 | +class IsolatedDbusTestCase(IsolatedAsyncioTestCase): |
| 48 | + dbus_executable_name: ClassVar[str] = 'dbus-daemon' |
| 49 | + |
| 50 | + async def asyncSetUp(self) -> None: |
| 51 | + self.temp_dir = TemporaryDirectory() |
| 52 | + self.temp_dir_path = Path(self.temp_dir.name) |
| 53 | + |
| 54 | + self.dbus_socket_path = self.temp_dir_path / 'test_dbus.socket' |
| 55 | + self.pid_path = self.temp_dir_path / 'dbus.pid' |
| 56 | + |
| 57 | + self.dbus_config_file = self.temp_dir_path / 'dbus.config' |
| 58 | + |
| 59 | + with open(self.dbus_config_file, mode='x') as conf_file: |
| 60 | + conf_file.write(dbus_config.format( |
| 61 | + socket_path=self.dbus_socket_path, |
| 62 | + pidfile_path=self.pid_path)) |
| 63 | + |
| 64 | + self.dbus_process = await create_subprocess_exec( |
| 65 | + self.dbus_executable_name, |
| 66 | + '--config-file', self.dbus_config_file, |
| 67 | + '--fork', |
| 68 | + stdin=DEVNULL, |
| 69 | + ) |
| 70 | + error_code = await self.dbus_process.wait() |
| 71 | + if error_code != 0: |
| 72 | + raise ChildProcessError('Failed to start dbus daemon') |
| 73 | + |
| 74 | + self.old_session_bus_address = environ.get('DBUS_SESSION_BUS_ADDRESS') |
| 75 | + environ[ |
| 76 | + 'DBUS_SESSION_BUS_ADDRESS'] = f"unix:path={self.dbus_socket_path}" |
| 77 | + |
| 78 | + self.bus = sd_bus_open_user() |
| 79 | + set_default_bus(self.bus) |
| 80 | + |
| 81 | + async def asyncTearDown(self) -> None: |
| 82 | + with open(self.pid_path) as pid_file: |
| 83 | + dbus_pid = int(pid_file.read()) |
| 84 | + |
| 85 | + kill(dbus_pid, SIGTERM) |
| 86 | + self.temp_dir.cleanup() |
| 87 | + environ.pop('DBUS_SESSION_BUS_ADDRESS') |
| 88 | + if self.old_session_bus_address is not None: |
| 89 | + environ['DBUS_SESSION_BUS_ADDRESS'] = self.old_session_bus_address |
0 commit comments