TestUrlTools.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2024 KeePassXC Team <team@keepassxc.org>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "TestUrlTools.h"
  18. #include <QTest>
  19. QTEST_GUILESS_MAIN(TestUrlTools)
  20. void TestUrlTools::initTestCase()
  21. {
  22. m_urlTools = urlTools();
  23. }
  24. void TestUrlTools::init()
  25. {
  26. }
  27. void TestUrlTools::testTopLevelDomain()
  28. {
  29. // Create list of URLs and expected TLD responses
  30. QList<QPair<QString, QString>> tldUrls{
  31. {QString("https://another.example.co.uk"), QString("co.uk")},
  32. {QString("https://www.example.com"), QString("com")},
  33. {QString("https://example.com"), QString("com")},
  34. {QString("https://github.com"), QString("com")},
  35. {QString("http://test.net"), QString("net")},
  36. {QString("http://so.many.subdomains.co.jp"), QString("co.jp")},
  37. {QString("https://192.168.0.1"), QString("192.168.0.1")},
  38. {QString("https://192.168.0.1:8000"), QString("192.168.0.1")},
  39. {QString("https://www.nic.ar"), QString("ar")},
  40. {QString("https://no.no.no"), QString("no")},
  41. {QString("https://www.blogspot.com.ar"), QString("blogspot.com.ar")}, // blogspot.com.ar is a TLD
  42. {QString("https://jap.an.ide.kyoto.jp"), QString("ide.kyoto.jp")}, // ide.kyoto.jp is a TLD
  43. {QString("ar"), QString("ar")},
  44. };
  45. for (const auto& u : tldUrls) {
  46. QCOMPARE(urlTools()->getTopLevelDomainFromUrl(u.first), u.second);
  47. }
  48. // Create list of URLs and expected base URL responses
  49. QList<QPair<QString, QString>> baseUrls{
  50. {QString("https://another.example.co.uk"), QString("example.co.uk")},
  51. {QString("https://www.example.com"), QString("example.com")},
  52. {QString("http://test.net"), QString("test.net")},
  53. {QString("http://so.many.subdomains.co.jp"), QString("subdomains.co.jp")},
  54. {QString("https://192.168.0.1"), QString("192.168.0.1")},
  55. {QString("https://192.168.0.1:8000"), QString("192.168.0.1")},
  56. {QString("https://www.nic.ar"), QString("nic.ar")},
  57. {QString("https://www.blogspot.com.ar"), QString("www.blogspot.com.ar")}, // blogspot.com.ar is a TLD
  58. {QString("https://www.arpa"), QString("www.arpa")},
  59. {QString("https://jap.an.ide.kyoto.jp"), QString("an.ide.kyoto.jp")}, // ide.kyoto.jp is a TLD
  60. {QString("https://kobe.jp"), QString("kobe.jp")},
  61. };
  62. for (const auto& u : baseUrls) {
  63. QCOMPARE(urlTools()->getBaseDomainFromUrl(u.first), u.second);
  64. }
  65. }
  66. void TestUrlTools::testIsIpAddress()
  67. {
  68. auto host1 = "example.com"; // Not valid
  69. auto host2 = "192.168.0.1";
  70. auto host3 = "278.21.2.0"; // Not valid
  71. auto host4 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
  72. auto host5 = "2001:db8:0:1:1:1:1:1";
  73. auto host6 = "fe80::1ff:fe23:4567:890a";
  74. auto host7 = "2001:20::1";
  75. auto host8 = "2001:0db8:85y3:0000:0000:8a2e:0370:7334"; // Not valid
  76. auto host9 = "[::]";
  77. auto host10 = "::";
  78. auto host11 = "[2001:20::1]";
  79. QVERIFY(!urlTools()->isIpAddress(host1));
  80. QVERIFY(urlTools()->isIpAddress(host2));
  81. QVERIFY(!urlTools()->isIpAddress(host3));
  82. QVERIFY(urlTools()->isIpAddress(host4));
  83. QVERIFY(urlTools()->isIpAddress(host5));
  84. QVERIFY(urlTools()->isIpAddress(host6));
  85. QVERIFY(urlTools()->isIpAddress(host7));
  86. QVERIFY(!urlTools()->isIpAddress(host8));
  87. QVERIFY(urlTools()->isIpAddress(host9));
  88. QVERIFY(urlTools()->isIpAddress(host10));
  89. QVERIFY(urlTools()->isIpAddress(host11));
  90. }
  91. void TestUrlTools::testIsUrlIdentical()
  92. {
  93. QVERIFY(urlTools()->isUrlIdentical("https://example.com", "https://example.com"));
  94. QVERIFY(urlTools()->isUrlIdentical("https://example.com", " https://example.com "));
  95. QVERIFY(!urlTools()->isUrlIdentical("https://example.com", "https://example2.com"));
  96. QVERIFY(!urlTools()->isUrlIdentical("https://example.com/", "https://example.com/#login"));
  97. QVERIFY(urlTools()->isUrlIdentical("https://example.com", "https://example.com/"));
  98. QVERIFY(urlTools()->isUrlIdentical("https://example.com/", "https://example.com"));
  99. QVERIFY(urlTools()->isUrlIdentical("https://example.com/ ", " https://example.com"));
  100. QVERIFY(!urlTools()->isUrlIdentical("https://example.com/", " example.com"));
  101. QVERIFY(urlTools()->isUrlIdentical("https://example.com/path/to/nowhere", "https://example.com/path/to/nowhere/"));
  102. QVERIFY(!urlTools()->isUrlIdentical("https://example.com/", "://example.com/"));
  103. QVERIFY(urlTools()->isUrlIdentical("ftp://127.0.0.1/", "ftp://127.0.0.1"));
  104. }
  105. void TestUrlTools::testIsUrlValid()
  106. {
  107. QHash<QString, bool> urls;
  108. urls["https://github.com/login"] = true;
  109. urls["https:///github.com/"] = false;
  110. urls["http://github.com/**//*"] = false;
  111. urls["http://*.github.com/login"] = false;
  112. urls["//github.com"] = true;
  113. urls["github.com/{}<>"] = false;
  114. urls["http:/example.com"] = false;
  115. urls["http:/example.com."] = false;
  116. urls["cmd://C:/Toolchains/msys2/usr/bin/mintty \"ssh jon@192.168.0.1:22\""] = true;
  117. urls["file:///Users/testUser/Code/test.html"] = true;
  118. urls["{REF:A@I:46C9B1FFBD4ABC4BBB260C6190BAD20C} "] = true;
  119. QHashIterator<QString, bool> i(urls);
  120. while (i.hasNext()) {
  121. i.next();
  122. QCOMPARE(urlTools()->isUrlValid(i.key()), i.value());
  123. }
  124. }
  125. void TestUrlTools::testDomainHasIllegalCharacters()
  126. {
  127. QVERIFY(!urlTools()->domainHasIllegalCharacters("example.com"));
  128. QVERIFY(urlTools()->domainHasIllegalCharacters("domain has spaces.com"));
  129. QVERIFY(urlTools()->domainHasIllegalCharacters("example#|.com"));
  130. }