From 8608d0dce878835dab3d92185792d8a1c01697f6 Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Thu, 5 Nov 2020 15:27:43 +0100 Subject: [PATCH 1/3] LongProcessNamesAreSupported: make test work on distros where sleep is a symlink/script --- .../tests/ProcessTests.Unix.cs | 24 ++++++++---------- .../tests/ProcessTests.Windows.cs | 4 +++ .../tests/ProcessTests.cs | 25 +++++++++++-------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs index 9097dd7672a53..99272c05f0c2e 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs @@ -162,9 +162,7 @@ public void ProcessNameMatchesScriptName() string scriptName = GetTestFileName(); string filename = Path.Combine(TestDirectory, scriptName); File.WriteAllText(filename, $"#!/bin/sh\nsleep 600\n"); // sleep 10 min. - // set x-bit - int mode = Convert.ToInt32("744", 8); - Assert.Equal(0, chmod(filename, mode)); + ChMod(filename, "744"); // set x-bit using (var process = Process.Start(new ProcessStartInfo { FileName = filename })) { @@ -198,8 +196,7 @@ public void ProcessStart_UseShellExecute_OnUnix_FallsBackWhenNotRealExecutable() // Create a file that has the x-bit set, but which isn't a valid script. string filename = WriteScriptFile(TestDirectory, GetTestFileName(), returnValue: 0); File.WriteAllText(filename, $"not a script"); - int mode = Convert.ToInt32("744", 8); - Assert.Equal(0, chmod(filename, mode)); + ChMod(filename, "744"); // set x-bit RemoteInvokeOptions options = new RemoteInvokeOptions(); options.StartInfo.EnvironmentVariables["PATH"] = path; @@ -482,9 +479,7 @@ public void TestStartOnUnixWithBadPermissions() { string path = GetTestFilePath(); File.Create(path).Dispose(); - int mode = Convert.ToInt32("644", 8); - - Assert.Equal(0, chmod(path, mode)); + ChMod(path, "644"); Win32Exception e = Assert.Throws(() => Process.Start(path)); Assert.NotEqual(0, e.NativeErrorCode); @@ -495,9 +490,7 @@ public void TestStartOnUnixWithBadFormat() { string path = GetTestFilePath(); File.Create(path).Dispose(); - int mode = Convert.ToInt32("744", 8); - - Assert.Equal(0, chmod(path, mode)); // execute permissions + ChMod(path, "744"); // set x-bit Win32Exception e = Assert.Throws(() => Process.Start(path)); Assert.NotEqual(0, e.NativeErrorCode); @@ -899,6 +892,11 @@ private static int GetWaitStateReferenceCount(object waitState) [DllImport("libc")] private static extern int chmod(string path, int mode); + private static void ChMod(string filename, string mode) + { + Assert.Equal(0, chmod(filename, Convert.ToInt32(mode, 8))); + } + [DllImport("libc")] private static extern uint geteuid(); [DllImport("libc")] @@ -946,9 +944,7 @@ private string WriteScriptFile(string directory, string name, int returnValue) { string filename = Path.Combine(directory, name); File.WriteAllText(filename, $"#!/bin/sh\nexit {returnValue}\n"); - // set x-bit - int mode = Convert.ToInt32("744", 8); - Assert.Equal(0, chmod(filename, mode)); + ChMod(filename, "744"); // set x-bit return filename; } diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Windows.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Windows.cs index fe061a046b1aa..5a8a0dc448bba 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Windows.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Windows.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; using System.IO; namespace System.Diagnostics.Tests @@ -14,5 +15,8 @@ private string WriteScriptFile(string directory, string name, int returnValue) File.WriteAllText(filename, $"exit {returnValue}"); return filename; } + + private static void ChMod(string filename, string mode) + => throw new PlatformNotSupportedException(); } } diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs index c67df209280de..a227da850e28c 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs @@ -1964,23 +1964,28 @@ public void TestLongProcessIsWorking() [Fact] public void LongProcessNamesAreSupported() { - // Alpine implements sleep as a symlink to the busybox executable. - // If we rename it, the program will no longer sleep. - if (PlatformDetection.IsAlpine) + string sleepPath; + if (OperatingSystem.IsLinux()) { - return; + // On some distros sleep is implemented using a script/symlink, which causes this test to fail. + // Instead of using sleep directly, we wrap it with a script. + string scriptName = GetTestFileName(); + sleepPath = Path.Combine(TestDirectory, scriptName); + File.WriteAllText(sleepPath, $"#!/bin/sh\nsleep 600\n"); // sleep 10 min. + ChMod(sleepPath, "744"); } - - string programPath = GetProgramPath("sleep"); - - if (programPath == null) + else { - return; + sleepPath = GetProgramPath("sleep"); + if (sleepPath == null) + { + return; + } } const string LongProcessName = "123456789012345678901234567890"; string sleepCommandPathFileName = Path.Combine(TestDirectory, LongProcessName); - File.Copy(programPath, sleepCommandPathFileName); + File.Copy(sleepPath, sleepCommandPathFileName); using (Process px = Process.Start(sleepCommandPathFileName, "600")) { From c718c59635226621dfa71d3c816498bcdd44504b Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Thu, 5 Nov 2020 16:23:50 +0100 Subject: [PATCH 2/3] PR feedback Co-authored-by: Stephen Toub --- src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs index a227da850e28c..73bb70ee360da 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs @@ -1969,8 +1969,7 @@ public void LongProcessNamesAreSupported() { // On some distros sleep is implemented using a script/symlink, which causes this test to fail. // Instead of using sleep directly, we wrap it with a script. - string scriptName = GetTestFileName(); - sleepPath = Path.Combine(TestDirectory, scriptName); + string sleepPath = GetTestFilePath(); File.WriteAllText(sleepPath, $"#!/bin/sh\nsleep 600\n"); // sleep 10 min. ChMod(sleepPath, "744"); } From aa3afff0dfdb3e13b5a90e9e730f5c353aed0317 Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Fri, 6 Nov 2020 08:09:54 +0100 Subject: [PATCH 3/3] fix compilation --- src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs index 73bb70ee360da..960eaec810404 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs @@ -1969,7 +1969,7 @@ public void LongProcessNamesAreSupported() { // On some distros sleep is implemented using a script/symlink, which causes this test to fail. // Instead of using sleep directly, we wrap it with a script. - string sleepPath = GetTestFilePath(); + sleepPath = GetTestFilePath(); File.WriteAllText(sleepPath, $"#!/bin/sh\nsleep 600\n"); // sleep 10 min. ChMod(sleepPath, "744"); }