Skip to content
Closed
Changes from all 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
35 changes: 10 additions & 25 deletions src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,18 +530,17 @@ private static int CheckUserAndGroupIds(string userId, string groupId, string gr

if (bool.Parse(checkGroupsExact))
{
AssertExtensions.Equal(expectedGroups, GetGroups());
AssertExtensions.Equal(expectedGroups, GetCurrentUserGroupIds());
}
else
{
Assert.Subset(expectedGroups, GetGroups());
Assert.Subset(expectedGroups, GetCurrentUserGroupIds());
}

return RemoteExecutor.SuccessExitCode;
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/28922", TestPlatforms.AnyUnix)]
public unsafe void TestCheckChildProcessUserAndGroupIds()
{
string userName = GetCurrentRealUserName();
Expand Down Expand Up @@ -620,10 +619,17 @@ private static string GetUserGroupId(string username)
private static string GetUserGroupIds(string username)
{
string[] groupIds = StartAndReadToEnd("id", new[] { "-G", username })
.Split(new[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
.Split(new[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
return string.Join(",", groupIds.Select(s => uint.Parse(s)).OrderBy(id => id));
}

private static HashSet<uint> GetCurrentUserGroupIds()
{
string[] groupIds = StartAndReadToEnd("id", new[] { "-G" })
.Split(new[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
return new HashSet<uint>(groupIds.Select(s => uint.Parse(s)));
}

private static string GetCurrentRealUserName()
{
string realUserName = geteuid() == 0 ?
Expand Down Expand Up @@ -906,27 +912,6 @@ private static void ChMod(string filename, string mode)
[DllImport("libc")]
private static extern uint getgid();

[DllImport("libc", SetLastError = true)]
private static extern unsafe int getgroups(int size, uint* list);

private static unsafe HashSet<uint> GetGroups()
{
int maxSize = 128;
Span<uint> groups = stackalloc uint[maxSize];
fixed (uint* pGroups = groups)
{
int rv = getgroups(maxSize, pGroups);
if (rv == -1)
{
// If this throws with EINVAL, maxSize should be increased.
throw new Win32Exception();
}

// Return this as a HashSet to filter out duplicates.
return new HashSet<uint>(groups.Slice(0, rv).ToArray());
}
}

[DllImport("libc")]
private static extern int seteuid(uint euid);

Expand Down