-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
VirtualDriveSymbolicLinks.Windows.cs
209 lines (169 loc) · 11.6 KB
/
VirtualDriveSymbolicLinks.Windows.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Xunit;
namespace System.IO.Tests
{
// Need to reuse the same virtual drive for all the test methods.
// Creating and disposing one virtual drive per class achieves this.
[PlatformSpecific(TestPlatforms.Windows)]
[ConditionalClass(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks), nameof(MountHelper.IsSubstAvailable))]
public class VirtualDrive_SymbolicLinks : BaseSymbolicLinks
{
private VirtualDriveHelper VirtualDrive { get; } = new VirtualDriveHelper();
protected override void Dispose(bool disposing)
{
VirtualDrive.Dispose();
base.Dispose(disposing);
}
[Theory]
// false, false, false, false // Target is not in virtual drive
// false, false, true, false // Target is not in virtual drive
[InlineData(false, true, false, true)] // Immediate target expected, target is in virtual drive
[InlineData(false, true, true, false)] // Final target expected, target is in virtual drive
// true, false, false, false // Target is not in virtual drive
// true, false, true, false // Target is not in virtual drive
[InlineData(true, true, false, true)] // Immediate target expected, target is in virtual drive
[InlineData(true, true, true, false)] // Final target expected, target is in virtual drive
public void VirtualDrive_SymbolicLinks_LinkAndTarget(
bool isLinkInVirtualDrive,
bool isTargetInVirtualDrive,
bool returnFinalTarget,
bool isExpectedTargetPathVirtual)
{
string linkExpectedFolderPath = GetVirtualOrRealPath(isLinkInVirtualDrive);
// File link
string fileLinkName = GetRandomLinkName();
string fileLinkPath = Path.Join(linkExpectedFolderPath, fileLinkName);
// Directory link
string dirLinkName = GetRandomLinkName();
string dirLinkPath = Path.Join(linkExpectedFolderPath, dirLinkName);
string targetExpectedFolderPath = GetVirtualOrRealPath(isTargetInVirtualDrive);
// File target
string fileTargetFileName = GetRandomFileName();
string fileTargetPath = Path.Join(targetExpectedFolderPath, fileTargetFileName);
// Directory target
string dirTargetFileName = GetRandomDirName();
string dirTargetPath = Path.Join(targetExpectedFolderPath, dirTargetFileName);
// Create targets
File.Create(fileTargetPath).Dispose();
Directory.CreateDirectory(dirTargetPath);
// Create links
FileInfo fileLinkInfo = new FileInfo(fileLinkPath);
fileLinkInfo.CreateAsSymbolicLink(fileTargetPath);
DirectoryInfo dirLinkInfo = new DirectoryInfo(dirLinkPath);
dirLinkInfo.CreateAsSymbolicLink(dirTargetPath);
// The expected results depend on the target location and the value of returnFinalTarget
// LinkTarget always retrieves the immediate target, so the expected value
// is always the path that was provided by the user for the target
// Verify the LinkTarget values of the link infos
Assert.Equal(fileTargetPath, fileLinkInfo.LinkTarget);
Assert.Equal(dirTargetPath, dirLinkInfo.LinkTarget);
// When the target is in a virtual drive, and returnFinalTarget is true,
// the expected target path is the real path, not the virtual path
string expectedTargetPath = GetVirtualOrRealPath(isExpectedTargetPathVirtual);
string expectedTargetFileInfoFullName = Path.Join(expectedTargetPath, fileTargetFileName);
string expectedTargetDirectoryInfoFullName = Path.Join(expectedTargetPath, dirTargetFileName);
// Verify target infos from link info instances
FileSystemInfo? targetFileInfoFromFileInfoLink = fileLinkInfo.ResolveLinkTarget(returnFinalTarget);
FileSystemInfo? targetDirInfoFromDirInfoLink = dirLinkInfo.ResolveLinkTarget(returnFinalTarget);
Assert.True(targetFileInfoFromFileInfoLink is FileInfo);
Assert.True(targetDirInfoFromDirInfoLink is DirectoryInfo);
Assert.Equal(expectedTargetFileInfoFullName, targetFileInfoFromFileInfoLink.FullName);
Assert.Equal(expectedTargetDirectoryInfoFullName, targetDirInfoFromDirInfoLink.FullName);
// Verify targets infos via static methods
FileSystemInfo? targetFileInfoFromFile = File.ResolveLinkTarget(fileLinkPath, returnFinalTarget);
FileSystemInfo? targetFileInfoFromDirectory = Directory.ResolveLinkTarget(dirLinkPath, returnFinalTarget);
Assert.True(targetFileInfoFromFile is FileInfo);
Assert.True(targetFileInfoFromDirectory is DirectoryInfo);
Assert.Equal(expectedTargetFileInfoFullName, targetFileInfoFromFile.FullName);
Assert.Equal(expectedTargetDirectoryInfoFullName, targetFileInfoFromDirectory.FullName);
}
[Theory]
// false, false, false, false, false // Target is not in virtual drive
// false, false, false, true, false // Target is not in virtual drive
[InlineData(false, false, true, false, false)] // Immediate target expected, middle link is NOT in virtual drive
[InlineData(false, false, true, true, false)] // Final target expected, target is in virtual drive
// false, true, false, false, false // Target is not in virtual drive
// false, true, false, true, false // Target is not in virtual drive
[InlineData(false, true, true, false, true)] // Immediate target expected, target is in virtual drive
[InlineData(false, true, true, true, false)] // Final target expected, target is in virtual drive
// true, false, false, false, false // Target is not in virtual drive
// true, false, false, true, false // Target is not in virtual drive
[InlineData(true, false, true, false, false)] // Immediate target expected, middle link is NOT in virtual drive
[InlineData(true, false, true, true, false)] // Final target expected, target is in virtual drive
// true, true, false, false, false // Target is not in virtual drive
// true, true, false, true, false // Target is not in virtual drive
[InlineData(true, true, true, false, true)] // Immediate target expected, target is in virtual drive
[InlineData(true, true, true, true, false)] // Final target expected, target is in virtual drive
public void VirtualDrive_SymbolicLinks_WithIndirection(
bool isFirstLinkInVirtualDrive,
bool isMiddleLinkInVirtualDrive,
bool isTargetInVirtualDrive,
bool returnFinalTarget,
bool isExpectedTargetPathVirtual)
{
string firstLinkExpectedFolderPath = GetVirtualOrRealPath(isFirstLinkInVirtualDrive);
// File link
string fileLinkPath = Path.Join(firstLinkExpectedFolderPath, GetRandomLinkName());
// Directory link
string dirLinkPath = Path.Join(firstLinkExpectedFolderPath, GetRandomLinkName());
string middleLinkExpectedFolderPath = GetVirtualOrRealPath(isMiddleLinkInVirtualDrive);
// File middle link
string fileMiddleLinkFileName = GetRandomLinkName();
string fileMiddleLinkPath = Path.Join(middleLinkExpectedFolderPath, fileMiddleLinkFileName);
// Directory middle link
string dirMiddleLinkFileName = GetRandomLinkName();
string dirMiddleLinkPath = Path.Join(middleLinkExpectedFolderPath, dirMiddleLinkFileName);
string targetExpectedFolderPath = GetVirtualOrRealPath(isTargetInVirtualDrive);
// File final target
string fileFinalTargetFileName = GetRandomFileName();
string fileFinalTargetPath = Path.Join(targetExpectedFolderPath, fileFinalTargetFileName);
// Directory final target
string dirFinalTargetFileName = GetRandomDirName();
string dirFinalTargetPath = Path.Join(targetExpectedFolderPath, dirFinalTargetFileName);
// Create targets
File.Create(fileFinalTargetPath).Dispose();
Directory.CreateDirectory(dirFinalTargetPath);
// Create initial links
FileInfo fileLinkInfo = new FileInfo(fileLinkPath);
fileLinkInfo.CreateAsSymbolicLink(fileMiddleLinkPath);
DirectoryInfo dirLinkInfo = new DirectoryInfo(dirLinkPath);
dirLinkInfo.CreateAsSymbolicLink(dirMiddleLinkPath);
// Create middle links
FileInfo fileMiddleLinkInfo = new FileInfo(fileMiddleLinkPath);
fileMiddleLinkInfo.CreateAsSymbolicLink(fileFinalTargetPath);
DirectoryInfo dirMiddleLinkInfo = new DirectoryInfo(dirMiddleLinkPath);
dirMiddleLinkInfo.CreateAsSymbolicLink(dirFinalTargetPath);
// The expected results depend on the target location and the value of returnFinalTarget
// LinkTarget always retrieves the immediate target, so the expected value
// is always the path that was provided by the user for the middle link
// Verify the LinkTarget values of the link infos
Assert.Equal(fileMiddleLinkPath, fileLinkInfo.LinkTarget);
Assert.Equal(dirMiddleLinkPath, dirLinkInfo.LinkTarget);
// When the target is in a virtual drive,
// the expected target path is the real path, not the virtual path
// When returnFinalTarget is true, the expected target path is the
// resolved path from the final target in the chain of links
string expectedTargetPath = GetVirtualOrRealPath(isExpectedTargetPathVirtual);
string expectedTargetFileInfoFullName = Path.Join(expectedTargetPath,
returnFinalTarget ? fileFinalTargetFileName : fileMiddleLinkFileName);
string expectedTargetDirectoryInfoFullName = Path.Join(expectedTargetPath,
returnFinalTarget ? dirFinalTargetFileName : dirMiddleLinkFileName);
// Verify target infos from link info instances
FileSystemInfo? targetFileInfoFromFileInfoLink = fileLinkInfo.ResolveLinkTarget(returnFinalTarget);
FileSystemInfo? targetDirInfoFromDirInfoLink = dirLinkInfo.ResolveLinkTarget(returnFinalTarget);
Assert.True(targetFileInfoFromFileInfoLink is FileInfo);
Assert.True(targetDirInfoFromDirInfoLink is DirectoryInfo);
Assert.Equal(expectedTargetFileInfoFullName, targetFileInfoFromFileInfoLink.FullName);
Assert.Equal(expectedTargetDirectoryInfoFullName, targetDirInfoFromDirInfoLink.FullName);
// Verify targets infos via static methods
FileSystemInfo? targetFileInfoFromFile = File.ResolveLinkTarget(fileLinkPath, returnFinalTarget);
FileSystemInfo? targetFileInfoFromDirectory = Directory.ResolveLinkTarget(dirLinkPath, returnFinalTarget);
Assert.True(targetFileInfoFromFile is FileInfo);
Assert.True(targetFileInfoFromDirectory is DirectoryInfo);
Assert.Equal(expectedTargetFileInfoFullName, targetFileInfoFromFile.FullName);
Assert.Equal(expectedTargetDirectoryInfoFullName, targetFileInfoFromDirectory.FullName);
}
private string GetVirtualOrRealPath(bool condition) => condition ? $"{VirtualDrive.VirtualDriveLetter}:" : VirtualDrive.VirtualDriveTargetDir;
}
}