1
/****************************************************************************
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and The Qt Company. For licensing terms
14
** and conditions see https://www.qt.io/terms-conditions. For further
15
** information use the contact form at https://www.qt.io/contact-us.
17
** GNU General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU
19
** General Public License version 3 as published by the Free Software
20
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21
** included in the packaging of this file. Please review the following
22
** information to ensure the GNU General Public License requirements will
23
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
27
****************************************************************************/
28
#include <QtTest/QtTest>
30
void loadInvalidUtf8Rows()
32
QTest::newRow("1char") << QByteArray("\x80");
33
QTest::newRow("2chars-1") << QByteArray("\xC2\xC0");
34
QTest::newRow("2chars-2") << QByteArray("\xC3\xDF");
35
QTest::newRow("2chars-3") << QByteArray("\xC7\xF0");
36
QTest::newRow("3chars-1") << QByteArray("\xE0\xA0\xC0");
37
QTest::newRow("3chars-2") << QByteArray("\xE0\xC0\xA0");
38
QTest::newRow("4chars-1") << QByteArray("\xF0\x90\x80\xC0");
39
QTest::newRow("4chars-2") << QByteArray("\xF0\x90\xC0\x80");
40
QTest::newRow("4chars-3") << QByteArray("\xF0\xC0\x80\x80");
42
// Surrogate pairs must now be present either
43
// U+D800: 1101 10 0000 00 0000
44
// encoding: xxxz:1101 xz10:0000 xz00:0000
45
QTest::newRow("hi-surrogate") << QByteArray("\xED\xA0\x80");
46
// U+DC00: 1101 11 0000 00 0000
47
// encoding: xxxz:1101 xz11:0000 xz00:0000
48
QTest::newRow("lo-surrogate") << QByteArray("\xED\xB0\x80");
51
QTest::newRow("surrogate-pair") << QByteArray("\xED\xA0\x80\xED\xB0\x80");
53
// Characters outside the Unicode range:
54
// 0x110000: 00 0100 01 0000 00 0000 00 0000
55
// encoding: xxxx:z100 xz01:0000 xz00:0000 xz00:0000
56
QTest::newRow("non-unicode-1") << QByteArray("\xF4\x90\x80\x80");
57
// 0x200000: 00 1000 00 0000 00 0000 00 0000
58
// encoding: xxxx:xz00 xz00:1000 xz00:0000 xz00:0000 xz00:0000
59
QTest::newRow("non-unicode-2") << QByteArray("\xF8\x88\x80\x80\x80");
60
// 0x04000000: 0100 00 0000 00 0000 00 0000 00 0000
61
// encoding: xxxx:xxz0 xz00:0100 xz00:0000 xz00:0000 xz00:0001 xz00:0001
62
QTest::newRow("non-unicode-3") << QByteArray("\xFC\x84\x80\x80\x80\x80");
63
// 0x7fffffff: 1 11 1111 11 1111 11 1111 11 1111 11 1111
64
// encoding: xxxx:xxz0 xz00:0100 xz00:0000 xz00:0000 xz00:0001 xz00:0001
65
QTest::newRow("non-unicode-4") << QByteArray("\xFD\xBF\xBF\xBF\xBF\xBF");
67
// As seen above, 0xFE and 0xFF never appear:
68
QTest::newRow("fe") << QByteArray("\xFE");
69
QTest::newRow("fe-bis") << QByteArray("\xFE\xBF\xBF\xBF\xBF\xBF\xBF");
70
QTest::newRow("ff") << QByteArray("\xFF");
71
QTest::newRow("ff-bis") << QByteArray("\xFF\xBF\xBF\xBF\xBF\xBF\xBF\xBF");
73
// some combinations in UTF-8 are invalid even though they have the proper bits set
74
// these are known as overlong sequences
76
// "A": U+0041: 01 00 0001
77
// overlong 2: xxz0:0001 xz00:0001
78
QTest::newRow("overlong-1-2") << QByteArray("\xC1\x81");
79
// overlong 3: xxxz:0000 xz00:0001 xz00:0001
80
QTest::newRow("overlong-1-3") << QByteArray("\xE0\x81\x81");
81
// overlong 4: xxxx:z000 xz00:0000 xz00:0001 xz00:0001
82
QTest::newRow("overlong-1-4") << QByteArray("\xF0\x80\x81\x81");
83
// overlong 5: xxxx:xz00 xz00:0000 xz00:0000 xz00:0001 xz00:0001
84
QTest::newRow("overlong-1-5") << QByteArray("\xF8\x80\x80\x81\x81");
85
// overlong 6: xxxx:xxz0 xz00:0000 xz00:0000 xz00:0000 xz00:0001 xz00:0001
86
QTest::newRow("overlong-1-6") << QByteArray("\xFC\x80\x80\x80\x81\x81");
88
// NBSP: U+00A0: 10 00 0000
89
// proper encoding: xxz0:0010 xz00:0000
90
// overlong 3: xxxz:0000 xz00:0010 xz00:0000
91
QTest::newRow("overlong-2-3") << QByteArray("\xC0\x82\x80");
92
// overlong 4: xxxx:z000 xz00:0000 xz00:0010 xz00:0000
93
QTest::newRow("overlong-2-4") << QByteArray("\xF0\x80\x82\x80");
94
// overlong 5: xxxx:xz00 xz00:0000 xz00:0000 xz00:0010 xz00:0000
95
QTest::newRow("overlong-2-5") << QByteArray("\xF8\x80\x80\x82\x80");
96
// overlong 6: xxxx:xxz0 xz00:0000 xz00:0000 xz00:0000 xz00:0010 xz00:0000
97
QTest::newRow("overlong-2-6") << QByteArray("\xFC\x80\x80\x80\x82\x80");
99
// U+0800: 10 0000 00 0000
100
// proper encoding: xxxz:0000 xz10:0000 xz00:0000
101
// overlong 4: xxxx:z000 xz00:0000 xz10:0000 xz00:0000
102
QTest::newRow("overlong-3-4") << QByteArray("\xF0\x80\xA0\x80");
103
// overlong 5: xxxx:xz00 xz00:0000 xz00:0000 xz10:0000 xz00:0000
104
QTest::newRow("overlong-3-5") << QByteArray("\xF8\x80\x80\xA0\x80");
105
// overlong 6: xxxx:xxz0 xz00:0000 xz00:0000 xz00:0000 xz10:0000 xz00:0000
106
QTest::newRow("overlong-3-6") << QByteArray("\xFC\x80\x80\x80\xA0\x80");
108
// U+010000: 00 0100 00 0000 00 0000
109
// proper encoding: xxxx:z000 xz00:0100 xz00:0000 xz00:0000
110
// overlong 5: xxxx:xz00 xz00:0000 xz00:0100 xz00:0000 xz00:0000
111
QTest::newRow("overlong-4-5") << QByteArray("\xF8\x80\x84\x80\x80");
112
// overlong 6: xxxx:xxz0 xz00:0000 xz00:0000 xz00:0100 xz00:0000 xz00:0000
113
QTest::newRow("overlong-4-6") << QByteArray("\xFC\x80\x80\x84\x80\x80");
117
void loadNonCharactersRows()
119
// Unicode has a couple of "non-characters" that one can use internally
120
// These characters are allowed for text-interchange (see http://www.unicode.org/versions/corrigendum9.html)
122
// Those are the last two entries each Unicode Plane (U+FFFE, U+FFFF,
123
// U+1FFFE, U+1FFFF, etc.) as well as the entries between U+FDD0 and
124
// U+FDEF (inclusive)
126
// U+FDD0 through U+FDEF
127
for (int i = 0; i < 16; ++i) {
128
char utf8[] = { char(0357), char(0267), char(0220 + i), 0 };
129
QString utf16 = QChar(0xfdd0 + i);
130
QTest::newRow(qPrintable(QString::number(0xfdd0 + i, 16))) << QByteArray(utf8) << utf16;
133
// the last two in Planes 1 through 16
134
for (uint plane = 1; plane <= 16; ++plane) {
135
for (uint lower = 0xfffe; lower < 0x10000; ++lower) {
136
uint ucs4 = (plane << 16) | lower;
137
char utf8[] = { char(0xf0 | uchar(ucs4 >> 18)),
138
char(0x80 | (uchar(ucs4 >> 12) & 0x3f)),
139
char(0x80 | (uchar(ucs4 >> 6) & 0x3f)),
140
char(0x80 | (uchar(ucs4) & 0x3f)),
142
ushort utf16[] = { QChar::highSurrogate(ucs4), QChar::lowSurrogate(ucs4), 0 };
144
QTest::newRow(qPrintable(QString::number(ucs4, 16))) << QByteArray(utf8) << QString::fromUtf16(utf16);
148
QTest::newRow("fffe") << QByteArray("\xEF\xBF\xBE") << QString(QChar(0xfffe));
149
QTest::newRow("ffff") << QByteArray("\xEF\xBF\xBF") << QString(QChar(0xffff));