From b14934547a1828ba37009ef6803ab0ba419b690e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Wed, 27 Dec 2017 15:39:10 +0100 Subject: [PATCH] Use TESTDATA for QSsl* tests Apparently this (undocumented) TESTDATA feature creates resource files, but lets you use wildcards as well, which is very handy. The reason I didn't know/realize this when adapting the tests to use a ".qrc"-file* was because some of the test-cases were using relative paths instead of the 'testDataDir' variable. This commit fixes the remaining uses of relative paths, removes a usage of QDir::setCurrent, and adapts QSslSocket to use TESTDATA. * in now-reverted commit e1600c1a73e0594f5fbea8f7341a9552d6ae3fbb Change-Id: Iee6d88f1e0810eeaadac90e7d44bc6db84bfeabf Reviewed-by: Edward Welbourne Reviewed-by: Jesus Fernandez --- .../qsslcertificate/tst_qsslcertificate.cpp | 136 ++++++++---------- tests/auto/network/ssl/qsslkey/qsslkey.pro | 3 +- .../auto/network/ssl/qsslkey/tst_qsslkey.cpp | 6 +- .../network/ssl/qsslsocket/qsslsocket.pro | 2 + .../network/ssl/qsslsocket/tst_qsslsocket.cpp | 93 ++++++------ 5 files changed, 118 insertions(+), 122 deletions(-) diff --git a/tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp b/tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp index 1721b5b960..1477fd212e 100644 --- a/tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp +++ b/tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp @@ -57,11 +57,8 @@ class tst_QSslCertificate : public QObject void compareCertificates(const QSslCertificate & cert1, const QSslCertificate & cert2); #endif - QString oldCurrentDir; - public slots: void initTestCase(); - void cleanupTestCase(); #ifndef QT_NO_SSL private slots: @@ -123,12 +120,6 @@ void tst_QSslCertificate::initTestCase() if (!testDataDir.endsWith(QLatin1String("/"))) testDataDir += QLatin1String("/"); - if (QDir::current().absolutePath() != testDataDir) { - oldCurrentDir = QDir::current().absolutePath(); - QVERIFY2(QDir::setCurrent(testDataDir), - qPrintable(QString("Cannot change directory to %1").arg(testDataDir))); - } - QDir dir(testDataDir + "certificates"); QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::Readable); QRegExp rxCert(QLatin1String("^.+\\.(pem|der)$")); @@ -153,13 +144,6 @@ void tst_QSslCertificate::initTestCase() } } -void tst_QSslCertificate::cleanupTestCase() -{ - if (!oldCurrentDir.isEmpty()) { - QDir::setCurrent(oldCurrentDir); - } -} - #ifndef QT_NO_SSL void tst_QSslCertificate::hash() @@ -447,7 +431,7 @@ void tst_QSslCertificate::subjectAlternativeNames() void tst_QSslCertificate::utf8SubjectNames() { - QSslCertificate cert = QSslCertificate::fromPath("certificates/cert-ss-san-utf8.pem", QSsl::Pem, + QSslCertificate cert = QSslCertificate::fromPath(testDataDir + "certificates/cert-ss-san-utf8.pem", QSsl::Pem, QRegExp::FixedString).first(); QVERIFY(!cert.isNull()); @@ -570,60 +554,60 @@ void tst_QSslCertificate::fromPath_data() QTest::newRow("empty regexp der") << QString() << int(QRegExp::RegExp) << false << 0; QTest::newRow("empty wildcard pem") << QString() << int(QRegExp::Wildcard) << true << 0; QTest::newRow("empty wildcard der") << QString() << int(QRegExp::Wildcard) << false << 0; - QTest::newRow("\"certificates\" fixed pem") << QString("certificates") << int(QRegExp::FixedString) << true << 0; - QTest::newRow("\"certificates\" fixed der") << QString("certificates") << int(QRegExp::FixedString) << false << 0; - QTest::newRow("\"certificates\" regexp pem") << QString("certificates") << int(QRegExp::RegExp) << true << 0; - QTest::newRow("\"certificates\" regexp der") << QString("certificates") << int(QRegExp::RegExp) << false << 0; - QTest::newRow("\"certificates\" wildcard pem") << QString("certificates") << int(QRegExp::Wildcard) << true << 0; - QTest::newRow("\"certificates\" wildcard der") << QString("certificates") << int(QRegExp::Wildcard) << false << 0; - QTest::newRow("\"certificates/cert.pem\" fixed pem") << QString("certificates/cert.pem") << int(QRegExp::FixedString) << true << 1; - QTest::newRow("\"certificates/cert.pem\" fixed der") << QString("certificates/cert.pem") << int(QRegExp::FixedString) << false << 0; - QTest::newRow("\"certificates/cert.pem\" regexp pem") << QString("certificates/cert.pem") << int(QRegExp::RegExp) << true << 1; - QTest::newRow("\"certificates/cert.pem\" regexp der") << QString("certificates/cert.pem") << int(QRegExp::RegExp) << false << 0; - QTest::newRow("\"certificates/cert.pem\" wildcard pem") << QString("certificates/cert.pem") << int(QRegExp::Wildcard) << true << 1; - QTest::newRow("\"certificates/cert.pem\" wildcard der") << QString("certificates/cert.pem") << int(QRegExp::Wildcard) << false << 0; - QTest::newRow("\"certificates/*\" fixed pem") << QString("certificates/*") << int(QRegExp::FixedString) << true << 0; - QTest::newRow("\"certificates/*\" fixed der") << QString("certificates/*") << int(QRegExp::FixedString) << false << 0; - QTest::newRow("\"certificates/*\" regexp pem") << QString("certificates/*") << int(QRegExp::RegExp) << true << 0; - QTest::newRow("\"certificates/*\" regexp der") << QString("certificates/*") << int(QRegExp::RegExp) << false << 0; - QTest::newRow("\"certificates/*\" wildcard pem") << QString("certificates/*") << int(QRegExp::Wildcard) << true << 7; - QTest::newRow("\"certificates/ca*\" wildcard pem") << QString("certificates/ca*") << int(QRegExp::Wildcard) << true << 1; - QTest::newRow("\"certificates/cert*\" wildcard pem") << QString("certificates/cert*") << int(QRegExp::Wildcard) << true << 4; - QTest::newRow("\"certificates/cert-[sure]*\" wildcard pem") << QString("certificates/cert-[sure]*") << int(QRegExp::Wildcard) << true << 3; - QTest::newRow("\"certificates/cert-[not]*\" wildcard pem") << QString("certificates/cert-[not]*") << int(QRegExp::Wildcard) << true << 0; - QTest::newRow("\"certificates/*\" wildcard der") << QString("certificates/*") << int(QRegExp::Wildcard) << false << 2; - QTest::newRow("\"c*/c*.pem\" fixed pem") << QString("c*/c*.pem") << int(QRegExp::FixedString) << true << 0; - QTest::newRow("\"c*/c*.pem\" fixed der") << QString("c*/c*.pem") << int(QRegExp::FixedString) << false << 0; - QTest::newRow("\"c*/c*.pem\" regexp pem") << QString("c*/c*.pem") << int(QRegExp::RegExp) << true << 0; - QTest::newRow("\"c*/c*.pem\" regexp der") << QString("c*/c*.pem") << int(QRegExp::RegExp) << false << 0; - QTest::newRow("\"c*/c*.pem\" wildcard pem") << QString("c*/c*.pem") << int(QRegExp::Wildcard) << true << 5; - QTest::newRow("\"c*/c*.pem\" wildcard der") << QString("c*/c*.pem") << int(QRegExp::Wildcard) << false << 0; - QTest::newRow("\"d*/c*.pem\" fixed pem") << QString("d*/c*.pem") << int(QRegExp::FixedString) << true << 0; - QTest::newRow("\"d*/c*.pem\" fixed der") << QString("d*/c*.pem") << int(QRegExp::FixedString) << false << 0; - QTest::newRow("\"d*/c*.pem\" regexp pem") << QString("d*/c*.pem") << int(QRegExp::RegExp) << true << 0; - QTest::newRow("\"d*/c*.pem\" regexp der") << QString("d*/c*.pem") << int(QRegExp::RegExp) << false << 0; - QTest::newRow("\"d*/c*.pem\" wildcard pem") << QString("d*/c*.pem") << int(QRegExp::Wildcard) << true << 0; - QTest::newRow("\"d*/c*.pem\" wildcard der") << QString("d*/c*.pem") << int(QRegExp::Wildcard) << false << 0; - QTest::newRow("\"c.*/c.*.pem\" fixed pem") << QString("c.*/c.*.pem") << int(QRegExp::FixedString) << true << 0; - QTest::newRow("\"c.*/c.*.pem\" fixed der") << QString("c.*/c.*.pem") << int(QRegExp::FixedString) << false << 0; - QTest::newRow("\"c.*/c.*.pem\" regexp pem") << QString("c.*/c.*.pem") << int(QRegExp::RegExp) << true << 5; - QTest::newRow("\"c.*/c.*.pem\" regexp der") << QString("c.*/c.*.pem") << int(QRegExp::RegExp) << false << 0; - QTest::newRow("\"c.*/c.*.pem\" wildcard pem") << QString("c.*/c.*.pem") << int(QRegExp::Wildcard) << true << 0; - QTest::newRow("\"c.*/c.*.pem\" wildcard der") << QString("c.*/c.*.pem") << int(QRegExp::Wildcard) << false << 0; - QTest::newRow("\"d.*/c.*.pem\" fixed pem") << QString("d.*/c.*.pem") << int(QRegExp::FixedString) << true << 0; - QTest::newRow("\"d.*/c.*.pem\" fixed der") << QString("d.*/c.*.pem") << int(QRegExp::FixedString) << false << 0; - QTest::newRow("\"d.*/c.*.pem\" regexp pem") << QString("d.*/c.*.pem") << int(QRegExp::RegExp) << true << 0; - QTest::newRow("\"d.*/c.*.pem\" regexp der") << QString("d.*/c.*.pem") << int(QRegExp::RegExp) << false << 0; - QTest::newRow("\"d.*/c.*.pem\" wildcard pem") << QString("d.*/c.*.pem") << int(QRegExp::Wildcard) << true << 0; - QTest::newRow("\"d.*/c.*.pem\" wildcard der") << QString("d.*/c.*.pem") << int(QRegExp::Wildcard) << false << 0; + QTest::newRow("\"certificates\" fixed pem") << (testDataDir + "certificates") << int(QRegExp::FixedString) << true << 0; + QTest::newRow("\"certificates\" fixed der") << (testDataDir + "certificates") << int(QRegExp::FixedString) << false << 0; + QTest::newRow("\"certificates\" regexp pem") << (testDataDir + "certificates") << int(QRegExp::RegExp) << true << 0; + QTest::newRow("\"certificates\" regexp der") << (testDataDir + "certificates") << int(QRegExp::RegExp) << false << 0; + QTest::newRow("\"certificates\" wildcard pem") << (testDataDir + "certificates") << int(QRegExp::Wildcard) << true << 0; + QTest::newRow("\"certificates\" wildcard der") << (testDataDir + "certificates") << int(QRegExp::Wildcard) << false << 0; + QTest::newRow("\"certificates/cert.pem\" fixed pem") << (testDataDir + "certificates/cert.pem") << int(QRegExp::FixedString) << true << 1; + QTest::newRow("\"certificates/cert.pem\" fixed der") << (testDataDir + "certificates/cert.pem") << int(QRegExp::FixedString) << false << 0; + QTest::newRow("\"certificates/cert.pem\" regexp pem") << (testDataDir + "certificates/cert.pem") << int(QRegExp::RegExp) << true << 1; + QTest::newRow("\"certificates/cert.pem\" regexp der") << (testDataDir + "certificates/cert.pem") << int(QRegExp::RegExp) << false << 0; + QTest::newRow("\"certificates/cert.pem\" wildcard pem") << (testDataDir + "certificates/cert.pem") << int(QRegExp::Wildcard) << true << 1; + QTest::newRow("\"certificates/cert.pem\" wildcard der") << (testDataDir + "certificates/cert.pem") << int(QRegExp::Wildcard) << false << 0; + QTest::newRow("\"certificates/*\" fixed pem") << (testDataDir + "certificates/*") << int(QRegExp::FixedString) << true << 0; + QTest::newRow("\"certificates/*\" fixed der") << (testDataDir + "certificates/*") << int(QRegExp::FixedString) << false << 0; + QTest::newRow("\"certificates/*\" regexp pem") << (testDataDir + "certificates/*") << int(QRegExp::RegExp) << true << 0; + QTest::newRow("\"certificates/*\" regexp der") << (testDataDir + "certificates/*") << int(QRegExp::RegExp) << false << 0; + QTest::newRow("\"certificates/*\" wildcard pem") << (testDataDir + "certificates/*") << int(QRegExp::Wildcard) << true << 7; + QTest::newRow("\"certificates/ca*\" wildcard pem") << (testDataDir + "certificates/ca*") << int(QRegExp::Wildcard) << true << 1; + QTest::newRow("\"certificates/cert*\" wildcard pem") << (testDataDir + "certificates/cert*") << int(QRegExp::Wildcard) << true << 4; + QTest::newRow("\"certificates/cert-[sure]*\" wildcard pem") << (testDataDir + "certificates/cert-[sure]*") << int(QRegExp::Wildcard) << true << 3; + QTest::newRow("\"certificates/cert-[not]*\" wildcard pem") << (testDataDir + "certificates/cert-[not]*") << int(QRegExp::Wildcard) << true << 0; + QTest::newRow("\"certificates/*\" wildcard der") << (testDataDir + "certificates/*") << int(QRegExp::Wildcard) << false << 2; + QTest::newRow("\"c*/c*.pem\" fixed pem") << (testDataDir + "c*/c*.pem") << int(QRegExp::FixedString) << true << 0; + QTest::newRow("\"c*/c*.pem\" fixed der") << (testDataDir + "c*/c*.pem") << int(QRegExp::FixedString) << false << 0; + QTest::newRow("\"c*/c*.pem\" regexp pem") << (testDataDir + "c*/c*.pem") << int(QRegExp::RegExp) << true << 0; + QTest::newRow("\"c*/c*.pem\" regexp der") << (testDataDir + "c*/c*.pem") << int(QRegExp::RegExp) << false << 0; + QTest::newRow("\"c*/c*.pem\" wildcard pem") << (testDataDir + "c*/c*.pem") << int(QRegExp::Wildcard) << true << 5; + QTest::newRow("\"c*/c*.pem\" wildcard der") << (testDataDir + "c*/c*.pem") << int(QRegExp::Wildcard) << false << 0; + QTest::newRow("\"d*/c*.pem\" fixed pem") << (testDataDir + "d*/c*.pem") << int(QRegExp::FixedString) << true << 0; + QTest::newRow("\"d*/c*.pem\" fixed der") << (testDataDir + "d*/c*.pem") << int(QRegExp::FixedString) << false << 0; + QTest::newRow("\"d*/c*.pem\" regexp pem") << (testDataDir + "d*/c*.pem") << int(QRegExp::RegExp) << true << 0; + QTest::newRow("\"d*/c*.pem\" regexp der") << (testDataDir + "d*/c*.pem") << int(QRegExp::RegExp) << false << 0; + QTest::newRow("\"d*/c*.pem\" wildcard pem") << (testDataDir + "d*/c*.pem") << int(QRegExp::Wildcard) << true << 0; + QTest::newRow("\"d*/c*.pem\" wildcard der") << (testDataDir + "d*/c*.pem") << int(QRegExp::Wildcard) << false << 0; + QTest::newRow("\"c.*/c.*.pem\" fixed pem") << (testDataDir + "c.*/c.*.pem") << int(QRegExp::FixedString) << true << 0; + QTest::newRow("\"c.*/c.*.pem\" fixed der") << (testDataDir + "c.*/c.*.pem") << int(QRegExp::FixedString) << false << 0; + QTest::newRow("\"c.*/c.*.pem\" regexp pem") << (testDataDir + "c.*/c.*.pem") << int(QRegExp::RegExp) << true << 5; + QTest::newRow("\"c.*/c.*.pem\" regexp der") << (testDataDir + "c.*/c.*.pem") << int(QRegExp::RegExp) << false << 0; + QTest::newRow("\"c.*/c.*.pem\" wildcard pem") << (testDataDir + "c.*/c.*.pem") << int(QRegExp::Wildcard) << true << 0; + QTest::newRow("\"c.*/c.*.pem\" wildcard der") << (testDataDir + "c.*/c.*.pem") << int(QRegExp::Wildcard) << false << 0; + QTest::newRow("\"d.*/c.*.pem\" fixed pem") << (testDataDir + "d.*/c.*.pem") << int(QRegExp::FixedString) << true << 0; + QTest::newRow("\"d.*/c.*.pem\" fixed der") << (testDataDir + "d.*/c.*.pem") << int(QRegExp::FixedString) << false << 0; + QTest::newRow("\"d.*/c.*.pem\" regexp pem") << (testDataDir + "d.*/c.*.pem") << int(QRegExp::RegExp) << true << 0; + QTest::newRow("\"d.*/c.*.pem\" regexp der") << (testDataDir + "d.*/c.*.pem") << int(QRegExp::RegExp) << false << 0; + QTest::newRow("\"d.*/c.*.pem\" wildcard pem") << (testDataDir + "d.*/c.*.pem") << int(QRegExp::Wildcard) << true << 0; + QTest::newRow("\"d.*/c.*.pem\" wildcard der") << (testDataDir + "d.*/c.*.pem") << int(QRegExp::Wildcard) << false << 0; #ifdef Q_OS_LINUX QTest::newRow("absolute path wildcard pem") << (testDataDir + "certificates/*.pem") << int(QRegExp::Wildcard) << true << 7; #endif - QTest::newRow("trailing-whitespace") << QString("more-certificates/trailing-whitespace.pem") << int(QRegExp::FixedString) << true << 1; - QTest::newRow("no-ending-newline") << QString("more-certificates/no-ending-newline.pem") << int(QRegExp::FixedString) << true << 1; - QTest::newRow("malformed-just-begin") << QString("more-certificates/malformed-just-begin.pem") << int(QRegExp::FixedString) << true << 0; - QTest::newRow("malformed-just-begin-no-newline") << QString("more-certificates/malformed-just-begin-no-newline.pem") << int(QRegExp::FixedString) << true << 0; + QTest::newRow("trailing-whitespace") << (testDataDir + "more-certificates/trailing-whitespace.pem") << int(QRegExp::FixedString) << true << 1; + QTest::newRow("no-ending-newline") << (testDataDir + "more-certificates/no-ending-newline.pem") << int(QRegExp::FixedString) << true << 1; + QTest::newRow("malformed-just-begin") << (testDataDir + "more-certificates/malformed-just-begin.pem") << int(QRegExp::FixedString) << true << 0; + QTest::newRow("malformed-just-begin-no-newline") << (testDataDir + "more-certificates/malformed-just-begin-no-newline.pem") << int(QRegExp::FixedString) << true << 0; } void tst_QSslCertificate::fromPath() @@ -724,7 +708,7 @@ void tst_QSslCertificate::certInfo() "dc:c2:eb:b7:bb:50:18:05:ba:ad:af:08:49:fe:98:63" "55:ba:e7:fb:95:5d:91"; - QSslCertificate cert = QSslCertificate::fromPath("certificates/cert.pem", QSsl::Pem, + QSslCertificate cert = QSslCertificate::fromPath(testDataDir + "certificates/cert.pem", QSsl::Pem, QRegExp::FixedString).first(); QVERIFY(!cert.isNull()); @@ -781,7 +765,7 @@ void tst_QSslCertificate::certInfo() void tst_QSslCertificate::certInfoQByteArray() { - QSslCertificate cert = QSslCertificate::fromPath("certificates/cert.pem", QSsl::Pem, + QSslCertificate cert = QSslCertificate::fromPath(testDataDir + "certificates/cert.pem", QSsl::Pem, QRegExp::FixedString).first(); QVERIFY(!cert.isNull()); @@ -898,8 +882,8 @@ void tst_QSslCertificate::largeExpirationDate() // QTBUG-12489 void tst_QSslCertificate::blacklistedCertificates() { - QList blacklistedCerts = QSslCertificate::fromPath("more-certificates/blacklisted*.pem", QSsl::Pem, QRegExp::Wildcard); - QVERIFY2(blacklistedCerts.count() > 0, "Please run this test from the source directory"); + QList blacklistedCerts = QSslCertificate::fromPath(testDataDir + "more-certificates/blacklisted*.pem", QSsl::Pem, QRegExp::Wildcard); + QVERIFY(blacklistedCerts.count() > 0); for (int a = 0; a < blacklistedCerts.count(); a++) { QVERIFY(blacklistedCerts.at(a).isBlacklisted()); } @@ -953,7 +937,7 @@ void tst_QSslCertificate::multipleCommonNames() { QList certList = QSslCertificate::fromPath(testDataDir + "more-certificates/test-cn-two-cns-cert.pem"); - QVERIFY2(certList.count() > 0, "Please run this test from the source directory"); + QVERIFY(certList.count() > 0); QStringList commonNames = certList[0].subjectInfo(QSslCertificate::CommonName); QVERIFY(commonNames.contains(QString("www.example.com"))); @@ -964,14 +948,14 @@ void tst_QSslCertificate::subjectAndIssuerAttributes() { QList certList = QSslCertificate::fromPath(testDataDir + "more-certificates/test-cn-with-drink-cert.pem"); - QVERIFY2(certList.count() > 0, "Please run this test from the source directory"); + QVERIFY(certList.count() > 0); QList attributes = certList[0].subjectInfoAttributes(); QVERIFY(attributes.contains(QByteArray("favouriteDrink"))); attributes.clear(); certList = QSslCertificate::fromPath(testDataDir + "more-certificates/natwest-banking.pem"); - QVERIFY2(certList.count() > 0, "Please run this test from the source directory"); + QVERIFY(certList.count() > 0); attributes = certList[0].subjectInfoAttributes(); QVERIFY(attributes.contains(QByteArray("1.3.6.1.4.1.311.60.2.1.3"))); @@ -1071,7 +1055,7 @@ void tst_QSslCertificate::extensions() { QList certList = QSslCertificate::fromPath(testDataDir + "more-certificates/natwest-banking.pem"); - QVERIFY2(certList.count() > 0, "Please run this test from the source directory"); + QVERIFY(certList.count() > 0); QSslCertificate cert = certList[0]; QList extensions = cert.extensions(); @@ -1169,7 +1153,7 @@ void tst_QSslCertificate::extensionsCritical() { QList certList = QSslCertificate::fromPath(testDataDir + "verify-certs/test-addons-mozilla-org-cert.pem"); - QVERIFY2(certList.count() > 0, "Please run this test from the source directory"); + QVERIFY(certList.count() > 0); QSslCertificate cert = certList[0]; QList extensions = cert.extensions(); diff --git a/tests/auto/network/ssl/qsslkey/qsslkey.pro b/tests/auto/network/ssl/qsslkey/qsslkey.pro index 8c3877631a..0074513878 100644 --- a/tests/auto/network/ssl/qsslkey/qsslkey.pro +++ b/tests/auto/network/ssl/qsslkey/qsslkey.pro @@ -9,4 +9,5 @@ qtConfig(private_tests) { TARGET = tst_qsslkey -TESTDATA += keys/* rsa-without-passphrase.pem rsa-with-passphrase.pem +TESTDATA += keys/* \ + rsa-*.pem diff --git a/tests/auto/network/ssl/qsslkey/tst_qsslkey.cpp b/tests/auto/network/ssl/qsslkey/tst_qsslkey.cpp index 7361e30e36..27d92db3bf 100644 --- a/tests/auto/network/ssl/qsslkey/tst_qsslkey.cpp +++ b/tests/auto/network/ssl/qsslkey/tst_qsslkey.cpp @@ -394,9 +394,9 @@ void tst_QSslKey::passphraseChecks_data() { QTest::addColumn("fileName"); - QTest::newRow("DES") << QString(testDataDir + "rsa-with-passphrase-des.pem"); - QTest::newRow("3DES") << QString(testDataDir + "rsa-with-passphrase-3des.pem"); - QTest::newRow("RC2") << QString(testDataDir + "rsa-with-passphrase-rc2.pem"); + QTest::newRow("DES") << (testDataDir + "rsa-with-passphrase-des.pem"); + QTest::newRow("3DES") << (testDataDir + "rsa-with-passphrase-3des.pem"); + QTest::newRow("RC2") << (testDataDir + "rsa-with-passphrase-rc2.pem"); } void tst_QSslKey::passphraseChecks() diff --git a/tests/auto/network/ssl/qsslsocket/qsslsocket.pro b/tests/auto/network/ssl/qsslsocket/qsslsocket.pro index 4ccf0f24b0..f45857b02d 100644 --- a/tests/auto/network/ssl/qsslsocket/qsslsocket.pro +++ b/tests/auto/network/ssl/qsslsocket/qsslsocket.pro @@ -14,6 +14,8 @@ win32 { } } +TESTDATA += certs + DEFINES += SRCDIR=\\\"$$PWD/\\\" requires(qtConfig(private_tests)) diff --git a/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp b/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp index e547521a74..01a7465564 100644 --- a/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp +++ b/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp @@ -278,7 +278,10 @@ private: #endif // QT_NO_SSL private: static int loopLevel; +public: + static QString testDataDir; }; +QString tst_QSslSocket::testDataDir; #ifndef QT_NO_SSL #ifndef QT_NO_OPENSSL @@ -332,6 +335,11 @@ void tst_QSslSocket::initTestCase_data() void tst_QSslSocket::initTestCase() { + testDataDir = QFileInfo(QFINDTESTDATA("certs")).absolutePath(); + if (testDataDir.isEmpty()) + testDataDir = QCoreApplication::applicationDirPath(); + if (!testDataDir.endsWith(QLatin1String("/"))) + testDataDir += QLatin1String("/"); #ifndef QT_NO_SSL qDebug("Using SSL library %s (%ld)", qPrintable(QSslSocket::sslLibraryVersionString()), @@ -705,7 +713,7 @@ void tst_QSslSocket::connectToHostEncrypted() QSslSocketPtr socket = newSocket(); this->socket = socket.data(); - QVERIFY(socket->addCaCertificates(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem"))); + QVERIFY(socket->addCaCertificates(testDataDir + "certs/qt-test-server-cacert.pem")); #ifdef QSSLSOCKET_CERTUNTRUSTED_WORKAROUND connect(socket.data(), SIGNAL(sslErrors(QList)), this, SLOT(untrustedWorkaroundSlot(QList))); @@ -739,7 +747,7 @@ void tst_QSslSocket::connectToHostEncryptedWithVerificationPeerName() QSslSocketPtr socket = newSocket(); this->socket = socket.data(); - socket->addCaCertificates(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem")); + socket->addCaCertificates(testDataDir + "certs/qt-test-server-cacert.pem"); #ifdef QSSLSOCKET_CERTUNTRUSTED_WORKAROUND connect(socket.data(), SIGNAL(sslErrors(QList)), this, SLOT(untrustedWorkaroundSlot(QList))); @@ -803,10 +811,10 @@ void tst_QSslSocket::localCertificate() // values. This test should just run the codepath inside qsslsocket_openssl.cpp QSslSocketPtr socket = newSocket(); - QList localCert = QSslCertificate::fromPath(SRCDIR "certs/qt-test-server-cacert.pem"); + QList localCert = QSslCertificate::fromPath(testDataDir + "certs/qt-test-server-cacert.pem"); socket->setCaCertificates(localCert); - socket->setLocalCertificate(QLatin1String(SRCDIR "certs/fluke.cert")); - socket->setPrivateKey(QLatin1String(SRCDIR "certs/fluke.key")); + socket->setLocalCertificate(testDataDir + "certs/fluke.cert"); + socket->setPrivateKey(testDataDir + "certs/fluke.key"); socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 443); QFETCH_GLOBAL(bool, setProxy); @@ -832,7 +840,7 @@ void tst_QSslSocket::peerCertificateChain() QSslSocketPtr socket = newSocket(); this->socket = socket.data(); - QList caCertificates = QSslCertificate::fromPath(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem")); + QList caCertificates = QSslCertificate::fromPath(testDataDir + "certs/qt-test-server-cacert.pem"); QCOMPARE(caCertificates.count(), 1); socket->addCaCertificates(caCertificates); #ifdef QSSLSOCKET_CERTUNTRUSTED_WORKAROUND @@ -895,7 +903,7 @@ void tst_QSslSocket::privateKeyOpaque() if (!QSslSocket::supportsSsl()) return; - QFile file(SRCDIR "certs/fluke.key"); + QFile file(testDataDir + "certs/fluke.key"); QVERIFY(file.open(QIODevice::ReadOnly)); QSslKey key(file.readAll(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey); QVERIFY(!key.isNull()); @@ -908,9 +916,9 @@ void tst_QSslSocket::privateKeyOpaque() // values. This test should just run the codepath inside qsslsocket_openssl.cpp QSslSocketPtr socket = newSocket(); - QList localCert = QSslCertificate::fromPath(SRCDIR "certs/qt-test-server-cacert.pem"); + QList localCert = QSslCertificate::fromPath(testDataDir + "certs/qt-test-server-cacert.pem"); socket->setCaCertificates(localCert); - socket->setLocalCertificate(QLatin1String(SRCDIR "certs/fluke.cert")); + socket->setLocalCertificate(testDataDir + "certs/fluke.cert"); socket->setPrivateKey(QSslKey(reinterpret_cast(pkey))); socket->setPeerVerifyMode(QSslSocket::QueryPeer); @@ -928,7 +936,7 @@ void tst_QSslSocket::protocol() QSslSocketPtr socket = newSocket(); this->socket = socket.data(); - QList certs = QSslCertificate::fromPath(SRCDIR "certs/qt-test-server-cacert.pem"); + QList certs = QSslCertificate::fromPath(testDataDir + "certs/qt-test-server-cacert.pem"); socket->setCaCertificates(certs); #ifdef QSSLSOCKET_CERTUNTRUSTED_WORKAROUND @@ -1075,8 +1083,8 @@ class SslServer : public QTcpServer { Q_OBJECT public: - SslServer(const QString &keyFile = SRCDIR "certs/fluke.key", - const QString &certFile = SRCDIR "certs/fluke.cert", + SslServer(const QString &keyFile = tst_QSslSocket::testDataDir + "certs/fluke.key", + const QString &certFile = tst_QSslSocket::testDataDir + "certs/fluke.cert", const QString &interFile = QString()) : socket(0), config(QSslConfiguration::defaultConfiguration()), @@ -1428,7 +1436,7 @@ void tst_QSslSocket::setCaCertificates() QSslSocket socket; QCOMPARE(socket.caCertificates(), QSslSocket::defaultCaCertificates()); - socket.setCaCertificates(QSslCertificate::fromPath(SRCDIR "certs/qt-test-server-cacert.pem")); + socket.setCaCertificates(QSslCertificate::fromPath(testDataDir + "certs/qt-test-server-cacert.pem")); QCOMPARE(socket.caCertificates().size(), 1); socket.setCaCertificates(socket.defaultCaCertificates()); QCOMPARE(socket.caCertificates(), QSslSocket::defaultCaCertificates()); @@ -1444,7 +1452,7 @@ void tst_QSslSocket::localCertificateChain() return; QSslSocket socket; - socket.setLocalCertificate(QLatin1String(SRCDIR "certs/fluke.cert")); + socket.setLocalCertificate(testDataDir + "certs/fluke.cert"); QSslConfiguration conf = socket.sslConfiguration(); QList chain = conf.localCertificateChain(); @@ -1465,9 +1473,9 @@ void tst_QSslSocket::setLocalCertificateChain() if (setProxy) return; - SslServer server(QLatin1String(SRCDIR "certs/leaf.key"), - QLatin1String(SRCDIR "certs/leaf.crt"), - QLatin1String(SRCDIR "certs/inter.crt")); + SslServer server(testDataDir + "certs/leaf.key", + testDataDir + "certs/leaf.crt", + testDataDir + "certs/inter.crt"); QVERIFY(server.listen()); @@ -1536,7 +1544,7 @@ void tst_QSslSocket::setSslConfiguration_data() QTest::newRow("empty") << QSslConfiguration() << false; QSslConfiguration conf = QSslConfiguration::defaultConfiguration(); QTest::newRow("default") << conf << false; // does not contain test server cert - QList testServerCert = QSslCertificate::fromPath(SRCDIR "certs/qt-test-server-cacert.pem"); + QList testServerCert = QSslCertificate::fromPath(testDataDir + "certs/qt-test-server-cacert.pem"); conf.setCaCertificates(testServerCert); QTest::newRow("set-root-cert") << conf << true; conf.setProtocol(QSsl::SecureProtocols); @@ -1639,7 +1647,7 @@ void tst_QSslSocket::addDefaultCaCertificate() // Reset the global CA chain QSslSocket::setDefaultCaCertificates(QSslSocket::systemCaCertificates()); - QList flukeCerts = QSslCertificate::fromPath(SRCDIR "certs/qt-test-server-cacert.pem"); + QList flukeCerts = QSslCertificate::fromPath(testDataDir + "certs/qt-test-server-cacert.pem"); QCOMPARE(flukeCerts.size(), 1); QList globalCerts = QSslSocket::defaultCaCertificates(); QVERIFY(!globalCerts.contains(flukeCerts.first())); @@ -1751,7 +1759,7 @@ void tst_QSslSocket::isMatchingHostname() { // with normalization: (the certificate has *.SCHÄUFELE.DE as a CN) // openssl req -x509 -nodes -subj "/CN=*.SCHÄUFELE.DE" -newkey rsa:512 -keyout /dev/null -out xn--schufele-2za.crt - QList certs = QSslCertificate::fromPath(SRCDIR "certs/xn--schufele-2za.crt"); + QList certs = QSslCertificate::fromPath(testDataDir + "certs/xn--schufele-2za.crt"); QVERIFY(!certs.isEmpty()); QSslCertificate cert = certs.first(); @@ -1807,7 +1815,7 @@ protected: socket->ignoreSslErrors(); // Only set the certificate - QList localCert = QSslCertificate::fromPath(SRCDIR "certs/fluke.cert"); + QList localCert = QSslCertificate::fromPath(tst_QSslSocket::testDataDir + "certs/fluke.cert"); QVERIFY(!localCert.isEmpty()); QVERIFY(!localCert.first().isNull()); socket->setLocalCertificate(localCert.first()); @@ -2045,13 +2053,14 @@ protected: socket = new QSslSocket(this); connect(socket, SIGNAL(sslErrors(QList)), this, SLOT(ignoreErrorSlot())); - QFile file(SRCDIR "certs/fluke.key"); + QFile file(tst_QSslSocket::testDataDir + "certs/fluke.key"); QVERIFY(file.open(QIODevice::ReadOnly)); QSslKey key(file.readAll(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey); QVERIFY(!key.isNull()); socket->setPrivateKey(key); - QList localCert = QSslCertificate::fromPath(SRCDIR "certs/fluke.cert"); + QList localCert = QSslCertificate::fromPath(tst_QSslSocket::testDataDir + + "certs/fluke.cert"); QVERIFY(!localCert.isEmpty()); QVERIFY(!localCert.first().isNull()); socket->setLocalCertificate(localCert.first()); @@ -2227,8 +2236,8 @@ protected: { socket = new QSslSocket(this); - socket->setPrivateKey(SRCDIR "certs/fluke.key"); - socket->setLocalCertificate(SRCDIR "certs/fluke.cert"); + socket->setPrivateKey(tst_QSslSocket::testDataDir + "certs/fluke.key"); + socket->setLocalCertificate(tst_QSslSocket::testDataDir + "certs/fluke.cert"); socket->setSocketDescriptor(socketDescriptor); socket->startServerEncryption(); } @@ -2346,7 +2355,7 @@ void tst_QSslSocket::resetProxy() // make sure the connection works, and then set a nonsense proxy, and then // make sure it does not work anymore QSslSocket socket; - socket.addCaCertificates(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem")); + socket.addCaCertificates(testDataDir + "certs/qt-test-server-cacert.pem"); socket.setProxy(goodProxy); socket.connectToHostEncrypted(QtNetworkSettings::serverName(), 443); QVERIFY2(socket.waitForConnected(10000), qPrintable(socket.errorString())); @@ -2365,7 +2374,7 @@ void tst_QSslSocket::resetProxy() // set the nonsense proxy and make sure the connection does not work, // and then set the right proxy and make sure it works QSslSocket socket2; - socket2.addCaCertificates(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem")); + socket2.addCaCertificates(testDataDir + "certs/qt-test-server-cacert.pem"); socket2.setProxy(badProxy); socket2.connectToHostEncrypted(QtNetworkSettings::serverName(), 443); QVERIFY(! socket2.waitForConnected(10000)); @@ -2384,7 +2393,7 @@ void tst_QSslSocket::ignoreSslErrorsList_data() // construct the list of errors that we will get with the SSL handshake and that we will ignore QList expectedSslErrors; // fromPath gives us a list of certs, but it actually only contains one - QList certs = QSslCertificate::fromPath(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem")); + QList certs = QSslCertificate::fromPath(testDataDir + "certs/qt-test-server-cacert.pem"); QSslError rightError(FLUKE_CERTIFICATE_ERROR, certs.at(0)); QSslError wrongError(FLUKE_CERTIFICATE_ERROR); @@ -2563,7 +2572,7 @@ void tst_QSslSocket::blacklistedCertificates() if (setProxy) return; - SslServer server(SRCDIR "certs/fake-login.live.com.key", SRCDIR "certs/fake-login.live.com.pem"); + SslServer server(testDataDir + "certs/fake-login.live.com.key", testDataDir + "certs/fake-login.live.com.pem"); QSslSocket *receiver = new QSslSocket(this); connect(receiver, SIGNAL(readyRead()), SLOT(exitLoop())); @@ -2675,7 +2684,7 @@ void tst_QSslSocket::resume_data() QTest::newRow("DoNotIgnoreErrors") << false << QList() << false; QTest::newRow("ignoreAllErrors") << true << QList() << true; - QList certs = QSslCertificate::fromPath(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem")); + QList certs = QSslCertificate::fromPath(testDataDir + "certs/qt-test-server-cacert.pem"); QSslError rightError(FLUKE_CERTIFICATE_ERROR, certs.at(0)); QSslError wrongError(FLUKE_CERTIFICATE_ERROR); errorsList.append(wrongError); @@ -2745,8 +2754,8 @@ class WebSocket : public QSslSocket Q_OBJECT public: explicit WebSocket(qintptr socketDescriptor, - const QString &keyFile = SRCDIR "certs/fluke.key", - const QString &certFile = SRCDIR "certs/fluke.cert"); + const QString &keyFile = tst_QSslSocket::testDataDir + "certs/fluke.key", + const QString &certFile = tst_QSslSocket::testDataDir + "certs/fluke.cert"); protected slots: void onReadyReadFirstBytes(void); @@ -2957,13 +2966,13 @@ void tst_QSslSocket::qtbug18498_peek2() QCOMPARE(a[2], 'S'); QCOMPARE(server->readAll(), QByteArray("TLS\r\n")); - QFile file(SRCDIR "certs/fluke.key"); + QFile file(testDataDir + "certs/fluke.key"); QVERIFY(file.open(QIODevice::ReadOnly)); QSslKey key(file.readAll(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey); QVERIFY(!key.isNull()); server->setPrivateKey(key); - QList localCert = QSslCertificate::fromPath(SRCDIR "certs/fluke.cert"); + QList localCert = QSslCertificate::fromPath(testDataDir + "certs/fluke.cert"); QVERIFY(!localCert.isEmpty()); QVERIFY(!localCert.first().isNull()); server->setLocalCertificate(localCert.first()); @@ -3163,10 +3172,10 @@ void tst_QSslSocket::verifyClientCertificate_data() QTest::newRow("NoCert:VerifyPeer") << QSslSocket::VerifyPeer << noCerts << noKey << false; // self-signed certificate - QList flukeCerts = QSslCertificate::fromPath(SRCDIR "certs/fluke.cert"); + QList flukeCerts = QSslCertificate::fromPath(testDataDir + "certs/fluke.cert"); QCOMPARE(flukeCerts.size(), 1); - QFile flukeFile(SRCDIR "certs/fluke.key"); + QFile flukeFile(testDataDir + "certs/fluke.key"); QVERIFY(flukeFile.open(QIODevice::ReadOnly)); QSslKey flukeKey(flukeFile.readAll(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey); QVERIFY(!flukeKey.isNull()); @@ -3177,10 +3186,10 @@ void tst_QSslSocket::verifyClientCertificate_data() QTest::newRow("SelfSignedCert:VerifyPeer") << QSslSocket::VerifyPeer << flukeCerts << flukeKey << false; // valid certificate, but wrong usage (server certificate) - QList serverCerts = QSslCertificate::fromPath(SRCDIR "certs/bogus-server.crt"); + QList serverCerts = QSslCertificate::fromPath(testDataDir + "certs/bogus-server.crt"); QCOMPARE(serverCerts.size(), 1); - QFile serverFile(SRCDIR "certs/bogus-server.key"); + QFile serverFile(testDataDir + "certs/bogus-server.key"); QVERIFY(serverFile.open(QIODevice::ReadOnly)); QSslKey serverKey(serverFile.readAll(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey); QVERIFY(!serverKey.isNull()); @@ -3191,10 +3200,10 @@ void tst_QSslSocket::verifyClientCertificate_data() QTest::newRow("ValidServerCert:VerifyPeer") << QSslSocket::VerifyPeer << serverCerts << serverKey << false; // valid certificate, correct usage (client certificate) - QList validCerts = QSslCertificate::fromPath(SRCDIR "certs/bogus-client.crt"); + QList validCerts = QSslCertificate::fromPath(testDataDir + "certs/bogus-client.crt"); QCOMPARE(validCerts.size(), 1); - QFile validFile(SRCDIR "certs/bogus-client.key"); + QFile validFile(testDataDir + "certs/bogus-client.key"); QVERIFY(validFile.open(QIODevice::ReadOnly)); QSslKey validKey(validFile.readAll(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey); QVERIFY(!validKey.isNull()); @@ -3205,7 +3214,7 @@ void tst_QSslSocket::verifyClientCertificate_data() QTest::newRow("ValidClientCert:VerifyPeer") << QSslSocket::VerifyPeer << validCerts << validKey << true; // valid certificate, correct usage (client certificate), with chain - validCerts += QSslCertificate::fromPath(SRCDIR "certs/bogus-ca.crt"); + validCerts += QSslCertificate::fromPath(testDataDir + "certs/bogus-ca.crt"); QCOMPARE(validCerts.size(), 2); QTest::newRow("ValidClientCert:AutoVerifyPeer") << QSslSocket::AutoVerifyPeer << validCerts << validKey << true; @@ -3238,7 +3247,7 @@ void tst_QSslSocket::verifyClientCertificate() QFETCH(QSslSocket::PeerVerifyMode, peerVerifyMode); SslServer server; - server.addCaCertificates = QLatin1String(SRCDIR "certs/bogus-ca.crt"); + server.addCaCertificates = testDataDir + "certs/bogus-ca.crt"; server.ignoreSslErrors = false; server.peerVerifyMode = peerVerifyMode; QVERIFY(server.listen());