altdesktop / python-dbus-next

🚌 The next great DBus library for Python with asyncio support
https://python-dbus-next.readthedocs.io/en/latest/
MIT License
191 stars 60 forks source link

Support for tcp transport #57

Closed M-Bab closed 4 years ago

M-Bab commented 4 years ago

Implementation for D-Bus communication via TCP/IP. Previously there was also AUTH ANONYMOUS added to the authentifications but this is now already implemented - so no need to change anything there.

acrisci commented 4 years ago

Is it possible to write tests for this?

M-Bab commented 4 years ago

It is - if there is a CI system available where a tcp-dbus connection is possible. Currently we are using a special test system where the dbus is open to the network and allows anonymous authentification (very evil).

I will try out if a simple, safe and effective test can be implemented with socat on the loopback interface.

M-Bab commented 4 years ago

Okay I managed to get it fully working for a local test:

  1. Open a local socket: socat TCP-LISTEN:55556,reuseaddr,fork,range=127.0.0.1/32 UNIX-CONNECT:$(echo $DBUS_SESSION_BUS_ADDRESS | sed 's/unix:path=//g')

  2. This piece of code creates a notification via tcp:

    
    import asyncio
    import sys
    from dbus_next.aio import MessageBus
    #from dbus_next.message import Message, MessageType

async def main(args): bus = await MessageBus(bus_address="tcp:host=127.0.0.1,port=55556").connect()

reply = await bus.call(Message(destination='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='ListNames'))

introspection = await bus.introspect('org.freedesktop.Notifications', '/org/freedesktop/Notifications') obj = bus.get_proxy_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications', introspection) notification = obj.get_interface('org.freedesktop.Notifications') await notification.call_notify("test.py", 0, "", "DBus Test", "Test notification", [""], dict(), 3000)

if name == "main": loop = asyncio.get_event_loop() basefilename = loop.run_until_complete(main(sys.argv))


It is somehow amazing that it works because neither d-feet nor notify-send were able to connect to the socat in my case.

Or what kind of tests specifically shall I add?
acrisci commented 4 years ago

The only way I can ensure this will be stable with future changes if you add tests to the test suite for your use cases.

M-Bab commented 4 years ago

Here you go :+1:. Added 2 tests which are simple but should be very effective to protect the functionality. Also added an example for anyone willing to use it.

acrisci commented 4 years ago

This is fine for now :+1:

M-Bab commented 4 years ago

Thanks a lot. I am sorry I could not get a good working test for the combination network+anonymous auth via dbus config. Some colleagues of mine figured it out on an embedded device but it didn't work for me on any x86 machine.