Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Address suggestions
  • Loading branch information
jozkee authored and github-actions committed Oct 12, 2021
commit ae00ff5363ece335d2a3ae13e507dbce01cb635c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ public class DevicesPipesAndSockets : FileSystemTest
[MemberData(nameof(DevicePath_FileOptions_TestData))]
public void CharacterDevice_FileStream_Write(string devicePath, FileOptions fileOptions)
{
FileStreamOptions options = new() { Options = fileOptions, Access = FileAccess.Write };
if (IsDeviceUnreachable(devicePath, options))
{
return;
}

FileStreamOptions options = new() { Options = fileOptions, Access = FileAccess.Write, Share = FileShare.Write };
using FileStream fs = new(devicePath, options);
fs.Write(Encoding.UTF8.GetBytes("foo"));
}
Expand All @@ -33,12 +28,7 @@ public void CharacterDevice_FileStream_Write(string devicePath, FileOptions file
[MemberData(nameof(DevicePath_FileOptions_TestData))]
public async Task CharacterDevice_FileStream_WriteAsync(string devicePath, FileOptions fileOptions)
{
FileStreamOptions options = new() { Options = fileOptions, Access = FileAccess.Write };
if (IsDeviceUnreachable(devicePath, options))
{
return;
}

FileStreamOptions options = new() { Options = fileOptions, Access = FileAccess.Write, Share = FileShare.Write };
using FileStream fs = new(devicePath, options);
await fs.WriteAsync(Encoding.UTF8.GetBytes("foo"));
}
Expand All @@ -47,47 +37,27 @@ public async Task CharacterDevice_FileStream_WriteAsync(string devicePath, FileO
[MemberData(nameof(DevicePath_TestData))]
public void CharacterDevice_WriteAllBytes(string devicePath)
{
if (IsDeviceUnreachable(devicePath, new FileStreamOptions{ Access = FileAccess.Write }))
{
return;
}

File.WriteAllBytes(devicePath, Encoding.UTF8.GetBytes("foo"));
}

[Theory]
[MemberData(nameof(DevicePath_TestData))]
public async Task CharacterDevice_WriteAllBytesAsync(string devicePath)
{
if (IsDeviceUnreachable(devicePath, new FileStreamOptions{ Options = FileOptions.Asynchronous, Access = FileAccess.Write }))
{
return;
}

await File.WriteAllBytesAsync(devicePath, Encoding.UTF8.GetBytes("foo"));
}

[Theory]
[MemberData(nameof(DevicePath_TestData))]
public void CharacterDevice_WriteAllText(string devicePath)
{
if (IsDeviceUnreachable(devicePath, new FileStreamOptions{ Access = FileAccess.Write }))
{
return;
}

File.WriteAllText(devicePath, "foo");
}

[Theory]
[MemberData(nameof(DevicePath_TestData))]
public async Task CharacterDevice_WriteAllTextAsync(string devicePath)
{
if (IsDeviceUnreachable(devicePath, new FileStreamOptions{ Options = FileOptions.Asynchronous, Access = FileAccess.Write }))
{
return;
}

await File.WriteAllTextAsync(devicePath, "foo");
}

Expand Down Expand Up @@ -130,12 +100,13 @@ await Task.WhenAll(
}));
}

private const int AF_UNIX = 1;
private const int SOCK_STREAM = 1;

[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)]
public unsafe void SocketPair_ReadWrite()
{
const int AF_UNIX = 1;
const int SOCK_STREAM = 1;
int* ptr = stackalloc int[2];
Assert.Equal(0, socketpair(AF_UNIX, SOCK_STREAM, 0, ptr));

Expand All @@ -151,8 +122,6 @@ public unsafe void SocketPair_ReadWrite()
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)]
public void SocketPair_ReadWrite_Async()
{
const int AF_UNIX = 1;
const int SOCK_STREAM = 1;
unsafe
{
int* ptr = stackalloc int[2];
Expand Down Expand Up @@ -193,35 +162,45 @@ private static async Task WriteByteAsync(FileStream fs, byte value)
await fs.FlushAsync();
}

private static bool IsDeviceUnreachable(string devicePath, FileStreamOptions? options)
{
if (!File.Exists(devicePath))
private static List<string> _devicePathsAvailable;
private static List<string> GetDevicePaths()
{
if (_devicePathsAvailable is null)
{
return true;
}
var options = new FileStreamOptions { Access = FileAccess.Write, Share = FileShare.Write };
_devicePathsAvailable = new List<string>();

try
{
File.Open(devicePath, options).Dispose();
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
foreach (string devicePath in new[] { "/dev/tty", "/dev/console", "/dev/null", "/dev/zero" })
{
return true;
if (!File.Exists(devicePath))
{
continue;
}

try
{
File.Open(devicePath, options).Dispose();
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
continue;
}

throw;
}

_devicePathsAvailable.Add(devicePath);
}

throw;
}

return false;
return _devicePathsAvailable;
}

private static string[] DevicePaths = { "/dev/tty", "/dev/console", "/dev/null", "/dev/zero" };

public static IEnumerable<object[]> DevicePath_FileOptions_TestData()
{
foreach (string devicePath in DevicePaths)
foreach (string devicePath in GetDevicePaths())
{
foreach (FileOptions options in new[] { FileOptions.None, FileOptions.Asynchronous })
{
Expand All @@ -232,7 +211,7 @@ public static IEnumerable<object[]> DevicePath_FileOptions_TestData()

public static IEnumerable<object[]> DevicePath_TestData()
{
foreach (string devicePath in DevicePaths)
foreach (string devicePath in GetDevicePaths())
{
yield return new object[] { devicePath };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ internal bool SupportsRandomAccess

return supportsRandomAccess == NullableBool.True;
}
set => _supportsRandomAccess = value ? NullableBool.True : NullableBool.False;
set
{
Debug.Assert(value == false); // We should only use the setter to disable random access.
_supportsRandomAccess = value ? NullableBool.True : NullableBool.False;
}
}

internal ThreadPoolBoundHandle? ThreadPoolBinding => null;
Expand Down