Fix QDBusAbstractInterface::isValid() for peer connections

Do not attempt to lookup the service owner on peer connections (it will
fail).
Make QDBusAbstractInterface::isValid() return a sensible result on peer
connections, instead of always returning false.

Task-number: QTBUG-32374
Change-Id: I1b02feaffb3b255188f8d63306f89f5034a32f22
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Alberto Mardegan 2013-07-15 12:12:08 +03:00 committed by The Qt Project
parent 8493f5d6a3
commit 9b8b6c7db8
2 changed files with 35 additions and 5 deletions

View File

@ -288,7 +288,8 @@ QDBusAbstractInterface::QDBusAbstractInterface(QDBusAbstractInterfacePrivate &d,
if (d.isValid && if (d.isValid &&
d.connection.isConnected() d.connection.isConnected()
&& !d.service.isEmpty() && !d.service.isEmpty()
&& !d.service.startsWith(QLatin1Char(':'))) && !d.service.startsWith(QLatin1Char(':'))
&& d.connectionPrivate()->mode != QDBusConnectionPrivate::PeerMode)
d_func()->connection.connect(QLatin1String(DBUS_SERVICE_DBUS), // service d_func()->connection.connect(QLatin1String(DBUS_SERVICE_DBUS), // service
QString(), // path QString(), // path
QLatin1String(DBUS_INTERFACE_DBUS), // interface QLatin1String(DBUS_INTERFACE_DBUS), // interface
@ -313,7 +314,8 @@ QDBusAbstractInterface::QDBusAbstractInterface(const QString &service, const QSt
if (d_func()->isValid && if (d_func()->isValid &&
d_func()->connection.isConnected() d_func()->connection.isConnected()
&& !service.isEmpty() && !service.isEmpty()
&& !service.startsWith(QLatin1Char(':'))) && !service.startsWith(QLatin1Char(':'))
&& d_func()->connectionPrivate()->mode != QDBusConnectionPrivate::PeerMode)
d_func()->connection.connect(QLatin1String(DBUS_SERVICE_DBUS), // service d_func()->connection.connect(QLatin1String(DBUS_SERVICE_DBUS), // service
QString(), // path QString(), // path
QLatin1String(DBUS_INTERFACE_DBUS), // interface QLatin1String(DBUS_INTERFACE_DBUS), // interface
@ -340,7 +342,13 @@ QDBusAbstractInterface::~QDBusAbstractInterface()
*/ */
bool QDBusAbstractInterface::isValid() const bool QDBusAbstractInterface::isValid() const
{ {
return !d_func()->currentOwner.isEmpty(); Q_D(const QDBusAbstractInterface);
/* We don't retrieve the owner name for peer connections */
if (d->connectionPrivate() && d->connectionPrivate()->mode == QDBusConnectionPrivate::PeerMode) {
return d->isValid;
} else {
return !d->currentOwner.isEmpty();
}
} }
/*! /*!

View File

@ -70,12 +70,12 @@ class tst_QDBusAbstractInterface: public QObject
return Pinger(new com::trolltech::QtDBus::Pinger(service, path, con)); return Pinger(new com::trolltech::QtDBus::Pinger(service, path, con));
} }
Pinger getPingerPeer(const QString &path = "/") Pinger getPingerPeer(const QString &path = "/", const QString &service = "")
{ {
QDBusConnection con = QDBusConnection("peer"); QDBusConnection con = QDBusConnection("peer");
if (!con.isConnected()) if (!con.isConnected())
return Pinger(); return Pinger();
return Pinger(new com::trolltech::QtDBus::Pinger("", path, con)); return Pinger(new com::trolltech::QtDBus::Pinger(service, path, con));
} }
void resetServer() void resetServer()
@ -197,6 +197,10 @@ private slots:
void directPropertyReadErrorsPeer(); void directPropertyReadErrorsPeer();
void directPropertyWriteErrorsPeer_data(); void directPropertyWriteErrorsPeer_data();
void directPropertyWriteErrorsPeer(); void directPropertyWriteErrorsPeer();
void validity_data();
void validity();
private: private:
QProcess proc; QProcess proc;
}; };
@ -1381,5 +1385,23 @@ void tst_QDBusAbstractInterface::directPropertyWriteErrorsPeer()
QTEST(p->lastError().name(), "errorName"); QTEST(p->lastError().name(), "errorName");
} }
void tst_QDBusAbstractInterface::validity_data()
{
QTest::addColumn<QString>("service");
QTest::newRow("null-service") << "";
QTest::newRow("ignored-service") << "org.example.anyservice";
}
void tst_QDBusAbstractInterface::validity()
{
/* Test case for QTBUG-32374 */
QFETCH(QString, service);
Pinger p = getPingerPeer("/", service);
QVERIFY2(p, "Not connected to D-Bus");
QVERIFY(p->isValid());
}
QTEST_MAIN(tst_QDBusAbstractInterface) QTEST_MAIN(tst_QDBusAbstractInterface)
#include "tst_qdbusabstractinterface.moc" #include "tst_qdbusabstractinterface.moc"