diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/GnuTarEntry.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/GnuTarEntry.cs index eda97e2b8a26f0..11e3b77c5f8f2d 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/GnuTarEntry.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/GnuTarEntry.cs @@ -20,14 +20,16 @@ internal GnuTarEntry(TarHeader header, TarReader readerOfOrigin) /// /// The type of the entry. /// A string with the path and file name of this entry. - /// is null or empty. - /// The entry type is not supported for creating an entry. /// When creating an instance using the constructor, only the following entry types are supported: /// /// In all platforms: , , , . /// In Unix platforms only: , and . /// /// + /// is . + /// is empty. + /// -or- + /// is not supported in the specified format. public GnuTarEntry(TarEntryType entryType, string entryName) : base(entryType, entryName, TarEntryFormat.Gnu, isGea: false) { @@ -38,6 +40,9 @@ public GnuTarEntry(TarEntryType entryType, string entryName) /// /// Initializes a new instance by converting the specified entry into the GNU format. /// + /// is a and cannot be converted. + /// -or- + /// The entry type of is not supported for conversion to the GNU format. public GnuTarEntry(TarEntry other) : base(other, TarEntryFormat.Gnu) { diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/PaxTarEntry.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/PaxTarEntry.cs index db317f7a884112..555e4feaa27f73 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/PaxTarEntry.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/PaxTarEntry.cs @@ -25,8 +25,6 @@ internal PaxTarEntry(TarHeader header, TarReader readerOfOrigin) /// /// The type of the entry. /// A string with the path and file name of this entry. - /// is null or empty. - /// The entry type is not supported for creating an entry. /// When creating an instance using the constructor, only the following entry types are supported: /// /// In all platforms: , , , . @@ -47,6 +45,10 @@ internal PaxTarEntry(TarHeader header, TarReader readerOfOrigin) /// File length, under the name size, as an , if the string representation of the number is larger than 12 bytes. /// /// + /// is . + /// is empty. + /// -or- + /// is not supported in the specified format. public PaxTarEntry(TarEntryType entryType, string entryName) : base(entryType, entryName, TarEntryFormat.Pax, isGea: false) { @@ -62,9 +64,6 @@ public PaxTarEntry(TarEntryType entryType, string entryName) /// The type of the entry. /// A string with the path and file name of this entry. /// An enumeration of string key-value pairs that represents the metadata to include in the Extended Attributes entry that precedes the current entry. - /// is . - /// is null or empty. - /// The entry type is not supported for creating an entry. /// When creating an instance using the constructor, only the following entry types are supported: /// /// In all platforms: , , , . @@ -85,6 +84,10 @@ public PaxTarEntry(TarEntryType entryType, string entryName) /// File length, under the name size, as an , if the string representation of the number is larger than 12 bytes. /// /// + /// or is . + /// is empty. + /// -or- + /// is not supported in the specified format. public PaxTarEntry(TarEntryType entryType, string entryName, IEnumerable> extendedAttributes) : base(entryType, entryName, TarEntryFormat.Pax, isGea: false) { @@ -100,6 +103,9 @@ public PaxTarEntry(TarEntryType entryType, string entryName, IEnumerable /// Initializes a new instance by converting the specified entry into the PAX format. /// + /// is a and cannot be converted. + /// -or- + /// The entry type of is not supported for conversion to the PAX format. public PaxTarEntry(TarEntry other) : base(other, TarEntryFormat.Pax) { diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/SeekableSubReadStream.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/SeekableSubReadStream.cs index e8fe9b7e01a16f..b35c958b4d53f0 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/SeekableSubReadStream.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/SeekableSubReadStream.cs @@ -18,7 +18,7 @@ public SeekableSubReadStream(Stream superStream, long startPosition, long maxLen { if (!superStream.CanSeek) { - throw new InvalidOperationException(SR.IO_NotSupported_UnseekableStream); + throw new ArgumentException(SR.IO_NotSupported_UnseekableStream, nameof(superStream)); } } diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/SubReadStream.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/SubReadStream.cs index 014165939ac1c7..998e53ea6fc99f 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/SubReadStream.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/SubReadStream.cs @@ -25,7 +25,7 @@ public SubReadStream(Stream superStream, long startPosition, long maxLength) { if (!superStream.CanRead) { - throw new InvalidOperationException(SR.IO_NotSupported_UnreadableStream); + throw new ArgumentException(SR.IO_NotSupported_UnreadableStream, nameof(superStream)); } _startInSuperStream = startPosition; _positionInSuperStream = startPosition; diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.cs index 9c2f0553eb39bf..a27df41f4c1c6a 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.cs @@ -51,12 +51,12 @@ internal TarEntry(TarEntry other, TarEntryFormat format) { if (other is PaxGlobalExtendedAttributesTarEntry) { - throw new InvalidOperationException(SR.TarCannotConvertPaxGlobalExtendedAttributesEntry); + throw new ArgumentException(SR.TarCannotConvertPaxGlobalExtendedAttributesEntry, nameof(other)); } TarEntryType compatibleEntryType = TarHelpers.GetCorrectTypeFlagForFormat(format, other.EntryType); - TarHelpers.ThrowIfEntryTypeNotSupported(compatibleEntryType, format); + TarHelpers.ThrowIfEntryTypeNotSupported(compatibleEntryType, format, nameof(other)); _readerOfOrigin = other._readerOfOrigin; @@ -92,6 +92,7 @@ public int Gid /// A timestamps that represents the last time the contents of the file represented by this entry were modified. /// /// In Unix platforms, this timestamp is commonly known as mtime. + /// The specified value is larger than . public DateTimeOffset ModificationTime { get => _header._mTime; @@ -114,7 +115,9 @@ public DateTimeOffset ModificationTime /// /// When the indicates a or a , this property returns the link target path of such link. /// - /// Cannot set the link name if the entry type is not or . + /// The entry type is not or . + /// The specified value is . + /// The specified value is empty. public string LinkName { get => _header._linkName ?? string.Empty; @@ -124,6 +127,7 @@ public string LinkName { throw new InvalidOperationException(SR.TarEntryHardLinkOrSymLinkExpected); } + ArgumentException.ThrowIfNullOrEmpty(value); _header._linkName = value; } } @@ -177,7 +181,8 @@ public int Uid /// Elevation is required to extract a or to disk. /// Symbolic links can be recreated using , or . /// Hard links can only be extracted when using or . - /// is or empty. + /// is . + /// is empty. /// The parent directory of does not exist. /// -or- /// is and a file already exists in . @@ -206,7 +211,8 @@ public void ExtractToFile(string destinationFileName, bool overwrite) /// A task that represents the asynchronous extraction operation. /// Files of type , or can only be extracted in Unix platforms. /// Elevation is required to extract a or to disk. - /// is or empty. + /// is . + /// is empty. /// The parent directory of does not exist. /// -or- /// is and a file already exists in . @@ -237,9 +243,8 @@ public Task ExtractToFileAsync(string destinationFileName, bool overwrite, Cance /// Sets a new stream that represents the data section, if it makes sense for the to contain data; if a stream already existed, the old stream gets disposed before substituting it with the new stream. Setting a stream is allowed. /// If you write data to this data stream, make sure to rewind it to the desired start position before writing this entry into an archive using . /// Setting a data section is not supported because the is not (or for an archive of format). - /// Cannot set an unreadable stream. - /// -or- - /// An I/O problem occurred. + /// Cannot set an unreadable stream. + /// An I/O problem occurred. public Stream? DataStream { get => _header._dataStream; @@ -252,7 +257,7 @@ public Stream? DataStream if (value != null && !value.CanRead) { - throw new IOException(SR.IO_NotSupported_UnreadableStream); + throw new ArgumentException(SR.IO_NotSupported_UnreadableStream, nameof(value)); } if (_readerOfOrigin != null) @@ -339,7 +344,7 @@ internal Task ExtractRelativeToDirectoryAsync(string destinationDirectoryPath, b { if (string.IsNullOrEmpty(LinkName)) { - throw new FormatException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty); + throw new InvalidDataException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty); } linkTargetPath = GetSanitizedFullPath(destinationDirectoryPath, LinkName); @@ -511,7 +516,7 @@ private void VerifyPathsForEntryType(string filePath, string? linkTargetPath, bo } else { - throw new FormatException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty); + throw new InvalidDataException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty); } } } diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarFile.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarFile.cs index 8d8902f5806d59..2ed7865a2e13e8 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarFile.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarFile.cs @@ -22,6 +22,12 @@ public static class TarFile /// The path of the directory to archive. /// The destination stream the archive. /// to include the base directory name as the first segment in all the names of the archive entries. to exclude the base directory name from the archive entry names. + /// or is . + /// is empty. + /// -or- + /// does not support writing. + /// The directory path was not found. + /// An I/O exception occurred. public static void CreateFromDirectory(string sourceDirectoryName, Stream destination, bool includeBaseDirectory) { ArgumentException.ThrowIfNullOrEmpty(sourceDirectoryName); @@ -29,7 +35,7 @@ public static void CreateFromDirectory(string sourceDirectoryName, Stream destin if (!destination.CanWrite) { - throw new IOException(SR.IO_NotSupported_UnwritableStream); + throw new ArgumentException(SR.IO_NotSupported_UnwritableStream, nameof(destination)); } if (!Directory.Exists(sourceDirectoryName)) @@ -51,6 +57,12 @@ public static void CreateFromDirectory(string sourceDirectoryName, Stream destin /// to include the base directory name as the first path segment in all the names of the archive entries. to exclude the base directory name from the entry name paths. /// The token to monitor for cancellation requests. The default value is . /// A task that represents the asynchronous creation operation. + /// or is . + /// is empty. + /// -or- + /// does not support writing. + /// The directory path was not found. + /// An I/O exception occurred. public static Task CreateFromDirectoryAsync(string sourceDirectoryName, Stream destination, bool includeBaseDirectory, CancellationToken cancellationToken = default) { if (cancellationToken.IsCancellationRequested) @@ -62,7 +74,7 @@ public static Task CreateFromDirectoryAsync(string sourceDirectoryName, Stream d if (!destination.CanWrite) { - return Task.FromException(new IOException(SR.IO_NotSupported_UnwritableStream)); + return Task.FromException(new ArgumentException(SR.IO_NotSupported_UnwritableStream, nameof(destination))); } if (!Directory.Exists(sourceDirectoryName)) @@ -82,6 +94,10 @@ public static Task CreateFromDirectoryAsync(string sourceDirectoryName, Stream d /// The path of the directory to archive. /// The path of the destination archive file. /// to include the base directory name as the first path segment in all the names of the archive entries. to exclude the base directory name from the entry name paths. + /// or is . + /// or is empty. + /// The directory path was not found. + /// An I/O exception occurred. public static void CreateFromDirectory(string sourceDirectoryName, string destinationFileName, bool includeBaseDirectory) { ArgumentException.ThrowIfNullOrEmpty(sourceDirectoryName); @@ -110,6 +126,10 @@ public static void CreateFromDirectory(string sourceDirectoryName, string destin /// to include the base directory name as the first path segment in all the names of the archive entries. to exclude the base directory name from the entry name paths. /// The token to monitor for cancellation requests. The default value is . /// A task that represents the asynchronous creation operation. + /// or is . + /// or is empty. + /// The directory path was not found. + /// An I/O exception occurred. public static Task CreateFromDirectoryAsync(string sourceDirectoryName, string destinationFileName, bool includeBaseDirectory, CancellationToken cancellationToken = default) { if (cancellationToken.IsCancellationRequested) @@ -139,8 +159,15 @@ public static Task CreateFromDirectoryAsync(string sourceDirectoryName, string d /// to overwrite files and directories in ; to avoid overwriting, and throw if any files or directories are found with existing names. /// Files of type , or can only be extracted in Unix platforms. /// Elevation is required to extract a or to disk. + /// or is . + /// The directory path was not found. /// Operation not permitted due to insufficient permissions. - /// Extracting tar entry would have resulted in a file outside the specified destination directory. + /// Extracting tar entry would have resulted in a file outside the specified destination directory. + /// -or- + /// is empty. + /// -or- + /// does not support reading. + /// An I/O exception occurred. public static void ExtractToDirectory(Stream source, string destinationDirectoryName, bool overwriteFiles) { ArgumentNullException.ThrowIfNull(source); @@ -148,7 +175,7 @@ public static void ExtractToDirectory(Stream source, string destinationDirectory if (!source.CanRead) { - throw new IOException(SR.IO_NotSupported_UnreadableStream); + throw new ArgumentException(SR.IO_NotSupported_UnreadableStream, nameof(source)); } if (!Directory.Exists(destinationDirectoryName)) @@ -172,7 +199,15 @@ public static void ExtractToDirectory(Stream source, string destinationDirectory /// A task that represents the asynchronous extraction operation. /// Files of type , or can only be extracted in Unix platforms. /// Elevation is required to extract a or to disk. + /// or is . + /// The directory path was not found. /// Operation not permitted due to insufficient permissions. + /// Extracting tar entry would have resulted in a file outside the specified destination directory. + /// -or- + /// is empty. + /// -or- + /// does not support reading. + /// An I/O exception occurred. public static Task ExtractToDirectoryAsync(Stream source, string destinationDirectoryName, bool overwriteFiles, CancellationToken cancellationToken = default) { if (cancellationToken.IsCancellationRequested) @@ -184,7 +219,7 @@ public static Task ExtractToDirectoryAsync(Stream source, string destinationDire if (!source.CanRead) { - return Task.FromException(new IOException(SR.IO_NotSupported_UnreadableStream)); + return Task.FromException(new ArgumentException(SR.IO_NotSupported_UnreadableStream, nameof(source))); } if (!Directory.Exists(destinationDirectoryName)) @@ -206,7 +241,14 @@ public static Task ExtractToDirectoryAsync(Stream source, string destinationDire /// to overwrite files and directories in ; to avoid overwriting, and throw if any files or directories are found with existing names. /// Files of type , or can only be extracted in Unix platforms. /// Elevation is required to extract a or to disk. + /// or is . + /// The directory path was not found. + /// The file path was not found. /// Operation not permitted due to insufficient permissions. + /// Extracting tar entry would have resulted in a file outside the specified destination directory. + /// -or- + /// or is empty. + /// An I/O exception occurred. public static void ExtractToDirectory(string sourceFileName, string destinationDirectoryName, bool overwriteFiles) { ArgumentException.ThrowIfNullOrEmpty(sourceFileName); @@ -241,7 +283,14 @@ public static void ExtractToDirectory(string sourceFileName, string destinationD /// A task that represents the asynchronous extraction operation. /// Files of type , or can only be extracted in Unix platforms. /// Elevation is required to extract a or to disk. + /// or is . + /// The directory path was not found. + /// The file path was not found. /// Operation not permitted due to insufficient permissions. + /// Extracting tar entry would have resulted in a file outside the specified destination directory. + /// -or- + /// or is empty. + /// An I/O exception occurred. public static Task ExtractToDirectoryAsync(string sourceFileName, string destinationDirectoryName, bool overwriteFiles, CancellationToken cancellationToken = default) { if (cancellationToken.IsCancellationRequested) diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs index 5578707d64eb18..ce37a74c304c80 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs @@ -201,7 +201,7 @@ internal void ProcessDataBlock(Stream archiveStream, bool copyData) // No data section if (_size > 0) { - throw new FormatException(string.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag)); + throw new InvalidDataException(string.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag)); } break; case TarEntryType.RegularFile: @@ -263,7 +263,7 @@ private async Task ProcessDataBlockAsync(Stream archiveStream, bool copyData, Ca // No data section if (_size > 0) { - throw new FormatException(string.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag)); + throw new InvalidDataException(string.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag)); } break; case TarEntryType.RegularFile: @@ -379,7 +379,7 @@ private async Task ProcessDataBlockAsync(Stream archiveStream, bool copyData, Ca long size = (int)TarHelpers.ParseOctal(buffer.Slice(FieldLocations.Size, FieldLengths.Size)); if (size < 0) { - throw new FormatException(string.Format(SR.TarSizeFieldNegative)); + throw new InvalidDataException(string.Format(SR.TarSizeFieldNegative)); } // Continue with the rest of the fields that require no special checks @@ -477,7 +477,7 @@ private void ReadVersionAttribute(Span buffer) // Check for gnu version header for mixed case if (!version.SequenceEqual(GnuVersionBytes)) { - throw new FormatException(string.Format(SR.TarPosixFormatExpected, _name)); + throw new InvalidDataException(string.Format(SR.TarPosixFormatExpected, _name)); } _version = GnuVersion; @@ -495,7 +495,7 @@ private void ReadVersionAttribute(Span buffer) // Check for ustar or pax version header for mixed case if (!version.SequenceEqual(UstarVersionBytes)) { - throw new FormatException(string.Format(SR.TarGnuFormatExpected, _name)); + throw new InvalidDataException(string.Format(SR.TarGnuFormatExpected, _name)); } _version = UstarVersion; @@ -626,7 +626,7 @@ private void ReadExtendedAttributesFromBuffer(ReadOnlySpan buffer, string { if (!ExtendedAttributes.TryAdd(key, value)) { - throw new FormatException(string.Format(SR.TarDuplicateExtendedAttribute, name)); + throw new InvalidDataException(string.Format(SR.TarDuplicateExtendedAttribute, name)); } } } diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHelpers.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHelpers.cs index c2a1b3b854c14b..fb12a2d3faa74e 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHelpers.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHelpers.cs @@ -7,6 +7,7 @@ using System.Globalization; using System.IO; using System.Numerics; +using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -228,7 +229,7 @@ internal static T ParseOctal(ReadOnlySpan buffer) where T : struct, INu [DoesNotReturn] private static void ThrowInvalidNumber() => - throw new FormatException(SR.Format(SR.TarInvalidNumber)); + throw new InvalidDataException(SR.Format(SR.TarInvalidNumber)); // Returns the string contained in the specified buffer of bytes, // in the specified encoding, removing the trailing null or space chars. @@ -291,7 +292,7 @@ internal static async ValueTask SkipBlockAlignmentPaddingAsync(Stream archi } // Throws if the specified entry type is not supported for the specified format. - internal static void ThrowIfEntryTypeNotSupported(TarEntryType entryType, TarEntryFormat archiveFormat) + internal static void ThrowIfEntryTypeNotSupported(TarEntryType entryType, TarEntryFormat archiveFormat, [CallerArgumentExpression("entryType")] string? paramName = null) { switch (archiveFormat) { @@ -365,10 +366,10 @@ TarEntryType.RegularFile or case TarEntryFormat.Unknown: default: - throw new FormatException(string.Format(SR.TarInvalidFormat, archiveFormat)); + throw new InvalidDataException(string.Format(SR.TarInvalidFormat, archiveFormat)); } - throw new InvalidOperationException(string.Format(SR.TarEntryTypeNotSupportedInFormat, entryType, archiveFormat)); + throw new ArgumentException(string.Format(SR.TarEntryTypeNotSupportedInFormat, entryType, archiveFormat), paramName); } } } diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs index 5328d61190280b..ae815dc0073b46 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs @@ -27,15 +27,16 @@ public sealed class TarReader : IDisposable, IAsyncDisposable /// Initializes a instance that can read tar entries from the specified stream, and can optionally leave the stream open upon disposal of this instance. /// /// The stream to read from. - /// to dispose the when this instance is disposed; to leave the stream open. - /// is unreadable. + /// to dispose the when this instance is disposed, as well as all the non-null instances from the entries that were visited by this reader; to leave all the streams open. + /// does not support reading. + /// is . 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; @@ -47,7 +48,7 @@ public TarReader(Stream archiveStream, bool leaveOpen = false) } /// - /// Disposes the current instance, and disposes the streams of all the entries that were read from the archive. + /// Disposes the current instance, and disposes the non-null instances of all the entries that were read from the archive. /// /// The property of any entry can be replaced with a new stream. If the user decides to replace it on a instance that was obtained using a , the underlying stream gets disposed immediately, freeing the of origin from the responsibility of having to dispose it. public void Dispose() @@ -67,7 +68,7 @@ public void Dispose() } /// - /// Asynchronously disposes the current instance, and disposes the streams of all the entries that were read from the archive. + /// Asynchronously disposes the current instance, and disposes the non-null instances of all the entries that were read from the archive. /// /// The property of any entry can be replaced with a new stream. If the user decides to replace it on a instance that was obtained using a , the underlying stream gets disposed immediately, freeing the of origin from the responsibility of having to dispose it. public async ValueTask DisposeAsync() @@ -93,7 +94,7 @@ public async ValueTask DisposeAsync() /// Set it to if the data should not be copied into a new stream. If the underlying stream is unseekable, the user has the responsibility of reading and processing the immediately after calling this method. /// The default value is . /// A instance if a valid entry was found, or if the end of the archive has been reached. - /// The archive is malformed. + /// The entry's data is malformed. /// -or- /// The archive contains entries in different formats. /// -or- @@ -153,7 +154,7 @@ public async ValueTask DisposeAsync() /// The default value is . /// The token to monitor for cancellation requests. The default value is . /// A value task containing a instance if a valid entry was found, or if the end of the archive has been reached. - /// The archive is malformed. + /// The archive is malformed. /// -or- /// The archive contains entries in different formats. /// -or- @@ -400,7 +401,7 @@ TarEntryType.ExtendedAttributes or TarEntryType.LongLink or TarEntryType.LongPath) { - throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, actualHeader._typeFlag, TarEntryType.ExtendedAttributes)); + throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, actualHeader._typeFlag, TarEntryType.ExtendedAttributes)); } // Replace all the attributes representing standard fields with the extended ones, if any @@ -432,13 +433,13 @@ TarEntryType.ExtendedAttributes or TarEntryType.LongLink or TarEntryType.LongPath) { - throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, actualHeader._typeFlag, TarEntryType.ExtendedAttributes)); + throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, actualHeader._typeFlag, TarEntryType.ExtendedAttributes)); } // Can't have two extended attribute metadata entries in a row if (actualHeader._typeFlag is TarEntryType.ExtendedAttributes) { - throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, TarEntryType.ExtendedAttributes, TarEntryType.ExtendedAttributes)); + throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, TarEntryType.ExtendedAttributes, TarEntryType.ExtendedAttributes)); } // Replace all the attributes representing standard fields with the extended ones, if any @@ -467,7 +468,7 @@ private bool TryProcessGnuMetadataHeader(TarHeader header, bool copyData, out Ta // Can't have two identical metadata entries in a row if (secondHeader._typeFlag == header._typeFlag) { - throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, secondHeader._typeFlag, header._typeFlag)); + throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, secondHeader._typeFlag, header._typeFlag)); } // It's possible to have the two different metadata entries in a row @@ -485,7 +486,7 @@ private bool TryProcessGnuMetadataHeader(TarHeader header, bool copyData, out Ta // Can't have three GNU metadata entries in a row if (thirdHeader._typeFlag is TarEntryType.LongLink or TarEntryType.LongPath) { - throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, thirdHeader._typeFlag, secondHeader._typeFlag)); + throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, thirdHeader._typeFlag, secondHeader._typeFlag)); } if (header._typeFlag is TarEntryType.LongLink) @@ -542,7 +543,7 @@ private bool TryProcessGnuMetadataHeader(TarHeader header, bool copyData, out Ta // Can't have two identical metadata entries in a row if (secondHeader._typeFlag == header._typeFlag) { - throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, secondHeader._typeFlag, header._typeFlag)); + throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, secondHeader._typeFlag, header._typeFlag)); } TarHeader finalHeader; @@ -561,7 +562,7 @@ private bool TryProcessGnuMetadataHeader(TarHeader header, bool copyData, out Ta // Can't have three GNU metadata entries in a row if (thirdHeader._typeFlag is TarEntryType.LongLink or TarEntryType.LongPath) { - throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, thirdHeader._typeFlag, secondHeader._typeFlag)); + throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, thirdHeader._typeFlag, secondHeader._typeFlag)); } if (header._typeFlag is TarEntryType.LongLink) diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.Unix.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.Unix.cs index 28eaf1e3323237..a691582178df6a 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.Unix.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.Unix.cs @@ -46,7 +46,7 @@ private TarEntry ConstructEntryForWriting(string fullPath, string entryName, Fil TarEntryFormat.Ustar => new UstarTarEntry(entryType, entryName), TarEntryFormat.Pax => new PaxTarEntry(entryType, entryName), TarEntryFormat.Gnu => new GnuTarEntry(entryType, entryName), - _ => throw new FormatException(string.Format(SR.TarInvalidFormat, Format)), + _ => throw new InvalidDataException(string.Format(SR.TarInvalidFormat, Format)), }; if (entryType is TarEntryType.BlockDevice or TarEntryType.CharacterDevice) diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.Windows.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.Windows.cs index d3fa19a5b12eac..7452246f742ed6 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.Windows.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.Windows.cs @@ -45,7 +45,7 @@ private TarEntry ConstructEntryForWriting(string fullPath, string entryName, Fil TarEntryFormat.Ustar => new UstarTarEntry(entryType, entryName), TarEntryFormat.Pax => new PaxTarEntry(entryType, entryName), TarEntryFormat.Gnu => new GnuTarEntry(entryType, entryName), - _ => throw new FormatException(string.Format(SR.TarInvalidFormat, Format)), + _ => throw new InvalidDataException(string.Format(SR.TarInvalidFormat, Format)), }; FileSystemInfo info = (attributes & FileAttributes.Directory) != 0 ? new DirectoryInfo(fullPath) : new FileInfo(fullPath); diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.cs index 7d3977eda68df4..b2d144a0bf6a7d 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.cs @@ -25,6 +25,8 @@ public sealed partial class TarWriter : IDisposable, IAsyncDisposable /// /// The stream to write to. /// When using this constructor, is used as the default format of the entries written to the archive using the method. + /// is . + /// does not support writing. public TarWriter(Stream archiveStream) : this(archiveStream, TarEntryFormat.Pax, leaveOpen: false) { @@ -35,6 +37,8 @@ public TarWriter(Stream archiveStream) /// /// The stream to write to. /// to dispose the when this instance is disposed; to leave the stream open. + /// is . + /// is unwritable. public TarWriter(Stream archiveStream, bool leaveOpen = false) : this(archiveStream, TarEntryFormat.Pax, leaveOpen) { @@ -50,7 +54,7 @@ public TarWriter(Stream archiveStream, bool leaveOpen = false) /// to leave the stream open. The default is . /// The recommended format is for its flexibility. /// is . - /// is unwritable. + /// is unwritable. /// is either , or not one of the other enum values. public TarWriter(Stream archiveStream, TarEntryFormat format = TarEntryFormat.Pax, bool leaveOpen = false) { @@ -58,7 +62,7 @@ public TarWriter(Stream archiveStream, TarEntryFormat format = TarEntryFormat.Pa if (!archiveStream.CanWrite) { - throw new IOException(SR.IO_NotSupported_UnwritableStream); + throw new ArgumentException(SR.IO_NotSupported_UnwritableStream); } if (format is not TarEntryFormat.V7 and not TarEntryFormat.Ustar and not TarEntryFormat.Pax and not TarEntryFormat.Gnu) @@ -210,7 +214,7 @@ private async Task ReadFileFromDiskAndWriteToArchiveStreamAsEntryAsync(string fu /// /// /// The archive stream is disposed. - /// The entry type of the is not supported for writing. + /// is . /// An I/O problem occurred. public void WriteEntry(TarEntry entry) { @@ -251,7 +255,7 @@ public void WriteEntry(TarEntry entry) /// /// /// The archive stream is disposed. - /// The entry type of the is not supported for writing. + /// is . /// An I/O problem occurred. public Task WriteEntryAsync(TarEntry entry, CancellationToken cancellationToken = default) { @@ -298,7 +302,7 @@ private void WriteEntryInternal(TarEntry entry) default: Debug.Assert(entry.Format == TarEntryFormat.Unknown, "Missing format handler"); - throw new FormatException(string.Format(SR.TarInvalidFormat, Format)); + throw new InvalidDataException(string.Format(SR.TarInvalidFormat, Format)); } _wroteEntries = true; @@ -320,7 +324,7 @@ private async Task WriteEntryAsyncInternal(TarEntry entry, CancellationToken can TarEntryFormat.Pax when entry._header._typeFlag is TarEntryType.GlobalExtendedAttributes => entry._header.WriteAsPaxGlobalExtendedAttributesAsync(_archiveStream, buffer, _nextGlobalExtendedAttributesEntryNumber++, cancellationToken), TarEntryFormat.Pax => entry._header.WriteAsPaxAsync(_archiveStream, buffer, cancellationToken), TarEntryFormat.Gnu => entry._header.WriteAsGnuAsync(_archiveStream, buffer, cancellationToken), - _ => throw new FormatException(string.Format(SR.TarInvalidFormat, Format)), + _ => throw new InvalidDataException(string.Format(SR.TarInvalidFormat, Format)), }; await task.ConfigureAwait(false); diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/UstarTarEntry.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/UstarTarEntry.cs index 10c5aad7325ade..9969520d96551c 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/UstarTarEntry.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/UstarTarEntry.cs @@ -19,14 +19,16 @@ internal UstarTarEntry(TarHeader header, TarReader readerOfOrigin) /// /// The type of the entry. /// A string with the path and file name of this entry. - /// is null or empty. - /// The entry type is not supported for creating an entry. /// When creating an instance using the constructor, only the following entry types are supported: /// /// In all platforms: , , , . /// In Unix platforms only: , and . /// /// + /// is . + /// is empty. + /// -or- + /// is not supported in the specified format. public UstarTarEntry(TarEntryType entryType, string entryName) : base(entryType, entryName, TarEntryFormat.Ustar, isGea: false) { @@ -36,6 +38,9 @@ public UstarTarEntry(TarEntryType entryType, string entryName) /// /// Initializes a new instance by converting the specified entry into the Ustar format. /// + /// is a and cannot be converted. + /// -or- + /// The entry type of is not supported for conversion to the Ustar format. public UstarTarEntry(TarEntry other) : base(other, TarEntryFormat.Ustar) { diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/V7TarEntry.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/V7TarEntry.cs index fd049c05d90c93..4b20f10faf4beb 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/V7TarEntry.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/V7TarEntry.cs @@ -19,9 +19,11 @@ internal V7TarEntry(TarHeader header, TarReader readerOfOrigin) /// /// The type of the entry. /// A string with the path and file name of this entry. - /// is null or empty. - /// The entry type is not supported for creating an entry. /// When creating an instance using the constructor, only the following entry types are supported: , , and . + /// is . + /// is empty. + /// -or- + /// is not supported for creating an entry. public V7TarEntry(TarEntryType entryType, string entryName) : base(entryType, entryName, TarEntryFormat.V7, isGea: false) { @@ -30,6 +32,9 @@ public V7TarEntry(TarEntryType entryType, string entryName) /// /// Initializes a new instance by converting the specified entry into the V7 format. /// + /// is a and cannot be converted. + /// -or- + /// The entry type of is not supported for conversion to the V7 format. public V7TarEntry(TarEntry other) : base(other, TarEntryFormat.V7) { diff --git a/src/libraries/System.Formats.Tar/tests/System.Formats.Tar.Tests.csproj b/src/libraries/System.Formats.Tar/tests/System.Formats.Tar.Tests.csproj index f06e639b74f564..c43c8dff343f23 100644 --- a/src/libraries/System.Formats.Tar/tests/System.Formats.Tar.Tests.csproj +++ b/src/libraries/System.Formats.Tar/tests/System.Formats.Tar.Tests.csproj @@ -40,6 +40,7 @@ + diff --git a/src/libraries/System.Formats.Tar/tests/TarEntry/GnuTarEntry.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarEntry/GnuTarEntry.Tests.cs index e9bb630dd3f873..4c13273a30ceaf 100644 --- a/src/libraries/System.Formats.Tar/tests/TarEntry/GnuTarEntry.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarEntry/GnuTarEntry.Tests.cs @@ -19,23 +19,23 @@ public void Constructor_InvalidEntryName() [Fact] public void Constructor_UnsupportedEntryTypes() { - Assert.Throws(() => new GnuTarEntry((TarEntryType)byte.MaxValue, InitialEntryName)); + Assert.Throws(() => new GnuTarEntry((TarEntryType)byte.MaxValue, InitialEntryName)); - Assert.Throws(() => new GnuTarEntry(TarEntryType.ExtendedAttributes, InitialEntryName)); - Assert.Throws(() => new GnuTarEntry(TarEntryType.GlobalExtendedAttributes, InitialEntryName)); - Assert.Throws(() => new GnuTarEntry(TarEntryType.V7RegularFile, InitialEntryName)); + Assert.Throws(() => new GnuTarEntry(TarEntryType.ExtendedAttributes, InitialEntryName)); + Assert.Throws(() => new GnuTarEntry(TarEntryType.GlobalExtendedAttributes, InitialEntryName)); + Assert.Throws(() => new GnuTarEntry(TarEntryType.V7RegularFile, InitialEntryName)); // These are specific to GNU, but currently the user cannot create them manually - Assert.Throws(() => new GnuTarEntry(TarEntryType.ContiguousFile, InitialEntryName)); - Assert.Throws(() => new GnuTarEntry(TarEntryType.DirectoryList, InitialEntryName)); - Assert.Throws(() => new GnuTarEntry(TarEntryType.MultiVolume, InitialEntryName)); - Assert.Throws(() => new GnuTarEntry(TarEntryType.RenamedOrSymlinked, InitialEntryName)); - Assert.Throws(() => new GnuTarEntry(TarEntryType.SparseFile, InitialEntryName)); - Assert.Throws(() => new GnuTarEntry(TarEntryType.TapeVolume, InitialEntryName)); + Assert.Throws(() => new GnuTarEntry(TarEntryType.ContiguousFile, InitialEntryName)); + Assert.Throws(() => new GnuTarEntry(TarEntryType.DirectoryList, InitialEntryName)); + Assert.Throws(() => new GnuTarEntry(TarEntryType.MultiVolume, InitialEntryName)); + Assert.Throws(() => new GnuTarEntry(TarEntryType.RenamedOrSymlinked, InitialEntryName)); + Assert.Throws(() => new GnuTarEntry(TarEntryType.SparseFile, InitialEntryName)); + Assert.Throws(() => new GnuTarEntry(TarEntryType.TapeVolume, InitialEntryName)); // The user should not create these entries manually - Assert.Throws(() => new GnuTarEntry(TarEntryType.LongLink, InitialEntryName)); - Assert.Throws(() => new GnuTarEntry(TarEntryType.LongPath, InitialEntryName)); + Assert.Throws(() => new GnuTarEntry(TarEntryType.LongLink, InitialEntryName)); + Assert.Throws(() => new GnuTarEntry(TarEntryType.LongPath, InitialEntryName)); } [Fact] diff --git a/src/libraries/System.Formats.Tar/tests/TarEntry/PaxTarEntry.Conversion.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarEntry/PaxTarEntry.Conversion.Tests.cs index 8125e45708eb30..e42f1df0ea6ea1 100644 --- a/src/libraries/System.Formats.Tar/tests/TarEntry/PaxTarEntry.Conversion.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarEntry/PaxTarEntry.Conversion.Tests.cs @@ -61,6 +61,15 @@ public class PaxTarEntry_Conversion_Tests : TarTestsConversionBase [Fact] public void Constructor_ConversionFromGnu_CharacterDevice() => TestConstructionConversion(TarEntryType.CharacterDevice, TarEntryFormat.Gnu, TarEntryFormat.Pax); + [Fact] + public void Constructor_ConversionFromPaxGEA_ToAny_Throw() + { + Assert.Throws(() => new V7TarEntry(new PaxGlobalExtendedAttributesTarEntry(new Dictionary()))); + Assert.Throws(() => new UstarTarEntry(new PaxGlobalExtendedAttributesTarEntry(new Dictionary()))); + Assert.Throws(() => new PaxTarEntry(new PaxGlobalExtendedAttributesTarEntry(new Dictionary()))); + Assert.Throws(() => new GnuTarEntry(new PaxGlobalExtendedAttributesTarEntry(new Dictionary()))); + } + [Theory] [InlineData(TarEntryFormat.V7)] [InlineData(TarEntryFormat.Ustar)] diff --git a/src/libraries/System.Formats.Tar/tests/TarEntry/PaxTarEntry.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarEntry/PaxTarEntry.Tests.cs index 69e028ae883aa2..0e8bcc952cea5d 100644 --- a/src/libraries/System.Formats.Tar/tests/TarEntry/PaxTarEntry.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarEntry/PaxTarEntry.Tests.cs @@ -19,21 +19,21 @@ public void Constructor_InvalidEntryName() [Fact] public void Constructor_UnsupportedEntryTypes() { - Assert.Throws(() => new PaxTarEntry((TarEntryType)byte.MaxValue, InitialEntryName)); + Assert.Throws(() => new PaxTarEntry((TarEntryType)byte.MaxValue, InitialEntryName)); - Assert.Throws(() => new PaxTarEntry(TarEntryType.ContiguousFile, InitialEntryName)); - Assert.Throws(() => new PaxTarEntry(TarEntryType.DirectoryList, InitialEntryName)); - Assert.Throws(() => new PaxTarEntry(TarEntryType.LongLink, InitialEntryName)); - Assert.Throws(() => new PaxTarEntry(TarEntryType.LongPath, InitialEntryName)); - Assert.Throws(() => new PaxTarEntry(TarEntryType.MultiVolume, InitialEntryName)); - Assert.Throws(() => new PaxTarEntry(TarEntryType.V7RegularFile, InitialEntryName)); - Assert.Throws(() => new PaxTarEntry(TarEntryType.RenamedOrSymlinked, InitialEntryName)); - Assert.Throws(() => new PaxTarEntry(TarEntryType.SparseFile, InitialEntryName)); - Assert.Throws(() => new PaxTarEntry(TarEntryType.TapeVolume, InitialEntryName)); + Assert.Throws(() => new PaxTarEntry(TarEntryType.ContiguousFile, InitialEntryName)); + Assert.Throws(() => new PaxTarEntry(TarEntryType.DirectoryList, InitialEntryName)); + Assert.Throws(() => new PaxTarEntry(TarEntryType.LongLink, InitialEntryName)); + Assert.Throws(() => new PaxTarEntry(TarEntryType.LongPath, InitialEntryName)); + Assert.Throws(() => new PaxTarEntry(TarEntryType.MultiVolume, InitialEntryName)); + Assert.Throws(() => new PaxTarEntry(TarEntryType.V7RegularFile, InitialEntryName)); + Assert.Throws(() => new PaxTarEntry(TarEntryType.RenamedOrSymlinked, InitialEntryName)); + Assert.Throws(() => new PaxTarEntry(TarEntryType.SparseFile, InitialEntryName)); + Assert.Throws(() => new PaxTarEntry(TarEntryType.TapeVolume, InitialEntryName)); // The user should not be creating these entries manually in pax - Assert.Throws(() => new PaxTarEntry(TarEntryType.ExtendedAttributes, InitialEntryName)); - Assert.Throws(() => new PaxTarEntry(TarEntryType.GlobalExtendedAttributes, InitialEntryName)); + Assert.Throws(() => new PaxTarEntry(TarEntryType.ExtendedAttributes, InitialEntryName)); + Assert.Throws(() => new PaxTarEntry(TarEntryType.GlobalExtendedAttributes, InitialEntryName)); } [Fact] diff --git a/src/libraries/System.Formats.Tar/tests/TarEntry/TarEntry.Conversion.Tests.Base.cs b/src/libraries/System.Formats.Tar/tests/TarEntry/TarEntry.Conversion.Tests.Base.cs index 6626ca958f25d4..9fa5e57d460c19 100644 --- a/src/libraries/System.Formats.Tar/tests/TarEntry/TarEntry.Conversion.Tests.Base.cs +++ b/src/libraries/System.Formats.Tar/tests/TarEntry/TarEntry.Conversion.Tests.Base.cs @@ -184,7 +184,7 @@ protected TarEntry InvokeTarEntryConversionConstructor(TarEntryFormat targetForm TarEntryFormat.Ustar => new UstarTarEntry(other), TarEntryFormat.Pax => new PaxTarEntry(other), TarEntryFormat.Gnu => new GnuTarEntry(other), - _ => throw new FormatException($"Unexpected format: {targetFormat}") + _ => throw new InvalidDataException($"Unexpected format: {targetFormat}") }; } } diff --git a/src/libraries/System.Formats.Tar/tests/TarEntry/UstarTarEntry.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarEntry/UstarTarEntry.Tests.cs index 8087621f57ae1d..2fdcb34069e50b 100644 --- a/src/libraries/System.Formats.Tar/tests/TarEntry/UstarTarEntry.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarEntry/UstarTarEntry.Tests.cs @@ -19,19 +19,19 @@ public void Constructor_InvalidEntryName() [Fact] public void Constructor_UnsupportedEntryTypes() { - Assert.Throws(() => new UstarTarEntry((TarEntryType)byte.MaxValue, InitialEntryName)); + Assert.Throws(() => new UstarTarEntry((TarEntryType)byte.MaxValue, InitialEntryName)); - Assert.Throws(() => new UstarTarEntry(TarEntryType.ContiguousFile, InitialEntryName)); - Assert.Throws(() => new UstarTarEntry(TarEntryType.DirectoryList, InitialEntryName)); - Assert.Throws(() => new UstarTarEntry(TarEntryType.ExtendedAttributes, InitialEntryName)); - Assert.Throws(() => new UstarTarEntry(TarEntryType.GlobalExtendedAttributes, InitialEntryName)); - Assert.Throws(() => new UstarTarEntry(TarEntryType.LongLink, InitialEntryName)); - Assert.Throws(() => new UstarTarEntry(TarEntryType.LongPath, InitialEntryName)); - Assert.Throws(() => new UstarTarEntry(TarEntryType.MultiVolume, InitialEntryName)); - Assert.Throws(() => new UstarTarEntry(TarEntryType.V7RegularFile, InitialEntryName)); - Assert.Throws(() => new UstarTarEntry(TarEntryType.RenamedOrSymlinked, InitialEntryName)); - Assert.Throws(() => new UstarTarEntry(TarEntryType.SparseFile, InitialEntryName)); - Assert.Throws(() => new UstarTarEntry(TarEntryType.TapeVolume, InitialEntryName)); + Assert.Throws(() => new UstarTarEntry(TarEntryType.ContiguousFile, InitialEntryName)); + Assert.Throws(() => new UstarTarEntry(TarEntryType.DirectoryList, InitialEntryName)); + Assert.Throws(() => new UstarTarEntry(TarEntryType.ExtendedAttributes, InitialEntryName)); + Assert.Throws(() => new UstarTarEntry(TarEntryType.GlobalExtendedAttributes, InitialEntryName)); + Assert.Throws(() => new UstarTarEntry(TarEntryType.LongLink, InitialEntryName)); + Assert.Throws(() => new UstarTarEntry(TarEntryType.LongPath, InitialEntryName)); + Assert.Throws(() => new UstarTarEntry(TarEntryType.MultiVolume, InitialEntryName)); + Assert.Throws(() => new UstarTarEntry(TarEntryType.V7RegularFile, InitialEntryName)); + Assert.Throws(() => new UstarTarEntry(TarEntryType.RenamedOrSymlinked, InitialEntryName)); + Assert.Throws(() => new UstarTarEntry(TarEntryType.SparseFile, InitialEntryName)); + Assert.Throws(() => new UstarTarEntry(TarEntryType.TapeVolume, InitialEntryName)); } [Fact] diff --git a/src/libraries/System.Formats.Tar/tests/TarEntry/V7TarEntry.Conversion.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarEntry/V7TarEntry.Conversion.Tests.cs index 3f5e41a8f3609f..8856ec4d54809a 100644 --- a/src/libraries/System.Formats.Tar/tests/TarEntry/V7TarEntry.Conversion.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarEntry/V7TarEntry.Conversion.Tests.cs @@ -16,25 +16,25 @@ public class V7TarEntry_Conversion_Tests : TarTestsConversionBase [Fact] public void Constructor_Conversion_UnsupportedEntryTypes_Ustar() { - Assert.Throws(() => new V7TarEntry(new UstarTarEntry(TarEntryType.BlockDevice, InitialEntryName))); - Assert.Throws(() => new V7TarEntry(new UstarTarEntry(TarEntryType.CharacterDevice, InitialEntryName))); - Assert.Throws(() => new V7TarEntry(new UstarTarEntry(TarEntryType.Fifo, InitialEntryName))); + Assert.Throws(() => new V7TarEntry(new UstarTarEntry(TarEntryType.BlockDevice, InitialEntryName))); + Assert.Throws(() => new V7TarEntry(new UstarTarEntry(TarEntryType.CharacterDevice, InitialEntryName))); + Assert.Throws(() => new V7TarEntry(new UstarTarEntry(TarEntryType.Fifo, InitialEntryName))); } [Fact] public void Constructor_Conversion_UnsupportedEntryTypes_Pax() { - Assert.Throws(() => new V7TarEntry(new PaxTarEntry(TarEntryType.BlockDevice, InitialEntryName))); - Assert.Throws(() => new V7TarEntry(new PaxTarEntry(TarEntryType.CharacterDevice, InitialEntryName))); - Assert.Throws(() => new V7TarEntry(new PaxTarEntry(TarEntryType.Fifo, InitialEntryName))); + Assert.Throws(() => new V7TarEntry(new PaxTarEntry(TarEntryType.BlockDevice, InitialEntryName))); + Assert.Throws(() => new V7TarEntry(new PaxTarEntry(TarEntryType.CharacterDevice, InitialEntryName))); + Assert.Throws(() => new V7TarEntry(new PaxTarEntry(TarEntryType.Fifo, InitialEntryName))); } [Fact] public void Constructor_Conversion_UnsupportedEntryTypes_Gnu() { - Assert.Throws(() => new V7TarEntry(new GnuTarEntry(TarEntryType.BlockDevice, InitialEntryName))); - Assert.Throws(() => new V7TarEntry(new GnuTarEntry(TarEntryType.CharacterDevice, InitialEntryName))); - Assert.Throws(() => new V7TarEntry(new GnuTarEntry(TarEntryType.Fifo, InitialEntryName))); + Assert.Throws(() => new V7TarEntry(new GnuTarEntry(TarEntryType.BlockDevice, InitialEntryName))); + Assert.Throws(() => new V7TarEntry(new GnuTarEntry(TarEntryType.CharacterDevice, InitialEntryName))); + Assert.Throws(() => new V7TarEntry(new GnuTarEntry(TarEntryType.Fifo, InitialEntryName))); } [Fact] diff --git a/src/libraries/System.Formats.Tar/tests/TarEntry/V7TarEntry.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarEntry/V7TarEntry.Tests.cs index 36dab66fb24e2f..2bf5471d4fa484 100644 --- a/src/libraries/System.Formats.Tar/tests/TarEntry/V7TarEntry.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarEntry/V7TarEntry.Tests.cs @@ -20,22 +20,22 @@ public void Constructor_InvalidEntryName() [Fact] public void Constructor_UnsupportedEntryTypes() { - Assert.Throws(() => new V7TarEntry((TarEntryType)byte.MaxValue, InitialEntryName)); + Assert.Throws(() => new V7TarEntry((TarEntryType)byte.MaxValue, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.BlockDevice, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.CharacterDevice, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.ContiguousFile, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.DirectoryList, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.ExtendedAttributes, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.Fifo, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.GlobalExtendedAttributes, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.LongLink, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.LongPath, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.MultiVolume, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.RegularFile, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.RenamedOrSymlinked, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.SparseFile, InitialEntryName)); - Assert.Throws(() => new V7TarEntry(TarEntryType.TapeVolume, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.BlockDevice, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.CharacterDevice, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.ContiguousFile, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.DirectoryList, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.ExtendedAttributes, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.Fifo, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.GlobalExtendedAttributes, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.LongLink, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.LongPath, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.MultiVolume, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.RegularFile, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.RenamedOrSymlinked, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.SparseFile, InitialEntryName)); + Assert.Throws(() => new V7TarEntry(TarEntryType.TapeVolume, InitialEntryName)); } [Fact] diff --git a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.CreateFromDirectory.Stream.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.CreateFromDirectory.Stream.Tests.cs index ca0cb57fab783b..33b550960ef563 100644 --- a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.CreateFromDirectory.Stream.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.CreateFromDirectory.Stream.Tests.cs @@ -28,7 +28,7 @@ public void UnwritableStream_Throws() { using MemoryStream archive = new MemoryStream(); using WrappedStream unwritable = new WrappedStream(archive, canRead: true, canWrite: false, canSeek: true); - Assert.Throws(() => TarFile.CreateFromDirectory(sourceDirectoryName: "path",destination: unwritable, includeBaseDirectory: false)); + Assert.Throws(() => TarFile.CreateFromDirectory(sourceDirectoryName: "path",destination: unwritable, includeBaseDirectory: false)); } [Fact] diff --git a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.CreateFromDirectoryAsync.Stream.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.CreateFromDirectoryAsync.Stream.Tests.cs index 7a67aac64508f7..8dbde4461290a6 100644 --- a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.CreateFromDirectoryAsync.Stream.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.CreateFromDirectoryAsync.Stream.Tests.cs @@ -48,7 +48,7 @@ public async Task UnwritableStream_Throws_Async() { await using (WrappedStream unwritable = new WrappedStream(archiveStream, canRead: true, canWrite: false, canSeek: true)) { - await Assert.ThrowsAsync(() => TarFile.CreateFromDirectoryAsync(sourceDirectoryName: "path", destination: unwritable, includeBaseDirectory: false)); + await Assert.ThrowsAsync(() => TarFile.CreateFromDirectoryAsync(sourceDirectoryName: "path", destination: unwritable, includeBaseDirectory: false)); } } } diff --git a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs index d53f354ee408d1..b62196bb5c5d6f 100644 --- a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs @@ -30,7 +30,7 @@ public void UnreadableStream_Throws() { using MemoryStream archive = new MemoryStream(); using WrappedStream unreadable = new WrappedStream(archive, canRead: false, canWrite: true, canSeek: true); - Assert.Throws(() => TarFile.ExtractToDirectory(unreadable, destinationDirectoryName: "path", overwriteFiles: false)); + Assert.Throws(() => TarFile.ExtractToDirectory(unreadable, destinationDirectoryName: "path", overwriteFiles: false)); } [Fact] diff --git a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectoryAsync.Stream.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectoryAsync.Stream.Tests.cs index 741ce8e102deb4..70c6fcbf8049d4 100644 --- a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectoryAsync.Stream.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectoryAsync.Stream.Tests.cs @@ -45,7 +45,7 @@ public async Task UnreadableStream_Throws_Async() { using (WrappedStream unreadable = new WrappedStream(archive, canRead: false, canWrite: true, canSeek: true)) { - await Assert.ThrowsAsync(() => TarFile.ExtractToDirectoryAsync(unreadable, destinationDirectoryName: "path", overwriteFiles: false)); + await Assert.ThrowsAsync(() => TarFile.ExtractToDirectoryAsync(unreadable, destinationDirectoryName: "path", overwriteFiles: false)); } } } diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Async.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Async.Tests.cs index 69a543a206d4b9..d86cfa4e34dd47 100644 --- a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Async.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Async.Tests.cs @@ -230,7 +230,7 @@ public async Task Throw_FifoContainsNonZeroDataSectionAsync() Assert.NotNull(await reader.GetNextEntryAsync()); Assert.NotNull(await reader.GetNextEntryAsync()); Assert.NotNull(await reader.GetNextEntryAsync()); - await Assert.ThrowsAsync(async () => await reader.GetNextEntryAsync()); + await Assert.ThrowsAsync(async () => await reader.GetNextEntryAsync()); } [Fact] @@ -268,7 +268,7 @@ public async Task Throw_ArchivesWithRandomCharsAsync(string testCaseName) { await using MemoryStream archiveStream = GetTarMemoryStream(CompressionMethod.Uncompressed, "golang_tar", testCaseName); await using TarReader reader = new TarReader(archiveStream); - await Assert.ThrowsAsync(async () => await reader.GetNextEntryAsync()); + await Assert.ThrowsAsync(async () => await reader.GetNextEntryAsync()); } [Fact] diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs index 298e3e8be47947..17c67423c390b2 100644 --- a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs @@ -230,7 +230,7 @@ public void Throw_FifoContainsNonZeroDataSection() Assert.NotNull(reader.GetNextEntry()); Assert.NotNull(reader.GetNextEntry()); Assert.NotNull(reader.GetNextEntry()); - Assert.Throws(() => reader.GetNextEntry()); + Assert.Throws(() => reader.GetNextEntry()); } [Fact] @@ -268,7 +268,7 @@ public void Throw_ArchivesWithRandomChars(string testCaseName) { using MemoryStream archiveStream = GetTarMemoryStream(CompressionMethod.Uncompressed, "golang_tar", testCaseName); using TarReader reader = new TarReader(archiveStream); - Assert.Throws(() => reader.GetNextEntry()); + Assert.Throws(() => reader.GetNextEntry()); } [Fact] diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.GetNextEntry.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.GetNextEntry.Tests.cs index dda0cae56b69f8..2cede3a350c825 100644 --- a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.GetNextEntry.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.GetNextEntry.Tests.cs @@ -30,7 +30,7 @@ public void MalformedArchive_HeaderSize() malformed.Seek(0, SeekOrigin.Begin); using TarReader reader = new TarReader(malformed); - Assert.Throws(() => reader.GetNextEntry()); + Assert.Throws(() => reader.GetNextEntry()); } [Fact] diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.GetNextEntryAsync.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.GetNextEntryAsync.Tests.cs index 1dcd9326ee81ee..f99e5853ebeaad 100644 --- a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.GetNextEntryAsync.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.GetNextEntryAsync.Tests.cs @@ -51,7 +51,7 @@ public async Task MalformedArchive_HeaderSize_Async() await using (TarReader reader = new TarReader(malformed)) { - await Assert.ThrowsAsync(async () => await reader.GetNextEntryAsync()); + await Assert.ThrowsAsync(async () => await reader.GetNextEntryAsync()); } } } diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Tests.cs new file mode 100644 index 00000000000000..8b41a5e9bb13bd --- /dev/null +++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Tests.cs @@ -0,0 +1,98 @@ +// 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 System.Linq; +using Xunit; + +namespace System.Formats.Tar.Tests +{ + public class TarReader_Tests : TarTestsBase + { + [Fact] + public void TarReader_NullArchiveStream() => Assert.Throws(() => 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(() => new TarReader(ws)); + } + + [Fact] + public void TarReader_LeaveOpen_False() + { + using MemoryStream ms = GetTarMemoryStream(CompressionMethod.Uncompressed, TestTarFormat.pax, "many_small_files"); + List dataStreams = new List(); + 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(() => ds.ReadByte()); + } + } + + [Fact] + public void TarReader_LeaveOpen_True() + { + using MemoryStream ms = GetTarMemoryStream(CompressionMethod.Uncompressed, TestTarFormat.pax, "many_small_files"); + List dataStreams = new List(); + 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 dataStreams = new List(); + 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(); + } + } + } +} diff --git a/src/libraries/System.Formats.Tar/tests/TarTestsBase.cs b/src/libraries/System.Formats.Tar/tests/TarTestsBase.cs index 23dc653c83a165..ae01821d62b6d6 100644 --- a/src/libraries/System.Formats.Tar/tests/TarTestsBase.cs +++ b/src/libraries/System.Formats.Tar/tests/TarTestsBase.cs @@ -288,6 +288,8 @@ protected void SetCommonHardLink(TarEntry hardLink) // LinkName Assert.Equal(DefaultLinkName, hardLink.LinkName); + Assert.Throws(() => hardLink.LinkName = null); + Assert.Throws(() => hardLink.LinkName = string.Empty); hardLink.LinkName = TestLinkName; } @@ -299,6 +301,8 @@ protected void SetCommonSymbolicLink(TarEntry symbolicLink) // LinkName Assert.Equal(DefaultLinkName, symbolicLink.LinkName); + Assert.Throws(() => symbolicLink.LinkName = null); + Assert.Throws(() => symbolicLink.LinkName = string.Empty); symbolicLink.LinkName = TestLinkName; } @@ -401,6 +405,13 @@ protected void VerifyDataStream(TarEntry entry, bool isFromWriter) if (isFromWriter) { Assert.Null(entry.DataStream); + + using (MemoryStream ms = new MemoryStream()) + using (WrappedStream ws = new WrappedStream(ms, canRead: false, canWrite: true, canSeek: true)) + { + Assert.Throws(() => entry.DataStream = ws); + } + entry.DataStream = new MemoryStream(); // Verify it is not modified or wrapped in any way Assert.True(entry.DataStream.CanRead); @@ -436,7 +447,7 @@ protected Type GetTypeForFormat(TarEntryFormat expectedFormat) TarEntryFormat.Ustar => typeof(UstarTarEntry), TarEntryFormat.Pax => typeof(PaxTarEntry), TarEntryFormat.Gnu => typeof(GnuTarEntry), - _ => throw new FormatException($"Unrecognized format: {expectedFormat}"), + _ => throw new InvalidDataException($"Unrecognized format: {expectedFormat}"), }; } @@ -472,7 +483,7 @@ protected TarEntry InvokeTarEntryCreationConstructor(TarEntryFormat targetFormat TarEntryFormat.Ustar => new UstarTarEntry(entryType, entryName), TarEntryFormat.Pax => new PaxTarEntry(entryType, entryName), TarEntryFormat.Gnu => new GnuTarEntry(entryType, entryName), - _ => throw new FormatException($"Unexpected format: {targetFormat}") + _ => throw new InvalidDataException($"Unexpected format: {targetFormat}") }; public static IEnumerable GetFormatsAndLinks() diff --git a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs index 894498934f0177..8d482af0b1dff1 100644 --- a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs @@ -63,8 +63,8 @@ public void Constructors_UnwritableStream_Throws() { using MemoryStream archiveStream = new MemoryStream(); using WrappedStream wrappedStream = new WrappedStream(archiveStream, canRead: true, canWrite: false, canSeek: false); - Assert.Throws(() => new TarWriter(wrappedStream)); - Assert.Throws(() => new TarWriter(wrappedStream, TarEntryFormat.V7)); + Assert.Throws(() => new TarWriter(wrappedStream)); + Assert.Throws(() => new TarWriter(wrappedStream, TarEntryFormat.V7)); } [Fact] diff --git a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.Tests.cs index 6d19503c4fc25d..b0c78298f2bb01 100644 --- a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.Tests.cs @@ -101,7 +101,7 @@ public void Write_RegularFileEntry_In_V7Writer(TarEntryFormat entryFormat) TarEntryFormat.Ustar => new UstarTarEntry(TarEntryType.RegularFile, InitialEntryName), TarEntryFormat.Pax => new PaxTarEntry(TarEntryType.RegularFile, InitialEntryName), TarEntryFormat.Gnu => new GnuTarEntry(TarEntryType.RegularFile, InitialEntryName), - _ => throw new FormatException($"Unexpected format: {entryFormat}") + _ => throw new InvalidDataException($"Unexpected format: {entryFormat}") }; // Should be written in the format of the entry diff --git a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntryAsync.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntryAsync.Tests.cs index 8f6637408a8c9b..c2eb58a7f1f24a 100644 --- a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntryAsync.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntryAsync.Tests.cs @@ -125,7 +125,7 @@ public async Task Write_RegularFileEntry_In_V7Writer_Async(TarEntryFormat entryF TarEntryFormat.Ustar => new UstarTarEntry(TarEntryType.RegularFile, InitialEntryName), TarEntryFormat.Pax => new PaxTarEntry(TarEntryType.RegularFile, InitialEntryName), TarEntryFormat.Gnu => new GnuTarEntry(TarEntryType.RegularFile, InitialEntryName), - _ => throw new FormatException($"Unexpected format: {entryFormat}") + _ => throw new InvalidDataException($"Unexpected format: {entryFormat}") }; // Should be written in the format of the entry