From 105e5e8cc9b025485a2770a3e8f0473500cb0b05 Mon Sep 17 00:00:00 2001 From: Diego Rivera Date: Thu, 29 Dec 2022 16:15:41 -0600 Subject: [PATCH 1/3] BUGFIX: add the interface type if given The interface type value "_type" is already null if not given as a parameter. But if given, it's being ignored while constructing the RemoteObject reference that will eventually be proxied. This leads to method invocations failing with "strange" errors (access denied). This correction caused the methods to at least be invoked correctly. --- .../org/freedesktop/dbus/connections/impl/DBusConnection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java index 05b83f1c..ef54aabd 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java @@ -327,7 +327,7 @@ protected T dynamicProxy(String _source, String _path, ifcs.add(DBusInterface.class); } - RemoteObject ro = new RemoteObject(_source, _path, null, false); + RemoteObject ro = new RemoteObject(_source, _path, _type, false); DBusInterface newi = (DBusInterface) Proxy.newProxyInstance(ifcs.get(0).getClassLoader(), ifcs.toArray(Class[]::new), new RemoteInvocationHandler(this, ro)); getImportedObjects().put(newi, ro); From b36ed63bf7a6badbff14ce04f47aab677f60cda3 Mon Sep 17 00:00:00 2001 From: diego Date: Fri, 30 Dec 2022 12:26:45 -0600 Subject: [PATCH 2/3] Test that the interface is set properly by getExportedObject() --- .../src/test/java/org/freedesktop/DBus.java | 13 ++++ .../dbus/InterfaceCreationTest.java | 70 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 dbus-java-tests/src/test/java/org/freedesktop/DBus.java create mode 100644 dbus-java-tests/src/test/java/org/freedesktop/dbus/InterfaceCreationTest.java diff --git a/dbus-java-tests/src/test/java/org/freedesktop/DBus.java b/dbus-java-tests/src/test/java/org/freedesktop/DBus.java new file mode 100644 index 00000000..d16a4ae6 --- /dev/null +++ b/dbus-java-tests/src/test/java/org/freedesktop/DBus.java @@ -0,0 +1,13 @@ +package org.freedesktop; + +import org.freedesktop.dbus.interfaces.DBusInterface; + +public interface DBus extends DBusInterface { + public interface Peer extends DBusInterface { + + public String GetMachineId(); + + public void Ping(); + + } +} \ No newline at end of file diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/InterfaceCreationTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/InterfaceCreationTest.java new file mode 100644 index 00000000..1f47579a --- /dev/null +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/InterfaceCreationTest.java @@ -0,0 +1,70 @@ +// The package must be "both" org.freedesktop.dbus.connections.impl and org.freedesktop.dbus in order +// for the test to be able to access the requisite methods and fields... obviously this is an issue... +package org.freedesktop.dbus; + +import java.lang.reflect.Proxy; + +import org.freedesktop.DBus; +import org.freedesktop.dbus.bin.EmbeddedDBusDaemon; +import org.freedesktop.dbus.connections.BusAddress; +import org.freedesktop.dbus.connections.impl.DBusConnection; +import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder; +import org.freedesktop.dbus.connections.transports.TransportBuilder; +import org.freedesktop.dbus.interfaces.DBusInterface; +import org.freedesktop.dbus.test.AbstractBaseTest; +import org.junit.jupiter.api.Assertions; + +public class InterfaceCreationTest extends AbstractBaseTest { + + // @Test + public void testCorrectInterfaceCreation() throws Exception { + String protocolType = TransportBuilder.getRegisteredBusTypes().get(0); + BusAddress busAddress = TransportBuilder.createWithDynamicSession(protocolType).configure().build() + .getBusAddress(); + + BusAddress listenBusAddress = BusAddress.of(busAddress).getListenerAddress(); + + try (EmbeddedDBusDaemon daemon = new EmbeddedDBusDaemon(listenBusAddress)) { + daemon.startInBackground(); + this.logger.debug("Started embedded bus on address {}", listenBusAddress); + + waitForDaemon(daemon); + + // connect to started daemon process + this.logger.info("Connecting to embedded DBus {}", busAddress); + + final String source = "org.freedesktop.DBus"; + final String path = "/org/freedesktop/DBus"; + + try (DBusConnection connection = DBusConnectionBuilder.forAddress(busAddress).build()) { + DBusInterface dbi = null; + RemoteInvocationHandler rih = null; + RemoteObject ro = null; + Class type = DBus.Peer.class; + + dbi = connection.getExportedObject(source, path, null); + Assertions.assertNotNull(dbi); + + rih = RemoteInvocationHandler.class.cast(Proxy.getInvocationHandler(dbi)); + Assertions.assertNotNull(rih); + + ro = rih.remote; + + Assertions.assertNull(ro.getInterface()); + + dbi = connection.getExportedObject(source, path, type); + Assertions.assertNotNull(dbi); + Assertions.assertTrue(type.isInstance(dbi)); + + rih = RemoteInvocationHandler.class.cast(Proxy.getInvocationHandler(dbi)); + Assertions.assertNotNull(rih); + + ro = rih.remote; + + Assertions.assertNotNull(ro.getInterface()); + Assertions.assertSame(type, ro.getInterface()); + } + } + } + +} From 05453373d985870672ff9429afbb7418922e9ab2 Mon Sep 17 00:00:00 2001 From: diego Date: Fri, 30 Dec 2022 12:33:55 -0600 Subject: [PATCH 3/3] Activate the new test --- .../test/java/org/freedesktop/dbus/InterfaceCreationTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/InterfaceCreationTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/InterfaceCreationTest.java index 1f47579a..e40e2e5f 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/InterfaceCreationTest.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/InterfaceCreationTest.java @@ -13,10 +13,11 @@ import org.freedesktop.dbus.interfaces.DBusInterface; import org.freedesktop.dbus.test.AbstractBaseTest; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class InterfaceCreationTest extends AbstractBaseTest { - // @Test + @Test public void testCorrectInterfaceCreation() throws Exception { String protocolType = TransportBuilder.getRegisteredBusTypes().get(0); BusAddress busAddress = TransportBuilder.createWithDynamicSession(protocolType).configure().build()