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

Commit b1fede3

Browse files
author
Lakshmi Priya Sekar
committed
Add test coverage to System.Diagnostics.Process types.
In this commit, represented internal ThreadInfo.threadId to ulong, as OSX threadIds are 64 bit. Changed implementation of PriorityLevel property on a Thread on OSX to throw PNSE. Added tests to types ProcessModule and ProcessThread.
1 parent bbbf587 commit b1fede3

15 files changed

+273
-23
lines changed

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;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Generic;
5+
using System.Security;
6+
using System.IO;
7+
using Xunit;
8+
using System.Threading;
9+
10+
namespace System.Diagnostics.ProcessTests
11+
{
12+
public class ProcessModuleTests : ProcessTestBase
13+
{
14+
[Fact, PlatformSpecific(~PlatformID.OSX)]
15+
public void TestModulePropertiesExceptOnOSX()
16+
{
17+
string fileName = CoreRunName;
18+
if (global::Interop.IsWindows)
19+
fileName = string.Format("{0}.exe", CoreRunName);
20+
21+
// Ensure the process has loaded the modules.
22+
Assert.True(SpinWait.SpinUntil(() =>
23+
{
24+
if (_process.Modules.Count > 0)
25+
return true;
26+
_process.Refresh();
27+
return false;
28+
}, WaitInMS));
29+
30+
ProcessModuleCollection moduleCollection = _process.Modules;
31+
Assert.True(moduleCollection.Count > 0);
32+
33+
ProcessModule coreRunModule = _process.MainModule;
34+
Assert.True(coreRunModule.BaseAddress.ToInt64() > 0);
35+
Assert.True(coreRunModule.EntryPointAddress.ToInt64() >= 0);
36+
Assert.Equal(fileName, coreRunModule.ModuleName);
37+
Assert.True(coreRunModule.ModuleMemorySize > 0);
38+
Assert.EndsWith(fileName, coreRunModule.FileName);
39+
40+
Assert.Equal(string.Format("System.Diagnostics.ProcessModule ({0})", fileName), coreRunModule.ToString());
41+
}
42+
43+
[Fact, PlatformSpecific(PlatformID.OSX)]
44+
public void TestModulePropertiesOnOSX()
45+
{
46+
// Getting modules for a process given a pid is not supported on OSX.
47+
ProcessModuleCollection moduleCollection = _process.Modules;
48+
Assert.Equal(0, moduleCollection.Count);
49+
Assert.Null(_process.MainModule);
50+
}
51+
}
52+
}

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

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

44
using System.Collections.Generic;
5+
using System.Security;
6+
using System.IO;
57
using Xunit;
68

79
namespace System.Diagnostics.ProcessTests
@@ -186,5 +188,59 @@ public void TestUseShellExecuteProperty()
186188
// Calling the getter
187189
Assert.False(psi.UseShellExecute, "UseShellExecute=true is not supported on onecore.");
188190
}
191+
192+
[Fact]
193+
public void TestArgumentsProperty()
194+
{
195+
ProcessStartInfo psi = new ProcessStartInfo();
196+
Assert.Equal(string.Empty, psi.Arguments);
197+
198+
psi = new ProcessStartInfo("filename", "-arg1 -arg2");
199+
Assert.Equal("-arg1 -arg2", psi.Arguments);
200+
}
201+
202+
[Fact]
203+
public void TestCreateNoWindowProperty()
204+
{
205+
Process testProcess = CreateProcessInfinite();
206+
try
207+
{
208+
testProcess.StartInfo.CreateNoWindow = true;
209+
testProcess.Start();
210+
211+
Assert.True(testProcess.StartInfo.CreateNoWindow);
212+
}
213+
finally
214+
{
215+
if (!testProcess.HasExited)
216+
testProcess.Kill();
217+
218+
Assert.True(testProcess.WaitForExit(WaitInMS));
219+
}
220+
}
221+
222+
[Fact, PlatformSpecific(PlatformID.Windows)]
223+
public void TestUserCredentialsPropertiesOnWindows()
224+
{
225+
// test the defaults here.
226+
Assert.Equal(string.Empty, _process.StartInfo.Domain);
227+
Assert.Equal(string.Empty, _process.StartInfo.UserName);
228+
Assert.Equal(default(SecureString), _process.StartInfo.Password);
229+
Assert.False(_process.StartInfo.LoadUserProfile);
230+
}
231+
232+
[Fact, PlatformSpecific(PlatformID.AnyUnix)]
233+
public void TestUserCredentialsPropertiesOnUnix()
234+
{
235+
Assert.Throws<PlatformNotSupportedException>(() => _process.StartInfo.Domain);
236+
Assert.Throws<PlatformNotSupportedException>(() => _process.StartInfo.UserName);
237+
Assert.Throws<PlatformNotSupportedException>(() => _process.StartInfo.Password);
238+
Assert.Throws<PlatformNotSupportedException>(() => _process.StartInfo.LoadUserProfile);
239+
}
240+
241+
public void TestWorkingDirectoryProperty()
242+
{
243+
Assert.Equal(Directory.GetCurrentDirectory(), _process.StartInfo.WorkingDirectory);
244+
}
189245
}
190246
}

0 commit comments

Comments
 (0)