Debian Codegraph @

tests/auto/serviceframework/qservicemanager_ipc/service/main.cpp

raw symbols SCIP
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 #include <QCoreApplication>
29 #include <QTimer>
30 #include <qremoteserviceregister.h>
31 #include <qservicemanager.h>
32 #include <QDebug>
33 #include <QTest>
34 #include <QFileInfo>
35
36 #include <qservicefilter.h> //only used to test custom metatype
37 #ifdef QT_BUILD_INTERNAL
38 #include <private/instancemanager_p.h>
39 #endif
40
41 QT_USE_NAMESPACE
42 Q_DECLARE_METATYPE(QServiceFilter);
43 Q_DECLARE_METATYPE(QVariant);
44 Q_DECLARE_METATYPE(QList<QString>);
45
46 #ifdef SFW_USE_DBUS_BACKEND
47 #include <QtDBus/QtDBus>
48 #if QT_VERSION < 0x040800
49 inline QDBusArgument &operator<<(QDBusArgument &arg, const QVariantHash &map)
50 {
51 arg.beginMap(QVariant::String, qMetaTypeId<QDBusVariant>());
52 QVariantHash::ConstIterator it = map.constBegin();
53 QVariantHash::ConstIterator end = map.constEnd();
54 for ( ; it != end; ++it) {
55 arg.beginMapEntry();
56 arg << it.key() << QDBusVariant(it.value());
57 arg.endMapEntry();
58 }
59 arg.endMap();
60 return arg;
61 }
62 #endif
63 #endif
64
65 class SharedTestService : public QObject
66 {
67 Q_OBJECT
68 Q_PROPERTY(QString value READ value WRITE setValue RESET resetValue NOTIFY valueChanged SCRIPTABLE true DESIGNABLE true STORED true);
69
70 public:
71 SharedTestService(QObject* parent = 0)
72 : QObject(parent)
73 {
74 qFatal("Insecure constructor called, this should never be called if a secured constructor is available");
75 resetValue();
76 }
77
78 Q_INVOKABLE SharedTestService(QServiceClientCredentials *creds)
79 : QObject(0)
80 {
81 creds->setClientAccepted(true);
82 resetValue();
83 }
84
85 Q_INVOKABLE void verifyNewServiceClientCredentials(QServiceClientCredentials *creds)
86 {
87 creds->setClientAccepted(true);
88 }
89
90 QString value() const
91 {
92 return m_value;
93 }
94
95 void setValue(const QString& value)
96 {
97 m_value = value;
98 emit valueChanged();
99 }
100
101 void resetValue()
102 {
103 m_value = "FFF";
104 emit valueChanged();
105 }
106
107 Q_INVOKABLE QString testFunctionWithReturnValue(int input)
108 {
109 QString output("%1 + 3 = %2");
110 output = output.arg(input).arg(input+3);
111 return output;
112 }
113
114 Q_INVOKABLE QVariant testFunctionWithVariantReturnValue(const QVariant& input)
115 {
116 m_hash = qHash(input.toString());
117 return input;
118 }
119
120 Q_INVOKABLE QServiceFilter testFunctionWithCustomReturnValue()
121 {
122 QServiceFilter f("com.nokia.qt.ipcunittest", "6.2");
123 f.setServiceName("MySharedService");
124 return f;
125 }
126
127 Q_INVOKABLE uint slotConfirmation() const
128 {
129 return m_hash;
130 }
131
132 Q_INVOKABLE void setConfirmationHash(uint hash)
133 {
134 m_hash = hash;
135 }
136
137 Q_SIGNALS:
138 void signalWithIntParam(int);
139 void signalWithVariousParam(QVariant,QString,QServiceFilter);
140 void valueChanged();
141
142 public slots:
143 void triggerSignalWithIntParam()
144 {
145 QTimer::singleShot(2000, this, SLOT(triggerSignalWithIntParamExecute()));
146 }
147
148 void triggerSignalWithVariousParam()
149 {
150 QTimer::singleShot(2000, this, SLOT(triggerSignalWithVariousParamExecute()));
151 }
152
153 void triggerSignalWithIntParamExecute()
154 {
155 emit signalWithIntParam( 5 );
156 }
157
158 void triggerSignalWithVariousParamExecute()
159 {
160 QServiceFilter f("com.nokia.qt.ipcunittest", "6.7");
161 f.setServiceName("MyService");
162 emit signalWithVariousParam( QVariant(), QString("string-value"), f );
163 }
164 void testSlot()
165 {
166 m_hash = qHash(QString("testSlot()"));
167 }
168
169 void testSlotWithArgs(const QByteArray& d, int a, const QVariant& variant)
170 {
171 QString output("%1, %2, %3, %4");
172 output = output.arg(d.constData()).arg(a).arg(variant.toString()).arg(variant.isValid());
173 m_hash = qHash(output);
174 }
175
176 void testSlotWithCustomArg(const QServiceFilter& f)
177 {
178 QString output("%1: %2 - %3.%4");
179 output = output.arg(f.serviceName()).arg(f.interfaceName())
180 .arg(f.majorVersion()).arg(f.minorVersion());
181 m_hash = qHash(output);
182 }
183
184 void testSlotWithUnknownArg(const QServiceInterfaceDescriptor& )
185 {
186 m_hash = 1;
187 }
188
189 void testSlotWithComplexArg(QVariantHash arg)
190 {
191 QHashIterator<QString, QVariant> i(arg);
192 QString output;
193 while (i.hasNext()) {
194 i.next();
195 // qDebug() << i.key() << ": " << i.value();
196 output += i.key();
197 output += "=";
198 output += i.value().toString();
199 output += ", ";
200 }
201 qDebug() << output;
202 m_hash = qHash(output);
203 }
204
205 void testIpcFailure()
206 {
207 qApp->exit(0); // exit to show failure
208 }
209
210 private:
211 QString m_value;
212 uint m_hash;
213 };
214
215 class UniqueTestService : public QObject
216 {
217 Q_OBJECT
218 Q_CLASSINFO("UniqueTestService", "First test")
219 Q_CLASSINFO("Key", "Value")
220
221 Q_PROPERTY(QString value READ value WRITE setValue RESET resetValue NOTIFY valueChanged SCRIPTABLE true DESIGNABLE true STORED true)
222 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
223 Q_PROPERTY(ServiceFlag serviceFlags READ flags WRITE setFlags)
224 Q_PROPERTY(QString blockingValue READ blockingValue)
225 Q_PROPERTY(QString releaseBlockingRead READ releaseBlockingRead)
226 Q_ENUMS(Priority)
227 Q_FLAGS(ServiceFlag ServiceFlags)
228
229 public:
230 enum ServiceFlag {
231 FirstBit = 0x01,
232 SecondBit = 0x02,
233 ThirdBit = 0x04
234 };
235 Q_DECLARE_FLAGS(ServiceFlags, ServiceFlag)
236
237 ServiceFlags flags() const { return m_flags; }
238 void setFlags(ServiceFlags f) {
239 m_flags = f;
240 }
241
242 enum Priority { High, Low, VeryLow, ExtremelyLow };
243
244 void setPriority(Priority p) {
245 m_priority = p;
246 emit priorityChanged();
247 }
248
249 Priority priority() const
250 {
251 return m_priority;
252 }
253
254 UniqueTestService(QObject* parent = 0)
255 : QObject(parent)
256 {
257 qFatal("Wrong constructor called, must call secure constructor only");
258 abort();
259 resetValue();
260 }
261
262 Q_INVOKABLE UniqueTestService(QServiceClientCredentials *creds)
263 : QObject(0), m_priority(UniqueTestService::High), m_flags(ThirdBit)
264 {
265 // accept everyone
266 creds->setClientAccepted(true);
267 resetValue();
268 }
269
270 QString value() const
271 {
272 return m_value;
273 }
274
275 void setValue(const QString& value)
276 {
277 m_value = value;
278 emit valueChanged();
279 }
280
281 void resetValue()
282 {
283 m_value = "FFF";
284 emit valueChanged();
285 }
286
287 QString blockingValue()
288 {
289 emit blockingValueRead();
290 m_loop.exec();
291 return QStringLiteral("blockingValueReturned");
292 }
293
294 QString releaseBlockingRead()
295 {
296 m_loop.quit();
297 return QStringLiteral("releaseBlockingReadReturned");
298 }
299
300 Q_INVOKABLE QString testFunctionWithReturnValue(int input)
301 {
302 QString output("%1 x 3 = %2");
303 output = output.arg(input).arg(input*3);
304 return output;
305 }
306
307 Q_INVOKABLE QVariant testFunctionWithVariantReturnValue(const QVariant& input)
308 {
309 return input;
310 }
311
312 Q_INVOKABLE QServiceFilter testFunctionWithCustomReturnValue()
313 {
314 QServiceFilter f("com.nokia.qt.ipcunittest", "6.7");
315 f.setServiceName("MyUniqueService");
316 return f;
317 }
318
319 Q_INVOKABLE QList<QString> testFunctionWithListReturn()
320 {
321 QList<QString> list;
322 list << "1" << "2" << "3";
323 return list;
324 }
325
326 Q_INVOKABLE uint slotConfirmation() const
327 {
328 return m_hash;
329 }
330
331 Q_INVOKABLE void setConfirmationHash(uint hash)
332 {
333 m_hash = hash;
334 }
335
336 Q_SIGNALS:
337 void signalWithIntParam(int);
338 void signalWithVariousParam(QVariant,QString,QServiceFilter,QVariant);
339 void valueChanged();
340 void priorityChanged();
341 void count(int value);
342 void blockingValueRead();
343
344 public slots:
345 void triggerSignalWithIntParam()
346 {
347 QTimer::singleShot(2000, this, SLOT(triggerSignalWithIntParamExecute()));
348 }
349
350 void triggerSignalWithVariousParam()
351 {
352 QTimer::singleShot(2000, this, SLOT(triggerSignalWithVariousParamExecute()));
353 }
354
355 void triggerSignalWithIntParamExecute()
356 {
357 emit signalWithIntParam( 5 );
358 }
359
360 void triggerSignalWithVariousParamExecute()
361 {
362 QServiceFilter f("com.nokia.qt.ipcunittest", "6.7");
363 f.setServiceName("MyService");
364 emit signalWithVariousParam( QVariant("CAN'T BE NULL"), QString("string-value"), f, QVariant(5) );
365 }
366
367 void testSlot() {
368 m_hash = qHash(QString("testSlot()"));
369 }
370
371 void testSlotWithArgs(const QByteArray& d, int a, const QVariant& variant)
372 {
373 QString output("%1, %2, %3, %4");
374 output = output.arg(d.constData()).arg(a).arg(variant.toString()).arg(variant.isValid());
375 m_hash = qHash(output);
376 }
377
378 void testSlotWithCustomArg(const QServiceFilter& f)
379 {
380 QString output("%1: %2 - %3.%4");
381 output = output.arg(f.serviceName()).arg(f.interfaceName())
382 .arg(f.majorVersion()).arg(f.minorVersion());
383 m_hash = qHash(output);
384 }
385
386 void testSlotWithUnknownArg(const QServiceInterfaceDescriptor& )
387 {
388 m_hash = 1;
389 }
390
391 void testSlotWithListArg(QList<QString> list)
392 {
393 QString output;
394 for (int i=0; i<list.size(); i++) {
395 output += list[i];
396 if (i<list.size()-1)
397 output += ", ";
398 }
399 m_hash = qHash(output);
400 }
401
402 void testSlotWithComplexArg(QVariantHash arg)
403 {
404 QHashIterator<QString, QVariant> i(arg);
405 QStringList lines;
406 while (i.hasNext()) {
407 i.next();
408 QString line = i.key();
409 line += "=";
410 line += i.value().toString();
411 lines << line;
412 }
413 lines.sort();
414 QString output = lines.join(QStringLiteral(","));
415 m_hash = qHash(output);
416 }
417
418 void testSlotWithData(QByteArray data)
419 {
420 m_data = data;
421 }
422
423 QByteArray testInvoableWithReturnData()
424 {
425 return m_data;
426 }
427
428 int testSignalSlotOrdering() {
429 m_count = 0;
430 QMetaObject::invokeMethod(this, "generateSignal", Qt::QueuedConnection);
431
432 return 0;
433 }
434
435 void generateSignal() {
436 emit count(m_count++);
437 if (m_count < 20) {
438 QMetaObject::invokeMethod(this, "generateSignal", Qt::QueuedConnection);
439 }
440 }
441
442 void testIpcFailure() {
443 qApp->exit(0); // exit to show failure
444 }
445
446 private:
447 QString m_value;
448 Priority m_priority;
449 ServiceFlags m_flags;
450 uint m_hash;
451 QByteArray m_data;
452 int m_count;
453 QEventLoop m_loop;
454 };
455
456 class FailureTestServiceCreation : public QObject
457 {
458 Q_OBJECT
459 Q_CLASSINFO("FailureTestServiceCreation", "Fails to be created");
460
461 };
462
463 class MiscTestService : public QObject
464 {
465 Q_OBJECT
466 public:
467 MiscTestService(QObject* parent = 0)
468 : QObject(parent)
469 {
470 qFatal("Invalid constructor called");
471 }
472
473 Q_INVOKABLE MiscTestService(QServiceClientCredentials *creds)
474 : QObject(0)
475 {
476 creds->setClientAccepted(true);
477 }
478
479
480 Q_INVOKABLE bool addTwice()
481 {
482 QRemoteServiceRegister* serviceRegister = new QRemoteServiceRegister();
483
484 //register the unique service
485 QRemoteServiceRegister::Entry uniqueEntry =
486 serviceRegister->createEntry<SharedTestService>(
487 "IPCExampleService", "com.nokia.qt.ipcunittest", "3.5");
488
489 QRemoteServiceRegister::Entry uniqueEntry2 =
490 serviceRegister->createEntry<SharedTestService>(
491 "IPCExampleService", "com.nokia.qt.ipcunittest", "3.5");
492 return (uniqueEntry == uniqueEntry2);
493
494 }
495
496 Q_INVOKABLE bool getInvalidEntry()
497 {
498 #ifdef QT_BUILD_INTERNAL
499 InstanceManager *i = InstanceManager::instance();
500 QRemoteServiceRegister::Entry e; // entry should be invalid
501 const QMetaObject *o = i->metaObject(e); // should return null
502 return (o == 0);
503 #else
504 return true;
505 #endif
506 }
507 };
508
509 class SecurityTestService : public QObject
510 {
511 Q_OBJECT
512 public:
513 SecurityTestService(QObject* parent = 0)
514 : QObject(parent)
515 {
516 qFatal("Invalid constructor called");
517 }
518
519 Q_INVOKABLE SecurityTestService(QServiceClientCredentials *creds)
520 : QObject(0)
521 {
522 qDebug() << "Creds" << creds->getProcessIdentifier() << creds->getUserIdentifier() << creds->getGroupIdentifier();
523 creds->setClientAccepted(false);
524 }
525 };
526
527 class GlobalSecurityTestService : public QObject
528 {
529 Q_OBJECT
530 public:
531 GlobalSecurityTestService(QObject* parent = 0)
532 : QObject(parent)
533 {
534 qFatal("Invalid constructor called");
535 // If this called, fail the rest of the tests, reverse this value
536 disabled = true;
537 }
538
539 Q_INVOKABLE GlobalSecurityTestService(QServiceClientCredentials *creds)
540 : QObject(0)
541 {
542 creds->setClientAccepted(true);
543 disabled = false;
544 }
545
546 Q_INVOKABLE void verifyNewServiceClientCredentials(QServiceClientCredentials *creds)
547 {
548 creds->setClientAccepted(!disabled);
549 }
550
551 Q_INVOKABLE bool disableConnections(bool disable)
552 {
553 this->disabled = disable;
554 return this->disabled;
555 }
556
557 private:
558 bool disabled;
559 };
560
561
562 Q_DECLARE_METATYPE(QMetaType::Type);
563
564 QT_BEGIN_NAMESPACE
565 template <>
566 QObject* qServiceTypeConstructHelper<FailureTestServiceCreation>()
567 {
568 return 0x0;
569 }
570 QT_END_NAMESPACE
571
572 int main(int argc, char** argv)
573 {
574 QCoreApplication app(argc, argv);
575
576 qRegisterMetaType<QServiceFilter>();
577 qRegisterMetaTypeStreamOperators<QServiceFilter>("QServiceFilter");
578
579 qRegisterMetaType<QVariant>();
580 qRegisterMetaTypeStreamOperators<QVariant>("QVariant");
581
582 qRegisterMetaType<QList<QString> >();
583 qRegisterMetaTypeStreamOperators<QList<QString> >("QList<QString>");
584
585 qRegisterMetaTypeStreamOperators<QVariantHash>("QVariantHash");
586 qRegisterMetaType<QVariantHash>("QVariantHash");
587 #ifdef SFW_USE_DBUS_BACKEND
588 qDBusRegisterMetaType<QVariantHash>();
589 #endif
590
591 QRemoteServiceRegister* serviceRegister = new QRemoteServiceRegister();
592
593 //register the unique service
594 QRemoteServiceRegister::Entry uniqueEntry =
595 serviceRegister->createEntry<UniqueTestService>(
596 "IPCExampleService", "com.nokia.qt.ipcunittest", "3.5");
597 uniqueEntry.setInstantiationType(QRemoteServiceRegister::PrivateInstance);
598
599 //register the shared srevice
600 QRemoteServiceRegister::Entry sharedEntry =
601 serviceRegister->createEntry<SharedTestService>(
602 "IPCExampleService", "com.nokia.qt.ipcunittest", "3.4");
603 sharedEntry.setInstantiationType(QRemoteServiceRegister::GlobalInstance);
604
605 // register the class to test creation failure
606 QRemoteServiceRegister::Entry creationEntry =
607 serviceRegister->createEntry<FailureTestServiceCreation>(
608 "IPCExampleService", "com.nokia.qt.ipcunittest", "3.6");
609 creationEntry.setInstantiationType(QRemoteServiceRegister::GlobalInstance);
610
611 // register the class to test creation failure
612 QRemoteServiceRegister::Entry creationEntryPriv =
613 serviceRegister->createEntry<FailureTestServiceCreation>(
614 "IPCExampleService", "com.nokia.qt.ipcunittest", "3.7");
615 creationEntryPriv.setInstantiationType(QRemoteServiceRegister::PrivateInstance);
616
617 // register the class to test creation failure
618 QRemoteServiceRegister::Entry miscEntry =
619 serviceRegister->createEntry<MiscTestService>(
620 "IPCExampleService", "com.nokia.qt.ipcunittest", "3.8");
621 creationEntryPriv.setInstantiationType(QRemoteServiceRegister::GlobalInstance);
622
623 // register the class to test creation failure
624 QRemoteServiceRegister::Entry securityEntry =
625 serviceRegister->createEntry<SecurityTestService>(
626 "IPCExampleService", "com.nokia.qt.ipcunittest", "3.9");
627 securityEntry.setInstantiationType(QRemoteServiceRegister::PrivateInstance);
628
629 // register the class to test creation failure
630 QRemoteServiceRegister::Entry globalSecurityEntry =
631 serviceRegister->createEntry<GlobalSecurityTestService>(
632 "IPCExampleService", "com.nokia.qt.ipcunittest", "3.10");
633 globalSecurityEntry.setInstantiationType(QRemoteServiceRegister::GlobalInstance);
634
635 //publish the registered services
636 serviceRegister->publishEntries("qt_sfw_example_ipc_unittest");
637 int res = app.exec();
638 delete serviceRegister;
639
640 return res;
641 }
642
643
644 #include "main.moc"