File indexing completed on 2024-12-08 13:24:28

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2022 Daniel Vrátil <dvratil@kde.org>
0003 
0004 #include "ipvalidator.h"
0005 
0006 #include <QTest>
0007 
0008 class IPValidatorTest : public QObject
0009 {
0010     Q_OBJECT
0011 private Q_SLOTS:
0012     void testIPv4Validation_data()
0013     {
0014         QTest::addColumn<QString>("input");
0015         QTest::addColumn<QValidator::State>("state");
0016 
0017         QTest::newRow("1") << QStringLiteral("1") << QValidator::Intermediate;
0018         QTest::newRow("10.") << QStringLiteral("10.") << QValidator::Intermediate;
0019         QTest::newRow("10.a") << QStringLiteral("10.a") << QValidator::Invalid;
0020         QTest::newRow("10.0.0") << QStringLiteral("10.0.0") << QValidator::Intermediate;
0021         QTest::newRow("10.0.0.0") << QStringLiteral("10.0.0.0") << QValidator::Acceptable;
0022         QTest::newRow("10.0.0.0/") << QStringLiteral("10.0.0.0/") << QValidator::Intermediate;
0023         QTest::newRow("10.0.0.0/1") << QStringLiteral("10.0.0.0/1") << QValidator::Acceptable;
0024         QTest::newRow("10.0.0.0/10") << QStringLiteral("10.0.0.0/10") << QValidator::Acceptable;
0025         QTest::newRow("10.0.0.0/100") << QStringLiteral("10.0.0.0/100") << QValidator::Invalid;
0026         QTest::newRow("192.168.0.256") << QStringLiteral("192.168.0.256") << QValidator::Intermediate;
0027         QTest::newRow("192.168.0.0/.") << QStringLiteral("192.168.0.0/.") << QValidator::Invalid;
0028         QTest::newRow("::1") << QStringLiteral("::1") << QValidator::Invalid;
0029         QTest::newRow("fe80::39fe:37cd:6d9d:9850/64") << QStringLiteral("fe80::39fe:37cd:6d9d:9850/64") << QValidator::Invalid;
0030     }
0031 
0032     void testIPv4Validation()
0033     {
0034         QFETCH(QString, input);
0035         QFETCH(QValidator::State, state);
0036 
0037         IPValidator validator;
0038         QCOMPARE(validator.ipVersion(), IPValidator::IPVersion::IPv4); // default
0039         int pos = 0;
0040         QCOMPARE(validator.validate(input, pos), state);
0041     }
0042 
0043     void testIPv6Validation_data()
0044     {
0045         QTest::addColumn<QString>("input");
0046         QTest::addColumn<QValidator::State>("state");
0047 
0048         QTest::newRow("::1") << QStringLiteral("::1") << QValidator::Acceptable;
0049         QTest::newRow("fe80:::") << QStringLiteral("fe80:::") << QValidator::Intermediate;
0050         QTest::newRow("fe80::39fe:37cd:6d9d:9850") << QStringLiteral("fe80::39fe:37cd:6d9d:9850") << QValidator::Acceptable;
0051         QTest::newRow("fe80::39fe:37cd:6d9d:9850/") << QStringLiteral("fe80::39fe:37cd:6d9d:9850/") << QValidator::Intermediate;
0052         QTest::newRow("fe80::39fe:37cd:6d9d:9850/64") << QStringLiteral("fe80::39fe:37cd:6d9d:9850/64") << QValidator::Acceptable;
0053         QTest::newRow("fe80::39fe:37cd:6d9d:9850/fe") << QStringLiteral("fe80::39fe:37cd:6d9d:9850/fe") << QValidator::Invalid;
0054         QTest::newRow("fe80::39fe:37cd:6d9d:9850/190") << QStringLiteral("fe80::39fe:37cd:6d9d:9850/190") << QValidator::Invalid;
0055         QTest::newRow("fg80::39fe") << QStringLiteral("fg80::39fe") << QValidator::Invalid;
0056         QTest::newRow("192.168.0.0") << QStringLiteral("192.168.0.1") << QValidator::Intermediate;
0057         QTest::newRow("192.168.0.0/24") << QStringLiteral("192.168.0.0/24") << QValidator::Intermediate;
0058         QTest::newRow("::FFFF:1.2.3.4") << QStringLiteral("::FFFF:1.2.3.4") << QValidator::Acceptable;
0059         QTest::newRow("::FFFF:1.2.0.0/112") << QStringLiteral("::FFFF:1.2.0.0/112") << QValidator::Acceptable;
0060     }
0061 
0062     void testIPv6Validation()
0063     {
0064         QFETCH(QString, input);
0065         QFETCH(QValidator::State, state);
0066 
0067         IPValidator validator;
0068         validator.setIPVersion(IPValidator::IPVersion::IPv6);
0069         int pos = 0;
0070         QCOMPARE(validator.validate(input, pos), state);
0071     }
0072 };
0073 
0074 QTEST_MAIN(IPValidatorTest)
0075 
0076 #include "ipvalidatortest.moc"