-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy.cs
391 lines (338 loc) · 15.7 KB
/
Copy.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
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
385
386
387
388
389
390
391
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using Xunit;
namespace System.IO.Tests
{
public partial class File_Copy_str_str : FileSystemTest
{
protected virtual void Copy(string source, string dest)
{
File.Copy(source, dest);
}
#region UniversalTests
[Fact]
public void NullFileName()
{
Assert.Throws<ArgumentNullException>(() => Copy(null, "."));
Assert.Throws<ArgumentNullException>(() => Copy(".", null));
}
[Fact]
public void EmptyFileName()
{
Assert.Throws<ArgumentException>(() => Copy(string.Empty, "."));
Assert.Throws<ArgumentException>(() => Copy(".", string.Empty));
}
[Fact]
public void CopyOntoDirectory()
{
string testFile = GetTestFilePath();
string targetTestDirectory = Directory.CreateDirectory(GetTestFilePath()).FullName;
File.Create(testFile).Dispose();
Assert.Throws<IOException>(() => Copy(testFile, targetTestDirectory));
}
[Fact]
public void CopyOntoSelf()
{
string testFile = GetTestFilePath();
File.Create(testFile).Dispose();
Assert.Throws<IOException>(() => Copy(testFile, testFile));
}
[Fact]
public void NonExistentPath()
{
FileInfo testFile = new FileInfo(GetTestFilePath());
testFile.Create().Dispose();
Assert.Throws<FileNotFoundException>(() => Copy(GetTestFilePath(), testFile.FullName));
Assert.Throws<DirectoryNotFoundException>(() => Copy(testFile.FullName, Path.Combine(TestDirectory, GetTestFileName(), GetTestFileName())));
Assert.Throws<DirectoryNotFoundException>(() => Copy(Path.Combine(TestDirectory, GetTestFileName(), GetTestFileName()), testFile.FullName));
}
[Fact]
public void CopyValid()
{
string testFileSource = GetTestFilePath();
string testFileDest = GetTestFilePath();
File.Create(testFileSource).Dispose();
Copy(testFileSource, testFileDest);
Assert.True(File.Exists(testFileDest));
Assert.True(File.Exists(testFileSource));
}
[Fact]
public void ShortenLongPath()
{
string testFileSource = GetTestFilePath();
string testFileDest = Path.GetDirectoryName(testFileSource) + string.Concat(Enumerable.Repeat(Path.DirectorySeparatorChar + ".", 90).ToArray()) + Path.DirectorySeparatorChar + Path.GetFileName(testFileSource);
File.Create(testFileSource).Dispose();
Assert.Throws<IOException>(() => Copy(testFileSource, testFileDest));
}
[Fact]
public void InvalidFileNames()
{
string testFile = GetTestFilePath();
File.Create(testFile).Dispose();
Assert.Throws<ArgumentException>(() => Copy(testFile, "\0"));
Assert.Throws<ArgumentException>(() => Copy(testFile, "*\0*"));
Assert.Throws<ArgumentException>(() => Copy("*\0*", testFile));
Assert.Throws<ArgumentException>(() => Copy("\0", testFile));
}
public static IEnumerable<object[]> CopyFileWithData_MemberData()
{
var rand = new Random();
foreach (bool readOnly in new[] { true, false })
{
foreach (int length in new[] { 0, 1, 3, 4096, 1024 * 80, 1024 * 1024 * 10 })
{
char[] data = new char[length];
for (int i = 0; i < data.Length; i++)
{
data[i] = (char)rand.Next(0, 256);
}
yield return new object[] { data, readOnly};
}
}
}
[Theory]
[MemberData(nameof(CopyFileWithData_MemberData))]
public void CopyFileWithData(char[] data, bool readOnly)
{
string testFileSource = GetTestFilePath();
string testFileDest = GetTestFilePath();
// Write and copy file
using (StreamWriter stream = new StreamWriter(File.Create(testFileSource)))
{
stream.Write(data, 0, data.Length);
}
// Set the last write time of the source file to something a while ago
DateTime lastWriteTime = DateTime.UtcNow.Subtract(TimeSpan.FromHours(1));
File.SetLastWriteTime(testFileSource, lastWriteTime);
if (readOnly)
{
File.SetAttributes(testFileSource, FileAttributes.ReadOnly);
}
// Copy over the data
Copy(testFileSource, testFileDest);
// Ensure copy transferred written data
using (StreamReader stream = new StreamReader(File.OpenRead(testFileDest)))
{
char[] readData = new char[data.Length];
stream.Read(readData, 0, data.Length);
AssertExtensions.Equal(data, readData);
}
// Ensure last write/access time on the new file is appropriate
//
// For browser, there is technically only 1 time. It's the max
// of LastWrite and LastAccess. On browser, File.SetLastWriteTime
// overwrites LastWrite and LastAccess, and File.Copy
// overwrites LastWrite , so this check doesn't apply.
if (PlatformDetection.IsNotBrowser)
{
Assert.InRange(File.GetLastWriteTimeUtc(testFileDest), lastWriteTime.AddSeconds(-1), lastWriteTime.AddSeconds(1));
}
Assert.Equal(readOnly, (File.GetAttributes(testFileDest) & FileAttributes.ReadOnly) != 0);
if (readOnly)
{
File.SetAttributes(testFileSource, FileAttributes.Normal);
File.SetAttributes(testFileDest, FileAttributes.Normal);
}
}
#endregion
#region PlatformSpecific
[Theory,
InlineData(" "),
InlineData(" ")]
[PlatformSpecific(TestPlatforms.Windows)]
public void WindowsAllSpacePath(string invalid)
{
string testFile = GetTestFilePath();
File.Create(testFile).Dispose();
Assert.Throws<ArgumentException>(() => Copy(testFile, invalid));
Assert.Throws<ArgumentException>(() => Copy(invalid, testFile));
}
[Theory,
InlineData("\n"),
InlineData(">"),
InlineData("<"),
InlineData("\t")]
[PlatformSpecific(TestPlatforms.Windows)]
public void WindowsInvalidCharsPath_Core(string invalid)
{
string testFile = GetTestFilePath();
File.Create(testFile).Dispose();
Assert.Throws<IOException>(() => Copy(testFile, invalid));
Assert.Throws<IOException>(() => Copy(invalid, testFile));
}
[Theory,
InlineData(" "),
InlineData(" "),
InlineData("\n"),
InlineData(">"),
InlineData("<"),
InlineData("\t")]
[PlatformSpecific(TestPlatforms.AnyUnix)]
public void UnixInvalidWindowsPaths(string valid)
{
// Unix allows whitespaces paths that aren't valid on Windows
string testFile = GetTestFilePath();
File.Create(testFile).Dispose();
Copy(testFile, Path.Combine(TestDirectory, valid));
Assert.True(File.Exists(testFile));
Assert.True(File.Exists(Path.Combine(TestDirectory, valid)));
}
[Theory,
InlineData("", ":bar"),
InlineData("", ":bar:$DATA"),
InlineData("::$DATA", ":bar"),
InlineData("::$DATA", ":bar:$DATA")]
[PlatformSpecific(TestPlatforms.Windows)]
public void WindowsAlternateDataStream(string defaultStream, string alternateStream)
{
DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath());
string testFile = Path.Combine(testDirectory.FullName, GetTestFileName());
string testFileDefaultStream = testFile + defaultStream;
string testFileAlternateStream = testFile + alternateStream;
// Copy the default stream into an alternate stream
File.WriteAllText(testFileDefaultStream, "Foo");
Copy(testFileDefaultStream, testFileAlternateStream);
Assert.Equal(testFile, testDirectory.GetFiles().Single().FullName);
Assert.Equal("Foo", File.ReadAllText(testFileDefaultStream));
Assert.Equal("Foo", File.ReadAllText(testFileAlternateStream));
// Copy another file over the alternate stream
string testFile2 = Path.Combine(testDirectory.FullName, GetTestFileName());
string testFile2DefaultStream = testFile2 + defaultStream;
File.WriteAllText(testFile2DefaultStream, "Bar");
Assert.Throws<IOException>(() => Copy(testFile2DefaultStream, testFileAlternateStream));
// This always throws as you can't copy an alternate stream out (oddly)
Assert.Throws<IOException>(() => Copy(testFileAlternateStream, testFile2));
Assert.Throws<IOException>(() => Copy(testFileAlternateStream, testFile2 + alternateStream));
}
[Theory]
[PlatformSpecific(TestPlatforms.Linux)]
[InlineData("/proc/cmdline")]
[InlineData("/proc/version")]
[InlineData("/proc/filesystems")]
public void Linux_CopyFromProcfsToFile(string path)
{
string testFile = GetTestFilePath();
File.Copy(path, testFile);
Assert.Equal(File.ReadAllText(path), File.ReadAllText(testFile)); // assumes chosen files won't change between reads
}
#endregion
}
public class File_Copy_str_str_b : File_Copy_str_str
{
protected override void Copy(string source, string dest)
{
File.Copy(source, dest, false);
}
protected virtual void Copy(string source, string dest, bool overwrite)
{
File.Copy(source, dest, overwrite);
}
[Fact]
public void OverwriteTrue()
{
string testFileSource = GetTestFilePath();
string testFileDest = GetTestFilePath();
char[] sourceData = { 'a', 'A', 'b' };
char[] destData = { 'x', 'X', 'y' };
// Write and copy file
using (StreamWriter sourceStream = new StreamWriter(File.Create(testFileSource)))
using (StreamWriter destStream = new StreamWriter(File.Create(testFileDest)))
{
sourceStream.Write(sourceData, 0, sourceData.Length);
destStream.Write(destData, 0, destData.Length);
}
Copy(testFileSource, testFileDest, true);
// Ensure copy transferred written data
using (StreamReader stream = new StreamReader(File.OpenRead(testFileDest)))
{
char[] readData = new char[sourceData.Length];
stream.Read(readData, 0, sourceData.Length);
AssertExtensions.Equal(sourceData, readData);
}
}
[Fact]
public void OverwriteFalse()
{
string testFileSource = GetTestFilePath();
string testFileDest = GetTestFilePath();
char[] sourceData = { 'a', 'A', 'b' };
char[] destData = { 'x', 'X', 'y' };
// Write and copy file
using (StreamWriter sourceStream = new StreamWriter(File.Create(testFileSource)))
using (StreamWriter destStream = new StreamWriter(File.Create(testFileDest)))
{
sourceStream.Write(sourceData, 0, sourceData.Length);
destStream.Write(destData, 0, destData.Length);
}
Assert.Throws<IOException>(() => Copy(testFileSource, testFileDest, false));
// Ensure copy didn't overwrite existing data
using (StreamReader stream = new StreamReader(File.OpenRead(testFileDest)))
{
char[] readData = new char[sourceData.Length];
stream.Read(readData, 0, sourceData.Length);
AssertExtensions.Equal(destData, readData);
}
}
[Theory,
InlineData("", ":bar"),
InlineData("", ":bar:$DATA"),
InlineData("::$DATA", ":bar"),
InlineData("::$DATA", ":bar:$DATA")]
[PlatformSpecific(TestPlatforms.Windows)]
public void WindowsAlternateDataStreamOverwrite(string defaultStream, string alternateStream)
{
DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath());
string testFile = Path.Combine(testDirectory.FullName, GetTestFileName());
string testFileDefaultStream = testFile + defaultStream;
string testFileAlternateStream = testFile + alternateStream;
// Copy the default stream into an alternate stream
File.WriteAllText(testFileDefaultStream, "Foo");
Copy(testFileDefaultStream, testFileAlternateStream);
Assert.Equal(testFile, testDirectory.GetFiles().Single().FullName);
Assert.Equal("Foo", File.ReadAllText(testFileDefaultStream));
Assert.Equal("Foo", File.ReadAllText(testFileAlternateStream));
// Copy another file over the alternate stream
string testFile2 = Path.Combine(testDirectory.FullName, GetTestFileName());
string testFile2DefaultStream = testFile2 + defaultStream;
File.WriteAllText(testFile2DefaultStream, "Bar");
Copy(testFile2DefaultStream, testFileAlternateStream, overwrite: true);
Assert.Equal("Foo", File.ReadAllText(testFileDefaultStream));
Assert.Equal("Bar", File.ReadAllText(testFileAlternateStream));
// This always throws as you can't copy an alternate stream out (oddly)
Assert.Throws<IOException>(() => Copy(testFileAlternateStream, testFile2, overwrite: true));
Assert.Throws<IOException>(() => Copy(testFileAlternateStream, testFile2 + alternateStream, overwrite: true));
}
[Fact]
public void DestinationFileIsTruncatedWhenItsLargerThanSourceFile()
{
string sourcePath = GetTestFilePath();
string destPath = GetTestFilePath();
byte[] content = RandomNumberGenerator.GetBytes(1000);
File.WriteAllBytes(sourcePath, content);
File.WriteAllBytes(destPath, RandomNumberGenerator.GetBytes(content.Length * 2));
Copy(sourcePath, destPath, overwrite: true);
Assert.Equal(content, File.ReadAllBytes(destPath));
}
}
/// <summary>
/// Single tests that shouldn't be duplicated by inheritance.
/// </summary>
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsFileLockingEnabled))]
public sealed class File_Copy_Single : FileSystemTest
{
[Fact]
public void EnsureThrowWhenCopyToNonSharedFile()
{
DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath());
string file1 = Path.Combine(testDirectory.FullName, GetTestFileName());
string file2 = Path.Combine(testDirectory.FullName, GetTestFileName());
File.WriteAllText(file1, "foo");
File.WriteAllText(file2, "bar");
using var stream = new FileStream(file1, FileMode.Open, FileAccess.Read, FileShare.None);
Assert.Throws<IOException>(() => File.Copy(file2, file1, overwrite: true));
}
}
}