Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
17fdb6e
Throw ArgumentException on unsupported tar entry type
carlossanlop Aug 30, 2022
151797a
Adjust tests
carlossanlop Aug 30, 2022
6b42246
Also change exception type for internal TarEntry conversion construct…
carlossanlop Aug 30, 2022
7b8a909
LinkName setter null check.
carlossanlop Aug 31, 2022
61010d8
Internal constructors SeekableSubReadStream and SubReadStream unseeka…
carlossanlop Aug 31, 2022
f51806e
DataStream setter for regular file should throw ArgumentException if …
carlossanlop Aug 31, 2022
0f9d96c
TarFile CreateFromDirectory unwritable destination change exception t…
carlossanlop Aug 31, 2022
5faad2c
Change to ArgumentException when ExtractToDirectory is an unreadable …
carlossanlop Aug 31, 2022
6094b2f
Add some missing exception docs for TarEntry.
carlossanlop Aug 31, 2022
2775c07
Change TarReader constructor exception if unreadable stream. Close te…
carlossanlop Aug 31, 2022
b116660
Change TarWriter exception for unwritable stream to ArgumentException…
carlossanlop Aug 31, 2022
18292b7
Add missing documentation for exceptions in constructors.
carlossanlop Aug 31, 2022
2f615e9
Change wording of conversion constructors comment when passing a Pax …
carlossanlop Aug 31, 2022
74747ea
Apply suggestions by Jozkee
carlossanlop Aug 31, 2022
3927c72
Add exception to LinkName if the entry type is hard/symlink and the u…
carlossanlop Aug 31, 2022
dd15f10
Convert all FormatException to InvalidDataException
carlossanlop Aug 31, 2022
eb4aebf
Address more suggestions
carlossanlop Aug 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change TarReader constructor exception if unreadable stream. Close te…
…st gap.
  • Loading branch information
carlossanlop authored and github-actions committed Aug 31, 2022
commit 2775c07be6efdcc5a691056e1c7e18d597cfc3aa
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ public sealed class TarReader : IDisposable, IAsyncDisposable
/// </summary>
/// <param name="archiveStream">The stream to read from.</param>
/// <param name="leaveOpen"><see langword="false"/> to dispose the <paramref name="archiveStream"/> when this instance is disposed; <see langword="true"/> to leave the stream open.</param>
/// <exception cref="IOException"><paramref name="archiveStream"/> is unreadable.</exception>
/// <exception cref="ArgumentException"><paramref name="archiveStream"/> is unreadable.</exception>
/// <exception cref="ArgumentNullException"><paramref name="archiveStream"/> is <see langword="null"/>.</exception>
public TarReader(Stream archiveStream, bool leaveOpen = false)
{
ArgumentNullException.ThrowIfNull(archiveStream);

if (!archiveStream.CanRead)
{
throw new IOException(SR.IO_NotSupported_UnreadableStream);
throw new ArgumentException(SR.IO_NotSupported_UnreadableStream, nameof(archiveStream));
}

_archiveStream = archiveStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Compile Include="TarReader\TarReader.TarEntry.ExtractToFile.Tests.cs" />
<Compile Include="TarReader\TarReader.File.Tests.cs" />
<Compile Include="TarReader\TarReader.GetNextEntry.Tests.cs" />
<Compile Include="TarReader\TarReader.Tests.cs" />
<Compile Include="TarTestsBase.cs" />
<Compile Include="TarTestsBase.Gnu.cs" />
<Compile Include="TarTestsBase.Pax.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Xml.Linq;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.IO;
using Xunit;
using System.Linq;

namespace System.Formats.Tar.Tests
{
public class TarReader_Tests : TarTestsBase
{
[Fact]
public void TarReader_NullArchiveStream() => Assert.Throws<ArgumentNullException>(() => new TarReader(archiveStream: null));

[Fact]
public void TarReader_UnreadableStream()
{
using MemoryStream ms = new MemoryStream();
using WrappedStream ws = new WrappedStream(ms, canRead: false, canWrite: true, canSeek: true);
Assert.Throws<ArgumentException>(() => new TarReader(ws));
}

[Fact]
public void TarReader_LeaveOpen_False()
{
using MemoryStream ms = GetTarMemoryStream(CompressionMethod.Uncompressed, TestTarFormat.pax, "many_small_files");
List<Stream> dataStreams = new List<Stream>();
using (TarReader reader = new TarReader(ms, leaveOpen: false))
{
TarEntry entry;
while ((entry = reader.GetNextEntry()) != null)
{
if (entry.DataStream != null)
{
dataStreams.Add(entry.DataStream);
}
}
}

Assert.True(dataStreams.Any());
foreach (Stream ds in dataStreams)
{
Assert.Throws<ObjectDisposedException>(() => ds.ReadByte());
}
}

[Fact]
public void TarReader_LeaveOpen_True()
{
using MemoryStream ms = GetTarMemoryStream(CompressionMethod.Uncompressed, TestTarFormat.pax, "many_small_files");
List<Stream> dataStreams = new List<Stream>();
using (TarReader reader = new TarReader(ms, leaveOpen: true))
{
TarEntry entry;
while ((entry = reader.GetNextEntry()) != null)
{
if (entry.DataStream != null)
{
dataStreams.Add(entry.DataStream);
}
}
}

Assert.True(dataStreams.Any());
foreach (Stream ds in dataStreams)
{
ds.ReadByte(); // Should not throw
ds.Dispose();
}
}

[Fact]
public void TarReader_LeaveOpen_False_CopiedDataNotDisposed()
{
using MemoryStream ms = GetTarMemoryStream(CompressionMethod.Uncompressed, TestTarFormat.pax, "many_small_files");
List<Stream> dataStreams = new List<Stream>();
using (TarReader reader = new TarReader(ms, leaveOpen: false))
{
TarEntry entry;
while ((entry = reader.GetNextEntry(copyData: true)) != null)
{
if (entry.DataStream != null)
{
dataStreams.Add(entry.DataStream);
}
}
}

Assert.True(dataStreams.Any());
foreach (Stream ds in dataStreams)
{
ds.ReadByte(); // Should not throw, copied streams, user should dispose
ds.Dispose();
}
}
}
}