Skip to content

Commit ee81e70

Browse files
committed
Fix tests
1 parent 94c52bf commit ee81e70

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Async.Tests.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,20 @@ public async Task Throw_ArchiveIsShortAsync()
283283
}
284284

285285
[Fact]
286-
public async Task GarbageEntryChecksumZeroReturnNullAsync()
286+
public async Task GarbageEntryChecksumZeroThrowsInvalidDataException()
287287
{
288288
await using MemoryStream archiveStream = GetTarMemoryStream(CompressionMethod.Uncompressed, "golang_tar", "issue12435");
289289
await using TarReader reader = new TarReader(archiveStream);
290-
Assert.Null(await reader.GetNextEntryAsync());
290+
await Assert.ThrowsAsync<InvalidDataException>(async () => await reader.GetNextEntryAsync());
291+
}
292+
293+
[Fact]
294+
public async Task InvalidChecksum_ThrowsInvalidDataException()
295+
{
296+
await using MemoryStream archiveStream = GetTarMemoryStream(CompressionMethod.Uncompressed, "node-tar", "bad-cksum");
297+
await using TarReader reader = new TarReader(archiveStream);
298+
await reader.GetNextEntryAsync(); // first entry is okay
299+
await Assert.ThrowsAsync<InvalidDataException>(async () => await reader.GetNextEntryAsync());
291300
}
292301

293302
[Theory]

src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,20 @@ public void Throw_ArchiveIsShort()
291291
}
292292

293293
[Fact]
294-
public void GarbageEntryChecksumZeroReturnNull()
294+
public void GarbageEntryChecksumZeroThrowsInvalidDataException()
295295
{
296296
using MemoryStream archiveStream = GetTarMemoryStream(CompressionMethod.Uncompressed, "golang_tar", "issue12435");
297297
using TarReader reader = new TarReader(archiveStream);
298-
Assert.Null(reader.GetNextEntry());
298+
Assert.Throws<InvalidDataException>(() => reader.GetNextEntry());
299+
}
300+
301+
[Fact]
302+
public void InvalidChecksum_ThrowsInvalidDataException()
303+
{
304+
using MemoryStream archiveStream = GetTarMemoryStream(CompressionMethod.Uncompressed, "node-tar", "bad-cksum");
305+
using TarReader reader = new TarReader(archiveStream);
306+
reader.GetNextEntry(); // first entry is okay
307+
Assert.Throws<InvalidDataException>(() => reader.GetNextEntry());
299308
}
300309

301310
[Theory]

src/libraries/System.Formats.Tar/tests/TarTestsBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ public abstract partial class TarTestsBase : FileCleanupTestBase
155155

156156
private static readonly string[] NodeTarTestCaseNames = new[]
157157
{
158-
"bad-cksum",
159158
"body-byte-counts",
160159
"dir",
161160
"emptypax",

src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ public void Verify_Compatibility_RegularFile_EmptyFile_NoSizeStored()
233233
archiveStream.Seek(sizeLocation, SeekOrigin.Begin);
234234
archiveStream.Write(replacement);
235235

236+
// Also fixup checksum
237+
int checksumLocation = 148;
238+
ReadOnlySpan<byte> checksumReplacement = "04116\0\0\0"u8; // 2126 in octal
239+
archiveStream.Seek(checksumLocation, SeekOrigin.Begin);
240+
archiveStream.Write(checksumReplacement);
241+
236242
archiveStream.Position = 0;
237243
using TarReader reader = new(archiveStream);
238244

@@ -368,7 +374,7 @@ private int GetChecksumForCommonFields(TarEntry entry, TarEntryType entryType)
368374
if (entryType is TarEntryType.RegularFile or TarEntryType.V7RegularFile)
369375
{
370376
entry.DataStream = new MemoryStream();
371-
byte[] buffer = [ 72, 101, 108, 108, 111 ]; // values don't matter, only length (5)
377+
byte[] buffer = [72, 101, 108, 108, 111]; // values don't matter, only length (5)
372378

373379
// '00000000005\0' = 48 + 48 + 48 + 48 + 48 + 48 + 48 + 48 + 48 + 48 + 53 + 0 = 533
374380
entry.DataStream.Write(buffer);

0 commit comments

Comments
 (0)