-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXisoReadFileBytesTests.cs
More file actions
191 lines (159 loc) · 6.18 KB
/
Copy pathXisoReadFileBytesTests.cs
File metadata and controls
191 lines (159 loc) · 6.18 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
namespace XISOSharp.Tests;
/// <summary>
/// Static span convenience (2.1): <c>XisoReader.ReadFileBytes</c> one-shot
/// reads with no extraction to disk — offset at/past EOF returns 0, a short
/// buffer or short file tail reads what exists, missing/directory targets
/// throw, and the stream overload leaves the caller's stream open.
/// </summary>
[Collection("Sequential")]
public sealed class XisoReadFileBytesTests : IDisposable
{
private readonly List<string> _tempDirs = [];
public void Dispose()
{
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(out byte[] fileA, out byte[] fileB)
{
fileA = new byte[Constants.SectorSize];
for (int i = 0; i < fileA.Length; i++) fileA[i] = (byte)(i & 0xFF);
fileB = new byte[(3 * Constants.SectorSize) + 5];
for (int i = 0; i < fileB.Length; i++) fileB[i] = (byte)(0x33 + (i % 64));
string src = CreateTempDir("xiso_rfb_src");
File.WriteAllBytes(Path.Combine(src, "a.bin"), fileA);
File.WriteAllBytes(Path.Combine(src, "b.bin"), fileB);
File.WriteAllBytes(Path.Combine(src, "empty.bin"), []);
Directory.CreateDirectory(Path.Combine(src, "sub"));
File.WriteAllText(Path.Combine(src, "sub", "c.txt"), "nested");
string outDir = CreateTempDir("xiso_rfb_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 ReadFileBytes_FullFile_MatchesSource()
{
string iso = CreateIso(out _, out byte[] fileB);
byte[] buffer = new byte[fileB.Length];
int n = XisoReader.ReadFileBytes(iso, "/b.bin", buffer, 0);
Assert.Equal(fileB.Length, n);
Assert.Equal(fileB, buffer);
}
[Fact]
public void ReadFileBytes_OffsetAtEof_ReturnsZero()
{
string iso = CreateIso(out byte[] fileA, out _);
byte[] buffer = new byte[64];
Assert.Equal(0, XisoReader.ReadFileBytes(iso, "/a.bin", buffer, fileA.Length));
Assert.Equal(0, XisoReader.ReadFileBytes(iso, "/a.bin", buffer, fileA.Length + 10_000));
}
[Fact]
public void ReadFileBytes_ShortFinalChunk_Clamps()
{
string iso = CreateIso(out byte[] fileA, out _);
byte[] buffer = new byte[64];
int n = XisoReader.ReadFileBytes(iso, "/a.bin", buffer, fileA.Length - 4);
Assert.Equal(4, n);
Assert.Equal(fileA.AsSpan(fileA.Length - 4, 4).ToArray(), buffer.AsSpan(0, n).ToArray());
}
[Fact]
public void ReadFileBytes_OffsetInside_ReturnsRequestedBytes()
{
string iso = CreateIso(out _, out byte[] fileB);
byte[] buffer = new byte[100];
const long offset = 2048 + 3;
int n = XisoReader.ReadFileBytes(iso, "/b.bin", buffer, offset);
Assert.Equal(100, n);
Assert.Equal(fileB.AsSpan((int)offset, 100).ToArray(), buffer);
}
[Fact]
public void ReadFileBytes_SmallerBufferThanRemaining_ReadsBufferSize()
{
string iso = CreateIso(out _, out _);
byte[] buffer = new byte[7];
Assert.Equal(7, XisoReader.ReadFileBytes(iso, "/b.bin", buffer, 0));
}
[Fact]
public void ReadFileBytes_EmptyBuffer_ReturnsZero()
{
string iso = CreateIso(out _, out _);
Assert.Equal(0, XisoReader.ReadFileBytes(iso, "/b.bin", Span<byte>.Empty, 0));
}
[Fact]
public void ReadFileBytes_NegativeOffset_Throws()
{
string iso = CreateIso(out _, out _);
Assert.Throws<ArgumentOutOfRangeException>(() =>
XisoReader.ReadFileBytes(iso, "/b.bin", new byte[8], -1));
}
[Fact]
public void ReadFileBytes_Missing_ThrowsInvalidData()
{
string iso = CreateIso(out _, out _);
Assert.Throws<InvalidDataException>(() =>
XisoReader.ReadFileBytes(iso, "/nope.bin", new byte[8], 0));
}
[Fact]
public void ReadFileBytes_Directory_ThrowsInvalidData()
{
string iso = CreateIso(out _, out _);
Assert.Throws<InvalidDataException>(() =>
XisoReader.ReadFileBytes(iso, "/sub", new byte[8], 0));
Assert.Throws<InvalidDataException>(() =>
XisoReader.ReadFileBytes(iso, "/", new byte[8], 0));
}
[Fact]
public void ReadFileBytes_EmptyFile_ReturnsZero()
{
string iso = CreateIso(out _, out _);
Assert.Equal(0, XisoReader.ReadFileBytes(iso, "/empty.bin", new byte[8], 0));
}
[Fact]
public void ReadFileBytes_StreamOverload_LeavesStreamOpen()
{
string iso = CreateIso(out _, out byte[] fileB);
using FileStream fs = new(iso, FileMode.Open, FileAccess.Read, FileShare.Read, 65536);
byte[] buffer = new byte[32];
int n = XisoReader.ReadFileBytes(fs, iso, "/b.bin", buffer, 0);
Assert.Equal(32, n);
Assert.Equal(fileB.AsSpan(0, 32).ToArray(), buffer);
Assert.True(fs.CanRead); // caller owns and keeps the stream
}
[Fact]
public void ReadFileBytes_CaseInsensitivePath_Resolves()
{
string iso = CreateIso(out byte[] fileA, out _);
byte[] buffer = new byte[fileA.Length];
int n = XisoReader.ReadFileBytes(iso, "/A.BIN", buffer, 0);
Assert.Equal(fileA.Length, n);
Assert.Equal(fileA, buffer);
}
[Fact]
public void ReadFileBytes_EmptyBuffer_StillValidatesPath()
{
string iso = CreateIso(out _, out _);
Assert.Throws<InvalidDataException>(() =>
XisoReader.ReadFileBytes(iso, "/nope.bin", Span<byte>.Empty, 0));
Assert.Throws<InvalidDataException>(() =>
XisoReader.ReadFileBytes(iso, "/sub", Span<byte>.Empty, 0));
Assert.Equal(0, XisoReader.ReadFileBytes(iso, "/b.bin", Span<byte>.Empty, 0));
}
}