| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 |
** Contact: http://www.qt-project.org/legal |
| 5 |
** |
| 6 |
** This file is part of the QtSystems module of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 |
** included in the packaging of this file. Please review the following |
| 22 |
** information to ensure the GNU General Public License requirements will |
| 23 |
** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 |
** |
| 25 |
** $QT_END_LICENSE$ |
| 26 |
** |
| 27 |
****************************************************************************/ |
| 28 |
|
| 29 |
//TESTED_COMPONENT=src/serviceframework |
| 30 |
|
| 31 |
#include <QCoreApplication> |
| 32 |
#include "qservicemanager.h" |
| 33 |
#include "qservicefilter.h" |
| 34 |
#include "service.h" |
| 35 |
#include <QTimer> |
| 36 |
#include <QMetaObject> |
| 37 |
#include <QMetaMethod> |
| 38 |
#include <QtTest/QtTest> |
| 39 |
#include <qservice.h> |
| 40 |
#include <qremoteserviceregister.h> |
| 41 |
#include <QDebug> |
| 42 |
#include <QByteArray> |
| 43 |
#include <QDataStream> |
| 44 |
|
| 45 |
QT_USE_NAMESPACE |
| 46 |
Q_DECLARE_METATYPE(QServiceFilter); |
| 47 |
Q_DECLARE_METATYPE(QVariant); |
| 48 |
Q_DECLARE_METATYPE(QList<QString>); |
| 49 |
|
| 50 |
|
| 51 |
class tst_QRemoteServiceRegister: public QObject |
| 52 |
{ |
| 53 |
Q_OBJECT |
| 54 |
public: |
| 55 |
|
| 56 |
private slots: |
| 57 |
void initTestCase(); |
| 58 |
void cleanupTestCase(); |
| 59 |
void checkCreateEntryWithEmptyServiceName(); |
| 60 |
void checkOperators(); |
| 61 |
void checkPublish(); |
| 62 |
void tst_instanceClosed(); |
| 63 |
|
| 64 |
private: |
| 65 |
QRemoteServiceRegister* serviceRegister; |
| 66 |
QRemoteServiceRegister::Entry uniqueEntry; |
| 67 |
QRemoteServiceRegister::Entry uniqueEntry2; |
| 68 |
|
| 69 |
QObject *connectToService(const QString &serviceName); |
| 70 |
bool servicePublished; |
| 71 |
}; |
| 72 |
|
| 73 |
void mySecurityFilterFunction(QServiceClientCredentials *cred) |
| 74 |
{ |
| 75 |
// allow the superuser |
| 76 |
if (cred->getUserIdentifier() == 0) { |
| 77 |
cred->setClientAccepted(false); |
| 78 |
} |
| 79 |
|
| 80 |
cred->setClientAccepted(true); |
| 81 |
} |
| 82 |
|
| 83 |
void alwaysPass(QServiceClientCredentials *cred) |
| 84 |
{ |
| 85 |
cred->setClientAccepted(true); |
| 86 |
} |
| 87 |
|
| 88 |
void tst_QRemoteServiceRegister::initTestCase() |
| 89 |
{ |
| 90 |
qRegisterMetaType<QServiceFilter>("QServiceFilter"); |
| 91 |
qRegisterMetaTypeStreamOperators<QServiceFilter>("QServiceFilter"); |
| 92 |
qRegisterMetaType<QList<QString> >("QList<QString>"); |
| 93 |
qRegisterMetaTypeStreamOperators<QList<QString> >("QList<QString>"); |
| 94 |
|
| 95 |
serviceRegister = new QRemoteServiceRegister(); |
| 96 |
|
| 97 |
//Check setting of close on last instance |
| 98 |
// serviceRegister->setQuitOnLastInstanceClosed(false); |
| 99 |
// QVERIFY(serviceRegister->quitOnLastInstanceClosed() == false); |
| 100 |
serviceRegister->setQuitOnLastInstanceClosed(true); |
| 101 |
QVERIFY(serviceRegister->quitOnLastInstanceClosed() == true); |
| 102 |
|
| 103 |
//check setting a security filter |
| 104 |
serviceRegister->setSecurityFilter(mySecurityFilterFunction); |
| 105 |
|
| 106 |
QServiceManager* manager = new QServiceManager(this); |
| 107 |
const QString path = QFINDTESTDATA("/xmldata/rsrexampleservice.xml"); |
| 108 |
bool r = manager->addService(path); |
| 109 |
QVERIFY2(r, qPrintable(QString("Cannot register RSRExampleService - %1").arg(path))); |
| 110 |
|
| 111 |
// D-Bus auto registration |
| 112 |
#ifndef QT_NO_DBUS |
| 113 |
const QString &file = QDir::homePath() + "/.local/share/dbus-1/services/" + |
| 114 |
"com.nokia.qt.rsrunittest.service"; |
| 115 |
QFile data(file); |
| 116 |
if (data.open(QFile::WriteOnly)) { |
| 117 |
QTextStream out(&data); |
| 118 |
out << "[D-BUS Service]\n" |
| 119 |
<< "Name=com.nokia.qtmobility.sfw.RSRExampleService" << '\n' |
| 120 |
<< "Exec=" << QFileInfo("./qt_sfw_example_rsr_unittest").absoluteFilePath(); |
| 121 |
data.close(); |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
//register the unique service |
| 126 |
uniqueEntry = serviceRegister->createEntry<QRemoteServiceRegisterService>( |
| 127 |
"RSRExampleService", "com.nokia.qt.rsrunittest", "1.0"); |
| 128 |
|
| 129 |
bool valid = uniqueEntry.isValid(); |
| 130 |
QVERIFY(valid == true); |
| 131 |
|
| 132 |
uniqueEntry2 = serviceRegister->createEntry<QRemoteServiceRegisterService>( |
| 133 |
"RSRExampleService", "com.nokia.qt.rsrunittest", "1.0"); |
| 134 |
|
| 135 |
valid = uniqueEntry2.isValid(); |
| 136 |
QVERIFY(valid == true); |
| 137 |
|
| 138 |
servicePublished = false; |
| 139 |
} |
| 140 |
|
| 141 |
void tst_QRemoteServiceRegister::cleanupTestCase() |
| 142 |
{ |
| 143 |
#ifndef QT_NO_DBUS |
| 144 |
const QString &file = QDir::homePath() + "/.local/share/dbus-1/services/" + |
| 145 |
"com.nokia.qt.rsrunittest.service"; |
| 146 |
QFile::remove(file); |
| 147 |
#endif |
| 148 |
|
| 149 |
// clean up the unit, don't leave it registered |
| 150 |
QServiceManager m; |
| 151 |
m.removeService("RSRExampleService"); |
| 152 |
delete serviceRegister; |
| 153 |
} |
| 154 |
|
| 155 |
void tst_QRemoteServiceRegister::checkCreateEntryWithEmptyServiceName() |
| 156 |
{ |
| 157 |
QRemoteServiceRegister::Entry emptyservicename = |
| 158 |
serviceRegister->createEntry<QRemoteServiceRegisterService>( |
| 159 |
"", "", ""); |
| 160 |
QVERIFY(emptyservicename.serviceName() == ""); |
| 161 |
bool valid = emptyservicename.isValid(); |
| 162 |
QVERIFY(valid == false); |
| 163 |
} |
| 164 |
|
| 165 |
void tst_QRemoteServiceRegister::checkOperators() |
| 166 |
{ |
| 167 |
//== operator |
| 168 |
bool equal = (uniqueEntry == uniqueEntry2 ? true : false); |
| 169 |
QVERIFY(equal == true); |
| 170 |
|
| 171 |
//!= operator |
| 172 |
bool notequal = (uniqueEntry != uniqueEntry2 ? true : false); |
| 173 |
QVERIFY(notequal == false); |
| 174 |
|
| 175 |
//= operator |
| 176 |
QRemoteServiceRegister::Entry assignval; |
| 177 |
assignval = uniqueEntry; |
| 178 |
equal = (assignval == uniqueEntry ? true : false); |
| 179 |
QVERIFY(equal == true); |
| 180 |
|
| 181 |
//QDataStream << >> |
| 182 |
#ifndef QT_NO_DATASTREAM |
| 183 |
QByteArray barray = QByteArray(); |
| 184 |
QDataStream streamOut(&barray, QIODevice::WriteOnly); |
| 185 |
streamOut.setVersion(QDataStream::Qt_4_6); |
| 186 |
streamOut << uniqueEntry; |
| 187 |
QDataStream streamIn(&barray, QIODevice::ReadOnly); |
| 188 |
streamOut.setVersion(QDataStream::Qt_4_6); |
| 189 |
QRemoteServiceRegister::Entry streamedentry; |
| 190 |
streamIn >> streamedentry; |
| 191 |
QVERIFY(uniqueEntry.serviceName() == streamedentry.serviceName()); |
| 192 |
QVERIFY(uniqueEntry.interfaceName() == streamedentry.interfaceName()); |
| 193 |
QVERIFY(uniqueEntry.version() == streamedentry.version()); |
| 194 |
#endif |
| 195 |
} |
| 196 |
|
| 197 |
void tst_QRemoteServiceRegister::checkPublish() |
| 198 |
{ |
| 199 |
//publish the registered services |
| 200 |
serviceRegister->publishEntries("qt_sfw_example_rsr_unittest"); |
| 201 |
servicePublished = true; |
| 202 |
|
| 203 |
//check instantiation type |
| 204 |
//- default value |
| 205 |
QRemoteServiceRegister::InstanceType type = uniqueEntry.instantiationType(); |
| 206 |
QRemoteServiceRegister::InstanceType type2 = uniqueEntry2.instantiationType(); |
| 207 |
QVERIFY(type == QRemoteServiceRegister::PrivateInstance); |
| 208 |
QVERIFY(type2 == QRemoteServiceRegister::PrivateInstance); |
| 209 |
//check setting the type |
| 210 |
uniqueEntry2.setInstantiationType(QRemoteServiceRegister::GlobalInstance); |
| 211 |
type2 = uniqueEntry2.instantiationType(); |
| 212 |
QVERIFY(type2 == QRemoteServiceRegister::GlobalInstance); |
| 213 |
} |
| 214 |
|
| 215 |
Q_DECLARE_METATYPE(QRemoteServiceRegister::Entry); |
| 216 |
|
| 217 |
void tst_QRemoteServiceRegister::tst_instanceClosed() |
| 218 |
{ |
| 219 |
qRegisterMetaType<QRemoteServiceRegister::Entry>("QRemoteServiceRegister::Entry"); |
| 220 |
if(!servicePublished) |
| 221 |
serviceRegister->publishEntries("qt_sfw_example_rsr_unittest"); |
| 222 |
|
| 223 |
serviceRegister->setSecurityFilter(alwaysPass); |
| 224 |
QSignalSpy spy(serviceRegister,SIGNAL(instanceClosed(QRemoteServiceRegister::Entry))); |
| 225 |
QSignalSpy spyAll(serviceRegister,SIGNAL(allInstancesClosed())); |
| 226 |
|
| 227 |
QObject *o = connectToService("RSRExampleService"); |
| 228 |
QVERIFY(o); |
| 229 |
|
| 230 |
delete o; |
| 231 |
|
| 232 |
QTRY_COMPARE(spy.count(), 1); |
| 233 |
QCOMPARE(spyAll.count(), 1); |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
QObject *tst_QRemoteServiceRegister::connectToService(const QString &serviceName) |
| 238 |
{ |
| 239 |
QServiceManager manager; |
| 240 |
|
| 241 |
QList<QServiceInterfaceDescriptor> list = manager.findInterfaces(serviceName); |
| 242 |
if (list.isEmpty()) { |
| 243 |
qWarning() << "Couldn't find service" << serviceName << manager.findServices("qt_sfw_example_rsr_unittest"); |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
// Get the interface descriptor |
| 248 |
QServiceInterfaceDescriptor desc = list.at(0); |
| 249 |
if (!desc.isValid()) { |
| 250 |
qWarning() << "Warning: Invalid service interface descriptor for" << serviceName; |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
QObject* service = manager.loadInterface(desc); |
| 255 |
if (!service) { |
| 256 |
qWarning() << "Couldn't load service interface for" << serviceName; |
| 257 |
return 0; |
| 258 |
} |
| 259 |
return service; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
QTEST_MAIN(tst_QRemoteServiceRegister); |
| 264 |
#include "tst_qremoteserviceregister.moc" |