Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
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)));
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}

[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.
string 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