-
Notifications
You must be signed in to change notification settings - Fork 0
/
VfsIntegrationTests.cs
187 lines (160 loc) · 7.46 KB
/
VfsIntegrationTests.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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WinVfs.VirtualFilesystem;
namespace WinVfs.Tests;
[TestClass]
public sealed class VfsIntegrationTests
{
[TestMethod]
[DataRow(false)]
[DataRow(true)]
public void EnumerateVirtRootAndReadFileAndSubDir(bool returnDirsInCacheLookup)
{
using var tempDir = new DisposableTempDirectory();
string virtRoot = Path.Combine(tempDir.Path, "virt");
Directory.CreateDirectory(virtRoot);
var logger = new ConsoleLogger { IsFinestLoggingEnabled = true };
var callbacks = new MockVfsCallbacks { ReturnDirectoriesInCacheLookup = returnDirsInCacheLookup };
var rootDirMetadata = new DirectoryMetadata(string.Empty);
rootDirMetadata.WritableEntries.Add(new FileSystemEntry("file", 1234));
rootDirMetadata.WritableEntries.Add(new FileSystemEntry("subdir"));
callbacks.Filesystem[rootDirMetadata.RelativeBasePath] = rootDirMetadata;
string filePath = Path.Combine(virtRoot, "file");
callbacks.FileSizes["file"] = 1234;
DateTime hourAgo = DateTime.UtcNow - TimeSpan.FromHours(1);
var subdirMetadata = new DirectoryMetadata("subdir");
subdirMetadata.WritableEntries.Add(new FileSystemEntry("subdirFile", 5678, hourAgo));
callbacks.Filesystem[subdirMetadata.RelativeBasePath] = subdirMetadata;
DateTime sessionStart = DateTime.UtcNow - TimeSpan.FromMinutes(1);
using var projFsSession = new ProjFsVirtualizationSession(
virtRoot,
callbacks,
logger,
sessionStart,
new ProjFsOptions { EnableDetailedTrace = true },
CancellationToken.None);
// Enumerate and verify the contents of the virtualization root.
var virtRootDir = new DirectoryInfo(virtRoot);
FileSystemInfo[] entries = virtRootDir.GetFileSystemInfos();
Assert.AreEqual(2, entries.Length);
Assert.AreEqual("file", entries[0].Name);
Assert.IsFalse(entries[0].Attributes.HasFlag(FileAttributes.Directory));
Assert.AreEqual(1234L, ((FileInfo)entries[0]).Length);
Assert.AreEqual(sessionStart, entries[0].LastWriteTimeUtc);
Assert.AreEqual(sessionStart, entries[0].CreationTimeUtc);
Assert.AreEqual(sessionStart, entries[0].LastAccessTimeUtc);
Assert.AreEqual("subdir", entries[1].Name);
Assert.IsTrue(entries[1].Attributes.HasFlag(FileAttributes.Directory));
Assert.AreEqual(1, callbacks.NumTryGetDirectoryMetadataCacheEntryCalls);
Assert.AreEqual(returnDirsInCacheLookup ? 0 : 1, callbacks.NumRequestDirectoryMetadataCalls);
Assert.AreEqual(1, callbacks.NumDirectoryEnumerations);
Assert.AreEqual(0, callbacks.NumGetFileCalls);
Assert.AreEqual(0, callbacks.NumFatalErrors);
// Read the contents of the file in the root.
byte[] fileContents = File.ReadAllBytes(filePath);
Assert.AreEqual(1234, fileContents.Length);
byte expectedContent = 0x01;
for (int i = 0; i < fileContents.Length; i++)
{
Assert.AreEqual(expectedContent, fileContents[i]);
if (expectedContent == 0xFF)
{
expectedContent = 0x00;
}
else
{
expectedContent++;
}
}
Assert.AreEqual(2, callbacks.NumTryGetDirectoryMetadataCacheEntryCalls);
Assert.AreEqual(returnDirsInCacheLookup ? 0 : 2, callbacks.NumRequestDirectoryMetadataCalls);
Assert.AreEqual(1, callbacks.NumDirectoryEnumerations);
Assert.AreEqual(1, callbacks.NumGetFileCalls);
Assert.AreEqual(0, callbacks.NumFatalErrors);
// Enumerate and verify the contents of subdir.
string subdirPath = Path.Combine(virtRoot, "subdir");
var subdirDir = new DirectoryInfo(subdirPath);
entries = subdirDir.GetFileSystemInfos();
Assert.AreEqual(1, entries.Length);
Assert.AreEqual("subdirFile", entries[0].Name);
Assert.IsFalse(entries[0].Attributes.HasFlag(FileAttributes.Directory));
Assert.AreEqual(5678, ((FileInfo)entries[0]).Length);
Assert.AreEqual(hourAgo, entries[0].LastWriteTimeUtc);
Assert.AreEqual(hourAgo, entries[0].CreationTimeUtc);
Assert.AreEqual(hourAgo, entries[0].LastAccessTimeUtc);
Assert.AreEqual(4, callbacks.NumTryGetDirectoryMetadataCacheEntryCalls);
Assert.AreEqual(returnDirsInCacheLookup ? 0 : 4, callbacks.NumRequestDirectoryMetadataCalls);
Assert.AreEqual(2,callbacks.NumDirectoryEnumerations);
Assert.AreEqual(1, callbacks.NumGetFileCalls);
Assert.AreEqual(0, callbacks.NumFatalErrors);
}
}
internal sealed class MockVfsCallbacks : IVfsCallbacks
{
/// <summary>
/// Whether directories in <see cref="Filesystem"/> are returned in <see cref="TryGetDirectoryMetadataCacheEntry"/>.
/// </summary>
public bool ReturnDirectoriesInCacheLookup { get; set; }
public Dictionary<string, IDirectoryMetadata> Filesystem { get; } = new(StringComparer.OrdinalIgnoreCase);
public Dictionary<string, int> FileSizes { get; } = new(StringComparer.OrdinalIgnoreCase);
public int NumTryGetDirectoryMetadataCacheEntryCalls;
public bool TryGetDirectoryMetadataCacheEntry(string virtualRelativePath, [NotNullWhen(true)] out IDirectoryMetadata? directoryMetadata)
{
Interlocked.Increment(ref NumTryGetDirectoryMetadataCacheEntryCalls);
if (!ReturnDirectoriesInCacheLookup)
{
directoryMetadata = null;
return false;
}
return Filesystem.TryGetValue(virtualRelativePath, out directoryMetadata);
}
public int NumRequestDirectoryMetadataCalls;
public ValueTask<IDirectoryMetadata> RequestDirectoryMetadataAsync(string virtualRelativePath, CancellationToken cancellationToken)
{
Interlocked.Increment(ref NumRequestDirectoryMetadataCalls);
if (Filesystem.TryGetValue(virtualRelativePath, out IDirectoryMetadata? dirMetadata))
{
return ValueTask.FromResult(dirMetadata);
}
return ValueTask.FromResult<IDirectoryMetadata>(new DirectoryMetadata(virtualRelativePath, exists: false));
}
public int NumDirectoryEnumerations;
public void OnDirectoryEnumeration(string virtualRelativePath, string enumerationPattern)
{
Interlocked.Increment(ref NumDirectoryEnumerations);
}
public int NumGetFileCalls;
public ValueTask<Stream> GetFileAsync(string virtualRelativePath, CancellationToken cancellationToken)
{
Interlocked.Increment(ref NumGetFileCalls);
if (FileSizes.TryGetValue(virtualRelativePath, out int size))
{
var contents = new byte[size];
byte content = 0x01;
for (int i = 0; i < size; i++)
{
contents[i] = content;
if (content == 0xFF)
{
content = 0x00;
}
else
{
content++;
}
}
return ValueTask.FromResult<Stream>(new MemoryStream(contents));
}
throw new FileNotFoundException(virtualRelativePath);
}
public int NumFatalErrors;
public void FatalErrorOccurred(Exception exception)
{
Interlocked.Increment(ref NumFatalErrors);
}
}