Repository navigation
Expand file tree
/
Copy pathXisoExplorerKeepOpenTests.cs
More file actions
183 lines (160 loc) · 6.45 KB
/
Copy pathXisoExplorerKeepOpenTests.cs
File metadata and controls
183 lines (160 loc) · 6.45 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
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using XISOSharp.Models;
namespace XISOSharp.Tests;
/// <summary>
/// Keep-open explorer mode (2.2): one held image stream for the explorer's
/// lifetime, an internal lock serializing operations, and a dispose that
/// releases both the stream and every read stream handed out. The stateless
/// default must stay byte-for-byte unchanged.
/// </summary>
[Collection("Sequential")]
public sealed class XisoExplorerKeepOpenTests : IDisposable
{
private readonly List<string> _tempDirs = [];
private readonly bool _savedQuiet;
private readonly bool _savedRealQuiet;
public XisoExplorerKeepOpenTests()
{
_savedQuiet = Logger.Quiet;
_savedRealQuiet = Logger.RealQuiet;
}
public void Dispose()
{
Logger.Quiet = _savedQuiet;
Logger.RealQuiet = _savedRealQuiet;
foreach (string dir in _tempDirs)
{
try
{
if (Directory.Exists(dir)) Directory.Delete(dir, true);
}
catch
{
// ignored
}
}
}
private string CreateTempDir(string prefix)
{
string dir = Path.Combine(Path.GetTempPath(), $"{prefix}_{Guid.NewGuid():N}");
Directory.CreateDirectory(dir);
_tempDirs.Add(dir);
return dir;
}
private string CreateIso()
{
string src = CreateTempDir("xiso_keep_src");
File.WriteAllText(Path.Combine(src, "a.txt"), "hello");
File.WriteAllText(Path.Combine(src, "b.txt"), new string('x', 3000));
Directory.CreateDirectory(Path.Combine(src, "sub"));
File.WriteAllText(Path.Combine(src, "sub", "c.txt"), "nested");
File.WriteAllBytes(Path.Combine(src, "empty.bin"), []);
string outDir = CreateTempDir("xiso_keep_iso");
int result = XisoWriter.CreateXiso(src, outDir, null, null, out string? isoPath, null, null);
Assert.Equal(0, result);
Assert.NotNull(isoPath);
return isoPath;
}
[Fact]
public void KeepOpen_OperationsMatchStatelessExplorer()
{
string iso = CreateIso();
using XisoExplorer stateful = new(iso, new XisoExplorerOptions { KeepOpen = true });
XisoExplorer stateless = new(iso);
Assert.True(stateful.IsKeepOpen);
Assert.False(stateless.IsKeepOpen);
Assert.Equal(stateless.Volume, stateful.Volume);
Assert.Equal(stateless.ListChildren("/"), stateful.ListChildren("/"));
Assert.Equal(stateless.ListChildren("/sub"), stateful.ListChildren("/sub"));
Assert.Equal(stateless.GetNode("/a.txt"), stateful.GetNode("/a.txt"));
Assert.Equal(stateless.GetNode("/sub/c.txt"), stateful.GetNode("/sub/c.txt"));
Assert.Equal(stateless.GetNode("/missing"), stateful.GetNode("/missing"));
Assert.Equal(stateless.ComputeHashHex("/b.txt", HashAlgorithmName.SHA256),
stateful.ComputeHashHex("/b.txt", HashAlgorithmName.SHA256));
Assert.Equal(stateless.Volume.DiscFormat, stateful.Volume.DiscFormat);
Assert.Equal(stateless.Volume.CreationTime, stateful.Volume.CreationTime);
Assert.Equal(stateless.Volume.DescriptorSector, stateful.Volume.DescriptorSector);
}
[Fact]
[SuppressMessage("ReSharper", "AccessToDisposedClosure")]
public void KeepOpen_ParallelMetadataOperations_Smoke()
{
string iso = CreateIso();
using XisoExplorer explorer = new(iso, new XisoExplorerOptions { KeepOpen = true });
Parallel.For(0, 64, i =>
{
switch (i % 4)
{
case 0:
Assert.Equal(4, explorer.ListChildren("/").Count);
break;
case 1:
Assert.Equal("a.txt", explorer.GetNode("/a.txt")!.Name);
break;
case 2:
Assert.NotNull(explorer.ComputeHashHex("/b.txt", HashAlgorithmName.SHA256));
break;
default:
Assert.Equal("c.txt", explorer.ListChildren("/sub").Single().Name);
break;
}
});
}
[Fact]
public void KeepOpen_DisposeThenOperate_ThrowsObjectDisposed()
{
string iso = CreateIso();
XisoExplorer explorer = new(iso, new XisoExplorerOptions { KeepOpen = true });
explorer.Dispose();
Assert.Throws<ObjectDisposedException>(() => explorer.ListChildren("/"));
Assert.Throws<ObjectDisposedException>(() => explorer.GetNode("/"));
Assert.Throws<ObjectDisposedException>(() => explorer.GetNode("/a.txt"));
Assert.Throws<ObjectDisposedException>(() => explorer.OpenReadStream("/a.txt"));
Assert.Throws<ObjectDisposedException>(() => explorer.ComputeHashHex("/a.txt", HashAlgorithmName.SHA256));
}
[Fact]
public void KeepOpen_DoubleDispose_IsHarmless()
{
string iso = CreateIso();
XisoExplorer explorer = new(iso, new XisoExplorerOptions { KeepOpen = true });
explorer.Dispose();
explorer.Dispose();
}
[Fact]
public void KeepOpen_OpenReadStreamDiesWithExplorer()
{
string iso = CreateIso();
XisoExplorer explorer = new(iso, new XisoExplorerOptions { KeepOpen = true });
using Stream stream = explorer.OpenReadStream("/a.txt");
Assert.Equal("hello".Length, stream.Read(new byte[16]));
explorer.Dispose();
// The held parent stream is gone; a read that reaches the parent throws.
stream.Seek(0, SeekOrigin.Begin);
Assert.Throws<ObjectDisposedException>(() => stream.Read(new byte[16]));
}
[Fact]
public void Stateless_DisposeIsHarmlessAndOperationsKeepWorking()
{
string iso = CreateIso();
XisoExplorer explorer = new(iso);
explorer.Dispose();
// Stateless mode owns no stream: disposing the explorer is a no-op and
// per-call streams keep the documented behavior.
Assert.False(explorer.IsKeepOpen);
Assert.Equal(4, explorer.ListChildren("/").Count);
Assert.NotNull(explorer.GetNode("/a.txt"));
}
[Fact]
public void KeepOpen_ShareParameterIsExposed()
{
string iso = CreateIso();
using XisoExplorer explorer = new(iso, new XisoExplorerOptions
{
KeepOpen = true,
Share = FileShare.ReadWrite,
});
Assert.Equal(FileShare.ReadWrite, explorer.Options.Share);
Assert.True(explorer.Volume.IsValid);
}
}