Skip to content

Commit

Permalink
LongProcessNamesAreSupported: make test work on distros where sleep i…
Browse files Browse the repository at this point in the history
…s a symlink/script (#44299)

* LongProcessNamesAreSupported: make test work on distros where sleep is a symlink/script

* PR feedback

Co-authored-by: Stephen Toub <stoub@microsoft.com>

* fix compilation

Co-authored-by: Stephen Toub <stoub@microsoft.com>
  • Loading branch information
tmds and stephentoub authored Nov 8, 2020
1 parent 1492699 commit 628c14b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
24 changes: 10 additions & 14 deletions src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Win32Exception>(() => Process.Start(path));
Assert.NotEqual(0, e.NativeErrorCode);
Expand All @@ -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<Win32Exception>(() => Process.Start(path));
Assert.NotEqual(0, e.NativeErrorCode);
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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();
}
}
24 changes: 14 additions & 10 deletions src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1964,23 +1964,27 @@ 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.
sleepPath = GetTestFilePath();
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"))
{
Expand Down

0 comments on commit 628c14b

Please sign in to comment.