TestIconDownloader.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "TestIconDownloader.h"
  2. #include <QTest>
  3. #include "core/Config.h"
  4. #include "gui/IconDownloader.h"
  5. QTEST_GUILESS_MAIN(TestIconDownloader)
  6. void TestIconDownloader::testIconDownloader()
  7. {
  8. QFETCH(QString, url);
  9. QFETCH(QStringList, expectation);
  10. config()->set(Config::Security_IconDownloadFallback, false);
  11. IconDownloader downloader;
  12. downloader.setUrl(url);
  13. QStringList resolved_urls;
  14. for (const auto& resolved_url : downloader.m_urlsToTry) {
  15. resolved_urls.push_back(resolved_url.toString());
  16. }
  17. QCOMPARE(resolved_urls, expectation);
  18. }
  19. void TestIconDownloader::testIconDownloader_data()
  20. {
  21. QTest::addColumn<QString>("url");
  22. QTest::addColumn<QStringList>("expectation");
  23. const QString keepassxc_favicon("https://keepassxc.org/favicon.ico");
  24. QTest::newRow("Invalid URL") << "http:sk/s.com" << QStringList{};
  25. QTest::newRow("Unsupported schema") << "ftp://google.com" << QStringList{};
  26. QTest::newRow("Missing schema") << "keepassxc.org" << QStringList{"https://keepassxc.org/favicon.ico"};
  27. QTest::newRow("Missing host") << "https:///register" << QStringList{};
  28. QTest::newRow("URL with path") << "https://keepassxc.org/register/here/"
  29. << QStringList{"https://keepassxc.org/register/here/favicon.ico", keepassxc_favicon};
  30. QTest::newRow("URL with path and query")
  31. << "https://keepassxc.org/register/here?login=me"
  32. << QStringList{"https://keepassxc.org/register/favicon.ico", keepassxc_favicon};
  33. QTest::newRow("URL with port") << "https://keepassxc.org:8080"
  34. << QStringList{"https://keepassxc.org:8080/favicon.ico"};
  35. QTest::newRow("2nd level domain") << "https://login.keepassxc.org"
  36. << QStringList{"https://login.keepassxc.org/favicon.ico", keepassxc_favicon};
  37. QTest::newRow("2nd level domain with additional fields")
  38. << "https://user:password@login.keepassxc.org:2021?test#1"
  39. << QStringList{"https://user:password@login.keepassxc.org:2021/favicon.ico",
  40. "https://user:password@keepassxc.org:2021/favicon.ico",
  41. keepassxc_favicon};
  42. QTest::newRow("2nd level domain (.co.uk special case), with subdomain")
  43. << "https://login.keepassxc.co.uk"
  44. << QStringList{"https://login.keepassxc.co.uk/favicon.ico", "https://keepassxc.co.uk/favicon.ico"};
  45. QTest::newRow("2nd level domain .co.uk special case")
  46. << "https://keepassxc.co.uk" << QStringList{"https://keepassxc.co.uk/favicon.ico"};
  47. QTest::newRow("2nd level domain with several subdomains")
  48. << "https://de.login.keepassxc.org"
  49. << QStringList{"https://de.login.keepassxc.org/favicon.ico", keepassxc_favicon};
  50. QTest::newRow("Raw IP with schema") << "https://134.130.155.184"
  51. << QStringList{"https://134.130.155.184/favicon.ico"};
  52. QTest::newRow("Raw IP") << "134.130.155.184" << QStringList{"https://134.130.155.184/favicon.ico"};
  53. QTest::newRow("Raw IP with schema and path")
  54. << "https://134.130.155.184/with/path/"
  55. << QStringList{"https://134.130.155.184/with/path/favicon.ico", "https://134.130.155.184/favicon.ico"};
  56. QTest::newRow("Raw IP with schema (https), path, and port")
  57. << "https://134.130.155.184:8080/test/"
  58. << QStringList{"https://134.130.155.184:8080/test/favicon.ico", "https://134.130.155.184:8080/favicon.ico"};
  59. QTest::newRow("Raw IP with schema (http), path, and port")
  60. << "134.130.155.184:8080/test/"
  61. << QStringList{"https://134.130.155.184:8080/test/favicon.ico", "https://134.130.155.184:8080/favicon.ico"};
  62. QTest::newRow("URL with username and password")
  63. << "https://user:password@keepassxc.org" << QStringList{"https://user:password@keepassxc.org/favicon.ico"};
  64. QTest::newRow("URL with username and password, several subdomains")
  65. << "https://user:password@de.login.keepassxc.org"
  66. << QStringList{"https://user:password@de.login.keepassxc.org/favicon.ico",
  67. "https://user:password@keepassxc.org/favicon.ico",
  68. "https://keepassxc.org/favicon.ico"};
  69. QTest::newRow("Raw IP with username and password")
  70. << "https://user:password@134.130.155.184" << QStringList{"https://user:password@134.130.155.184/favicon.ico"};
  71. QTest::newRow("Relative path should be preserved")
  72. << "https://test.com/rel-path/"
  73. << QStringList{"https://test.com/rel-path/favicon.ico", "https://test.com/favicon.ico"};
  74. }