Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 6ea3d21

Browse files
committed
Merge pull request #2421 from Priya91/processtest
Add test coverage to System.Diagnostics.Process types.
2 parents 3fdc22c + 3f8175f commit 6ea3d21

17 files changed

+680
-55
lines changed

src/Common/tests/System/Diagnostics/AssertWithCallerAttributes.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,20 @@ public static bool Equals(object a, object b,
201201
catch (Exception e) { throw WrapException(e, path, line); }
202202
}
203203

204-
public static void False(bool condition,
204+
public static void False(bool condition, string userMessage = null,
205205
[CallerFilePath] string path = null, [CallerLineNumber] int line = 0)
206206
{
207-
try { Xunit.Assert.False(condition); }
208-
catch (Exception e) { throw WrapException(e, path, line); }
209-
}
210-
211-
public static void False(bool condition, string userMessage,
212-
[CallerFilePath] string path = null, [CallerLineNumber] int line = 0)
213-
{
214-
try { Xunit.Assert.False(condition, userMessage); }
207+
try
208+
{
209+
if (userMessage == null)
210+
{
211+
Xunit.Assert.False(condition);
212+
}
213+
else
214+
{
215+
Xunit.Assert.False(condition, userMessage);
216+
}
217+
}
215218
catch (Exception e) { throw WrapException(e, path, line); }
216219
}
217220

@@ -604,7 +607,7 @@ private static XunitException WrapException(Exception e, string callerFilePath,
604607
// so to use it we derive a custom exception type
605608
internal sealed class WrapperXunitException : XunitException
606609
{
607-
internal WrapperXunitException(string message, Exception innerException) :
610+
internal WrapperXunitException(string message, Exception innerException) :
608611
base(message, innerException)
609612
{
610613
}

src/System.Diagnostics.Process/src/System/Diagnostics/Process.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ public ProcessThreadCollection Threads
511511
{
512512
newThreadsArray[i] = new ProcessThread(_isRemoteMachine, _processId, (ThreadInfo)_processInfo._threadInfoList[i]);
513513
}
514+
514515
ProcessThreadCollection newThreads = new ProcessThreadCollection(newThreadsArray);
515516
_threads = newThreads;
516517
}

src/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private static ProcessInfo CreateProcessInfo(int pid)
118118
pi._threadInfoList.Add(new ThreadInfo
119119
{
120120
_processId = pid,
121-
_threadId = tid,
121+
_threadId = (ulong)tid,
122122
_basePriority = pi.BasePriority,
123123
_currentPriority = (int)stat.nice,
124124
_startAddress = (IntPtr)stat.startstack,

src/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.OSX.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ private static ProcessInfo CreateProcessInfo(int pid)
5656
var ti = new ThreadInfo()
5757
{
5858
_processId = pid,
59-
_threadId = (int)t.Key, // The OS X thread ID is 64-bits, but we're forced to truncate due to the public API signature
60-
_basePriority = 0,
59+
_threadId = t.Key,
60+
_basePriority = procInfo.BasePriority,
6161
_startAddress = IntPtr.Zero
6262
};
6363

src/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ static ThreadInfo GetThreadInfo(Interop.mincore.PERF_OBJECT_TYPE type, IntPtr in
677677
threadInfo._processId = (int)value;
678678
break;
679679
case ValueId.ThreadId:
680-
threadInfo._threadId = (int)value;
680+
threadInfo._threadId = (ulong)value;
681681
break;
682682
case ValueId.BasePriority:
683683
threadInfo._basePriority = (int)value;
@@ -992,7 +992,7 @@ static ProcessInfo[] GetProcessInfos(IntPtr dataPtr)
992992
ThreadInfo threadInfo = new ThreadInfo();
993993

994994
threadInfo._processId = (int)ti.UniqueProcess;
995-
threadInfo._threadId = (int)ti.UniqueThread;
995+
threadInfo._threadId = (ulong)ti.UniqueThread;
996996
threadInfo._basePriority = ti.BasePriority;
997997
threadInfo._currentPriority = ti.Priority;
998998
threadInfo._startAddress = ti.StartAddress;

src/System.Diagnostics.Process/src/System/Diagnostics/ProcessThread.OSX.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,11 @@ private ThreadPriorityLevel PriorityLevelCore
1616
{
1717
get
1818
{
19-
int nice = 0;
20-
int result = Interop.libc.getpriority(Interop.libc.PriorityWhich.PRIO_DARWIN_THREAD, _threadInfo._threadId, out nice);
21-
if (result != 0)
22-
{
23-
throw new System.ComponentModel.Win32Exception();
24-
}
25-
26-
return Interop.libc.GetThreadPriorityFromNiceValue(nice);
19+
throw new PlatformNotSupportedException();
2720
}
2821
set
2922
{
30-
int result = Interop.libc.setpriority(Interop.libc.PriorityWhich.PRIO_DARWIN_THREAD, _threadInfo._threadId, Interop.libc.GetNiceValueFromThreadPriority(value));
31-
if (result != 0)
32-
{
33-
throw new System.ComponentModel.Win32Exception();
34-
}
23+
throw new PlatformNotSupportedException();
3524
}
3625
}
3726

src/System.Diagnostics.Process/src/System/Diagnostics/ProcessThread.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private ProcessThreadTimes GetThreadTimes()
171171
private SafeThreadHandle OpenThreadHandle(int access)
172172
{
173173
EnsureState(State.IsLocal);
174-
return ProcessManager.OpenThread(_threadInfo._threadId, access);
174+
return ProcessManager.OpenThread((int)_threadInfo._threadId, access);
175175
}
176176
}
177177
}

src/System.Diagnostics.Process/src/System/Diagnostics/ProcessThread.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public int CurrentPriority
5353
/// </devdoc>
5454
public int Id
5555
{
56-
get { return _threadInfo._threadId; }
56+
get { return (int)_threadInfo._threadId; }
5757
}
5858

5959
/// <devdoc>

src/System.Diagnostics.Process/src/System/Diagnostics/ThreadInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace System.Diagnostics
1212
/// <internalonly/>
1313
internal sealed class ThreadInfo
1414
{
15-
internal int _threadId;
15+
internal ulong _threadId;
1616
internal int _processId;
1717
internal int _basePriority;
1818
internal int _currentPriority;

src/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests/Interop.cs

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using Microsoft.Win32.SafeHandles;
5+
using System.ComponentModel;
56
using System.Runtime.InteropServices;
7+
using System.Security.Principal;
68

79
namespace System.Diagnostics.ProcessTests
810
{
@@ -23,15 +25,35 @@ public struct PROCESS_MEMORY_COUNTERS
2325
public uint PeakPagefileUsage;
2426
}
2527

26-
[DllImport("api-ms-win-core-memory-l1-1-1.dll")]
27-
public static extern bool GetProcessWorkingSetSizeEx(SafeProcessHandle hProcess, out IntPtr lpMinimumWorkingSetSize, out IntPtr lpMaximumWorkingSetSize, out uint flags);
28+
[StructLayout(LayoutKind.Sequential)]
29+
internal struct USER_INFO_1
30+
{
31+
public string usri1_name;
32+
public string usri1_password;
33+
public uint usri1_password_age;
34+
public uint usri1_priv;
35+
public string usri1_home_dir;
36+
public string usri1_comment;
37+
public uint usri1_flags;
38+
public string usri1_script_path;
39+
}
2840

29-
[DllImport("api-ms-win-core-processthreads-l1-1-0", CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
30-
public static extern int GetPriorityClass(SafeProcessHandle handle);
41+
[StructLayout(LayoutKind.Sequential)]
42+
public struct TOKEN_USER
43+
{
44+
public SID_AND_ATTRIBUTES User;
45+
}
3146

32-
[DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
33-
public static extern SafeProcessHandle GetCurrentProcess();
47+
[StructLayout(LayoutKind.Sequential)]
48+
public struct SID_AND_ATTRIBUTES
49+
{
50+
public IntPtr Sid;
51+
public int Attributes;
52+
}
3453

54+
[DllImport("api-ms-win-core-memory-l1-1-1.dll")]
55+
public static extern bool GetProcessWorkingSetSizeEx(SafeProcessHandle hProcess, out IntPtr lpMinimumWorkingSetSize, out IntPtr lpMaximumWorkingSetSize, out uint flags);
56+
3557
[DllImport("api-ms-win-core-processthreads-l1-1-0.dll")]
3658
internal static extern int GetCurrentProcessId();
3759

@@ -58,5 +80,66 @@ public struct PROCESS_MEMORY_COUNTERS
5880

5981
[DllImport("api-ms-win-core-console-l1-1-0.dll")]
6082
internal extern static int SetConsoleOutputCP(int codePage);
83+
84+
[DllImport("netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
85+
internal extern static uint NetUserAdd(string servername, uint level, ref USER_INFO_1 buf, out uint parm_err);
86+
87+
[DllImport("netapi32.dll")]
88+
internal extern static uint NetUserDel(string servername, string username);
89+
90+
[DllImport("advapi32.dll")]
91+
internal static extern bool OpenProcessToken(SafeProcessHandle ProcessHandle, uint DesiredAccess, out SafeProcessHandle TokenHandle);
92+
93+
[DllImport("advapi32.dll")]
94+
internal static extern bool GetTokenInformation(SafeProcessHandle TokenHandle, uint TokenInformationClass, IntPtr TokenInformation, int TokenInformationLength, ref int ReturnLength);
95+
96+
internal static bool NetUserAdd(string username, string password)
97+
{
98+
USER_INFO_1 userInfo = new USER_INFO_1();
99+
userInfo.usri1_name = username;
100+
userInfo.usri1_password = password;
101+
userInfo.usri1_priv = 1;
102+
103+
uint parm_err;
104+
uint result = NetUserAdd(null, 1, ref userInfo, out parm_err);
105+
106+
if (result != 0)
107+
{
108+
throw new Win32Exception();
109+
}
110+
111+
return true;
112+
}
113+
114+
internal static bool ProcessTokenToSid(SafeProcessHandle token, out SecurityIdentifier sid)
115+
{
116+
bool ret = false;
117+
sid = null;
118+
IntPtr tu = IntPtr.Zero;
119+
try
120+
{
121+
TOKEN_USER tokUser;
122+
const int bufLength = 256;
123+
124+
tu = Marshal.AllocHGlobal(bufLength);
125+
int cb = bufLength;
126+
ret = GetTokenInformation(token, 1, tu, cb, ref cb);
127+
if (ret)
128+
{
129+
tokUser = Marshal.PtrToStructure<TOKEN_USER>(tu);
130+
sid = new SecurityIdentifier(tokUser.User.Sid);
131+
}
132+
return ret;
133+
}
134+
catch (Exception)
135+
{
136+
return false;
137+
}
138+
finally
139+
{
140+
if (tu != IntPtr.Zero)
141+
Marshal.FreeHGlobal(tu);
142+
}
143+
}
61144
}
62145
}

0 commit comments

Comments
 (0)