[process] Improve error message for inaccessible process (#4354)
[mono.git] / mcs / class / System / Test / System.Diagnostics / ProcessTest.cs
index 75c4614ad2a433dbcdf00f7704d8e6dfba225fff..dcf83695f06517fa3d91dd10a1519d2bf7f5cf91 100644 (file)
@@ -284,7 +284,9 @@ namespace MonoTests.System.Diagnostics
                                Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
                                Assert.IsNull (ex.InnerException, "#4");
                                Assert.IsNotNull (ex.Message, "#5");
-                               Assert.AreEqual (2, ex.NativeErrorCode, "#6");
+                               // TODO: On windows we get ACCESS_DENIED (5) instead of FILE_NOT_FOUND (2) and .NET
+                               // gives ERROR_INVALID_PARAMETER (87). See https://bugzilla.xamarin.com/show_bug.cgi?id=44514
+                               Assert.IsTrue (ex.NativeErrorCode == 2 || ex.NativeErrorCode == 5 || ex.NativeErrorCode == 87, "#6");
                        }
                }
 
@@ -440,7 +442,9 @@ namespace MonoTests.System.Diagnostics
                                Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
                                Assert.IsNull (ex.InnerException, "#4");
                                Assert.IsNotNull (ex.Message, "#5");
-                               Assert.AreEqual (2, ex.NativeErrorCode, "#6");
+                               // TODO: On windows we get ACCESS_DENIED (5) instead of FILE_NOT_FOUND (2) and .NET
+                               // gives ERROR_INVALID_PARAMETER (87). See https://bugzilla.xamarin.com/show_bug.cgi?id=44514
+                               Assert.IsTrue (ex.NativeErrorCode == 2 || ex.NativeErrorCode == 5 || ex.NativeErrorCode == 87, "#6");
                        }
                }
 
@@ -892,8 +896,11 @@ namespace MonoTests.System.Diagnostics
                                path = "/bin/cat";
 #endif
                                return new ProcessStartInfo (path);
-                       } else
-                               return new ProcessStartInfo ("type");
+                       } else {
+                               var psi = new ProcessStartInfo ("findstr");
+                               psi.Arguments = "\"^\"";
+                               return psi;
+                       }
                }
 #endif // MONO_FEATURE_PROCESS_START
 
@@ -1018,7 +1025,7 @@ namespace MonoTests.System.Diagnostics
 
                                StringBuilder sb = new StringBuilder ();
                                sb.AppendFormat ("Could not found: {0} {1}\n", name.Name, name.Version);
-                               sb.AppendLine ("Looked in assemblies:");
+                               sb.AppendLine ("Looked in modules:");
 
                                foreach (var o in modules) {
                                        var m = (ProcessModule) o;
@@ -1084,6 +1091,44 @@ namespace MonoTests.System.Diagnostics
                                Assert.Fail ();
                        }
                }
+
+               [Test]
+               [NUnit.Framework.Category ("MobileNotWorking")]
+               public void TestExitedRaisedTooSoon ()
+               {
+                       if (!RunningOnUnix)
+                               Assert.Ignore ("using sleep command, only available on unix");
+
+                       int sleeptime = 5;
+
+                       using (Process p = Process.Start("sleep", sleeptime.ToString ())) {
+                               ManualResetEvent mre = new ManualResetEvent (false);
+
+                               p.EnableRaisingEvents = true;
+                               p.Exited += (sender, e) => {
+                                       mre.Set ();
+                               };
+
+                               Assert.IsFalse (mre.WaitOne ((sleeptime - 2) * 1000), "Exited triggered before the process returned");
+                       }
+               }
 #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;
+               }
        }
 }