[ProcessTest] Ensure we enable raising Exited event (#4397)
[mono.git] / mcs / class / System / Test / System.Diagnostics / ProcessTest.cs
index 20a93a732b6f1d1c3dc00368a5e0e0bc846db7c9..5db7246a4bd45ba8a60c47dc69427edb2d94f7c6 100644 (file)
@@ -14,6 +14,7 @@ using System.Diagnostics;
 using System.IO;
 using System.Text;
 using System.Threading;
+using System.Runtime.InteropServices;
 
 using NUnit.Framework;
 
@@ -1113,5 +1114,75 @@ namespace MonoTests.System.Diagnostics
                        }
                }
 #endif // MONO_FEATURE_PROCESS_START
+
+               [Test]
+               public void GetProcessesByName()
+               {
+                       // This should return Process[0] or a Process[] with all the "foo" programs running
+                       Process.GetProcessesByName ("foo");
+               }
+
+               [Test]
+               [ExpectedException(typeof(InvalidOperationException))]
+               public void HigherPrivilegeProcessName ()
+               {
+                       if (!RunningOnUnix)
+                               Assert.Ignore ("accessing pid 1, only available on unix");
+
+                       string v = Process.GetProcessById (1).ProcessName;
+               }
+
+               [Test]
+               public void NonChildProcessWaitForExit ()
+               {
+                       if (!RunningOnUnix)
+                               Assert.Ignore ("accessing parent pid, only available on unix");
+
+                       using (Process process = Process.GetProcessById (getppid ()))
+                       {
+                               Assert.IsFalse (process.WaitForExit (10), "#1");
+                               Assert.IsFalse (process.HasExited, "#2");
+                               Assert.Throws<InvalidOperationException>(delegate { int exitCode = process.ExitCode; }, "#3");
+
+                               process.Exited += (s, e) => Assert.Fail ("#4");
+                               process.EnableRaisingEvents = true;
+
+                               Assert.IsFalse (process.WaitForExit (10), "#5");
+                               Assert.IsFalse (process.HasExited, "#6");
+                               Assert.Throws<InvalidOperationException>(delegate { int exitCode = process.ExitCode; }, "#7");
+                       }
+               }
+
+               [Test]
+               public void NonChildProcessName ()
+               {
+                       if (!RunningOnUnix)
+                               Assert.Ignore ("accessing parent pid, only available on unix");
+
+                       using (Process process = Process.GetProcessById (getppid ()))
+                       {
+                               string pname = process.ProcessName;
+                               Assert.IsNotNull (pname, "#1");
+                               AssertHelper.IsNotEmpty (pname, "#2");
+                       }
+               }
+
+               [Test]
+               public void NonChildProcessId ()
+               {
+                       if (!RunningOnUnix)
+                               Assert.Ignore ("accessing parent pid, only available on unix");
+
+                       int ppid;
+                       using (Process process = Process.GetProcessById (ppid = getppid ()))
+                       {
+                               int pid = process.Id;
+                               Assert.AreEqual (ppid, pid, "#1");
+                               AssertHelper.Greater (pid, 0, "#2");
+                       }
+               }
+
+               [DllImport ("libc")]
+               static extern int getppid();
        }
 }