| 1 | |
| 2 | /**************************************************************************** |
| 3 | ** |
| 4 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 5 | ** Contact: http://www.qt-project.org/legal |
| 6 | ** |
| 7 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | //TESTED_COMPONENT=src/serviceframework |
| 31 | |
| 32 | #include <QCoreApplication> |
| 33 | #include "qservicemanager.h" |
| 34 | #include "qservicefilter.h" |
| 35 | #include <QTimer> |
| 36 | #include <QMetaObject> |
| 37 | #include <QMetaMethod> |
| 38 | #include <QThread> |
| 39 | #include <QtTest/QtTest> |
| 40 | #include <qservice.h> |
| 41 | #include <qremoteserviceregister.h> |
| 42 | #include <qservicereply.h> |
| 43 | |
| 44 | #include <signal.h> |
| 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 | |
| 66 | #ifdef Q_OS_MAC |
| 67 | #include <stdlib.h> |
| 68 | #include <sys/resource.h> |
| 69 | #endif |
| 70 | |
| 71 | QT_USE_NAMESPACE |
| 72 | Q_DECLARE_METATYPE(QServiceFilter); |
| 73 | Q_DECLARE_METATYPE(QVariant); |
| 74 | Q_DECLARE_METATYPE(QList<QString>); |
| 75 | Q_DECLARE_METATYPE(QVariantHash); |
| 76 | |
| 77 | char serviceReuqestXml[] = |
| 78 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 79 | "<SFW version=\"1.1\">" |
| 80 | "<service>" |
| 81 | "<name>com.nokia.mt.processmanager</name>" |
| 82 | "<ipcaddress>com.nokia.mt.processmanager.ServiceRequestSocket</ipcaddress>" |
| 83 | "<interface>" |
| 84 | "<name>com.nokia.mt.processmanager.ProcessManagerController</name>" |
| 85 | "<version>1.0</version>" |
| 86 | "</interface>" |
| 87 | "</service>" |
| 88 | "</SFW>"; |
| 89 | |
| 90 | char serviceReuqestXmlFake[] = |
| 91 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 92 | "<SFW version=\"1.1\">" |
| 93 | "<service>" |
| 94 | "<name>com.nokia.qt.does.not.exist</name>" |
| 95 | "<ipcaddress>com.nokia.qt.does.not.exist</ipcaddress>" |
| 96 | "<interface>" |
| 97 | "<name>com.nokia.qt.interface.does.not.exist</name>" |
| 98 | "<version>1.0</version>" |
| 99 | "</interface>" |
| 100 | "</service>" |
| 101 | "</SFW>"; |
| 102 | |
| 103 | char serviceReuqestXmlStarted[] = |
| 104 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 105 | "<SFW version=\"1.1\">" |
| 106 | "<service>" |
| 107 | "<name>com.nokia.qt.tests.autostart</name>" |
| 108 | "<ipcaddress>com.nokia.qt.tests.autostart</ipcaddress>" |
| 109 | "<interface>" |
| 110 | "<name>com.nokkia.qt.tests.autostarted</name>" |
| 111 | "<version>1.0</version>" |
| 112 | "</interface>" |
| 113 | "</service>" |
| 114 | "</SFW>"; |
| 115 | |
| 116 | |
| 117 | class ServiceRequest : public QObject |
| 118 | { |
| 119 | Q_OBJECT |
| 120 | public: |
| 121 | ServiceRequest(QObject *o = 0) : QObject(o) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | Q_INVOKABLE void startService(const QString location) |
| 126 | { |
| 127 | qDebug() << "Got startService"; |
| 128 | if (location == "com.nokia.qt.tests.autostart") { |
| 129 | QMetaObject::invokeMethod(this, "sendOk", Qt::DirectConnection, Q_ARG(QString, location)); |
| 130 | } else { |
| 131 | QMetaObject::invokeMethod(this, "sendError", Qt::DirectConnection, Q_ARG(QString, location)); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | protected slots: |
| 136 | void sendError(const QString &location) |
| 137 | { |
| 138 | qDebug() << "Send fail"; |
| 139 | emit failed(QStringLiteral("Test code failing creation"), location); |
| 140 | } |
| 141 | |
| 142 | void sendOk(const QString &location) |
| 143 | { |
| 144 | emit launched(location); |
| 145 | QMetaObject::invokeMethod(this, "startNewService", Qt::DirectConnection); |
| 146 | } |
| 147 | |
| 148 | void startNewService() |
| 149 | { |
| 150 | QRemoteServiceRegister *r = new QRemoteServiceRegister(this); |
| 151 | |
| 152 | QRemoteServiceRegister::Entry pmEntry = |
| 153 | r->createEntry<ServiceRequest>( |
| 154 | QStringLiteral("com.nokia.qt.tests.autostart"), |
| 155 | QStringLiteral("com.nokkia.qt.tests.autostarted"), |
| 156 | QStringLiteral("1.0")); |
| 157 | |
| 158 | r->publishEntries(QStringLiteral("com.nokia.qt.tests.autostart")); |
| 159 | } |
| 160 | |
| 161 | signals: |
| 162 | |
| 163 | void launched(QString process); |
| 164 | void failed(QString error, QString process); |
| 165 | |
| 166 | }; |
| 167 | |
| 168 | class tst_QServiceManager_IPC: public QObject |
| 169 | { |
| 170 | Q_OBJECT |
| 171 | public: |
| 172 | tst_QServiceManager_IPC(); |
| 173 | |
| 174 | protected slots: |
| 175 | void ipcError(QService::UnrecoverableIPCError error); |
| 176 | void ipcErrorNonTest(QService::UnrecoverableIPCError error); |
| 177 | void unblockRemote(); |
| 178 | |
| 179 | private slots: |
| 180 | void initTestCase(); |
| 181 | void cleanupTestCase(); |
| 182 | void init(); |
| 183 | void cleanup(); |
| 184 | |
| 185 | void verifyIsServiceRunning(); |
| 186 | |
| 187 | void verifySharedServiceObject(); //rough count |
| 188 | void verifySharedMethods(); |
| 189 | void verifySharedMethods_data(); |
| 190 | void verifySharedProperties(); |
| 191 | void verifySharedProperties_data(); |
| 192 | |
| 193 | void verifyUniqueServiceObject(); //rough count |
| 194 | void verifyUniqueMethods(); |
| 195 | void verifyUniqueMethods_data(); |
| 196 | |
| 197 | void verifyUniqueProperties(); |
| 198 | void verifyUniqueProperties_data(); |
| 199 | |
| 200 | void verifyUniqueClassInfo(); |
| 201 | void verifyUniqueClassInfo_data(); |
| 202 | |
| 203 | void verifyUniqueEnumerator(); |
| 204 | void verifyUniqueEnumerator_data(); |
| 205 | |
| 206 | void verifyLargeDataTransfer(); |
| 207 | void verifyLargeDataTransfer_data(); |
| 208 | |
| 209 | void verifyRemoteBlockingFunctions(); |
| 210 | |
| 211 | void verifyThreadSafety(); |
| 212 | void verifyThreadSafety_data(); |
| 213 | |
| 214 | void sharedTestService(); |
| 215 | void uniqueTestService(); |
| 216 | |
| 217 | void testInvokableFunctions(); |
| 218 | void testSlotInvokation(); |
| 219 | void testSignalling(); |
| 220 | |
| 221 | void testSignalSlotOrdering(); |
| 222 | |
| 223 | void verifyServiceClass(); |
| 224 | void verifyFailures(); |
| 225 | |
| 226 | void verifyAsyncLoading(); |
| 227 | void verifyAsyncLoading_data(); |
| 228 | |
| 229 | void testServiceSecurity(); |
| 230 | |
| 231 | void testProcessLaunch(); |
| 232 | |
| 233 | void testIpcFailure(); |
| 234 | |
| 235 | signals: |
| 236 | void threadReady(); |
| 237 | void threadRun(); |
| 238 | void threadFinished(); |
| 239 | void threadError(); |
| 240 | void threadRemaining(); |
| 241 | |
| 242 | private: |
| 243 | QObject* serviceUnique; |
| 244 | QObject* serviceUniqueOther; |
| 245 | QObject* serviceShared; |
| 246 | QObject* serviceSharedOther; |
| 247 | QObject* miscTest; |
| 248 | QServiceManager* manager; |
| 249 | bool verbose; |
| 250 | bool ipcfailure; |
| 251 | }; |
| 252 | |
| 253 | tst_QServiceManager_IPC::tst_QServiceManager_IPC() |
| 254 | : serviceUnique(0) |
| 255 | , serviceUniqueOther(0) |
| 256 | , serviceShared(0) |
| 257 | , serviceSharedOther(0) |
| 258 | , miscTest(0) |
| 259 | , manager(0) |
| 260 | , verbose(false) |
| 261 | , ipcfailure(false) |
| 262 | { |
| 263 | } |
| 264 | |
| 265 | #ifdef Q_OS_WIN |
| 266 | static const char serviceBinaryC[] = "qt_sfw_example_ipc_unittest.exe"; |
| 267 | #else |
| 268 | static const char serviceBinaryC[] = "qt_sfw_example_ipc_unittest"; |
| 269 | #endif |
| 270 | |
| 271 | void tst_QServiceManager_IPC::initTestCase() |
| 272 | { |
| 273 | const QString serviceBinary = QFINDTESTDATA(serviceBinaryC); |
| 274 | QVERIFY(!serviceBinary.isEmpty()); |
| 275 | const QFileInfo serviceBinaryInfo(serviceBinary); |
| 276 | QVERIFY(serviceBinaryInfo.isExecutable()); |
| 277 | const QString serviceBinaryAbsPath = serviceBinaryInfo.absoluteFilePath(); |
| 278 | |
| 279 | const QString path = QFINDTESTDATA("xmldata/ipcexampleservice.xml"); |
| 280 | QVERIFY(!path.isEmpty()); |
| 281 | |
| 282 | //verbose = true; |
| 283 | ipcfailure = false; |
| 284 | verbose = false; |
| 285 | serviceUnique = 0; |
| 286 | serviceUniqueOther = 0; |
| 287 | serviceSharedOther = 0; |
| 288 | serviceShared = 0; |
| 289 | serviceSharedOther = 0; |
| 290 | miscTest = 0; |
| 291 | qRegisterMetaType<QServiceFilter>("QServiceFilter"); |
| 292 | qRegisterMetaTypeStreamOperators<QServiceFilter>("QServiceFilter"); |
| 293 | qRegisterMetaType<QList<QString> >("QList<QString>"); |
| 294 | qRegisterMetaTypeStreamOperators<QList<QString> >("QList<QString>"); |
| 295 | qRegisterMetaTypeStreamOperators<QVariantHash>("QVariantHash"); |
| 296 | qRegisterMetaType<QVariantHash>("QVariantHash"); |
| 297 | #ifdef SFW_USE_DBUS_BACKEND |
| 298 | qDBusRegisterMetaType<QVariantHash>(); |
| 299 | #endif |
| 300 | |
| 301 | #ifdef Q_OS_MAC |
| 302 | struct rlimit rlp; |
| 303 | int ret = getrlimit(RLIMIT_NOFILE, &rlp); |
| 304 | if (ret == -1) { |
| 305 | qWarning() << "Failed to get fd limit on mac, expect the threading test to fail"; |
| 306 | } else { |
| 307 | rlp.rlim_cur = 512; |
| 308 | ret = setrlimit(RLIMIT_NOFILE, &rlp); |
| 309 | if (ret == -1) { |
| 310 | qWarning() << "Failed to raise the number of open files to 512, expect the threading test to fail"; |
| 311 | } |
| 312 | } |
| 313 | #endif |
| 314 | |
| 315 | QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath()); |
| 316 | |
| 317 | QServiceManager* manager = new QServiceManager(this); |
| 318 | |
| 319 | const bool r = manager->addService(path); |
| 320 | if (!r) |
| 321 | qWarning() << "Cannot register IPCExampleService" << path; |
| 322 | |
| 323 | #ifdef SFW_USE_DBUS_BACKEND |
| 324 | // D-Bus auto registration |
| 325 | const QString &file = QDir::homePath() + "/.local/share/dbus-1/services/" + |
| 326 | "com.nokia.qt.ipcunittest.service"; |
| 327 | |
| 328 | QDir dir(QDir::homePath()); |
| 329 | dir.mkpath(".local/share/dbus-1/services/"); |
| 330 | QFile data(file); |
| 331 | if (data.open(QFile::WriteOnly)) { |
| 332 | QTextStream out(&data); |
| 333 | out << "[D-BUS Service]\n" |
| 334 | << "Name=com.nokia.qtmobility.sfw.IPCExampleService" << '\n' |
| 335 | << "Exec=" << serviceBinaryAbsPath << '\n'; |
| 336 | data.close(); |
| 337 | } |
| 338 | QVERIFY(data.exists()); |
| 339 | #endif // SFW_USE_DBUS_BACKEND |
| 340 | |
| 341 | #ifdef Q_OS_UNIX |
| 342 | char *p = getenv("PATH"); |
| 343 | char newpath[strlen(p)+3]; |
| 344 | strcpy(newpath, p); |
| 345 | strcat(newpath, ":."); |
| 346 | setenv("PATH", newpath, 1); |
| 347 | #endif |
| 348 | |
| 349 | //test that the service is installed |
| 350 | QList<QServiceInterfaceDescriptor> list = manager->findInterfaces("IPCExampleService"); |
| 351 | QVERIFY2(list.count() >= 7,"unit test specific IPCExampleService not registered/found" ); |
| 352 | QServiceInterfaceDescriptor d; |
| 353 | foreach (d, list) { |
| 354 | if (d.majorVersion() == 3 && d.minorVersion() == 5) { |
| 355 | serviceUnique = manager->loadInterface(d); |
| 356 | QVERIFY(serviceUnique); |
| 357 | serviceUniqueOther = manager->loadInterface(d); |
| 358 | QVERIFY(serviceUniqueOther); |
| 359 | connect(serviceUnique, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)), |
| 360 | this, SLOT(ipcErrorNonTest(QService::UnrecoverableIPCError))); |
| 361 | connect(serviceUniqueOther, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)), |
| 362 | this, SLOT(ipcErrorNonTest(QService::UnrecoverableIPCError))); |
| 363 | |
| 364 | } |
| 365 | if (d.majorVersion() == 3 && d.minorVersion() == 4) { |
| 366 | serviceShared = manager->loadInterface(d); |
| 367 | QVERIFY(serviceShared); |
| 368 | serviceSharedOther = manager->loadInterface(d); |
| 369 | connect(serviceShared, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)), |
| 370 | this, SLOT(ipcErrorNonTest(QService::UnrecoverableIPCError))); |
| 371 | QVERIFY(serviceSharedOther); |
| 372 | connect(serviceSharedOther, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)), |
| 373 | this, SLOT(ipcErrorNonTest(QService::UnrecoverableIPCError))); |
| 374 | |
| 375 | } |
| 376 | if (d.majorVersion() == 3 && d.minorVersion() == 8) { |
| 377 | miscTest = manager->loadInterface(d); |
| 378 | QVERIFY(miscTest); |
| 379 | connect(miscTest, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)), |
| 380 | this, SLOT(ipcErrorNonTest(QService::UnrecoverableIPCError))); |
| 381 | } |
| 382 | |
| 383 | } |
| 384 | |
| 385 | QString errorCode = "Cannot find service. Error: %1"; |
| 386 | errorCode = errorCode.arg(manager->error()); |
| 387 | QVERIFY2(serviceUnique,errorCode.toLatin1()); |
| 388 | QVERIFY2(serviceUniqueOther,errorCode.toLatin1()); |
| 389 | QVERIFY2(serviceShared,errorCode.toLatin1()); |
| 390 | QVERIFY2(serviceSharedOther,errorCode.toLatin1()); |
| 391 | |
| 392 | // all objects come from the same service, just need to connect to 1 signal |
| 393 | connect(serviceSharedOther, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)), this, SLOT(ipcError(QService::UnrecoverableIPCError))); |
| 394 | |
| 395 | QRemoteServiceRegister *reg = new QRemoteServiceRegister(); |
| 396 | |
| 397 | QRemoteServiceRegister::Entry pmEntry = |
| 398 | reg->createEntry<ServiceRequest>( |
| 399 | QStringLiteral("com.nokia.mt.processmanager"), |
| 400 | QStringLiteral("com.nokia.mt.processmanager.ServiceRequest"), |
| 401 | QStringLiteral("1.0")); |
| 402 | |
| 403 | reg->publishEntries("com.nokia.mt.processmanager.ServiceRequestSocket"); |
| 404 | |
| 405 | QByteArray ba(serviceReuqestXml); |
| 406 | QBuffer b(&ba); |
| 407 | manager->removeService(QStringLiteral("com.nokia.mt.processmanager")); |
| 408 | manager->addService(&b); |
| 409 | |
| 410 | QByteArray baFake(serviceReuqestXmlFake); |
| 411 | QBuffer bFake(&baFake); |
| 412 | manager->removeService(QStringLiteral("com.nokia.qt.does.not.exists")); |
| 413 | manager->addService(&bFake); |
| 414 | |
| 415 | QByteArray baAutoStart(serviceReuqestXmlStarted); |
| 416 | QBuffer bAutoStart(&baAutoStart); |
| 417 | manager->removeService(QStringLiteral("com.nokia.qt.tests.autostart")); |
| 418 | manager->addService(&bAutoStart); |
| 419 | |
| 420 | } |
| 421 | |
| 422 | void tst_QServiceManager_IPC::ipcError(QService::UnrecoverableIPCError err) |
| 423 | { |
| 424 | Q_UNUSED(err); |
| 425 | ipcfailure = true; |
| 426 | } |
| 427 | |
| 428 | // unexpected ipc failures |
| 429 | void tst_QServiceManager_IPC::ipcErrorNonTest(QService::UnrecoverableIPCError err) |
| 430 | { |
| 431 | Q_UNUSED(err); |
| 432 | } |
| 433 | |
| 434 | void tst_QServiceManager_IPC::cleanupTestCase() |
| 435 | { |
| 436 | #ifdef SFW_USE_DBUS_BACKEND |
| 437 | const QString &file = QDir::homePath() + "/.local/share/dbus-1/services/" + |
| 438 | "com.nokia.qt.ipcunittest.service"; |
| 439 | QFile::remove(file); |
| 440 | #endif |
| 441 | |
| 442 | if (serviceUnique) { |
| 443 | delete serviceUnique; |
| 444 | } |
| 445 | |
| 446 | if (serviceUniqueOther) { |
| 447 | delete serviceUniqueOther; |
| 448 | } |
| 449 | |
| 450 | if (serviceShared) { |
| 451 | delete serviceShared; |
| 452 | } |
| 453 | |
| 454 | if (serviceSharedOther) { |
| 455 | delete serviceSharedOther; |
| 456 | } |
| 457 | |
| 458 | // clean up the unit, don't leave it registered |
| 459 | QServiceManager m; |
| 460 | m.removeService("IPCExampleService"); |
| 461 | m.removeService(QStringLiteral("com.nokia.mt.processmanager")); |
| 462 | m.removeService(QStringLiteral("com.nokia.qt.does.not.exists")); |
| 463 | m.removeService(QStringLiteral("com.nokia.qt.tests.autostart")); |
| 464 | } |
| 465 | |
| 466 | void tst_QServiceManager_IPC::init() |
| 467 | { |
| 468 | } |
| 469 | |
| 470 | void tst_QServiceManager_IPC::cleanup() |
| 471 | { |
| 472 | } |
| 473 | |
| 474 | void tst_QServiceManager_IPC::verifyIsServiceRunning() |
| 475 | { |
| 476 | #if defined(Q_OS_UNIX) && !defined(SFW_USE_DBUS_BACKEND) |
| 477 | |
| 478 | QServiceManager m; |
| 479 | |
| 480 | QCOMPARE(m.isInterfaceRunning("com.nokia.qt.ipcunittest"), true); |
| 481 | QCOMPARE(m.isInterfaceRunning("com.nokis.qt.ipcunittest.that.does.not.exsist.and.should.not.be.created"), false); |
| 482 | |
| 483 | QList<QServiceInterfaceDescriptor> list = m.findInterfaces("IPCExampleService"); |
| 484 | QServiceInterfaceDescriptor d; |
| 485 | foreach (d, list) { |
| 486 | QCOMPARE(m.isInterfaceRunning(d), true); |
| 487 | } |
| 488 | #else |
| 489 | QSKIP("isInterfaceRunning needs more testing on supported platforms"); |
| 490 | #endif |
| 491 | } |
| 492 | |
| 493 | void tst_QServiceManager_IPC::sharedTestService() |
| 494 | { |
| 495 | const QMetaObject *meta = serviceShared->metaObject(); |
| 496 | const QMetaObject *metaOther = serviceSharedOther->metaObject(); |
| 497 | |
| 498 | // test changes on the property 'value' |
| 499 | QMetaProperty prop = meta->property(1); |
| 500 | QMetaProperty propOther = metaOther->property(1); |
| 501 | |
| 502 | // check that the property values are the same before and after an edit |
| 503 | QCOMPARE(prop.read(serviceShared), propOther.read(serviceSharedOther)); |
| 504 | prop.write(serviceShared, "Shared"); |
| 505 | // write does not block so the QCOMPARE below may result in serviceSharedOther being read before |
| 506 | // the new value has been sent to the service. To avoid this, read the property back now |
| 507 | (void)prop.read(serviceShared); // flush |
| 508 | QCOMPARE(prop.read(serviceShared), propOther.read(serviceSharedOther)); |
| 509 | prop.reset(serviceShared); |
| 510 | |
| 511 | // test changes through a method call |
| 512 | uint hash, hashOther; |
| 513 | QMetaObject::invokeMethod(serviceShared, "setConfirmationHash", |
| 514 | Q_ARG(uint, 0)); |
| 515 | QMetaObject::invokeMethod(serviceShared, "slotConfirmation", |
| 516 | Q_RETURN_ARG(uint, hash)); |
| 517 | QMetaObject::invokeMethod(serviceSharedOther, "setConfirmationHash", |
| 518 | Q_ARG(uint, 0)); |
| 519 | QMetaObject::invokeMethod(serviceSharedOther, "slotConfirmation", |
| 520 | Q_RETURN_ARG(uint, hashOther)); |
| 521 | QCOMPARE(hash, (uint)0); |
| 522 | QCOMPARE(hashOther, (uint)0); |
| 523 | |
| 524 | // check that the method values are the same after an edit |
| 525 | QMetaObject::invokeMethod(serviceShared, "setConfirmationHash", |
| 526 | Q_ARG(uint, 1)); |
| 527 | QMetaObject::invokeMethod(serviceShared, "slotConfirmation", |
| 528 | Q_RETURN_ARG(uint, hash)); |
| 529 | QMetaObject::invokeMethod(serviceSharedOther, "slotConfirmation", |
| 530 | Q_RETURN_ARG(uint, hashOther)); |
| 531 | QCOMPARE(hash, (uint)1); |
| 532 | QCOMPARE(hashOther, (uint)1); |
| 533 | } |
| 534 | |
| 535 | void tst_QServiceManager_IPC::uniqueTestService() |
| 536 | { |
| 537 | const QMetaObject *meta = serviceUnique->metaObject(); |
| 538 | const QMetaObject *metaOther = serviceUniqueOther->metaObject(); |
| 539 | |
| 540 | // test changes on the property 'value' |
| 541 | QMetaProperty prop = meta->property(1); |
| 542 | QMetaProperty propOther = metaOther->property(1); |
| 543 | |
| 544 | // check that the property values are not the same after an edit |
| 545 | QCOMPARE(prop.read(serviceUnique), propOther.read(serviceUniqueOther)); |
| 546 | prop.write(serviceUnique, "Unique"); |
| 547 | QVERIFY(prop.read(serviceUnique) != propOther.read(serviceUniqueOther)); |
| 548 | prop.reset(serviceUnique); |
| 549 | |
| 550 | // test changes through a method call |
| 551 | uint hash, hashOther; |
| 552 | QMetaObject::invokeMethod(serviceUnique, "setConfirmationHash", |
| 553 | Q_ARG(uint, 0)); |
| 554 | QMetaObject::invokeMethod(serviceUnique, "slotConfirmation", |
| 555 | Q_RETURN_ARG(uint, hash)); |
| 556 | QMetaObject::invokeMethod(serviceUniqueOther, "setConfirmationHash", |
| 557 | Q_ARG(uint, 0)); |
| 558 | QMetaObject::invokeMethod(serviceUniqueOther, "slotConfirmation", |
| 559 | Q_RETURN_ARG(uint, hashOther)); |
| 560 | QCOMPARE(hash, (uint)0); |
| 561 | QCOMPARE(hashOther, (uint)0); |
| 562 | |
| 563 | // check that the method values are not the same after an edit |
| 564 | QMetaObject::invokeMethod(serviceUnique, "setConfirmationHash", |
| 565 | Q_ARG(uint, 1)); |
| 566 | QMetaObject::invokeMethod(serviceUnique, "slotConfirmation", |
| 567 | Q_RETURN_ARG(uint, hash)); |
| 568 | QMetaObject::invokeMethod(serviceUniqueOther, "slotConfirmation", |
| 569 | Q_RETURN_ARG(uint, hashOther)); |
| 570 | QCOMPARE(hash, (uint)1); |
| 571 | QCOMPARE(hashOther, (uint)0); |
| 572 | } |
| 573 | |
| 574 | void tst_QServiceManager_IPC::verifySharedServiceObject() |
| 575 | { |
| 576 | QVERIFY(serviceShared != 0); |
| 577 | const QMetaObject* mo = serviceShared->metaObject(); |
| 578 | QCOMPARE(mo->className(), "SharedTestService"); |
| 579 | QVERIFY(mo->superClass()); |
| 580 | QCOMPARE(mo->superClass()->className(), "QServiceProxyBase"); |
| 581 | QCOMPARE(mo->methodCount()-mo-> methodOffset(), 19); |
| 582 | QCOMPARE(mo->methodCount(), 25); //20 meta functions available |
| 583 | //actual function presence will be tested later |
| 584 | |
| 585 | // for (int i = 0; i < mo->methodCount(); i++) { |
| 586 | // qDebug() << "Methods" << i << mo->method(i).methodSignature(); |
| 587 | // } |
| 588 | |
| 589 | //test properties |
| 590 | QCOMPARE(mo->propertyCount()-mo->propertyOffset(), 1); |
| 591 | QCOMPARE(mo->propertyCount(), 2); |
| 592 | |
| 593 | QCOMPARE(mo->enumeratorCount()-mo->enumeratorOffset(), 0); |
| 594 | QCOMPARE(mo->enumeratorCount(), 0); |
| 595 | |
| 596 | QCOMPARE(mo->classInfoCount()-mo->classInfoOffset(), 0); |
| 597 | QCOMPARE(mo->classInfoCount(), 0); |
| 598 | |
| 599 | if (verbose) { |
| 600 | qDebug() << "ServiceObject class: " << mo->className() << mo->superClass() << mo->superClass()->className(); |
| 601 | qDebug() << "------------------- Meta Methods -----------"; |
| 602 | qDebug() << "Methods:" << mo->methodCount()- mo->methodOffset() << "(" << mo->methodCount() << ")"; |
| 603 | for (int i=0; i< mo->methodCount(); i++) { |
| 604 | QMetaMethod method = mo->method(i); |
| 605 | QString type; |
| 606 | switch (method.methodType()) { |
| 607 | case QMetaMethod::Signal: |
| 608 | type = "signal"; break; |
| 609 | case QMetaMethod::Slot: |
| 610 | type = "slot"; break; |
| 611 | case QMetaMethod::Constructor: |
| 612 | type = "constrcutor"; break; |
| 613 | case QMetaMethod::Method: |
| 614 | type = "method"; break; |
| 615 | } |
| 616 | qDebug() << " " << i << "." << method.methodSignature() << type; |
| 617 | } |
| 618 | |
| 619 | qDebug() << "------------------- Meta Properties --------"; |
| 620 | qDebug() << "Properties:" << mo->propertyCount()- mo->propertyOffset() << "(" << mo->propertyCount() << ")"; |
| 621 | for (int i=0; i< mo->propertyCount(); i++) { |
| 622 | QMetaProperty property = mo->property(i); |
| 623 | QString info = "Readable: %1 Resettable: %2 Writeable: %3 Designable: %4 Scriptable: %5 User: %6 Stored: %7 Constant: %8 Final: %9 HasNotify: %10 EnumType: %11 FlagType: %12"; |
| 624 | info = info.arg(property.isReadable()).arg(property.isResettable()).arg(property.isWritable()); |
| 625 | info = info.arg(property.isDesignable()).arg(property.isScriptable()).arg(property.isUser()); |
| 626 | info = info.arg(property.isStored()).arg(property.isConstant()).arg(property.isFinal()); |
| 627 | info = info.arg(property.hasNotifySignal()).arg(property.isEnumType()).arg(property.isFlagType()); |
| 628 | |
| 629 | qDebug() << " " << i << "." << property.name() << "Type:" << property.typeName() << info; |
| 630 | } |
| 631 | |
| 632 | qDebug() << "------------------- Meta Enumerators --------"; |
| 633 | qDebug() << "Enums:" << mo->enumeratorCount()- mo->enumeratorOffset() << "(" << mo->enumeratorCount() << ")"; |
| 634 | |
| 635 | qDebug() << "------------------- Meta class info ---------"; |
| 636 | qDebug() << "ClassInfos:" << mo->classInfoCount()- mo->classInfoOffset() << "(" << mo->classInfoCount() << ")"; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | void tst_QServiceManager_IPC::verifySharedMethods_data() |
| 641 | { |
| 642 | QTest::addColumn<QByteArray>("signature"); |
| 643 | QTest::addColumn<int>("metaMethodType"); |
| 644 | QTest::addColumn<QByteArray>("returnType"); |
| 645 | |
| 646 | //list of all slots, signals and invokable functions |
| 647 | QTest::newRow("signalWithIntParam(int)") |
| 648 | << QByteArray("signalWithIntParam(int)") << (int)( QMetaMethod::Signal) << QByteArray("void"); |
| 649 | QTest::newRow("signalWithVariousParam(QVariant,QString,QServiceFilter)") |
| 650 | << QByteArray("signalWithVariousParam(QVariant,QString,QServiceFilter)") << (int)( QMetaMethod::Signal) << QByteArray("void"); |
| 651 | QTest::newRow("valueChanged()") |
| 652 | << QByteArray("valueChanged()") << (int)( QMetaMethod::Signal) << QByteArray("void"); |
| 653 | QTest::newRow("triggerSignalWithIntParam()") |
| 654 | << QByteArray("triggerSignalWithIntParam()") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 655 | QTest::newRow("triggerSignalWithVariousParam()") |
| 656 | << QByteArray("triggerSignalWithVariousParam()") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 657 | QTest::newRow("triggerSignalWithIntParamExecute()") |
| 658 | << QByteArray("triggerSignalWithIntParamExecute()") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 659 | QTest::newRow("triggerSignalWithVariousParamExecute()") |
| 660 | << QByteArray("triggerSignalWithVariousParamExecute()") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 661 | QTest::newRow("testSlot()") |
| 662 | << QByteArray("testSlot()") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 663 | QTest::newRow("testSlotWithArgs(QByteArray,int,QVariant)") |
| 664 | << QByteArray("testSlotWithArgs(QByteArray,int,QVariant)") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 665 | QTest::newRow("testSlotWithCustomArg(QServiceFilter)") |
| 666 | << QByteArray("testSlotWithCustomArg(QServiceFilter)") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 667 | QTest::newRow("testSlotWidthComplexArg(QVariantHash)") |
| 668 | << QByteArray("testSlotWithComplexArg(QVariantHash)") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 669 | |
| 670 | //QServiceInterfaceDescriptor has not been declared as meta type |
| 671 | QTest::newRow("testSlotWithUnknownArg(QServiceInterfaceDescriptor)") |
| 672 | << QByteArray("testSlotWithUnknownArg(QServiceInterfaceDescriptor)") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 673 | QTest::newRow("testFunctionWithReturnValue(int)") |
| 674 | << QByteArray("testFunctionWithReturnValue(int)") << (int)( QMetaMethod::Method) << QByteArray("QString"); |
| 675 | QTest::newRow("testFunctionWithVariantReturnValue(QVariant)") |
| 676 | << QByteArray("testFunctionWithVariantReturnValue(QVariant)") << (int)( QMetaMethod::Method) << QByteArray("QVariant"); |
| 677 | QTest::newRow("testFunctionWithCustomReturnValue()") |
| 678 | << QByteArray("testFunctionWithCustomReturnValue()") << (int)( QMetaMethod::Method) << QByteArray("QServiceFilter"); |
| 679 | QTest::newRow("slotConfirmation()") |
| 680 | << QByteArray("slotConfirmation()") << (int)( QMetaMethod::Method) << QByteArray("uint"); |
| 681 | QTest::newRow("setConfirmationHash(uint)") |
| 682 | << QByteArray("setConfirmationHash(uint)") << (int)( QMetaMethod::Method) << QByteArray("void"); |
| 683 | } |
| 684 | |
| 685 | void tst_QServiceManager_IPC::verifySharedMethods() |
| 686 | { |
| 687 | QVERIFY(serviceShared); |
| 688 | QFETCH(QByteArray, signature); |
| 689 | QFETCH(int, metaMethodType); |
| 690 | QFETCH(QByteArray, returnType); |
| 691 | |
| 692 | const QMetaObject* meta = serviceShared->metaObject(); |
| 693 | const int index = meta->indexOfMethod(signature.constData()); |
| 694 | QVERIFY(index>=0); |
| 695 | QMetaMethod method = meta->method(index); |
| 696 | QCOMPARE(metaMethodType, (int)method.methodType()); |
| 697 | QCOMPARE(returnType, QByteArray(method.typeName())); |
| 698 | } |
| 699 | |
| 700 | void tst_QServiceManager_IPC::verifySharedProperties_data() |
| 701 | { |
| 702 | QTest::addColumn<QByteArray>("signature"); |
| 703 | QTest::addColumn<QString>("typeName"); |
| 704 | QTest::addColumn<qint16>("flags"); |
| 705 | QTest::addColumn<QVariant>("defaultValue"); |
| 706 | QTest::addColumn<QVariant>("writeValue"); |
| 707 | QTest::addColumn<QVariant>("expectedReturnValue"); |
| 708 | |
| 709 | // |
| 710 | // bit order: |
| 711 | // 0 isWritable - 1 isUser - 2 isStored - 3 isScriptable |
| 712 | // 4 isResettable - 5 isReadable - 6 isFlagType - 7 isFinal |
| 713 | // 8 isEnumType - 9 isDesignable - 10 isConstant - 11 hasNotifySgnal |
| 714 | // |
| 715 | // for more details see verifyProperties() |
| 716 | // |
| 717 | |
| 718 | QTest::newRow("value property") << QByteArray("value") |
| 719 | << QString("QString") << qint16(0x0A3D) << QVariant(QString("FFF")) << QVariant(QString("GGG")) << QVariant(QString("GGG")); |
| 720 | } |
| 721 | |
| 722 | void tst_QServiceManager_IPC::verifySharedProperties() |
| 723 | { |
| 724 | QVERIFY(serviceShared); |
| 725 | QFETCH(QByteArray, signature); |
| 726 | QFETCH(QString, typeName); |
| 727 | QFETCH(qint16, flags); |
| 728 | QFETCH(QVariant, defaultValue); |
| 729 | QFETCH(QVariant, writeValue); |
| 730 | QFETCH(QVariant, expectedReturnValue); |
| 731 | |
| 732 | const QMetaObject* meta = serviceShared->metaObject(); |
| 733 | const int index = meta->indexOfProperty(signature.constData()); |
| 734 | QVERIFY(index>=0); |
| 735 | QMetaProperty property = meta->property(index); |
| 736 | QVERIFY(property.isValid()); |
| 737 | QCOMPARE(QString(property.typeName()), typeName); |
| 738 | |
| 739 | QVERIFY( property.isWritable() == (bool) (flags & 0x0001) ); |
| 740 | QVERIFY( property.isUser() == (bool) (flags & 0x0002) ); |
| 741 | QVERIFY( property.isStored() == (bool) (flags & 0x0004) ); |
| 742 | QVERIFY( property.isScriptable() == (bool) (flags & 0x0008) ); |
| 743 | QVERIFY( property.isResettable() == (bool) (flags & 0x0010) ); |
| 744 | QVERIFY( property.isReadable() == (bool) (flags & 0x0020) ); |
| 745 | QVERIFY( property.isFlagType() == (bool) (flags & 0x0040) ); |
| 746 | QVERIFY( property.isFinal() == (bool) (flags & 0x0080) ); |
| 747 | QVERIFY( property.isEnumType() == (bool) (flags & 0x0100) ); |
| 748 | QVERIFY( property.isDesignable() == (bool) (flags & 0x0200) ); |
| 749 | QVERIFY( property.isConstant() == (bool) (flags & 0x0400) ); |
| 750 | QVERIFY( property.hasNotifySignal() == (bool) (flags & 0x0800) ); |
| 751 | |
| 752 | if (property.isReadable()) { |
| 753 | QCOMPARE(defaultValue, serviceShared->property(signature)); |
| 754 | if (property.isWritable()) { |
| 755 | serviceShared->setProperty(signature, writeValue); |
| 756 | QCOMPARE(expectedReturnValue, serviceShared->property(signature)); |
| 757 | if (property.isResettable()) { |
| 758 | property.reset(serviceShared); |
| 759 | QCOMPARE(defaultValue, serviceShared->property(signature)); |
| 760 | } |
| 761 | serviceShared->setProperty(signature, defaultValue); |
| 762 | QCOMPARE(defaultValue, serviceShared->property(signature)); |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | void tst_QServiceManager_IPC::verifyUniqueServiceObject() |
| 768 | { |
| 769 | QVERIFY(serviceUnique != 0); |
| 770 | const QMetaObject* mo = serviceUnique->metaObject(); |
| 771 | QCOMPARE(mo->className(), "UniqueTestService"); |
| 772 | QVERIFY(mo->superClass()); |
| 773 | QCOMPARE(mo->superClass()->className(), "QServiceProxyBase"); |
| 774 | // TODO adding the ipc failure signal seems to break these |
| 775 | QCOMPARE(mo->methodCount()-mo-> methodOffset(), 27); // 28+1 added signal for error signal added by library |
| 776 | QCOMPARE(mo->methodCount(), 33); //33 meta functions available + 1 signal |
| 777 | //actual function presence will be tested later |
| 778 | |
| 779 | // for (int i = 0; i < mo->methodCount(); i++) { |
| 780 | // qDebug() << "Methods" << i << mo->method(i).methodSignature(); |
| 781 | // } |
| 782 | |
| 783 | //test properties |
| 784 | QCOMPARE(mo->propertyCount()-mo->propertyOffset(), 5); |
| 785 | QCOMPARE(mo->propertyCount(), 6); |
| 786 | |
| 787 | QCOMPARE(mo->enumeratorCount()-mo->enumeratorOffset(), 3); |
| 788 | QCOMPARE(mo->enumeratorCount(), 3); |
| 789 | |
| 790 | QCOMPARE(mo->classInfoCount()-mo->classInfoOffset(), 2); |
| 791 | QCOMPARE(mo->classInfoCount(), 2); |
| 792 | |
| 793 | if (verbose) { |
| 794 | qDebug() << "ServiceObject class: " << mo->className() << mo->superClass() << mo->superClass()->className(); |
| 795 | qDebug() << "------------------- Meta Methods -----------"; |
| 796 | qDebug() << "Methods:" << mo->methodCount()- mo->methodOffset() << "(" << mo->methodCount() << ")"; |
| 797 | for (int i=0; i< mo->methodCount(); i++) { |
| 798 | QMetaMethod method = mo->method(i); |
| 799 | QString type; |
| 800 | switch (method.methodType()) { |
| 801 | case QMetaMethod::Signal: |
| 802 | type = "signal"; break; |
| 803 | case QMetaMethod::Slot: |
| 804 | type = "slot"; break; |
| 805 | case QMetaMethod::Constructor: |
| 806 | type = "constrcutor"; break; |
| 807 | case QMetaMethod::Method: |
| 808 | type = "method"; break; |
| 809 | } |
| 810 | qDebug() << " " << i << "." << method.methodSignature() << type; |
| 811 | } |
| 812 | |
| 813 | qDebug() << "------------------- Meta Properties --------"; |
| 814 | qDebug() << "Properties:" << mo->propertyCount()- mo->propertyOffset() << "(" << mo->propertyCount() << ")"; |
| 815 | for (int i=0; i< mo->propertyCount(); i++) { |
| 816 | QMetaProperty property = mo->property(i); |
| 817 | QString info = "Readable: %1 Resettable: %2 Writeable: %3 Designable: %4 Scriptable: %5 User: %6 Stored: %7 Constant: %8 Final: %9 HasNotify: %10 EnumType: %11 FlagType: %12"; |
| 818 | info = info.arg(property.isReadable()).arg(property.isResettable()).arg(property.isWritable()); |
| 819 | info = info.arg(property.isDesignable()).arg(property.isScriptable()).arg(property.isUser()); |
| 820 | info = info.arg(property.isStored()).arg(property.isConstant()).arg(property.isFinal()); |
| 821 | info = info.arg(property.hasNotifySignal()).arg(property.isEnumType()).arg(property.isFlagType()); |
| 822 | |
| 823 | qDebug() << " " << i << "." << property.name() << "Type:" << property.typeName() << info; |
| 824 | } |
| 825 | |
| 826 | qDebug() << "------------------- Meta Enumerators --------"; |
| 827 | qDebug() << "Enums:" << mo->enumeratorCount()- mo->enumeratorOffset() << "(" << mo->enumeratorCount() << ")"; |
| 828 | for (int i=0; i< mo->enumeratorCount(); i++) { |
| 829 | QMetaEnum e = mo->enumerator(i); |
| 830 | qDebug() << " " << i << "." << e.name() << "Scope:" << e.scope() << "KeyCount: " << e.keyCount(); |
| 831 | for (int j = 0; j<e.keyCount(); j++) |
| 832 | qDebug() << " " << e.key(j) << " - " << e.value(j); |
| 833 | } |
| 834 | |
| 835 | qDebug() << "------------------- Meta class info ---------"; |
| 836 | qDebug() << "ClassInfos:" << mo->classInfoCount()- mo->classInfoOffset() << "(" << mo->classInfoCount() << ")"; |
| 837 | for (int i=0; i< mo->classInfoCount(); i++) { |
| 838 | QMetaClassInfo info = mo->classInfo(i); |
| 839 | qDebug() << " " << i << "." << info.name() << "Value:" << info.value(); |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | void tst_QServiceManager_IPC::verifyUniqueMethods_data() |
| 845 | { |
| 846 | QTest::addColumn<QByteArray>("signature"); |
| 847 | QTest::addColumn<int>("metaMethodType"); |
| 848 | QTest::addColumn<QByteArray>("returnType"); |
| 849 | |
| 850 | //list of all slots, signals and invokable functions |
| 851 | QTest::newRow("signalWithIntParam(int)") |
| 852 | << QByteArray("signalWithIntParam(int)") << (int)( QMetaMethod::Signal) << QByteArray("void"); |
| 853 | QTest::newRow("signalWithVariousParam(QVariant,QString,QServiceFilter,QVariant)") |
| 854 | << QByteArray("signalWithVariousParam(QVariant,QString,QServiceFilter,QVariant)") << (int)( QMetaMethod::Signal) << QByteArray("void"); |
| 855 | QTest::newRow("valueChanged()") |
| 856 | << QByteArray("valueChanged()") << (int)( QMetaMethod::Signal) << QByteArray("void"); |
| 857 | QTest::newRow("priorityChanged()") |
| 858 | << QByteArray("priorityChanged()") << (int)( QMetaMethod::Signal) << QByteArray("void"); |
| 859 | QTest::newRow("triggerSignalWithIntParam()") |
| 860 | << QByteArray("triggerSignalWithIntParam()") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 861 | QTest::newRow("triggerSignalWithVariousParam()") |
| 862 | << QByteArray("triggerSignalWithVariousParam()") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 863 | QTest::newRow("triggerSignalWithIntParamExecute()") |
| 864 | << QByteArray("triggerSignalWithIntParamExecute()") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 865 | QTest::newRow("triggerSignalWithVariousParamExecute()") |
| 866 | << QByteArray("triggerSignalWithVariousParamExecute()") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 867 | QTest::newRow("testSlot()") |
| 868 | << QByteArray("testSlot()") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 869 | QTest::newRow("testSlotWithArgs(QByteArray,int,QVariant)") |
| 870 | << QByteArray("testSlotWithArgs(QByteArray,int,QVariant)") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 871 | QTest::newRow("testSlotWithCustomArg(QServiceFilter)") |
| 872 | << QByteArray("testSlotWithCustomArg(QServiceFilter)") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 873 | |
| 874 | //QServiceInterfaceDescriptor has not been declared as meta type |
| 875 | QTest::newRow("testSlotWithUnknownArg(QServiceInterfaceDescriptor)") |
| 876 | << QByteArray("testSlotWithUnknownArg(QServiceInterfaceDescriptor)") << (int)( QMetaMethod::Slot) << QByteArray("void"); |
| 877 | QTest::newRow("testFunctionWithReturnValue(int)") |
| 878 | << QByteArray("testFunctionWithReturnValue(int)") << (int)( QMetaMethod::Method) << QByteArray("QString"); |
| 879 | QTest::newRow("testFunctionWithVariantReturnValue(QVariant)") |
| 880 | << QByteArray("testFunctionWithVariantReturnValue(QVariant)") << (int)( QMetaMethod::Method) << QByteArray("QVariant"); |
| 881 | QTest::newRow("testFunctionWithCustomReturnValue()") |
| 882 | << QByteArray("testFunctionWithCustomReturnValue()") << (int)( QMetaMethod::Method) << QByteArray("QServiceFilter"); |
| 883 | QTest::newRow("slotConfirmation()") |
| 884 | << QByteArray("slotConfirmation()") << (int)( QMetaMethod::Method) << QByteArray("uint"); |
| 885 | QTest::newRow("setConfirmationHash(uint)") |
| 886 | << QByteArray("setConfirmationHash(uint)") << (int)( QMetaMethod::Method) << QByteArray("void"); |
| 887 | } |
| 888 | |
| 889 | void tst_QServiceManager_IPC::verifyUniqueMethods() |
| 890 | { |
| 891 | QVERIFY(serviceUnique); |
| 892 | QFETCH(QByteArray, signature); |
| 893 | QFETCH(int, metaMethodType); |
| 894 | QFETCH(QByteArray, returnType); |
| 895 | |
| 896 | const QMetaObject* meta = serviceUnique->metaObject(); |
| 897 | const int index = meta->indexOfMethod(signature.constData()); |
| 898 | QVERIFY(index>=0); |
| 899 | QMetaMethod method = meta->method(index); |
| 900 | QCOMPARE((int)method.methodType(), metaMethodType); |
| 901 | QCOMPARE(QByteArray(method.typeName()), returnType); |
| 902 | } |
| 903 | |
| 904 | void tst_QServiceManager_IPC::verifyUniqueProperties_data() |
| 905 | { |
| 906 | QTest::addColumn<QByteArray>("signature"); |
| 907 | QTest::addColumn<QString>("typeName"); |
| 908 | QTest::addColumn<qint16>("flags"); |
| 909 | QTest::addColumn<QVariant>("defaultValue"); |
| 910 | QTest::addColumn<QVariant>("writeValue"); |
| 911 | QTest::addColumn<QVariant>("expectedReturnValue"); |
| 912 | |
| 913 | // |
| 914 | // bit order: |
| 915 | // 0 isWritable - 1 isUser - 2 isStored - 3 isScriptable |
| 916 | // 4 isResettable - 5 isReadable - 6 isFlagType - 7 isFinal |
| 917 | // 8 isEnumType - 9 isDesignable - 10 isConstant - 11 hasNotifySgnal |
| 918 | // |
| 919 | // for more details see verifyProperties() |
| 920 | // |
| 921 | |
| 922 | QTest::newRow("value property") << QByteArray("value") |
| 923 | << QString("QString") << qint16(0x0A3D) << QVariant(QString("FFF")) << QVariant(QString("GGG")) << QVariant(QString("GGG")); |
| 924 | #ifndef SFW_USE_DBUS_BACKEND |
| 925 | // ENUMS DONT WORK OVER DBUS |
| 926 | QTest::newRow("priority property") << QByteArray("priority") |
| 927 | << QString("Priority") << qint16(0x0B2D) << QVariant((int)0) << QVariant("Low") << QVariant((int)1) ; |
| 928 | QTest::newRow("serviceFlags property") << QByteArray("serviceFlags") |
| 929 | << QString("ServiceFlag") << qint16(0x036D) << QVariant((int)4) << QVariant("FirstBit|ThirdBit") << QVariant((int)5); |
| 930 | #endif |
| 931 | } |
| 932 | |
| 933 | void tst_QServiceManager_IPC::verifyUniqueProperties() |
| 934 | { |
| 935 | QVERIFY(serviceUnique); |
| 936 | QFETCH(QByteArray, signature); |
| 937 | QFETCH(QString, typeName); |
| 938 | QFETCH(qint16, flags); |
| 939 | QFETCH(QVariant, defaultValue); |
| 940 | QFETCH(QVariant, writeValue); |
| 941 | QFETCH(QVariant, expectedReturnValue); |
| 942 | |
| 943 | const QMetaObject* meta = serviceUnique->metaObject(); |
| 944 | const int index = meta->indexOfProperty(signature.constData()); |
| 945 | QVERIFY(index>=0); |
| 946 | QMetaProperty property = meta->property(index); |
| 947 | QVERIFY(property.isValid()); |
| 948 | QCOMPARE(QString(property.typeName()), typeName); |
| 949 | |
| 950 | QVERIFY( property.isWritable() == (bool) (flags & 0x0001) ); |
| 951 | QVERIFY( property.isUser() == (bool) (flags & 0x0002) ); |
| 952 | QVERIFY( property.isStored() == (bool) (flags & 0x0004) ); |
| 953 | QVERIFY( property.isScriptable() == (bool) (flags & 0x0008) ); |
| 954 | QVERIFY( property.isResettable() == (bool) (flags & 0x0010) ); |
| 955 | QVERIFY( property.isReadable() == (bool) (flags & 0x0020) ); |
| 956 | QVERIFY( property.isFlagType() == (bool) (flags & 0x0040) ); |
| 957 | QVERIFY( property.isFinal() == (bool) (flags & 0x0080) ); |
| 958 | QVERIFY( property.isEnumType() == (bool) (flags & 0x0100) ); |
| 959 | QVERIFY( property.isDesignable() == (bool) (flags & 0x0200) ); |
| 960 | QVERIFY( property.isConstant() == (bool) (flags & 0x0400) ); |
| 961 | QVERIFY( property.hasNotifySignal() == (bool) (flags & 0x0800) ); |
| 962 | |
| 963 | if (property.isReadable()) { |
| 964 | QCOMPARE(defaultValue, serviceUnique->property(signature)); |
| 965 | if (property.isWritable()) { |
| 966 | serviceUnique->setProperty(signature, writeValue); |
| 967 | QCOMPARE(expectedReturnValue, serviceUnique->property(signature)); |
| 968 | if (property.isResettable()) { |
| 969 | property.reset(serviceUnique); |
| 970 | QCOMPARE(defaultValue, serviceUnique->property(signature)); |
| 971 | } |
| 972 | serviceUnique->setProperty(signature, defaultValue); |
| 973 | QCOMPARE(defaultValue, serviceUnique->property(signature)); |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | void tst_QServiceManager_IPC::verifyUniqueClassInfo_data() |
| 979 | { |
| 980 | QTest::addColumn<QString>("classInfoKey"); |
| 981 | QTest::addColumn<QString>("classInfoValue"); |
| 982 | |
| 983 | QTest::newRow("UniqueTestService") << QString("UniqueTestService") << QString("First test"); |
| 984 | QTest::newRow("Key") << QString("Key") << QString("Value"); |
| 985 | |
| 986 | } |
| 987 | void tst_QServiceManager_IPC::verifyUniqueClassInfo() |
| 988 | { |
| 989 | QFETCH(QString, classInfoKey); |
| 990 | QFETCH(QString, classInfoValue); |
| 991 | |
| 992 | const QMetaObject* meta = serviceUnique->metaObject(); |
| 993 | const int index = meta->indexOfClassInfo(classInfoKey.toLatin1().constData()); |
| 994 | |
| 995 | QMetaClassInfo info = meta->classInfo(index); |
| 996 | QCOMPARE(classInfoKey, QString(info.name())); |
| 997 | QCOMPARE(classInfoValue, QString(info.value())); |
| 998 | |
| 999 | } |
| 1000 | |
| 1001 | Q_DECLARE_METATYPE(QList<int> ) |
| 1002 | void tst_QServiceManager_IPC::verifyUniqueEnumerator_data() |
| 1003 | { |
| 1004 | QTest::addColumn<QString>("enumName"); |
| 1005 | QTest::addColumn<QString>("enumScope"); |
| 1006 | QTest::addColumn<int>("enumKeyCount"); |
| 1007 | QTest::addColumn<bool>("enumIsFlag"); |
| 1008 | QTest::addColumn<QStringList>("enumKeyNames"); |
| 1009 | QTest::addColumn<QList<int> >("enumKeyValues"); |
| 1010 | |
| 1011 | |
| 1012 | QStringList keynames; |
| 1013 | keynames << "FirstBit" << "SecondBit" << "ThirdBit"; |
| 1014 | QList<int> values; |
| 1015 | values << 1 << 2 << 4; |
| 1016 | QTest::newRow("ServiceFlag") << QString("ServiceFlag") << QString("UniqueTestService") |
| 1017 | << 3 << true << keynames << values; |
| 1018 | QTest::newRow("ServiceFlags") << QString("ServiceFlags") << QString("UniqueTestService") |
| 1019 | << 3 << true << keynames << values; |
| 1020 | |
| 1021 | keynames.clear(); |
| 1022 | values.clear(); |
| 1023 | |
| 1024 | keynames << "High" << "Low" << "VeryLow" << "ExtremelyLow"; |
| 1025 | values << 0 << 1 << 2 << 3 ; |
| 1026 | |
| 1027 | QTest::newRow("Priority") << QString("Priority") << QString("UniqueTestService") |
| 1028 | << 4 << false << keynames << values; |
| 1029 | } |
| 1030 | |
| 1031 | void tst_QServiceManager_IPC::verifyUniqueEnumerator() |
| 1032 | { |
| 1033 | QFETCH(QString, enumName); |
| 1034 | QFETCH(QString, enumScope); |
| 1035 | QFETCH(int, enumKeyCount); |
| 1036 | QFETCH(bool, enumIsFlag); |
| 1037 | QFETCH(QStringList, enumKeyNames); |
| 1038 | QFETCH(QList<int>, enumKeyValues); |
| 1039 | |
| 1040 | const QMetaObject* meta = serviceUnique->metaObject(); |
| 1041 | const int index = meta->indexOfEnumerator(enumName.toLatin1().constData()); |
| 1042 | QMetaEnum metaEnum = meta->enumerator(index); |
| 1043 | |
| 1044 | QVERIFY(metaEnum.isValid()); |
| 1045 | QCOMPARE(enumScope, QString(metaEnum.scope())); |
| 1046 | QCOMPARE(enumKeyCount, metaEnum.keyCount()); |
| 1047 | QCOMPARE(enumIsFlag, metaEnum.isFlag()); |
| 1048 | QCOMPARE(enumKeyNames.count(), enumKeyValues.count()); |
| 1049 | |
| 1050 | for (int i=0; i<enumKeyNames.count(); i++) { |
| 1051 | QCOMPARE(enumKeyNames.at(i), QString(metaEnum.valueToKey(enumKeyValues.at(i)))); |
| 1052 | QCOMPARE(enumKeyValues.at(i), metaEnum.keyToValue(enumKeyNames.at(i).toLatin1().constData())); |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | void tst_QServiceManager_IPC::testInvokableFunctions() |
| 1057 | { |
| 1058 | // Test invokable method input with calculated return value |
| 1059 | QString temp; |
| 1060 | QString result; |
| 1061 | QString patternShared("%1 + 3 = %2"); |
| 1062 | QString patternUnique("%1 x 3 = %2"); |
| 1063 | for (int i = -10; i<10; i++) { |
| 1064 | result.clear(); temp.clear(); |
| 1065 | QVERIFY(result.isEmpty()); |
| 1066 | QVERIFY(temp.isEmpty()); |
| 1067 | |
| 1068 | // Shared service |
| 1069 | QMetaObject::invokeMethod(serviceShared, "testFunctionWithReturnValue", |
| 1070 | Q_RETURN_ARG(QString, result), |
| 1071 | Q_ARG(int, i)); |
| 1072 | temp = patternShared.arg(i).arg(i+3); |
| 1073 | QCOMPARE(temp, result); |
| 1074 | |
| 1075 | result.clear(); temp.clear(); |
| 1076 | QVERIFY(result.isEmpty()); |
| 1077 | QVERIFY(temp.isEmpty()); |
| 1078 | |
| 1079 | // Unique service |
| 1080 | QMetaObject::invokeMethod(serviceUnique, "testFunctionWithReturnValue", |
| 1081 | Q_RETURN_ARG(QString, result), |
| 1082 | Q_ARG(int, i)); |
| 1083 | temp = patternUnique.arg(i).arg(i*3); |
| 1084 | QCOMPARE(temp, result); |
| 1085 | } |
| 1086 | |
| 1087 | // Test invokable method with QVariant return type |
| 1088 | QList<QVariant> variants; |
| 1089 | variants << QVariant("CANT BE NULL") << QVariant(6) << QVariant(QString("testString")); |
| 1090 | for (int i = 0; i<variants.count(); i++) { |
| 1091 | QVariant varResult; |
| 1092 | |
| 1093 | // Shared service |
| 1094 | QMetaObject::invokeMethod(serviceShared, "testFunctionWithVariantReturnValue", |
| 1095 | Q_RETURN_ARG(QVariant, varResult), |
| 1096 | Q_ARG(QVariant, variants.at(i))); |
| 1097 | QCOMPARE(variants.at(i), varResult); |
| 1098 | |
| 1099 | // Unique service |
| 1100 | QMetaObject::invokeMethod(serviceUnique, "testFunctionWithVariantReturnValue", |
| 1101 | Q_RETURN_ARG(QVariant, varResult), |
| 1102 | Q_ARG(QVariant, variants.at(i))); |
| 1103 | QCOMPARE(variants.at(i), varResult); |
| 1104 | } |
| 1105 | |
| 1106 | // Test invokable method with custom return type |
| 1107 | QServiceFilter f; |
| 1108 | // Shared service |
| 1109 | QMetaObject::invokeMethod(serviceShared, "testFunctionWithCustomReturnValue", |
| 1110 | Q_RETURN_ARG(QServiceFilter, f)); |
| 1111 | QCOMPARE(f.serviceName(), QString("MySharedService")); |
| 1112 | QCOMPARE(f.interfaceName(), QString("com.nokia.qt.ipcunittest")); |
| 1113 | QCOMPARE(f.majorVersion(), 6); |
| 1114 | QCOMPARE(f.minorVersion(), 2); |
| 1115 | |
| 1116 | // Unique service |
| 1117 | QMetaObject::invokeMethod(serviceUnique, "testFunctionWithCustomReturnValue", |
| 1118 | Q_RETURN_ARG(QServiceFilter, f)); |
| 1119 | QCOMPARE(f.serviceName(), QString("MyUniqueService")); |
| 1120 | QCOMPARE(f.interfaceName(), QString("com.nokia.qt.ipcunittest")); |
| 1121 | QCOMPARE(f.majorVersion(), 6); |
| 1122 | QCOMPARE(f.minorVersion(), 7); |
| 1123 | |
| 1124 | // Test invokable method with list return type |
| 1125 | QList<QString> list, retList; |
| 1126 | list << "1" << "2" << "3"; |
| 1127 | QMetaObject::invokeMethod(serviceUnique, "testFunctionWithListReturn", |
| 1128 | Q_RETURN_ARG(QList<QString>, retList)); |
| 1129 | QCOMPARE(list, retList); |
| 1130 | } |
| 1131 | |
| 1132 | void tst_QServiceManager_IPC::testSignalling() |
| 1133 | { |
| 1134 | // Test signalling for simple methods |
| 1135 | QSignalSpy spy(serviceUnique, SIGNAL(signalWithIntParam(int))); |
| 1136 | QMetaObject::invokeMethod(serviceUnique, "triggerSignalWithIntParam"); |
| 1137 | QTRY_VERIFY(spy.count() > 0); |
| 1138 | QCOMPARE(spy.at(0).at(0).toInt(), 5); |
| 1139 | |
| 1140 | // Test signalling for property changes |
| 1141 | QCOMPARE(QString("FFF"), serviceUnique->property("value").toString()); |
| 1142 | QSignalSpy propSpy(serviceUnique, SIGNAL(valueChanged())); |
| 1143 | |
| 1144 | serviceUnique->setProperty("value", QString("GGG")); |
| 1145 | QTRY_VERIFY(propSpy.count() == 1); |
| 1146 | propSpy.clear(); |
| 1147 | serviceUnique->setProperty("value", QString("FFF")); |
| 1148 | QTRY_VERIFY(propSpy.count() == 1); |
| 1149 | QCOMPARE(QString("FFF"), serviceUnique->property("value").toString()); |
| 1150 | |
| 1151 | // Test signalling with custom types |
| 1152 | QSignalSpy variousSpy(serviceUnique, SIGNAL(signalWithVariousParam(QVariant,QString,QServiceFilter,QVariant))); |
| 1153 | QMetaObject::invokeMethod(serviceUnique, "triggerSignalWithVariousParam"); |
| 1154 | |
| 1155 | QTRY_VERIFY(variousSpy.count() == 1); |
| 1156 | QCOMPARE(variousSpy.at(0).count(), 4); |
| 1157 | QCOMPARE(variousSpy.at(0).at(0).value<QVariant>(), QVariant("CAN'T BE NULL")); |
| 1158 | QCOMPARE(variousSpy.at(0).at(1).toString(), QString("string-value")); |
| 1159 | |
| 1160 | QVariant vFilter = variousSpy.at(0).at(2); |
| 1161 | QServiceFilter filter; |
| 1162 | QVERIFY(vFilter.canConvert<QServiceFilter>()); |
| 1163 | filter = vFilter.value<QServiceFilter>(); |
| 1164 | QCOMPARE(filter.serviceName(), QString("MyService")); |
| 1165 | QCOMPARE(filter.interfaceName(), QString("com.nokia.qt.ipcunittest")); |
| 1166 | QCOMPARE(filter.majorVersion(), 6); |
| 1167 | QCOMPARE(filter.minorVersion(), 7); |
| 1168 | |
| 1169 | QCOMPARE(variousSpy.at(0).at(3).value<QVariant>(), QVariant(5)); |
| 1170 | } |
| 1171 | |
| 1172 | void tst_QServiceManager_IPC::testSlotInvokation() |
| 1173 | { |
| 1174 | QObject *service = serviceUnique; |
| 1175 | |
| 1176 | // check the success of our invokable slot by using a hash of the method and its parameters |
| 1177 | uint hash = 1; |
| 1178 | uint expectedHash = 0; |
| 1179 | QMetaObject::invokeMethod(service, "setConfirmationHash", |
| 1180 | Q_ARG(uint, 0)); |
| 1181 | QMetaObject::invokeMethod(service, "slotConfirmation", |
| 1182 | Q_RETURN_ARG(uint, hash)); |
| 1183 | QCOMPARE(hash, (uint)0); |
| 1184 | |
| 1185 | // Test invokable slot with no arguments |
| 1186 | QMetaObject::invokeMethod(service, "testSlot"); |
| 1187 | QMetaObject::invokeMethod(service, "slotConfirmation", |
| 1188 | Q_RETURN_ARG(uint, hash)); |
| 1189 | expectedHash = qHash(QString("testSlot()")); |
| 1190 | QCOMPARE(hash, expectedHash); |
| 1191 | |
| 1192 | // Test invokable slot with various arguments |
| 1193 | QVariant test("CANT BE NULL"); |
| 1194 | QByteArray d = "array"; |
| 1195 | int num = 5; |
| 1196 | QString output = QString("%1, %2, %3, %4"); |
| 1197 | output = output.arg(d.constData()).arg(num).arg(test.toString()).arg(test.isValid()); |
| 1198 | expectedHash = qHash(output); |
| 1199 | QMetaObject::invokeMethod(service, "testSlotWithArgs", |
| 1200 | Q_ARG(QByteArray, d), Q_ARG(int, num), Q_ARG(QVariant, test)); |
| 1201 | QMetaObject::invokeMethod(service, "slotConfirmation", |
| 1202 | Q_RETURN_ARG(uint, hash)); |
| 1203 | QCOMPARE(hash, expectedHash); |
| 1204 | |
| 1205 | // Test failed invokable slot since QServiceInterfaceDescriptor is not a registered meta type |
| 1206 | // by checking that the service didn't set the hash to -1 due to being called |
| 1207 | QServiceInterfaceDescriptor desc; |
| 1208 | QMetaObject::invokeMethod(service, "testSlotWithUnknownArg", |
| 1209 | Q_ARG(QServiceInterfaceDescriptor, desc)); |
| 1210 | QMetaObject::invokeMethod(service, "slotConfirmation", |
| 1211 | Q_RETURN_ARG(uint, hash)); |
| 1212 | QVERIFY(hash != 1); |
| 1213 | |
| 1214 | // Test slot function with custom argument |
| 1215 | QServiceFilter f("com.myInterface" , "4.5"); |
| 1216 | f.setServiceName("MyService"); |
| 1217 | output = QString("%1: %2 - %3.%4"); |
| 1218 | output = output.arg(f.serviceName()).arg(f.interfaceName()) |
| 1219 | .arg(f.majorVersion()).arg(f.minorVersion()); |
| 1220 | expectedHash = qHash(output); |
| 1221 | QMetaObject::invokeMethod(service, "testSlotWithCustomArg", |
| 1222 | Q_ARG(QServiceFilter, f)); |
| 1223 | QMetaObject::invokeMethod(service, "slotConfirmation", |
| 1224 | Q_RETURN_ARG(uint, hash)); |
| 1225 | QCOMPARE(hash, expectedHash); |
| 1226 | |
| 1227 | // Test invokable method with a QList argument |
| 1228 | QList<QString> list; |
| 1229 | list << "one" << "two" << "three"; |
| 1230 | output = ""; |
| 1231 | for (int i=0; i<list.size(); i++) { |
| 1232 | output += list[i]; |
| 1233 | if (i<list.size()-1) |
| 1234 | output += ", "; |
| 1235 | } |
| 1236 | expectedHash = qHash(output); |
| 1237 | QMetaObject::invokeMethod(serviceUnique, "testSlotWithListArg", |
| 1238 | Q_ARG(QList<QString>, list)); |
| 1239 | QMetaObject::invokeMethod(service, "slotConfirmation", |
| 1240 | Q_RETURN_ARG(uint, hash)); |
| 1241 | QCOMPARE(hash, expectedHash); |
| 1242 | |
| 1243 | // Test slot with QHash<QString, QVariant> |
| 1244 | QHash<QString, QVariant> hashv; |
| 1245 | hashv["test"] = QVariant::fromValue(1); |
| 1246 | hashv["test2"] = QVariant::fromValue(QString("test2 string")); |
| 1247 | hashv["test3"] = QVariant::fromValue(true); |
| 1248 | QHashIterator<QString, QVariant> i(hashv); |
| 1249 | output.clear(); |
| 1250 | QStringList lines; |
| 1251 | while (i.hasNext()) { |
| 1252 | i.next(); |
| 1253 | QString line = i.key(); |
| 1254 | line += "="; |
| 1255 | line += i.value().toString(); |
| 1256 | lines << line; |
| 1257 | } |
| 1258 | lines.sort(); |
| 1259 | output = lines.join(QStringLiteral(",")); |
| 1260 | expectedHash = qHash(output); |
| 1261 | |
| 1262 | QMetaObject::invokeMethod(serviceUnique, "testSlotWithComplexArg", |
| 1263 | Q_ARG(QVariantHash, hashv)); |
| 1264 | QMetaObject::invokeMethod(service, "slotConfirmation", |
| 1265 | Q_RETURN_ARG(uint, hash)); |
| 1266 | QCOMPARE(hash, expectedHash); |
| 1267 | |
| 1268 | |
| 1269 | } |
| 1270 | |
| 1271 | void tst_QServiceManager_IPC::verifyServiceClass() |
| 1272 | { |
| 1273 | QRemoteServiceRegister *registerObject = new QRemoteServiceRegister(); |
| 1274 | |
| 1275 | QVERIFY2(registerObject->quitOnLastInstanceClosed() == true, "should default to true, default is to shutdown"); |
| 1276 | registerObject->setQuitOnLastInstanceClosed(false); |
| 1277 | QVERIFY2(registerObject->quitOnLastInstanceClosed() == false, "must transition to false"); |
| 1278 | registerObject->setQuitOnLastInstanceClosed(true); |
| 1279 | QVERIFY2(registerObject->quitOnLastInstanceClosed() == true, "must transition back to true"); |
| 1280 | |
| 1281 | delete registerObject; |
| 1282 | } |
| 1283 | |
| 1284 | void tst_QServiceManager_IPC::verifyLargeDataTransfer() |
| 1285 | { |
| 1286 | QFETCH(QByteArray, data); |
| 1287 | |
| 1288 | QMetaObject::invokeMethod(serviceUnique, "testSlotWithData", |
| 1289 | Q_ARG(QByteArray, data)); |
| 1290 | QByteArray ret_data; |
| 1291 | QMetaObject::invokeMethod(serviceUnique, "testInvoableWithReturnData", Q_RETURN_ARG(QByteArray, ret_data)); |
| 1292 | |
| 1293 | QVERIFY2(data.length() == ret_data.length(), "Returned data from the service is not the same length as data sent"); |
| 1294 | QVERIFY2(data == ret_data, "Returned data from service does not match"); |
| 1295 | } |
| 1296 | |
| 1297 | void tst_QServiceManager_IPC::verifyLargeDataTransfer_data() |
| 1298 | { |
| 1299 | QTest::addColumn<QByteArray>("data"); |
| 1300 | |
| 1301 | QTest::newRow("Trivial") << QByteArray("c"); |
| 1302 | QTest::newRow("1k") << QByteArray(1024, 'A'); |
| 1303 | QTest::newRow("2k") << QByteArray(2048, 'A'); |
| 1304 | QTest::newRow("4k") << QByteArray(4096, 'A'); |
| 1305 | QTest::newRow("8k") << QByteArray(8192, 'A'); |
| 1306 | QTest::newRow("16k") << QByteArray(16384, 'A'); |
| 1307 | QTest::newRow("32k") << QByteArray(32768, 'A'); |
| 1308 | QTest::newRow("64k") << QByteArray(65536, 'A'); |
| 1309 | QTest::newRow("128k") << QByteArray(131072, 'A'); |
| 1310 | QTest::newRow("1 megabyte") << QByteArray(1048576, 'A'); |
| 1311 | QTest::newRow("Free mem") << QByteArray(""); |
| 1312 | } |
| 1313 | |
| 1314 | class FetchLotsOfProperties : public QThread |
| 1315 | { |
| 1316 | Q_OBJECT |
| 1317 | public: |
| 1318 | FetchLotsOfProperties(int runs, bool shared, QObject *parent = 0); |
| 1319 | void run(); |
| 1320 | |
| 1321 | public slots: |
| 1322 | void startFetches(); |
| 1323 | void printProgress(); |
| 1324 | |
| 1325 | private slots: |
| 1326 | void setup(); |
| 1327 | void fetchProperty(); |
| 1328 | void ipcError(); |
| 1329 | |
| 1330 | signals: |
| 1331 | void ready(); |
| 1332 | void error(); |
| 1333 | |
| 1334 | private: |
| 1335 | int runs; |
| 1336 | int start_runs; |
| 1337 | bool shared; |
| 1338 | QString lastValue; |
| 1339 | QObject *service; |
| 1340 | QServiceManager *mgr; |
| 1341 | }; |
| 1342 | |
| 1343 | FetchLotsOfProperties::FetchLotsOfProperties(int runs, bool shared, QObject *parent) |
| 1344 | : QThread(parent), |
| 1345 | service(0), |
| 1346 | mgr(0) |
| 1347 | { |
| 1348 | this->runs = runs; |
| 1349 | this->shared = shared; |
| 1350 | this->start_runs = runs; |
| 1351 | } |
| 1352 | |
| 1353 | void FetchLotsOfProperties::run() |
| 1354 | { |
| 1355 | QMetaObject::invokeMethod(this, "setup", Qt::QueuedConnection); |
| 1356 | exec(); |
| 1357 | } |
| 1358 | |
| 1359 | void FetchLotsOfProperties::setup() |
| 1360 | { |
| 1361 | mgr = new QServiceManager(this); |
| 1362 | |
| 1363 | if (!mgr) { |
| 1364 | qWarning() << "Failed to create a manager"; |
| 1365 | emit error(); |
| 1366 | exit(-1); |
| 1367 | } |
| 1368 | |
| 1369 | QList<QServiceInterfaceDescriptor> list = mgr->findInterfaces("IPCExampleService"); |
| 1370 | if (list.isEmpty()) { |
| 1371 | qWarning() << "Thread failed to query the database, or the database contained no services"; |
| 1372 | emit error(); |
| 1373 | exit(-1); |
| 1374 | return; |
| 1375 | } |
| 1376 | foreach (const QServiceInterfaceDescriptor &d, list) { |
| 1377 | if (d.majorVersion() == 3 && d.minorVersion() == 5 && !shared) { |
| 1378 | service = mgr->loadInterface(d); |
| 1379 | } |
| 1380 | if (d.majorVersion() == 3 && d.minorVersion() == 4 && shared) { |
| 1381 | service = mgr->loadInterface(d); |
| 1382 | } |
| 1383 | if (service) |
| 1384 | connect(service, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)), this, SLOT(ipcError())); |
| 1385 | } |
| 1386 | |
| 1387 | if (!service) { |
| 1388 | qWarning() << "Thread failed to load service"; |
| 1389 | emit error(); |
| 1390 | exit(-1); |
| 1391 | return; |
| 1392 | } |
| 1393 | |
| 1394 | connect(this, SIGNAL(destroyed()), service, SLOT(deleteLater())); |
| 1395 | |
| 1396 | lastValue = service->property("value").toString(); |
| 1397 | |
| 1398 | emit ready(); |
| 1399 | } |
| 1400 | |
| 1401 | void FetchLotsOfProperties::startFetches() |
| 1402 | { |
| 1403 | if (!mgr || !service) { |
| 1404 | exit(-1); |
| 1405 | } |
| 1406 | |
| 1407 | if (runs > 0) { |
| 1408 | QMetaObject::invokeMethod(this, "fetchProperty", Qt::QueuedConnection); |
| 1409 | } |
| 1410 | else { |
| 1411 | quit(); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | void FetchLotsOfProperties::printProgress() |
| 1416 | { |
| 1417 | qWarning() << "Thread has" << runs << "left, started with" << start_runs; |
| 1418 | } |
| 1419 | |
| 1420 | void FetchLotsOfProperties::fetchProperty() |
| 1421 | { |
| 1422 | if (!service) { |
| 1423 | emit error(); |
| 1424 | exit(-1); |
| 1425 | } |
| 1426 | |
| 1427 | QString value = service->property("value").toString(); |
| 1428 | if (value != lastValue) { |
| 1429 | qWarning() << "Value changed on read! Got" << value << "wanted" << lastValue; |
| 1430 | emit error(); |
| 1431 | exit(-1); |
| 1432 | } |
| 1433 | |
| 1434 | if (runs-- > 0) { |
| 1435 | QMetaObject::invokeMethod(this, "fetchProperty", Qt::QueuedConnection); |
| 1436 | } |
| 1437 | else { |
| 1438 | quit(); |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | void FetchLotsOfProperties::ipcError() |
| 1443 | { |
| 1444 | |
| 1445 | } |
| 1446 | |
| 1447 | void tst_QServiceManager_IPC::verifyThreadSafety() |
| 1448 | { |
| 1449 | QFETCH(int, shared); |
| 1450 | QFETCH(int, runs); |
| 1451 | QFETCH(int, threads); |
| 1452 | |
| 1453 | QSignalSpy ready(this, SIGNAL(threadReady())); |
| 1454 | QSignalSpy finished(this, SIGNAL(threadFinished())); |
| 1455 | QSignalSpy errors(this, SIGNAL(threadError())); |
| 1456 | bool mixed = true; |
| 1457 | for (int i = 0; i < threads; i++) { |
| 1458 | switch (shared) { |
| 1459 | case 0: |
| 1460 | mixed = false; |
| 1461 | break; |
| 1462 | case 1: |
| 1463 | mixed = true; |
| 1464 | break; |
| 1465 | case 2: |
| 1466 | mixed = !mixed; |
| 1467 | } |
| 1468 | FetchLotsOfProperties *fetch = new FetchLotsOfProperties(runs, mixed, this); |
| 1469 | connect(this, SIGNAL(threadRun()), fetch, SLOT(startFetches())); |
| 1470 | connect(fetch, SIGNAL(ready()), this, SIGNAL(threadReady())); |
| 1471 | connect(fetch, SIGNAL(error()), this, SIGNAL(threadError())); |
| 1472 | connect(fetch, SIGNAL(finished()), this, SIGNAL(threadFinished())); |
| 1473 | connect(fetch, SIGNAL(finished()), fetch, SLOT(deleteLater())); |
| 1474 | connect(this, SIGNAL(threadRemaining()), fetch, SLOT(printProgress())); |
| 1475 | fetch->start(); |
| 1476 | } |
| 1477 | |
| 1478 | |
| 1479 | #define THREAD_TIMEOUT 30*1000 |
| 1480 | |
| 1481 | QTimer timer; |
| 1482 | timer.setInterval(THREAD_TIMEOUT); |
| 1483 | timer.setSingleShot(true); |
| 1484 | timer.start(); |
| 1485 | |
| 1486 | QTime start = QTime::currentTime(); |
| 1487 | |
| 1488 | while (((ready.count()) < threads) && (timer.isActive())) { |
| 1489 | QCOMPARE(errors.count(), 0); |
| 1490 | QCoreApplication::processEvents(); |
| 1491 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
| 1492 | } |
| 1493 | QTRY_COMPARE(ready.count(), threads); |
| 1494 | QVERIFY2(!errors.count(), "Threads reported errors on setup"); |
| 1495 | emit threadRun(); |
| 1496 | |
| 1497 | qDebug() << "Waiting on" << threads << "threads to finish"; |
| 1498 | |
| 1499 | while (((finished.count() + errors.count()) < threads) && (timer.isActive())) { |
| 1500 | QCOMPARE(errors.count(), 0); |
| 1501 | QCoreApplication::processEvents(); |
| 1502 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
| 1503 | } |
| 1504 | |
| 1505 | qDebug() << "Waited" << start.secsTo(QTime::currentTime()) << "seconds"; |
| 1506 | |
| 1507 | if (finished.count() != threads){ |
| 1508 | emit threadRemaining(); |
| 1509 | } |
| 1510 | |
| 1511 | QCOMPARE(finished.count(), threads); |
| 1512 | QVERIFY2(!errors.count(), "Threads reported errors"); |
| 1513 | } |
| 1514 | |
| 1515 | void tst_QServiceManager_IPC::verifyThreadSafety_data() |
| 1516 | { |
| 1517 | QTest::addColumn<int>("shared"); |
| 1518 | QTest::addColumn<int>("runs"); |
| 1519 | QTest::addColumn<int>("threads"); |
| 1520 | |
| 1521 | QTest::newRow("unique, 2 threads") << 0 << 50 << 2; |
| 1522 | QTest::newRow("unique, 4 threads") << 0 << 25 << 4; |
| 1523 | QTest::newRow("unique, 16 threads") << 0 << 5 << 16; |
| 1524 | QTest::newRow("shared, 2 threads") << 1 << 50 << 2; |
| 1525 | QTest::newRow("shared, 4 threads") << 1 << 25 << 4; |
| 1526 | QTest::newRow("shared, 16 threads") << 1 << 5 << 16; |
| 1527 | QTest::newRow("mixed, 16 threads") << 2 << 5 << 16; |
| 1528 | #ifndef Q_OS_WIN // Limit on event notifiers. |
| 1529 | QTest::newRow("unique, 128 threads") << 0 << 5 << 128; |
| 1530 | QTest::newRow("shared, 128 threads") << 1 << 5 << 128; |
| 1531 | QTest::newRow("mixed, 128 threads") << 2 << 5 << 128; |
| 1532 | #endif // Q_OS_WIN |
| 1533 | } |
| 1534 | |
| 1535 | void tst_QServiceManager_IPC::unblockRemote() |
| 1536 | { |
| 1537 | QString ret = serviceUnique->property("releaseBlockingRead").toString(); |
| 1538 | QCOMPARE(ret, QStringLiteral("releaseBlockingReadReturned")); |
| 1539 | } |
| 1540 | |
| 1541 | void tst_QServiceManager_IPC::verifyRemoteBlockingFunctions() |
| 1542 | { |
| 1543 | #ifdef SFW_USE_DBUS_BACKEND |
| 1544 | QEXPECT_FAIL("", "Test case known to segfault on dbus, skipping, QTBUG-23168", Abort); |
| 1545 | QVERIFY(false); |
| 1546 | #else |
| 1547 | connect(serviceUnique, SIGNAL(blockingValueRead()), this, SLOT(unblockRemote())); |
| 1548 | |
| 1549 | QString ret = serviceUnique->property("blockingValue").toString(); |
| 1550 | QCOMPARE(ret, QStringLiteral("blockingValueReturned")); |
| 1551 | #endif |
| 1552 | } |
| 1553 | |
| 1554 | void tst_QServiceManager_IPC::testSignalSlotOrdering() |
| 1555 | { |
| 1556 | QSignalSpy spy(serviceUnique, SIGNAL(count(int))); |
| 1557 | |
| 1558 | int value = -1; |
| 1559 | bool ret = QMetaObject::invokeMethod(serviceUnique, "testSignalSlotOrdering", Q_RETURN_ARG(int, value)); |
| 1560 | QVERIFY2(ret == true, "Verify slot call worked"); |
| 1561 | QVERIFY2(value == 0, "Verify slot return value"); |
| 1562 | QVERIFY2(spy.count() == 0, "Verify no signals fired before the invokeMethod returns"); |
| 1563 | QTRY_COMPARE(spy.count(), 20); |
| 1564 | } |
| 1565 | |
| 1566 | void tst_QServiceManager_IPC::testServiceSecurity() |
| 1567 | { |
| 1568 | |
| 1569 | QServiceManager* manager = new QServiceManager(this); |
| 1570 | QList<QServiceInterfaceDescriptor> list = manager->findInterfaces("IPCExampleService"); |
| 1571 | QServiceInterfaceDescriptor d, dPriv, dGlobal; |
| 1572 | foreach (QServiceInterfaceDescriptor d, list){ |
| 1573 | if (d.majorVersion() == 3 && d.minorVersion() == 9) { |
| 1574 | dPriv = d; |
| 1575 | } |
| 1576 | if (d.majorVersion() == 3 && d.minorVersion() == 10) { |
| 1577 | dGlobal = d; |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | QVERIFY(dPriv.isValid()); |
| 1582 | QVERIFY(dGlobal.isValid()); |
| 1583 | |
| 1584 | // Rejects all connections |
| 1585 | QObject *o = manager->loadInterface(dPriv); |
| 1586 | QVERIFY2(o == 0, "Everything should fail security/creation with this interface"); |
| 1587 | |
| 1588 | o = manager->loadInterface(dGlobal); |
| 1589 | QVERIFY2(o, "Global class should allow the first connection"); |
| 1590 | |
| 1591 | bool status; |
| 1592 | // make sync call to ensure remote is in the same state |
| 1593 | QMetaObject::invokeMethod(o, "disableConnections", Q_RETURN_ARG(bool, status), Q_ARG(bool, true)); |
| 1594 | QCOMPARE(status, true); |
| 1595 | |
| 1596 | // expect a null object back |
| 1597 | QObject *second = manager->loadInterface(dGlobal); |
| 1598 | QVERIFY2(second == 0, "Security checks should no longer allow objects to be created"); |
| 1599 | |
| 1600 | // make sync call to ensure remote is in the same state |
| 1601 | QMetaObject::invokeMethod(o, "disableConnections", Q_RETURN_ARG(bool, status), Q_ARG(bool, false)); |
| 1602 | |
| 1603 | second = manager->loadInterface(dGlobal); |
| 1604 | QVERIFY2(second != 0, "Object creation should be allowed"); |
| 1605 | |
| 1606 | delete second; |
| 1607 | |
| 1608 | } |
| 1609 | |
| 1610 | void tst_QServiceManager_IPC::testProcessLaunch() |
| 1611 | { |
| 1612 | } |
| 1613 | |
| 1614 | void tst_QServiceManager_IPC::verifyAsyncLoading() |
| 1615 | { |
| 1616 | |
| 1617 | QFETCH(QString, serviceInterface); |
| 1618 | QFETCH(int, descriptor); |
| 1619 | QFETCH(bool, errors); |
| 1620 | QFETCH(int, simultaneous); |
| 1621 | |
| 1622 | QServiceManager mgr; |
| 1623 | |
| 1624 | QServiceReply *reply = 0; |
| 1625 | QServiceReply *replies[16]; // must be constant for windows, set to max size below |
| 1626 | |
| 1627 | if (simultaneous > 0) { |
| 1628 | for (int i = 0; i < simultaneous; i++) { |
| 1629 | replies[i] = mgr.loadInterfaceRequest(serviceInterface); |
| 1630 | } |
| 1631 | reply = replies[0]; |
| 1632 | } else if (descriptor > 0) { |
| 1633 | QList<QServiceInterfaceDescriptor> list = mgr.findInterfaces("IPCExampleService"); |
| 1634 | QServiceInterfaceDescriptor d; |
| 1635 | foreach (d, list) { |
| 1636 | if (d.majorVersion() == 3 && d.minorVersion() == descriptor) { |
| 1637 | break; |
| 1638 | } |
| 1639 | } |
| 1640 | reply = mgr.loadInterfaceRequest(d); |
| 1641 | } else if (!serviceInterface.isEmpty()) { |
| 1642 | reply = mgr.loadInterfaceRequest(serviceInterface); |
| 1643 | } else { |
| 1644 | QFAIL("No descriptor nor interface speficied"); |
| 1645 | } |
| 1646 | |
| 1647 | |
| 1648 | QSignalSpy startedSpy(reply, SIGNAL(started())); |
| 1649 | QSignalSpy errorSpy(reply, SIGNAL(errorChanged())); |
| 1650 | QSignalSpy finishedSpy(reply, SIGNAL(finished())); |
| 1651 | |
| 1652 | QCOMPARE(startedSpy.count(), 0); |
| 1653 | |
| 1654 | QTRY_COMPARE(startedSpy.count(), 1); |
| 1655 | if (!errors) |
| 1656 | QCOMPARE(errorSpy.count(), 0); |
| 1657 | |
| 1658 | QTRY_COMPARE(finishedSpy.count(), 1); |
| 1659 | QCOMPARE(reply->isFinished(), true); |
| 1660 | if (!errors) { |
| 1661 | QCOMPARE(errorSpy.count(), 0); |
| 1662 | QCOMPARE(reply->error(), QServiceManager::NoError); |
| 1663 | } else { |
| 1664 | QCOMPARE(errorSpy.count(), 1); |
| 1665 | QVERIFY(reply->error() != QServiceManager::NoError); |
| 1666 | } |
| 1667 | |
| 1668 | |
| 1669 | |
| 1670 | if (simultaneous) { |
| 1671 | for (int i = 0; i < simultaneous; i++) { |
| 1672 | QTRY_COMPARE(replies[i]->isFinished(), true); |
| 1673 | QCOMPARE(replies[i]->error(), QServiceManager::NoError); |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | delete reply; |
| 1678 | } |
| 1679 | |
| 1680 | void tst_QServiceManager_IPC::verifyAsyncLoading_data() |
| 1681 | { |
| 1682 | QTest::addColumn<QString>("serviceInterface"); |
| 1683 | QTest::addColumn<int>("descriptor"); |
| 1684 | QTest::addColumn<bool>("errors"); |
| 1685 | QTest::addColumn<int>("simultaneous"); |
| 1686 | |
| 1687 | |
| 1688 | QTest::newRow("Load minor version 5") << QString() << 5 << false << 0; |
| 1689 | QTest::newRow("Load default interface") << QString("com.nokia.qt.ipcunittest") << 0 << false << 0; |
| 1690 | QTest::newRow("Load invalid interface") << QString("com.nokia.qt.ipcunittest.does.not.exist") << 0 << true << 0; |
| 1691 | QTest::newRow("Load 4 interfaces") << QString("com.nokia.qt.ipcunittest") << 1 << false << 4; |
| 1692 | QTest::newRow("Load 16 interfaces") << QString("com.nokia.qt.ipcunittest") << 1 << false << 16; |
| 1693 | |
| 1694 | } |
| 1695 | |
| 1696 | void tst_QServiceManager_IPC::testIpcFailure() |
| 1697 | { |
| 1698 | // test deleting an object doesn't trigger an IPC fault |
| 1699 | ipcfailure = false; |
| 1700 | delete serviceShared; |
| 1701 | QVERIFY2(!ipcfailure, "Deleting an object should not cause an IPC failure message"); |
| 1702 | serviceShared = 0; |
| 1703 | |
| 1704 | ipcfailure = false; |
| 1705 | QMetaObject::invokeMethod(serviceUnique, "testIpcFailure"); |
| 1706 | int i = 0; |
| 1707 | while (!ipcfailure && i++ < 50) |
| 1708 | QTest::qWait(50); |
| 1709 | |
| 1710 | QVERIFY(ipcfailure); |
| 1711 | |
| 1712 | // TODO restart the connection |
| 1713 | //initTestCase(); |
| 1714 | } |
| 1715 | |
| 1716 | void tst_QServiceManager_IPC::verifyFailures() |
| 1717 | { |
| 1718 | bool result; |
| 1719 | |
| 1720 | QServiceManager* manager = new QServiceManager(this); |
| 1721 | QList<QServiceInterfaceDescriptor> list = manager->findInterfaces("IPCExampleService"); |
| 1722 | QServiceInterfaceDescriptor d; |
| 1723 | foreach (d, list){ |
| 1724 | if (d.majorVersion() == 3 && d.minorVersion() == 6) { |
| 1725 | QObject *o = manager->loadInterface(d); |
| 1726 | QVERIFY2(o == 0, "Failure to allocate remote object returns null"); |
| 1727 | } |
| 1728 | if (d.majorVersion() == 3 && d.minorVersion() == 7) { |
| 1729 | QObject *o = manager->loadInterface(d); |
| 1730 | QVERIFY2(o == 0, "Failure to allocate remote object returns null"); |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | QMetaObject::invokeMethod(miscTest, "addTwice", |
| 1735 | Q_RETURN_ARG(bool, result)); |
| 1736 | QVERIFY2(result, "Added the same service twice, returned different entries"); |
| 1737 | |
| 1738 | QMetaObject::invokeMethod(miscTest, "getInvalidEntry", |
| 1739 | Q_RETURN_ARG(bool, result)); |
| 1740 | QVERIFY2(result, "Invalid entry returns invalid meta data"); |
| 1741 | |
| 1742 | delete manager; |
| 1743 | |
| 1744 | } |
| 1745 | |
| 1746 | |
| 1747 | |
| 1748 | QTEST_MAIN(tst_QServiceManager_IPC); |
| 1749 | #include "tst_qservicemanager_ipc.moc" |