summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Petersson <Martin.Petersson@nokia.com>2012-06-08 10:39:37 +0200
committerQt by Nokia <qt-info@nokia.com>2012-07-03 01:09:33 +0200
commit6c59cdecee42dad1e100857314d28a3d8279a164 (patch)
tree834b54e0197a443d88ab056bfe4bac85128bb12a
parent41064f851591d9437baeda502b6e2504fee8f213 (diff)
QSslCertificate::fromPath fix wildcard handling
The reqExp used to handle wildcards in the path was broken. So we always searched the working directory and not the specified path. Autotest where passing because of a hack used for Windows paths where we removed the first two chars in the path string. This fix will not use nativeSeparators thus removing the Windows hack and fix the regExp to match wildcard chars. Task-number: QTBUG-23573 Change-Id: I56fadbb67f25b8ce9c0f17cb6232e0bdb9148b1c Reviewed-by: Shane Kearns <shane.kearns@accenture.com>
-rw-r--r--src/network/ssl/qsslcertificate.cpp53
-rw-r--r--tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp4
2 files changed, 34 insertions, 23 deletions
diff --git a/src/network/ssl/qsslcertificate.cpp b/src/network/ssl/qsslcertificate.cpp
index 105cb4b323..1b9563b0f5 100644
--- a/src/network/ssl/qsslcertificate.cpp
+++ b/src/network/ssl/qsslcertificate.cpp
@@ -843,39 +843,46 @@ QList<QSslCertificate> QSslCertificate::fromPath(const QString &path,
QRegExp::PatternSyntax syntax)
{
// $, (,), *, +, ., ?, [, ,], ^, {, | and }.
+
+ // make sure to use the same path separators on Windows and Unix like systems.
+ QString sourcePath = QDir::fromNativeSeparators(path);
+
+ // Find the path without the filename
+ QString pathPrefix = sourcePath.left(sourcePath.lastIndexOf(QLatin1Char('/')));
+
+ // Check if the path contains any special chars
int pos = -1;
if (syntax == QRegExp::Wildcard)
- pos = path.indexOf(QRegExp(QLatin1String("[^\\][\\*\\?\\[\\]]")));
+ pos = pathPrefix.indexOf(QRegExp(QLatin1String("[*?[]")));
else if (syntax != QRegExp::FixedString)
- pos = path.indexOf(QRegExp(QLatin1String("[^\\][\\$\\(\\)\\*\\+\\.\\?\\[\\]\\^\\{\\}\\|]")));
- QString pathPrefix = path.left(pos); // == path if pos < 0
- if (pos != -1)
- pathPrefix = pathPrefix.left(pathPrefix.lastIndexOf(QLatin1Char('/')));
-
- // Special case - if the prefix ends up being nothing, use "." instead and
- // chop off the first two characters from the glob'ed paths.
- int startIndex = 0;
- if (pathPrefix.trimmed().isEmpty()) {
- if(path.startsWith(QLatin1Char('/'))) {
- pathPrefix = path.left(path.indexOf(QRegExp(QLatin1String("[\\*\\?\\[]"))));
- pathPrefix = path.left(path.lastIndexOf(QLatin1Char('/')));
- } else {
- startIndex = 2;
- pathPrefix = QLatin1String(".");
+ pos = sourcePath.indexOf(QRegExp(QLatin1String("[\\$\\(\\)\\*\\+\\.\\?\\[\\]\\^\\{\\}\\|]")));
+ if (pos != -1) {
+ // there was a special char in the path so cut of the part containing that char.
+ pathPrefix = pathPrefix.left(pos);
+ if (pathPrefix.contains(QLatin1Char('/')))
+ pathPrefix = pathPrefix.left(pathPrefix.lastIndexOf(QLatin1Char('/')));
+ else
+ pathPrefix.clear();
+ } else {
+ // Check if the path is a file.
+ if (QFileInfo(sourcePath).isFile()) {
+ QFile file(sourcePath);
+ if (file.open(QIODevice::ReadOnly | QIODevice::Text))
+ return QSslCertificate::fromData(file.readAll(),format);
+ return QList<QSslCertificate>();
}
}
- // The path is a file.
- if (pos == -1 && QFileInfo(pathPrefix).isFile()) {
- QFile file(pathPrefix);
- if (file.open(QIODevice::ReadOnly | QIODevice::Text))
- return QSslCertificate::fromData(file.readAll(),format);
- return QList<QSslCertificate>();
+ // Special case - if the prefix ends up being nothing, use "." instead.
+ int startIndex = 0;
+ if (pathPrefix.isEmpty()) {
+ pathPrefix = QLatin1String(".");
+ startIndex = 2;
}
// The path can be a file or directory.
QList<QSslCertificate> certs;
- QRegExp pattern(path, Qt::CaseSensitive, syntax);
+ QRegExp pattern(sourcePath, Qt::CaseSensitive, syntax);
QDirIterator it(pathPrefix, QDir::Files, QDirIterator::FollowSymlinks | QDirIterator::Subdirectories);
while (it.hasNext()) {
QString filePath = startIndex == 0 ? it.next() : it.next().mid(startIndex);
diff --git a/tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp b/tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp
index b4cee47895..2ec5ec6407 100644
--- a/tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp
+++ b/tests/auto/network/ssl/qsslcertificate/tst_qsslcertificate.cpp
@@ -534,6 +534,10 @@ void tst_QSslCertificate::fromPath_data()
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 << 5;
+ 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 << 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;