From e4f2d69e9e23b7e12736d6a3f906a79d525321b0 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Fri, 29 Jan 2021 18:55:52 +0000 Subject: [PATCH 1/2] Add test validating against regression in #46469 --- .../tests/ProcessTests.Unix.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs index 59a24f55a0fd39..3332998c4ae3aa 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs @@ -802,6 +802,25 @@ public void TestProcessRecycledPid() Assert.True(foundRecycled); } + [PlatformSpecific(TestPlatforms.AnyUnix)] + [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] + [InlineData("/dev/stdin")] + [InlineData("/dev/stdout")] + [InlineData("/dev/stderr")] + public void ChildProcessRedirectedIO_FilePathOpenShouldSucceed(string filename) + { + var options = new RemoteInvokeOptions { StartInfo = new ProcessStartInfo { RedirectStandardOutput = true, RedirectStandardInput = true, RedirectStandardError = true }}; + using (RemoteInvokeHandle handle = RemoteExecutor.Invoke(ExecuteChildProcess, filename, options)) + { } + + static void ExecuteChildProcess(string filename) + { + int flags = filename == "/dev/stdin" ? /* O_WRONLY */ 1 : /* O_RDONLY */ 0; + int result = open(filename, flags); + Assert.True(result >= 0); + } + } + [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] [InlineData(true)] [InlineData(false)] @@ -941,6 +960,9 @@ private static unsafe HashSet GetGroups() [DllImport("libc", SetLastError = true)] private static extern int kill(int pid, int sig); + [DllImport("libc", SetLastError = true)] + private static extern int open(string pathname, int flags); + private static readonly string[] s_allowedProgramsToRun = new string[] { "xdg-open", "gnome-open", "kfmclient" }; private string WriteScriptFile(string directory, string name, int returnValue) From a6cf539ff52c0e9f558788b89ada2476836b9cee Mon Sep 17 00:00:00 2001 From: eiriktsarpalis Date: Mon, 1 Feb 2021 14:27:01 +0000 Subject: [PATCH 2/2] fix test bug --- .../tests/ProcessTests.Unix.cs | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs index 3332998c4ae3aa..e64153fe1e1a0b 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs @@ -4,6 +4,7 @@ using System.Collections; using System.Collections.Generic; using System.ComponentModel; +using System.Globalization; using System.IO; using System.Linq; using System.Reflection; @@ -804,20 +805,19 @@ public void TestProcessRecycledPid() [PlatformSpecific(TestPlatforms.AnyUnix)] [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] - [InlineData("/dev/stdin")] - [InlineData("/dev/stdout")] - [InlineData("/dev/stderr")] - public void ChildProcessRedirectedIO_FilePathOpenShouldSucceed(string filename) + [InlineData("/dev/stdin", O_RDONLY)] + [InlineData("/dev/stdout", O_WRONLY)] + [InlineData("/dev/stderr", O_WRONLY)] + public void ChildProcessRedirectedIO_FilePathOpenShouldSucceed(string filename, int flags) { var options = new RemoteInvokeOptions { StartInfo = new ProcessStartInfo { RedirectStandardOutput = true, RedirectStandardInput = true, RedirectStandardError = true }}; - using (RemoteInvokeHandle handle = RemoteExecutor.Invoke(ExecuteChildProcess, filename, options)) + using (RemoteInvokeHandle handle = RemoteExecutor.Invoke(ExecuteChildProcess, filename, flags.ToString(CultureInfo.InvariantCulture), options)) { } - static void ExecuteChildProcess(string filename) + static void ExecuteChildProcess(string filename, string flags) { - int flags = filename == "/dev/stdin" ? /* O_WRONLY */ 1 : /* O_RDONLY */ 0; - int result = open(filename, flags); - Assert.True(result >= 0); + int result = open(filename, int.Parse(flags, CultureInfo.InvariantCulture)); + Assert.True(result >= 0, $"failed to open file with {result} and errno {Marshal.GetLastWin32Error()}."); } } @@ -963,6 +963,9 @@ private static unsafe HashSet GetGroups() [DllImport("libc", SetLastError = true)] private static extern int open(string pathname, int flags); + private const int O_RDONLY = 0; + private const int O_WRONLY = 1; + private static readonly string[] s_allowedProgramsToRun = new string[] { "xdg-open", "gnome-open", "kfmclient" }; private string WriteScriptFile(string directory, string name, int returnValue)