diff --git a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.File.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.File.Tests.cs index d8d4e46d005da1..04e1c0088c1012 100644 --- a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.File.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.File.Tests.cs @@ -544,8 +544,13 @@ public void ExtractToDirectory_RejectsChainedSymlinkDirectoryTraversal_WithNeste if (OperatingSystem.IsWindows()) { - // Windows only creates file symlinks and trying to process a directory symlink will throw UnauthorizedAccessException instead of IOException - Assert.Throws(() => TarFile.ExtractToDirectory(tarPath, destDir, overwriteFiles: true)); + // Windows only creates file symlinks. Processing the chained directory symlink usually + // surfaces as UnauthorizedAccessException, but depending on the order in which the entries + // are extracted the colliding directory creation can instead surface as IOException + // ("a file or directory with the same name already exists"). Either way the traversal is + // rejected before anything escapes the destination, which the assertions below verify. + Exception ex = Assert.ThrowsAny(() => TarFile.ExtractToDirectory(tarPath, destDir, overwriteFiles: true)); + Assert.True(ex is UnauthorizedAccessException or IOException, $"Unexpected exception type: {ex.GetType()}"); } else {