-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCisoSplitInteropTests.cs
More file actions
294 lines (250 loc) · 11.8 KB
/
Copy pathCisoSplitInteropTests.cs
File metadata and controls
294 lines (250 loc) · 11.8 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
using System.Diagnostics;
using System.Security.Cryptography;
using XISOSharp.BlockDevice;
using XISOSharp.TestDataGenerator;
namespace XISOSharp.Tests;
/// <summary>
/// <para>
/// Golden round-trips against actual Rust-produced <c>.N.cso</c> files from the
/// reference <c>xdvdfs-cli 0.8.3</c> binary (<c>ciso 0.2.1</c> split writer/reader).
/// The binary lives in the gitignored <c>References/xdvdfs-0.8.3</c> folder; tests that
/// need it silently pass when it is absent (same pattern as the <c>zarchive.exe</c> interop).
/// </para>
/// <para>Notes on the reference tooling, verified against <c>ciso 0.2.1</c> sources:</para>
/// <list type="bullet">
/// <item><c>unpack</c>/<c>copy-out</c> use <c>open_image_raw</c> and do NOT accept <c>.cso</c>
/// input, so the content oracle here is <c>xdvdfs md5</c> (cso-aware via
/// <c>open_image</c>), comparing <c>md5 /path</c> listings.</item>
/// <item>The reference <c>SplitOutput</c> writes each part sparsely at global stream
/// offsets (a write crossing the split point lands whole, overshooting into the next
/// part's range); <c>CisoSplitOutput</c> replicates this loop exactly.</item>
/// <item>Multi-part files cannot be consumed by stock 0.8.3 itself: its
/// <c>SplitFileReader</c> maps parts at cumulative file sizes while part data sits at
/// global offsets, so reads past the first part hit zero gaps (<c>read.rs</c> assert).
/// <c>CisoSplitInputStream</c> instead tiles parts by previous part length
/// (<c>starts[k] = parts[k-1].Length</c>), which matches the writer layout exactly.
/// Single-part files are unaffected on both sides.</item>
/// </list>
/// </summary>
[Collection("Sequential")]
public class CisoSplitInteropTests : 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()
{
foreach (string dir in _tempDirs)
{
try
{
if (Directory.Exists(dir)) Directory.Delete(dir, true);
}
catch
{
/* best effort */
}
try
{
if (File.Exists(dir)) File.Delete(dir);
}
catch
{
/* best effort */
}
}
}
private static string SolutionRoot() =>
// Centralized via TestDataLocator (BUG-TEST-006).
TestDataLocator.GetSolutionRoot(AppContext.BaseDirectory)
?? throw new InvalidOperationException("Solution root not found.");
private static string XdvdfsExePath() => Path.Combine(SolutionRoot(), "References", "xdvdfs-0.8.3", "xdvdfs.exe");
private static bool ReferenceAvailable() => OperatingSystem.IsWindows() && File.Exists(XdvdfsExePath());
private static string RunXdvdfs(string[] arguments, string workDir)
{
ProcessStartInfo psi = new()
{
FileName = XdvdfsExePath(),
WorkingDirectory = workDir,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
foreach (string arg in arguments)
{
psi.ArgumentList.Add(arg);
}
using Process? proc = Process.Start(psi);
Assert.NotNull(proc);
Task<string> stdoutTask = proc.StandardOutput.ReadToEndAsync();
Task<string> stderrTask = proc.StandardError.ReadToEndAsync();
if (!proc.WaitForExit(120000))
{
try
{
proc.Kill(entireProcessTree: true);
}
catch
{
// Best effort kill after timeout.
}
proc.WaitForExit(5000);
Assert.Fail("xdvdfs.exe timed out and was killed.");
}
string stdout = stdoutTask.GetAwaiter().GetResult();
string stderr = stderrTask.GetAwaiter().GetResult();
Assert.True(proc.ExitCode == 0, $"xdvdfs.exe failed (exit {proc.ExitCode}): {stderr}");
return stdout;
}
private string CreateTempDir()
{
string dir = Path.Combine(Path.GetTempPath(), "xiso_cisointerop_" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(dir);
_tempDirs.Add(dir);
return dir;
}
private string CreateTempIso(string? sourceDir = null)
{
sourceDir ??= SourceDir;
string outDir = CreateTempDir();
int rc = XisoWriter.CreateXiso(sourceDir, outDir, null, null, out string? outPath, null, null);
Assert.Equal(0, rc);
Assert.NotNull(outPath);
Assert.True(File.Exists(outPath));
_tempDirs.Add(Path.GetDirectoryName(outPath)!);
return outPath;
}
private static byte[] ComputeSha256(string path) => SHA256.HashData(File.ReadAllBytes(path));
private static string Md5Hex(byte[] data) => Convert.ToHexString(MD5.HashData(data)).ToLowerInvariant();
private static Dictionary<string, string> ParseMd5Listing(string stdout)
{
Dictionary<string, string> map = new(StringComparer.Ordinal);
foreach (string line in stdout.Split('\n'))
{
string trimmed = line.Trim();
if (trimmed.Length == 0) continue;
int sep = trimmed.IndexOf(" ", StringComparison.Ordinal);
Assert.True(sep > 0, $"unexpected md5 line: {trimmed}");
map[trimmed[(sep + 2)..]] = trimmed[..sep];
}
return map;
}
private static string StockMd5(string imagePath, string workDir) => RunXdvdfs(["md5", imagePath], workDir);
/// <summary>
/// Keeps only entries that are files on disk: directory dirtab bytes are
/// packer-specific (entry order/padding) and legitimately differ between
/// our packer and the reference repack.
/// </summary>
private static Dictionary<string, string> FilesOnly(Dictionary<string, string> listing, string sourceDir)
{
Dictionary<string, string> files = new(StringComparer.Ordinal);
foreach ((string path, string hash) in listing)
{
string local = path.TrimStart('/').Replace('/', Path.DirectorySeparatorChar);
if (File.Exists(Path.Combine(sourceDir, local)))
{
files[path] = hash;
}
}
return files;
}
[RequiresOracleFact(OracleKind.Xdvdfs)]
public void RustCompressIso_OurReaderDecompressesIdenticalContent()
{
Assert.True(ReferenceAvailable(), "Missing reference oracle 'Xdvdfs'.");
string isoPath = CreateTempIso();
string workDir = CreateTempDir();
// Note: the reference SplitOutput derives part names from the file name only
// and creates them relative to the process working directory.
RunXdvdfs(["compress", isoPath, Path.Combine(workDir, "rust.cso")], workDir);
string part1 = Path.Combine(workDir, "rust.1.cso");
Assert.True(File.Exists(part1), "expected the reference writer to emit rust.1.cso");
Assert.True(CisoReader.IsCso(part1));
string decPath = Path.Combine(CreateTempDir(), "rust.iso");
Assert.Equal(0, CisoReader.DecompressToIso(part1, decPath));
// The reference repacks the image before compressing, so compare content
// listings through the same tool rather than raw ISO bytes (as dicts:
// dirtab order may differ between packers).
Assert.Equal(
FilesOnly(ParseMd5Listing(StockMd5(isoPath, workDir)), SourceDir),
FilesOnly(ParseMd5Listing(StockMd5(decPath, workDir)), SourceDir));
using CisoBlockDevice dev = new(part1);
Assert.Equal(new FileInfo(decPath).Length, dev.Length);
}
[RequiresOracleFact(OracleKind.Xdvdfs)]
public void RustCompressDir_OurReaderRoundTripsContent()
{
Assert.True(ReferenceAvailable(), "Missing reference oracle 'Xdvdfs'.");
string srcDir = CreateTempDir();
Directory.CreateDirectory(Path.Combine(srcDir, "sub"));
byte[] blob = Enumerable.Range(0, 200000).Select(i => (byte)((i * 2654435761u) >> 16)).ToArray();
File.WriteAllText(Path.Combine(srcDir, "hello.txt"), "hello rust golden world");
File.WriteAllBytes(Path.Combine(srcDir, "sub", "blob.bin"), blob);
string workDir = CreateTempDir();
RunXdvdfs(["compress", srcDir, Path.Combine(workDir, "rustdir.cso")], workDir);
string part1 = Path.Combine(workDir, "rustdir.1.cso");
Assert.True(File.Exists(part1), "expected the reference writer to emit rustdir.1.cso");
string decPath = Path.Combine(CreateTempDir(), "rustdir.iso");
Assert.Equal(0, CisoReader.DecompressToIso(part1, decPath));
Dictionary<string, string> got = FilesOnly(ParseMd5Listing(StockMd5(decPath, workDir)), srcDir);
Dictionary<string, string> expected = new(StringComparer.Ordinal)
{
["/hello.txt"] = Md5Hex("hello rust golden world"u8.ToArray()),
["/sub/blob.bin"] = Md5Hex(blob),
};
Assert.Equal(expected, got);
}
[RequiresOracleFact(OracleKind.Xdvdfs)]
public void OurSingleCso_StockReadsIdenticalContent()
{
Assert.True(ReferenceAvailable(), "Missing reference oracle 'Xdvdfs'.");
string isoPath = CreateTempIso();
string workDir = CreateTempDir();
string csoPath = Path.Combine(workDir, "single.cso");
Assert.Equal(0, CisoWriter.CompressToCso(isoPath, csoPath, level: 9));
Assert.Equal(
FilesOnly(ParseMd5Listing(StockMd5(isoPath, workDir)), SourceDir),
FilesOnly(ParseMd5Listing(StockMd5(csoPath, workDir)), SourceDir));
}
[Fact]
public void OurSplitParts_FollowReferenceWriterLayout()
{
// Structural parity with ciso::split::SplitOutput (verified against ciso 0.2.1
// split.rs): part names, sparse global-offset writes, and the overshoot rule
// (a write crossing the split point lands whole, so each part's data starts
// exactly where the previous part's file ends). Needs no reference binary.
string isoPath = CreateTempIso();
string csoDir = CreateTempDir();
string csoPath = Path.Combine(csoDir, "ours.cso");
const long splitBytes = 16384;
Assert.Equal(0, CisoWriter.CompressToCso(isoPath, csoPath, level: 9, splitBytes: splitBytes));
List<string> parts = new();
for (int i = 1;; i++)
{
string part = Path.Combine(csoDir, $"ours.{i}.cso");
if (!File.Exists(part)) break;
parts.Add(part);
}
Assert.True(parts.Count >= 2, $"expected at least 2 parts, got {parts.Count}");
long previousLength = 0;
foreach (string part in parts)
{
byte[] data = File.ReadAllBytes(part);
int firstData = Array.FindIndex(data, b => b != 0);
Assert.True(firstData >= 0, $"{part} has no data");
// Data starts exactly where the previous part's file ends (sequential
// global-offset writes into sparse part files).
Assert.Equal(previousLength, firstData);
previousLength = data.Length;
}
// The logical stream tiles contiguously: the last part ends at the stream end.
Assert.Equal(new FileInfo(parts[^1]).Length, previousLength);
// Content round-trips through our tiling reader (SplitFileReader parity note
// in the class doc: stock 0.8.3 itself cannot read sparse multi-part files).
string decPath = Path.Combine(CreateTempDir(), "ours.iso");
Assert.Equal(0, CisoReader.DecompressToIso(parts[0], decPath));
Assert.True(ComputeSha256(isoPath).AsSpan().SequenceEqual(ComputeSha256(decPath)));
}
}