This commit is contained in:
2024-11-29 18:15:30 +00:00
parent 40aade2d8e
commit bc9415586e
5298 changed files with 1938676 additions and 80 deletions

View File

@ -0,0 +1,81 @@
from tempfile import TemporaryFile
import threading
import pytest
from jeepney import (
DBusAddress, HeaderFields, message_bus, MessageType, new_error,
new_method_return,
)
from jeepney.io.threading import open_dbus_connection, DBusRouter, Proxy
@pytest.fixture()
def respond_with_fd():
name = "io.gitlab.takluyver.jeepney.tests.respond_with_fd"
addr = DBusAddress(bus_name=name, object_path='/')
with open_dbus_connection(bus='SESSION', enable_fds=True) as conn:
with DBusRouter(conn) as router:
status, = Proxy(message_bus, router).RequestName(name)
assert status == 1 # DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
def _reply_once():
while True:
msg = conn.receive()
if msg.header.message_type is MessageType.method_call:
if msg.header.fields[HeaderFields.member] == 'GetFD':
with TemporaryFile('w+') as tf:
tf.write('readme')
tf.seek(0)
rep = new_method_return(msg, 'h', (tf,))
conn.send(rep)
return
else:
conn.send(new_error(msg, 'NoMethod'))
reply_thread = threading.Thread(target=_reply_once, daemon=True)
reply_thread.start()
yield addr
reply_thread.join()
@pytest.fixture()
def read_from_fd():
name = "io.gitlab.takluyver.jeepney.tests.read_from_fd"
addr = DBusAddress(bus_name=name, object_path='/')
with open_dbus_connection(bus='SESSION', enable_fds=True) as conn:
with DBusRouter(conn) as router:
status, = Proxy(message_bus, router).RequestName(name)
assert status == 1 # DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
def _reply_once():
while True:
msg = conn.receive()
if msg.header.message_type is MessageType.method_call:
if msg.header.fields[HeaderFields.member] == 'ReadFD':
with msg.body[0].to_file('rb') as f:
f.seek(0)
b = f.read()
conn.send(new_method_return(msg, 'ay', (b,)))
return
else:
conn.send(new_error(msg, 'NoMethod'))
reply_thread = threading.Thread(target=_reply_once, daemon=True)
reply_thread.start()
yield addr
reply_thread.join()
@pytest.fixture()
def temp_file_and_contents():
data = b'abc123'
with TemporaryFile('w+b') as tf:
tf.write(data)
tf.flush()
tf.seek(0)
yield tf, data

View File

@ -0,0 +1,91 @@
import asyncio
import async_timeout
import pytest
import pytest_asyncio
from jeepney import DBusAddress, new_method_call
from jeepney.bus_messages import message_bus, MatchRule
from jeepney.io.asyncio import (
open_dbus_connection, open_dbus_router, Proxy
)
from .utils import have_session_bus
pytestmark = [
pytest.mark.asyncio,
pytest.mark.skipif(
not have_session_bus, reason="Tests require DBus session bus"
),
]
bus_peer = DBusAddress(
bus_name='org.freedesktop.DBus',
object_path='/org/freedesktop/DBus',
interface='org.freedesktop.DBus.Peer'
)
@pytest_asyncio.fixture()
async def connection():
async with (await open_dbus_connection(bus='SESSION')) as conn:
yield conn
async def test_connect(connection):
assert connection.unique_name.startswith(':')
@pytest_asyncio.fixture()
async def router():
async with open_dbus_router(bus='SESSION') as router:
yield router
async def test_send_and_get_reply(router):
ping_call = new_method_call(bus_peer, 'Ping')
reply = await asyncio.wait_for(
router.send_and_get_reply(ping_call), timeout=5
)
assert reply.body == ()
async def test_proxy(router):
proxy = Proxy(message_bus, router)
name = "io.gitlab.takluyver.jeepney.examples.Server"
res = await proxy.RequestName(name)
assert res in {(1,), (2,)} # 1: got the name, 2: queued
has_owner, = await proxy.NameHasOwner(name)
assert has_owner is True
async def test_filter(router):
bus = Proxy(message_bus, router)
name = "io.gitlab.takluyver.jeepney.tests.asyncio_test_filter"
match_rule = MatchRule(
type="signal",
sender=message_bus.bus_name,
interface=message_bus.interface,
member="NameOwnerChanged",
path=message_bus.object_path,
)
match_rule.add_arg_condition(0, name)
# Ask the message bus to subscribe us to this signal
await bus.AddMatch(match_rule)
with router.filter(match_rule) as queue:
res, = await bus.RequestName(name)
assert res == 1 # 1: got the name
signal_msg = await asyncio.wait_for(queue.get(), timeout=2.0)
assert signal_msg.body == (name, '', router.unique_name)
async def test_recv_after_connect():
# Can't use here:
# 1. 'connection' fixture
# 2. asyncio.wait_for()
# If (1) and/or (2) is used, the error won't be triggered.
conn = await open_dbus_connection(bus='SESSION')
try:
with pytest.raises(asyncio.TimeoutError):
async with async_timeout.timeout(0):
await conn.receive()
finally:
await conn.close()

View File

@ -0,0 +1,88 @@
import pytest
from jeepney import new_method_call, MessageType, DBusAddress
from jeepney.bus_messages import message_bus, MatchRule
from jeepney.io.blocking import open_dbus_connection, Proxy
from .utils import have_session_bus
pytestmark = pytest.mark.skipif(
not have_session_bus, reason="Tests require DBus session bus"
)
@pytest.fixture
def session_conn():
with open_dbus_connection(bus='SESSION') as conn:
yield conn
def test_connect(session_conn):
assert session_conn.unique_name.startswith(':')
bus_peer = DBusAddress(
bus_name='org.freedesktop.DBus',
object_path='/org/freedesktop/DBus',
interface='org.freedesktop.DBus.Peer'
)
def test_send_and_get_reply(session_conn):
ping_call = new_method_call(bus_peer, 'Ping')
reply = session_conn.send_and_get_reply(ping_call, timeout=5)
assert reply.header.message_type == MessageType.method_return
assert reply.body == ()
ping_call = new_method_call(bus_peer, 'Ping')
reply_body = session_conn.send_and_get_reply(ping_call, timeout=5, unwrap=True)
assert reply_body == ()
def test_proxy(session_conn):
proxy = Proxy(message_bus, session_conn, timeout=5)
name = "io.gitlab.takluyver.jeepney.examples.Server"
res = proxy.RequestName(name)
assert res in {(1,), (2,)} # 1: got the name, 2: queued
has_owner, = proxy.NameHasOwner(name, _timeout=3)
assert has_owner is True
def test_filter(session_conn):
bus = Proxy(message_bus, session_conn)
name = "io.gitlab.takluyver.jeepney.tests.blocking_test_filter"
match_rule = MatchRule(
type="signal",
sender=message_bus.bus_name,
interface=message_bus.interface,
member="NameOwnerChanged",
path=message_bus.object_path,
)
match_rule.add_arg_condition(0, name)
# Ask the message bus to subscribe us to this signal
bus.AddMatch(match_rule)
with session_conn.filter(match_rule) as matches:
res, = bus.RequestName(name)
assert res == 1 # 1: got the name
signal_msg = session_conn.recv_until_filtered(matches, timeout=2)
assert signal_msg.body == (name, '', session_conn.unique_name)
def test_recv_fd(respond_with_fd):
getfd_call = new_method_call(respond_with_fd, 'GetFD')
with open_dbus_connection(bus='SESSION', enable_fds=True) as conn:
reply = conn.send_and_get_reply(getfd_call, timeout=5)
assert reply.header.message_type is MessageType.method_return
with reply.body[0].to_file('w+') as f:
assert f.read() == 'readme'
def test_send_fd(temp_file_and_contents, read_from_fd):
temp_file, data = temp_file_and_contents
readfd_call = new_method_call(read_from_fd, 'ReadFD', 'h', (temp_file,))
with open_dbus_connection(bus='SESSION', enable_fds=True) as conn:
reply = conn.send_and_get_reply(readfd_call, timeout=5)
assert reply.header.message_type is MessageType.method_return
assert reply.body[0] == data

View File

@ -0,0 +1,83 @@
import pytest
from jeepney import new_method_call, MessageType, DBusAddress
from jeepney.bus_messages import message_bus, MatchRule
from jeepney.io.threading import open_dbus_router, Proxy
from .utils import have_session_bus
pytestmark = pytest.mark.skipif(
not have_session_bus, reason="Tests require DBus session bus"
)
@pytest.fixture
def router():
with open_dbus_router(bus='SESSION') as conn:
yield conn
def test_connect(router):
assert router.unique_name.startswith(':')
bus_peer = DBusAddress(
bus_name='org.freedesktop.DBus',
object_path='/org/freedesktop/DBus',
interface='org.freedesktop.DBus.Peer'
)
def test_send_and_get_reply(router):
ping_call = new_method_call(bus_peer, 'Ping')
reply = router.send_and_get_reply(ping_call, timeout=5)
assert reply.header.message_type == MessageType.method_return
assert reply.body == ()
def test_proxy(router):
proxy = Proxy(message_bus, router, timeout=5)
name = "io.gitlab.takluyver.jeepney.examples.Server"
res = proxy.RequestName(name)
assert res in {(1,), (2,)} # 1: got the name, 2: queued
has_owner, = proxy.NameHasOwner(name, _timeout=3)
assert has_owner is True
def test_filter(router):
bus = Proxy(message_bus, router)
name = "io.gitlab.takluyver.jeepney.tests.threading_test_filter"
match_rule = MatchRule(
type="signal",
sender=message_bus.bus_name,
interface=message_bus.interface,
member="NameOwnerChanged",
path=message_bus.object_path,
)
match_rule.add_arg_condition(0, name)
# Ask the message bus to subscribe us to this signal
bus.AddMatch(match_rule)
with router.filter(match_rule) as queue:
res, = bus.RequestName(name)
assert res == 1 # 1: got the name
signal_msg = queue.get(timeout=2.0)
assert signal_msg.body == (name, '', router.unique_name)
def test_recv_fd(respond_with_fd):
getfd_call = new_method_call(respond_with_fd, 'GetFD')
with open_dbus_router(bus='SESSION', enable_fds=True) as router:
reply = router.send_and_get_reply(getfd_call, timeout=5)
assert reply.header.message_type is MessageType.method_return
with reply.body[0].to_file('w+') as f:
assert f.read() == 'readme'
def test_send_fd(temp_file_and_contents, read_from_fd):
temp_file, data = temp_file_and_contents
readfd_call = new_method_call(read_from_fd, 'ReadFD', 'h', (temp_file,))
with open_dbus_router(bus='SESSION', enable_fds=True) as router:
reply = router.send_and_get_reply(readfd_call, timeout=5)
assert reply.header.message_type is MessageType.method_return
assert reply.body[0] == data

View File

@ -0,0 +1,114 @@
import trio
import pytest
from jeepney import DBusAddress, DBusErrorResponse, MessageType, new_method_call
from jeepney.bus_messages import message_bus, MatchRule
from jeepney.io.trio import (
open_dbus_connection, open_dbus_router, Proxy,
)
from .utils import have_session_bus
pytestmark = [
pytest.mark.trio,
pytest.mark.skipif(
not have_session_bus, reason="Tests require DBus session bus"
),
]
# Can't use any async fixtures here, because pytest-asyncio tries to handle
# all of them: https://github.com/pytest-dev/pytest-asyncio/issues/124
async def test_connect():
conn = await open_dbus_connection(bus='SESSION')
async with conn:
assert conn.unique_name.startswith(':')
bus_peer = DBusAddress(
bus_name='org.freedesktop.DBus',
object_path='/org/freedesktop/DBus',
interface='org.freedesktop.DBus.Peer'
)
async def test_send_and_get_reply():
ping_call = new_method_call(bus_peer, 'Ping')
async with open_dbus_router(bus='SESSION') as req:
with trio.fail_after(5):
reply = await req.send_and_get_reply(ping_call)
assert reply.header.message_type == MessageType.method_return
assert reply.body == ()
async def test_send_and_get_reply_error():
ping_call = new_method_call(bus_peer, 'Snart') # No such method
async with open_dbus_router(bus='SESSION') as req:
with trio.fail_after(5):
reply = await req.send_and_get_reply(ping_call)
assert reply.header.message_type == MessageType.error
async def test_proxy():
async with open_dbus_router(bus='SESSION') as req:
proxy = Proxy(message_bus, req)
name = "io.gitlab.takluyver.jeepney.examples.Server"
res = await proxy.RequestName(name)
assert res in {(1,), (2,)} # 1: got the name, 2: queued
has_owner, = await proxy.NameHasOwner(name)
assert has_owner is True
async def test_proxy_error():
async with open_dbus_router(bus='SESSION') as req:
proxy = Proxy(message_bus, req)
with pytest.raises(DBusErrorResponse):
await proxy.RequestName(":123") # Invalid name
async def test_filter():
name = "io.gitlab.takluyver.jeepney.tests.trio_test_filter"
async with open_dbus_router(bus='SESSION') as router:
bus = Proxy(message_bus, router)
match_rule = MatchRule(
type="signal",
sender=message_bus.bus_name,
interface=message_bus.interface,
member="NameOwnerChanged",
path=message_bus.object_path,
)
match_rule.add_arg_condition(0, name)
# Ask the message bus to subscribe us to this signal
await bus.AddMatch(match_rule)
async with router.filter(match_rule) as chan:
res, = await bus.RequestName(name)
assert res == 1 # 1: got the name
with trio.fail_after(2.0):
signal_msg = await chan.receive()
assert signal_msg.body == (name, '', router.unique_name)
async def test_recv_fd(respond_with_fd):
getfd_call = new_method_call(respond_with_fd, 'GetFD')
with trio.fail_after(5):
async with open_dbus_router(bus='SESSION', enable_fds=True) as router:
reply = await router.send_and_get_reply(getfd_call)
assert reply.header.message_type is MessageType.method_return
with reply.body[0].to_file('w+') as f:
assert f.read() == 'readme'
async def test_send_fd(temp_file_and_contents, read_from_fd):
temp_file, data = temp_file_and_contents
readfd_call = new_method_call(read_from_fd, 'ReadFD', 'h', (temp_file,))
with trio.fail_after(5):
async with open_dbus_router(bus='SESSION', enable_fds=True) as router:
reply = await router.send_and_get_reply(readfd_call)
assert reply.header.message_type is MessageType.method_return
assert reply.body[0] == data

View File

@ -0,0 +1,3 @@
import os
have_session_bus = bool(os.environ.get('DBUS_SESSION_BUS_ADDRESS'))