[Process] Fix NonChildProcessWaitForExit Exited callback (#4409)
[mono.git] / mcs / class / System / Test / System.Diagnostics / ProcessTest.cs
index ac07fdc8bab5cfb132beb28b8ede2f3a76dd832b..cb3827785af4dab4ced082075e03b608d27e1861 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;
 
@@ -1120,5 +1121,70 @@ namespace MonoTests.System.Diagnostics
                        // 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 ()))
+                       using (ManualResetEvent mre = new ManualResetEvent (false))
+                       {
+                               Assert.IsFalse (process.WaitForExit (10), "#1");
+                               Assert.IsFalse (process.HasExited, "#2");
+                               Assert.Throws<InvalidOperationException>(delegate { int exitCode = process.ExitCode; }, "#3");
+
+                               process.Exited += (s, e) => mre.Set ();
+                               process.EnableRaisingEvents = true;
+                               Assert.IsFalse (mre.WaitOne (100), "#4");
+
+                               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();
        }
 }