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

Commit 545771c

Browse files
author
Lakshmi Priya Sekar
committed
Add test coverage to System.Diagnostics.Process.
In this commit, removed HandleCount implementation, as it's not exposed in contract. Added some more functional tests. Reorganized the test structure to inherit from a TestBase. Renamed the tests to match the Given When Then naming scheme.
1 parent 9de86b6 commit 545771c

File tree

14 files changed

+454
-524
lines changed

14 files changed

+454
-524
lines changed

src/Common/src/Interop/OSX/Interop.libproc.cs

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ internal static partial class libproc
1818

1919
// Constants from proc_info.h
2020
private const int MAXTHREADNAMESIZE = 64;
21-
private const int PROC_PIDLISTFDS = 1;
2221
private const int PROC_PIDTASKALLINFO = 2;
2322
private const int PROC_PIDTHREADINFO = 5;
2423
private const int PROC_PIDLISTTHREADS = 6;
2524
private const int PROC_PIDPATHINFO_MAXSIZE = 4 * MAXPATHLEN;
26-
private static int PROC_PIDLISTFD_SIZE = Marshal.SizeOf<proc_fdinfo>();
2725
private static int PROC_PIDLISTTHREADS_SIZE = (Marshal.SizeOf<uint>() * 2);
2826

2927
// Constants from sys\resource.h
@@ -407,70 +405,6 @@ private static unsafe extern int proc_pidinfo(
407405
return threads;
408406
}
409407

410-
/// <summary>
411-
/// Retrieves the number of open file descriptors for the specified pid
412-
/// </summary>
413-
/// <returns>A count of file descriptors for this process.</returns>
414-
/// <remarks>
415-
/// This function doesn't use the helper since it seems to allow passing NULL
416-
/// values in to the buffer and length parameters to get back an estimation
417-
/// of how much data we will need to allocate; the other flavors don't seem
418-
/// to support doing that.
419-
/// </remarks>
420-
internal static unsafe int GetFileDescriptorCountForPid(int pid)
421-
{
422-
// Negative PIDs are invalid
423-
if (pid < 0)
424-
{
425-
throw new ArgumentOutOfRangeException("pid");
426-
}
427-
428-
// Query for an estimation about the size of the buffer we will need. This seems
429-
// to add some padding from the real number, so we don't need to do that
430-
int result = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, (proc_fdinfo*)null, 0);
431-
if (result <= 0)
432-
{
433-
// If we were unable to access the information, just return the empty list.
434-
// This is likely to happen for privileged processes, if the process went away
435-
// by the time we tried to query it, etc.
436-
return 0;
437-
}
438-
439-
proc_fdinfo[] fds;
440-
int size = (int)(result / Marshal.SizeOf<proc_fdinfo>()) + 1;
441-
442-
// Just in case the app opened a ton of handles between when we asked and now,
443-
// make sure we retry if our buffer is filled
444-
do
445-
{
446-
fds = new proc_fdinfo[size];
447-
fixed (proc_fdinfo* pFds = fds)
448-
{
449-
result = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, pFds, Marshal.SizeOf<proc_fdinfo>() * fds.Length);
450-
}
451-
452-
if (result <= 0)
453-
{
454-
// If we were unable to access the information, just return the empty list.
455-
// This is likely to happen for privileged processes, if the process went away
456-
// by the time we tried to query it, etc.
457-
return 0;
458-
}
459-
else
460-
{
461-
checked
462-
{
463-
size *= 2;
464-
}
465-
}
466-
}
467-
while (result == (fds.Length * Marshal.SizeOf<proc_fdinfo>()));
468-
469-
Debug.Assert((result % Marshal.SizeOf<proc_fdinfo>()) == 0);
470-
471-
return (int)(result / Marshal.SizeOf<proc_fdinfo>());
472-
}
473-
474408
/// <summary>
475409
/// Gets the full path to the executable file identified by the specified PID
476410
/// </summary>

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,6 @@ public DateTime ExitTime
199199
}
200200
}
201201

202-
/// <devdoc>
203-
/// <para>
204-
/// Gets the number of handles that are associated
205-
/// with the process.
206-
/// </para>
207-
/// </devdoc>
208-
public int HandleCount
209-
{
210-
get
211-
{
212-
EnsureState(State.HaveProcessInfo);
213-
return _processInfo.HandleCount;
214-
}
215-
}
216-
217202
/// <devdoc>
218203
/// <para>
219204
/// Gets

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ internal sealed class ProcessInfo
1818
internal int BasePriority { get; set; }
1919
internal string ProcessName { get; set; }
2020
internal int ProcessId { get; set; }
21-
internal int HandleCount { get; set; }
2221
internal long PoolPagedBytes { get; set; }
2322
internal long PoolNonPagedBytes { get; set; }
2423
internal long VirtualBytes { get; set; }
@@ -35,7 +34,6 @@ internal ProcessInfo()
3534
BasePriority = 0;
3635
ProcessName = "";
3736
ProcessId = 0;
38-
HandleCount = 0;
3937
PoolPagedBytes = 0;
4038
PoolNonPagedBytes = 0;
4139
VirtualBytes = 0;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ private static ProcessInfo CreateProcessInfo(int pid)
9494
VirtualBytes = (long)procFsStat.vsize,
9595
WorkingSet = procFsStat.rss,
9696
SessionId = procFsStat.session,
97-
HandleCount = 0, // not a Unix concept
9897

9998
// We don't currently fill in the other values.
10099
// A few of these could probably be filled in from getrusage,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ private static ProcessInfo CreateProcessInfo(int pid)
4040
Interop.libproc.proc_taskallinfo temp = info.Value;
4141
unsafe { procInfo.ProcessName = Marshal.PtrToStringAnsi(new IntPtr(temp.pbsd.pbi_comm)); }
4242
procInfo.BasePriority = temp.pbsd.pbi_nice;
43-
procInfo.HandleCount = Interop.libproc.GetFileDescriptorCountForPid(pid);
4443
procInfo.VirtualBytes = (long)temp.ptinfo.pti_virtual_size;
4544
procInfo.WorkingSet = (long)temp.ptinfo.pti_resident_size;
4645
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ internal static class NtProcessManager
229229
static NtProcessManager()
230230
{
231231
s_valueIds = new Dictionary<String, ValueId>();
232-
s_valueIds.Add("Handle Count", ValueId.HandleCount);
233232
s_valueIds.Add("Pool Paged Bytes", ValueId.PoolPagedBytes);
234233
s_valueIds.Add("Pool Nonpaged Bytes", ValueId.PoolNonpagedBytes);
235234
s_valueIds.Add("Elapsed Time", ValueId.ElapsedTime);
@@ -741,9 +740,6 @@ static ProcessInfo GetProcessInfo(Interop.mincore.PERF_OBJECT_TYPE type, IntPtr
741740
case ValueId.ProcessId:
742741
processInfo.ProcessId = (int)value;
743742
break;
744-
case ValueId.HandleCount:
745-
processInfo.HandleCount = (int)value;
746-
break;
747743
case ValueId.PoolPagedBytes:
748744
processInfo.PoolPagedBytes = value;
749745
break;
@@ -802,7 +798,6 @@ static long ReadCounterValue(int counterType, IntPtr dataPtr)
802798
enum ValueId
803799
{
804800
Unknown = -1,
805-
HandleCount,
806801
PoolPagedBytes,
807802
PoolNonpagedBytes,
808803
ElapsedTime,
@@ -950,7 +945,6 @@ static ProcessInfo[] GetProcessInfos(IntPtr dataPtr)
950945
ProcessInfo processInfo = new ProcessInfo();
951946
// Process ID shouldn't overflow. OS API GetCurrentProcessID returns DWORD.
952947
processInfo.ProcessId = pi.UniqueProcessId.ToInt32();
953-
processInfo.HandleCount = (int)pi.HandleCount;
954948
processInfo.SessionId = (int)pi.SessionId;
955949
processInfo.PoolPagedBytes = (long)pi.QuotaPagedPoolUsage; ;
956950
processInfo.PoolNonPagedBytes = (long)pi.QuotaNonPagedPoolUsage;

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

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
using Microsoft.Win32.SafeHandles;
2-
using System;
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 Microsoft.Win32.SafeHandles;
35
using System.Runtime.InteropServices;
46

57
namespace System.Diagnostics.ProcessTests
68
{
7-
class Interop
9+
internal class Interop
810
{
9-
[DllImport("api-ms-win-core-processthreads-l1-1-0.dll")]
10-
public static extern int GetProcessId(SafeProcessHandle nativeHandle);
11-
12-
[DllImport("psapi.dll", SetLastError = true)]
13-
public static extern bool GetProcessMemoryInfo(IntPtr hProcess, out PROCESS_MEMORY_COUNTERS counters, uint size);
14-
1511
[StructLayout(LayoutKind.Sequential, Size = 40)]
1612
public struct PROCESS_MEMORY_COUNTERS
1713
{
@@ -30,11 +26,37 @@ public struct PROCESS_MEMORY_COUNTERS
3026
[DllImport("api-ms-win-core-memory-l1-1-1.dll")]
3127
public static extern bool GetProcessWorkingSetSizeEx(SafeProcessHandle hProcess, out IntPtr lpMinimumWorkingSetSize, out IntPtr lpMaximumWorkingSetSize, out uint flags);
3228

33-
3429
[DllImport("api-ms-win-core-processthreads-l1-1-0", CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
3530
public static extern int GetPriorityClass(SafeProcessHandle handle);
3631

3732
[DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
3833
public static extern SafeProcessHandle GetCurrentProcess();
34+
35+
[DllImport("api-ms-win-core-processthreads-l1-1-0.dll")]
36+
internal static extern int GetCurrentProcessId();
37+
38+
[DllImport("libc")]
39+
internal static extern int getpid();
40+
41+
[DllImport("libc")]
42+
internal static extern int getsid(int pid);
43+
44+
[DllImport("api-ms-win-core-processthreads-l1-1-2.dll")]
45+
internal static extern bool ProcessIdToSessionId(uint dwProcessId, out uint pSessionId);
46+
47+
[DllImport("api-ms-win-core-processthreads-l1-1-0.dll")]
48+
public static extern int GetProcessId(SafeProcessHandle nativeHandle);
49+
50+
[DllImport("api-ms-win-core-console-l1-1-0.dll")]
51+
internal extern static int GetConsoleCP();
52+
53+
[DllImport("api-ms-win-core-console-l1-1-0.dll")]
54+
internal extern static int GetConsoleOutputCP();
55+
56+
[DllImport("api-ms-win-core-console-l1-1-0.dll")]
57+
internal extern static int SetConsoleCP(int codePage);
58+
59+
[DllImport("api-ms-win-core-console-l1-1-0.dll")]
60+
internal extern static int SetConsoleOutputCP(int codePage);
3961
}
4062
}

src/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests/Process_EncodingTest.cs renamed to src/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests/ProcessStandardConsoleTests.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,12 @@
66

77
namespace System.Diagnostics.ProcessTests
88
{
9-
public partial class ProcessTest
9+
public class ProcessStandardConsoleTests : ProcessTestBase
1010
{
11-
[System.Runtime.InteropServices.DllImport("api-ms-win-core-console-l1-1-0.dll")]
12-
private extern static int GetConsoleCP();
13-
14-
[System.Runtime.InteropServices.DllImport("api-ms-win-core-console-l1-1-0.dll")]
15-
private extern static int GetConsoleOutputCP();
16-
17-
[System.Runtime.InteropServices.DllImport("api-ms-win-core-console-l1-1-0.dll")]
18-
private extern static int SetConsoleCP(int codePage);
19-
20-
[System.Runtime.InteropServices.DllImport("api-ms-win-core-console-l1-1-0.dll")]
21-
private extern static int SetConsoleOutputCP(int codePage);
22-
2311
private const int s_ConsoleEncoding = 437;
2412

2513
[Fact]
26-
public void Process_EncodingBeforeProvider()
14+
public void TestChangesInConsoleEncoding()
2715
{
2816
Action<int> run = expectedCodePage =>
2917
{
@@ -47,21 +35,21 @@ public void Process_EncodingBeforeProvider()
4735
return;
4836
}
4937

50-
int inputEncoding = GetConsoleCP();
51-
int outputEncoding = GetConsoleOutputCP();
38+
int inputEncoding = Interop.GetConsoleCP();
39+
int outputEncoding = Interop.GetConsoleOutputCP();
5240

5341
try
5442
{
5543
{
56-
SetConsoleCP(s_ConsoleEncoding);
57-
SetConsoleOutputCP(s_ConsoleEncoding);
44+
Interop.SetConsoleCP(s_ConsoleEncoding);
45+
Interop.SetConsoleOutputCP(s_ConsoleEncoding);
5846

5947
run(Encoding.UTF8.CodePage);
6048
}
6149

6250
{
63-
SetConsoleCP(s_ConsoleEncoding);
64-
SetConsoleOutputCP(s_ConsoleEncoding);
51+
Interop.SetConsoleCP(s_ConsoleEncoding);
52+
Interop.SetConsoleOutputCP(s_ConsoleEncoding);
6553

6654
// Register the codeprovider which will ensure 437 is enabled.
6755
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
@@ -71,10 +59,9 @@ public void Process_EncodingBeforeProvider()
7159
}
7260
finally
7361
{
74-
SetConsoleCP(inputEncoding);
75-
SetConsoleOutputCP(outputEncoding);
62+
Interop.SetConsoleCP(inputEncoding);
63+
Interop.SetConsoleOutputCP(outputEncoding);
7664
}
7765
}
78-
7966
}
8067
}

0 commit comments

Comments
 (0)