未验证 提交 cfe27312 编写于 作者: C Carlos Sanchez 提交者: GitHub

Expect UnauthorizedAccessException in tvOS if extracting fifo from tar without elevation (#71175)

* Add ConditionalFact to 'unelevated' tests to ensure they are not executed in elevated mode.

* Fix test that was incorrectly executed async
上级 eba01b7f
......@@ -9,7 +9,7 @@ namespace System.Formats.Tar.Tests
{
public partial class TarFile_ExtractToDirectory_File_Tests : TarTestsBase
{
[Fact]
[ConditionalFact(nameof(IsUnixButNotSuperUser))]
public void Extract_SpecialFiles_Unix_Unelevated_ThrowsUnauthorizedAccess()
{
string originalFileName = GetTarFilePath(CompressionMethod.Uncompressed, TestTarFormat.ustar, "specialfiles");
......
......@@ -10,7 +10,7 @@ namespace System.Formats.Tar.Tests
{
public partial class TarFile_ExtractToDirectoryAsync_File_Tests : TarTestsBase
{
[Fact]
[ConditionalFact(nameof(IsUnixButNotSuperUser))]
public async Task Extract_SpecialFiles_Unix_Unelevated_ThrowsUnauthorizedAccess_Async()
{
using (TempDirectory root = new TempDirectory())
......
......@@ -10,36 +10,35 @@ namespace System.Formats.Tar.Tests
public partial class TarReader_TarEntry_ExtractToFile_Tests : TarTestsBase
{
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.tvOS)] // https://github.com/dotnet/runtime/issues/68360
[Fact]
public async Task SpecialFile_Unelevated_Throws_Async()
[ConditionalFact(nameof(IsUnixButNotSuperUser))]
public void SpecialFile_Unelevated_Throws()
{
using TempDirectory root = new TempDirectory();
using MemoryStream ms = GetTarMemoryStream(CompressionMethod.Uncompressed, TestTarFormat.ustar, "specialfiles");
TarReader reader = new TarReader(ms);
await using (reader)
using (TarReader reader = new TarReader(ms))
{
string path = Path.Join(root.Path, "output");
// Block device requires elevation for writing
PosixTarEntry blockDevice = await reader.GetNextEntryAsync() as PosixTarEntry;
PosixTarEntry blockDevice = reader.GetNextEntry() as PosixTarEntry;
Assert.NotNull(blockDevice);
await Assert.ThrowsAsync<UnauthorizedAccessException>(() => blockDevice.ExtractToFileAsync(path, overwrite: false));
Assert.Throws<UnauthorizedAccessException>(() => blockDevice.ExtractToFile(path, overwrite: false));
Assert.False(File.Exists(path));
// Character device requires elevation for writing
PosixTarEntry characterDevice = await reader.GetNextEntryAsync() as PosixTarEntry;
PosixTarEntry characterDevice = reader.GetNextEntry() as PosixTarEntry;
Assert.NotNull(characterDevice);
await Assert.ThrowsAsync<UnauthorizedAccessException>(() => characterDevice.ExtractToFileAsync(path, overwrite: false));
Assert.Throws<UnauthorizedAccessException>(() => characterDevice.ExtractToFile(path, overwrite: false));
Assert.False(File.Exists(path));
// Fifo does not require elevation, should succeed
PosixTarEntry fifo = await reader.GetNextEntryAsync() as PosixTarEntry;
PosixTarEntry fifo = reader.GetNextEntry() as PosixTarEntry;
Assert.NotNull(fifo);
await fifo.ExtractToFileAsync(path, overwrite: false);
fifo.ExtractToFile(path, overwrite: false);
Assert.True(File.Exists(path));
Assert.Null(await reader.GetNextEntryAsync());
Assert.Null(reader.GetNextEntry());
}
}
}
......
......@@ -10,7 +10,7 @@ namespace System.Formats.Tar.Tests
public partial class TarReader_TarEntry_ExtractToFileAsync_Tests : TarTestsBase
{
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.tvOS)] // https://github.com/dotnet/runtime/issues/68360
[Fact]
[ConditionalFact(nameof(IsUnixButNotSuperUser))]
public async Task SpecialFile_Unelevated_Throws_Async()
{
using (TempDirectory root = new TempDirectory())
......
......@@ -101,6 +101,8 @@ public enum TestTarFormat
}
protected static bool IsRemoteExecutorSupportedAndOnUnixAndSuperUser => RemoteExecutor.IsSupported && PlatformDetection.IsUnixAndSuperUser;
protected static bool IsUnixButNotSuperUser => !PlatformDetection.IsWindows && !PlatformDetection.IsSuperUser;
protected static string GetTestCaseUnarchivedFolderPath(string testCaseName) =>
Path.Join(Directory.GetCurrentDirectory(), "unarchived", testCaseName);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册