Debian Codegraph @

tests/auto/corelib/text/qbytearray_large/tst_qbytearray_large.cpp

raw
1 // Copyright (C) 2022 The Qt Company Ltd.
2 // Copyright (C) 2022 Intel Corporation.
3 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4
5 #include <QTest>
6
7 #include <qbytearray.h>
8
9 #include <q20iterator.h>
10 #include <stdexcept>
11 #include <string_view>
12
13 class tst_QByteArrayLarge : public QObject
14 {
15 Q_OBJECT
16
17 private slots:
18 void initTestCase();
19 #ifndef QT_NO_COMPRESS
20 void qCompress_data();
21 void qCompress();
22 void qUncompressCorruptedData_data();
23 void qUncompressCorruptedData();
24 void qUncompress4GiBPlus();
25 void qCompressionZeroTermination();
26 #endif
27 void base64_2GiB();
28 };
29
30 void tst_QByteArrayLarge::initTestCase()
31 {
32 #if defined(QT_ASAN_ENABLED)
33 QSKIP("Skipping QByteArray tests under ASAN as they are too slow");
34 #endif
35 }
36
37 #ifndef QT_NO_COMPRESS
38 void tst_QByteArrayLarge::qCompress_data()
39 {
40 QTest::addColumn<QByteArray>("ba");
41
42 const int size1 = 1024*1024;
43 QByteArray ba1( size1, 0 );
44
45 QTest::newRow( "00" ) << QByteArray();
46
47 int i;
48 for ( i=0; i<size1; i++ )
49 ba1[i] = (char)( i / 1024 );
50 QTest::newRow( "01" ) << ba1;
51
52 for ( i=0; i<size1; i++ )
53 ba1[i] = (char)( i % 256 );
54 QTest::newRow( "02" ) << ba1;
55
56 ba1.fill( 'A' );
57 QTest::newRow( "03" ) << ba1;
58
59 QFile file( QFINDTESTDATA("rfc3252.txt") );
60 QVERIFY( file.open(QIODevice::ReadOnly) );
61 QTest::newRow( "04" ) << file.readAll();
62 }
63
64 void tst_QByteArrayLarge::qCompress()
65 {
66 QFETCH( QByteArray, ba );
67 QByteArray compressed = ::qCompress( ba );
68 QTEST( ::qUncompress( compressed ), "ba" );
69 }
70
71 void tst_QByteArrayLarge::qUncompressCorruptedData_data()
72 {
73 QTest::addColumn<QByteArray>("in");
74
75 QTest::newRow("0x00000000") << QByteArray("\x00\x00\x00\x00", 4);
76 QTest::newRow("0x000000ff") << QByteArray("\x00\x00\x00\xff", 4);
77 QTest::newRow("0x3f000000") << QByteArray("\x3f\x00\x00\x00", 4);
78 QTest::newRow("0x3fffffff") << QByteArray("\x3f\xff\xff\xff", 4);
79 QTest::newRow("0x7fffff00") << QByteArray("\x7f\xff\xff\x00", 4);
80 QTest::newRow("0x7fffffff") << QByteArray("\x7f\xff\xff\xff", 4);
81 QTest::newRow("0x80000000") << QByteArray("\x80\x00\x00\x00", 4);
82 QTest::newRow("0x800000ff") << QByteArray("\x80\x00\x00\xff", 4);
83 QTest::newRow("0xcf000000") << QByteArray("\xcf\x00\x00\x00", 4);
84 QTest::newRow("0xcfffffff") << QByteArray("\xcf\xff\xff\xff", 4);
85 QTest::newRow("0xffffff00") << QByteArray("\xff\xff\xff\x00", 4);
86 QTest::newRow("0xffffffff") << QByteArray("\xff\xff\xff\xff", 4);
87 }
88
89 // This test is expected to produce some warning messages in the test output.
90 void tst_QByteArrayLarge::qUncompressCorruptedData()
91 {
92 QFETCH(QByteArray, in);
93
94 QByteArray res;
95 res = ::qUncompress(in);
96 QCOMPARE(res, QByteArray());
97
98 res = ::qUncompress(in + "blah");
99 QCOMPARE(res, QByteArray());
100 }
101
102 void tst_QByteArrayLarge::qUncompress4GiBPlus()
103 {
104 // after three rounds, this decompresses to 4GiB + 1 'X' bytes:
105 constexpr uchar compressed_3x[] = {
106 0x00, 0x00, 0x1a, 0x76, 0x78, 0x9c, 0x63, 0xb0, 0xdf, 0xb4, 0xad, 0x62,
107 0xce, 0xdb, 0x3b, 0x0b, 0xf3, 0x26, 0x27, 0x4a, 0xb4, 0x3d, 0x34, 0x5b,
108 0xed, 0xb4, 0x41, 0xf1, 0xc0, 0x99, 0x2f, 0x02, 0x05, 0x67, 0x26, 0x88,
109 0x6c, 0x66, 0x71, 0x34, 0x62, 0x9c, 0x75, 0x26, 0xb1, 0xa0, 0x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 0x32, 0xa3, 0x32, 0xa3, 0x32,
125 0xa3, 0x32, 0xa3, 0x32, 0xa3, 0x32, 0xa3, 0x32, 0xa3, 0x32, 0xa3, 0x32,
126 0xa3, 0x32, 0xa3, 0x32, 0xa3, 0x32, 0xa3, 0x32, 0xa3, 0x32, 0xa3, 0x32,
127 0xa3, 0x32, 0xa3, 0x32, 0xa3, 0x32, 0x34, 0x90, 0x99, 0xb6, 0x7e, 0xf5,
128 0xd3, 0xe9, 0xbf, 0x35, 0x13, 0xca, 0x8c, 0x75, 0xec, 0xec, 0xa4, 0x2f,
129 0x7e, 0x2d, 0xf9, 0xf3, 0xf0, 0xee, 0xea, 0xd5, 0xf5, 0xd3, 0x14, 0x57,
130 0x06, 0x00, 0x00, 0xb9, 0x1e, 0x35, 0xce
131 };
132
133 constexpr qint64 GiB = 1024LL * 1024 * 1024;
134
135 if constexpr (sizeof(qsizetype) == sizeof(int)) {
136 QSKIP("This is a 64-bit-only test.");
137 } else {
138
139 // 1st
140 auto c = ::qUncompress(std::data(compressed_3x), q20::ssize(compressed_3x));
141 QVERIFY(!c.isNull()); // check for decompression error
142
143 // 2nd
144 c = ::qUncompress(c);
145 QVERIFY(!c.isNull());
146
147 // 3rd
148 try {
149 c = ::qUncompress(c);
150 if (c.isNull()) // this step (~18MiB -> 4GiB) might have run out of memory
151 QSKIP("Failed to allocate enough memory.");
152 } catch (const std::bad_alloc &) {
153 QSKIP("Failed to allocate enough memory.");
154 }
155
156 QCOMPARE(c.size(), 4 * GiB + 1);
157 QCOMPARE(std::string_view{c}.find_first_not_of('X'),
158 std::string_view::npos);
159
160 // re-compress once
161 // (produces 18MiB, we shouldn't use much more than that in allocated capacity)
162 c = ::qCompress(c);
163 QVERIFY(!c.isNull());
164
165 // and un-compress again, to make sure compression worked (we
166 // can't compare with compressed_3x, because zlib may change):
167 c = ::qUncompress(c);
168
169 QCOMPARE(c.size(), 4 * GiB + 1);
170 QCOMPARE(std::string_view{c}.find_first_not_of('X'),
171 std::string_view::npos);
172 }
173 }
174
175 void tst_QByteArrayLarge::qCompressionZeroTermination()
176 {
177 QByteArray s = "Hello, I'm a string.";
178 QByteArray ba = ::qUncompress(::qCompress(s));
179 QCOMPARE(ba.data()[ba.size()], '\0');
180 QCOMPARE(ba, s);
181 }
182 #endif
183
184 void tst_QByteArrayLarge::base64_2GiB()
185 {
186 #if defined Q_OS_ANDROID || defined Q_OS_WEBOS
187 QSKIP("Android and webOS device will kill the test when using too much memory");
188 #endif
189 if constexpr (sizeof(qsizetype) > sizeof(int)) {
190 try {
191 constexpr qint64 GiB = 1024 * 1024 * 1024;
192 static_assert((2 * GiB + 1) % 3 == 0);
193 const char inputChar = '\0'; // all-NULs encode as
194 const char outputChar = 'A'; // all-'A's
195 const qint64 inputSize = 2 * GiB + 1;
196 const qint64 outputSize = inputSize / 3 * 4;
197 const auto sv = [](const QByteArray &ba) {
198 return std::string_view{ba.data(), size_t(ba.size())};
199 };
200 QByteArray output;
201 {
202 const QByteArray input(inputSize, inputChar);
203 output = input.toBase64();
204 QCOMPARE(output.size(), outputSize);
205 QCOMPARE(sv(output).find_first_not_of(outputChar),
206 std::string_view::npos);
207 }
208 {
209 auto r = QByteArray::fromBase64Encoding(output);
210 QCOMPARE_EQ(r.decodingStatus, QByteArray::Base64DecodingStatus::Ok);
211 QCOMPARE(r.decoded.size(), inputSize);
212 QCOMPARE(sv(r.decoded).find_first_not_of(inputChar),
213 std::string_view::npos);
214 }
215 } catch (const std::bad_alloc &) {
216 QSKIP("Could not allocate enough RAM.");
217 }
218 } else {
219 QSKIP("This is a 64-bit only test");
220 }
221 }
222
223 QTEST_MAIN(tst_QByteArrayLarge)
224 #include "tst_qbytearray_large.moc"