Debian Codegraph @

tests/auto/serviceframework/qremoteserviceregister/tst_qremoteserviceregister.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 QSKIP("QTBUG-35662: fails due to missing /etc/machine-id");
220 qRegisterMetaType<QRemoteServiceRegister::Entry>("QRemoteServiceRegister::Entry");
221 if(!servicePublished)
222 serviceRegister->publishEntries("qt_sfw_example_rsr_unittest");
223
224 serviceRegister->setSecurityFilter(alwaysPass);
225 QSignalSpy spy(serviceRegister,SIGNAL(instanceClosed(QRemoteServiceRegister::Entry)));
226 QSignalSpy spyAll(serviceRegister,SIGNAL(allInstancesClosed()));
227
228 QObject *o = connectToService("RSRExampleService");
229 QVERIFY(o);
230
231 delete o;
232
233 QTRY_COMPARE(spy.count(), 1);
234 QCOMPARE(spyAll.count(), 1);
235
236 }
237
238 QObject *tst_QRemoteServiceRegister::connectToService(const QString &serviceName)
239 {
240 QServiceManager manager;
241
242 QList<QServiceInterfaceDescriptor> list = manager.findInterfaces(serviceName);
243 if (list.isEmpty()) {
244 qWarning() << "Couldn't find service" << serviceName << manager.findServices("qt_sfw_example_rsr_unittest");
245 return 0;
246 }
247
248 // Get the interface descriptor
249 QServiceInterfaceDescriptor desc = list.at(0);
250 if (!desc.isValid()) {
251 qWarning() << "Warning: Invalid service interface descriptor for" << serviceName;
252 return 0;
253 }
254
255 QObject* service = manager.loadInterface(desc);
256 if (!service) {
257 qWarning() << "Couldn't load service interface for" << serviceName;
258 return 0;
259 }
260 return service;
261 }
262
263
264 QTEST_MAIN(tst_QRemoteServiceRegister);
265 #include "tst_qremoteserviceregister.moc"