Files
keepassxc/tests/TestIconDownloader.cpp
Jessy2027 cd42236540 Fix UI freeze when downloading favicons with slow DNS
IconDownloader::setUrl resolved the entry's host name with the blocking QHostInfo::fromName on the GUI thread, only to find out whether the host is an IP address. When the DNS servers are slow or unreachable, the whole window stopped responding until the lookup timed out.

Check the host with UrlTools::isIpAddress instead, which parses the literal address without any network access. Also add IPv6 literal cases to the icon downloader tests.

Fixes #13989
2026-11-17 08:46:04 -05:00

110 lines
5.5 KiB
C++

/*
* Copyright (C) 2026 KeePassXC Team <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestIconDownloader.h"
#include <QTest>
#include "core/Config.h"
#include "gui/IconDownloader.h"
QTEST_GUILESS_MAIN(TestIconDownloader)
void TestIconDownloader::initTestCase()
{
QLocale::setDefault(QLocale::c());
}
void TestIconDownloader::testIconDownloader()
{
QFETCH(QString, url);
QFETCH(QStringList, expectation);
config()->set(Config::Security_IconDownloadFallback, false);
IconDownloader downloader;
downloader.setUrl(url);
QStringList resolved_urls;
for (const auto& resolved_url : downloader.m_urlsToTry) {
resolved_urls.push_back(resolved_url.toString());
}
QCOMPARE(resolved_urls, expectation);
}
void TestIconDownloader::testIconDownloader_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<QStringList>("expectation");
const QString keepassxc_favicon("https://keepassxc.org/favicon.ico");
QTest::newRow("Invalid URL") << "http:sk/s.com" << QStringList{};
QTest::newRow("Unsupported schema") << "ftp://google.com" << QStringList{};
QTest::newRow("Missing schema") << "keepassxc.org" << QStringList{"https://keepassxc.org/favicon.ico"};
QTest::newRow("Missing host") << "https:///register" << QStringList{};
QTest::newRow("URL with path") << "https://keepassxc.org/register/here/"
<< QStringList{"https://keepassxc.org/register/here/favicon.ico", keepassxc_favicon};
QTest::newRow("URL with path and query")
<< "https://keepassxc.org/register/here?login=me"
<< QStringList{"https://keepassxc.org/register/favicon.ico", keepassxc_favicon};
QTest::newRow("URL with port") << "https://keepassxc.org:8080"
<< QStringList{"https://keepassxc.org:8080/favicon.ico"};
QTest::newRow("2nd level domain") << "https://login.keepassxc.org"
<< QStringList{"https://login.keepassxc.org/favicon.ico", keepassxc_favicon};
QTest::newRow("2nd level domain with additional fields")
<< "https://user:[email protected]:2021?test#1"
<< QStringList{"https://user:[email protected]:2021/favicon.ico",
"https://user:[email protected]:2021/favicon.ico",
keepassxc_favicon};
QTest::newRow("2nd level domain (.co.uk special case), with subdomain")
<< "https://login.keepassxc.co.uk"
<< QStringList{"https://login.keepassxc.co.uk/favicon.ico", "https://keepassxc.co.uk/favicon.ico"};
QTest::newRow("2nd level domain .co.uk special case")
<< "https://keepassxc.co.uk" << QStringList{"https://keepassxc.co.uk/favicon.ico"};
QTest::newRow("2nd level domain with several subdomains")
<< "https://de.login.keepassxc.org"
<< QStringList{"https://de.login.keepassxc.org/favicon.ico", keepassxc_favicon};
QTest::newRow("Raw IP with schema") << "https://134.130.155.184"
<< QStringList{"https://134.130.155.184/favicon.ico"};
QTest::newRow("Raw IP") << "134.130.155.184" << QStringList{"https://134.130.155.184/favicon.ico"};
QTest::newRow("Raw IP with schema and path")
<< "https://134.130.155.184/with/path/"
<< QStringList{"https://134.130.155.184/with/path/favicon.ico", "https://134.130.155.184/favicon.ico"};
QTest::newRow("Raw IP with schema (https), path, and port")
<< "https://134.130.155.184:8080/test/"
<< QStringList{"https://134.130.155.184:8080/test/favicon.ico", "https://134.130.155.184:8080/favicon.ico"};
QTest::newRow("Raw IP with schema (http), path, and port")
<< "134.130.155.184:8080/test/"
<< QStringList{"https://134.130.155.184:8080/test/favicon.ico", "https://134.130.155.184:8080/favicon.ico"};
QTest::newRow("URL with username and password")
<< "https://user:[email protected]" << QStringList{"https://user:[email protected]/favicon.ico"};
QTest::newRow("URL with username and password, several subdomains")
<< "https://user:[email protected]"
<< QStringList{"https://user:[email protected]/favicon.ico",
"https://user:[email protected]/favicon.ico",
"https://keepassxc.org/favicon.ico"};
QTest::newRow("Raw IPv6 with schema")
<< "https://[2001:db8::1]" << QStringList{"https://[2001:db8::1]/favicon.ico"};
QTest::newRow("Raw IPv6 with schema and port")
<< "https://[2001:db8::1]:8080" << QStringList{"https://[2001:db8::1]:8080/favicon.ico"};
QTest::newRow("Raw IP with username and password")
<< "https://user:[email protected]" << QStringList{"https://user:[email protected]/favicon.ico"};
QTest::newRow("Relative path should be preserved")
<< "https://test.com/rel-path/"
<< QStringList{"https://test.com/rel-path/favicon.ico", "https://test.com/favicon.ico"};
}