This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathFileSystemTest.cs
130 lines (111 loc) · 5.74 KB
/
FileSystemTest.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using Microsoft.DotNet.XUnitExtensions;
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 };
protected const TestPlatforms CaseInsensitivePlatforms = TestPlatforms.Windows | TestPlatforms.OSX;
protected const TestPlatforms CaseSensitivePlatforms = TestPlatforms.AnyUnix & ~TestPlatforms.OSX;
public static bool AreAllLongPathsAvailable => PathFeatures.AreAllLongPathsAvailable();
public static bool LongPathsAreNotBlocked => !PathFeatures.AreLongPathsBlocked();
public static bool UsingNewNormalization => !PathFeatures.IsUsingLegacyPathNormalization();
public static TheoryData<string> PathsWithInvalidColons = TestData.PathsWithInvalidColons;
public static TheoryData<string> PathsWithInvalidCharacters = TestData.PathsWithInvalidCharacters;
public static TheoryData<char> TrailingCharacters = TestData.TrailingCharacters;
public static TheoryData ValidPathComponentNames = IOInputs.GetValidPathComponentNames().ToTheoryData();
public static TheoryData SimpleWhiteSpace = IOInputs.GetSimpleWhiteSpace().ToTheoryData();
public static TheoryData WhiteSpace = IOInputs.GetWhiteSpace().ToTheoryData();
public static TheoryData UncPathsWithoutShareName = IOInputs.GetUncPathsWithoutShareName().ToTheoryData();
public static TheoryData PathsWithReservedDeviceNames = IOInputs.GetPathsWithReservedDeviceNames().ToTheoryData();
public static TheoryData PathsWithColons = IOInputs.GetPathsWithColons().ToTheoryData();
public static TheoryData PathsWithComponentLongerThanMaxComponent = IOInputs.GetPathsWithComponentLongerThanMaxComponent().ToTheoryData();
public static TheoryData ControlWhiteSpace = IOInputs.GetControlWhiteSpace().ToTheoryData();
public static TheoryData 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>
/// In some cases (such as when running without elevated privileges),
/// the symbolic link may fail to create. Only run this test if it creates
/// links successfully.
/// </summary>
protected static bool CanCreateSymbolicLinks => s_canCreateSymbolicLinks.Value;
private static readonly Lazy<bool> s_canCreateSymbolicLinks = new Lazy<bool>(() =>
{
// Verify file symlink creation
string path = Path.GetTempFileName();
string linkPath = path + ".link";
bool success = MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: false);
try { File.Delete(path); } catch { }
try { File.Delete(linkPath); } catch { }
// Verify directory symlink creation
path = Path.GetTempFileName();
linkPath = path + ".link";
success = success && MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true);
try { Directory.Delete(path); } catch { }
try { Directory.Delete(linkPath); } catch { }
return success;
});
protected string GetNamedPipeServerStreamName()
{
if (PlatformDetection.IsInAppContainer)
{
return @"LOCAL\" + Guid.NewGuid().ToString("N");
}
return Guid.NewGuid().ToString("N");
}
/// <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}"));
}
}
}
}