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
44300eb
Throw ArgumentException on unsupported tar entry type
carlossanlop Aug 30, 2022
386ca70
Adjust tests
carlossanlop Aug 30, 2022
4c049be
Also change exception type for internal TarEntry conversion construct…
carlossanlop Aug 30, 2022
712acb5
LinkName setter null check.
carlossanlop Aug 31, 2022
785584d
Internal constructors SeekableSubReadStream and SubReadStream unseeka…
carlossanlop Aug 31, 2022
5931c6b
DataStream setter for regular file should throw ArgumentException if …
carlossanlop Aug 31, 2022
2848e80
TarFile CreateFromDirectory unwritable destination change exception t…
carlossanlop Aug 31, 2022
1647a89
Change to ArgumentException when ExtractToDirectory is an unreadable …
carlossanlop Aug 31, 2022
5216a08
Add some missing exception docs for TarEntry.
carlossanlop Aug 31, 2022
64efec2
Change TarReader constructor exception if unreadable stream. Close te…
carlossanlop Aug 31, 2022
3694af2
Change TarWriter exception for unwritable stream to ArgumentException…
carlossanlop Aug 31, 2022
bc608fc
Add missing documentation for exceptions in constructors.
carlossanlop Aug 31, 2022
b6d620c
Change wording of conversion constructors comment when passing a Pax …
carlossanlop Aug 31, 2022
9c4ccaa
Apply suggestions by Jozkee
carlossanlop Aug 31, 2022
13f52c0
Add exception to LinkName if the entry type is hard/symlink and the u…
carlossanlop Aug 31, 2022
e7d911d
Convert all FormatException to InvalidDataException
carlossanlop Aug 31, 2022
ed4ab9c
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 committed Aug 31, 2022
commit 64efec269b3a0029a396ff85ec197406bbf456be
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();
}
}
}
}