diff options
Diffstat (limited to 'tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp')
-rw-r--r-- | tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp | 133 |
1 files changed, 26 insertions, 107 deletions
diff --git a/tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp b/tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp index 6cd730bd38..0611deb184 100644 --- a/tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp +++ b/tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp @@ -84,8 +84,6 @@ private slots: void toPemOrDer_data(); void toPemOrDer(); void fromDevice(); - void fromPath_data(); - void fromPath(); void fromPath_qregularexpression_data(); void fromPath_qregularexpression(); void certInfo(); @@ -128,24 +126,25 @@ void tst_QSslCertificate::initTestCase() QDir dir(testDataDir + "certificates"); QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::Readable); - QRegExp rxCert(QLatin1String("^.+\\.(pem|der)$")); - QRegExp rxSan(QLatin1String("^(.+\\.(?:pem|der))\\.san$")); - QRegExp rxPubKey(QLatin1String("^(.+\\.(?:pem|der))\\.pubkey$")); - QRegExp rxDigest(QLatin1String("^(.+\\.(?:pem|der))\\.digest-(md5|sha1)$")); + QRegularExpression rxCert(QLatin1String("^.+\\.(pem|der)$")); + QRegularExpression rxSan(QLatin1String("^(.+\\.(?:pem|der))\\.san$")); + QRegularExpression rxPubKey(QLatin1String("^(.+\\.(?:pem|der))\\.pubkey$")); + QRegularExpression rxDigest(QLatin1String("^(.+\\.(?:pem|der))\\.digest-(md5|sha1)$")); + QRegularExpressionMatch match; foreach (QFileInfo fileInfo, fileInfoList) { - if (rxCert.indexIn(fileInfo.fileName()) >= 0) + if ((match = rxCert.match(fileInfo.fileName())).hasMatch()) certInfoList << CertInfo(fileInfo, - rxCert.cap(1) == QLatin1String("pem") ? QSsl::Pem : QSsl::Der); - if (rxSan.indexIn(fileInfo.fileName()) >= 0) - subjAltNameMap.insert(rxSan.cap(1), fileInfo.absoluteFilePath()); - if (rxPubKey.indexIn(fileInfo.fileName()) >= 0) - pubkeyMap.insert(rxPubKey.cap(1), fileInfo.absoluteFilePath()); - if (rxDigest.indexIn(fileInfo.fileName()) >= 0) { - if (rxDigest.cap(2) == QLatin1String("md5")) - md5Map.insert(rxDigest.cap(1), fileInfo.absoluteFilePath()); + match.captured(1) == QLatin1String("pem") ? QSsl::Pem : QSsl::Der); + if ((match = rxSan.match(fileInfo.fileName())).hasMatch()) + subjAltNameMap.insert(match.captured(1), fileInfo.absoluteFilePath()); + if ((match = rxPubKey.match(fileInfo.fileName())).hasMatch()) + pubkeyMap.insert(match.captured(1), fileInfo.absoluteFilePath()); + if ((match = rxDigest.match(fileInfo.fileName())).hasMatch()) { + if (match.captured(2) == QLatin1String("md5")) + md5Map.insert(match.captured(1), fileInfo.absoluteFilePath()); else - sha1Map.insert(rxDigest.cap(1), fileInfo.absoluteFilePath()); + sha1Map.insert(match.captured(1), fileInfo.absoluteFilePath()); } } } @@ -340,11 +339,12 @@ void tst_QSslCertificate::digest_data() static QByteArray convertDigest(const QByteArray &input) { QByteArray result; - QRegExp rx(QLatin1String("(?:=|:)([0-9A-Fa-f]{2})")); + QRegularExpression rx(QLatin1String("(?:=|:)([0-9A-Fa-f]{2})")); + QRegularExpressionMatch match; int pos = 0; - while ((pos = rx.indexIn(input, pos)) != -1) { - result.append(rx.cap(1).toLatin1()); - pos += rx.matchedLength(); + while ((match = rx.match(input, pos)).hasMatch()) { + result.append(match.captured(1).toLatin1()); + pos = match.capturedEnd(); } return QByteArray::fromHex(result); } @@ -420,16 +420,17 @@ void tst_QSslCertificate::subjectAlternativeNames() } // verify that each entry in fileContents is present in subjAltNames - QRegExp rx(QLatin1String("(email|DNS):([^,\\r\\n]+)")); - for (int pos = 0; (pos = rx.indexIn(fileContents, pos)) != -1; pos += rx.matchedLength()) { + QRegularExpression rx(QLatin1String("(email|DNS):([^,\\r\\n]+)")); + QRegularExpressionMatch match; + for (int pos = 0; (match = rx.match(fileContents, pos)).hasMatch(); pos = match.capturedEnd()) { QSsl::AlternativeNameEntryType key; - if (rx.cap(1) == QLatin1String("email")) + if (match.captured(1) == QLatin1String("email")) key = QSsl::EmailEntry; - else if (rx.cap(1) == QLatin1String("DNS")) + else if (match.captured(1) == QLatin1String("DNS")) key = QSsl::DnsEntry; else QFAIL("unsupported alternative name type"); - QVERIFY(altSubjectNames.contains(key, rx.cap(2))); + QVERIFY(altSubjectNames.contains(key, match.captured(2))); } } @@ -545,88 +546,6 @@ void tst_QSslCertificate::fromDevice() QVERIFY(certs.isEmpty()); } -void tst_QSslCertificate::fromPath_data() -{ - QTest::addColumn<QString>("path"); - QTest::addColumn<int>("syntax"); - QTest::addColumn<bool>("pemencoding"); - QTest::addColumn<int>("numCerts"); - - QTest::newRow("empty fixed pem") << QString() << int(QRegExp::FixedString) << true << 0; - QTest::newRow("empty fixed der") << QString() << int(QRegExp::FixedString) << false << 0; - QTest::newRow("empty regexp pem") << QString() << int(QRegExp::RegExp) << true << 0; - 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") << (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") << (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() -{ - QFETCH(QString, path); - QFETCH(int, syntax); - QFETCH(bool, pemencoding); - QFETCH(int, numCerts); - - QCOMPARE(QSslCertificate::fromPath(path, - pemencoding ? QSsl::Pem : QSsl::Der, - QRegExp::PatternSyntax(syntax)).size(), - numCerts); -} - void tst_QSslCertificate::fromPath_qregularexpression_data() { QTest::addColumn<QString>("path"); |