-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXisoReaderEdgeCaseTests.cs
More file actions
476 lines (373 loc) · 15.5 KB
/
Copy pathXisoReaderEdgeCaseTests.cs
File metadata and controls
476 lines (373 loc) · 15.5 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
using System.Security.Cryptography;
using XISOSharp.Models;
using XISOSharp.TestDataGenerator;
namespace XISOSharp.Tests;
/// <summary>
/// Additional integration tests for edge cases in <see cref="XisoReader"/>:
/// ListDirectory edge cases, ComputeFileHash edge cases, and cancellation behavior.
/// </summary>
[Collection("Sequential")]
public class XisoReaderEdgeCaseTests : IDisposable
{
// Resolved via TestDataLocator (BUG-TEST-006): no fragile 4x ".." literal.
private static readonly string TestDataRoot = TestDataLocator.GetTestDataRoot(AppContext.BaseDirectory);
private static readonly string SourceDir = Path.Combine(TestDataRoot, "source");
private readonly List<string> _tempDirs = [];
public void Dispose()
{
Logger.Quiet = false;
Logger.RealQuiet = false;
foreach (string dir in _tempDirs)
{
try
{
if (Directory.Exists(dir)) Directory.Delete(dir, true);
}
catch
{
/* best effort cleanup */
}
}
}
private string CreateTempDir()
{
string dir = Path.Combine(Path.GetTempPath(), $"xiso_edge_{Guid.NewGuid():N}");
Directory.CreateDirectory(dir);
_tempDirs.Add(dir);
return dir;
}
private string CreateTestIso()
{
string outputDir = CreateTempDir();
XisoWriter.CreateXiso(SourceDir, outputDir, null, null, out string? isoPath, null, null);
Assert.NotNull(isoPath);
return isoPath;
}
#region ListDirectory edge cases
[Fact]
public void ListDirectory_NonExistentPath_ThrowsInvalidDataException()
{
string isoPath = CreateTestIso();
Assert.Throws<InvalidDataException>(() => XisoReader.ListDirectory(isoPath, "/nonexistent"));
}
[Fact]
public void ListDirectory_DeeplyNestedPath_WorksCorrectly()
{
string isoPath = CreateTestIso();
IReadOnlyList<EntryInfo> entries = XisoReader.ListDirectory(isoPath, "/subdir/nested");
Assert.NotEmpty(entries);
Assert.Contains(entries, static e => string.Equals(e.Name, "deep.txt", StringComparison.Ordinal));
}
[Fact]
public void ListDirectory_InvalidIso_ThrowsXisoFormatException()
{
string tempFile = Path.Combine(CreateTempDir(), "bad.bin");
File.WriteAllBytes(tempFile, new byte[1024]);
Assert.Throws<XisoFormatException>(() => XisoReader.ListDirectory(tempFile));
}
[Fact]
public void ListDirectory_EntryInfo_SectorAndSizeAreConsistent()
{
string isoPath = CreateTestIso();
IReadOnlyList<EntryInfo> entries = XisoReader.ListDirectory(isoPath);
foreach (EntryInfo entry in entries)
{
if (!entry.IsDirectory)
{
Assert.True(entry.StartSector > 0, $"File {entry.Name} should have non-zero start sector");
Assert.True(entry.FileSize > 0, $"File {entry.Name} should have non-zero size");
}
}
}
#endregion
#region GetEntryInfo edge cases
[Fact]
public void GetEntryInfo_DeepPath_ReturnsCorrectEntry()
{
string isoPath = CreateTestIso();
EntryInfo? entry = XisoReader.GetEntryInfo(isoPath, "/subdir/nested/deep.txt");
Assert.NotNull(entry);
Assert.Equal("deep.txt", entry.Name);
Assert.False(entry.IsDirectory);
}
[Fact]
public void GetEntryInfo_InvalidIso_ThrowsXisoFormatException()
{
string tempFile = Path.Combine(CreateTempDir(), "bad.bin");
File.WriteAllBytes(tempFile, new byte[1024]);
Assert.Throws<XisoFormatException>(() => XisoReader.GetEntryInfo(tempFile, "/file.txt"));
}
[Fact]
public void GetEntryInfo_EmptyPath_ReturnsNull()
{
string isoPath = CreateTestIso();
Assert.Null(XisoReader.GetEntryInfo(isoPath, ""));
}
[Fact]
public void GetEntryInfo_PathWithTrailingSlash_ReturnsDirectoryEntry()
{
string isoPath = CreateTestIso();
// A trailing slash is ignored: "/subdir/" resolves to the "subdir" directory entry.
EntryInfo? entry = XisoReader.GetEntryInfo(isoPath, "/subdir/");
Assert.NotNull(entry);
Assert.Equal("subdir", entry.Name);
Assert.True(entry.IsDirectory);
}
#endregion
#region ComputeFileHash edge cases
[Fact]
public void ComputeFileHash_AllSupportedAlgorithms_ReturnHash()
{
string isoPath = CreateTestIso();
byte[]? md5 = XisoReader.ComputeFileHash(isoPath, "/file1.txt", HashAlgorithmName.MD5);
byte[]? sha1 = XisoReader.ComputeFileHash(isoPath, "/file1.txt", HashAlgorithmName.SHA1);
byte[]? sha256 = XisoReader.ComputeFileHash(isoPath, "/file1.txt", HashAlgorithmName.SHA256);
byte[]? sha384 = XisoReader.ComputeFileHash(isoPath, "/file1.txt", HashAlgorithmName.SHA384);
byte[]? sha512 = XisoReader.ComputeFileHash(isoPath, "/file1.txt", HashAlgorithmName.SHA512);
Assert.NotNull(md5);
Assert.NotNull(sha1);
Assert.NotNull(sha256);
Assert.NotNull(sha384);
Assert.NotNull(sha512);
Assert.Equal(16, md5.Length);
Assert.Equal(20, sha1.Length);
Assert.Equal(32, sha256.Length);
Assert.Equal(48, sha384.Length);
Assert.Equal(64, sha512.Length);
}
[Fact]
public void ComputeFileHash_SHA1_ReturnsCorrectLength()
{
string isoPath = CreateTestIso();
byte[]? hash = XisoReader.ComputeFileHash(isoPath, "/file1.txt", HashAlgorithmName.SHA1);
Assert.NotNull(hash);
Assert.Equal(20, hash.Length); // SHA-1 is 20 bytes
}
[Fact]
public void ComputeFileHash_SHA512_ReturnsCorrectLength()
{
string isoPath = CreateTestIso();
byte[]? hash = XisoReader.ComputeFileHash(isoPath, "/file1.txt", HashAlgorithmName.SHA512);
Assert.NotNull(hash);
Assert.Equal(64, hash.Length); // SHA-512 is 64 bytes
}
[Fact]
public void ComputeFileHash_MD5_IsDeterministic()
{
string isoPath = CreateTestIso();
byte[]? hash1 = XisoReader.ComputeFileHash(isoPath, "/file1.txt", HashAlgorithmName.MD5);
byte[]? hash2 = XisoReader.ComputeFileHash(isoPath, "/file1.txt", HashAlgorithmName.MD5);
Assert.NotNull(hash1);
Assert.NotNull(hash2);
Assert.Equal(hash1, hash2);
}
[Fact]
public void ComputeDirectoryHashes_EmptyDirectory_ReturnsEmpty()
{
string srcDir = CreateTempDir();
string outputDir = CreateTempDir();
// Create source with only an empty subdirectory
Directory.CreateDirectory(Path.Combine(srcDir, "empty"));
XisoWriter.CreateXiso(srcDir, outputDir, null, null, out string? isoPath, null, null);
Assert.NotNull(isoPath);
IReadOnlyList<(string Path, byte[] Hash)> hashes =
XisoReader.ComputeDirectoryHashes(isoPath, "/", HashAlgorithmName.SHA256);
// Should have no file hashes (only empty dir exists)
Assert.Empty(hashes);
}
[Fact]
public void ComputeDirectoryHashes_NestedDirectory_ReturnsAllFiles()
{
string isoPath = CreateTestIso();
IReadOnlyList<(string Path, byte[] Hash)> hashes =
XisoReader.ComputeDirectoryHashes(isoPath, "/subdir", HashAlgorithmName.SHA256);
Assert.NotEmpty(hashes);
// Should include subfile.txt and nested/deep.txt
Assert.Contains(hashes, static h => h.Path.Contains("subfile.txt", StringComparison.Ordinal));
Assert.Contains(hashes, static h => h.Path.Contains("deep.txt", StringComparison.Ordinal));
}
#endregion
#region GetVolumeInfo edge cases
[Fact]
public void GetVolumeInfo_VerySmallFile_ReturnsNotValid()
{
string tempFile = Path.Combine(CreateTempDir(), "tiny.bin");
File.WriteAllBytes(tempFile, new byte[10]);
VolumeInfo info = XisoReader.GetVolumeInfo(tempFile);
Assert.False(info.IsValid);
}
[Fact]
public void GetVolumeInfo_ExactHeaderSize_ReturnsNotValid()
{
string tempFile = Path.Combine(CreateTempDir(), "header_only.bin");
File.WriteAllBytes(tempFile, new byte[Constants.HeaderOffset + Constants.HeaderDataLength]);
VolumeInfo info = XisoReader.GetVolumeInfo(tempFile);
Assert.False(info.IsValid);
}
[Fact]
public void GetVolumeInfo_CreatedIso_HasCorrectMetadata()
{
string isoPath = CreateTestIso();
VolumeInfo info = XisoReader.GetVolumeInfo(isoPath);
Assert.True(info.IsValid);
Assert.True(info.RootDirSector > 0);
Assert.True(info.RootDirSize > 0);
Assert.True(info.FileLength > 0);
Assert.True(info.TotalSectors > 0);
Assert.Equal(0, info.DiscLseek);
}
[Fact]
public void GetVolumeInfo_FileLength_MatchesActualFileSize()
{
string isoPath = CreateTestIso();
VolumeInfo info = XisoReader.GetVolumeInfo(isoPath);
long actualSize = new FileInfo(isoPath).Length;
Assert.Equal(actualSize, info.FileLength);
}
[Fact]
public void GetVolumeInfo_TotalSectors_MatchesFileLength()
{
string isoPath = CreateTestIso();
VolumeInfo info = XisoReader.GetVolumeInfo(isoPath);
long expectedSectors = info.FileLength / Constants.SectorSize;
Assert.Equal(expectedSectors, info.TotalSectors);
}
#endregion
#region CopyOut edge cases
[Fact]
public void CopyOut_DirectoryWithNestedFiles_ExtractsAll()
{
string isoPath = CreateTestIso();
string destDir = CreateTempDir();
string destPath = Path.Combine(destDir, "subdir_copy");
XisoReader.CopyOut(isoPath, "/subdir", destPath);
Assert.True(Directory.Exists(destPath));
Assert.True(File.Exists(Path.Combine(destPath, "subfile.txt")));
Assert.True(File.Exists(Path.Combine(destPath, "nested", "deep.txt")));
}
[Fact]
public void CopyOut_EmptyFile_CreatesZeroLengthFile()
{
string srcDir = CreateTempDir();
string outputDir = CreateTempDir();
string destDir = CreateTempDir();
File.WriteAllText(Path.Combine(srcDir, "empty.txt"), "");
XisoWriter.CreateXiso(srcDir, outputDir, null, null, out string? isoPath, null, null);
Assert.NotNull(isoPath);
string destPath = Path.Combine(destDir, "empty.txt");
XisoReader.CopyOut(isoPath, "/empty.txt", destPath);
Assert.True(File.Exists(destPath));
Assert.Equal(0, new FileInfo(destPath).Length);
}
[Fact]
public void CopyOut_LargeFile_PreservesContent()
{
string srcDir = CreateTempDir();
string outputDir = CreateTempDir();
string destDir = CreateTempDir();
byte[] data = new byte[512 * 1024]; // 512KB
new Random(42).NextBytes(data);
File.WriteAllBytes(Path.Combine(srcDir, "large.bin"), data);
XisoWriter.CreateXiso(srcDir, outputDir, null, null, out string? isoPath, null, null);
Assert.NotNull(isoPath);
string destPath = Path.Combine(destDir, "large.bin");
XisoReader.CopyOut(isoPath, "/large.bin", destPath);
byte[] extracted = File.ReadAllBytes(destPath);
Assert.Equal(data, extracted);
}
#endregion
#region Cancellation
[Fact]
public void DecodeXiso_CancelledBeforeStart_ThrowsOperationCanceled()
{
string isoPath = CreateTestIso();
string extractDir = CreateTempDir();
CancellationTokenSource cts = new();
cts.Cancel();
Assert.Throws<OperationCanceledException>(() =>
XisoReader.DecodeXiso(isoPath, extractDir, ExtractMode.Extract, out _, false, cts.Token));
}
[Fact]
public async Task DecodeXisoAsync_CancelledBeforeStart_ThrowsOperationCanceled()
{
string isoPath = CreateTestIso();
string extractDir = CreateTempDir();
CancellationTokenSource cts = new();
await cts.CancelAsync();
#pragma warning disable MA0004
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
await XisoReader.DecodeXisoAsync(isoPath, extractDir, ExtractMode.Extract, false, cts.Token));
#pragma warning restore MA0004
}
#endregion
#region VerifyXiso XGD offset detection
[Fact]
public void VerifyXiso_RawIso_DetectsZeroOffset()
{
string isoPath = CreateTestIso();
using FileStream fs = new(isoPath, FileMode.Open, FileAccess.Read, FileShare.Read);
(_, _, long discLseek) = XisoReader.VerifyXiso(fs, "test.iso");
Assert.Equal(0, discLseek);
}
#endregion
#region Rewrite edge cases
[Fact]
public void Rewrite_ThenExtract_PreservesAllContent()
{
string createDir = CreateTempDir();
string rewriteDir = CreateTempDir();
string extractDir = CreateTempDir();
XisoWriter.CreateXiso(SourceDir, createDir, null, null, out string? isoPath, null, null);
Assert.NotNull(isoPath);
XisoReader.Rewrite(isoPath, rewriteDir, out string? rewrittenPath);
Assert.NotNull(rewrittenPath);
XisoReader.Extract(rewrittenPath, extractDir, false);
// Verify all files preserved
Assert.True(File.Exists(Path.Combine(extractDir, "file1.txt")));
Assert.True(File.Exists(Path.Combine(extractDir, "file2.txt")));
Assert.True(File.Exists(Path.Combine(extractDir, "binary.bin")));
Assert.True(File.Exists(Path.Combine(extractDir, "test.xbe")));
Assert.True(File.Exists(Path.Combine(extractDir, "subdir", "subfile.txt")));
Assert.True(File.Exists(Path.Combine(extractDir, "subdir", "nested", "deep.txt")));
}
[Fact]
public void Rewrite_WithCustomName_UsesProvidedName()
{
string createDir = CreateTempDir();
string rewriteDir = CreateTempDir();
XisoWriter.CreateXiso(SourceDir, createDir, null, null, out string? isoPath, null, null);
Assert.NotNull(isoPath);
XisoReader.Rewrite(isoPath, rewriteDir, out string? rewrittenPath, outputName: "rewritten_custom");
Assert.NotNull(rewrittenPath);
// The outputName controls the ISO filename within the output directory
Assert.Equal("rewritten_custom", Path.GetFileName(rewrittenPath));
Assert.True(File.Exists(rewrittenPath), $"Rewritten ISO not found at {rewrittenPath}");
}
[Fact]
public void Rewrite_WithAbsoluteOutputName_UsesProvidedName()
{
string createDir = CreateTempDir();
string rewriteDir = CreateTempDir();
XisoWriter.CreateXiso(SourceDir, createDir, null, null, out string? isoPath, null, null);
Assert.NotNull(isoPath);
// A rooted -o path must be honored verbatim: the Windows drive-letter
// strip used to rewrite C:\... into :\... and fail the write.
string absoluteName = Path.Combine(rewriteDir, "abs_custom.iso");
XisoReader.Rewrite(isoPath, null, out string? rewrittenPath, outputName: absoluteName);
Assert.Equal(absoluteName, rewrittenPath);
Assert.True(File.Exists(rewrittenPath), $"Rewritten ISO not found at {rewrittenPath}");
}
[Fact]
public void Rewrite_UnwritableOutput_ReturnsError()
{
string createDir = CreateTempDir();
XisoWriter.CreateXiso(SourceDir, createDir, null, null, out string? isoPath, null, null);
Assert.NotNull(isoPath);
string missingDir = $"missing_{Guid.NewGuid():N}";
int result = XisoReader.Rewrite(isoPath, null, out string? rewrittenPath,
outputName: Path.Combine(missingDir, "out.iso"));
Assert.NotEqual(0, result);
Assert.Null(rewrittenPath);
}
#endregion
}