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 public void GetProcesses_StartTime ()
58 foreach (var p in Process.GetProcesses ()) {
59 if (!p.HasExited && p.StartTime.Year < 1800)
60 Assert.Fail ("Process should not be started since the 18th century.");
65 public void PriorityClass_NotStarted ()
67 Process process = new Process ();
69 process.PriorityClass = ProcessPriorityClass.Normal;
71 } catch (InvalidOperationException ex) {
72 // No process is associated with this object
73 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
74 Assert.IsNull (ex.InnerException, "#A3");
75 Assert.IsNotNull (ex.Message, "#A4");
79 Assert.Fail ("#B1:" + process.PriorityClass);
80 } catch (InvalidOperationException ex) {
81 // No process is associated with this object
82 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
83 Assert.IsNull (ex.InnerException, "#B3");
84 Assert.IsNotNull (ex.Message, "#B4");
89 public void PriorityClass_Invalid ()
91 Process process = new Process ();
93 process.PriorityClass = (ProcessPriorityClass) 666;
95 } catch (InvalidEnumArgumentException ex) {
96 Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
97 Assert.IsNull (ex.InnerException, "#3");
98 Assert.IsNotNull (ex.Message, "#4");
99 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#5");
100 Assert.IsTrue (ex.Message.IndexOf (typeof (ProcessPriorityClass).Name) != -1, "#6");
101 Assert.IsNotNull (ex.ParamName, "#7");
102 Assert.AreEqual ("value", ex.ParamName, "#8");
107 public void Start1_FileName_Empty ()
109 Process process = new Process ();
110 process.StartInfo = new ProcessStartInfo (string.Empty);
113 process.StartInfo.UseShellExecute = false;
117 } catch (InvalidOperationException ex) {
118 // Cannot start process because a file name has
120 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
121 Assert.IsNull (ex.InnerException, "#A3");
122 Assert.IsNotNull (ex.Message, "#A4");
126 process.StartInfo.UseShellExecute = true;
130 } catch (InvalidOperationException ex) {
131 // Cannot start process because a file name has
133 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
134 Assert.IsNull (ex.InnerException, "#B3");
135 Assert.IsNotNull (ex.Message, "#B4");
140 public void Start1_FileName_InvalidPathCharacters ()
143 // on unix, all characters are allowed
144 Assert.Ignore ("Running on Unix.");
146 string systemDir = Environment.GetFolderPath (Environment.SpecialFolder.System);
147 string exe = "\"" + Path.Combine (systemDir, "calc.exe") + "\"";
149 Process process = new Process ();
150 process.StartInfo = new ProcessStartInfo (exe);
153 process.StartInfo.UseShellExecute = false;
154 Assert.IsTrue (process.Start ());
158 process.StartInfo.UseShellExecute = true;
159 Assert.IsTrue (process.Start ());
164 public void Start1_FileName_NotFound ()
166 Process process = new Process ();
167 string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
168 : @"c:\shouldnoteverexist.exe";
170 // absolute path, no shell
171 process.StartInfo = new ProcessStartInfo (exe);
172 process.StartInfo.UseShellExecute = false;
176 } catch (Win32Exception ex) {
177 // The system cannot find the file specified
178 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
179 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
180 Assert.IsNull (ex.InnerException, "#A4");
181 Assert.IsNotNull (ex.Message, "#A5");
182 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
185 // relative path, no shell
186 process.StartInfo.FileName = "shouldnoteverexist.exe";
187 process.StartInfo.UseShellExecute = false;
191 } catch (Win32Exception ex) {
192 // The system cannot find the file specified
193 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
194 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
195 Assert.IsNull (ex.InnerException, "#B4");
196 Assert.IsNotNull (ex.Message, "#B5");
197 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
201 Assert.Ignore ("On Unix and Mac OS X, we try " +
202 "to open any file (using xdg-open, ...)" +
203 " and we do not report an exception " +
206 // absolute path, shell
207 process.StartInfo.FileName = exe;
208 process.StartInfo.UseShellExecute = true;
212 } catch (Win32Exception ex) {
213 // The system cannot find the file specified
214 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
215 Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
216 Assert.IsNull (ex.InnerException, "#C4");
217 Assert.IsNotNull (ex.Message, "#C5");
218 Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
221 // relative path, shell
222 process.StartInfo.FileName = "shouldnoteverexist.exe";
223 process.StartInfo.UseShellExecute = true;
227 } catch (Win32Exception ex) {
228 // The system cannot find the file specified
229 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
230 Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
231 Assert.IsNull (ex.InnerException, "#D4");
232 Assert.IsNotNull (ex.Message, "#D5");
233 Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
238 public void Start1_FileName_Null ()
240 Process process = new Process ();
241 process.StartInfo = new ProcessStartInfo ((string) null);
244 process.StartInfo.UseShellExecute = false;
248 } catch (InvalidOperationException ex) {
249 // Cannot start process because a file name has
251 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
252 Assert.IsNull (ex.InnerException, "#A3");
253 Assert.IsNotNull (ex.Message, "#A4");
257 process.StartInfo.UseShellExecute = true;
261 } catch (InvalidOperationException ex) {
262 // Cannot start process because a file name has
264 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
265 Assert.IsNull (ex.InnerException, "#B3");
266 Assert.IsNotNull (ex.Message, "#B4");
271 public void Start1_FileName_Whitespace ()
273 Process process = new Process ();
274 process.StartInfo = new ProcessStartInfo (" ");
275 process.StartInfo.UseShellExecute = false;
279 } catch (Win32Exception ex) {
280 // The system cannot find the file specified
281 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
282 Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
283 Assert.IsNull (ex.InnerException, "#4");
284 Assert.IsNotNull (ex.Message, "#5");
285 Assert.AreEqual (2, ex.NativeErrorCode, "#6");
289 [Test] // Start (ProcessStartInfo)
290 public void Start2_FileName_Empty ()
292 ProcessStartInfo startInfo = new ProcessStartInfo (string.Empty);
295 startInfo.UseShellExecute = false;
297 Process.Start (startInfo);
299 } catch (InvalidOperationException ex) {
300 // Cannot start process because a file name has
302 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
303 Assert.IsNull (ex.InnerException, "#A3");
304 Assert.IsNotNull (ex.Message, "#A4");
308 startInfo.UseShellExecute = true;
310 Process.Start (startInfo);
312 } catch (InvalidOperationException ex) {
313 // Cannot start process because a file name has
315 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
316 Assert.IsNull (ex.InnerException, "#B3");
317 Assert.IsNotNull (ex.Message, "#B4");
321 [Test] // Start (ProcessStartInfo)
322 public void Start2_FileName_NotFound ()
324 ProcessStartInfo startInfo = new ProcessStartInfo ();
325 string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
326 : @"c:\shouldnoteverexist.exe";
328 // absolute path, no shell
329 startInfo.FileName = exe;
330 startInfo.UseShellExecute = false;
332 Process.Start (startInfo);
334 } catch (Win32Exception ex) {
335 // The system cannot find the file specified
336 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
337 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
338 Assert.IsNull (ex.InnerException, "#A4");
339 Assert.IsNotNull (ex.Message, "#A5");
340 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
343 // relative path, no shell
344 startInfo.FileName = "shouldnoteverexist.exe";
345 startInfo.UseShellExecute = false;
347 Process.Start (startInfo);
349 } catch (Win32Exception ex) {
350 // The system cannot find the file specified
351 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
352 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
353 Assert.IsNull (ex.InnerException, "#B4");
354 Assert.IsNotNull (ex.Message, "#B5");
355 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
359 Assert.Ignore ("On Unix and Mac OS X, we try " +
360 "to open any file (using xdg-open, ...)" +
361 " and we do not report an exception " +
364 // absolute path, shell
365 startInfo.FileName = exe;
366 startInfo.UseShellExecute = true;
368 Process.Start (startInfo);
370 } catch (Win32Exception ex) {
371 // The system cannot find the file specified
372 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
373 Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
374 Assert.IsNull (ex.InnerException, "#C4");
375 Assert.IsNotNull (ex.Message, "#C5");
376 Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
379 // relative path, shell
380 startInfo.FileName = "shouldnoteverexist.exe";
381 startInfo.UseShellExecute = true;
383 Process.Start (startInfo);
385 } catch (Win32Exception ex) {
386 // The system cannot find the file specified
387 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
388 Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
389 Assert.IsNull (ex.InnerException, "#D4");
390 Assert.IsNotNull (ex.Message, "#D5");
391 Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
395 [Test] // Start (ProcessStartInfo)
396 public void Start2_FileName_Null ()
398 ProcessStartInfo startInfo = new ProcessStartInfo ((string) null);
401 startInfo.UseShellExecute = false;
403 Process.Start (startInfo);
405 } catch (InvalidOperationException ex) {
406 // Cannot start process because a file name has
408 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
409 Assert.IsNull (ex.InnerException, "#A3");
410 Assert.IsNotNull (ex.Message, "#A4");
414 startInfo.UseShellExecute = true;
416 Process.Start (startInfo);
418 } catch (InvalidOperationException ex) {
419 // Cannot start process because a file name has
421 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
422 Assert.IsNull (ex.InnerException, "#B3");
423 Assert.IsNotNull (ex.Message, "#B4");
427 [Test] // Start (ProcessStartInfo)
428 public void Start2_FileName_Whitespace ()
430 ProcessStartInfo startInfo = new ProcessStartInfo (" ");
431 startInfo.UseShellExecute = false;
433 Process.Start (startInfo);
435 } catch (Win32Exception ex) {
436 // The system cannot find the file specified
437 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
438 Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
439 Assert.IsNull (ex.InnerException, "#4");
440 Assert.IsNotNull (ex.Message, "#5");
441 Assert.AreEqual (2, ex.NativeErrorCode, "#6");
445 [Test] // Start (ProcessStartInfo)
446 public void Start2_StartInfo_Null ()
449 Process.Start ((ProcessStartInfo) null);
451 } catch (ArgumentNullException ex) {
452 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
453 Assert.IsNull (ex.InnerException, "#3");
454 Assert.IsNotNull (ex.Message, "#4");
455 Assert.IsNotNull (ex.ParamName, "#5");
456 Assert.AreEqual ("startInfo", ex.ParamName, "#6");
460 [Test] // Start (string)
461 public void Start3_FileName_Empty ()
464 Process.Start (string.Empty);
466 } catch (InvalidOperationException ex) {
467 // Cannot start process because a file name has
469 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
470 Assert.IsNull (ex.InnerException, "#3");
471 Assert.IsNotNull (ex.Message, "#4");
475 [Test] // Start (string)
476 public void Start3_FileName_NotFound ()
479 Assert.Ignore ("On Unix and Mac OS X, we try " +
480 "to open any file (using xdg-open, ...)" +
481 " and we do not report an exception " +
484 string exe = @"c:\shouldnoteverexist.exe";
486 // absolute path, no shell
490 } catch (Win32Exception ex) {
491 // The system cannot find the file specified
492 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
493 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
494 Assert.IsNull (ex.InnerException, "#A4");
495 Assert.IsNotNull (ex.Message, "#A5");
496 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
499 // relative path, no shell
501 Process.Start ("shouldnoteverexist.exe");
503 } catch (Win32Exception ex) {
504 // The system cannot find the file specified
505 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
506 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
507 Assert.IsNull (ex.InnerException, "#B4");
508 Assert.IsNotNull (ex.Message, "#B5");
509 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
513 [Test] // Start (string)
514 public void Start3_FileName_Null ()
517 Process.Start ((string) null);
519 } catch (InvalidOperationException ex) {
520 // Cannot start process because a file name has
522 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
523 Assert.IsNull (ex.InnerException, "#3");
524 Assert.IsNotNull (ex.Message, "#4");
528 [Test] // Start (string, string)
529 public void Start4_Arguments_Null ()
532 Assert.Ignore ("On Unix and Mac OS X, we try " +
533 "to open any file (using xdg-open, ...)" +
534 " and we do not report an exception " +
537 string exe = @"c:\shouldnoteverexist.exe";
540 Process.Start ("whatever.exe", (string) null);
542 } catch (Win32Exception ex) {
543 // The system cannot find the file specified
544 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
545 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
546 Assert.IsNull (ex.InnerException, "#B4");
547 Assert.IsNotNull (ex.Message, "#B5");
548 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
552 [Test] // Start (string, string)
553 public void Start4_FileName_Empty ()
556 Process.Start (string.Empty, string.Empty);
558 } catch (InvalidOperationException ex) {
559 // Cannot start process because a file name has
561 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
562 Assert.IsNull (ex.InnerException, "#3");
563 Assert.IsNotNull (ex.Message, "#4");
567 [Test] // Start (string, string)
568 public void Start4_FileName_NotFound ()
571 Assert.Ignore ("On Unix and Mac OS X, we try " +
572 "to open any file (using xdg-open, ...)" +
573 " and we do not report an exception " +
576 string exe = @"c:\shouldnoteverexist.exe";
578 // absolute path, no shell
580 Process.Start (exe, string.Empty);
582 } catch (Win32Exception ex) {
583 // The system cannot find the file specified
584 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
585 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
586 Assert.IsNull (ex.InnerException, "#A4");
587 Assert.IsNotNull (ex.Message, "#A5");
588 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
591 // relative path, no shell
593 Process.Start ("shouldnoteverexist.exe", string.Empty);
595 } catch (Win32Exception ex) {
596 // The system cannot find the file specified
597 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
598 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
599 Assert.IsNull (ex.InnerException, "#B4");
600 Assert.IsNotNull (ex.Message, "#B5");
601 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
606 public void Start_UseShellExecuteWithEmptyUserName ()
609 Assert.Ignore ("On Unix and Mac OS X, we try " +
610 "to open any file (using xdg-open, ...)" +
611 " and we do not report an exception " +
614 string exe = @"c:\shouldnoteverexist.exe";
617 Process p = new Process ();
618 p.StartInfo.FileName = exe;
619 p.StartInfo.UseShellExecute = true;
620 p.StartInfo.UserName = "";
623 } catch (InvalidOperationException) {
625 } catch (Win32Exception) {
629 Process p = new Process ();
630 p.StartInfo.FileName = exe;
631 p.StartInfo.UseShellExecute = true;
632 p.StartInfo.UserName = null;
635 } catch (InvalidOperationException) {
637 } catch (Win32Exception) {
641 [Test] // Start (string, string)
642 public void Start4_FileName_Null ()
645 Process.Start ((string) null, string.Empty);
647 } catch (InvalidOperationException ex) {
648 // Cannot start process because a file name has
650 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
651 Assert.IsNull (ex.InnerException, "#3");
652 Assert.IsNotNull (ex.Message, "#4");
657 public void StartInfo ()
659 ProcessStartInfo startInfo = new ProcessStartInfo ();
661 Process p = new Process ();
662 Assert.IsNotNull (p.StartInfo, "#A1");
663 p.StartInfo = startInfo;
664 Assert.AreSame (startInfo, p.StartInfo, "#A2");
668 public void StartInfo_Null ()
670 Process p = new Process ();
674 } catch (ArgumentNullException ex) {
675 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
676 Assert.IsNull (ex.InnerException, "#3");
677 Assert.IsNotNull (ex.Message, "#4");
678 Assert.IsNotNull (ex.ParamName, "#5");
679 Assert.AreEqual ("value", ex.ParamName, "#6");
684 [NUnit.Framework.Category ("NotDotNet")]
685 [NUnit.Framework.Category ("MobileNotWorking")]
686 public void TestRedirectedOutputIsAsync ()
688 // Test requires cygwin, so we just bail out for now.
689 if (Path.DirectorySeparatorChar == '\\')
690 Assert.Ignore ("Test requires cygwin.");
692 Process p = new Process ();
693 p.StartInfo = new ProcessStartInfo ("/bin/sh", "-c \"sleep 2; echo hello\"");
694 p.StartInfo.RedirectStandardOutput = true;
695 p.StartInfo.UseShellExecute = false;
698 Stream stdout = p.StandardOutput.BaseStream;
700 byte [] buffer = new byte [200];
702 // start async Read operation
703 DateTime start = DateTime.Now;
704 IAsyncResult ar = stdout.BeginRead (buffer, 0, buffer.Length,
705 new AsyncCallback (Read), stdout);
707 Assert.IsTrue ((DateTime.Now - start).TotalMilliseconds < 1000, "#01 BeginRead was not async");
709 Assert.AreEqual (0, p.ExitCode, "#02 script failure");
712 ar.AsyncWaitHandle.WaitOne (2000, false);
713 if (bytesRead < "hello".Length)
714 Assert.Fail ("#03 got {0} bytes", bytesRead);
715 Assert.AreEqual ("hello", Encoding.Default.GetString (buffer, 0, 5), "#04");
719 void Read (IAsyncResult ar)
721 Stream stm = (Stream) ar.AsyncState;
722 bytesRead = stm.EndRead (ar);
725 static bool RunningOnUnix {
727 int p = (int)Environment.OSVersion.Platform;
728 return ((p == 128) || (p == 4) || (p == 6));
732 public int bytesRead = -1;
734 // Not technically a 2.0 only test, but I use lambdas, so I need gmcs
737 [NUnit.Framework.Category ("MobileNotWorking")]
738 // This was for bug #459450
739 public void TestEventRaising ()
741 EventWaitHandle errorClosed = new ManualResetEvent(false);
742 EventWaitHandle outClosed = new ManualResetEvent(false);
743 EventWaitHandle exited = new ManualResetEvent(false);
745 Process p = new Process();
747 p.StartInfo = GetCrossPlatformStartInfo ();
748 p.StartInfo.UseShellExecute = false;
749 p.StartInfo.RedirectStandardOutput = true;
750 p.StartInfo.RedirectStandardError = true;
751 p.StartInfo.RedirectStandardInput = false;
752 p.OutputDataReceived += (object sender, DataReceivedEventArgs e) => {
753 if (e.Data == null) {
758 p.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => {
759 if (e.Data == null) {
764 p.Exited += (object sender, EventArgs e) => {
768 p.EnableRaisingEvents = true;
772 p.BeginErrorReadLine();
773 p.BeginOutputReadLine();
775 Console.WriteLine("started, waiting for handles");
776 bool r = WaitHandle.WaitAll(new WaitHandle[] { errorClosed, outClosed, exited }, 10000, false);
778 Assert.AreEqual (true, r, "Null Argument Events Raised");
782 [NUnit.Framework.Category ("MobileNotWorking")]
783 public void TestEnableEventsAfterExitedEvent ()
785 Process p = new Process ();
787 p.StartInfo = GetCrossPlatformStartInfo ();
788 p.StartInfo.UseShellExecute = false;
789 p.StartInfo.RedirectStandardOutput = true;
790 p.StartInfo.RedirectStandardError = true;
792 var exitedCalledCounter = 0;
793 p.Exited += (object sender, EventArgs e) => {
794 exitedCalledCounter++;
795 Assert.IsTrue (p.HasExited);
798 p.EnableRaisingEvents = true;
801 p.BeginErrorReadLine ();
802 p.BeginOutputReadLine ();
805 Assert.AreEqual (1, exitedCalledCounter);
807 Assert.AreEqual (1, exitedCalledCounter);
811 [NUnit.Framework.Category ("MobileNotWorking")]
812 public void TestEnableEventsBeforeExitedEvent ()
814 Process p = new Process ();
816 p.StartInfo = GetCrossPlatformStartInfo ();
817 p.StartInfo.UseShellExecute = false;
818 p.StartInfo.RedirectStandardOutput = true;
819 p.StartInfo.RedirectStandardError = true;
821 p.EnableRaisingEvents = true;
823 var exitedCalledCounter = 0;
824 p.Exited += (object sender, EventArgs e) => {
825 exitedCalledCounter++;
826 Assert.IsTrue (p.HasExited);
830 p.BeginErrorReadLine ();
831 p.BeginOutputReadLine ();
834 Assert.AreEqual (1, exitedCalledCounter);
836 Assert.AreEqual (1, exitedCalledCounter);
840 private ProcessStartInfo GetCrossPlatformStartInfo ()
842 return RunningOnUnix ? new ProcessStartInfo ("/bin/ls", "/") : new ProcessStartInfo ("help", "");
845 [Test] // Covers #26362
846 public void TestExitedEvent ()
848 var falseExitedEvents = 0;
849 var cp = Process.GetCurrentProcess ();
850 foreach (var p in Process.GetProcesses ()) {
851 if (p.Id != cp.Id && !p.HasExited) {
852 p.EnableRaisingEvents = true;
853 p.Exited += (s, e) => {
859 Assert.AreEqual (0, falseExitedEvents);
863 public void ProcessName_NotStarted ()
865 Process p = new Process ();
868 String.IsNullOrEmpty (p.ProcessName);
869 } catch (Exception ex) {
873 Assert.IsNotNull (e, "ProcessName should raise if process was not started");
875 //msg should be "No process is associated with this object"
876 Assert.AreEqual (e.GetType (), typeof (InvalidOperationException),
877 "exception should be IOE, I got: " + e.GetType ().Name);
879 Assert.IsNull (e.InnerException, "IOE inner exception should be null");
883 [NUnit.Framework.Category ("MobileNotWorking")]
884 public void ProcessName_AfterExit ()
886 Process p = new Process ();
887 p.StartInfo = GetCrossPlatformStartInfo ();
888 p.StartInfo.UseShellExecute = false;
889 p.StartInfo.RedirectStandardOutput = true;
890 p.StartInfo.RedirectStandardError = true;
892 p.BeginErrorReadLine();
893 p.BeginOutputReadLine();
895 String.IsNullOrEmpty (p.ExitCode + "");
899 String.IsNullOrEmpty (p.ProcessName);
900 } catch (Exception ex) {
904 Assert.IsNotNull (e, "ProcessName should raise if process was finished");
906 //msg should be "Process has exited, so the requested information is not available"
907 Assert.AreEqual (e.GetType (), typeof (InvalidOperationException),
908 "exception should be IOE, I got: " + e.GetType ().Name);
910 Assert.IsNull (e.InnerException, "IOE inner exception should be null");
914 public void Handle_ThrowsOnNotStarted ()
916 Process p = new Process ();
919 Assert.Fail ("Handle should throw for unstated procs, but returned " + x);
920 } catch (InvalidOperationException) {
925 public void HasExitedCurrent () {
926 Assert.IsFalse (Process.GetCurrentProcess ().HasExited);