94 QStringList getInterfaceIDs(const QString &serviceName); |
96 QStringList getInterfaceIDs(const QString &serviceName); |
95 QStringList getServiceIDs(const QString &serviceName); |
97 QStringList getServiceIDs(const QString &serviceName); |
96 bool existsInInterfacePropertyTable(const QString &interfaceID); |
98 bool existsInInterfacePropertyTable(const QString &interfaceID); |
97 bool existsInServicePropertyTable(const QString &serviceID); |
99 bool existsInServicePropertyTable(const QString &serviceID); |
98 bool existsInDefaultsTable(const QString &interfaceID); |
100 bool existsInDefaultsTable(const QString &interfaceID); |
|
101 bool registerService(const ServiceMetaDataResults &service, const QString &securityToken = QString()); |
|
102 bool unregisterService(const QString &serviceName, const QString &securityToken = QString()); |
99 |
103 |
100 ServiceMetaData* parser; |
104 ServiceMetaData* parser; |
101 ServiceDatabase database; |
105 ServiceDatabase database; |
102 }; |
106 }; |
103 |
107 |
|
108 void ServiceDatabaseUnitTest::initTestCase() |
|
109 { |
|
110 #if defined(Q_OS_SYMBIAN) |
|
111 database.m_databasePath = QDir::toNativeSeparators(QDir::currentPath().append("/services.db")); |
|
112 #endif |
|
113 database.close(); |
|
114 QFile::remove(database.databasePath()); |
|
115 } |
|
116 |
|
117 static const QString securityTokenOwner("SecurityTokenOwner"); |
|
118 static const QString securityTokenStranger("SecurityTokenStranger"); |
|
119 |
104 void ServiceDatabaseUnitTest::testRegistration() |
120 void ServiceDatabaseUnitTest::testRegistration() |
105 { |
121 { |
106 QDir testdir = QDir(TESTDATA_DIR "/testdata" ); |
122 QDir testdir = QDir(TESTDATA_DIR "/testdata" ); |
107 |
123 |
108 ServiceMetaData parser(testdir.absoluteFilePath("ServiceAcme.xml")); |
124 ServiceMetaData parser(testdir.absoluteFilePath("ServiceAcme.xml")); |
109 QVERIFY(parser.extractMetadata()); |
125 QVERIFY(parser.extractMetadata()); |
110 QVERIFY(!database.registerService(parser.parseResults())); |
126 QVERIFY(!registerService(parser.parseResults())); |
|
127 |
111 QCOMPARE(database.lastError().code(), DBError::DatabaseNotOpen); |
128 QCOMPARE(database.lastError().code(), DBError::DatabaseNotOpen); |
112 #if defined(Q_OS_SYMBIAN) |
129 #if defined(Q_OS_SYMBIAN) |
113 database.m_databasePath = QDir::toNativeSeparators(QDir::currentPath().append("/services.db")); |
130 database.m_databasePath = QDir::toNativeSeparators(QDir::currentPath().append("/services.db")); |
114 #endif |
131 #endif |
115 QVERIFY(database.open()); |
132 QVERIFY(database.open()); |
116 QVERIFY(database.registerService(parser.parseResults())); |
133 QVERIFY(registerService(parser.parseResults())); |
117 |
134 |
118 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceOmni.xml"))); |
135 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceOmni.xml"))); |
119 QVERIFY(parser.extractMetadata()); |
136 QVERIFY(parser.extractMetadata()); |
120 QVERIFY(database.registerService(parser.parseResults())); |
137 QVERIFY(registerService(parser.parseResults())); |
121 |
138 |
122 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceLuthorCorp.xml"))); |
139 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceLuthorCorp.xml"))); |
123 QVERIFY(parser.extractMetadata()); |
140 QVERIFY(parser.extractMetadata()); |
124 QVERIFY(database.registerService(parser.parseResults())); |
141 QVERIFY(registerService(parser.parseResults())); |
125 |
142 |
126 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceWayneEnt.xml"))); |
143 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceWayneEnt.xml"))); |
127 QVERIFY(parser.extractMetadata()); |
144 QVERIFY(parser.extractMetadata()); |
128 QVERIFY(database.registerService(parser.parseResults())); |
145 QVERIFY(registerService(parser.parseResults())); |
129 |
146 |
130 parser.setDevice(new QFile(testdir.absoluteFilePath("ServicePrimatech.xml"))); |
147 parser.setDevice(new QFile(testdir.absoluteFilePath("ServicePrimatech.xml"))); |
131 QVERIFY(parser.extractMetadata()); |
148 QVERIFY(parser.extractMetadata()); |
132 QVERIFY(database.registerService(parser.parseResults())); |
149 QVERIFY(registerService(parser.parseResults())); |
133 |
150 |
134 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceCyberdyne.xml"))); |
151 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceCyberdyne.xml"))); |
135 QVERIFY(parser.extractMetadata()); |
152 QVERIFY(parser.extractMetadata()); |
136 QVERIFY(database.registerService(parser.parseResults())); |
153 QVERIFY(registerService(parser.parseResults())); |
137 |
154 |
138 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceSkynet.xml"))); |
155 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceSkynet.xml"))); |
139 QVERIFY(parser.extractMetadata()); |
156 QVERIFY(parser.extractMetadata()); |
140 QVERIFY(database.registerService(parser.parseResults())); |
157 QVERIFY(registerService(parser.parseResults())); |
141 |
158 |
142 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceAutobot.xml"))); |
159 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceAutobot.xml"))); |
143 QVERIFY(parser.extractMetadata()); |
160 QVERIFY(parser.extractMetadata()); |
144 QVERIFY(database.registerService(parser.parseResults())); |
161 QVERIFY(registerService(parser.parseResults())); |
145 |
162 |
146 //try to register an already registered service |
163 //try to register an already registered service |
147 QVERIFY(!database.registerService(parser.parseResults())); |
164 QVERIFY(!registerService(parser.parseResults())); |
148 QCOMPARE(database.lastError().code(), DBError::LocationAlreadyRegistered); |
165 QCOMPARE(database.lastError().code(), DBError::LocationAlreadyRegistered); |
149 |
166 |
150 //try to register a service with a dll that provides interface implementations |
167 //try to register a service with a dll that provides interface implementations |
151 //that are already provided by a currently registered service |
168 //that are already provided by a currently registered service |
152 parser.setDevice(new QFile(testdir.absoluteFilePath("ServicePrimatech2error.xml"))); |
169 parser.setDevice(new QFile(testdir.absoluteFilePath("ServicePrimatech2error.xml"))); |
153 QVERIFY(parser.extractMetadata()); |
170 QVERIFY(parser.extractMetadata()); |
154 QVERIFY(!database.registerService(parser.parseResults())); |
171 QVERIFY(!registerService(parser.parseResults())); |
155 QCOMPARE(database.lastError().code(), DBError::IfaceImplAlreadyRegistered); |
172 QCOMPARE(database.lastError().code(), DBError::IfaceImplAlreadyRegistered); |
156 |
173 |
157 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceYamagatoError.xml"))); |
174 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceYamagatoError.xml"))); |
158 QVERIFY(parser.extractMetadata()); |
175 QVERIFY(parser.extractMetadata()); |
159 QVERIFY(!database.registerService(parser.parseResults())); |
176 QVERIFY(!registerService(parser.parseResults())); |
160 QCOMPARE(database.lastError().code(), DBError::LocationAlreadyRegistered); |
177 QCOMPARE(database.lastError().code(), DBError::LocationAlreadyRegistered); |
161 |
178 |
162 //make sure errors above are corectly rolled back by |
179 //make sure errors above are corectly rolled back by |
163 //registering a valid service |
180 //registering a valid service |
164 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceDecepticon.xml"))); |
181 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceDecepticon.xml"))); |
165 QVERIFY(parser.extractMetadata()); |
182 QVERIFY(parser.extractMetadata()); |
166 QVERIFY(database.registerService(parser.parseResults())); |
183 QVERIFY(registerService(parser.parseResults())); |
167 |
184 |
168 QStringList xmlFiles; |
185 QStringList xmlFiles; |
169 xmlFiles << "ServiceDharma_Swan.xml" |
186 xmlFiles << "ServiceDharma_Swan.xml" |
170 << "ServiceDharma_Pearl.xml" |
187 << "ServiceDharma_Pearl.xml" |
171 << "ServiceDharma_Flame.xml"; |
188 << "ServiceDharma_Flame.xml"; |
172 |
189 |
173 foreach(const QString &file, xmlFiles) { |
190 foreach(const QString &file, xmlFiles) { |
174 parser.setDevice(new QFile(testdir.absoluteFilePath(file))); |
191 parser.setDevice(new QFile(testdir.absoluteFilePath(file))); |
175 QVERIFY(parser.extractMetadata()); |
192 QVERIFY(parser.extractMetadata()); |
176 QVERIFY(database.registerService(parser.parseResults())); |
193 QVERIFY(registerService(parser.parseResults())); |
177 } |
194 } |
178 |
195 |
179 QVERIFY(database.close()); |
196 QVERIFY(database.close()); |
180 QVERIFY(database.close()); //verify we can close multiple times |
197 QVERIFY(database.close()); //verify we can close multiple times |
181 //without side effects |
198 //without side effects |
1355 // == check that the service can be registered again |
1372 // == check that the service can be registered again |
1356 // after it has been unregistered == |
1373 // after it has been unregistered == |
1357 QDir testdir = QDir(TESTDATA_DIR "/testdata" ); |
1374 QDir testdir = QDir(TESTDATA_DIR "/testdata" ); |
1358 ServiceMetaData parser(testdir.absoluteFilePath("ServiceDharma_Flame.xml")); |
1375 ServiceMetaData parser(testdir.absoluteFilePath("ServiceDharma_Flame.xml")); |
1359 QVERIFY(parser.extractMetadata()); |
1376 QVERIFY(parser.extractMetadata()); |
1360 QVERIFY(database.registerService(parser.parseResults())); |
1377 QVERIFY(registerService(parser.parseResults())); |
1361 interface = database.interfaceDefault("com.dharma.electro.discharge"); |
1378 interface = database.interfaceDefault("com.dharma.electro.discharge"); |
1362 QVERIFY(interface.isValid()); |
1379 QVERIFY(interface.isValid()); |
1363 filter.setServiceName("DharmaInitiative"); |
1380 filter.setServiceName("DharmaInitiative"); |
1364 filter.setInterface(""); |
1381 filter.setInterface(""); |
1365 interfaces = database.getInterfaces(filter); |
1382 interfaces = database.getInterfaces(filter); |
1366 QCOMPARE(interfaces.count(), 3); |
1383 QCOMPARE(interfaces.count(), 3); |
1367 |
1384 |
1368 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceDharma_Swan.xml"))); |
1385 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceDharma_Swan.xml"))); |
1369 QVERIFY(parser.extractMetadata()); |
1386 QVERIFY(parser.extractMetadata()); |
1370 QVERIFY(database.registerService(parser.parseResults())); |
1387 QVERIFY(registerService(parser.parseResults())); |
1371 filter.setServiceName("DharmaInitiative"); |
1388 filter.setServiceName("DharmaInitiative"); |
1372 filter.setInterface(""); |
1389 filter.setInterface(""); |
1373 interfaces = database.getInterfaces(filter); |
1390 interfaces = database.getInterfaces(filter); |
1374 QCOMPARE(interfaces.count(), 4); |
1391 QCOMPARE(interfaces.count(), 4); |
1375 interface = database.interfaceDefault("com.dharma.electro.discharge"); |
1392 interface = database.interfaceDefault("com.dharma.electro.discharge"); |
1459 return true; |
1476 return true; |
1460 else |
1477 else |
1461 return false; |
1478 return false; |
1462 } |
1479 } |
1463 |
1480 |
1464 void ServiceDatabaseUnitTest::cleanupTestCase() |
1481 // Two small helper functions to assist with passing the security token to the servicedatabase. |
1465 { |
1482 // Do not use this function if you intentionally want to provide emtpy securityToken, |
|
1483 // but rather call database directly. |
|
1484 bool ServiceDatabaseUnitTest::registerService(const ServiceMetaDataResults &service, const QString &securityToken) |
|
1485 { |
|
1486 #ifdef QT_SFW_SERVICEDATABASE_USE_SECURITY_TOKEN |
|
1487 if (securityToken.isEmpty()) { |
|
1488 return database.registerService(service, securityTokenOwner); |
|
1489 } else { |
|
1490 return database.registerService(service, securityToken); |
|
1491 } |
|
1492 #else |
|
1493 Q_UNUSED(securityToken); |
|
1494 return database.registerService(service); |
|
1495 #endif |
|
1496 } |
|
1497 |
|
1498 bool ServiceDatabaseUnitTest::unregisterService(const QString &serviceName, const QString &securityToken) |
|
1499 { |
|
1500 #ifdef QT_SFW_SERVICEDATABASE_USE_SECURITY_TOKEN |
|
1501 if (securityToken.isEmpty()) { |
|
1502 return database.unregisterService(serviceName, securityTokenOwner); |
|
1503 } else { |
|
1504 return database.unregisterService(serviceName, securityToken); |
|
1505 } |
|
1506 #else |
|
1507 Q_UNUSED(securityToken); |
|
1508 return database.unregisterService(serviceName); |
|
1509 #endif |
|
1510 } |
|
1511 |
|
1512 void ServiceDatabaseUnitTest::securityTokens() { |
|
1513 #ifndef QT_SFW_SERVICEDATABASE_USE_SECURITY_TOKEN |
|
1514 QSKIP("Security tokens are not enabled (currently only enabled on Symbian).", SkipAll); |
|
1515 #endif |
|
1516 // Clear databases just in case |
1466 database.close(); |
1517 database.close(); |
1467 QFile::remove(database.databasePath()); |
1518 QFile::remove(database.databasePath()); |
|
1519 QDir testdir = QDir(TESTDATA_DIR "/testdata" ); |
|
1520 QVERIFY(database.open()); |
|
1521 database.m_databasePath = QDir::toNativeSeparators(QDir::currentPath().append("/services.db")); |
|
1522 |
|
1523 // Declare and setup testdata |
|
1524 ServiceMetaData parser(testdir.absoluteFilePath("ServiceAcme.xml")); |
|
1525 QVERIFY(parser.extractMetadata()); |
|
1526 |
|
1527 // Actual teststeps |
|
1528 qDebug("---------- 1. Add and remove with same security token. (OK)"); |
|
1529 QVERIFY(registerService(parser.parseResults(), securityTokenOwner)); |
|
1530 QVERIFY(unregisterService("acme", securityTokenOwner)); |
|
1531 |
|
1532 qDebug("---------- 2. Add and remove with empty security token. (NOK)"); |
|
1533 QVERIFY(!database.registerService(parser.parseResults())); |
|
1534 QCOMPARE(database.lastError().code(), DBError::NoWritePermissions); |
|
1535 QVERIFY(!database.unregisterService("acme")); |
|
1536 QCOMPARE(database.lastError().code(), DBError::NoWritePermissions); |
|
1537 |
|
1538 qDebug("---------- 3. Add, then remove with different security token. (NOK)"); |
|
1539 QVERIFY(registerService(parser.parseResults(), securityTokenOwner)); |
|
1540 QVERIFY(!unregisterService("acme", securityTokenStranger)); |
|
1541 QCOMPARE(database.lastError().code(), DBError::NoWritePermissions); |
|
1542 QVERIFY(unregisterService("acme", securityTokenOwner)); |
|
1543 |
|
1544 qDebug("---------- 4. Add namesake but but differently located service with same security token. (OK)"); |
|
1545 QStringList xmlFilesNamesakeServices; |
|
1546 xmlFilesNamesakeServices << "ServiceDharma_Swan.xml" << "ServiceDharma_Pearl.xml"; |
|
1547 foreach(const QString &file, xmlFilesNamesakeServices) { |
|
1548 parser.setDevice(new QFile(testdir.absoluteFilePath(file))); |
|
1549 QVERIFY(parser.extractMetadata()); |
|
1550 QVERIFY(registerService(parser.parseResults(), securityTokenOwner)); |
|
1551 } |
|
1552 QVERIFY(!database.getServiceNames(QString()).isEmpty()); |
|
1553 QVERIFY(unregisterService("DharmaInitiative", securityTokenOwner)); |
|
1554 QVERIFY(database.getServiceNames(QString()).isEmpty()); |
|
1555 |
|
1556 qDebug("---------- 5. Add namesake but differently located services with different security token. (NOK)"); |
|
1557 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceDharma_Swan.xml"))); |
|
1558 QVERIFY(parser.extractMetadata()); |
|
1559 QVERIFY(registerService(parser.parseResults(), securityTokenOwner)); |
|
1560 parser.setDevice(new QFile(testdir.absoluteFilePath("ServiceDharma_Pearl.xml"))); |
|
1561 QVERIFY(parser.extractMetadata()); |
|
1562 QVERIFY(!registerService(parser.parseResults(), securityTokenStranger)); |
|
1563 QCOMPARE(database.lastError().code(), DBError::NoWritePermissions); |
|
1564 QVERIFY(unregisterService("DharmaInitiative", securityTokenOwner)); |
|
1565 } |
|
1566 |
|
1567 void ServiceDatabaseUnitTest::cleanupTestCase() |
|
1568 { |
|
1569 database.close(); |
|
1570 QFile::remove(database.databasePath()); |
1468 } |
1571 } |
1469 QTEST_MAIN(ServiceDatabaseUnitTest) |
1572 QTEST_MAIN(ServiceDatabaseUnitTest) |
1470 |
1573 |
1471 #include "tst_servicedatabase.moc" |
1574 #include "tst_servicedatabase.moc" |
1472 |
|