-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathctor_options.cs
More file actions
183 lines (165 loc) · 7.91 KB
/
ctor_options.cs
File metadata and controls
183 lines (165 loc) · 7.91 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Linq;
using System.Security.Cryptography;
using Xunit;
namespace System.IO.Tests
{
// Don't run in parallel as the WhenDiskIsFullTheErrorMessageContainsAllDetails test
// consumes entire available free space on the disk (only on Linux, this is how posix_fallocate works)
// and if we try to run other disk-writing test in the meantime we are going to get "No space left on device" exception.
[Collection(nameof(DisableParallelization))]
public partial class FileStream_ctor_options : FileStream_ctor_str_fm_fa_fs_buffer_fo
{
protected override string GetExpectedParamName(string paramName) => "value";
protected override FileStream CreateFileStream(string path, FileMode mode)
=> new FileStream(path,
new FileStreamOptions
{
Mode = mode,
Access = mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite
});
protected override FileStream CreateFileStream(string path, FileMode mode, FileAccess access)
=> new FileStream(path,
new FileStreamOptions
{
Mode = mode,
Access = access
});
protected override FileStream CreateFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
=> new FileStream(path,
new FileStreamOptions
{
Mode = mode,
Access = access,
Share = share,
BufferSize = bufferSize,
Options = options
});
protected virtual FileStream CreateFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, long preallocationSize)
=> new FileStream(path,
new FileStreamOptions
{
Mode = mode,
Access = access,
Share = share,
BufferSize = bufferSize,
Options = options,
PreallocationSize = preallocationSize
});
protected virtual FileStream CreateFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, long preallocationSize, UnixFileMode unixFileMode)
=> new FileStream(path,
new FileStreamOptions
{
Mode = mode,
Access = access,
Share = share,
BufferSize = bufferSize,
Options = options,
PreallocationSize = preallocationSize,
UnixCreateMode = unixFileMode
});
[Fact]
public virtual void NegativePreallocationSizeThrows()
{
string filePath = GetTestFilePath();
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(
() => CreateFileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None, bufferSize: 1, FileOptions.None, preallocationSize: -1));
}
[Theory]
[InlineData(FileMode.Append)]
[InlineData(FileMode.Open)]
[InlineData(FileMode.OpenOrCreate)]
[InlineData(FileMode.Truncate)]
public void PreallocationSizeThrowsForFileModesThatOpenExistingFiles(FileMode mode)
{
Assert.Throws<ArgumentException>(
() => CreateFileStream(GetTestFilePath(), mode, FileAccess.Write, FileShare.None, bufferSize: 1, FileOptions.None, preallocationSize: 20));
}
[Theory]
[InlineData(FileMode.Create)]
[InlineData(FileMode.CreateNew)]
public void PreallocationSizeThrowsForReadOnlyAccess(FileMode mode)
{
Assert.Throws<ArgumentException>(
() => CreateFileStream(GetTestFilePath(), mode, FileAccess.Read, FileShare.None, bufferSize: 1, FileOptions.None, preallocationSize: 20));
}
[Theory]
[InlineData(FileMode.Create, false)]
[InlineData(FileMode.Create, true)]
[InlineData(FileMode.CreateNew, false)]
[InlineData(FileMode.Append, false)]
[InlineData(FileMode.Append, true)]
[InlineData(FileMode.Open, true)]
[InlineData(FileMode.OpenOrCreate, true)]
[InlineData(FileMode.OpenOrCreate, false)]
[InlineData(FileMode.Truncate, true)]
public void ZeroPreallocationSizeDoesNotAllocate(FileMode mode, bool createFile)
{
string filename = GetTestFilePath();
if (createFile)
{
File.WriteAllText(filename, "");
}
using (FileStream fs = CreateFileStream(filename, mode, FileAccess.Write, FileShare.None, bufferSize: 1, FileOptions.None, preallocationSize: 0))
{
Assert.Equal(0, fs.Length);
if (IsGetAllocatedSizeImplemented)
{
Assert.Equal(0, GetAllocatedSize(fs));
}
Assert.Equal(0, fs.Position);
}
}
[Theory]
[InlineData(FileAccess.Write, FileMode.Create)]
[InlineData(FileAccess.Write, FileMode.CreateNew)]
[InlineData(FileAccess.ReadWrite, FileMode.Create)]
[InlineData(FileAccess.ReadWrite, FileMode.CreateNew)]
public void PreallocationSize(FileAccess access, FileMode mode)
{
const long preallocationSize = 123;
using (var fs = CreateFileStream(GetTestFilePath(), mode, access, FileShare.None, bufferSize: 1, FileOptions.None, preallocationSize))
{
Assert.Equal(0, fs.Length);
if (IsGetAllocatedSizeImplemented)
{
if (SupportsPreallocation)
{
Assert.True(GetAllocatedSize(fs) >= preallocationSize);
}
else
{
Assert.Equal(0, GetAllocatedSize(fs));
}
}
Assert.Equal(0, fs.Position);
}
}
// macOS fcntl doc does not mention ENOSPC error: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fcntl.2.html
// But depending on the OS version, it might actually return it.
// Since we don't want to have unstable tests, it's better to not run it on macOS at all.
[PlatformSpecific(TestPlatforms.Windows | TestPlatforms.Linux)]
[Theory]
[InlineData(FileMode.Create)]
[InlineData(FileMode.CreateNew)]
public void WhenDiskIsFullTheErrorMessageContainsAllDetails(FileMode mode)
{
const long tooMuch = 1024L * 1024L * 1024L * 1024L * 1024L * 1024L; // 1 Exbibyte .. we are assuming this is not available
string filePath = GetTestFilePath();
try
{
// not using Assert.Throws because in the event of failure, we want to dispose, so the next test isn't affected
using FileStream fs = CreateFileStream(filePath, mode, FileAccess.Write, FileShare.None, bufferSize: 1, FileOptions.None, tooMuch);
Assert.Fail($"Expected to throw IOException, {fs.Length}");
}
catch (IOException ex)
{
Assert.Contains(filePath, ex.Message);
Assert.Contains(tooMuch.ToString(), ex.Message);
// ensure it was NOT created
Assert.False(File.Exists(filePath));
}
}
}
}