-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathReadWriteAllBytes.cs
235 lines (211 loc) · 8.68 KB
/
ReadWriteAllBytes.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using System.IO.Pipes;
using Microsoft.DotNet.XUnitExtensions;
namespace System.IO.Tests
{
public class File_ReadWriteAllBytes : FileSystemTest
{
[Fact]
public void NullParameters()
{
string path = GetTestFilePath();
Assert.Throws<ArgumentNullException>(() => File.WriteAllBytes(null, new byte[0]));
Assert.Throws<ArgumentNullException>(() => File.WriteAllBytes(path, null));
Assert.Throws<ArgumentNullException>(() => File.ReadAllBytes(null));
}
[Fact]
public void InvalidParameters()
{
Assert.Throws<ArgumentException>(() => File.WriteAllBytes(string.Empty, new byte[0]));
Assert.Throws<ArgumentException>(() => File.ReadAllBytes(string.Empty));
}
[Fact]
public void Read_FileNotFound()
{
string path = GetTestFilePath();
Assert.Throws<FileNotFoundException>(() => File.ReadAllBytes(path));
}
[Fact]
public void EmptyContentCreatesFile()
{
string path = GetTestFilePath();
File.WriteAllBytes(path, new byte[0]);
Assert.True(File.Exists(path));
Assert.Empty(File.ReadAllText(path));
File.Delete(path);
}
[Theory]
[InlineData(0)]
[InlineData(100)]
public void ValidWrite(int size)
{
string path = GetTestFilePath();
byte[] buffer = Encoding.UTF8.GetBytes(new string('c', size));
File.WriteAllBytes(path, buffer);
Assert.Equal(buffer, File.ReadAllBytes(path));
File.Delete(path);
}
[Fact]
[OuterLoop]
[ActiveIssue("https://github.com/dotnet/runtime/issues/45954", TestPlatforms.Browser)]
public void ReadFileOver2GB()
{
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 Overwrite()
{
string path = GetTestFilePath();
byte[] bytes = Encoding.UTF8.GetBytes(new string('c', 100));
byte[] overwriteBytes = Encoding.UTF8.GetBytes(new string('b', 50));
File.WriteAllBytes(path, bytes);
File.WriteAllBytes(path, overwriteBytes);
Assert.Equal(overwriteBytes, File.ReadAllBytes(path));
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsFileLockingEnabled))]
public void OpenFile_ThrowsIOException()
{
string path = GetTestFilePath();
byte[] bytes = Encoding.UTF8.GetBytes(new string('c', 100));
using (File.Create(path))
{
Assert.Throws<IOException>(() => File.WriteAllBytes(path, bytes));
Assert.Throws<IOException>(() => File.ReadAllBytes(path));
}
}
/// <summary>
/// On Unix, modifying a file that is ReadOnly will fail under normal permissions.
/// If the test is being run under the superuser, however, modification of a ReadOnly
/// file is allowed.
/// </summary>
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/53021", TestPlatforms.Browser)]
public void WriteToReadOnlyFile()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
File.SetAttributes(path, FileAttributes.ReadOnly);
try
{
// Operation succeeds when being run by the Unix superuser
if (PlatformDetection.IsSuperUser)
{
File.WriteAllBytes(path, Encoding.UTF8.GetBytes("text"));
Assert.Equal(Encoding.UTF8.GetBytes("text"), File.ReadAllBytes(path));
}
else
Assert.Throws<UnauthorizedAccessException>(() => File.WriteAllBytes(path, Encoding.UTF8.GetBytes("text")));
}
finally
{
File.SetAttributes(path, FileAttributes.Normal);
}
}
[Fact]
public void EmptyFile_ReturnsEmptyArray()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
Assert.Equal(0, File.ReadAllBytes(path).Length);
}
[Theory]
[PlatformSpecific(TestPlatforms.Linux)]
[InlineData("/proc/cmdline")]
[InlineData("/proc/version")]
[InlineData("/proc/filesystems")]
public void ProcFs_EqualsReadAllText(string path)
{
byte[] bytes = null;
string text = null;
const int NumTries = 3; // some of these could theoretically change between reads, so allow retries just in case
for (int i = 1; i <= NumTries; i++)
{
try
{
bytes = File.ReadAllBytes(path);
text = File.ReadAllText(path);
Assert.Equal(text, Encoding.UTF8.GetString(bytes));
}
catch when (i < NumTries) { }
}
}
[Fact]
[PlatformSpecific(TestPlatforms.Linux)]
public void ReadAllBytes_ProcFs_Uptime_ContainsTwoNumbers()
{
string text = Encoding.UTF8.GetString(File.ReadAllBytes("/proc/uptime"));
string[] parts = text.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
Assert.Equal(2, parts.Length);
Assert.True(double.TryParse(parts[0].Trim(), out _));
Assert.True(double.TryParse(parts[1].Trim(), out _));
}
[Theory]
[PlatformSpecific(TestPlatforms.Linux)]
[InlineData("/proc/meminfo")]
[InlineData("/proc/stat")]
[InlineData("/proc/cpuinfo")]
public void ProcFs_NotEmpty(string path)
{
Assert.InRange(File.ReadAllBytes(path).Length, 1, int.MaxValue);
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // DOS device paths (\\.\ and \\?\) are a Windows concept
public async Task ReadAllBytes_NonSeekableFileStream_InWindows()
{
string pipeName = FileSystemTest.GetNamedPipeServerStreamName();
string pipePath = Path.GetFullPath($@"\\.\pipe\{pipeName}");
var namedPipeWriterStream = new NamedPipeServerStream(pipeName, PipeDirection.Out);
var contentBytes = new byte[] { 1, 2, 3 };
using (var cts = new CancellationTokenSource())
{
Task writingServerTask = WaitConnectionAndWritePipeStreamAsync(namedPipeWriterStream, contentBytes, cts.Token);
Task<byte[]> readTask = Task.Run(() => File.ReadAllBytes(pipePath), cts.Token);
cts.CancelAfter(TimeSpan.FromSeconds(50));
await writingServerTask;
byte[] readBytes = await readTask;
Assert.Equal<byte>(contentBytes, readBytes);
}
static async Task WaitConnectionAndWritePipeStreamAsync(NamedPipeServerStream namedPipeWriterStream, byte[] contentBytes, CancellationToken cancellationToken)
{
await using (namedPipeWriterStream)
{
await namedPipeWriterStream.WaitForConnectionAsync(cancellationToken);
await namedPipeWriterStream.WriteAsync(contentBytes, cancellationToken);
}
}
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)]
public async Task ReadAllBytes_NonSeekableFileStream_InUnix()
{
string fifoPath = GetTestFilePath();
Assert.Equal(0, mkfifo(fifoPath, 438 /* 666 in octal */ ));
var contentBytes = new byte[] { 1, 2, 3 };
await Task.WhenAll(
Task.Run(() =>
{
byte[] readBytes = File.ReadAllBytes(fifoPath);
Assert.Equal<byte>(contentBytes, readBytes);
}),
Task.Run(() =>
{
using var fs = new FileStream(fifoPath, FileMode.Open, FileAccess.Write, FileShare.Read);
foreach (byte content in contentBytes)
{
fs.WriteByte(content);
}
}));
}
}
}