更新
更旧
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Buffers;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing;
using Moq;
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
using Xunit;
namespace Microsoft.AspNetCore.WebUtilities
{
public class FileBufferingReadStreamTests
{
private Stream MakeStream(int size)
{
// TODO: Fill with random data? Make readonly?
return new MemoryStream(new byte[size]);
}
[Fact]
public void FileBufferingReadStream_Properties_ExpectedValues()
{
var inner = MakeStream(1024 * 2);
using (var stream = new FileBufferingReadStream(inner, 1024, null, Directory.GetCurrentDirectory()))
{
Assert.True(stream.CanRead);
Assert.True(stream.CanSeek);
Assert.False(stream.CanWrite);
Assert.Equal(0, stream.Length); // Nothing buffered yet
Assert.Equal(0, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
}
}
[Fact]
public void FileBufferingReadStream_SyncReadUnderThreshold_DoesntCreateFile()
{
var inner = MakeStream(1024 * 2);
using (var stream = new FileBufferingReadStream(inner, 1024 * 3, null, Directory.GetCurrentDirectory()))
{
var bytes = new byte[1000];
var read0 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read0);
Assert.Equal(read0, stream.Length);
Assert.Equal(read0, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
var read1 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read1);
Assert.Equal(read0 + read1, stream.Length);
Assert.Equal(read0 + read1, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
var read2 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(inner.Length - read0 - read1, read2);
Assert.Equal(read0 + read1 + read2, stream.Length);
Assert.Equal(read0 + read1 + read2, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
var read3 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(0, read3);
}
}
[Fact]
public void FileBufferingReadStream_SyncReadOverThreshold_CreatesFile()
{
var inner = MakeStream(1024 * 2);
string tempFileName;
using (var stream = new FileBufferingReadStream(inner, 1024, null, GetCurrentDirectory()))
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
{
var bytes = new byte[1000];
var read0 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read0);
Assert.Equal(read0, stream.Length);
Assert.Equal(read0, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
var read1 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read1);
Assert.Equal(read0 + read1, stream.Length);
Assert.Equal(read0 + read1, stream.Position);
Assert.False(stream.InMemory);
Assert.NotNull(stream.TempFileName);
tempFileName = stream.TempFileName;
Assert.True(File.Exists(tempFileName));
var read2 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(inner.Length - read0 - read1, read2);
Assert.Equal(read0 + read1 + read2, stream.Length);
Assert.Equal(read0 + read1 + read2, stream.Position);
Assert.False(stream.InMemory);
Assert.NotNull(stream.TempFileName);
Assert.True(File.Exists(tempFileName));
var read3 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(0, read3);
}
Assert.False(File.Exists(tempFileName));
}
[Fact]
public void FileBufferingReadStream_SyncReadWithInMemoryLimit_EnforcesLimit()
{
var inner = MakeStream(1024 * 2);
using (var stream = new FileBufferingReadStream(inner, 1024, 900, Directory.GetCurrentDirectory()))
{
var bytes = new byte[500];
var read0 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read0);
Assert.Equal(read0, stream.Length);
Assert.Equal(read0, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
var exception = Assert.Throws<IOException>(() => stream.Read(bytes, 0, bytes.Length));
Assert.Equal("Buffer limit exceeded.", exception.Message);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
Assert.False(File.Exists(stream.TempFileName));
}
}
[Fact]
public void FileBufferingReadStream_SyncReadWithOnDiskLimit_EnforcesLimit()
{
var inner = MakeStream(1024 * 2);
string tempFileName;
using (var stream = new FileBufferingReadStream(inner, 512, 1024, GetCurrentDirectory()))
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
{
var bytes = new byte[500];
var read0 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read0);
Assert.Equal(read0, stream.Length);
Assert.Equal(read0, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
var read1 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read1);
Assert.Equal(read0 + read1, stream.Length);
Assert.Equal(read0 + read1, stream.Position);
Assert.False(stream.InMemory);
Assert.NotNull(stream.TempFileName);
tempFileName = stream.TempFileName;
Assert.True(File.Exists(tempFileName));
var exception = Assert.Throws<IOException>(() => stream.Read(bytes, 0, bytes.Length));
Assert.Equal("Buffer limit exceeded.", exception.Message);
Assert.False(stream.InMemory);
Assert.NotNull(stream.TempFileName);
Assert.False(File.Exists(tempFileName));
}
Assert.False(File.Exists(tempFileName));
}
///////////////////
[Fact]
public async Task FileBufferingReadStream_AsyncReadUnderThreshold_DoesntCreateFile()
{
var inner = MakeStream(1024 * 2);
using (var stream = new FileBufferingReadStream(inner, 1024 * 3, null, Directory.GetCurrentDirectory()))
{
var bytes = new byte[1000];
var read0 = await stream.ReadAsync(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read0);
Assert.Equal(read0, stream.Length);
Assert.Equal(read0, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
var read1 = await stream.ReadAsync(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read1);
Assert.Equal(read0 + read1, stream.Length);
Assert.Equal(read0 + read1, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
var read2 = await stream.ReadAsync(bytes, 0, bytes.Length);
Assert.Equal(inner.Length - read0 - read1, read2);
Assert.Equal(read0 + read1 + read2, stream.Length);
Assert.Equal(read0 + read1 + read2, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
var read3 = await stream.ReadAsync(bytes, 0, bytes.Length);
Assert.Equal(0, read3);
}
}
[Fact]
public async Task FileBufferingReadStream_AsyncReadOverThreshold_CreatesFile()
{
var inner = MakeStream(1024 * 2);
string tempFileName;
using (var stream = new FileBufferingReadStream(inner, 1024, null, GetCurrentDirectory()))
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
{
var bytes = new byte[1000];
var read0 = await stream.ReadAsync(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read0);
Assert.Equal(read0, stream.Length);
Assert.Equal(read0, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
var read1 = await stream.ReadAsync(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read1);
Assert.Equal(read0 + read1, stream.Length);
Assert.Equal(read0 + read1, stream.Position);
Assert.False(stream.InMemory);
Assert.NotNull(stream.TempFileName);
tempFileName = stream.TempFileName;
Assert.True(File.Exists(tempFileName));
var read2 = await stream.ReadAsync(bytes, 0, bytes.Length);
Assert.Equal(inner.Length - read0 - read1, read2);
Assert.Equal(read0 + read1 + read2, stream.Length);
Assert.Equal(read0 + read1 + read2, stream.Position);
Assert.False(stream.InMemory);
Assert.NotNull(stream.TempFileName);
Assert.True(File.Exists(tempFileName));
var read3 = await stream.ReadAsync(bytes, 0, bytes.Length);
Assert.Equal(0, read3);
}
Assert.False(File.Exists(tempFileName));
}
[Fact]
public async Task FileBufferingReadStream_AsyncReadWithInMemoryLimit_EnforcesLimit()
{
var inner = MakeStream(1024 * 2);
using (var stream = new FileBufferingReadStream(inner, 1024, 900, Directory.GetCurrentDirectory()))
{
var bytes = new byte[500];
var read0 = await stream.ReadAsync(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read0);
Assert.Equal(read0, stream.Length);
Assert.Equal(read0, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
var exception = await Assert.ThrowsAsync<IOException>(() => stream.ReadAsync(bytes, 0, bytes.Length));
Assert.Equal("Buffer limit exceeded.", exception.Message);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
Assert.False(File.Exists(stream.TempFileName));
}
}
[Fact]
public async Task FileBufferingReadStream_AsyncReadWithOnDiskLimit_EnforcesLimit()
{
var inner = MakeStream(1024 * 2);
string tempFileName;
using (var stream = new FileBufferingReadStream(inner, 512, 1024, GetCurrentDirectory()))
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
{
var bytes = new byte[500];
var read0 = await stream.ReadAsync(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read0);
Assert.Equal(read0, stream.Length);
Assert.Equal(read0, stream.Position);
Assert.True(stream.InMemory);
Assert.Null(stream.TempFileName);
var read1 = await stream.ReadAsync(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read1);
Assert.Equal(read0 + read1, stream.Length);
Assert.Equal(read0 + read1, stream.Position);
Assert.False(stream.InMemory);
Assert.NotNull(stream.TempFileName);
tempFileName = stream.TempFileName;
Assert.True(File.Exists(tempFileName));
var exception = await Assert.ThrowsAsync<IOException>(() => stream.ReadAsync(bytes, 0, bytes.Length));
Assert.Equal("Buffer limit exceeded.", exception.Message);
Assert.False(stream.InMemory);
Assert.NotNull(stream.TempFileName);
Assert.False(File.Exists(tempFileName));
}
Assert.False(File.Exists(tempFileName));
}
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
[Fact]
public void FileBufferingReadStream_UsingMemoryStream_RentsAndReturnsRentedBuffer_WhenCopyingFromMemoryStreamDuringRead()
{
var inner = MakeStream(1024 * 1024 + 25);
string tempFileName;
var arrayPool = new Mock<ArrayPool<byte>>();
arrayPool.Setup(p => p.Rent(It.IsAny<int>()))
.Returns((int m) => ArrayPool<byte>.Shared.Rent(m));
arrayPool.Setup(p => p.Return(It.IsAny<byte[]>(), It.IsAny<bool>()))
.Callback((byte[] bytes, bool clear) => ArrayPool<byte>.Shared.Return(bytes, clear));
using (var stream = new FileBufferingReadStream(inner, 1024 * 1024 + 1, 2 * 1024 * 1024, GetCurrentDirectory(), arrayPool.Object))
{
arrayPool.Verify(v => v.Rent(It.IsAny<int>()), Times.Never());
stream.Read(new byte[1024 * 1024]);
Assert.False(File.Exists(stream.TempFileName), "tempFile should not be created as yet");
stream.Read(new byte[4]);
Assert.True(File.Exists(stream.TempFileName), "tempFile should be created");
tempFileName = stream.TempFileName;
arrayPool.Verify(v => v.Rent(It.IsAny<int>()), Times.Once());
arrayPool.Verify(v => v.Return(It.IsAny<byte[]>(), It.IsAny<bool>()), Times.Once());
}
Assert.False(File.Exists(tempFileName));
}
[Fact]
public async Task FileBufferingReadStream_UsingMemoryStream_RentsAndReturnsRentedBuffer_WhenCopyingFromMemoryStreamDuringReadAsync()
{
var inner = MakeStream(1024 * 1024 + 25);
string tempFileName;
var arrayPool = new Mock<ArrayPool<byte>>();
arrayPool.Setup(p => p.Rent(It.IsAny<int>()))
.Returns((int m) => ArrayPool<byte>.Shared.Rent(m));
arrayPool.Setup(p => p.Return(It.IsAny<byte[]>(), It.IsAny<bool>()))
.Callback((byte[] bytes, bool clear) => ArrayPool<byte>.Shared.Return(bytes, clear));
using (var stream = new FileBufferingReadStream(inner, 1024 * 1024 + 1, 2 * 1024 * 1024, GetCurrentDirectory(), arrayPool.Object))
{
arrayPool.Verify(v => v.Rent(It.IsAny<int>()), Times.Never());
await stream.ReadAsync(new byte[1024 * 1024]);
Assert.False(File.Exists(stream.TempFileName), "tempFile should not be created as yet");
await stream.ReadAsync(new byte[4]);
Assert.True(File.Exists(stream.TempFileName), "tempFile should be created");
tempFileName = stream.TempFileName;
arrayPool.Verify(v => v.Rent(It.IsAny<int>()), Times.Once());
arrayPool.Verify(v => v.Return(It.IsAny<byte[]>(), It.IsAny<bool>()), Times.Once());
}
Assert.False(File.Exists(tempFileName));
}
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
385
386
387
388
389
390
391
392
393
394
395
396
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Windows, SkipReason = "UnixFileMode is not supported on Windows.")]
public void Read_BufferingContentToDisk_CreatesFileWithUserOnlyUnixFileMode()
{
var inner = MakeStream(1024 * 2);
string tempFileName;
using (var stream = new FileBufferingReadStream(inner, 1024, null, GetCurrentDirectory()))
{
var bytes = new byte[1024 * 2];
var read0 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(bytes.Length, read0);
Assert.Equal(read0, stream.Length);
Assert.Equal(read0, stream.Position);
Assert.False(stream.InMemory);
Assert.NotNull(stream.TempFileName);
var read1 = stream.Read(bytes, 0, bytes.Length);
Assert.Equal(0, read1);
tempFileName = stream.TempFileName!;
Assert.True(File.Exists(tempFileName));
//Assert.Equal(UnixFileMode.UserRead | UnixFileMode.UserWrite, File.GetUnixFileMode(tempFileName));
var processStartInfo = new ProcessStartInfo
{
FileName = "ls",
Arguments = $"-l {tempFileName}",
RedirectStandardOutput = true,
UseShellExecute = false
};
var process = Process.Start(processStartInfo);
Assert.NotNull(process);
var output = process!.StandardOutput.ReadToEnd();
process.WaitForExit();
Assert.StartsWith("-rw-------", output);
}
Assert.False(File.Exists(tempFileName));
}
private static string GetCurrentDirectory()
{
return AppContext.BaseDirectory;
}