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