-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathFileSystemTest.cs
More file actions
200 lines (172 loc) · 8.59 KB
/
Copy pathFileSystemTest.cs
File metadata and controls
200 lines (172 loc) · 8.59 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// 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.Diagnostics;
using Xunit;
namespace System.IO.Tests
{
public abstract partial class FileSystemTest : FileCleanupTestBase
{
public static readonly byte[] TestBuffer = { 0xBA, 0x5E, 0xBA, 0x11, 0xF0, 0x07, 0xBA, 0x11 };
public static bool ReservedDeviceNamesAreBlocked => PlatformDetection.IsWindows && !PlatformDetection.IsWindows10OrLater;
public static bool IsAsyncIoSupportedForRegularFiles => PlatformDetection.IsWindows;
private static readonly Lazy<string> s_tempDriveFormat = new Lazy<string>(() =>
PlatformDetection.IsInAppContainer ? string.Empty : new DriveInfo(Path.GetTempPath()).DriveFormat);
private static readonly Lazy<bool> s_supportsAlternateDataStreams =
new Lazy<bool>(GetSupportsAlternateDataStreams);
public static string TempDriveFormat => s_tempDriveFormat.Value;
public static bool IsTempPathOnFat32 =>
string.Equals(TempDriveFormat, "FAT32", StringComparison.OrdinalIgnoreCase);
public static bool TempPathSupportsLargeFiles => !IsTempPathOnFat32;
public static bool DeletesOpenFileNameImmediately =>
PlatformDetection.IsWindows10Version1903OrGreater && !IsTempPathOnFat32;
public static bool SupportsAlternateDataStreams => s_supportsAlternateDataStreams.Value;
private static bool GetSupportsAlternateDataStreams()
{
if (!PlatformDetection.IsWindows)
{
return false;
}
string path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
string streamPath = path + ":stream";
try
{
File.WriteAllText(path, string.Empty);
File.WriteAllText(streamPath, "stream");
return File.ReadAllText(streamPath) == "stream";
}
catch (Exception ex) when (ex is IOException || ex is NotSupportedException || ex is UnauthorizedAccessException)
{
return false;
}
finally
{
try { File.Delete(path); } catch { }
}
}
public static TheoryData<string> PathsWithInvalidColons = TestData.PathsWithInvalidColons;
public static TheoryData<string> PathsWithInvalidCharacters = TestData.PathsWithInvalidCharacters;
public static TheoryData<char> TrailingCharacters = TestData.TrailingCharacters;
public static TheoryData<string> ValidPathComponentNames = IOInputs.GetValidPathComponentNames().ToTheoryData();
public static TheoryData<string> SimpleWhiteSpace = IOInputs.GetSimpleWhiteSpace().ToTheoryData();
public static TheoryData<string> WhiteSpace = IOInputs.GetWhiteSpace().ToTheoryData();
public static TheoryData<string> UncPathsWithoutShareName = IOInputs.GetUncPathsWithoutShareName().ToTheoryData();
public static TheoryData<string> PathsWithReservedDeviceNames = IOInputs.GetPathsWithReservedDeviceNames().ToTheoryData();
public static TheoryData<string> PathsWithColons = IOInputs.GetPathsWithColons().ToTheoryData();
public static TheoryData<string> PathsWithComponentLongerThanMaxComponent = IOInputs.GetPathsWithComponentLongerThanMaxComponent().ToTheoryData();
public static TheoryData<string> ControlWhiteSpace = IOInputs.GetControlWhiteSpace().ToTheoryData();
public static TheoryData<string> NonControlWhiteSpace = IOInputs.GetNonControlWhiteSpace().ToTheoryData();
public static TheoryData<string> TrailingSeparators
{
get
{
var data = new TheoryData<string>()
{
"",
"" + Path.DirectorySeparatorChar,
"" + Path.DirectorySeparatorChar + Path.DirectorySeparatorChar
};
if (PlatformDetection.IsWindows)
{
data.Add("" + Path.AltDirectorySeparatorChar);
}
return data;
}
}
/// <summary>
/// Do a test action against read only file system (for Unix).
/// </summary>
/// <param name="testAction">Test action to perform. The string argument will be read only directory.</param>
/// <param name="subDirectoryName">Optional subdirectory to create.</param>
protected void ReadOnly_FileSystemHelper(Action<string> testAction, string subDirectoryName = null)
{
// Set up read only file system
// Set up the source directory
string sourceDirectory = GetTestFilePath();
if (subDirectoryName == null)
{
Directory.CreateDirectory(sourceDirectory);
}
else
{
string sourceSubDirectory = Path.Combine(sourceDirectory, subDirectoryName);
Directory.CreateDirectory(sourceSubDirectory);
}
// Set up the target directory and mount as a read only
string readOnlyDirectory = GetTestFilePath();
Directory.CreateDirectory(readOnlyDirectory);
Assert.Equal(0, AdminHelpers.RunAsSudo($"mount --bind {sourceDirectory} {readOnlyDirectory}"));
try
{
Assert.Equal(0, AdminHelpers.RunAsSudo($"mount -o remount,ro,bind {sourceDirectory} {readOnlyDirectory}"));
testAction(readOnlyDirectory);
}
finally
{
// Clean up test environment
Assert.Equal(0, AdminHelpers.RunAsSudo($"umount {readOnlyDirectory}"));
}
}
/// <summary>
/// Determines whether the file system is case sensitive by creating a file in the specified folder and observing the result.
/// </summary>
/// <remarks>
/// Ideally we'd use something like pathconf with _PC_CASE_SENSITIVE, but that is non-portable,
/// not supported on Windows or Linux, etc. For now, this function creates a tmp file with capital letters
/// and then tests for its existence with lower-case letters. This could return invalid results in corner
/// cases where, for example, different file systems are mounted with differing sensitivities.
/// </remarks>
protected static bool GetIsCaseSensitiveByProbing(string probingDirectory)
{
string pathWithUpperCase = Path.Combine(probingDirectory, $"CASESENSITIVETEST{Guid.NewGuid():N}");
using (new FileStream(pathWithUpperCase, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, 0x1000, FileOptions.DeleteOnClose))
{
string lowerCased = pathWithUpperCase.ToLowerInvariant();
return !File.Exists(lowerCased);
}
}
protected const UnixFileMode AllAccess =
UnixFileMode.UserRead |
UnixFileMode.UserWrite |
UnixFileMode.UserExecute |
UnixFileMode.GroupRead |
UnixFileMode.GroupWrite |
UnixFileMode.GroupExecute |
UnixFileMode.OtherRead |
UnixFileMode.OtherWrite |
UnixFileMode.OtherExecute;
public static IEnumerable<object[]> TestUnixFileModes
{
get
{
// Make combinations of the enum with 0, 1 and 2 bits set.
UnixFileMode[] modes = Enum.GetValues<UnixFileMode>();
for (int i = 0; i < modes.Length; i++)
{
for (int j = i; j < modes.Length; j++)
{
yield return new object[] { modes[i] | modes[j] };
}
}
}
}
private static UnixFileMode s_umask = (UnixFileMode)(-1);
protected static UnixFileMode GetUmask()
{
if (s_umask == (UnixFileMode)(-1))
{
// The umask can't be retrieved without changing it.
// We launch a child process to get its value.
using Process px = Process.Start(new ProcessStartInfo
{
FileName = "/bin/sh",
ArgumentList = { "-c", "umask" },
RedirectStandardOutput = true
});
string stdout = px.StandardOutput.ReadToEnd().Trim();
s_umask = (UnixFileMode)Convert.ToInt32(stdout, 8);
}
return s_umask;
}
}
}