| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | //TESTED_COMPONENT=src/serviceframework |
| 30 | |
| 31 | #include <QtTest/QtTest> |
| 32 | #include <QtCore> |
| 33 | #define private public |
| 34 | #include <qserviceinterfacedescriptor.h> |
| 35 | #include <private/qserviceinterfacedescriptor_p.h> |
| 36 | #include <private/servicedatabase_p.h> |
| 37 | #include <qservicefilter.h> |
| 38 | |
| 39 | #define RESOLVERDATABASE "services.db" |
| 40 | |
| 41 | QT_USE_NAMESPACE |
| 42 | |
| 43 | class ServiceDatabaseUnitTest: public QObject |
| 44 | { |
| 45 | Q_OBJECT |
| 46 | |
| 47 | private slots: |
| 48 | void initTestCase(); |
| 49 | void testRegistration(); |
| 50 | void getInterfaces(); |
| 51 | void searchByInterfaceName(); |
| 52 | void searchByInterfaceAndService(); |
| 53 | void searchByCustomAttribute(); |
| 54 | void searchByCapability(); |
| 55 | void attributes(); |
| 56 | void getServiceNames(); |
| 57 | void defaultExternalIfaceIDs(); |
| 58 | void interfaceDefault(); |
| 59 | void setInterfaceDefault(); |
| 60 | void unregister(); |
| 61 | void securityTokens(); |
| 62 | void cleanupTestCase(); |
| 63 | |
| 64 | private: |
| 65 | |
| 66 | bool compareDescriptor(QServiceInterfaceDescriptor interface, |
| 67 | QString interfaceName, |
| 68 | QString serviceName, |
| 69 | int majorVersion, |
| 70 | int minorVersion); |
| 71 | |
| 72 | bool compareDescriptor(QServiceInterfaceDescriptor interface, |
| 73 | QString interfaceName, |
| 74 | QString serviceName, |
| 75 | int majorVersion, |
| 76 | int minorVersion, |
| 77 | QStringList capabilities, |
| 78 | const QHash<QString,QString> customProps, |
| 79 | QString filePath="", |
| 80 | QString serviceDescription="", |
| 81 | QString interfaceDescription=""); |
| 82 | |
| 83 | QStringList getInterfaceIDs(const QString &serviceName); |
| 84 | QStringList getServiceIDs(const QString &serviceName); |
| 85 | bool existsInInterfacePropertyTable(const QString &interfaceID); |
| 86 | bool existsInServicePropertyTable(const QString &serviceID); |
| 87 | bool existsInDefaultsTable(const QString &interfaceID); |
| 88 | bool registerService(const ServiceMetaDataResults &service, const QString &securityToken = QString()); |
| 89 | bool unregisterService(const QString &serviceName, const QString &securityToken = QString()); |
| 90 | |
| 91 | ServiceMetaData* parser; |
| 92 | ServiceDatabase database; |
| 93 | }; |
| 94 | |
| 95 | void ServiceDatabaseUnitTest::initTestCase() |
| 96 | { |
| 97 | database.close(); |
| 98 | QFile::remove(database.databasePath()); |
| 99 | } |
| 100 | |
| 101 | static const QString securityTokenOwner("SecurityTokenOwner"); |
| 102 | static const QString securityTokenStranger("SecurityTokenStranger"); |
| 103 | |
| 104 | void ServiceDatabaseUnitTest::testRegistration() |
| 105 | { |
| 106 | QDir testdir = QDir(QFINDTESTDATA("testdata")); |
| 107 | |
| 108 | ServiceMetaData parser(testdir.absoluteFilePath("ServiceAcme.xml")); |
| 109 | QVERIFY(parser.extractMetadata()); |
| 110 | QVERIFY(!registerService(parser.parseResults())); |
| 111 | |
| 112 | QCOMPARE(database.lastError().code(), DBError::DatabaseNotOpen); |
| 113 | QVERIFY(database.open()); |
| 114 | QVERIFY(registerService(parser.parseResults())); |
| 115 | |
| 116 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceOmni.xml"))); |
| 117 | QVERIFY(parser.extractMetadata()); |
| 118 | QVERIFY(registerService(parser.parseResults())); |
| 119 | |
| 120 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceLuthorCorp.xml"))); |
| 121 | QVERIFY(parser.extractMetadata()); |
| 122 | QVERIFY(registerService(parser.parseResults())); |
| 123 | |
| 124 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceWayneEnt.xml"))); |
| 125 | QVERIFY(parser.extractMetadata()); |
| 126 | QVERIFY(registerService(parser.parseResults())); |
| 127 | |
| 128 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServicePrimatech.xml"))); |
| 129 | QVERIFY(parser.extractMetadata()); |
| 130 | QVERIFY(registerService(parser.parseResults())); |
| 131 | |
| 132 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceCyberdyne.xml"))); |
| 133 | QVERIFY(parser.extractMetadata()); |
| 134 | QVERIFY(registerService(parser.parseResults())); |
| 135 | |
| 136 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceSkynet.xml"))); |
| 137 | QVERIFY(parser.extractMetadata()); |
| 138 | QVERIFY(registerService(parser.parseResults())); |
| 139 | |
| 140 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceAutobot.xml"))); |
| 141 | QVERIFY(parser.extractMetadata()); |
| 142 | QVERIFY(registerService(parser.parseResults())); |
| 143 | |
| 144 | //try to register an already registered service |
| 145 | QVERIFY(!registerService(parser.parseResults())); |
| 146 | QCOMPARE(database.lastError().code(), DBError::LocationAlreadyRegistered); |
| 147 | |
| 148 | //try to register a service with a dll that provides interface implementations |
| 149 | //that are already provided by a currently registered service |
| 150 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServicePrimatech2error.xml"))); |
| 151 | QVERIFY(parser.extractMetadata()); |
| 152 | QVERIFY(!registerService(parser.parseResults())); |
| 153 | QCOMPARE(database.lastError().code(), DBError::IfaceImplAlreadyRegistered); |
| 154 | |
| 155 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceYamagatoError.xml"))); |
| 156 | QVERIFY(parser.extractMetadata()); |
| 157 | QVERIFY(!registerService(parser.parseResults())); |
| 158 | QCOMPARE(database.lastError().code(), DBError::LocationAlreadyRegistered); |
| 159 | |
| 160 | //make sure errors above are corectly rolled back by |
| 161 | //registering a valid service |
| 162 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceDecepticon.xml"))); |
| 163 | QVERIFY(parser.extractMetadata()); |
| 164 | QVERIFY(registerService(parser.parseResults())); |
| 165 | |
| 166 | QStringList xmlFiles; |
| 167 | xmlFiles << "ServiceDharma_Swan.xml" |
| 168 | << "ServiceDharma_Pearl.xml" |
| 169 | << "ServiceDharma_Flame.xml"; |
| 170 | |
| 171 | foreach(const QString &file, xmlFiles) { |
| 172 | //Check that when we register a service we reseed |
| 173 | //the random number generator so we don't have |
| 174 | //any service id clashes. To verify this is |
| 175 | //happening we set the seed here to 1. |
| 176 | qsrand(1); |
| 177 | parser.setDevice(new QFile(testdir.absoluteFilePath(file))); |
| 178 | QVERIFY(parser.extractMetadata()); |
| 179 | QVERIFY(registerService(parser.parseResults())); |
| 180 | } |
| 181 | |
| 182 | QVERIFY(database.close()); |
| 183 | QVERIFY(database.close()); //verify we can close multiple times |
| 184 | //without side effects |
| 185 | } |
| 186 | |
| 187 | void ServiceDatabaseUnitTest::getInterfaces() |
| 188 | { |
| 189 | QServiceFilter filter; |
| 190 | QList<QServiceInterfaceDescriptor> interfaces; |
| 191 | |
| 192 | filter.setServiceName("acme"); |
| 193 | interfaces = database.getInterfaces(filter); |
| 194 | QCOMPARE(database.lastError().code(), DBError::DatabaseNotOpen); |
| 195 | QVERIFY(database.close()); //check we can close the database even if it |
| 196 | //has not been opened |
| 197 | |
| 198 | QVERIFY(database.open()); |
| 199 | interfaces = database.getInterfaces(filter); |
| 200 | QCOMPARE(database.lastError().code(), DBError::NoError); |
| 201 | QCOMPARE(interfaces.count(), 5); |
| 202 | |
| 203 | QHash<QString,QString> customs; |
| 204 | QStringList capabilities; |
| 205 | QVERIFY(compareDescriptor(interfaces[0], "com.acme.service.downloader", "acme", 1, 0, capabilities, customs, "C:/TestData/testservice.dll", |
| 206 | "Acme services", "Interface that provides download support")); |
| 207 | |
| 208 | QServiceInterfaceDescriptor interface; |
| 209 | customs["coordinate"] = QString("global"); |
| 210 | QVERIFY(compareDescriptor(interfaces[1], "com.acme.service.location", "acme", 1,0, capabilities,customs, "C:/TestData/testservice.dll")); |
| 211 | |
| 212 | customs.clear(); |
| 213 | customs["cpu"]=QString(""); |
| 214 | capabilities.append(QString("ReadUserData")); |
| 215 | QVERIFY(compareDescriptor(interfaces[2], "com.acme.device.sysinfo", "acme", 2, 3, capabilities, customs, "C:/TestData/testservice.dll")); |
| 216 | |
| 217 | customs.clear(); |
| 218 | customs["smtp"]=QString("smtp.mail.com"); |
| 219 | capabilities.clear(); |
| 220 | capabilities.append("ReadUserData"); |
| 221 | capabilities.append("WriteUserData"); |
| 222 | QVERIFY(compareDescriptor(interfaces[3], "com.acme.device.sendMessage", "acme", 3, 0, capabilities,customs, "C:/TestData/testservice.dll")); |
| 223 | |
| 224 | customs.clear(); |
| 225 | customs["imap"]=QString("imap.mail.com"); |
| 226 | customs["pop"]=QString("pop.mail.com"); |
| 227 | capabilities.clear(); |
| 228 | capabilities.append("ReadUserData"); |
| 229 | capabilities.append("WriteUserData"); |
| 230 | capabilities.append("ExecUserData"); |
| 231 | QVERIFY(compareDescriptor(interfaces[4], "com.acme.device.receiveMessage", "acme", 1, 1, capabilities, customs, "C:/TestData/testservice.dll")); |
| 232 | |
| 233 | //check that searching is case insensitive |
| 234 | filter.setServiceName("OmNi"); |
| 235 | interfaces = database.getInterfaces(filter); |
| 236 | QCOMPARE(database.lastError().code(), DBError::NoError); |
| 237 | QCOMPARE(interfaces.count(), 3); |
| 238 | |
| 239 | customs.clear(); |
| 240 | capabilities.clear(); |
| 241 | capabilities << "SurroundingsDD"; |
| 242 | QVERIFY(compareDescriptor(interfaces[0], "com.omni.device.Accelerometer", "OMNI", 1, 1, capabilities, customs, "C:/OmniInc/omniinc.dll", |
| 243 | "Omni mobile", "Interface that provides accelerometer readings(omni)")); |
| 244 | |
| 245 | capabilities.clear(); |
| 246 | QVERIFY(compareDescriptor(interfaces[1], "com.omni.device.Lights","OMNI", 9, 0, capabilities, customs, "C:/OmniInc/omniinc.dll")); |
| 247 | |
| 248 | capabilities.clear(); |
| 249 | capabilities << "MultimediaDD" << "NetworkServices" << "ReadUserData" << "WriteUserData"; |
| 250 | QVERIFY(compareDescriptor(interfaces[2], "com.omni.service.Video", "OMNI", 1, 4, capabilities, customs, "C:/OmniInc/omniinc.dll")); |
| 251 | |
| 252 | QVERIFY(database.close()); |
| 253 | } |
| 254 | |
| 255 | void ServiceDatabaseUnitTest::searchByInterfaceName() |
| 256 | { |
| 257 | QServiceFilter filter; |
| 258 | QList<QServiceInterfaceDescriptor> interfaces; |
| 259 | |
| 260 | QVERIFY(database.open()); |
| 261 | |
| 262 | QString iface = "com.omni.device.Accelerometer"; |
| 263 | // == search via interface name only == |
| 264 | filter.setInterface(iface); |
| 265 | interfaces = database.getInterfaces(filter); |
| 266 | |
| 267 | QCOMPARE(interfaces.count(), 5); |
| 268 | QHash<QString,QString> customs; |
| 269 | QStringList capabilities; |
| 270 | capabilities << "SurroundingsDD"; |
| 271 | QVERIFY(compareDescriptor(interfaces[0], iface, "OMNI", 1, 1, capabilities,customs,"C:/OmniInc/omniinc.dll", |
| 272 | "Omni mobile", "Interface that provides accelerometer readings(omni)")); |
| 273 | QVERIFY(compareDescriptor(interfaces[1], iface, "LuthorCorp", 1, 2, capabilities,customs,"C:/Metropolis/kryptonite.dll", |
| 274 | "", "Interface that provides accelerometer readings")); //service description is empty tag in xml |
| 275 | QVERIFY(compareDescriptor(interfaces[2], iface, "WayneEnt", 2, 0, capabilities,customs,"C:/Gotham/knight.dll", |
| 276 | "", "Interface that provides accelerometer readings"));//no service description tag in xml |
| 277 | QVERIFY(compareDescriptor(interfaces[3], iface, "Primatech", 1, 4, capabilities,customs,"C:/NewYork/kensei.dll", |
| 278 | "Primatech Cellular Services", "Interface that provides accelerometer readings")); |
| 279 | QVERIFY(compareDescriptor(interfaces[4], iface, "Primatech", 1, 2, capabilities,customs,"C:/NewYork/kensei.dll", |
| 280 | "Primatech Cellular Services", "Interface that provides accelerometer readings")); |
| 281 | |
| 282 | //search with non-existent interface name |
| 283 | filter.setInterface("com.omni.device.FluxCapacitor"); |
| 284 | interfaces = database.getInterfaces(filter); |
| 285 | |
| 286 | QCOMPARE(interfaces.count(), 0); |
| 287 | |
| 288 | // == search for an exact version match == |
| 289 | filter.setInterface(iface, "1.4", QServiceFilter::ExactVersionMatch); |
| 290 | interfaces = database.getInterfaces(filter); |
| 291 | |
| 292 | QCOMPARE(interfaces.count(), 1); |
| 293 | QVERIFY(compareDescriptor(interfaces[0], iface, "Primatech", 1, 4)); |
| 294 | |
| 295 | //try exact version match but with multiple expected instances returned. |
| 296 | filter.setInterface("com.oMNi.device.accelerometer", "1.2", QServiceFilter::ExactVersionMatch); //also test case insensitivity |
| 297 | interfaces = database.getInterfaces(filter); |
| 298 | |
| 299 | QCOMPARE(interfaces.count(), 2); |
| 300 | QVERIFY(compareDescriptor(interfaces[0], iface, "LuthorCorp", 1, 2)); |
| 301 | QVERIFY(compareDescriptor(interfaces[1], iface, "Primatech", 1, 2)); |
| 302 | |
| 303 | //try exact match for an interface that exists but a version that doesn't |
| 304 | filter.setInterface(iface,"1.3", QServiceFilter::ExactVersionMatch); |
| 305 | interfaces = database.getInterfaces(filter); |
| 306 | QCOMPARE(interfaces.count(), 0); |
| 307 | |
| 308 | // == search for a minimum version match == |
| 309 | filter.setInterface(iface,"1.2", QServiceFilter::MinimumVersionMatch);//use existent version |
| 310 | interfaces = database.getInterfaces(filter); |
| 311 | QCOMPARE(interfaces.count(), 4); |
| 312 | |
| 313 | QVERIFY(compareDescriptor(interfaces[0], iface, "LuthorCorp", 1, 2)); |
| 314 | QVERIFY(compareDescriptor(interfaces[1], iface, "WayneEnt", 2, 0)); |
| 315 | QVERIFY(compareDescriptor(interfaces[2], iface, "Primatech", 1, 4)); |
| 316 | QVERIFY(compareDescriptor(interfaces[3], iface, "Primatech", 1, 2)); |
| 317 | |
| 318 | filter.setInterface(iface, "1.3", QServiceFilter::MinimumVersionMatch);//use non-existent version |
| 319 | interfaces = database.getInterfaces(filter); |
| 320 | QCOMPARE(interfaces.count(), 2); |
| 321 | QVERIFY(compareDescriptor(interfaces[0], iface, "WayneEnt", 2, 0)); |
| 322 | QVERIFY(compareDescriptor(interfaces[1], iface, "Primatech", 1, 4)); |
| 323 | |
| 324 | //try minimum version match that will find all available versions |
| 325 | filter.setInterface(iface,"1.0", QServiceFilter::MinimumVersionMatch); |
| 326 | interfaces = database.getInterfaces(filter); |
| 327 | QCOMPARE(interfaces.count(), 5); |
| 328 | |
| 329 | //try minimum version match but no implementations are available |
| 330 | filter.setInterface(iface,"9.0", QServiceFilter::MinimumVersionMatch);//use non-existent version |
| 331 | interfaces = database.getInterfaces(filter); |
| 332 | QCOMPARE(interfaces.count(), 0); |
| 333 | |
| 334 | //try setting an invalid version (should default to interface name search) |
| 335 | filter.setInterface(iface,"-1.0", QServiceFilter::MinimumVersionMatch);//use non-existent version |
| 336 | interfaces =database.getInterfaces(filter); |
| 337 | QCOMPARE(interfaces.count(), 0); |
| 338 | |
| 339 | // == search for an interface spread over multiple plugins by a single service== |
| 340 | iface = "com.dharma.electro.discharge"; |
| 341 | filter.setInterface(iface); |
| 342 | interfaces = database.getInterfaces(filter); |
| 343 | QCOMPARE(interfaces.count(), 4); |
| 344 | capabilities.clear(); |
| 345 | QVERIFY(compareDescriptor(interfaces[0], iface, "DharmaInitiative", 4, 0, capabilities,customs, "C:/island/swan.dll")); |
| 346 | QVERIFY(compareDescriptor(interfaces[1], iface, "DharmaInitiative", 8, 0, capabilities, customs,"C:/island/pearl.dll")); |
| 347 | QVERIFY(compareDescriptor(interfaces[2], iface, "DharmaInitiative", 15, 0, capabilities, customs,"C:/island/flame.dll")); |
| 348 | QVERIFY(compareDescriptor(interfaces[3], iface, "DharmaInitiative", 16, 0, capabilities, customs,"C:/island/flame.dll")); |
| 349 | |
| 350 | //try searching by minimum version for interface implementation spread over multiple plugins |
| 351 | filter.setInterface(iface, "5.0", QServiceFilter::MinimumVersionMatch); |
| 352 | interfaces = database.getInterfaces(filter); |
| 353 | QCOMPARE(interfaces.count(), 3); |
| 354 | QVERIFY(compareDescriptor(interfaces[0], iface, "DharmaInitiative", 8, 0, capabilities, customs,"C:/island/pearl.dll")); |
| 355 | QVERIFY(compareDescriptor(interfaces[1], iface, "DharmaInitiative", 15, 0, capabilities, customs,"C:/island/flame.dll")); |
| 356 | QVERIFY(compareDescriptor(interfaces[2], iface, "DharmaInitiative", 16, 0, capabilities, customs,"C:/island/flame.dll")); |
| 357 | |
| 358 | //try searching for a single version of an interface implementation |
| 359 | filter.setInterface(iface, "8.0", QServiceFilter::ExactVersionMatch); |
| 360 | interfaces = database.getInterfaces(filter); |
| 361 | QCOMPARE(interfaces.count(), 1); |
| 362 | QVERIFY(compareDescriptor(interfaces[0], iface, "DharmaInitiative", 8, 0, capabilities, customs,"C:/island/pearl.dll")); |
| 363 | |
| 364 | QVERIFY(database.close()); |
| 365 | } |
| 366 | |
| 367 | void ServiceDatabaseUnitTest::searchByInterfaceAndService() |
| 368 | { |
| 369 | QServiceFilter filter; |
| 370 | QList<QServiceInterfaceDescriptor> interfaces; |
| 371 | |
| 372 | QVERIFY(database.open()); |
| 373 | |
| 374 | // == search using only interface and service name == |
| 375 | filter.setServiceName("Omni"); |
| 376 | filter.setInterface("com.omni.device.Lights"); |
| 377 | interfaces = database.getInterfaces(filter); |
| 378 | QVERIFY(compareDescriptor(interfaces[0], "com.omni.device.Lights", "OMNI", 9, 0)); |
| 379 | |
| 380 | QCOMPARE(interfaces.count(), 1); |
| 381 | |
| 382 | //try searching with service that implements the same interface |
| 383 | //more than once |
| 384 | filter.setServiceName("Primatech"); |
| 385 | filter.setInterface("com.omni.device.Accelerometer"); |
| 386 | interfaces = database.getInterfaces(filter); |
| 387 | |
| 388 | QCOMPARE(interfaces.count(), 2); |
| 389 | QVERIFY(compareDescriptor(interfaces[0], "com.omni.device.Accelerometer", "Primatech", 1, 4)); |
| 390 | QVERIFY(compareDescriptor(interfaces[1], "com.omni.device.Accelerometer", "Primatech", 1, 2)); |
| 391 | |
| 392 | //try and existing service but non-existent interface |
| 393 | filter.setServiceName("Primatech"); |
| 394 | filter.setInterface("com.omni.device.FluxCapacitor"); |
| 395 | |
| 396 | interfaces = database.getInterfaces(filter); |
| 397 | QCOMPARE(interfaces.count(), 0); |
| 398 | |
| 399 | //try an non-existing service but an existing interface |
| 400 | filter.setServiceName("StarkInd"); |
| 401 | filter.setInterface("com.omni.device.Accelerometer"); |
| 402 | |
| 403 | interfaces = database.getInterfaces(filter); |
| 404 | QCOMPARE(interfaces.count(), 0); |
| 405 | |
| 406 | // == search using interface and service name and exact version match == |
| 407 | filter.setServiceName("Primatech"); |
| 408 | filter.setInterface("com.omni.device.Accelerometer", "1.2", QServiceFilter::ExactVersionMatch); |
| 409 | interfaces = database.getInterfaces(filter); |
| 410 | |
| 411 | QCOMPARE(interfaces.count(), 1); |
| 412 | QVERIFY(compareDescriptor(interfaces[0], "com.omni.device.Accelerometer", "Primatech", 1, 2)); |
| 413 | |
| 414 | //try an exact match for and non-existent interface version |
| 415 | filter.setInterface("com.omni.device.Accelerometer","1.3", QServiceFilter::ExactVersionMatch); |
| 416 | interfaces = database.getInterfaces(filter); |
| 417 | |
| 418 | // == search using interface and service name and minimum version match == |
| 419 | filter.setServiceName("Cyberdyne"); |
| 420 | filter.setInterface("com.cyberdyne.terminator","1.6", QServiceFilter::MinimumVersionMatch); |
| 421 | interfaces = database.getInterfaces(filter); |
| 422 | QCOMPARE(interfaces.count(), 4); |
| 423 | |
| 424 | QVERIFY(compareDescriptor(interfaces[0], "com.cyberdyne.terminator", "Cyberdyne", 2, 1)); |
| 425 | QVERIFY(compareDescriptor(interfaces[1], "com.cyberdyne.terminator", "Cyberdyne", 2, 0)); |
| 426 | QVERIFY(compareDescriptor(interfaces[2], "com.cyberdyne.terminator", "Cyberdyne", 1, 7)); |
| 427 | QVERIFY(compareDescriptor(interfaces[3], "com.cyberdyne.terminator", "Cyberdyne", 1, 6)); |
| 428 | |
| 429 | //try again with the same interface but a different service |
| 430 | filter.setServiceName("Skynet"); |
| 431 | filter.setInterface("com.cyberdyne.terminator", "1.6", QServiceFilter::MinimumVersionMatch); |
| 432 | interfaces = database.getInterfaces(filter); |
| 433 | QCOMPARE(interfaces.count(), 4); |
| 434 | |
| 435 | QVERIFY(compareDescriptor(interfaces[0], "com.cyberdyne.terminator", "skynet", 3, 6)); |
| 436 | QVERIFY(compareDescriptor(interfaces[1], "com.cyberdyne.terminator", "skynet", 2, 0)); |
| 437 | QVERIFY(compareDescriptor(interfaces[2], "com.cyberdyne.terminator", "skynet", 1, 8)); |
| 438 | QVERIFY(compareDescriptor(interfaces[3], "com.cyberdyne.terminator", "skynet", 1, 6)); |
| 439 | |
| 440 | //try with a non-existent interface version (but later versions exist) |
| 441 | filter.setServiceName("Skynet"); |
| 442 | filter.setInterface("com.cyberdyne.terminator","1.9", QServiceFilter::MinimumVersionMatch); |
| 443 | interfaces = database.getInterfaces(filter); |
| 444 | QVERIFY(compareDescriptor(interfaces[0], "com.cyberdyne.terminator", "skynet", 3, 6)); |
| 445 | QVERIFY(compareDescriptor(interfaces[1], "com.cyberdyne.terminator", "skynet", 2, 0)); |
| 446 | QCOMPARE(interfaces.count(), 2); |
| 447 | |
| 448 | // == using wildcard matching when searching, ie service and/or interface field is empty |
| 449 | filter.setServiceName(""); |
| 450 | filter.setInterface(""); |
| 451 | interfaces = database.getInterfaces(filter); |
| 452 | QCOMPARE(interfaces.count(), 36); |
| 453 | |
| 454 | // == searches on a service which is made up of multiple plugins |
| 455 | // try searching for all interfaces offered by the service |
| 456 | filter.setServiceName("DharmaInitiative"); |
| 457 | filter.setInterface(""); |
| 458 | interfaces = database.getInterfaces(filter); |
| 459 | QCOMPARE(interfaces.count(), 5); |
| 460 | |
| 461 | QHash<QString,QString> customs; |
| 462 | QStringList capabilities; |
| 463 | QVERIFY(compareDescriptor(interfaces[0], "com.dharma.electro.discharge", "DharmaInitiative", 4, 0, capabilities, customs, "C:/island/swan.dll", |
| 464 | "Department of Heuristics And Research on Material Applications(S)", |
| 465 | "Releases electromagnetic energy buildup every 108 minutes")); |
| 466 | QVERIFY(compareDescriptor(interfaces[1], "com.dharma.electro.discharge", "DharmaInitiative", 8, 0, capabilities, customs, "C:/island/pearl.dll", |
| 467 | "Department of Heuristics And Research on Material Applications(P)", |
| 468 | "Releases electromagnetic energy buildup every 108 minutes")); |
| 469 | QVERIFY(compareDescriptor(interfaces[2], "com.dharma.electro.discharge", "DharmaInitiative", 15, 0, capabilities, customs, "C:/island/flame.dll", |
| 470 | "Department of Heuristics And Research on Material Applications(F)", |
| 471 | "Releases electromagnetic energy buildup every 108 minutes")); |
| 472 | QVERIFY(compareDescriptor(interfaces[3], "com.dharma.radio", "DharmaInitiative", 8, 15, capabilities, customs, "C:/island/flame.dll", |
| 473 | "Department of Heuristics And Research on Material Applications(F)", |
| 474 | "Enables communication off island")); |
| 475 | QVERIFY(compareDescriptor(interfaces[4], "com.dharma.electro.discharge", "DharmaInitiative", 16, 0, capabilities, customs, "C:/island/flame.dll", |
| 476 | "Department of Heuristics And Research on Material Applications(F)", |
| 477 | "Releases electromagnetic energy buildup every 108 minutes")); |
| 478 | |
| 479 | // try searching for all implementations of a specific interface offered by the service |
| 480 | filter.setServiceName("DharmaInitiative"); |
| 481 | filter.setInterface("com.dharma.electro.discharge"); |
| 482 | interfaces = database.getInterfaces(filter); |
| 483 | QCOMPARE(interfaces.count(), 4); |
| 484 | QVERIFY(compareDescriptor(interfaces[0], "com.dharma.electro.discharge", "DharmaInitiative", 4, 0, capabilities, customs, "C:/island/swan.dll")); |
| 485 | QVERIFY(compareDescriptor(interfaces[1], "com.dharma.electro.discharge", "DharmaInitiative", 8, 0, capabilities, customs, "C:/island/pearl.dll")); |
| 486 | QVERIFY(compareDescriptor(interfaces[2], "com.dharma.electro.discharge", "DharmaInitiative", 15, 0, capabilities, customs, "C:/island/flame.dll")); |
| 487 | QVERIFY(compareDescriptor(interfaces[3], "com.dharma.electro.discharge", "DharmaInitiative", 16, 0, capabilities, customs, "C:/island/flame.dll")); |
| 488 | |
| 489 | //try doing a minimum version search |
| 490 | filter.setServiceName("DharmaInitiative"); |
| 491 | filter.setInterface("com.dharma.electro.discharge", "7.9", QServiceFilter::MinimumVersionMatch); |
| 492 | interfaces = database.getInterfaces(filter); |
| 493 | QCOMPARE(interfaces.count(), 3); |
| 494 | QVERIFY(compareDescriptor(interfaces[0], "com.dharma.electro.discharge", "DharmaInitiative", 8, 0, capabilities, customs, "C:/island/pearl.dll")); |
| 495 | QVERIFY(compareDescriptor(interfaces[1], "com.dharma.electro.discharge", "DharmaInitiative", 15, 0, capabilities, customs, "C:/island/flame.dll")); |
| 496 | QVERIFY(compareDescriptor(interfaces[2], "com.dharma.electro.discharge", "DharmaInitiative", 16, 0, capabilities, customs, "C:/island/flame.dll")); |
| 497 | |
| 498 | //try doing a exact version search |
| 499 | filter.setServiceName("DharmaInitiative"); |
| 500 | filter.setInterface("com.dharma.electro.discharge", "15.0", QServiceFilter::ExactVersionMatch); |
| 501 | interfaces = database.getInterfaces(filter); |
| 502 | QCOMPARE(interfaces.count(), 1); |
| 503 | QVERIFY(compareDescriptor(interfaces[0], "com.dharma.electro.discharge", "DharmaInitiative", 15, 0, capabilities, customs, "C:/island/flame.dll")); |
| 504 | |
| 505 | //trying setting invalid interface parameters, supply a version without an interface |
| 506 | filter.setInterface("", "3.0", QServiceFilter::MinimumVersionMatch); //this call should be ignored |
| 507 | interfaces = database.getInterfaces(filter); |
| 508 | QCOMPARE(interfaces.count(), 1); |
| 509 | QVERIFY(compareDescriptor(interfaces[0], "com.dharma.electro.discharge", "DharmaInitiative", 15, 0, capabilities, customs, "C:/island/flame.dll")); |
| 510 | |
| 511 | QVERIFY(database.close()); |
| 512 | } |
| 513 | |
| 514 | void ServiceDatabaseUnitTest::searchByCapability() |
| 515 | { |
| 516 | QServiceFilter filter; |
| 517 | QList<QServiceInterfaceDescriptor> interfaces; |
| 518 | QHash<QString,QString> customs; |
| 519 | QHash<QString,QString> customsW = customs; |
| 520 | customsW["weapon"] = ""; |
| 521 | |
| 522 | QVERIFY(database.open()); |
| 523 | |
| 524 | filter.setServiceName("Decepticon"); |
| 525 | interfaces = database.getInterfaces(filter); |
| 526 | QCOMPARE(interfaces.count(),4); |
| 527 | |
| 528 | filter.setCapabilities(QServiceFilter::MatchLoadable, QStringList()); |
| 529 | interfaces = database.getInterfaces(filter); |
| 530 | QCOMPARE(interfaces.count(),1); |
| 531 | QVERIFY(compareDescriptor(interfaces[0], "com.cybertron.transform", "Decepticon", 5, 3, |
| 532 | QStringList(), customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 533 | |
| 534 | QStringList caps; |
| 535 | caps << "hunt" << "spy"; |
| 536 | filter.setCapabilities(QServiceFilter::MatchMinimum, caps); |
| 537 | interfaces = database.getInterfaces(filter); |
| 538 | QCOMPARE(interfaces.count(),2); |
| 539 | QVERIFY(compareDescriptor(interfaces[0], "com.cybertron.transform", "Decepticon", 2, 0, |
| 540 | (QStringList()<<"hunt" << "spy" << "kill"), customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 541 | QVERIFY(compareDescriptor(interfaces[1], "com.cybertron.transform", "Decepticon", 2, 5, |
| 542 | (QStringList()<<"hunt" << "spy"), customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 543 | |
| 544 | filter.setCapabilities(QServiceFilter::MatchLoadable, caps); |
| 545 | interfaces = database.getInterfaces(filter); |
| 546 | QCOMPARE(interfaces.count(),3); |
| 547 | QVERIFY(compareDescriptor(interfaces[0], "com.cybertron.transform", "Decepticon", 1, 1, |
| 548 | QStringList()<<"hunt", customsW, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 549 | QVERIFY(compareDescriptor(interfaces[1], "com.cybertron.transform", "Decepticon", 5, 3, |
| 550 | QStringList() , customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 551 | QVERIFY(compareDescriptor(interfaces[2], "com.cybertron.transform", "Decepticon", 2, 5, |
| 552 | (QStringList()<<"hunt" << "spy"), customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 553 | |
| 554 | caps.clear(); |
| 555 | caps << "hunt"; |
| 556 | filter.setCapabilities(QServiceFilter::MatchMinimum, caps); |
| 557 | interfaces = database.getInterfaces(filter); |
| 558 | QCOMPARE(interfaces.count(),3); |
| 559 | QVERIFY(compareDescriptor(interfaces[0], "com.cybertron.transform", "Decepticon", 2, 0, |
| 560 | (QStringList()<<"hunt" << "spy" << "kill"), customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 561 | QVERIFY(compareDescriptor(interfaces[1], "com.cybertron.transform", "Decepticon", 1, 1, |
| 562 | QStringList()<<"hunt" , customsW, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 563 | QVERIFY(compareDescriptor(interfaces[2], "com.cybertron.transform", "Decepticon", 2, 5, |
| 564 | (QStringList()<<"hunt" << "spy"), customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 565 | |
| 566 | |
| 567 | filter.setCapabilities(QServiceFilter::MatchLoadable, caps); |
| 568 | interfaces = database.getInterfaces(filter); |
| 569 | QCOMPARE(interfaces.count(),2); |
| 570 | QVERIFY(compareDescriptor(interfaces[0], "com.cybertron.transform", "Decepticon", 1, 1, |
| 571 | QStringList()<<"hunt", customsW, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 572 | QVERIFY(compareDescriptor(interfaces[1], "com.cybertron.transform", "Decepticon", 5, 3, |
| 573 | QStringList(), customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 574 | |
| 575 | caps.clear(); |
| 576 | filter.setCapabilities(QServiceFilter::MatchMinimum, caps); |
| 577 | interfaces = database.getInterfaces(filter); |
| 578 | QCOMPARE(interfaces.count(),4); |
| 579 | QVERIFY(compareDescriptor(interfaces[0], "com.cybertron.transform", "Decepticon", 2, 0, |
| 580 | (QStringList()<<"hunt" << "spy" << "kill"), customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 581 | QVERIFY(compareDescriptor(interfaces[1], "com.cybertron.transform", "Decepticon", 1, 1, |
| 582 | QStringList()<<"hunt", customsW, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 583 | QVERIFY(compareDescriptor(interfaces[2], "com.cybertron.transform", "Decepticon", 5, 3, |
| 584 | QStringList(), customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 585 | QVERIFY(compareDescriptor(interfaces[3], "com.cybertron.transform", "Decepticon", 2, 5, |
| 586 | (QStringList()<<"hunt" << "spy"), customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 587 | |
| 588 | |
| 589 | filter.setCapabilities(QServiceFilter::MatchLoadable, caps); |
| 590 | interfaces = database.getInterfaces(filter); |
| 591 | QCOMPARE(interfaces.count(),1); |
| 592 | QVERIFY(compareDescriptor(interfaces[0], "com.cybertron.transform", "Decepticon", 5, 3, |
| 593 | QStringList(), customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 594 | |
| 595 | QServiceFilter emptyFilter; |
| 596 | emptyFilter.setCapabilities(QServiceFilter::MatchLoadable); |
| 597 | interfaces = database.getInterfaces(emptyFilter); |
| 598 | QCOMPARE(interfaces.count(), 14); //show all services which don't require any caps |
| 599 | } |
| 600 | |
| 601 | void ServiceDatabaseUnitTest::searchByCustomAttribute() |
| 602 | { |
| 603 | QServiceFilter filter; |
| 604 | QList<QServiceInterfaceDescriptor> interfaces; |
| 605 | QHash<QString,QString> customs; |
| 606 | QStringList capabilities; |
| 607 | |
| 608 | QVERIFY(database.open()); |
| 609 | |
| 610 | filter.setServiceName("Autobot"); |
| 611 | interfaces = database.getInterfaces(filter); |
| 612 | QCOMPARE(interfaces.count(),5); |
| 613 | |
| 614 | filter.setCustomAttribute("bot", "automatic"); |
| 615 | QCOMPARE(filter.customAttribute("bot"), QString("automatic")); |
| 616 | interfaces = database.getInterfaces(filter); |
| 617 | QCOMPARE(interfaces.count(),3); |
| 618 | |
| 619 | customs["bot"] = "automatic"; |
| 620 | customs["extension"] = "multidrive"; |
| 621 | QVERIFY(compareDescriptor(interfaces[0], "com.cybertron.transform", "Autobot", 2, 7, |
| 622 | capabilities, customs, "C:/Ark/matrix.dll", "Autobot Protection Services", "Transformation interface")); |
| 623 | |
| 624 | customs.clear(); |
| 625 | customs["bot"] = "automatic"; |
| 626 | QVERIFY(compareDescriptor(interfaces[1], "com.cybertron.transform", "Autobot", 2, 5, |
| 627 | capabilities, customs, "C:/Ark/matrix.dll", "Autobot Protection Services", "Transformation interface")); |
| 628 | |
| 629 | customs.clear(); |
| 630 | customs["bot"] = "automatic"; |
| 631 | customs["weapon"] = ""; |
| 632 | QVERIFY(compareDescriptor(interfaces[2], "com.cybertron.transform", "Autobot", 1, 9, |
| 633 | capabilities, customs, "C:/Ark/matrix.dll", "Autobot Protection Services", "Transformation interface")); |
| 634 | |
| 635 | filter.setCustomAttribute("extension","multidrive"); |
| 636 | QCOMPARE(filter.customAttribute("extension"), QString("multidrive")); |
| 637 | QCOMPARE(filter.customAttribute("bot"), QString("automatic")); |
| 638 | interfaces = database.getInterfaces(filter); |
| 639 | QCOMPARE(interfaces.count(),1); |
| 640 | |
| 641 | customs.clear(); |
| 642 | customs["bot"] = "automatic"; |
| 643 | customs["extension"] = "multidrive"; |
| 644 | QVERIFY(compareDescriptor(interfaces[0], "com.cybertron.transform", "Autobot", 2, 7, |
| 645 | capabilities, customs, "C:/Ark/matrix.dll", "Autobot Protection Services", "Transformation interface")); |
| 646 | |
| 647 | QServiceFilter manualFilter; |
| 648 | manualFilter.setCustomAttribute("bot", "manual"); |
| 649 | interfaces = database.getInterfaces(manualFilter); |
| 650 | QCOMPARE(interfaces.count(),2); |
| 651 | |
| 652 | customs.clear(); |
| 653 | customs["bot"]="manual"; |
| 654 | customs["weapon"]="laser"; |
| 655 | QVERIFY(compareDescriptor(interfaces[0], "com.cybertron.transform", "Autobot", 1, 0, |
| 656 | capabilities, customs, "C:/Ark/matrix.dll", "Autobot Protection Services", "Transformation interface")); |
| 657 | customs.clear(); |
| 658 | customs["bot"] = "manual"; |
| 659 | QVERIFY(compareDescriptor(interfaces[1], "com.cybertron.transform", "Autobot", 2, 0, |
| 660 | capabilities, customs, "C:/Ark/matrix.dll", "Autobot Protection Services", "Transformation interface")); |
| 661 | |
| 662 | QServiceFilter multidriveFilter; |
| 663 | multidriveFilter.setCustomAttribute("extension", "multidrive"); |
| 664 | interfaces = database.getInterfaces(multidriveFilter); |
| 665 | QCOMPARE(interfaces.count(),1); |
| 666 | |
| 667 | customs.clear(); |
| 668 | customs["bot"] = "automatic"; |
| 669 | customs["extension"] = "multidrive"; |
| 670 | QVERIFY(compareDescriptor(interfaces[0], "com.cybertron.transform", "Autobot", 2, 7, |
| 671 | capabilities, customs, "C:/Ark/matrix.dll", "Autobot Protection Services", "Transformation interface")); |
| 672 | |
| 673 | //test whether querying a custom property will affect the filter |
| 674 | filter.setServiceName(""); |
| 675 | filter.setInterface(""); |
| 676 | filter.clearCustomAttribute(); |
| 677 | interfaces = database.getInterfaces(filter); |
| 678 | QCOMPARE(interfaces.count(), 36); |
| 679 | QString customProperty = filter.customAttribute("spark"); |
| 680 | QVERIFY(customProperty.isEmpty()); |
| 681 | interfaces = database.getInterfaces(filter); |
| 682 | QCOMPARE(interfaces.count(), 36); |
| 683 | |
| 684 | //test the removal of a custom property from the filter |
| 685 | filter.setCustomAttribute("bot", "automatic"); |
| 686 | filter.setCustomAttribute("extension", "multidrive"); |
| 687 | QCOMPARE(filter.customAttributes().length(), 2); |
| 688 | filter.clearCustomAttribute("bot"); |
| 689 | QCOMPARE(filter.customAttributes().length(), 1); |
| 690 | filter.clearCustomAttribute("extension"); |
| 691 | QCOMPARE(filter.customAttributes().length(), 0); |
| 692 | |
| 693 | //test clearing of custom attributes |
| 694 | filter.setCustomAttribute("bot", "automatic"); |
| 695 | filter.setCustomAttribute("extension", "multidrive"); |
| 696 | QCOMPARE(filter.customAttributes().length(),2); |
| 697 | interfaces = database.getInterfaces(filter); |
| 698 | QCOMPARE(interfaces.count(), 1); |
| 699 | filter.clearCustomAttribute(); |
| 700 | QCOMPARE(filter.customAttributes().length(), 0); |
| 701 | interfaces = database.getInterfaces(filter); |
| 702 | QCOMPARE(interfaces.count(), 36); |
| 703 | |
| 704 | //test searching for an empty custom property |
| 705 | filter.setCustomAttribute("weapon", ""); |
| 706 | interfaces = database.getInterfaces(filter); |
| 707 | customs.clear(); |
| 708 | customs["bot"] = "automatic"; |
| 709 | customs["weapon"] = ""; |
| 710 | QCOMPARE(interfaces.length(), 2); |
| 711 | QVERIFY(compareDescriptor(interfaces[0], "com.cybertron.transform", "Autobot", 1, 9, |
| 712 | capabilities, customs, "C:/Ark/matrix.dll", "Autobot Protection Services", "Transformation interface")); |
| 713 | customs.clear(); |
| 714 | capabilities.clear(); |
| 715 | capabilities << "hunt"; |
| 716 | customs["weapon"]= ""; |
| 717 | QVERIFY(compareDescriptor(interfaces[1], "com.cybertron.transform", "Decepticon", 1, 1, |
| 718 | capabilities, customs, "C:/Cybertron/unicron.dll", "Decepticon Elimination Services", "Transformation interface")); |
| 719 | |
| 720 | |
| 721 | filter.clearCustomAttribute(); |
| 722 | |
| 723 | //test searching against a non-existent custom property |
| 724 | filter.setCustomAttribute("fluxcapacitor", "fluxing"); |
| 725 | interfaces = database.getInterfaces(filter); |
| 726 | QCOMPARE(interfaces.count(), 0); |
| 727 | QCOMPARE(database.lastError().code(), DBError::NoError); |
| 728 | |
| 729 | //try searching for custom property with service name and interface constraints |
| 730 | filter.clearCustomAttribute(); |
| 731 | filter.setServiceName("autobot"); |
| 732 | filter.setInterface("com.cybertron.transform", "2.0"); |
| 733 | filter.setCustomAttribute("bot", "automatic"); |
| 734 | interfaces = database.getInterfaces(filter); |
| 735 | QCOMPARE(interfaces.count(),2); |
| 736 | QVERIFY(interfaces[0].majorVersion() == 2 && interfaces[0].minorVersion() == 7); |
| 737 | QVERIFY(interfaces[1].majorVersion() == 2 && interfaces[1].minorVersion() == 5); |
| 738 | |
| 739 | //test that there is a difference between querying a custom |
| 740 | //property with an empty value and a custom property that has not been set |
| 741 | filter.clearCustomAttribute(); |
| 742 | filter.setCustomAttribute("AllSpark", ""); |
| 743 | QVERIFY(!filter.customAttribute("AllSpark").isNull()); |
| 744 | QVERIFY(filter.customAttribute("AllSpark").isEmpty()); |
| 745 | QVERIFY(filter.customAttribute("Non-existentProperty").isNull()); |
| 746 | |
| 747 | QVERIFY(database.close()); |
| 748 | } |
| 749 | |
| 750 | void ServiceDatabaseUnitTest::attributes() |
| 751 | { |
| 752 | QVERIFY(database.open()); |
| 753 | |
| 754 | // == Capability property == |
| 755 | //get empty list of capabilities from an interface registered with capabilities="" |
| 756 | QServiceFilter filter; |
| 757 | filter.setServiceName("acme"); |
| 758 | filter.setInterface("com.acme.service.location", "1.0", QServiceFilter::ExactVersionMatch); |
| 759 | QList<QServiceInterfaceDescriptor> interfaces = database.getInterfaces(filter); |
| 760 | QCOMPARE(database.lastError().code(), DBError::NoError); |
| 761 | QCOMPARE(interfaces.count(), 1); |
| 762 | QServiceInterfaceDescriptor interface = interfaces[0]; |
| 763 | QCOMPARE(interface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList(), QStringList()); |
| 764 | |
| 765 | //get empty list of capabilites from an interface registered without a |
| 766 | //capabilities attribute |
| 767 | filter.setServiceName("DHarmaInitiative"); |
| 768 | filter.setInterface("Com.dharma.electro.discharge", "16.0", QServiceFilter::ExactVersionMatch); |
| 769 | interfaces = database.getInterfaces(filter); |
| 770 | QCOMPARE(interfaces.count(), 1); |
| 771 | interface = interfaces[0]; |
| 772 | QCOMPARE(interface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList(), QStringList()); |
| 773 | |
| 774 | //get a list of capabilites from an interface with only 1 capability |
| 775 | filter.setServiceName("Omni"); |
| 776 | filter.setInterface("com.omni.device.Accelerometer", "1.1", QServiceFilter::ExactVersionMatch); |
| 777 | interfaces = database.getInterfaces(filter); |
| 778 | QCOMPARE(interfaces.count(), 1); |
| 779 | interface = interfaces[0]; |
| 780 | QStringList capabilities; |
| 781 | capabilities << "SurroundingsDD"; |
| 782 | QCOMPARE(interface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList(), capabilities); |
| 783 | |
| 784 | //get a list of capabilites from an interface with multiple capabilities |
| 785 | filter.setServiceName("Omni"); |
| 786 | filter.setInterface("com.omni.service.Video", "1.4", QServiceFilter::ExactVersionMatch); |
| 787 | interfaces = database.getInterfaces(filter); |
| 788 | QCOMPARE(interfaces.count(), 1); |
| 789 | interface = interfaces[0]; |
| 790 | capabilities.clear(); |
| 791 | capabilities << "MultimediaDD" << "NetworkServices" << "ReadUserData" << "WriteUserData"; |
| 792 | QCOMPARE(interface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList(), capabilities); |
| 793 | |
| 794 | //get a list of capabilities from a default service |
| 795 | interface = database.interfaceDefault("com.cyberdyne.terminator"); |
| 796 | capabilities.clear(); |
| 797 | capabilities << "NetworkServices"; |
| 798 | QCOMPARE(interface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList(), capabilities); |
| 799 | |
| 800 | // == Interface Description Property == |
| 801 | //get the interface description off an interface registered without a description tag |
| 802 | filter.setServiceName("Skynet"); |
| 803 | filter.setInterface("com.cyberdyne.terminator", "1.8", QServiceFilter::ExactVersionMatch); |
| 804 | interfaces = database.getInterfaces(filter); |
| 805 | QCOMPARE(interfaces.count(), 1); |
| 806 | interface = interfaces[0]; |
| 807 | QCOMPARE(interface.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString(), QString()); |
| 808 | |
| 809 | //get the interface description off an interface registered with an |
| 810 | //empty description tag |
| 811 | filter.setServiceName("Skynet"); |
| 812 | filter.setInterface("com.cyberdyne.terminator", "1,6", QServiceFilter::ExactVersionMatch); |
| 813 | interfaces = database.getInterfaces(filter); |
| 814 | QCOMPARE(interfaces.count(), 1); |
| 815 | interface = interfaces[0]; |
| 816 | QCOMPARE(interface.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString(), QString()); |
| 817 | |
| 818 | //get the interface description of an interface |
| 819 | filter.setServiceName("Skynet"); |
| 820 | filter.setInterface("com.cyberdyne.terminator", "1.5", QServiceFilter::ExactVersionMatch); |
| 821 | interfaces = database.getInterfaces(filter); |
| 822 | QCOMPARE(interfaces.count(), 1); |
| 823 | interface = interfaces[0]; |
| 824 | QCOMPARE(interface.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString(), |
| 825 | QString("Remote communications interface for the T-800v1.5")); |
| 826 | |
| 827 | //get a description from a default service |
| 828 | interface = database.interfaceDefault("com.omni.device.Accelerometer"); |
| 829 | QCOMPARE(interface.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString(), |
| 830 | QString("Interface that provides accelerometer readings(omni)")); |
| 831 | |
| 832 | QVERIFY(database.close()); |
| 833 | } |
| 834 | |
| 835 | void ServiceDatabaseUnitTest::getServiceNames() |
| 836 | { |
| 837 | QStringList services; |
| 838 | services = database.getServiceNames("com.acme.device.sysinfo"); |
| 839 | QCOMPARE(database.lastError().code(), DBError::DatabaseNotOpen); |
| 840 | QCOMPARE(services.count(), 0); |
| 841 | QVERIFY(database.open()); |
| 842 | |
| 843 | //try wildcard match to get all services |
| 844 | services = database.getServiceNames("" ); |
| 845 | QCOMPARE(database.lastError().code(), DBError::NoError); |
| 846 | QCOMPARE(services.count(), 10); |
| 847 | |
| 848 | //try an interface that is implemented by only one service |
| 849 | services = database.getServiceNames("com.acme.device.sysinfo"); |
| 850 | QCOMPARE(database.lastError().code(), DBError::NoError); |
| 851 | QCOMPARE(services.count(), 1); |
| 852 | QCOMPARE(services[0], QString("acme")); |
| 853 | |
| 854 | //try an interface which is implmented by multiple services |
| 855 | services = database.getServiceNames("COM.omni.device.ACCELerometer"); //also test case insensitivity |
| 856 | QCOMPARE(database.lastError().code(), DBError::NoError); |
| 857 | QCOMPARE(services.count(), 4); |
| 858 | QVERIFY(services.contains("LuthorCorp")); |
| 859 | QVERIFY(services.contains("OMNI")); |
| 860 | QVERIFY(services.contains("Primatech")); |
| 861 | QVERIFY(services.contains("WayneEnt")); |
| 862 | |
| 863 | //try again but with services that have multiple implementation versions of a particular implementation |
| 864 | services = database.getServiceNames("com.cyberdyne.terminator"); |
| 865 | QCOMPARE(database.lastError().code(), DBError::NoError); |
| 866 | QCOMPARE(services.count(), 2); |
| 867 | QVERIFY(services.contains("Cyberdyne")); |
| 868 | QVERIFY(services.contains("skynet")); |
| 869 | |
| 870 | //try with an interface implemented in multiple plugins |
| 871 | services = database.getServiceNames("com.dharma.electro.discharge"); |
| 872 | QCOMPARE(database.lastError().code(), DBError::NoError); |
| 873 | QCOMPARE(services.count(), 1); |
| 874 | QVERIFY(services.contains("DharmaInitiative", Qt::CaseInsensitive)); |
| 875 | |
| 876 | //try with a non-existing interface |
| 877 | services = database.getServiceNames("com.omni.device.FluxCapacitor"); |
| 878 | QCOMPARE(database.lastError().code(), DBError::NoError); |
| 879 | QCOMPARE(services.count(), 0); |
| 880 | |
| 881 | QVERIFY(database.close()); |
| 882 | } |
| 883 | |
| 884 | bool ServiceDatabaseUnitTest::compareDescriptor(QServiceInterfaceDescriptor interface, |
| 885 | QString interfaceName, QString serviceName, int majorVersion, int minorVersion) |
| 886 | { |
| 887 | interface.d->attributes[QServiceInterfaceDescriptor::Capabilities] = QStringList(); |
| 888 | |
| 889 | QHash<QString,QString> customs; |
| 890 | return compareDescriptor(interface, interfaceName, serviceName, majorVersion, minorVersion, |
| 891 | QStringList(), customs); |
| 892 | } |
| 893 | |
| 894 | bool ServiceDatabaseUnitTest::compareDescriptor(QServiceInterfaceDescriptor interface, |
| 895 | QString interfaceName, QString serviceName, int majorVersion, int minorVersion, |
| 896 | QStringList capabilities, const QHash<QString,QString> customProps, QString filePath, QString serviceDescription, |
| 897 | QString interfaceDescription) |
| 898 | { |
| 899 | |
| 900 | if(interface.interfaceName().compare(interfaceName, Qt::CaseInsensitive) !=0) { |
| 901 | qWarning() << "Interface name mismatch: expected =" << interfaceName |
| 902 | << " actual =" << interface.interfaceName(); |
| 903 | return false; |
| 904 | } |
| 905 | |
| 906 | if (interface.serviceName().compare(serviceName, Qt::CaseInsensitive) != 0) { |
| 907 | qWarning() << "Service name mismatch: expected =" << serviceName |
| 908 | << " actual =" << interface.serviceName(); |
| 909 | return false; |
| 910 | } |
| 911 | |
| 912 | if (interface.majorVersion() != majorVersion) { |
| 913 | qWarning() << "Major version mismatch: expected =" << majorVersion |
| 914 | << " actual =" << interface.majorVersion(); |
| 915 | return false; |
| 916 | } |
| 917 | |
| 918 | if (interface.minorVersion() != minorVersion) { |
| 919 | qWarning() << "Minor version mismatch: expected =" << minorVersion |
| 920 | << " actual =" << interface.minorVersion(); |
| 921 | return false; |
| 922 | } |
| 923 | |
| 924 | if (capabilities.count() != 0 || interface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList().count() != 0 ) { |
| 925 | QStringList securityCapabilities; |
| 926 | securityCapabilities = interface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList(); |
| 927 | |
| 928 | if(securityCapabilities.count() != capabilities.count()) { |
| 929 | qWarning() << "Capabilities count mismatch: expected =" << capabilities.count() |
| 930 | << " actual="<< securityCapabilities.count() |
| 931 | << "\texpected capabilities =" << capabilities |
| 932 | << "actual capabilities =" << securityCapabilities; |
| 933 | return false; |
| 934 | } |
| 935 | |
| 936 | for (int i = 0; i < securityCapabilities.count(); ++i) { |
| 937 | if (securityCapabilities[i] != capabilities[i]) { |
| 938 | qWarning() << "Capability mismatch: expected =" << capabilities[i] |
| 939 | << " actual =" << securityCapabilities[i]; |
| 940 | return false; |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if (!filePath.isEmpty()) { |
| 946 | if (interface.attribute(QServiceInterfaceDescriptor::Location).toString() != filePath) { |
| 947 | qWarning() << "File path mismatch: expected =" << filePath |
| 948 | << " actual =" << interface.attribute(QServiceInterfaceDescriptor::Location).toString(); |
| 949 | return false; |
| 950 | } |
| 951 | } |
| 952 | if (!serviceDescription.isEmpty()) { |
| 953 | if (interface.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString() != serviceDescription) { |
| 954 | qWarning() << "Service Description mismatch: expected =" << serviceDescription |
| 955 | << " actual=" << interface.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString(); |
| 956 | return false; |
| 957 | } |
| 958 | } |
| 959 | if (!interfaceDescription.isEmpty()) { |
| 960 | if (interface.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString() != interfaceDescription) { |
| 961 | qWarning() << "Interface Description mismatch: expected =" << interfaceDescription |
| 962 | << " actual =" << interface.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString(); |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | } |
| 967 | |
| 968 | if (interface.d->customAttributes.size() != customProps.size()) { |
| 969 | qWarning() << "Number of Interface custom attributes don't match. expected: " |
| 970 | <<customProps.size() << "actual: " << interface.d->customAttributes.size(); |
| 971 | ; |
| 972 | qWarning() << "expected:" << customProps << "actual:" << interface.d->customAttributes; |
| 973 | return false; |
| 974 | } |
| 975 | |
| 976 | QHash<QString, QString>::const_iterator i; |
| 977 | for (i = customProps.constBegin(); i!=customProps.constEnd(); i++) { |
| 978 | if (interface.customAttribute(i.key()) != i.value()) { |
| 979 | qWarning() << "Interface custom property mismatch: expected =" << i.key() <<"("<<i.value()<<")" |
| 980 | << " actual =" << i.key() << "(" << interface.customAttribute(i.key()) << ")"; |
| 981 | return false; |
| 982 | } |
| 983 | } |
| 984 | return true; |
| 985 | } |
| 986 | |
| 987 | void ServiceDatabaseUnitTest::defaultExternalIfaceIDs() |
| 988 | { |
| 989 | database.open(); |
| 990 | QServiceInterfaceDescriptor interface; |
| 991 | interface.d = new QServiceInterfaceDescriptorPrivate; |
| 992 | interface.d->serviceName = "StargateCommand"; |
| 993 | interface.d->interfaceName = "gov.usa.stargate"; |
| 994 | interface.d->major = 13; |
| 995 | interface.d->minor = 37; |
| 996 | |
| 997 | //see if we can set a "cross-reference" default interface |
| 998 | //ie user db referencing an interfaceID belonging to the system db |
| 999 | QVERIFY(database.setInterfaceDefault(interface, "FAKE-INTERFACE-ID")); |
| 1000 | QString interfaceID; |
| 1001 | QServiceInterfaceDescriptor descriptor = database.interfaceDefault("gov.usa.stargate", &interfaceID); |
| 1002 | QCOMPARE(database.lastError().code(), DBError::ExternalIfaceIDFound); |
| 1003 | QCOMPARE(interfaceID, QString("FAKE-INTERFACE-ID")); |
| 1004 | |
| 1005 | interface.d->interfaceName = "gov.ru.stargate"; |
| 1006 | QVERIFY(database.setInterfaceDefault(interface, "FAKE-INTERFACE-ID2")); |
| 1007 | QList<QPair<QString,QString> > externalDefaultsInfo = database.externalDefaultsInfo(); |
| 1008 | QCOMPARE(externalDefaultsInfo[0].second, QString("FAKE-INTERFACE-ID")); |
| 1009 | QCOMPARE(externalDefaultsInfo[1].second, QString("FAKE-INTERFACE-ID2")); |
| 1010 | |
| 1011 | //see if we can remove the "cross-reference" default interface |
| 1012 | QVERIFY(database.removeExternalDefaultServiceInterface("FAKE-INTERFACE-ID")); |
| 1013 | interfaceID.clear(); |
| 1014 | descriptor = database.interfaceDefault("gov.usa.stargate", &interfaceID); |
| 1015 | QVERIFY(database.lastError().code() == DBError::NotFound); |
| 1016 | QVERIFY(interfaceID.isEmpty()); |
| 1017 | |
| 1018 | //try to delete an interfaceID that's actually a local interfaceID |
| 1019 | QServiceFilter filter; |
| 1020 | filter.setServiceName("omnI"); |
| 1021 | filter.setInterface("com.omni.Device.Lights"); |
| 1022 | QList<QServiceInterfaceDescriptor> interfaces; |
| 1023 | interfaces = database.getInterfaces(filter); |
| 1024 | QCOMPARE(interfaces.count(), 1); |
| 1025 | interfaceID = database.getInterfaceID(interfaces[0]); |
| 1026 | QVERIFY(!database.removeExternalDefaultServiceInterface(interfaceID)); |
| 1027 | QVERIFY(database.lastError().code() == DBError::IfaceIDNotExternal); |
| 1028 | |
| 1029 | database.close(); |
| 1030 | } |
| 1031 | |
| 1032 | void ServiceDatabaseUnitTest::interfaceDefault() |
| 1033 | { |
| 1034 | QServiceInterfaceDescriptor interface; |
| 1035 | bool ok; |
| 1036 | |
| 1037 | //try getting the default service interface implementation when database is not open |
| 1038 | interface = database.interfaceDefault("com.cyberdyne.terminator"); |
| 1039 | QCOMPARE(database.lastError().code(), DBError::DatabaseNotOpen); |
| 1040 | QVERIFY(!interface.isValid()); |
| 1041 | |
| 1042 | //try getting a valid default, in this case only one implementation exists |
| 1043 | QVERIFY(database.open()); |
| 1044 | interface = database.interfaceDefault("com.omni.device.Lights"); |
| 1045 | QVERIFY(database.lastError().code() == DBError::NoError); |
| 1046 | QVERIFY(interface.isValid()); |
| 1047 | QStringList capabilities; |
| 1048 | QHash<QString,QString> customs; |
| 1049 | QVERIFY(compareDescriptor(interface, "com.omni.device.Lights", |
| 1050 | "OMNI", 9, 0, capabilities, customs, |
| 1051 | "C:/OmniInc/omniinc.dll", |
| 1052 | "Omni mobile", |
| 1053 | "Interface that provides access to device lights")); |
| 1054 | |
| 1055 | //try getting a valid default, in this case two services implement the interface |
| 1056 | ok = false; |
| 1057 | interface = database.interfaceDefault("com.CyBerDynE.Terminator"); |
| 1058 | QVERIFY(database.lastError().code() == DBError::NoError); |
| 1059 | QVERIFY(interface.isValid()); |
| 1060 | |
| 1061 | capabilities << "NetworkServices"; |
| 1062 | QVERIFY(compareDescriptor(interface, "com.cyberdyne.terminator", |
| 1063 | "Cyberdyne", 2,1, capabilities, customs, |
| 1064 | "C:/California/connor.dll", |
| 1065 | "Cyberdyne Termination Services", |
| 1066 | "Remote communications interface for the T-800")); |
| 1067 | |
| 1068 | //try getting a valid default, in this case multiple services implement the interface |
| 1069 | ok = false; |
| 1070 | interface = database.interfaceDefault("com.omni.device.Accelerometer"); |
| 1071 | QVERIFY(database.lastError().code() == DBError::NoError); |
| 1072 | QVERIFY(interface.isValid()); |
| 1073 | capabilities.clear(); |
| 1074 | capabilities << "SurroundingsDD"; |
| 1075 | QVERIFY(compareDescriptor(interface, "com.omni.device.Accelerometer", |
| 1076 | "OMNI", 1, 1, capabilities, customs, |
| 1077 | "C:/OmniInc/omniinc.dll", |
| 1078 | "Omni mobile", |
| 1079 | "Interface that provides accelerometer readings(omni)")); |
| 1080 | |
| 1081 | //try searching for an interface that isn't registered |
| 1082 | interface = database.interfaceDefault("com.omni.device.FluxCapacitor"); |
| 1083 | QVERIFY(database.lastError().code() == DBError::NotFound); |
| 1084 | QVERIFY(!interface.isValid()); |
| 1085 | |
| 1086 | //try getting the default interface impl for a service that is made up of multiple |
| 1087 | //plugins |
| 1088 | interface = database.interfaceDefault("com.dharma.electro.discharge"); |
| 1089 | QVERIFY(database.lastError().code() == DBError::NoError); |
| 1090 | capabilities.clear(); |
| 1091 | QVERIFY(compareDescriptor(interface, "com.dharma.electro.discharge", |
| 1092 | "DharmaInitiative", 4, 0, |
| 1093 | capabilities, customs, "C:/island/swan.dll")); |
| 1094 | |
| 1095 | //trying getting the default using an empty interface name |
| 1096 | interface = database.interfaceDefault(""); |
| 1097 | QVERIFY(database.lastError().code() == DBError::NotFound); |
| 1098 | QVERIFY(!interface.isValid()); |
| 1099 | QVERIFY(database.close()); |
| 1100 | } |
| 1101 | |
| 1102 | void ServiceDatabaseUnitTest::setInterfaceDefault() |
| 1103 | { |
| 1104 | QServiceInterfaceDescriptor interface; |
| 1105 | interface.d = new QServiceInterfaceDescriptorPrivate; |
| 1106 | interface.d->serviceName = "cyberdyne"; |
| 1107 | interface.d->interfaceName = "com.cyberdyne.terminator"; |
| 1108 | interface.d->major = 1; |
| 1109 | interface.d->minor = 6; |
| 1110 | |
| 1111 | QVERIFY(!database.setInterfaceDefault(interface)); |
| 1112 | QCOMPARE(database.lastError().code(), DBError::DatabaseNotOpen); |
| 1113 | |
| 1114 | QVERIFY(database.open()); |
| 1115 | |
| 1116 | //try setting the default to a older version provided by the same |
| 1117 | //service |
| 1118 | QServiceInterfaceDescriptor defaultInterface; |
| 1119 | defaultInterface = database.interfaceDefault("com.cyberdyne.terminator"); |
| 1120 | QVERIFY(compareDescriptor(defaultInterface, "com.cyberdyne.terminator", |
| 1121 | "Cyberdyne", 2, 1)); |
| 1122 | |
| 1123 | QVERIFY(database.setInterfaceDefault(interface)); |
| 1124 | defaultInterface = database.interfaceDefault("com.cyberdyne.terminator"); |
| 1125 | QVERIFY(compareDescriptor(defaultInterface, "com.cyberdyne.terminator", |
| 1126 | "Cyberdyne", 1, 6)); |
| 1127 | |
| 1128 | //try setting the default to another service |
| 1129 | interface.d->serviceName = "SKYnet"; //check that behaviour is case insensitive |
| 1130 | interface.d->interfaceName = "COM.cyberdyne.terminaTOR"; |
| 1131 | interface.d->major = 1; |
| 1132 | interface.d->minor = 5; |
| 1133 | |
| 1134 | QVERIFY(database.setInterfaceDefault(interface)); |
| 1135 | defaultInterface = database.interfaceDefault("com.CYBERDYNE.terminator"); |
| 1136 | QVERIFY(compareDescriptor(defaultInterface, "com.cyberdyne.terminator", |
| 1137 | "skynet", 1, 5)); |
| 1138 | |
| 1139 | //try setting the default of a service that is made up of multiple plugins |
| 1140 | interface.d->serviceName = "DharmaInitiative"; |
| 1141 | interface.d->interfaceName = "com.dharma.electro.discharge"; |
| 1142 | interface.d->major = 8; |
| 1143 | interface.d->minor = 0; |
| 1144 | QVERIFY(database.setInterfaceDefault(interface)); |
| 1145 | QStringList capabilities; |
| 1146 | QHash<QString,QString> customs; |
| 1147 | defaultInterface = database.interfaceDefault("com.dharma.electro.discharge"); |
| 1148 | QVERIFY(compareDescriptor(defaultInterface, "com.dharma.electro.discharge", |
| 1149 | "DharmaInitiative", 8, 0, capabilities, customs, |
| 1150 | "C:/island/pearl.dll")); |
| 1151 | |
| 1152 | //try setting the default to a implementation verison not supplied |
| 1153 | //by the service |
| 1154 | interface.d->serviceName = "SKYnet"; //check that behaviour is case insensitive |
| 1155 | interface.d->interfaceName = "COM.cyberdyne.terminaTOR"; |
| 1156 | interface.d->major = 1; |
| 1157 | interface.d->minor = 9; |
| 1158 | |
| 1159 | QVERIFY(!database.setInterfaceDefault(interface)); |
| 1160 | QCOMPARE(database.lastError().code(), DBError::NotFound); |
| 1161 | defaultInterface = database.interfaceDefault("com.CYBERDYNE.terminator"); |
| 1162 | QVERIFY(compareDescriptor(defaultInterface, "com.cyberdyne.terminator", |
| 1163 | "skynet", 1, 5)); |
| 1164 | |
| 1165 | //try setting the default of an interface that doesn't exist |
| 1166 | interface.d->serviceName = "SKYnet"; //check that behaviour is case insensitive |
| 1167 | interface.d->interfaceName = "com.omni.device.FluxCapacitor"; |
| 1168 | interface.d->major = 1; |
| 1169 | interface.d->minor = 5; |
| 1170 | |
| 1171 | QVERIFY(!database.setInterfaceDefault(interface)); |
| 1172 | QCOMPARE(database.lastError().code(), DBError::NotFound); |
| 1173 | defaultInterface = database.interfaceDefault("com.CYBERDYNE.terminator"); |
| 1174 | QVERIFY(compareDescriptor(defaultInterface, "com.cyberdyne.terminator", |
| 1175 | "skynet", 1, 5)); |
| 1176 | |
| 1177 | //try setting an interface default to a service that does not implement the interface |
| 1178 | interface.d->serviceName = "Primatech"; |
| 1179 | interface.d->interfaceName = "com.cyberdyne.terminator"; |
| 1180 | interface.d->major = 1; |
| 1181 | interface.d->minor = 4; |
| 1182 | QVERIFY(!database.setInterfaceDefault(interface)); |
| 1183 | QCOMPARE(database.lastError().code(), DBError::NotFound); |
| 1184 | defaultInterface = database.interfaceDefault("com.CYBERDYNE.terminator"); |
| 1185 | QVERIFY(compareDescriptor(defaultInterface, "com.cyberdyne.terminator", |
| 1186 | "skynet", 1, 5)); |
| 1187 | |
| 1188 | //try setting an interface default to a non-existent service |
| 1189 | interface.d->serviceName = "StarkInd"; //check that behaviour is case insensitive |
| 1190 | interface.d->interfaceName = "COM.cyberdyne.terminaTOR"; |
| 1191 | interface.d->major = 1; |
| 1192 | interface.d->minor = 5; |
| 1193 | QVERIFY(!database.setInterfaceDefault(interface)); |
| 1194 | QCOMPARE(database.lastError().code(), DBError::NotFound); |
| 1195 | defaultInterface = database.interfaceDefault("com.CYBERDYNE.terminator"); |
| 1196 | QVERIFY(compareDescriptor(defaultInterface, "com.cyberdyne.terminator", |
| 1197 | "skynet", 1, 5)); |
| 1198 | |
| 1199 | //try setting the default using an invalid interface |
| 1200 | QServiceInterfaceDescriptor invalidInterface; |
| 1201 | QVERIFY(!invalidInterface.isValid()); |
| 1202 | QVERIFY(!database.setInterfaceDefault(invalidInterface)); |
| 1203 | QCOMPARE(database.lastError().code(), DBError::NotFound); |
| 1204 | defaultInterface = database.interfaceDefault("com.CYBERDYNE.terminator"); |
| 1205 | QVERIFY(compareDescriptor(defaultInterface, "com.cyberdyne.terminator", |
| 1206 | "skynet", 1, 5)); |
| 1207 | |
| 1208 | QVERIFY(database.close()); |
| 1209 | } |
| 1210 | |
| 1211 | void ServiceDatabaseUnitTest::unregister() |
| 1212 | { |
| 1213 | QVERIFY(!unregisterService("acme")); |
| 1214 | QCOMPARE(database.lastError().code(), DBError::DatabaseNotOpen); |
| 1215 | |
| 1216 | QVERIFY(database.open()); |
| 1217 | |
| 1218 | //try unregister a non-existing service |
| 1219 | QVERIFY(!unregisterService("StarkInd")); |
| 1220 | QCOMPARE(database.lastError().code(), DBError::NotFound); |
| 1221 | QServiceFilter filter; |
| 1222 | |
| 1223 | // == check that the service to delete is already in the database == |
| 1224 | //try a search for descriptors by service name |
| 1225 | filter.setServiceName("omni"); |
| 1226 | QList<QServiceInterfaceDescriptor> interfaces; |
| 1227 | interfaces = database.getInterfaces(filter); |
| 1228 | QCOMPARE(interfaces.count(), 3); |
| 1229 | |
| 1230 | //search for service via interface name |
| 1231 | QStringList services; |
| 1232 | services = database.getServiceNames("com.omni.device.Accelerometer"); |
| 1233 | QCOMPARE(services.count(), 4); |
| 1234 | QVERIFY(services.contains("OMNI")); |
| 1235 | |
| 1236 | //search for descriptors via interface name |
| 1237 | filter.setServiceName(""); |
| 1238 | filter.setInterface("com.omni.device.Accelerometer"); |
| 1239 | interfaces = database.getInterfaces(filter); |
| 1240 | QCOMPARE(interfaces.count(), 5); |
| 1241 | bool serviceFound = false; |
| 1242 | foreach (QServiceInterfaceDescriptor interface, interfaces) { |
| 1243 | if(interface.serviceName() == "OMNI") |
| 1244 | serviceFound = true; |
| 1245 | } |
| 1246 | QVERIFY(serviceFound); |
| 1247 | |
| 1248 | //confirm that it is the default service for a couple of interfaces |
| 1249 | QServiceInterfaceDescriptor interface; |
| 1250 | interface = database.interfaceDefault("com.omni.device.Accelerometer"); |
| 1251 | QVERIFY(interface.serviceName() == "OMNI");//other services implement this interface |
| 1252 | |
| 1253 | interface = database.interfaceDefault("com.omni.service.Video"); |
| 1254 | QVERIFY(interface.serviceName() == "OMNI");//no other services implmement this interface |
| 1255 | |
| 1256 | //confirm that interface and service attributes exist for the service |
| 1257 | QStringList serviceIDs = getServiceIDs("omni"); |
| 1258 | foreach(const QString &serviceID, serviceIDs) |
| 1259 | QVERIFY(existsInServicePropertyTable(serviceID)); |
| 1260 | |
| 1261 | QStringList interfaceIDs = getInterfaceIDs("omni"); |
| 1262 | QCOMPARE(interfaceIDs.count(), 3); |
| 1263 | foreach(const QString &interfaceID, interfaceIDs) |
| 1264 | QVERIFY(existsInInterfacePropertyTable(interfaceID)); |
| 1265 | |
| 1266 | QVERIFY(unregisterService("oMni")); //ensure case insensitive behaviour |
| 1267 | |
| 1268 | // == check that deleted service and associated interfaces cannot be found == |
| 1269 | //try a search for descriptors by service name |
| 1270 | QServiceFilter serviceFilter; |
| 1271 | serviceFilter.setServiceName("omni"); |
| 1272 | interfaces = database.getInterfaces(serviceFilter); |
| 1273 | QCOMPARE(interfaces.count(), 0); |
| 1274 | |
| 1275 | //search for service via interface name |
| 1276 | services = database.getServiceNames("com.omni.device.Accelerometer"); |
| 1277 | QCOMPARE(services.count(), 3); |
| 1278 | QVERIFY(!services.contains("OMNI")); |
| 1279 | |
| 1280 | //search for descriptors via interface name |
| 1281 | filter.setServiceName(""); |
| 1282 | filter.setInterface("com.omni.device.Accelerometer"); |
| 1283 | interfaces = database.getInterfaces(filter); |
| 1284 | QCOMPARE(interfaces.count(), 4); |
| 1285 | serviceFound = false; |
| 1286 | foreach (QServiceInterfaceDescriptor interface, interfaces) { |
| 1287 | if(interface.serviceName() == "OMNI") |
| 1288 | serviceFound = true; |
| 1289 | } |
| 1290 | QVERIFY(!serviceFound); |
| 1291 | |
| 1292 | //ensure a new default interface has been assigned for the following interface |
| 1293 | interface = database.interfaceDefault("com.omni.device.Accelerometer"); |
| 1294 | QVERIFY(interface.isValid()); |
| 1295 | QCOMPARE(interface.serviceName(), QString("WayneEnt")); |
| 1296 | QCOMPARE(interface.majorVersion(), 2); |
| 1297 | QCOMPARE(interface.minorVersion(), 0); |
| 1298 | |
| 1299 | //ensure there is no longer a default for the following interface |
| 1300 | interface = database.interfaceDefault("com.omni.service.Video"); |
| 1301 | QVERIFY(!interface.isValid()); |
| 1302 | |
| 1303 | //ensure the associated interfaceIDs no longer exist in the Property |
| 1304 | //and Defaults tables |
| 1305 | foreach (const QString &serviceID, serviceIDs) |
| 1306 | QVERIFY(!existsInServicePropertyTable(serviceID)); |
| 1307 | foreach (const QString &interfaceID, interfaceIDs) |
| 1308 | QVERIFY(!existsInInterfacePropertyTable(interfaceID)); |
| 1309 | foreach(const QString &interfaceID, interfaceIDs) |
| 1310 | QVERIFY(!existsInDefaultsTable(interfaceID)); |
| 1311 | |
| 1312 | // == unregister an service that implements a default interface |
| 1313 | // but whose default interface name differs in case |
| 1314 | // from the other implementations it provides == |
| 1315 | interface.d = new QServiceInterfaceDescriptorPrivate; |
| 1316 | interface.d->serviceName = "Cyberdyne"; |
| 1317 | interface.d->interfaceName = "com.cyberdyne.terminator"; |
| 1318 | interface.d->major = 2; |
| 1319 | interface.d->minor = 0; |
| 1320 | QVERIFY(database.setInterfaceDefault(interface)); |
| 1321 | |
| 1322 | interface = database.interfaceDefault("com.cyberdyne.terminator"); |
| 1323 | QVERIFY(interface.isValid()); |
| 1324 | QVERIFY(unregisterService("cyberDYNE")); |
| 1325 | interface = database.interfaceDefault("com.cyberdyne.terminatOR"); |
| 1326 | QVERIFY(interface.isValid()); |
| 1327 | QVERIFY(compareDescriptor(interface, "com.cyberdyne.terminator", |
| 1328 | "skynet", 3, 6)); |
| 1329 | |
| 1330 | // == unregister a service that is made up of multiple plugins == |
| 1331 | // get the serviceIDs and interfaceIDs to make sure it's been deleted later on |
| 1332 | serviceIDs = getServiceIDs("DharmaInitiative"); |
| 1333 | QVERIFY(serviceIDs.count() > 0); |
| 1334 | interfaceIDs = getInterfaceIDs("DharmaInitiative"); |
| 1335 | QVERIFY(interfaceIDs.count() > 0); |
| 1336 | |
| 1337 | foreach(const QString &interfaceID, interfaceIDs) |
| 1338 | QVERIFY(existsInInterfacePropertyTable(interfaceID)); |
| 1339 | foreach(const QString &serviceID, serviceIDs) |
| 1340 | QVERIFY(existsInServicePropertyTable(serviceID)); |
| 1341 | |
| 1342 | QVERIFY(unregisterService("DHARMAInitiative")); |
| 1343 | interface = database.interfaceDefault("com.dharma.electro.discharge"); |
| 1344 | QVERIFY(!interface.isValid()); |
| 1345 | QCOMPARE(database.lastError().code(), DBError::NotFound); |
| 1346 | filter.setServiceName("DharmaInitiative"); |
| 1347 | filter.setInterface(""); |
| 1348 | interfaces = database.getInterfaces(filter); |
| 1349 | QCOMPARE(interfaces.count(), 0); |
| 1350 | |
| 1351 | foreach(const QString &serviceID, serviceIDs) |
| 1352 | QVERIFY(!existsInServicePropertyTable(serviceID)); |
| 1353 | foreach(const QString &interfaceID, interfaceIDs) |
| 1354 | QVERIFY(!existsInInterfacePropertyTable(interfaceID)); |
| 1355 | foreach(const QString &interfaceID, interfaceIDs) |
| 1356 | QVERIFY(!existsInDefaultsTable(interfaceID)); |
| 1357 | |
| 1358 | // == check that the service can be registered again |
| 1359 | // after it has been unregistered == |
| 1360 | QDir testdir = QDir(QFINDTESTDATA("testdata")); |
| 1361 | ServiceMetaData parser(testdir.absoluteFilePath("ServiceDharma_Flame.xml")); |
| 1362 | QVERIFY(parser.extractMetadata()); |
| 1363 | QVERIFY(registerService(parser.parseResults())); |
| 1364 | interface = database.interfaceDefault("com.dharma.electro.discharge"); |
| 1365 | QVERIFY(interface.isValid()); |
| 1366 | filter.setServiceName("DharmaInitiative"); |
| 1367 | filter.setInterface(""); |
| 1368 | interfaces = database.getInterfaces(filter); |
| 1369 | QCOMPARE(interfaces.count(), 3); |
| 1370 | |
| 1371 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceDharma_Swan.xml"))); |
| 1372 | QVERIFY(parser.extractMetadata()); |
| 1373 | QVERIFY(registerService(parser.parseResults())); |
| 1374 | filter.setServiceName("DharmaInitiative"); |
| 1375 | filter.setInterface(""); |
| 1376 | interfaces = database.getInterfaces(filter); |
| 1377 | QCOMPARE(interfaces.count(), 4); |
| 1378 | interface = database.interfaceDefault("com.dharma.electro.discharge"); |
| 1379 | QVERIFY(interface.isValid()); |
| 1380 | QStringList capabilities; |
| 1381 | QHash<QString,QString> customs; |
| 1382 | QVERIFY(compareDescriptor(interface, "com.dharma.electro.discharge", |
| 1383 | "DharmaInitiative", 16, 0, capabilities, customs, "C:/island/flame.dll")); |
| 1384 | } |
| 1385 | |
| 1386 | QStringList ServiceDatabaseUnitTest::getInterfaceIDs(const QString &serviceName) { |
| 1387 | QSqlDatabase sqlDatabase = QSqlDatabase::database(database.m_connectionName); |
| 1388 | QSqlQuery query(sqlDatabase); |
| 1389 | QString statement("Select Interface.ID FROM Interface, Service " |
| 1390 | "WHERE Service.ID = Interface.ServiceID " |
| 1391 | "AND Service.Name = ? COLLATE NOCASE"); |
| 1392 | QList<QVariant> bindValues; |
| 1393 | bindValues.append(serviceName); |
| 1394 | database.executeQuery(&query, statement, bindValues); |
| 1395 | |
| 1396 | QStringList ids; |
| 1397 | while (query.next()) { |
| 1398 | ids << query.value(0).toString(); |
| 1399 | } |
| 1400 | return ids; |
| 1401 | } |
| 1402 | |
| 1403 | QStringList ServiceDatabaseUnitTest::getServiceIDs(const QString &serviceName) { |
| 1404 | QSqlDatabase sqlDatabase = QSqlDatabase::database(database.m_connectionName); |
| 1405 | QSqlQuery query(sqlDatabase); |
| 1406 | QString statement("SELECT Service.ID from Service " |
| 1407 | "WHERE Service.Name = ? COLLATE NOCASE"); |
| 1408 | QList<QVariant> bindValues; |
| 1409 | bindValues.append(serviceName); |
| 1410 | database.executeQuery(&query, statement, bindValues); |
| 1411 | |
| 1412 | QStringList ids; |
| 1413 | while(query.next()) { |
| 1414 | ids << query.value(0).toString(); |
| 1415 | } |
| 1416 | return ids; |
| 1417 | } |
| 1418 | |
| 1419 | bool ServiceDatabaseUnitTest::existsInInterfacePropertyTable(const QString &interfaceID) |
| 1420 | { |
| 1421 | QSqlDatabase sqlDatabase = QSqlDatabase::database(database.m_connectionName); |
| 1422 | QSqlQuery query(sqlDatabase); |
| 1423 | |
| 1424 | QString statement("SELECT InterfaceID FROM InterfaceProperty " |
| 1425 | "WHERE InterfaceID = ?"); |
| 1426 | QList<QVariant> bindValues; |
| 1427 | bindValues.append(interfaceID); |
| 1428 | database.executeQuery(&query, statement, bindValues); |
| 1429 | if (query.next()) |
| 1430 | return true; |
| 1431 | else |
| 1432 | return false; |
| 1433 | } |
| 1434 | |
| 1435 | bool ServiceDatabaseUnitTest::existsInServicePropertyTable(const QString &serviceID) |
| 1436 | { |
| 1437 | QSqlDatabase sqlDatabase = QSqlDatabase::database(database.m_connectionName); |
| 1438 | QSqlQuery query(sqlDatabase); |
| 1439 | |
| 1440 | QString statement("SELECT ServiceID from ServiceProperty " |
| 1441 | "WHERE ServiceID = ?"); |
| 1442 | QList<QVariant> bindValues; |
| 1443 | bindValues.append(serviceID); |
| 1444 | database.executeQuery(&query, statement, bindValues); |
| 1445 | if (query.next()) |
| 1446 | return true; |
| 1447 | else |
| 1448 | return false; |
| 1449 | } |
| 1450 | |
| 1451 | bool ServiceDatabaseUnitTest::existsInDefaultsTable(const QString &interfaceID) |
| 1452 | { |
| 1453 | QSqlDatabase sqlDatabase = QSqlDatabase::database(database.m_connectionName); |
| 1454 | QSqlQuery query(sqlDatabase); |
| 1455 | QString statement("SELECT InterfaceID From Defaults " |
| 1456 | "WHERE InterfaceID = ?"); |
| 1457 | QList<QVariant> bindValues; |
| 1458 | bindValues.append(interfaceID); |
| 1459 | database.executeQuery(&query, statement, bindValues); |
| 1460 | |
| 1461 | if (query.next()) |
| 1462 | return true; |
| 1463 | else |
| 1464 | return false; |
| 1465 | } |
| 1466 | |
| 1467 | // Two small helper functions to assist with passing the security token to the servicedatabase. |
| 1468 | // Do not use this function if you intentionally want to provide emtpy securityToken, |
| 1469 | // but rather call database directly. |
| 1470 | bool ServiceDatabaseUnitTest::registerService(const ServiceMetaDataResults &service, const QString &securityToken) |
| 1471 | { |
| 1472 | #ifdef QT_SFW_SERVICEDATABASE_USE_SECURITY_TOKEN |
| 1473 | if (securityToken.isEmpty()) { |
| 1474 | return database.registerService(service, securityTokenOwner); |
| 1475 | } else { |
| 1476 | return database.registerService(service, securityToken); |
| 1477 | } |
| 1478 | #else |
| 1479 | Q_UNUSED(securityToken); |
| 1480 | return database.registerService(service); |
| 1481 | #endif |
| 1482 | } |
| 1483 | |
| 1484 | bool ServiceDatabaseUnitTest::unregisterService(const QString &serviceName, const QString &securityToken) |
| 1485 | { |
| 1486 | #ifdef QT_SFW_SERVICEDATABASE_USE_SECURITY_TOKEN |
| 1487 | if (securityToken.isEmpty()) { |
| 1488 | return database.unregisterService(serviceName, securityTokenOwner); |
| 1489 | } else { |
| 1490 | return database.unregisterService(serviceName, securityToken); |
| 1491 | } |
| 1492 | #else |
| 1493 | Q_UNUSED(securityToken); |
| 1494 | return database.unregisterService(serviceName); |
| 1495 | #endif |
| 1496 | } |
| 1497 | |
| 1498 | void ServiceDatabaseUnitTest::securityTokens() { |
| 1499 | #ifndef QT_SFW_SERVICEDATABASE_USE_SECURITY_TOKEN |
| 1500 | QSKIP("Security tokens are not enabled."); |
| 1501 | #endif |
| 1502 | // Clear databases just in case |
| 1503 | database.close(); |
| 1504 | QFile::remove(database.databasePath()); |
| 1505 | QDir testdir = QDir(QFINDTESTDATA("testdata")); |
| 1506 | QVERIFY(database.open()); |
| 1507 | database.m_databasePath = QDir::toNativeSeparators(QDir::currentPath().append("/services.db")); |
| 1508 | |
| 1509 | // Declare and setup testdata |
| 1510 | ServiceMetaData parser(testdir.absoluteFilePath("ServiceAcme.xml")); |
| 1511 | QVERIFY(parser.extractMetadata()); |
| 1512 | |
| 1513 | // Actual teststeps |
| 1514 | qDebug("---------- 1. Add and remove with same security token. (OK)"); |
| 1515 | QVERIFY(registerService(parser.parseResults(), securityTokenOwner)); |
| 1516 | QVERIFY(unregisterService("acme", securityTokenOwner)); |
| 1517 | |
| 1518 | qDebug("---------- 2. Add and remove with empty security token. (NOK)"); |
| 1519 | QVERIFY(!database.registerService(parser.parseResults())); |
| 1520 | QCOMPARE(database.lastError().code(), DBError::NoWritePermissions); |
| 1521 | QVERIFY(!database.unregisterService("acme")); |
| 1522 | QCOMPARE(database.lastError().code(), DBError::NoWritePermissions); |
| 1523 | |
| 1524 | qDebug("---------- 3. Add, then remove with different security token. (NOK)"); |
| 1525 | QVERIFY(registerService(parser.parseResults(), securityTokenOwner)); |
| 1526 | QVERIFY(!unregisterService("acme", securityTokenStranger)); |
| 1527 | QCOMPARE(database.lastError().code(), DBError::NoWritePermissions); |
| 1528 | QVERIFY(unregisterService("acme", securityTokenOwner)); |
| 1529 | |
| 1530 | qDebug("---------- 4. Add namesake but but differently located service with same security token. (OK)"); |
| 1531 | QStringList xmlFilesNamesakeServices; |
| 1532 | xmlFilesNamesakeServices << "ServiceDharma_Swan.xml" << "ServiceDharma_Pearl.xml"; |
| 1533 | foreach(const QString &file, xmlFilesNamesakeServices) { |
| 1534 | parser.setDevice(new QFile(testdir.absoluteFilePath(file))); |
| 1535 | QVERIFY(parser.extractMetadata()); |
| 1536 | QVERIFY(registerService(parser.parseResults(), securityTokenOwner)); |
| 1537 | } |
| 1538 | QVERIFY(!database.getServiceNames(QString()).isEmpty()); |
| 1539 | QVERIFY(unregisterService("DharmaInitiative", securityTokenOwner)); |
| 1540 | QVERIFY(database.getServiceNames(QString()).isEmpty()); |
| 1541 | |
| 1542 | qDebug("---------- 5. Add namesake but differently located services with different security token. (NOK)"); |
| 1543 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceDharma_Swan.xml"))); |
| 1544 | QVERIFY(parser.extractMetadata()); |
| 1545 | QVERIFY(registerService(parser.parseResults(), securityTokenOwner)); |
| 1546 | parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceDharma_Pearl.xml"))); |
| 1547 | QVERIFY(parser.extractMetadata()); |
| 1548 | QVERIFY(!registerService(parser.parseResults(), securityTokenStranger)); |
| 1549 | QCOMPARE(database.lastError().code(), DBError::NoWritePermissions); |
| 1550 | QVERIFY(unregisterService("DharmaInitiative", securityTokenOwner)); |
| 1551 | } |
| 1552 | |
| 1553 | void ServiceDatabaseUnitTest::cleanupTestCase() |
| 1554 | { |
| 1555 | database.close(); |
| 1556 | QFile::remove(database.databasePath()); |
| 1557 | } |
| 1558 | QTEST_MAIN(ServiceDatabaseUnitTest) |
| 1559 | |
| 1560 | #include "tst_servicedatabase.moc" |