Rewrote resolveUrl function to conform to various test cases

This commit is contained in:
Jonathan White 2017-10-03 13:11:00 -04:00 committed by Jonathan White
parent 91d746c5c0
commit 5098866413
3 changed files with 47 additions and 15 deletions

View File

@ -801,28 +801,31 @@ QString Entry::resolvePlaceholder(const QString& str) const
QString Entry::resolveUrl(const QString& url) const QString Entry::resolveUrl(const QString& url) const
{ {
#ifdef WITH_XC_HTTP
QString newUrl = url; QString newUrl = url;
if (!url.contains("://")) { if (!url.isEmpty() && !url.contains("://")) {
// URL doesn't have a protocol, add https by default // URL doesn't have a protocol, add https by default
newUrl.prepend("https://"); newUrl.prepend("https://");
} }
QUrl tempUrl = QUrl(newUrl);
if (tempUrl.isValid()) { if (newUrl.startsWith("cmd://")) {
if (tempUrl.scheme() == "cmd") { QStringList cmdList = newUrl.split(" ");
// URL is a cmd, hopefully the second argument is an URL for (int i=1; i < cmdList.size(); ++i) {
QStringList cmd = newUrl.split(" "); // Don't pass arguments to the resolveUrl function (they look like URL's)
if (cmd.size() > 1) { if (!cmdList[i].startsWith("-") && !cmdList[i].startsWith("/")) {
return resolveUrl(cmd[1].remove("'").remove("\"")); return resolveUrl(cmdList[i].remove(QRegExp("'|\"")));
} }
} else if (tempUrl.scheme() == "http" || tempUrl.scheme() == "https") {
// URL is nice
return tempUrl.url();
} }
// No URL in this command
return QString("");
} }
#else
Q_UNUSED(url); // Validate the URL
#endif QUrl tempUrl = QUrl(newUrl);
if (tempUrl.isValid() && (tempUrl.scheme() == "http" || tempUrl.scheme() == "https")) {
return tempUrl.url();
}
// No valid http URL's found
return QString(""); return QString("");
} }

View File

@ -16,6 +16,7 @@
*/ */
#include "TestEntry.h" #include "TestEntry.h"
#include "config-keepassx-tests.h"
#include <QTest> #include <QTest>
@ -130,3 +131,30 @@ void TestEntry::testClone()
delete entryOrg; delete entryOrg;
} }
void TestEntry::testResolveUrl()
{
Entry* entry = new Entry();
QString testUrl("www.google.com");
QString testCmd("cmd://firefox " + testUrl);
QString testComplexCmd("cmd://firefox --start-now --url 'http://" + testUrl + "' --quit");
QString nonHttpUrl("ftp://google.com");
QString noUrl("random text inserted here");
// Test standard URL's
QCOMPARE(entry->resolveUrl(""), QString(""));
QCOMPARE(entry->resolveUrl(testUrl), "https://" + testUrl);
QCOMPARE(entry->resolveUrl("http://" + testUrl), "http://" + testUrl);
// Test cmd:// with no URL
QCOMPARE(entry->resolveUrl("cmd://firefox"), QString(""));
QCOMPARE(entry->resolveUrl("cmd://firefox --no-url"), QString(""));
// Test cmd:// with URL's
QCOMPARE(entry->resolveUrl(testCmd), "https://" + testUrl);
QCOMPARE(entry->resolveUrl(testComplexCmd), "http://" + testUrl);
// Test non-http URL
QCOMPARE(entry->resolveUrl(nonHttpUrl), QString(""));
// Test no URL
QCOMPARE(entry->resolveUrl(noUrl), QString(""));
delete entry;
}

View File

@ -31,6 +31,7 @@ private slots:
void testHistoryItemDeletion(); void testHistoryItemDeletion();
void testCopyDataFrom(); void testCopyDataFrom();
void testClone(); void testClone();
void testResolveUrl();
}; };
#endif // KEEPASSX_TESTENTRY_H #endif // KEEPASSX_TESTENTRY_H