2 // ProcessTest.cs - NUnit Test Cases for System.Diagnostics.Process
5 // Gert Driesen (drieseng@users.sourceforge.net)
6 // Robert Jordan <robertj@gmx.net>
8 // (C) 2007 Gert Driesen
12 using System.ComponentModel;
13 using System.Diagnostics;
16 using System.Threading;
18 using NUnit.Framework;
20 namespace MonoTests.System.Diagnostics
23 public class ProcessTest
26 public void GetProcessById_MachineName_Null ()
29 Process.GetProcessById (1, (string) null);
31 } catch (ArgumentNullException ex) {
32 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
33 Assert.IsNotNull (ex.Message, "#3");
34 Assert.IsNotNull (ex.ParamName, "#4");
35 Assert.AreEqual ("machineName", ex.ParamName, "#5");
36 Assert.IsNull (ex.InnerException, "#6");
41 public void GetProcesses_MachineName_Null ()
44 Process.GetProcesses ((string) null);
46 } catch (ArgumentNullException ex) {
47 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
48 Assert.IsNotNull (ex.Message, "#3");
49 Assert.IsNotNull (ex.ParamName, "#4");
50 Assert.AreEqual ("machineName", ex.ParamName, "#5");
51 Assert.IsNull (ex.InnerException, "#6");
55 [Test] // Covers #26363
56 [NUnit.Framework.Category ("MobileNotWorking")]
57 public void GetProcesses_StartTime ()
59 foreach (var p in Process.GetProcesses ()) {
60 if (!p.HasExited && p.StartTime.Year < 1800)
61 Assert.Fail ("Process should not be started since the 18th century.");
66 public void PriorityClass_NotStarted ()
68 Process process = new Process ();
70 process.PriorityClass = ProcessPriorityClass.Normal;
72 } catch (InvalidOperationException ex) {
73 // No process is associated with this object
74 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
75 Assert.IsNull (ex.InnerException, "#A3");
76 Assert.IsNotNull (ex.Message, "#A4");
80 Assert.Fail ("#B1:" + process.PriorityClass);
81 } catch (InvalidOperationException ex) {
82 // No process is associated with this object
83 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
84 Assert.IsNull (ex.InnerException, "#B3");
85 Assert.IsNotNull (ex.Message, "#B4");
90 public void PriorityClass_Invalid ()
92 Process process = new Process ();
94 process.PriorityClass = (ProcessPriorityClass) 666;
96 } catch (InvalidEnumArgumentException ex) {
97 Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
98 Assert.IsNull (ex.InnerException, "#3");
99 Assert.IsNotNull (ex.Message, "#4");
100 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#5");
101 Assert.IsTrue (ex.Message.IndexOf (typeof (ProcessPriorityClass).Name) != -1, "#6");
102 Assert.IsNotNull (ex.ParamName, "#7");
103 Assert.AreEqual ("value", ex.ParamName, "#8");
107 #if MONO_FEATURE_PROCESS_START
109 public void Start1_FileName_Empty ()
111 Process process = new Process ();
112 process.StartInfo = new ProcessStartInfo (string.Empty);
115 process.StartInfo.UseShellExecute = false;
119 } catch (InvalidOperationException ex) {
120 // Cannot start process because a file name has
122 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
123 Assert.IsNull (ex.InnerException, "#A3");
124 Assert.IsNotNull (ex.Message, "#A4");
128 process.StartInfo.UseShellExecute = true;
132 } catch (InvalidOperationException ex) {
133 // Cannot start process because a file name has
135 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
136 Assert.IsNull (ex.InnerException, "#B3");
137 Assert.IsNotNull (ex.Message, "#B4");
142 public void Start1_FileName_InvalidPathCharacters ()
145 // on unix, all characters are allowed
146 Assert.Ignore ("Running on Unix.");
148 string systemDir = Environment.GetFolderPath (Environment.SpecialFolder.System);
149 string exe = "\"" + Path.Combine (systemDir, "calc.exe") + "\"";
151 Process process = new Process ();
152 process.StartInfo = new ProcessStartInfo (exe);
155 process.StartInfo.UseShellExecute = false;
156 Assert.IsTrue (process.Start ());
160 process.StartInfo.UseShellExecute = true;
161 Assert.IsTrue (process.Start ());
166 public void Start1_FileName_NotFound ()
168 Process process = new Process ();
169 string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
170 : @"c:\shouldnoteverexist.exe";
172 // absolute path, no shell
173 process.StartInfo = new ProcessStartInfo (exe);
174 process.StartInfo.UseShellExecute = false;
178 } catch (Win32Exception ex) {
179 // The system cannot find the file specified
180 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
181 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
182 Assert.IsNull (ex.InnerException, "#A4");
183 Assert.IsNotNull (ex.Message, "#A5");
184 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
187 // relative path, no shell
188 process.StartInfo.FileName = "shouldnoteverexist.exe";
189 process.StartInfo.UseShellExecute = false;
193 } catch (Win32Exception ex) {
194 // The system cannot find the file specified
195 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
196 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
197 Assert.IsNull (ex.InnerException, "#B4");
198 Assert.IsNotNull (ex.Message, "#B5");
199 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
203 Assert.Ignore ("On Unix and Mac OS X, we try " +
204 "to open any file (using xdg-open, ...)" +
205 " and we do not report an exception " +
208 // absolute path, shell
209 process.StartInfo.FileName = exe;
210 process.StartInfo.UseShellExecute = true;
214 } catch (Win32Exception ex) {
215 // The system cannot find the file specified
216 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
217 Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
218 Assert.IsNull (ex.InnerException, "#C4");
219 Assert.IsNotNull (ex.Message, "#C5");
220 Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
223 // relative path, shell
224 process.StartInfo.FileName = "shouldnoteverexist.exe";
225 process.StartInfo.UseShellExecute = true;
229 } catch (Win32Exception ex) {
230 // The system cannot find the file specified
231 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
232 Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
233 Assert.IsNull (ex.InnerException, "#D4");
234 Assert.IsNotNull (ex.Message, "#D5");
235 Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
240 public void Start1_FileName_Null ()
242 Process process = new Process ();
243 process.StartInfo = new ProcessStartInfo ((string) null);
246 process.StartInfo.UseShellExecute = false;
250 } catch (InvalidOperationException ex) {
251 // Cannot start process because a file name has
253 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
254 Assert.IsNull (ex.InnerException, "#A3");
255 Assert.IsNotNull (ex.Message, "#A4");
259 process.StartInfo.UseShellExecute = true;
263 } catch (InvalidOperationException ex) {
264 // Cannot start process because a file name has
266 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
267 Assert.IsNull (ex.InnerException, "#B3");
268 Assert.IsNotNull (ex.Message, "#B4");
273 public void Start1_FileName_Whitespace ()
275 Process process = new Process ();
276 process.StartInfo = new ProcessStartInfo (" ");
277 process.StartInfo.UseShellExecute = false;
281 } catch (Win32Exception ex) {
282 // The system cannot find the file specified
283 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
284 Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
285 Assert.IsNull (ex.InnerException, "#4");
286 Assert.IsNotNull (ex.Message, "#5");
287 // TODO: On windows we get ACCESS_DENIED (5) instead of FILE_NOT_FOUND (2) and .NET
288 // gives ERROR_INVALID_PARAMETER (87). See https://bugzilla.xamarin.com/show_bug.cgi?id=44514
289 Assert.IsTrue (ex.NativeErrorCode == 2 || ex.NativeErrorCode == 5 || ex.NativeErrorCode == 87, "#6");
293 [Test] // Start (ProcessStartInfo)
294 public void Start2_FileName_Empty ()
296 ProcessStartInfo startInfo = new ProcessStartInfo (string.Empty);
299 startInfo.UseShellExecute = false;
301 Process.Start (startInfo);
303 } catch (InvalidOperationException ex) {
304 // Cannot start process because a file name has
306 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
307 Assert.IsNull (ex.InnerException, "#A3");
308 Assert.IsNotNull (ex.Message, "#A4");
312 startInfo.UseShellExecute = true;
314 Process.Start (startInfo);
316 } catch (InvalidOperationException ex) {
317 // Cannot start process because a file name has
319 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
320 Assert.IsNull (ex.InnerException, "#B3");
321 Assert.IsNotNull (ex.Message, "#B4");
325 [Test] // Start (ProcessStartInfo)
326 public void Start2_FileName_NotFound ()
328 ProcessStartInfo startInfo = new ProcessStartInfo ();
329 string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
330 : @"c:\shouldnoteverexist.exe";
332 // absolute path, no shell
333 startInfo.FileName = exe;
334 startInfo.UseShellExecute = false;
336 Process.Start (startInfo);
338 } catch (Win32Exception ex) {
339 // The system cannot find the file specified
340 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
341 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
342 Assert.IsNull (ex.InnerException, "#A4");
343 Assert.IsNotNull (ex.Message, "#A5");
344 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
347 // relative path, no shell
348 startInfo.FileName = "shouldnoteverexist.exe";
349 startInfo.UseShellExecute = false;
351 Process.Start (startInfo);
353 } catch (Win32Exception ex) {
354 // The system cannot find the file specified
355 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
356 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
357 Assert.IsNull (ex.InnerException, "#B4");
358 Assert.IsNotNull (ex.Message, "#B5");
359 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
363 Assert.Ignore ("On Unix and Mac OS X, we try " +
364 "to open any file (using xdg-open, ...)" +
365 " and we do not report an exception " +
368 // absolute path, shell
369 startInfo.FileName = exe;
370 startInfo.UseShellExecute = true;
372 Process.Start (startInfo);
374 } catch (Win32Exception ex) {
375 // The system cannot find the file specified
376 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
377 Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
378 Assert.IsNull (ex.InnerException, "#C4");
379 Assert.IsNotNull (ex.Message, "#C5");
380 Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
383 // relative path, shell
384 startInfo.FileName = "shouldnoteverexist.exe";
385 startInfo.UseShellExecute = true;
387 Process.Start (startInfo);
389 } catch (Win32Exception ex) {
390 // The system cannot find the file specified
391 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
392 Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
393 Assert.IsNull (ex.InnerException, "#D4");
394 Assert.IsNotNull (ex.Message, "#D5");
395 Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
399 [Test] // Start (ProcessStartInfo)
400 public void Start2_FileName_Null ()
402 ProcessStartInfo startInfo = new ProcessStartInfo ((string) null);
405 startInfo.UseShellExecute = false;
407 Process.Start (startInfo);
409 } catch (InvalidOperationException ex) {
410 // Cannot start process because a file name has
412 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
413 Assert.IsNull (ex.InnerException, "#A3");
414 Assert.IsNotNull (ex.Message, "#A4");
418 startInfo.UseShellExecute = true;
420 Process.Start (startInfo);
422 } catch (InvalidOperationException ex) {
423 // Cannot start process because a file name has
425 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
426 Assert.IsNull (ex.InnerException, "#B3");
427 Assert.IsNotNull (ex.Message, "#B4");
431 [Test] // Start (ProcessStartInfo)
432 public void Start2_FileName_Whitespace ()
434 ProcessStartInfo startInfo = new ProcessStartInfo (" ");
435 startInfo.UseShellExecute = false;
437 Process.Start (startInfo);
439 } catch (Win32Exception ex) {
440 // The system cannot find the file specified
441 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
442 Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
443 Assert.IsNull (ex.InnerException, "#4");
444 Assert.IsNotNull (ex.Message, "#5");
445 // TODO: On windows we get ACCESS_DENIED (5) instead of FILE_NOT_FOUND (2) and .NET
446 // gives ERROR_INVALID_PARAMETER (87). See https://bugzilla.xamarin.com/show_bug.cgi?id=44514
447 Assert.IsTrue (ex.NativeErrorCode == 2 || ex.NativeErrorCode == 5 || ex.NativeErrorCode == 87, "#6");
451 [Test] // Start (ProcessStartInfo)
452 public void Start2_StartInfo_Null ()
455 Process.Start ((ProcessStartInfo) null);
457 } catch (ArgumentNullException ex) {
458 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
459 Assert.IsNull (ex.InnerException, "#3");
460 Assert.IsNotNull (ex.Message, "#4");
461 Assert.IsNotNull (ex.ParamName, "#5");
462 Assert.AreEqual ("startInfo", ex.ParamName, "#6");
466 [Test] // Start (string)
467 public void Start3_FileName_Empty ()
470 Process.Start (string.Empty);
472 } catch (InvalidOperationException ex) {
473 // Cannot start process because a file name has
475 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
476 Assert.IsNull (ex.InnerException, "#3");
477 Assert.IsNotNull (ex.Message, "#4");
481 [Test] // Start (string)
482 public void Start3_FileName_NotFound ()
485 Assert.Ignore ("On Unix and Mac OS X, we try " +
486 "to open any file (using xdg-open, ...)" +
487 " and we do not report an exception " +
490 string exe = @"c:\shouldnoteverexist.exe";
492 // absolute path, no shell
496 } catch (Win32Exception ex) {
497 // The system cannot find the file specified
498 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
499 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
500 Assert.IsNull (ex.InnerException, "#A4");
501 Assert.IsNotNull (ex.Message, "#A5");
502 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
505 // relative path, no shell
507 Process.Start ("shouldnoteverexist.exe");
509 } catch (Win32Exception ex) {
510 // The system cannot find the file specified
511 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
512 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
513 Assert.IsNull (ex.InnerException, "#B4");
514 Assert.IsNotNull (ex.Message, "#B5");
515 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
519 [Test] // Start (string)
520 public void Start3_FileName_Null ()
523 Process.Start ((string) null);
525 } catch (InvalidOperationException ex) {
526 // Cannot start process because a file name has
528 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
529 Assert.IsNull (ex.InnerException, "#3");
530 Assert.IsNotNull (ex.Message, "#4");
534 [Test] // Start (string, string)
535 public void Start4_Arguments_Null ()
538 Assert.Ignore ("On Unix and Mac OS X, we try " +
539 "to open any file (using xdg-open, ...)" +
540 " and we do not report an exception " +
543 string exe = @"c:\shouldnoteverexist.exe";
546 Process.Start ("whatever.exe", (string) null);
548 } catch (Win32Exception ex) {
549 // The system cannot find the file specified
550 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
551 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
552 Assert.IsNull (ex.InnerException, "#B4");
553 Assert.IsNotNull (ex.Message, "#B5");
554 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
558 [Test] // Start (string, string)
559 public void Start4_FileName_Empty ()
562 Process.Start (string.Empty, string.Empty);
564 } catch (InvalidOperationException ex) {
565 // Cannot start process because a file name has
567 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
568 Assert.IsNull (ex.InnerException, "#3");
569 Assert.IsNotNull (ex.Message, "#4");
573 [Test] // Start (string, string)
574 public void Start4_FileName_NotFound ()
577 Assert.Ignore ("On Unix and Mac OS X, we try " +
578 "to open any file (using xdg-open, ...)" +
579 " and we do not report an exception " +
582 string exe = @"c:\shouldnoteverexist.exe";
584 // absolute path, no shell
586 Process.Start (exe, string.Empty);
588 } catch (Win32Exception ex) {
589 // The system cannot find the file specified
590 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
591 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
592 Assert.IsNull (ex.InnerException, "#A4");
593 Assert.IsNotNull (ex.Message, "#A5");
594 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
597 // relative path, no shell
599 Process.Start ("shouldnoteverexist.exe", string.Empty);
601 } catch (Win32Exception ex) {
602 // The system cannot find the file specified
603 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
604 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
605 Assert.IsNull (ex.InnerException, "#B4");
606 Assert.IsNotNull (ex.Message, "#B5");
607 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
612 public void Start_UseShellExecuteWithEmptyUserName ()
615 Assert.Ignore ("On Unix and Mac OS X, we try " +
616 "to open any file (using xdg-open, ...)" +
617 " and we do not report an exception " +
620 string exe = @"c:\shouldnoteverexist.exe";
623 Process p = new Process ();
624 p.StartInfo.FileName = exe;
625 p.StartInfo.UseShellExecute = true;
626 p.StartInfo.UserName = "";
629 } catch (InvalidOperationException) {
631 } catch (Win32Exception) {
635 Process p = new Process ();
636 p.StartInfo.FileName = exe;
637 p.StartInfo.UseShellExecute = true;
638 p.StartInfo.UserName = null;
641 } catch (InvalidOperationException) {
643 } catch (Win32Exception) {
647 [Test] // Start (string, string)
648 public void Start4_FileName_Null ()
651 Process.Start ((string) null, string.Empty);
653 } catch (InvalidOperationException ex) {
654 // Cannot start process because a file name has
656 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
657 Assert.IsNull (ex.InnerException, "#3");
658 Assert.IsNotNull (ex.Message, "#4");
663 public void StartInfo ()
665 ProcessStartInfo startInfo = new ProcessStartInfo ();
667 Process p = new Process ();
668 Assert.IsNotNull (p.StartInfo, "#A1");
669 p.StartInfo = startInfo;
670 Assert.AreSame (startInfo, p.StartInfo, "#A2");
674 public void StartInfo_Null ()
676 Process p = new Process ();
680 } catch (ArgumentNullException ex) {
681 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
682 Assert.IsNull (ex.InnerException, "#3");
683 Assert.IsNotNull (ex.Message, "#4");
684 Assert.IsNotNull (ex.ParamName, "#5");
685 Assert.AreEqual ("value", ex.ParamName, "#6");
690 [NUnit.Framework.Category ("NotDotNet")]
691 [NUnit.Framework.Category ("MobileNotWorking")]
692 public void TestRedirectedOutputIsAsync ()
694 // Test requires cygwin, so we just bail out for now.
695 if (Path.DirectorySeparatorChar == '\\')
696 Assert.Ignore ("Test requires cygwin.");
698 Process p = new Process ();
699 p.StartInfo = new ProcessStartInfo ("/bin/sh", "-c \"sleep 2; echo hello\"");
700 p.StartInfo.RedirectStandardOutput = true;
701 p.StartInfo.UseShellExecute = false;
704 Stream stdout = p.StandardOutput.BaseStream;
706 byte [] buffer = new byte [200];
708 // start async Read operation
709 DateTime start = DateTime.Now;
710 IAsyncResult ar = stdout.BeginRead (buffer, 0, buffer.Length,
711 new AsyncCallback (Read), stdout);
713 Assert.IsTrue ((DateTime.Now - start).TotalMilliseconds < 1000, "#01 BeginRead was not async");
715 Assert.AreEqual (0, p.ExitCode, "#02 script failure");
718 ar.AsyncWaitHandle.WaitOne (2000, false);
719 if (bytesRead < "hello".Length)
720 Assert.Fail ("#03 got {0} bytes", bytesRead);
721 Assert.AreEqual ("hello", Encoding.Default.GetString (buffer, 0, 5), "#04");
725 void Read (IAsyncResult ar)
727 Stream stm = (Stream) ar.AsyncState;
728 bytesRead = stm.EndRead (ar);
731 static bool RunningOnUnix {
733 int p = (int)Environment.OSVersion.Platform;
734 return ((p == 128) || (p == 4) || (p == 6));
738 public int bytesRead = -1;
741 [NUnit.Framework.Category ("MobileNotWorking")]
742 // This was for bug #459450
743 public void TestEventRaising ()
745 EventWaitHandle errorClosed = new ManualResetEvent(false);
746 EventWaitHandle outClosed = new ManualResetEvent(false);
747 EventWaitHandle exited = new ManualResetEvent(false);
749 Process p = new Process();
751 p.StartInfo = GetCrossPlatformStartInfo ();
752 p.StartInfo.UseShellExecute = false;
753 p.StartInfo.RedirectStandardOutput = true;
754 p.StartInfo.RedirectStandardError = true;
755 p.StartInfo.RedirectStandardInput = false;
756 p.OutputDataReceived += (object sender, DataReceivedEventArgs e) => {
757 if (e.Data == null) {
762 p.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => {
763 if (e.Data == null) {
768 p.Exited += (object sender, EventArgs e) => {
772 p.EnableRaisingEvents = true;
776 p.BeginErrorReadLine();
777 p.BeginOutputReadLine();
779 Console.WriteLine("started, waiting for handles");
780 bool r = WaitHandle.WaitAll(new WaitHandle[] { errorClosed, outClosed, exited }, 10000, false);
782 Assert.AreEqual (true, r, "Null Argument Events Raised");
786 [NUnit.Framework.Category ("MobileNotWorking")]
787 public void TestEnableEventsAfterExitedEvent ()
789 Process p = new Process ();
791 p.StartInfo = GetCrossPlatformStartInfo ();
792 p.StartInfo.UseShellExecute = false;
793 p.StartInfo.RedirectStandardOutput = true;
794 p.StartInfo.RedirectStandardError = true;
796 var exitedCalledCounter = 0;
797 var exited = new ManualResetEventSlim ();
798 p.Exited += (object sender, EventArgs e) => {
799 exitedCalledCounter++;
800 Assert.IsTrue (p.HasExited);
804 p.EnableRaisingEvents = true;
807 p.BeginErrorReadLine ();
808 p.BeginOutputReadLine ();
812 Assert.AreEqual (1, exitedCalledCounter);
814 Assert.AreEqual (1, exitedCalledCounter);
818 [NUnit.Framework.Category ("MobileNotWorking")]
819 public void TestEnableEventsBeforeExitedEvent ()
821 Process p = new Process ();
823 p.StartInfo = GetCrossPlatformStartInfo ();
824 p.StartInfo.UseShellExecute = false;
825 p.StartInfo.RedirectStandardOutput = true;
826 p.StartInfo.RedirectStandardError = true;
828 p.EnableRaisingEvents = true;
830 var exitedCalledCounter = 0;
831 var exited = new ManualResetEventSlim ();
832 p.Exited += (object sender, EventArgs e) => {
833 exitedCalledCounter++;
834 Assert.IsTrue (p.HasExited);
839 p.BeginErrorReadLine ();
840 p.BeginOutputReadLine ();
843 Assert.IsTrue (exited.Wait (10000));
844 Assert.AreEqual (1, exitedCalledCounter);
846 Assert.AreEqual (1, exitedCalledCounter);
850 [NUnit.Framework.Category ("MobileNotWorking")]
851 public void TestDisableEventsBeforeExitedEvent ()
853 Process p = new Process ();
855 p.StartInfo = GetCrossPlatformStartInfo ();
856 p.StartInfo.UseShellExecute = false;
857 p.StartInfo.RedirectStandardOutput = true;
858 p.StartInfo.RedirectStandardError = true;
860 p.EnableRaisingEvents = false;
862 ManualResetEvent mre = new ManualResetEvent (false);
863 p.Exited += (object sender, EventArgs e) => {
868 p.BeginErrorReadLine ();
869 p.BeginOutputReadLine ();
872 Assert.IsFalse (mre.WaitOne (1000));
875 ProcessStartInfo GetCrossPlatformStartInfo ()
880 path = "/system/bin/ls";
884 return new ProcessStartInfo (path, "/");
886 return new ProcessStartInfo ("help", "");
889 ProcessStartInfo GetEchoCrossPlatformStartInfo ()
894 path = "/system/bin/cat";
898 return new ProcessStartInfo (path);
900 var psi = new ProcessStartInfo ("findstr");
901 psi.Arguments = "\"^\"";
905 #endif // MONO_FEATURE_PROCESS_START
908 public void ProcessName_NotStarted ()
910 Process p = new Process ();
913 String.IsNullOrEmpty (p.ProcessName);
914 } catch (Exception ex) {
918 Assert.IsNotNull (e, "ProcessName should raise if process was not started");
920 //msg should be "No process is associated with this object"
921 Assert.AreEqual (e.GetType (), typeof (InvalidOperationException),
922 "exception should be IOE, I got: " + e.GetType ().Name);
924 Assert.IsNull (e.InnerException, "IOE inner exception should be null");
927 #if MONO_FEATURE_PROCESS_START
929 [NUnit.Framework.Category ("MobileNotWorking")]
930 public void ProcessName_AfterExit ()
932 Process p = new Process ();
933 p.StartInfo = GetCrossPlatformStartInfo ();
934 p.StartInfo.UseShellExecute = false;
935 p.StartInfo.RedirectStandardOutput = true;
936 p.StartInfo.RedirectStandardError = true;
938 p.BeginErrorReadLine();
939 p.BeginOutputReadLine();
941 String.IsNullOrEmpty (p.ExitCode + "");
945 String.IsNullOrEmpty (p.ProcessName);
946 } catch (Exception ex) {
950 Assert.IsNotNull (e, "ProcessName should raise if process was finished");
952 //msg should be "Process has exited, so the requested information is not available"
953 Assert.AreEqual (e.GetType (), typeof (InvalidOperationException),
954 "exception should be IOE, I got: " + e.GetType ().Name);
956 Assert.IsNull (e.InnerException, "IOE inner exception should be null");
958 #endif // MONO_FEATURE_PROCESS_START
961 public void Handle_ThrowsOnNotStarted ()
963 Process p = new Process ();
966 Assert.Fail ("Handle should throw for unstated procs, but returned " + x);
967 } catch (InvalidOperationException) {
972 public void HasExitedCurrent () {
973 Assert.IsFalse (Process.GetCurrentProcess ().HasExited);
976 #if MONO_FEATURE_PROCESS_START
978 [NUnit.Framework.Category ("MobileNotWorking")]
979 public void DisposeWithDisposedStreams ()
981 var psi = GetCrossPlatformStartInfo ();
982 psi.RedirectStandardInput = true;
983 psi.RedirectStandardOutput = true;
984 psi.UseShellExecute = false;
986 var p = Process.Start (psi);
987 p.StandardInput.BaseStream.Dispose ();
988 p.StandardOutput.BaseStream.Dispose ();
993 [NUnit.Framework.Category ("MobileNotWorking")]
994 public void StandardInputWrite ()
996 var psi = GetEchoCrossPlatformStartInfo ();
997 psi.RedirectStandardInput = true;
998 psi.RedirectStandardOutput = true;
999 psi.UseShellExecute = false;
1001 using (var p = Process.Start (psi)) {
1003 p.OutputDataReceived += (s, e) => {};
1004 p.BeginOutputReadLine ();
1006 for (int i = 0; i < 1024 * 9; ++i) {
1007 p.StandardInput.Write ('x');
1008 if (i > 0 && i % 128 == 0)
1009 p.StandardInput.WriteLine ();
1012 p.StandardInput.Close ();
1017 #endif // MONO_FEATURE_PROCESS_START
1020 public void Modules () {
1021 var modules = Process.GetCurrentProcess ().Modules;
1022 foreach (var a in AppDomain.CurrentDomain.GetAssemblies ()) {
1024 var name = a.GetName ();
1026 StringBuilder sb = new StringBuilder ();
1027 sb.AppendFormat ("Could not found: {0} {1}\n", name.Name, name.Version);
1028 sb.AppendLine ("Looked in modules:");
1030 foreach (var o in modules) {
1031 var m = (ProcessModule) o;
1033 sb.AppendFormat (" {0} {1}.{2}.{3}\n", m.FileName.ToString (),
1034 m.FileVersionInfo.FileMajorPart,
1035 m.FileVersionInfo.FileMinorPart,
1036 m.FileVersionInfo.FileBuildPart);
1038 if (!m.FileName.StartsWith ("[In Memory] " + name.Name))
1041 var fv = m.FileVersionInfo;
1042 if (fv.FileBuildPart != name.Version.Build ||
1043 fv.FileMinorPart != name.Version.Minor ||
1044 fv.FileMajorPart != name.Version.Major)
1050 Assert.IsTrue (found, sb.ToString ());
1054 #if MONO_FEATURE_PROCESS_START
1056 [NUnit.Framework.Category ("MobileNotWorking")]
1057 [ExpectedException (typeof (InvalidOperationException))]
1058 public void TestDoubleBeginOutputReadLine ()
1060 using (Process p = new Process ()) {
1061 p.StartInfo = GetCrossPlatformStartInfo ();
1062 p.StartInfo.UseShellExecute = false;
1063 p.StartInfo.RedirectStandardOutput = true;
1064 p.StartInfo.RedirectStandardError = true;
1068 p.BeginOutputReadLine ();
1069 p.BeginOutputReadLine ();
1076 [NUnit.Framework.Category ("MobileNotWorking")]
1077 [ExpectedException (typeof (InvalidOperationException))]
1078 public void TestDoubleBeginErrorReadLine ()
1080 using (Process p = new Process ()) {
1081 p.StartInfo = GetCrossPlatformStartInfo ();
1082 p.StartInfo.UseShellExecute = false;
1083 p.StartInfo.RedirectStandardOutput = true;
1084 p.StartInfo.RedirectStandardError = true;
1088 p.BeginErrorReadLine ();
1089 p.BeginErrorReadLine ();
1096 [NUnit.Framework.Category ("MobileNotWorking")]
1097 public void TestExitedRaisedTooSoon ()
1100 Assert.Ignore ("using sleep command, only available on unix");
1104 using (Process p = Process.Start("sleep", sleeptime.ToString ())) {
1105 ManualResetEvent mre = new ManualResetEvent (false);
1107 p.EnableRaisingEvents = true;
1108 p.Exited += (sender, e) => {
1112 Assert.IsFalse (mre.WaitOne ((sleeptime - 2) * 1000), "Exited triggered before the process returned");
1115 #endif // MONO_FEATURE_PROCESS_START
1118 public void GetProcessesByName()
1120 // This should return Process[0] or a Process[] with all the "foo" programs running
1121 Process.GetProcessesByName ("foo");
1125 [ExpectedException(typeof(InvalidOperationException))]
1126 public void HigherPrivilegeProcessName ()
1129 Assert.Ignore ("accessing pid 1, only available on unix");
1131 string v = Process.GetProcessById (1).ProcessName;