Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use FileOptions.DeleteOnClose to ensure that each test removes it's o…
…wn file

use single large file to test File.ReadAllBytes and ile.ReadAllBytesAsync for both limits
  • Loading branch information
adamsitnik committed Dec 9, 2021
commit 1fb35d55cd1701c2621abbb622225e65627f71d1
54 changes: 8 additions & 46 deletions src/libraries/System.IO.FileSystem/tests/LargeFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,17 @@ namespace System.IO.FileSystem.Tests
public class LargeFileTests : FileSystemTest
{
[Fact]
public void ReadFileOver2GB()
public async Task ReadAllBytesOverLimit()
{
string path = GetTestFilePath();
using (FileStream fs = File.Create(path))
{
fs.SetLength(int.MaxValue + 1L);
}

// File is too large for ReadAllBytes at once
Assert.Throws<IOException>(() => File.ReadAllBytes(path));
}

[Fact]
public void ReadFileOverMaxArrayLength()
{
string path = GetTestFilePath();
using (FileStream fs = File.Create(path))
{
fs.SetLength(Array.MaxLength + 1L);
}

// File is too large for ReadAllBytes at once
Assert.Throws<IOException>(() => File.ReadAllBytes(path));
}
using FileStream fs = new (GetTestFilePath(), FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.DeleteOnClose);

[Fact]
public async Task ReadFileOver2GBAsync()
{
string path = GetTestFilePath();
using (FileStream fs = File.Create(path))
foreach (long lengthOverLimit in new long[] { Array.MaxLength + 1L, int.MaxValue + 1L })
{
fs.SetLength(int.MaxValue + 1L);
}

// File is too large for ReadAllBytesAsync at once
await Assert.ThrowsAsync<IOException>(async () => await File.ReadAllBytesAsync(path));
}
fs.SetLength(lengthOverLimit);

[Fact]
public async Task ReadFileOverMaxArrayLengthAsync()
{
string path = GetTestFilePath();
using (FileStream fs = File.Create(path))
{
fs.SetLength(Array.MaxLength + 1L);
Assert.Throws<IOException>(() => File.ReadAllBytes(fs.Name));
await Assert.ThrowsAsync<IOException>(async () => await File.ReadAllBytesAsync(fs.Name));
}

// File is too large for ReadAllBytesAsync at once
await Assert.ThrowsAsync<IOException>(async () => await File.ReadAllBytesAsync(path));
}

[Fact]
Expand All @@ -75,7 +37,7 @@ public void NoInt32OverflowInTheBufferingLogic()
byte[] data2 = new byte[] { 6, 7, 8, 9, 10 };
byte[] buffer = new byte[5];

using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
using (FileStream stream = File.Create(filePath))
{
stream.Seek(position1, SeekOrigin.Begin);
stream.Write(data1);
Expand All @@ -84,7 +46,7 @@ public void NoInt32OverflowInTheBufferingLogic()
stream.Write(data2);
}

using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (FileStream stream = new (filePath, FileMode.Open, FileAccess.Read, FileShare.None, 4096, FileOptions.DeleteOnClose))
{
stream.Seek(position1, SeekOrigin.Begin);
Assert.Equal(buffer.Length, stream.Read(buffer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public void NoInt32OverflowInTheBufferingLogic()
stream.Write(data2);
}

using (var stream = new BufferedStream(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize: 0)))
using (var stream = new BufferedStream(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize: 0, FileOptions.DeleteOnClose)))
{
stream.Seek(position1, SeekOrigin.Begin);
Assert.Equal(buffer.Length, stream.Read(buffer));
Expand Down