summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/io/qurluts46/tst_qurluts46.cpp
blob: 9d738941ef57fd24ee727c178b61ed0787da25be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QtCore/QUrl>
#include <QtCore/QFile>
#include <QTest>
#include <QSet>
#include <QByteArray>
#include <algorithm>

class tst_QUrlUts46 : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void idnaTestV2_data();
    void idnaTestV2();

private:
    // All error codes:
    //      A3, A4_1, A4_2,
    //      B1, B2, B3, B4, B5, B6,
    //      C1, C2,
    //      P1, P4,
    //      V1, V2, V3, V5, V6,
    //      X4_2
    //
    // NOTE: moving this inside idnaTestV2_data() results in ICE with MSVC 2019
    static const QSet<QByteArray> fatalErrors;
};

const QSet<QByteArray> tst_QUrlUts46::fatalErrors = { "A3", "A4_2", "P1", "X4_2" };

void tst_QUrlUts46::idnaTestV2_data()
{
    QTest::addColumn<QString>("source");
    QTest::addColumn<QString>("toUnicode");
    QTest::addColumn<bool>("toUnicodeOk");
    QTest::addColumn<QString>("toAsciiN");
    QTest::addColumn<bool>("toAsciiNOk");
    QTest::addColumn<QString>("toAsciiT");
    QTest::addColumn<bool>("toAsciiTOk");

    QFile dataFile(QFINDTESTDATA("testdata/IdnaTestV2.txt"));
    qDebug() << "Data file:" << dataFile.fileName();
    QVERIFY(dataFile.open(QFile::ReadOnly));

    auto isToAsciiOk = [](const QByteArray &s, bool ifEmpty) {
        if (s.isEmpty())
            return ifEmpty;

        Q_ASSERT(s.startsWith('[') && s.endsWith(']'));

        const auto errors = s.sliced(1, s.size() - 2).split(',');
        // NOTE: empty string is not in fatalErrors and it's ok
        return std::all_of(errors.begin(), errors.end(),
                           [](auto &e) { return !fatalErrors.contains(e.trimmed()); });
    };

    for (unsigned int lineNo = 1; !dataFile.atEnd(); lineNo++) {
        auto line = dataFile.readLine().trimmed();

        int commentIdx = line.indexOf('#');
        if (commentIdx != -1)
            line = line.left(commentIdx).trimmed();
        if (line.isEmpty())
            continue;

        auto fields = line.split(';');
        Q_ASSERT(fields.size() == 7);

        for (auto &field : fields)
            field = field.trimmed();

        const QString &source = fields[0];
        QString toUnicode = fields[1].isEmpty() ? source : fields[1];
        bool toUnicodeOk = fields[2].isEmpty();
        bool toUnicodeOkForAscii = isToAsciiOk(fields[2], true);
        QString toAsciiN = fields[3].isEmpty() ? toUnicode : fields[3];
        bool toAsciiNOk = isToAsciiOk(fields[4], toUnicodeOkForAscii);
        QString toAsciiT = fields[5].isEmpty() ? toAsciiN : fields[5];
        bool toAsciiTOk = isToAsciiOk(fields[6], toAsciiNOk);

        QTest::addRow("line %u", lineNo) << source << toUnicode << toUnicodeOk << toAsciiN
                                         << toAsciiNOk << toAsciiT << toAsciiTOk;
    }
}

void tst_QUrlUts46::idnaTestV2()
{
    QFETCH(QString, source);
    QFETCH(QString, toUnicode);
    QFETCH(bool, toUnicodeOk);
    QFETCH(QString, toAsciiN);
    QFETCH(bool, toAsciiNOk);
    QFETCH(QString, toAsciiT);
    QFETCH(bool, toAsciiTOk);

    auto dashesOk = [](const QString &domain) {
        const auto labels = domain.split(u'.');
        return std::all_of(labels.begin(), labels.end(), [](const QString &label) {
            return label.isEmpty() || !(label.startsWith(u'-') || label.endsWith(u'-'));
        });
    };

    QString toAceN = QUrl::toAce(source);
    if (toAsciiNOk && dashesOk(toAsciiN))
        QCOMPARE(toAceN, toAsciiN);
    else
        QCOMPARE(toAceN, QString());

    QString toAceT = QUrl::toAce(source, QUrl::AceTransitionalProcessing);
    if (toAsciiTOk && dashesOk(toAsciiT))
        QCOMPARE(toAceT, toAsciiT);
    else
        QCOMPARE(toAceT, QString());

    QString normalized = QUrl::fromAce(toAceN.toUtf8(), QUrl::IgnoreIDNWhitelist);
    if (toUnicodeOk && !toAceN.isEmpty())
        QCOMPARE(normalized, toUnicode);
    else
        QCOMPARE(normalized, toAceN);
}

QTEST_APPLESS_MAIN(tst_QUrlUts46)

#include "tst_qurluts46.moc"