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

Commit 03c5b0b

Browse files
committed
Merge pull request #2592 from Priya91/process
Add more process tests.
2 parents b047ab4 + 8b7ccd0 commit 03c5b0b

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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;
5+
using System.Linq;
6+
using Xunit;
7+
8+
namespace System.Diagnostics.ProcessTests
9+
{
10+
public class ProcessCollectionTests : ProcessTestBase
11+
{
12+
[Fact]
13+
public void TestModuleCollectionBehavior()
14+
{
15+
ProcessModule[] mArray = _process.Modules.Cast<ProcessModule>().ToArray();
16+
17+
// Constructor
18+
ProcessModuleCollection moduleCollection = new ProcessModuleCollection(mArray);
19+
20+
// Count
21+
Assert.Equal(mArray.Count(), moduleCollection.Count);
22+
23+
// get_item, Contains, IndexOf
24+
for (int i = 0; i < mArray.Count(); i++)
25+
{
26+
Assert.Equal(mArray[i], moduleCollection[i]);
27+
Assert.True(moduleCollection.Contains(mArray[i]));
28+
Assert.Equal(i, moduleCollection.IndexOf(mArray[i]));
29+
}
30+
31+
// CopyTo
32+
ProcessModule[] moduleArray = new ProcessModule[moduleCollection.Count + 1];
33+
moduleCollection.CopyTo(moduleArray, 1);
34+
for (int i = 0; i < mArray.Count(); i++)
35+
{
36+
Assert.Equal(mArray[i], moduleArray[i + 1]);
37+
}
38+
39+
Assert.Throws<ArgumentOutOfRangeException>(() => moduleCollection.CopyTo(moduleArray, -1));
40+
41+
// Explicit interface implementations
42+
Assert.False(((ICollection)moduleCollection).IsSynchronized);
43+
Assert.NotNull(((ICollection)moduleCollection).SyncRoot);
44+
45+
moduleArray = new ProcessModule[moduleCollection.Count];
46+
((ICollection)moduleCollection).CopyTo(moduleArray, 0);
47+
Assert.Equal(moduleCollection.Cast<ProcessModule>().ToArray(), moduleArray);
48+
49+
// GetEnumerator
50+
IEnumerator enumerator = moduleCollection.GetEnumerator();
51+
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
52+
53+
for (int i = 0; i < moduleCollection.Count; i++)
54+
{
55+
enumerator.MoveNext();
56+
Assert.Equal(moduleCollection[i], enumerator.Current);
57+
}
58+
}
59+
60+
[Fact]
61+
public void TestThreadCollectionBehavior()
62+
{
63+
ProcessThread[] tArray = _process.Threads.Cast<ProcessThread>().ToArray();
64+
int countOfTArray = tArray.Count();
65+
66+
// constructor
67+
ProcessThreadCollection threadCollection = new ProcessThreadCollection(tArray);
68+
69+
// Count
70+
Assert.Equal(countOfTArray, threadCollection.Count);
71+
72+
// get_item, Contains, IndexOf
73+
for (int i = 0; i < countOfTArray; i++)
74+
{
75+
Assert.Equal(tArray[i], threadCollection[i]);
76+
Assert.True(threadCollection.Contains(tArray[i]));
77+
Assert.Equal(i, threadCollection.IndexOf(tArray[i]));
78+
}
79+
80+
// CopyTo
81+
ProcessThread[] threadArray = new ProcessThread[threadCollection.Count + 1];
82+
threadCollection.CopyTo(threadArray, 1);
83+
for (int i = 0; i < countOfTArray; i++)
84+
{
85+
Assert.Equal(tArray[i], threadArray[i + 1]);
86+
}
87+
88+
Assert.Throws<ArgumentOutOfRangeException>(() => threadCollection.CopyTo(threadArray, -1));
89+
90+
// Remove
91+
threadCollection.Remove(tArray[0]);
92+
Assert.Equal(-1, threadCollection.IndexOf(tArray[0]));
93+
Assert.False(threadCollection.Contains(tArray[0]));
94+
// Try remove non existent member
95+
threadCollection.Remove(tArray[0]);
96+
// Cleanup after remove
97+
threadCollection.Insert(0, tArray[0]);
98+
99+
// Add
100+
threadCollection.Add(default(ProcessThread));
101+
Assert.Equal(threadCollection.Count - 1, threadCollection.IndexOf(default(ProcessThread)));
102+
// Add same member again
103+
threadCollection.Add(default(ProcessThread));
104+
Assert.Equal(threadCollection.Count - 2, threadCollection.IndexOf(default(ProcessThread)));
105+
Assert.Equal(default(ProcessThread), threadCollection[threadCollection.Count - 1]);
106+
// Cleanup after Add.
107+
threadCollection.Remove(default(ProcessThread));
108+
threadCollection.Remove(default(ProcessThread));
109+
Assert.False(threadCollection.Contains(default(ProcessThread)));
110+
111+
// Insert
112+
int index = threadCollection.Count / 2;
113+
int initialCount = threadCollection.Count;
114+
threadCollection.Insert(index, null);
115+
Assert.Equal(index, threadCollection.IndexOf(null));
116+
Assert.Equal(initialCount + 1, threadCollection.Count);
117+
// Insert at invalid index
118+
Assert.Throws<ArgumentOutOfRangeException>(() => threadCollection.Insert(-1, tArray[0]));
119+
120+
// Explicit interface implementations
121+
Assert.False(((ICollection)threadCollection).IsSynchronized);
122+
Assert.NotNull(((ICollection)threadCollection).SyncRoot);
123+
124+
threadArray = new ProcessThread[threadCollection.Count];
125+
((ICollection)threadCollection).CopyTo(threadArray, 0);
126+
Assert.Equal(threadCollection.Cast<ProcessThread>().ToArray(), threadArray);
127+
128+
// GetEnumerator
129+
IEnumerator enumerator = threadCollection.GetEnumerator();
130+
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
131+
132+
for (int i = 0; i < threadCollection.Count; i++)
133+
{
134+
enumerator.MoveNext();
135+
Assert.Equal(threadCollection[i], enumerator.Current);
136+
}
137+
}
138+
}
139+
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,34 @@ public void TestGetProcessesByName()
544544
Assert.True(Process.GetProcessesByName(currentProcess.ProcessName, currentProcess.MachineName).Count() > 0, "TestGetProcessesByName001 failed");
545545
}
546546

547+
public static IEnumerable<object[]> GetTestProcess()
548+
{
549+
Process currentProcess = Process.GetCurrentProcess();
550+
yield return new object[] { currentProcess, Process.GetProcessById(currentProcess.Id, "127.0.0.1") };
551+
yield return new object[] { currentProcess, Process.GetProcessesByName(currentProcess.ProcessName, "127.0.0.1").Where(p => p.Id == currentProcess.Id).Single() };
552+
}
553+
554+
[Theory, PlatformSpecific(PlatformID.Windows)]
555+
[MemberData("GetTestProcess")]
556+
public void TestProcessOnRemoteMachineWindows(Process currentProcess, Process remoteProcess)
557+
{
558+
Assert.Equal(currentProcess.Id, remoteProcess.Id);
559+
Assert.Equal(currentProcess.BasePriority, remoteProcess.BasePriority);
560+
Assert.Equal(currentProcess.EnableRaisingEvents, remoteProcess.EnableRaisingEvents);
561+
Assert.Equal("127.0.0.1", remoteProcess.MachineName);
562+
// This property throws exception only on remote processes.
563+
Assert.Throws<NotSupportedException>(() => remoteProcess.MainModule);
564+
}
565+
566+
[Fact, PlatformSpecific(PlatformID.AnyUnix)]
567+
public void TestProcessOnRemoteMachineUnix()
568+
{
569+
Process currentProcess = Process.GetCurrentProcess();
570+
571+
Assert.Throws<PlatformNotSupportedException>(() => Process.GetProcessesByName(currentProcess.ProcessName, "127.0.0.1"));
572+
Assert.Throws<PlatformNotSupportedException>(() => Process.GetProcessById(currentProcess.Id, "127.0.0.1"));
573+
}
574+
547575
[Fact]
548576
public void TestStartInfo()
549577
{

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public void TestCommonPriorityAndTimeProperties()
2222

2323
if (ThreadState.Terminated != thread.ThreadState)
2424
{
25+
Assert.True(thread.Id >= 0);
2526
Assert.Equal(_process.BasePriority, thread.BasePriority);
2627
Assert.True(thread.CurrentPriority >= 0);
2728
Assert.True(thread.PrivilegedProcessorTime.TotalSeconds >= 0);

src/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests/System.Diagnostics.Process.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<Compile Include="$(CommonTestPath)\System\Diagnostics\AssertWithCallerAttributes.cs">
2222
<Link>Common\tests\System\Diagnostics\AssertWithCallerAttributes.cs</Link>
2323
</Compile>
24+
<Compile Include="ProcessCollectionTests.cs" />
2425
<Compile Include="ProcessModuleTests.cs" />
2526
<Compile Include="ProcessStartInfoTests.cs" />
2627
<Compile Include="Interop.cs" />

0 commit comments

Comments
 (0)