File: httpheaderdispositiontest.cpp

package info (click to toggle)
kio 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,496 kB
  • sloc: cpp: 123,468; xml: 528; ansic: 466; ruby: 60; sh: 21; makefile: 13
file content (381 lines) | stat: -rw-r--r-- 25,176 bytes parent folder | download | duplicates (2)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
    This file is part of the KDE libraries
    SPDX-FileCopyrightText: 2010, 2011 Rolf Eike Beer <kde@opensource.sf-tec.de>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "httpheaderdispositiontest.h"

#include <QTest>

#include <QByteArray>

#include <parsinghelpers.h>

#include <parsinghelpers.cpp>

// QT5 TODO QTEST_GUILESS_MAIN(HeaderDispositionTest)
QTEST_MAIN(HeaderDispositionTest)

static void runTest(const QString &header, const QByteArray &result)
{
    QMap<QString, QString> parameters = contentDispositionParser(header);

    QList<QByteArray> results = result.split('\n');
    if (result.isEmpty()) {
        results.clear();
    }

    for (const QByteArray &ba : std::as_const(results)) {
        QList<QByteArray> values = ba.split('\t');
        const QString key(QString::fromLatin1(values.takeFirst()));

        QVERIFY(parameters.contains(key));

        const QByteArray val = values.takeFirst();
        QVERIFY(values.isEmpty());

        QCOMPARE(parameters[key], QString::fromUtf8(val.constData(), val.length()));
    }

    QCOMPARE(parameters.count(), results.count());
}

void HeaderDispositionTest::runAllTests_data()
{
    QTest::addColumn<QString>("header");
    QTest::addColumn<QByteArray>("result");

    // http://greenbytes.de/tech/tc2231/
    QTest::newRow("greenbytes-inlonly") << "inline" << QByteArray("type\tinline");
    QTest::newRow("greenbytes-inlonlyquoted") << "\"inline\"" << QByteArray();
    QTest::newRow("greenbytes-inlwithasciifilename") << "inline; filename=\"foo.html\""
                                                     << QByteArray(
                                                            "type\tinline\n"
                                                            "filename\tfoo.html");
    QTest::newRow("greenbytes-inlwithfnattach") << "inline; filename=\"Not an attachment!\""
                                                << QByteArray(
                                                       "type\tinline\n"
                                                       "filename\tNot an attachment!");
    QTest::newRow("greenbytes-inlwithasciifilenamepdf") << "inline; filename=\"foo.pdf\""
                                                        << QByteArray(
                                                               "type\tinline\n"
                                                               "filename\tfoo.pdf");
    QTest::newRow("greenbytes-attonly") << "attachment" << QByteArray("type\tattachment");
    QTest::newRow("greenbytes-attonlyquoted") << "\"attachment\"" << QByteArray();
    QTest::newRow("greenbytes-attonlyucase") << "ATTACHMENT" << QByteArray("type\tattachment");
    QTest::newRow("greenbytes-attwithasciifilename") << "attachment; filename=\"foo.html\""
                                                     << QByteArray(
                                                            "type\tattachment\n"
                                                            "filename\tfoo.html");
    QTest::newRow("greenbytes-attwithasciifnescapedchar") << "attachment; filename=\"f\\oo.html\""
                                                          << QByteArray(
                                                                 "type\tattachment\n"
                                                                 "filename\tfoo.html");
    QTest::newRow("greenbytes-attwithasciifnescapedquote") << "attachment; filename=\"\\\"quoting\\\" tested.html\""
                                                           << QByteArray(
                                                                  "type\tattachment\n"
                                                                  "filename\t\"quoting\" tested.html");
    QTest::newRow("greenbytes-attwithquotedsemicolon") << "attachment; filename=\"Here's a semicolon;.html\""
                                                       << QByteArray(
                                                              "type\tattachment\n"
                                                              "filename\tHere's a semicolon;.html");
    QTest::newRow("greenbytes-attwithfilenameandextparam") << "attachment; foo=\"bar\"; filename=\"foo.html\""
                                                           << QByteArray(
                                                                  "type\tattachment\n"
                                                                  "foo\tbar\n"
                                                                  "filename\tfoo.html");
    QTest::newRow("greenbytes-attwithfilenameandextparamescaped") << "attachment; foo=\"\\\"\\\\\";filename=\"foo.html\""
                                                                  << QByteArray(
                                                                         "type\tattachment\n"
                                                                         "foo\t\"\\\n"
                                                                         "filename\tfoo.html");
    QTest::newRow("greenbytes-attwithasciifilenameucase") << "attachment; FILENAME=\"foo.html\""
                                                          << QByteArray(
                                                                 "type\tattachment\n"
                                                                 "filename\tfoo.html");
    // specification bug in RfC 2616, legal through RfC 2183 and 6266
    QTest::newRow("greenbytes-attwithasciifilenamenq") << "attachment; filename=foo.html"
                                                       << QByteArray(
                                                              "type\tattachment\n"
                                                              "filename\tfoo.html");
    QTest::newRow("greenbytes-attwithasciifilenamenqws") << "attachment; filename=foo bar.html" << QByteArray("type\tattachment");
    QTest::newRow("greenbytes-attwithfntokensq") << "attachment; filename='foo.bar'"
                                                 << QByteArray(
                                                        "type\tattachment\n"
                                                        "filename\t'foo.bar'");
    QTest::newRow("greenbytes-attwithisofnplain-x") << QStringLiteral("attachment; filename=\"foo-\xe4.html\"")
                                                    << QByteArray(
                                                           "type\tattachment\n"
                                                           "filename\tfoo-ä.html");
    QTest::newRow("greenbytes-attwithisofnplain") << QString::fromLatin1("attachment; filename=\"foo-ä.html\"")
                                                  << QByteArray(
                                                         "type\tattachment\n"
                                                         "filename\tfoo-ä.html");
    QTest::newRow("greenbytes-attwithfnrawpctenca") << "attachment; filename=\"foo-%41.html\""
                                                    << QByteArray(
                                                           "type\tattachment\n"
                                                           "filename\tfoo-%41.html");
    QTest::newRow("greenbytes-attwithfnusingpct") << "attachment; filename=\"50%.html\""
                                                  << QByteArray(
                                                         "type\tattachment\n"
                                                         "filename\t50%.html");
    QTest::newRow("greenbytes-attwithfnrawpctencaq") << "attachment; filename=\"foo-%\\41.html\""
                                                     << QByteArray(
                                                            "type\tattachment\n"
                                                            "filename\tfoo-%41.html");
    QTest::newRow("greenbytes-attwithnamepct") << "attachment; name=\"foo-%41.html\""
                                               << QByteArray(
                                                      "type\tattachment\n"
                                                      "name\tfoo-%41.html");
    QTest::newRow("greenbytes-attwithfilenamepctandiso") << QStringLiteral("attachment; filename=\"\xe4-%41.html\"")
                                                         << QByteArray(
                                                                "type\tattachment\n"
                                                                "filename\tä-%41.html");
    QTest::newRow("greenbytes-attwithfnrawpctenclong") << "attachment; filename=\"foo-%c3%a4-%e2%82%ac.html\""
                                                       << QByteArray(
                                                              "type\tattachment\n"
                                                              "filename\tfoo-%c3%a4-%e2%82%ac.html");
    QTest::newRow("greenbytes-attwithasciifilenamews1") << "attachment; filename =\"foo.html\""
                                                        << QByteArray(
                                                               "type\tattachment\n"
                                                               "filename\tfoo.html");
    QTest::newRow("greenbytes-attwith2filenames") << "attachment; filename=\"foo.html\"; filename=\"bar.html\"" << QByteArray("type\tattachment");
    QTest::newRow("greenbytes-attfnbrokentoken") << "attachment; filename=foo[1](2).html" << QByteArray("type\tattachment");
    QTest::newRow("greenbytes-attmissingdisposition") << "filename=foo.html" << QByteArray();
    QTest::newRow("greenbytes-attmissingdisposition2") << "x=y; filename=foo.html" << QByteArray();
    QTest::newRow("greenbytes-attmissingdisposition3") << "\"foo; filename=bar;baz\"; filename=qux" << QByteArray();
    QTest::newRow("greenbytes-attmissingdisposition4") << "filename=foo.html, filename=bar.html" << QByteArray();
    QTest::newRow("greenbytes-emptydisposition") << "; filename=foo.html" << QByteArray();
    QTest::newRow("greenbytes-attbrokenquotedfn") << "attachment; filename=\"foo.html\".txt" << QByteArray("type\tattachment");
    QTest::newRow("greenbytes-attbrokenquotedfn2") << "attachment; filename=\"bar" << QByteArray("type\tattachment");
    QTest::newRow("greenbytes-attbrokenquotedfn3") << "attachment; filename=foo\"bar;baz\"qux" << QByteArray("type\tattachment");
    QTest::newRow("greenbytes-attreversed") << "filename=foo.html; attachment" << QByteArray();
    QTest::newRow("greenbytes-attconfusedparam") << "attachment; xfilename=foo.html"
                                                 << QByteArray(
                                                        "type\tattachment\n"
                                                        "xfilename\tfoo.html");
    QTest::newRow("greenbytes-attabspath") << "attachment; filename=\"/foo.html\""
                                           << QByteArray(
                                                  "type\tattachment\n"
                                                  "filename\tfoo.html");
#ifdef Q_OS_WIN
    QTest::newRow("greenbytes-attabspath") << "attachment; filename=\"\\\\foo.html\""
                                           << QByteArray(
                                                  "type\tattachment\n"
                                                  "filename\tfoo.html");
#else // Q_OS_WIN
    QTest::newRow("greenbytes-attabspath") << "attachment; filename=\"\\\\foo.html\""
                                           << QByteArray(
                                                  "type\tattachment\n"
                                                  "filename\t\\foo.html");
#endif // Q_OS_WIN
    QTest::newRow("greenbytes-") << "attachment; creation-date=\"Wed, 12 Feb 1997 16:29:51 -0500\""
                                 << QByteArray(
                                        "type\tattachment\n"
                                        "creation-date\tWed, 12 Feb 1997 16:29:51 -0500");
    QTest::newRow("greenbytes-") << "attachment; modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\""
                                 << QByteArray(
                                        "type\tattachment\n"
                                        "modification-date\tWed, 12 Feb 1997 16:29:51 -0500");
    QTest::newRow("greenbytes-dispext") << "foobar" << QByteArray("type\tfoobar");
    QTest::newRow("greenbytes-dispextbadfn") << "attachment; example=\"filename=example.txt\""
                                             << QByteArray(
                                                    "type\tattachment\n"
                                                    "example\tfilename=example.txt");
    QTest::newRow("greenbytes-attwithisofn2231iso") << "attachment; filename*=iso-8859-1''foo-%E4.html"
                                                    << QByteArray(
                                                           "type\tattachment\n"
                                                           "filename\tfoo-ä.html");
    QTest::newRow("greenbytes-attwithfn2231utf8") << "attachment; filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html"
                                                  << QByteArray(
                                                         "type\tattachment\n"
                                                         "filename\tfoo-ä-€.html");
    QTest::newRow("greenbytes-attwithfn2231noc") << "attachment; filename*=''foo-%c3%a4-%e2%82%ac.html" << QByteArray("type\tattachment");
    // it's not filename, but "filename "
    QTest::newRow("greenbytes-attwithfn2231ws1") << "attachment; filename *=UTF-8''foo-%c3%a4.html" << QByteArray("type\tattachment");
    QTest::newRow("greenbytes-attwithfn2231ws2") << "attachment; filename*= UTF-8''foo-%c3%a4.html"
                                                 << QByteArray(
                                                        "type\tattachment\n"
                                                        "filename\tfoo-ä.html");
    QTest::newRow("greenbytes-attwithfn2231ws3") << "attachment; filename* =UTF-8''foo-%c3%a4.html"
                                                 << QByteArray(
                                                        "type\tattachment\n"
                                                        "filename\tfoo-ä.html");
    // argument must not be enclosed in double quotes
    QTest::newRow("greenbytes-attwithfn2231quot") << "attachment; filename*=\"UTF-8''foo-%c3%a4.html\"" << QByteArray("type\tattachment");
    QTest::newRow("greenbytes-attwithfn2231dpct") << "attachment; filename*=UTF-8''A-%2541.html"
                                                  << QByteArray(
                                                         "type\tattachment\n"
                                                         "filename\tA-%41.html");
#ifdef Q_OS_WIN
    QTest::newRow("greenbytes-attwithfn2231abspathdisguised") << "attachment; filename*=UTF-8''%5cfoo.html"
                                                              << QByteArray(
                                                                     "type\tattachment\n"
                                                                     "filename\tfoo.html");
#else // Q_OS_WIN
    QTest::newRow("greenbytes-attwithfn2231abspathdisguised") << "attachment; filename*=UTF-8''%5cfoo.html"
                                                              << QByteArray(
                                                                     "type\tattachment\n"
                                                                     "filename\t\\foo.html");
#endif // Q_OS_WIN
    QTest::newRow("greenbytes-attfncont") << "attachment; filename*0=\"foo.\"; filename*1=\"html\""
                                          << QByteArray(
                                                 "type\tattachment\n"
                                                 "filename\tfoo.html");
    QTest::newRow("greenbytes-attfncontenc") << "attachment; filename*0*=UTF-8''foo-%c3%a4; filename*1=\".html\""
                                             << QByteArray(
                                                    "type\tattachment\n"
                                                    "filename\tfoo-ä.html");
    // no leading zeros
    QTest::newRow("greenbytes-attfncontlz") << "attachment; filename*0=\"foo\"; filename*01=\"bar\""
                                            << QByteArray(
                                                   "type\tattachment\n"
                                                   "filename\tfoo");
    QTest::newRow("greenbytes-attfncontnc") << "attachment; filename*0=\"foo\"; filename*2=\"bar\""
                                            << QByteArray(
                                                   "type\tattachment\n"
                                                   "filename\tfoo");
    // first element must have number 0
    QTest::newRow("greenbytes-attfnconts1") << "attachment; filename*1=\"foo.\"; filename*2=\"html\"" << QByteArray("type\tattachment");
    // we must not rely on element ordering
    QTest::newRow("greenbytes-attfncontord") << "attachment; filename*1=\"bar\"; filename*0=\"foo\""
                                             << QByteArray(
                                                    "type\tattachment\n"
                                                    "filename\tfoobar");
    // specifying both param and param* is allowed, param* should be taken
    QTest::newRow("greenbytes-attfnboth") << "attachment; filename=\"foo-ae.html\"; filename*=UTF-8''foo-%c3%a4.html"
                                          << QByteArray(
                                                 "type\tattachment\n"
                                                 "filename\tfoo-ä.html");
    // specifying both param and param* is allowed, param* should be taken
    QTest::newRow("greenbytes-attfnboth2") << "attachment; filename*=UTF-8''foo-%c3%a4.html; filename=\"foo-ae.html\""
                                           << QByteArray(
                                                  "type\tattachment\n"
                                                  "filename\tfoo-ä.html");
    QTest::newRow("greenbytes-attnewandfn") << "attachment; foobar=x; filename=\"foo.html\""
                                            << QByteArray(
                                                   "type\tattachment\n"
                                                   "filename\tfoo.html\n"
                                                   "foobar\tx");
    // invalid argument, should be ignored
    QTest::newRow("greenbytes-attrfc2047token") << "attachment; filename==?ISO-8859-1?Q?foo-=E4.html?=" << QByteArray("type\tattachment");
    QTest::newRow("space_before_value") << "attachment; filename= \"foo.html\""
                                        << QByteArray(
                                               "type\tattachment\n"
                                               "filename\tfoo.html");
    // no character set given but 8 bit characters
    QTest::newRow("8bit_in_ascii") << "attachment; filename*=''foo-%c3%a4.html" << QByteArray("type\tattachment");
    // there may not be gaps in numbering
    QTest::newRow("continuation013") << "attachment; filename*0=\"foo.\"; filename*1=\"html\"; filename*3=\"bar\""
                                     << QByteArray(
                                            "type\tattachment\n"
                                            "filename\tfoo.html");
    // "wrong" element ordering and encoding
    QTest::newRow("reversed_continuation+encoding") << "attachment; filename*1=\"html\"; filename*0*=us-ascii''foo."
                                                    << QByteArray(
                                                           "type\tattachment\n"
                                                           "filename\tfoo.html");
    // unknown charset
    QTest::newRow("unknown_charset") << "attachment; filename*=unknown''foo" << QByteArray("type\tattachment");
    // no apostrophs
    QTest::newRow("encoding-no-apostrophs") << "attachment; filename*=foo" << QByteArray("type\tattachment");
    // only one apostroph
    QTest::newRow("encoding-one-apostroph") << "attachment; filename*=us-ascii'foo" << QByteArray("type\tattachment");
    // duplicate filename, both should be ignored and parsing should stop
    QTest::newRow("duplicate-filename") << "attachment; filename=foo; filename=bar; foo=bar" << QByteArray("type\tattachment");
    // garbage after closing quote, parsing should stop there
    QTest::newRow("garbage_after_closing_quote") << "attachment; filename*=''foo; bar=\"f\"oo; baz=foo"
                                                 << QByteArray(
                                                        "type\tattachment\n"
                                                        "filename\tfoo");
    // trailing whitespace should be ignored
    QTest::newRow("whitespace_after_value") << "attachment; filename=\"foo\" ; bar=baz"
                                            << QByteArray(
                                                   "type\tattachment\n"
                                                   "filename\tfoo\n"
                                                   "bar\tbaz");
    // invalid syntax for type
    QTest::newRow("invalid_type1") << "filename=foo.html" << QByteArray();
    // invalid syntax for type
    QTest::newRow("invalid_type2") << "inline{; filename=\"foo\"" << QByteArray();
    QTest::newRow("invalid_type3") << "foo bar; filename=\"foo\"" << QByteArray();
    QTest::newRow("invalid_type4") << "foo\tbar; filename=\"foo\"" << QByteArray();
    // missing closing quote, so parameter is broken
    QTest::newRow("no_closing_quote") << "attachment; filename=\"bar" << QByteArray("type\tattachment");
    // we ignore any path given in the header and use only the filename
    QTest::newRow("full_path_given") << "attachment; filename=\"/etc/shadow\""
                                     << QByteArray(
                                            "type\tattachment\n"
                                            "filename\tshadow");
    // we ignore any path given in the header and use only the filename even if there is an error later
    QTest::newRow("full_path_and_parse_error") << "attachment; filename=\"/etc/shadow\"; foo=\"baz\"; foo=\"bar\""
                                               << QByteArray(
                                                      "type\tattachment\n"
                                                      "filename\tshadow");
    // control characters are forbidden in the quoted string
    QTest::newRow("control_character_in_value") << "attachment; filename=\"foo\003\"" << QByteArray("type\tattachment");
    // duplicate keys must be ignored
    QTest::newRow("duplicate_with_continuation") << "attachment; filename=\"bar\"; filename*0=\"foo.\"; filename*1=\"html\"" << QByteArray("type\tattachment");

    // percent encoding, invalid first character
    QTest::newRow("percent-first-char-invalid") << "attachment; filename*=UTF-8''foo-%o5.html" << QByteArray("type\tattachment");
    // percent encoding, invalid second character
    QTest::newRow("percent-second-char-invalid") << "attachment; filename*=UTF-8''foo-%5o.html" << QByteArray("type\tattachment");
    // percent encoding, both characters invalid
    QTest::newRow("greenbytes-attwithfn2231nbadpct2") << "attachment; filename*=UTF-8''foo-%oo.html" << QByteArray("type\tattachment");
    // percent encoding, invalid second character
    QTest::newRow("percent-second-char-missing") << "attachment; filename*=UTF-8''foo-%f.html" << QByteArray("type\tattachment");
    // percent encoding, too short value
    QTest::newRow("percent-short-encoding-at-end") << "attachment; filename*=UTF-8''foo-%f" << QByteArray("type\tattachment");
}

#if 0
// currently unclear if our behaviour is only accidentally correct
// invalid syntax
{  "inline; attachment; filename=foo.html",
    "type\tinline"
},
// invalid syntax
{
    "attachment; inline; filename=foo.html",
    "type\tattachment"
},

// deactivated for now: failing due to missing implementation
{
    "attachment; filename=\"foo-&#xc3;&#xa4;.html\"",
    "type\tattachment\n"
    "filename\tfoo-ä.html"
},
// deactivated for now: not the same utf, no idea if the expected value is actually correct
{
    "attachment; filename*=UTF-8''foo-a%cc%88.html",
    "type\tattachment\n"
    "filename\tfoo-ä.html"
}

// deactivated for now: only working to missing implementation
// string is not valid iso-8859-1 so filename should be ignored
//"attachment; filename*=iso-8859-1''foo-%c3%a4-%e2%82%ac.html",
//"type\tattachment",

// deactivated for now: only working to missing implementation
// should not be decoded
//"attachment; filename=\"=?ISO-8859-1?Q?foo-=E4.html?=\"",
//"type\tattachment\n"
//"filename\t=?ISO-8859-1?Q?foo-=E4.html?=",

};
#endif

void HeaderDispositionTest::runAllTests()
{
    QFETCH(QString, header);
    QFETCH(QByteArray, result);

    runTest(header, result);
}

#include "moc_httpheaderdispositiontest.cpp"