-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXisoSectorLayoutTests.cs
More file actions
292 lines (254 loc) · 11.4 KB
/
Copy pathXisoSectorLayoutTests.cs
File metadata and controls
292 lines (254 loc) · 11.4 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System.Buffers.Binary;
using System.Text;
using XISOSharp.Models;
namespace XISOSharp.Tests;
/// <summary>
/// Tests for <see cref="XisoReader.GetSectorLayout"/> (TODO #4, xdvdfs #49):
/// every file and directory table mapped to its sector range, with merged used
/// ranges and free gaps tiling the partition.
/// </summary>
[Collection("Sequential")]
public class XisoSectorLayoutTests : IDisposable
{
private readonly List<string> _tempDirs = [];
public void Dispose()
{
foreach (string dir in _tempDirs)
{
try
{
if (Directory.Exists(dir)) Directory.Delete(dir, true);
}
catch
{
/* best effort */
}
if (File.Exists(dir))
{
try
{
File.Delete(dir);
}
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 CreateSourceDir(Action<string> populate)
{
string src = CreateTempDir("xiso_lay_src");
populate(src);
return src;
}
private string CreateIso(string srcDir)
{
string outDir = CreateTempDir("xiso_lay_out");
int result = XisoWriter.CreateXiso(srcDir, outDir, null, null, out string? isoPath, null, null);
Assert.Equal(0, result);
Assert.NotNull(isoPath);
return isoPath;
}
private static byte[] RandomBytes(int length, int seed)
{
byte[] bin = new byte[length];
new Random(seed).NextBytes(bin);
return bin;
}
private static void PopulateMixed(string dir)
{
File.WriteAllText(Path.Combine(dir, "file1.txt"), "hello");
File.WriteAllBytes(Path.Combine(dir, "file2.txt"), RandomBytes(5000, 42));
File.WriteAllBytes(Path.Combine(dir, "empty.txt"), Array.Empty<byte>());
Directory.CreateDirectory(Path.Combine(dir, "subdir"));
File.WriteAllText(Path.Combine(dir, "subdir", "nested.txt"), "nested");
File.WriteAllBytes(Path.Combine(dir, "subdir", "data.bin"), RandomBytes(7000, 7));
}
private static uint SectorCountFor(long byteSize) =>
byteSize == 0 ? 0u : (uint)((byteSize + Constants.SectorSize - 1) / Constants.SectorSize);
[Fact]
public void GetSectorLayout_MapsFilesToCorrectSectorsAndBytes()
{
string src = CreateSourceDir(PopulateMixed);
string iso = CreateIso(src);
VolumeInfo vol = XisoReader.GetVolumeInfo(iso);
Assert.True(vol.IsValid);
SectorLayout layout = XisoReader.GetSectorLayout(iso);
Assert.Equal(vol.RootDirSector, layout.Volume.RootDirSector);
string[] expectedFiles = new[]
{ "/file1.txt", "/file2.txt", "/empty.txt", "/subdir/nested.txt", "/subdir/data.bin" };
foreach (string path in expectedFiles)
{
FileSectorExtent extent = Assert.Single(layout.Entries,
e => string.Equals(e.Path, path, StringComparison.Ordinal) && !e.IsDirectory);
EntryInfo? info = XisoReader.GetEntryInfo(iso, path);
Assert.NotNull(info);
Assert.Equal(info.StartSector, extent.StartSector);
byte[] sourceBytes =
File.ReadAllBytes(Path.Combine(src, path.TrimStart('/').Replace('/', Path.DirectorySeparatorChar)));
Assert.Equal((uint)sourceBytes.Length, extent.FileSize);
Assert.Equal(SectorCountFor(sourceBytes.Length), extent.SectorCount);
// Physical proof: bytes at the mapped sectors equal the source file.
if (sourceBytes.Length > 0)
{
using FileStream fs = new(iso, FileMode.Open, FileAccess.Read, FileShare.Read);
fs.Seek(vol.DiscLseek + ((long)extent.StartSector * Constants.SectorSize), SeekOrigin.Begin);
byte[] actual = new byte[sourceBytes.Length];
int read = 0;
while (read < actual.Length)
{
int n = fs.Read(actual, read, actual.Length - read);
Assert.True(n > 0);
read += n;
}
Assert.Equal(sourceBytes, actual);
}
}
// Multi-sector spans: 5000 bytes -> 3 sectors, 7000 -> 4.
Assert.Equal(3u,
layout.Entries.Single(e => string.Equals(e.Path, "/file2.txt", StringComparison.Ordinal)).SectorCount);
Assert.Equal(4u,
layout.Entries.Single(e => string.Equals(e.Path, "/subdir/data.bin", StringComparison.Ordinal))
.SectorCount);
Assert.Equal(0u,
layout.Entries.Single(e => string.Equals(e.Path, "/empty.txt", StringComparison.Ordinal)).SectorCount);
}
[Fact]
public void GetSectorLayout_IncludesDirectoryTables()
{
string src = CreateSourceDir(PopulateMixed);
string iso = CreateIso(src);
VolumeInfo vol = XisoReader.GetVolumeInfo(iso);
SectorLayout layout = XisoReader.GetSectorLayout(iso);
FileSectorExtent root = Assert.Single(layout.Entries,
e => string.Equals(e.Path, "/", StringComparison.Ordinal) && e.IsDirectory);
Assert.Equal(vol.RootDirSector, root.StartSector);
Assert.Equal(vol.RootDirSize, root.FileSize);
Assert.Equal(SectorCountFor(vol.RootDirSize), root.SectorCount);
FileSectorExtent sub = Assert.Single(layout.Entries,
e => string.Equals(e.Path, "/subdir", StringComparison.Ordinal) && e.IsDirectory);
EntryInfo? subInfo = XisoReader.GetEntryInfo(iso, "/subdir");
Assert.NotNull(subInfo);
Assert.True(subInfo.IsDirectory);
Assert.Equal(subInfo.StartSector, sub.StartSector);
Assert.True(sub.SectorCount >= 1);
}
[Fact]
public void GetSectorLayout_UsedAndFreeTilePartition()
{
string src = CreateSourceDir(PopulateMixed);
string iso = CreateIso(src);
SectorLayout layout = XisoReader.GetSectorLayout(iso);
// Entries sorted by start sector.
for (int i = 1; i < layout.Entries.Count; i++)
Assert.True(layout.Entries[i].StartSector >= layout.Entries[i - 1].StartSector);
// Used ranges sorted and non-overlapping.
for (int i = 1; i < layout.UsedRanges.Count; i++)
{
Assert.True(layout.UsedRanges[i].StartSector >=
(long)layout.UsedRanges[i - 1].StartSector + layout.UsedRanges[i - 1].SectorCount);
}
// Used + free cover [0, TotalSectors) exactly once.
int[] coverage = new int[(int)layout.TotalSectors];
foreach (SectorRange r in layout.UsedRanges.Concat(layout.FreeRanges))
{
for (long s = r.StartSector; s < (long)r.StartSector + r.SectorCount; s++)
coverage[(int)s]++;
}
Assert.All(coverage, c => Assert.Equal(1, c));
// Header sector is always allocated; every named non-empty extent is covered.
Assert.Contains(layout.UsedRanges,
r => r.StartSector <= Constants.HeaderOffset / Constants.SectorSize &&
(long)r.StartSector + r.SectorCount > Constants.HeaderOffset / Constants.SectorSize);
foreach (FileSectorExtent e in layout.Entries.Where(e => e.SectorCount > 0))
{
Assert.Contains(layout.UsedRanges,
r => r.StartSector <= e.StartSector &&
(long)r.StartSector + r.SectorCount >= (long)e.StartSector + e.SectorCount);
}
}
[Fact]
public void GetSectorLayout_InvalidImage_Throws()
{
string bad = Path.Combine(CreateTempDir("xiso_lay_bad"), "bad.iso");
File.WriteAllBytes(bad, "not an xiso image at all"u8.ToArray());
Assert.Throws<XisoFormatException>(() => XisoReader.GetSectorLayout(bad));
}
[Fact]
public void GetSectorLayout_CorruptRightOffset_ThrowsInvalidToc()
{
string src = CreateSourceDir(PopulateMixed);
string iso = CreateIso(src);
VolumeInfo vol = XisoReader.GetVolumeInfo(iso);
// Point the first root entry's right child far outside the table
// (0xFFFF is the pad sentinel and would merely terminate the branch).
long rootAbs = vol.DiscLseek + ((long)vol.RootDirSector * Constants.SectorSize);
using (FileStream fs = new(iso, FileMode.Open, FileAccess.Write, FileShare.None))
{
fs.Seek(rootAbs + 2, SeekOrigin.Begin);
Span<byte> u16 = stackalloc byte[2];
BinaryPrimitives.WriteUInt16LittleEndian(u16, 0xFFFE);
fs.Write(u16);
}
XisoFormatException ex = Assert.Throws<XisoFormatException>(() => XisoReader.GetSectorLayout(iso));
Assert.Contains("invalid TOC entry", ex.Message, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void GetSectorLayout_DirectoryCycle_ThrowsInvalidToc()
{
string src = CreateSourceDir(PopulateMixed);
string iso = CreateIso(src);
VolumeInfo vol = XisoReader.GetVolumeInfo(iso);
EntryInfo? subInfo = XisoReader.GetEntryInfo(iso, "/subdir");
Assert.NotNull(subInfo);
// Rewrite the subdir entry's data sector to the root sector, so the walk
// re-enters an already-visited table (Burnout-style cycle across tables).
// NOTE: GetEntryInfo zeroes FileSize for directories, so locate the entry
// by linearly scanning the packed table for its name instead.
long rootAbs = vol.DiscLseek + ((long)vol.RootDirSector * Constants.SectorSize);
byte[] table = File.ReadAllBytes(iso);
int at = FindEntrySectorOffset(table, (int)rootAbs, (int)(rootAbs + vol.RootDirSize), "subdir");
Assert.True(at >= 0, "subdir entry not found in root table");
using (FileStream fs = new(iso, FileMode.Open, FileAccess.Write, FileShare.None))
{
fs.Seek(at, SeekOrigin.Begin);
Span<byte> u32 = stackalloc byte[4];
BinaryPrimitives.WriteUInt32LittleEndian(u32, vol.RootDirSector);
fs.Write(u32);
}
XisoFormatException ex = Assert.Throws<XisoFormatException>(() => XisoReader.GetSectorLayout(iso));
Assert.Contains("invalid TOC entry", ex.Message, StringComparison.OrdinalIgnoreCase);
Assert.Contains("already visited", ex.Message, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Linearly scans the packed entries of one directory table for an entry with
/// the given name (entries are dword-aligned: 14-byte header + name, padded to 4).
/// Returns the file offset of the entry's sector field, or -1 when absent.
/// </summary>
private static int FindEntrySectorOffset(byte[] image, int tableStart, int tableEnd, string name)
{
int pos = tableStart;
while (pos + 14 <= Math.Min(tableEnd, image.Length))
{
byte nameLen = image[pos + 13];
if (pos + 14 + nameLen > Math.Min(tableEnd, image.Length))
break;
string entryName = Encoding.ASCII.GetString(image, pos + 14, nameLen);
if (string.Equals(entryName, name, StringComparison.Ordinal) &&
(image[pos + 12] & 0x10) != 0)
{
return pos + 4;
}
pos += (14 + nameLen + 3) & ~3;
}
return -1;
}
}