18
#include "TestUrlTools.h"
21
QTEST_GUILESS_MAIN(TestUrlTools)
23
void TestUrlTools::initTestCase()
25
m_urlTools = urlTools();
28
void TestUrlTools::init()
32
void TestUrlTools::testTopLevelDomain()
35
QList<QPair<QString, QString>> tldUrls{
36
{QString("https://another.example.co.uk"), QString("co.uk")},
37
{QString("https://www.example.com"), QString("com")},
38
{QString("https://example.com"), QString("com")},
39
{QString("https://github.com"), QString("com")},
40
{QString("http://test.net"), QString("net")},
41
{QString("http://so.many.subdomains.co.jp"), QString("co.jp")},
42
{QString("https://192.168.0.1"), QString("192.168.0.1")},
43
{QString("https://192.168.0.1:8000"), QString("192.168.0.1")},
44
{QString("https://www.nic.ar"), QString("ar")},
45
{QString("https://no.no.no"), QString("no")},
46
{QString("https://www.blogspot.com.ar"), QString("blogspot.com.ar")},
47
{QString("https://jap.an.ide.kyoto.jp"), QString("ide.kyoto.jp")},
48
{QString("ar"), QString("ar")},
51
for (const auto& u : tldUrls) {
52
QCOMPARE(urlTools()->getTopLevelDomainFromUrl(u.first), u.second);
56
QList<QPair<QString, QString>> baseUrls{
57
{QString("https://another.example.co.uk"), QString("example.co.uk")},
58
{QString("https://www.example.com"), QString("example.com")},
59
{QString("http://test.net"), QString("test.net")},
60
{QString("http://so.many.subdomains.co.jp"), QString("subdomains.co.jp")},
61
{QString("https://192.168.0.1"), QString("192.168.0.1")},
62
{QString("https://192.168.0.1:8000"), QString("192.168.0.1")},
63
{QString("https://www.nic.ar"), QString("nic.ar")},
64
{QString("https://www.blogspot.com.ar"), QString("www.blogspot.com.ar")},
65
{QString("https://www.arpa"), QString("www.arpa")},
66
{QString("https://jap.an.ide.kyoto.jp"), QString("an.ide.kyoto.jp")},
67
{QString("https://kobe.jp"), QString("kobe.jp")},
70
for (const auto& u : baseUrls) {
71
QCOMPARE(urlTools()->getBaseDomainFromUrl(u.first), u.second);
75
void TestUrlTools::testIsIpAddress()
77
auto host1 = "example.com";
78
auto host2 = "192.168.0.1";
79
auto host3 = "278.21.2.0";
80
auto host4 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
81
auto host5 = "2001:db8:0:1:1:1:1:1";
82
auto host6 = "fe80::1ff:fe23:4567:890a";
83
auto host7 = "2001:20::1";
84
auto host8 = "2001:0db8:85y3:0000:0000:8a2e:0370:7334";
87
auto host11 = "[2001:20::1]";
89
QVERIFY(!urlTools()->isIpAddress(host1));
90
QVERIFY(urlTools()->isIpAddress(host2));
91
QVERIFY(!urlTools()->isIpAddress(host3));
92
QVERIFY(urlTools()->isIpAddress(host4));
93
QVERIFY(urlTools()->isIpAddress(host5));
94
QVERIFY(urlTools()->isIpAddress(host6));
95
QVERIFY(urlTools()->isIpAddress(host7));
96
QVERIFY(!urlTools()->isIpAddress(host8));
97
QVERIFY(urlTools()->isIpAddress(host9));
98
QVERIFY(urlTools()->isIpAddress(host10));
99
QVERIFY(urlTools()->isIpAddress(host11));
102
void TestUrlTools::testIsUrlIdentical()
104
QVERIFY(urlTools()->isUrlIdentical("https://example.com", "https://example.com"));
105
QVERIFY(urlTools()->isUrlIdentical("https://example.com", " https://example.com "));
106
QVERIFY(!urlTools()->isUrlIdentical("https://example.com", "https://example2.com"));
107
QVERIFY(!urlTools()->isUrlIdentical("https://example.com/", "https://example.com/#login"));
108
QVERIFY(urlTools()->isUrlIdentical("https://example.com", "https://example.com/"));
109
QVERIFY(urlTools()->isUrlIdentical("https://example.com/", "https://example.com"));
110
QVERIFY(urlTools()->isUrlIdentical("https://example.com/ ", " https://example.com"));
111
QVERIFY(!urlTools()->isUrlIdentical("https://example.com/", " example.com"));
112
QVERIFY(urlTools()->isUrlIdentical("https://example.com/path/to/nowhere", "https://example.com/path/to/nowhere/"));
113
QVERIFY(!urlTools()->isUrlIdentical("https://example.com/", "://example.com/"));
114
QVERIFY(urlTools()->isUrlIdentical("ftp://127.0.0.1/", "ftp://127.0.0.1"));
117
void TestUrlTools::testIsUrlValid()
119
QHash<QString, bool> urls;
120
urls["https://github.com/login"] = true;
121
urls["https:///github.com/"] = false;
122
urls["http://github.com/**//*"] = false;
123
urls["http://*.github.com/login"] = false;
124
urls["//github.com"] = true;
125
urls["github.com/{}<>"] = false;
126
urls["http:/example.com"] = false;
127
urls["http:/example.com."] = false;
128
urls["cmd://C:/Toolchains/msys2/usr/bin/mintty \"ssh jon@192.168.0.1:22\""] = true;
129
urls["file:///Users/testUser/Code/test.html"] = true;
130
urls["{REF:A@I:46C9B1FFBD4ABC4BBB260C6190BAD20C} "] = true;
132
QHashIterator<QString, bool> i(urls);
133
while (i.hasNext()) {
135
QCOMPARE(urlTools()->isUrlValid(i.key()), i.value());
139
void TestUrlTools::testDomainHasIllegalCharacters()
141
QVERIFY(!urlTools()->domainHasIllegalCharacters("example.com"));
142
QVERIFY(urlTools()->domainHasIllegalCharacters("domain has spaces.com"));
143
QVERIFY(urlTools()->domainHasIllegalCharacters("example#|.com"));