keepassxc

Форк
0
/
TestUrlTools.cpp 
144 строки · 6.0 Кб
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

18
#include "TestUrlTools.h"
19
#include <QTest>
20

21
QTEST_GUILESS_MAIN(TestUrlTools)
22

23
void TestUrlTools::initTestCase()
24
{
25
    m_urlTools = urlTools();
26
}
27

28
void TestUrlTools::init()
29
{
30
}
31

32
void TestUrlTools::testTopLevelDomain()
33
{
34
    // Create list of URLs and expected TLD responses
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")}, // blogspot.com.ar is a TLD
47
        {QString("https://jap.an.ide.kyoto.jp"), QString("ide.kyoto.jp")}, // ide.kyoto.jp is a TLD
48
        {QString("ar"), QString("ar")},
49
    };
50

51
    for (const auto& u : tldUrls) {
52
        QCOMPARE(urlTools()->getTopLevelDomainFromUrl(u.first), u.second);
53
    }
54

55
    // Create list of URLs and expected base URL responses
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")}, // blogspot.com.ar is a TLD
65
        {QString("https://www.arpa"), QString("www.arpa")},
66
        {QString("https://jap.an.ide.kyoto.jp"), QString("an.ide.kyoto.jp")}, // ide.kyoto.jp is a TLD
67
        {QString("https://kobe.jp"), QString("kobe.jp")},
68
    };
69

70
    for (const auto& u : baseUrls) {
71
        QCOMPARE(urlTools()->getBaseDomainFromUrl(u.first), u.second);
72
    }
73
}
74

75
void TestUrlTools::testIsIpAddress()
76
{
77
    auto host1 = "example.com"; // Not valid
78
    auto host2 = "192.168.0.1";
79
    auto host3 = "278.21.2.0"; // Not valid
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"; // Not valid
85
    auto host9 = "[::]";
86
    auto host10 = "::";
87
    auto host11 = "[2001:20::1]";
88

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));
100
}
101

102
void TestUrlTools::testIsUrlIdentical()
103
{
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"));
115
}
116

117
void TestUrlTools::testIsUrlValid()
118
{
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;
131

132
    QHashIterator<QString, bool> i(urls);
133
    while (i.hasNext()) {
134
        i.next();
135
        QCOMPARE(urlTools()->isUrlValid(i.key()), i.value());
136
    }
137
}
138

139
void TestUrlTools::testDomainHasIllegalCharacters()
140
{
141
    QVERIFY(!urlTools()->domainHasIllegalCharacters("example.com"));
142
    QVERIFY(urlTools()->domainHasIllegalCharacters("domain has spaces.com"));
143
    QVERIFY(urlTools()->domainHasIllegalCharacters("example#|.com"));
144
}
145

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.