Repository navigation
Expand file tree
/
Copy pathXisoOperationsTests.cs
More file actions
529 lines (439 loc) · 19 KB
/
Copy pathXisoOperationsTests.cs
File metadata and controls
529 lines (439 loc) · 19 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
namespace XISOSharp.Tests;
/// <summary>
/// Tests for <see cref="XisoOperations"/>: filler extract, seed, wipe, trim.
/// </summary>
[Collection("Sequential")]
public class XisoOperationsTests : IDisposable
{
private readonly List<string> _tempDirs = [];
private readonly List<string> _tempFiles = [];
public void Dispose()
{
foreach (string dir in _tempDirs)
{
try
{
if (Directory.Exists(dir)) Directory.Delete(dir, true);
}
catch
{
// ignored
}
}
foreach (string f in _tempFiles)
{
try
{
if (File.Exists(f)) File.Delete(f);
}
catch
{
// ignored
}
}
}
private string CreateTempDir()
{
string dir = Path.Combine(Path.GetTempPath(), $"xiso_ops_{Guid.NewGuid():N}");
Directory.CreateDirectory(dir);
_tempDirs.Add(dir);
return dir;
}
private string CreateSourceDir(Action<string> populate)
{
string src = Path.Combine(Path.GetTempPath(), $"xiso_ops_src_{Guid.NewGuid():N}");
Directory.CreateDirectory(src);
_tempDirs.Add(src);
populate(src);
return src;
}
private string CreateIso(string srcDir, int? prependSectors = null)
{
string outDir = CreateTempDir();
int result = XisoWriter.CreateXiso(srcDir, outDir, null, null, out string? isoPath, null, null,
prependSectors: prependSectors);
Assert.Equal(0, result);
Assert.NotNull(isoPath);
Assert.True(File.Exists(isoPath));
return isoPath;
}
private static void PopulateSimple(string dir)
{
File.WriteAllText(Path.Combine(dir, "a.txt"), "hello");
File.WriteAllText(Path.Combine(dir, "b.txt"), new string('x', 5000));
Directory.CreateDirectory(Path.Combine(dir, "sub"));
File.WriteAllText(Path.Combine(dir, "sub", "c.txt"), "nested content");
Random rnd = new(42);
byte[] data = new byte[7000];
rnd.NextBytes(data);
File.WriteAllBytes(Path.Combine(dir, "data.bin"), data);
}
// -----------------------------------------------------------------------
// ExtractFiller
// -----------------------------------------------------------------------
[Fact]
public void ExtractFiller_CreatesFillerFile_QuietTrue()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string fillerPath = Path.Combine(outDir, "out.filler");
bool ok = XisoOperations.ExtractFiller(iso, fillerPath, 0, null, quiet: true);
Assert.True(ok);
Assert.True(File.Exists(fillerPath));
// Filler is gaps between file extents; should be non-empty for a populated ISO
Assert.True(new FileInfo(fillerPath).Length > 0);
}
[Fact]
public void ExtractFiller_CreatesFillerFile_QuietFalse()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string fillerPath = Path.Combine(outDir, "out.filler");
bool ok = XisoOperations.ExtractFiller(iso, fillerPath, 0, null, quiet: false);
Assert.True(ok);
Assert.True(File.Exists(fillerPath));
Assert.True(new FileInfo(fillerPath).Length > 0);
}
[Fact]
public void ExtractFiller_FileStreamOverload_Succeeds()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string fillerPath = Path.Combine(outDir, "stream.filler");
using FileStream fs = new(iso, FileMode.Open, FileAccess.Read, FileShare.Read, 65536);
bool ok = XisoOperations.ExtractFiller(fs, 0, fs.Length, fillerPath, quiet: true);
Assert.True(ok);
Assert.True(File.Exists(fillerPath));
Assert.True(new FileInfo(fillerPath).Length > 0);
}
[Fact]
public void ExtractFiller_WithXisoLengthOverride_Succeeds()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string fillerPath = Path.Combine(outDir, "override.filler");
long isoLen = new FileInfo(iso).Length;
bool ok = XisoOperations.ExtractFiller(iso, fillerPath, 0, xisoLengthOverride: isoLen, quiet: true);
Assert.True(ok);
Assert.True(File.Exists(fillerPath));
}
[Fact]
public void ExtractFiller_WithIsoOffset_PrependedIso_Succeeds()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src, prependSectors: 16);
string outDir = CreateTempDir();
string fillerPath = Path.Combine(outDir, "prepend.filler");
const long offset = 16L * Constants.SectorSize;
bool ok = XisoOperations.ExtractFiller(iso, fillerPath, offset, null, quiet: true);
Assert.True(ok);
Assert.True(File.Exists(fillerPath));
}
[Fact]
public void ExtractFiller_FillerSizeMatchesExpected()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string fillerPath = Path.Combine(outDir, "size.filler");
bool ok = XisoOperations.ExtractFiller(iso, fillerPath, 0, null, quiet: true);
Assert.True(ok);
// Compute expected filler size via XisoRanges
using FileStream fs = new(iso, FileMode.Open, FileAccess.Read, FileShare.Read, 65536);
(List<(uint Start, uint End)> sys, List<(uint Start, uint End)> files) = XisoRanges.GetXisoRanges(fs, 0, true);
List<(uint Start, uint End)> merged = XisoRanges.MergeRanges(sys, files);
long validBytes = 0;
foreach ((uint s, uint e) in merged) validBytes += (e - s + 1L) * Constants.SectorSize;
long expectedFiller = fs.Length - validBytes;
Assert.Equal(expectedFiller, new FileInfo(fillerPath).Length);
}
[Fact]
public void ExtractFiller_MissingFile_ThrowsFileNotFoundException()
{
string outDir = CreateTempDir();
string missing = Path.Combine(outDir, "nonexistent.iso");
string filler = Path.Combine(outDir, "out.filler");
Assert.Throws<FileNotFoundException>(() => XisoOperations.ExtractFiller(missing, filler, 0, null, true));
}
[Fact]
public void ExtractFiller_InvalidIso_ThrowsEndOfStreamException()
{
string outDir = CreateTempDir();
string bad = Path.Combine(outDir, "bad.iso");
File.WriteAllBytes(bad, new byte[100]);
string filler = Path.Combine(outDir, "out.filler");
Assert.Throws<EndOfStreamException>(() => XisoOperations.ExtractFiller(bad, filler, 0, null, true));
}
[Fact]
public void ExtractFiller_Cancellation_ThrowsOperationCanceledException()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string filler = Path.Combine(outDir, "cancel.filler");
using CancellationTokenSource cts = new();
cts.Cancel();
Assert.Throws<OperationCanceledException>(() =>
XisoOperations.ExtractFiller(iso, filler, 0, null, true, cts.Token));
}
// -----------------------------------------------------------------------
// Seed extraction (XGD1 only)
// -----------------------------------------------------------------------
[Fact]
public void ExtractSeed_NonXgd1_ReturnsNull()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
uint? seed = XisoOperations.ExtractSeed(iso, 0, quiet: true);
Assert.Null(seed);
}
[Fact]
public void TryExtractSeed_NonXgd1_ReturnsFalseAndDoesNotCreateFile()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string seedPath = Path.Combine(outDir, "seed.bin");
if (File.Exists(seedPath)) File.Delete(seedPath);
bool ok = XisoOperations.TryExtractSeed(iso, seedPath, 0, quiet: true);
Assert.False(ok);
Assert.False(File.Exists(seedPath));
}
[Fact]
public void TryExtractSeed_MissingFile_ThrowsFileNotFoundException()
{
string outDir = CreateTempDir();
string missing = Path.Combine(outDir, "nonexistent.iso");
string seedPath = Path.Combine(outDir, "seed.bin");
Assert.Throws<FileNotFoundException>(() => XisoOperations.TryExtractSeed(missing, seedPath, 0, true));
}
[Fact]
public void ExtractSeed_WithIsoOffset_PrependedIso_ReturnsNullForNonXgd1()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src, prependSectors: 16);
const long offset = 16L * Constants.SectorSize;
uint? seed = XisoOperations.ExtractSeed(iso, offset, quiet: true);
Assert.Null(seed);
string outDir = CreateTempDir();
string seedPath = Path.Combine(outDir, "seed2.bin");
bool ok = XisoOperations.TryExtractSeed(iso, seedPath, offset, quiet: true);
Assert.False(ok);
}
// -----------------------------------------------------------------------
// WipeFiller
// -----------------------------------------------------------------------
[Fact]
public void WipeFiller_CreatesOutputSameSizeAndExtractable()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string wiped = Path.Combine(outDir, "wiped.iso");
bool ok = XisoOperations.WipeFiller(iso, wiped, 0, quiet: true);
Assert.True(ok);
Assert.True(File.Exists(wiped));
Assert.Equal(new FileInfo(iso).Length, new FileInfo(wiped).Length);
// Wiped ISO should still be extractable and preserve file content
string extractDir = CreateTempDir();
int res = XisoReader.Extract(wiped, extractDir, false);
Assert.Equal(0, res);
Assert.True(File.Exists(Path.Combine(extractDir, "a.txt")));
Assert.Equal("hello", File.ReadAllText(Path.Combine(extractDir, "a.txt")));
Assert.True(File.Exists(Path.Combine(extractDir, "sub", "c.txt")));
}
[Fact]
public void WipeFiller_MissingFile_ThrowsFileNotFoundException()
{
string outDir = CreateTempDir();
string missing = Path.Combine(outDir, "nonexistent.iso");
string wiped = Path.Combine(outDir, "wiped.iso");
Assert.Throws<FileNotFoundException>(() => XisoOperations.WipeFiller(missing, wiped, 0, true));
}
[Fact]
public void WipeFiller_InvalidIso_ThrowsEndOfStreamException()
{
string outDir = CreateTempDir();
string bad = Path.Combine(outDir, "bad.iso");
File.WriteAllBytes(bad, new byte[100]);
string wiped = Path.Combine(outDir, "wiped.iso");
Assert.Throws<EndOfStreamException>(() => XisoOperations.WipeFiller(bad, wiped, 0, true));
}
[Fact]
public void WipeFiller_Cancellation_ThrowsOperationCanceledException()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string wiped = Path.Combine(outDir, "wiped.iso");
using CancellationTokenSource cts = new();
cts.Cancel();
Assert.Throws<OperationCanceledException>(() => XisoOperations.WipeFiller(iso, wiped, 0, true, cts.Token));
}
// -----------------------------------------------------------------------
// TrimXiso
// -----------------------------------------------------------------------
[Fact]
public void TrimXiso_WithOutput_ReducesOrEqualsSizeAndRemainsValid()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string trimmed = Path.Combine(outDir, "trimmed.iso");
long origLen = new FileInfo(iso).Length;
bool ok = XisoOperations.TrimXiso(iso, trimmed, 0, quiet: true);
Assert.True(ok);
Assert.True(File.Exists(trimmed));
long trimmedLen = new FileInfo(trimmed).Length;
Assert.True(trimmedLen <= origLen, $"Trimmed length {trimmedLen} should be <= original {origLen}");
Assert.Equal(0, trimmedLen % Constants.SectorSize);
// Trimmed ISO should still be auditable / extractable
string extractDir = CreateTempDir();
int res = XisoReader.Extract(trimmed, extractDir, false);
Assert.Equal(0, res);
Assert.True(File.Exists(Path.Combine(extractDir, "a.txt")));
}
[Fact]
public void TrimXiso_InPlace_TrimsFileAndRemainsValid()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string inPlace = Path.Combine(outDir, "inplace.iso");
File.Copy(iso, inPlace, true);
long before = new FileInfo(inPlace).Length;
bool ok = XisoOperations.TrimXiso(inPlace, null, 0, quiet: true);
Assert.True(ok);
long after = new FileInfo(inPlace).Length;
Assert.True(after <= before);
Assert.Equal(0, after % Constants.SectorSize);
string extractDir = CreateTempDir();
int res = XisoReader.Extract(inPlace, extractDir, false);
Assert.Equal(0, res);
Assert.True(File.Exists(Path.Combine(extractDir, "b.txt")));
}
[Fact]
public void TrimXiso_InPlace_WithExplicitSamePath_TrimsFile()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string copy = Path.Combine(outDir, "copy.iso");
File.Copy(iso, copy, true);
long before = new FileInfo(copy).Length;
// Passing the same path as output should trigger in-place logic
bool ok = XisoOperations.TrimXiso(copy, copy, 0, quiet: true);
Assert.True(ok);
Assert.True(new FileInfo(copy).Length <= before);
}
[Fact]
public void TrimXiso_MissingFile_ThrowsFileNotFoundException()
{
string outDir = CreateTempDir();
string missing = Path.Combine(outDir, "nonexistent.iso");
string trimmed = Path.Combine(outDir, "trimmed.iso");
Assert.Throws<FileNotFoundException>(() => XisoOperations.TrimXiso(missing, trimmed, 0, true));
}
[Fact]
public void TrimXiso_InvalidIso_ThrowsEndOfStreamException()
{
string outDir = CreateTempDir();
string bad = Path.Combine(outDir, "bad.iso");
File.WriteAllBytes(bad, new byte[100]);
string trimmed = Path.Combine(outDir, "trimmed.iso");
Assert.Throws<EndOfStreamException>(() => XisoOperations.TrimXiso(bad, trimmed, 0, true));
}
[Fact]
public void TrimXiso_Cancellation_ThrowsOperationCanceledException()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string trimmed = Path.Combine(outDir, "trimmed.iso");
using CancellationTokenSource cts = new();
cts.Cancel();
Assert.Throws<OperationCanceledException>(() => XisoOperations.TrimXiso(iso, trimmed, 0, true, cts.Token));
}
// -----------------------------------------------------------------------
// WipeAndTrim
// -----------------------------------------------------------------------
[Fact]
public void WipeAndTrim_CreatesSmallerOutputAndRemainsValid()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string wipeTrimmed = Path.Combine(outDir, "wipetrim.iso");
long origLen = new FileInfo(iso).Length;
bool ok = XisoOperations.WipeAndTrim(iso, wipeTrimmed, 0, quiet: true);
Assert.True(ok);
Assert.True(File.Exists(wipeTrimmed));
long newLen = new FileInfo(wipeTrimmed).Length;
Assert.True(newLen <= origLen);
Assert.Equal(0, newLen % Constants.SectorSize);
// Output should be at most as large as TrimXiso output (wipe+trim is <= trim+wipe order)
string trimmed = Path.Combine(outDir, "trimmed.iso");
XisoOperations.TrimXiso(iso, trimmed, 0, true);
Assert.True(newLen <= new FileInfo(trimmed).Length);
// Verify extractable
string extractDir = CreateTempDir();
int res = XisoReader.Extract(wipeTrimmed, extractDir, false);
Assert.Equal(0, res);
Assert.True(File.Exists(Path.Combine(extractDir, "a.txt")));
Assert.Equal("hello", File.ReadAllText(Path.Combine(extractDir, "a.txt")));
}
[Fact]
public void WipeAndTrim_MissingFile_ThrowsFileNotFoundException()
{
string outDir = CreateTempDir();
string missing = Path.Combine(outDir, "nonexistent.iso");
string outPath = Path.Combine(outDir, "wipetrim.iso");
Assert.Throws<FileNotFoundException>(() => XisoOperations.WipeAndTrim(missing, outPath, 0, true));
}
[Fact]
public void WipeAndTrim_InvalidIso_ThrowsEndOfStreamException()
{
string outDir = CreateTempDir();
string bad = Path.Combine(outDir, "bad.iso");
File.WriteAllBytes(bad, new byte[100]);
string outPath = Path.Combine(outDir, "wipetrim.iso");
Assert.Throws<EndOfStreamException>(() => XisoOperations.WipeAndTrim(bad, outPath, 0, true));
}
[Fact]
public void WipeAndTrim_Cancellation_ThrowsOperationCanceledException()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string outPath = Path.Combine(outDir, "wipetrim.iso");
using CancellationTokenSource cts = new();
cts.Cancel();
Assert.Throws<OperationCanceledException>(() => XisoOperations.WipeAndTrim(iso, outPath, 0, true, cts.Token));
}
[Fact]
public void ExtractFiller_AndWipeFiller_FillerAndWipedAreConsistent()
{
string src = CreateSourceDir(PopulateSimple);
string iso = CreateIso(src);
string outDir = CreateTempDir();
string filler = Path.Combine(outDir, "filler.bin");
string wiped = Path.Combine(outDir, "wiped.iso");
Assert.True(XisoOperations.ExtractFiller(iso, filler, 0, null, true));
Assert.True(XisoOperations.WipeFiller(iso, wiped, 0, true));
long fillerLen = new FileInfo(filler).Length;
long isoLen = new FileInfo(iso).Length;
long wipedLen = new FileInfo(wiped).Length;
// Wiped should be same size as original, filler + valid data == iso size
Assert.Equal(isoLen, wipedLen);
using FileStream fs = new(iso, FileMode.Open, FileAccess.Read, FileShare.Read, 65536);
(List<(uint Start, uint End)> sys, List<(uint Start, uint End)> files) = XisoRanges.GetXisoRanges(fs, 0, true);
List<(uint Start, uint End)> merged = XisoRanges.MergeRanges(sys, files);
long validBytes = 0;
foreach ((uint s, uint e) in merged) validBytes += (e - s + 1L) * Constants.SectorSize;
Assert.Equal(validBytes + fillerLen, isoLen);
}
}