Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MultipartReaderTests.cs « test « WebUtilities « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 206c02831aba4e33312d1a1c0ee71f9fa56213d3 (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
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
382
383
384
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable warnings

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.AspNetCore.WebUtilities;

public class MultipartReaderTests
{
    private const string Boundary = "9051914041544843365972754266";
    // Note that CRLF (\r\n) is required. You can't use multi-line C# strings here because the line breaks on Linux are just LF.
    private const string OnePartBody =
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"text\"\r\n" +
"\r\n" +
"text default\r\n" +
"--9051914041544843365972754266--\r\n";
    private const string OnePartBodyTwoHeaders =
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"text\"\r\n" +
"Custom-header: custom-value\r\n" +
"\r\n" +
"text default\r\n" +
"--9051914041544843365972754266--\r\n";
    private const string OnePartBodyWithTrailingWhitespace =
"--9051914041544843365972754266             \r\n" +
"Content-Disposition: form-data; name=\"text\"\r\n" +
"\r\n" +
"text default\r\n" +
"--9051914041544843365972754266--\r\n";
    // It's non-compliant but common to leave off the last CRLF.
    private const string OnePartBodyWithoutFinalCRLF =
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"text\"\r\n" +
"\r\n" +
"text default\r\n" +
"--9051914041544843365972754266--";
    private const string TwoPartBody =
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"text\"\r\n" +
"\r\n" +
"text default\r\n" +
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\"\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"Content of a.txt.\r\n" +
"\r\n" +
"--9051914041544843365972754266--\r\n";
    private const string TwoPartBodyWithUnicodeFileName =
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"text\"\r\n" +
"\r\n" +
"text default\r\n" +
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"file1\"; filename=\"a色.txt\"\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"Content of a.txt.\r\n" +
"\r\n" +
"--9051914041544843365972754266--\r\n";
    private const string ThreePartBody =
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"text\"\r\n" +
"\r\n" +
"text default\r\n" +
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\"\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"Content of a.txt.\r\n" +
"\r\n" +
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"file2\"; filename=\"a.html\"\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<!DOCTYPE html><title>Content of a.html.</title>\r\n" +
"\r\n" +
"--9051914041544843365972754266--\r\n";

    private const string TwoPartBodyIncompleteBuffer =
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"text\"\r\n" +
"\r\n" +
"text default\r\n" +
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\"\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"Content of a.txt.\r\n" +
"\r\n" +
"--9051914041544843365";

    private static MemoryStream MakeStream(string text)
    {
        return new MemoryStream(Encoding.UTF8.GetBytes(text));
    }

    private static string GetString(byte[] buffer, int count)
    {
        return Encoding.ASCII.GetString(buffer, 0, count);
    }

    [Fact]
    public async Task MultipartReader_ReadSinglePartBody_Success()
    {
        var stream = MakeStream(OnePartBody);
        var reader = new MultipartReader(Boundary, stream);

        var section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Single(section.Headers);
        Assert.Equal("form-data; name=\"text\"", section.Headers["Content-Disposition"][0]);
        var buffer = new MemoryStream();
        await section.Body.CopyToAsync(buffer);
        Assert.Equal("text default", Encoding.ASCII.GetString(buffer.ToArray()));

        Assert.Null(await reader.ReadNextSectionAsync());
    }

    [Fact]
    public async Task MultipartReader_HeaderCountExceeded_Throws()
    {
        var stream = MakeStream(OnePartBodyTwoHeaders);
        var reader = new MultipartReader(Boundary, stream)
        {
            HeadersCountLimit = 1,
        };

        var exception = await Assert.ThrowsAsync<InvalidDataException>(() => reader.ReadNextSectionAsync());
        Assert.Equal("Multipart headers count limit 1 exceeded.", exception.Message);
    }

    [Fact]
    public async Task MultipartReader_HeadersLengthExceeded_Throws()
    {
        var stream = MakeStream(OnePartBodyTwoHeaders);
        var reader = new MultipartReader(Boundary, stream)
        {
            HeadersLengthLimit = 60,
        };

        var exception = await Assert.ThrowsAsync<InvalidDataException>(() => reader.ReadNextSectionAsync());
        Assert.Equal("Line length limit 17 exceeded.", exception.Message);
    }

    [Fact]
    public async Task MultipartReader_ReadSinglePartBodyWithTrailingWhitespace_Success()
    {
        var stream = MakeStream(OnePartBodyWithTrailingWhitespace);
        var reader = new MultipartReader(Boundary, stream);

        var section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Single(section.Headers);
        Assert.Equal("form-data; name=\"text\"", section.Headers["Content-Disposition"][0]);
        var buffer = new MemoryStream();
        await section.Body.CopyToAsync(buffer);
        Assert.Equal("text default", Encoding.ASCII.GetString(buffer.ToArray()));

        Assert.Null(await reader.ReadNextSectionAsync());
    }

    [Fact]
    public async Task MultipartReader_ReadSinglePartBodyWithoutLastCRLF_Success()
    {
        var stream = MakeStream(OnePartBodyWithoutFinalCRLF);
        var reader = new MultipartReader(Boundary, stream);

        var section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Single(section.Headers);
        Assert.Equal("form-data; name=\"text\"", section.Headers["Content-Disposition"][0]);
        var buffer = new MemoryStream();
        await section.Body.CopyToAsync(buffer);
        Assert.Equal("text default", Encoding.ASCII.GetString(buffer.ToArray()));

        Assert.Null(await reader.ReadNextSectionAsync());
    }

    [Fact]
    public async Task MultipartReader_ReadTwoPartBody_Success()
    {
        var stream = MakeStream(TwoPartBody);
        var reader = new MultipartReader(Boundary, stream);

        var section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Single(section.Headers);
        Assert.Equal("form-data; name=\"text\"", section.Headers["Content-Disposition"][0]);
        var buffer = new MemoryStream();
        await section.Body.CopyToAsync(buffer);
        Assert.Equal("text default", Encoding.ASCII.GetString(buffer.ToArray()));

        section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Equal(2, section.Headers.Count);
        Assert.Equal("form-data; name=\"file1\"; filename=\"a.txt\"", section.Headers["Content-Disposition"][0]);
        Assert.Equal("text/plain", section.Headers["Content-Type"][0]);
        buffer = new MemoryStream();
        await section.Body.CopyToAsync(buffer);
        Assert.Equal("Content of a.txt.\r\n", Encoding.ASCII.GetString(buffer.ToArray()));

        Assert.Null(await reader.ReadNextSectionAsync());
    }

    [Fact]
    public async Task MultipartReader_ReadTwoPartBodyWithUnicodeFileName_Success()
    {
        var stream = MakeStream(TwoPartBodyWithUnicodeFileName);
        var reader = new MultipartReader(Boundary, stream);

        var section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Single(section.Headers);
        Assert.Equal("form-data; name=\"text\"", section.Headers["Content-Disposition"][0]);
        var buffer = new MemoryStream();
        await section.Body.CopyToAsync(buffer);
        Assert.Equal("text default", Encoding.ASCII.GetString(buffer.ToArray()));

        section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Equal(2, section.Headers.Count);
        Assert.Equal("form-data; name=\"file1\"; filename=\"a色.txt\"", section.Headers["Content-Disposition"][0]);
        Assert.Equal("text/plain", section.Headers["Content-Type"][0]);
        buffer = new MemoryStream();
        await section.Body.CopyToAsync(buffer);
        Assert.Equal("Content of a.txt.\r\n", Encoding.ASCII.GetString(buffer.ToArray()));

        Assert.Null(await reader.ReadNextSectionAsync());
    }

    [Fact]
    public async Task MultipartReader_ThreePartBody_Success()
    {
        var stream = MakeStream(ThreePartBody);
        var reader = new MultipartReader(Boundary, stream);

        var section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Single(section.Headers);
        Assert.Equal("form-data; name=\"text\"", section.Headers["Content-Disposition"][0]);
        var buffer = new MemoryStream();
        await section.Body.CopyToAsync(buffer);
        Assert.Equal("text default", Encoding.ASCII.GetString(buffer.ToArray()));

        section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Equal(2, section.Headers.Count);
        Assert.Equal("form-data; name=\"file1\"; filename=\"a.txt\"", section.Headers["Content-Disposition"][0]);
        Assert.Equal("text/plain", section.Headers["Content-Type"][0]);
        buffer = new MemoryStream();
        await section.Body.CopyToAsync(buffer);
        Assert.Equal("Content of a.txt.\r\n", Encoding.ASCII.GetString(buffer.ToArray()));

        section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Equal(2, section.Headers.Count);
        Assert.Equal("form-data; name=\"file2\"; filename=\"a.html\"", section.Headers["Content-Disposition"][0]);
        Assert.Equal("text/html", section.Headers["Content-Type"][0]);
        buffer = new MemoryStream();
        await section.Body.CopyToAsync(buffer);
        Assert.Equal("<!DOCTYPE html><title>Content of a.html.</title>\r\n", Encoding.ASCII.GetString(buffer.ToArray()));

        Assert.Null(await reader.ReadNextSectionAsync());
    }

    [Fact]
    public void MultipartReader_BufferSizeMustBeLargerThanBoundary_Throws()
    {
        var stream = MakeStream(ThreePartBody);
        Assert.Throws<ArgumentOutOfRangeException>(() =>
        {
            var reader = new MultipartReader(Boundary, stream, 5);
        });
    }

    [Fact]
    public async Task MultipartReader_TwoPartBodyIncompleteBuffer_TwoSectionsReadSuccessfullyThirdSectionThrows()
    {
        var stream = MakeStream(TwoPartBodyIncompleteBuffer);
        var reader = new MultipartReader(Boundary, stream);
        var buffer = new byte[128];

        //first section can be read successfully
        var section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Single(section.Headers);
        Assert.Equal("form-data; name=\"text\"", section.Headers["Content-Disposition"][0]);
        var read = section.Body.Read(buffer, 0, buffer.Length);
        Assert.Equal("text default", GetString(buffer, read));

        //second section can be read successfully (even though the bottom boundary is truncated)
        section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Equal(2, section.Headers.Count);
        Assert.Equal("form-data; name=\"file1\"; filename=\"a.txt\"", section.Headers["Content-Disposition"][0]);
        Assert.Equal("text/plain", section.Headers["Content-Type"][0]);
        read = section.Body.Read(buffer, 0, buffer.Length);
        Assert.Equal("Content of a.txt.\r\n", GetString(buffer, read));

        await Assert.ThrowsAsync<IOException>(async () =>
        {
                // we'll be unable to ensure enough bytes are buffered to even contain a final boundary
                section = await reader.ReadNextSectionAsync();
        });
    }

    [Fact]
    public async Task MultipartReader_ReadInvalidUtf8Header_ReplacementCharacters()
    {
        var body1 =
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"text\" filename=\"a";

        var body2 =
".txt\"\r\n" +
"\r\n" +
"text default\r\n" +
"--9051914041544843365972754266--\r\n";
        var stream = new MemoryStream();
        var bytes = Encoding.UTF8.GetBytes(body1);
        stream.Write(bytes, 0, bytes.Length);

        // Write an invalid utf-8 segment in the middle
        stream.Write(new byte[] { 0xC1, 0x21 }, 0, 2);

        bytes = Encoding.UTF8.GetBytes(body2);
        stream.Write(bytes, 0, bytes.Length);
        stream.Seek(0, SeekOrigin.Begin);
        var reader = new MultipartReader(Boundary, stream);

        var section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Single(section.Headers);
        Assert.Equal("form-data; name=\"text\" filename=\"a\uFFFD!.txt\"", section.Headers["Content-Disposition"][0]);
        var buffer = new MemoryStream();
        await section.Body.CopyToAsync(buffer);
        Assert.Equal("text default", Encoding.ASCII.GetString(buffer.ToArray()));

        Assert.Null(await reader.ReadNextSectionAsync());
    }

    [Fact]
    public async Task MultipartReader_ReadInvalidUtf8SurrogateHeader_ReplacementCharacters()
    {
        var body1 =
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"text\" filename=\"a";

        var body2 =
".txt\"\r\n" +
"\r\n" +
"text default\r\n" +
"--9051914041544843365972754266--\r\n";
        var stream = new MemoryStream();
        var bytes = Encoding.UTF8.GetBytes(body1);
        stream.Write(bytes, 0, bytes.Length);

        // Write an invalid utf-8 segment in the middle
        stream.Write(new byte[] { 0xED, 0xA0, 85 }, 0, 3);

        bytes = Encoding.UTF8.GetBytes(body2);
        stream.Write(bytes, 0, bytes.Length);
        stream.Seek(0, SeekOrigin.Begin);
        var reader = new MultipartReader(Boundary, stream);

        var section = await reader.ReadNextSectionAsync();
        Assert.NotNull(section);
        Assert.Single(section.Headers);
        Assert.Equal("form-data; name=\"text\" filename=\"a\uFFFD\uFFFDU.txt\"", section.Headers["Content-Disposition"][0]);
        var buffer = new MemoryStream();
        await section.Body.CopyToAsync(buffer);
        Assert.Equal("text default", Encoding.ASCII.GetString(buffer.ToArray()));

        Assert.Null(await reader.ReadNextSectionAsync());
    }
}