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");
108 public void Start1_FileName_Empty ()
110 Process process = new Process ();
111 process.StartInfo = new ProcessStartInfo (string.Empty);
114 process.StartInfo.UseShellExecute = false;
118 } catch (InvalidOperationException ex) {
119 // Cannot start process because a file name has
121 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
122 Assert.IsNull (ex.InnerException, "#A3");
123 Assert.IsNotNull (ex.Message, "#A4");
127 process.StartInfo.UseShellExecute = true;
131 } catch (InvalidOperationException ex) {
132 // Cannot start process because a file name has
134 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
135 Assert.IsNull (ex.InnerException, "#B3");
136 Assert.IsNotNull (ex.Message, "#B4");
141 public void Start1_FileName_InvalidPathCharacters ()
144 // on unix, all characters are allowed
145 Assert.Ignore ("Running on Unix.");
147 string systemDir = Environment.GetFolderPath (Environment.SpecialFolder.System);
148 string exe = "\"" + Path.Combine (systemDir, "calc.exe") + "\"";
150 Process process = new Process ();
151 process.StartInfo = new ProcessStartInfo (exe);
154 process.StartInfo.UseShellExecute = false;
155 Assert.IsTrue (process.Start ());
159 process.StartInfo.UseShellExecute = true;
160 Assert.IsTrue (process.Start ());
165 public void Start1_FileName_NotFound ()
167 Process process = new Process ();
168 string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
169 : @"c:\shouldnoteverexist.exe";
171 // absolute path, no shell
172 process.StartInfo = new ProcessStartInfo (exe);
173 process.StartInfo.UseShellExecute = false;
177 } catch (Win32Exception ex) {
178 // The system cannot find the file specified
179 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
180 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
181 Assert.IsNull (ex.InnerException, "#A4");
182 Assert.IsNotNull (ex.Message, "#A5");
183 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
186 // relative path, no shell
187 process.StartInfo.FileName = "shouldnoteverexist.exe";
188 process.StartInfo.UseShellExecute = false;
192 } catch (Win32Exception ex) {
193 // The system cannot find the file specified
194 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
195 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
196 Assert.IsNull (ex.InnerException, "#B4");
197 Assert.IsNotNull (ex.Message, "#B5");
198 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
202 Assert.Ignore ("On Unix and Mac OS X, we try " +
203 "to open any file (using xdg-open, ...)" +
204 " and we do not report an exception " +
207 // absolute path, shell
208 process.StartInfo.FileName = exe;
209 process.StartInfo.UseShellExecute = true;
213 } catch (Win32Exception ex) {
214 // The system cannot find the file specified
215 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
216 Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
217 Assert.IsNull (ex.InnerException, "#C4");
218 Assert.IsNotNull (ex.Message, "#C5");
219 Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
222 // relative path, shell
223 process.StartInfo.FileName = "shouldnoteverexist.exe";
224 process.StartInfo.UseShellExecute = true;
228 } catch (Win32Exception ex) {
229 // The system cannot find the file specified
230 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
231 Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
232 Assert.IsNull (ex.InnerException, "#D4");
233 Assert.IsNotNull (ex.Message, "#D5");
234 Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
239 public void Start1_FileName_Null ()
241 Process process = new Process ();
242 process.StartInfo = new ProcessStartInfo ((string) null);
245 process.StartInfo.UseShellExecute = false;
249 } catch (InvalidOperationException ex) {
250 // Cannot start process because a file name has
252 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
253 Assert.IsNull (ex.InnerException, "#A3");
254 Assert.IsNotNull (ex.Message, "#A4");
258 process.StartInfo.UseShellExecute = true;
262 } catch (InvalidOperationException ex) {
263 // Cannot start process because a file name has
265 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
266 Assert.IsNull (ex.InnerException, "#B3");
267 Assert.IsNotNull (ex.Message, "#B4");
272 public void Start1_FileName_Whitespace ()
274 Process process = new Process ();
275 process.StartInfo = new ProcessStartInfo (" ");
276 process.StartInfo.UseShellExecute = false;
280 } catch (Win32Exception ex) {
281 // The system cannot find the file specified
282 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
283 Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
284 Assert.IsNull (ex.InnerException, "#4");
285 Assert.IsNotNull (ex.Message, "#5");
286 Assert.AreEqual (2, ex.NativeErrorCode, "#6");
290 [Test] // Start (ProcessStartInfo)
291 public void Start2_FileName_Empty ()
293 ProcessStartInfo startInfo = new ProcessStartInfo (string.Empty);
296 startInfo.UseShellExecute = false;
298 Process.Start (startInfo);
300 } catch (InvalidOperationException ex) {
301 // Cannot start process because a file name has
303 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
304 Assert.IsNull (ex.InnerException, "#A3");
305 Assert.IsNotNull (ex.Message, "#A4");
309 startInfo.UseShellExecute = true;
311 Process.Start (startInfo);
313 } catch (InvalidOperationException ex) {
314 // Cannot start process because a file name has
316 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
317 Assert.IsNull (ex.InnerException, "#B3");
318 Assert.IsNotNull (ex.Message, "#B4");
322 [Test] // Start (ProcessStartInfo)
323 public void Start2_FileName_NotFound ()
325 ProcessStartInfo startInfo = new ProcessStartInfo ();
326 string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
327 : @"c:\shouldnoteverexist.exe";
329 // absolute path, no shell
330 startInfo.FileName = exe;
331 startInfo.UseShellExecute = false;
333 Process.Start (startInfo);
335 } catch (Win32Exception ex) {
336 // The system cannot find the file specified
337 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
338 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
339 Assert.IsNull (ex.InnerException, "#A4");
340 Assert.IsNotNull (ex.Message, "#A5");
341 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
344 // relative path, no shell
345 startInfo.FileName = "shouldnoteverexist.exe";
346 startInfo.UseShellExecute = false;
348 Process.Start (startInfo);
350 } catch (Win32Exception ex) {
351 // The system cannot find the file specified
352 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
353 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
354 Assert.IsNull (ex.InnerException, "#B4");
355 Assert.IsNotNull (ex.Message, "#B5");
356 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
360 Assert.Ignore ("On Unix and Mac OS X, we try " +
361 "to open any file (using xdg-open, ...)" +
362 " and we do not report an exception " +
365 // absolute path, shell
366 startInfo.FileName = exe;
367 startInfo.UseShellExecute = true;
369 Process.Start (startInfo);
371 } catch (Win32Exception ex) {
372 // The system cannot find the file specified
373 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
374 Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
375 Assert.IsNull (ex.InnerException, "#C4");
376 Assert.IsNotNull (ex.Message, "#C5");
377 Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
380 // relative path, shell
381 startInfo.FileName = "shouldnoteverexist.exe";
382 startInfo.UseShellExecute = true;
384 Process.Start (startInfo);
386 } catch (Win32Exception ex) {
387 // The system cannot find the file specified
388 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
389 Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
390 Assert.IsNull (ex.InnerException, "#D4");
391 Assert.IsNotNull (ex.Message, "#D5");
392 Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
396 [Test] // Start (ProcessStartInfo)
397 public void Start2_FileName_Null ()
399 ProcessStartInfo startInfo = new ProcessStartInfo ((string) null);
402 startInfo.UseShellExecute = false;
404 Process.Start (startInfo);
406 } catch (InvalidOperationException ex) {
407 // Cannot start process because a file name has
409 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
410 Assert.IsNull (ex.InnerException, "#A3");
411 Assert.IsNotNull (ex.Message, "#A4");
415 startInfo.UseShellExecute = true;
417 Process.Start (startInfo);
419 } catch (InvalidOperationException ex) {
420 // Cannot start process because a file name has
422 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
423 Assert.IsNull (ex.InnerException, "#B3");
424 Assert.IsNotNull (ex.Message, "#B4");
428 [Test] // Start (ProcessStartInfo)
429 public void Start2_FileName_Whitespace ()
431 ProcessStartInfo startInfo = new ProcessStartInfo (" ");
432 startInfo.UseShellExecute = false;
434 Process.Start (startInfo);
436 } catch (Win32Exception ex) {
437 // The system cannot find the file specified
438 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
439 Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
440 Assert.IsNull (ex.InnerException, "#4");
441 Assert.IsNotNull (ex.Message, "#5");
442 Assert.AreEqual (2, ex.NativeErrorCode, "#6");
446 [Test] // Start (ProcessStartInfo)
447 public void Start2_StartInfo_Null ()
450 Process.Start ((ProcessStartInfo) null);
452 } catch (ArgumentNullException ex) {
453 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
454 Assert.IsNull (ex.InnerException, "#3");
455 Assert.IsNotNull (ex.Message, "#4");
456 Assert.IsNotNull (ex.ParamName, "#5");
457 Assert.AreEqual ("startInfo", ex.ParamName, "#6");
461 [Test] // Start (string)
462 public void Start3_FileName_Empty ()
465 Process.Start (string.Empty);
467 } catch (InvalidOperationException ex) {
468 // Cannot start process because a file name has
470 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
471 Assert.IsNull (ex.InnerException, "#3");
472 Assert.IsNotNull (ex.Message, "#4");
476 [Test] // Start (string)
477 public void Start3_FileName_NotFound ()
480 Assert.Ignore ("On Unix and Mac OS X, we try " +
481 "to open any file (using xdg-open, ...)" +
482 " and we do not report an exception " +
485 string exe = @"c:\shouldnoteverexist.exe";
487 // absolute path, no shell
491 } catch (Win32Exception ex) {
492 // The system cannot find the file specified
493 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
494 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
495 Assert.IsNull (ex.InnerException, "#A4");
496 Assert.IsNotNull (ex.Message, "#A5");
497 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
500 // relative path, no shell
502 Process.Start ("shouldnoteverexist.exe");
504 } catch (Win32Exception ex) {
505 // The system cannot find the file specified
506 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
507 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
508 Assert.IsNull (ex.InnerException, "#B4");
509 Assert.IsNotNull (ex.Message, "#B5");
510 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
514 [Test] // Start (string)
515 public void Start3_FileName_Null ()
518 Process.Start ((string) null);
520 } catch (InvalidOperationException ex) {
521 // Cannot start process because a file name has
523 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
524 Assert.IsNull (ex.InnerException, "#3");
525 Assert.IsNotNull (ex.Message, "#4");
529 [Test] // Start (string, string)
530 public void Start4_Arguments_Null ()
533 Assert.Ignore ("On Unix and Mac OS X, we try " +
534 "to open any file (using xdg-open, ...)" +
535 " and we do not report an exception " +
538 string exe = @"c:\shouldnoteverexist.exe";
541 Process.Start ("whatever.exe", (string) null);
543 } catch (Win32Exception ex) {
544 // The system cannot find the file specified
545 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
546 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
547 Assert.IsNull (ex.InnerException, "#B4");
548 Assert.IsNotNull (ex.Message, "#B5");
549 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
553 [Test] // Start (string, string)
554 public void Start4_FileName_Empty ()
557 Process.Start (string.Empty, string.Empty);
559 } catch (InvalidOperationException ex) {
560 // Cannot start process because a file name has
562 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
563 Assert.IsNull (ex.InnerException, "#3");
564 Assert.IsNotNull (ex.Message, "#4");
568 [Test] // Start (string, string)
569 public void Start4_FileName_NotFound ()
572 Assert.Ignore ("On Unix and Mac OS X, we try " +
573 "to open any file (using xdg-open, ...)" +
574 " and we do not report an exception " +
577 string exe = @"c:\shouldnoteverexist.exe";
579 // absolute path, no shell
581 Process.Start (exe, string.Empty);
583 } catch (Win32Exception ex) {
584 // The system cannot find the file specified
585 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
586 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
587 Assert.IsNull (ex.InnerException, "#A4");
588 Assert.IsNotNull (ex.Message, "#A5");
589 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
592 // relative path, no shell
594 Process.Start ("shouldnoteverexist.exe", string.Empty);
596 } catch (Win32Exception ex) {
597 // The system cannot find the file specified
598 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
599 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
600 Assert.IsNull (ex.InnerException, "#B4");
601 Assert.IsNotNull (ex.Message, "#B5");
602 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
607 public void Start_UseShellExecuteWithEmptyUserName ()
610 Assert.Ignore ("On Unix and Mac OS X, we try " +
611 "to open any file (using xdg-open, ...)" +
612 " and we do not report an exception " +
615 string exe = @"c:\shouldnoteverexist.exe";
618 Process p = new Process ();
619 p.StartInfo.FileName = exe;
620 p.StartInfo.UseShellExecute = true;
621 p.StartInfo.UserName = "";
624 } catch (InvalidOperationException) {
626 } catch (Win32Exception) {
630 Process p = new Process ();
631 p.StartInfo.FileName = exe;
632 p.StartInfo.UseShellExecute = true;
633 p.StartInfo.UserName = null;
636 } catch (InvalidOperationException) {
638 } catch (Win32Exception) {
642 [Test] // Start (string, string)
643 public void Start4_FileName_Null ()
646 Process.Start ((string) null, string.Empty);
648 } catch (InvalidOperationException ex) {
649 // Cannot start process because a file name has
651 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
652 Assert.IsNull (ex.InnerException, "#3");
653 Assert.IsNotNull (ex.Message, "#4");
658 public void StartInfo ()
660 ProcessStartInfo startInfo = new ProcessStartInfo ();
662 Process p = new Process ();
663 Assert.IsNotNull (p.StartInfo, "#A1");
664 p.StartInfo = startInfo;
665 Assert.AreSame (startInfo, p.StartInfo, "#A2");
669 public void StartInfo_Null ()
671 Process p = new Process ();
675 } catch (ArgumentNullException ex) {
676 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
677 Assert.IsNull (ex.InnerException, "#3");
678 Assert.IsNotNull (ex.Message, "#4");
679 Assert.IsNotNull (ex.ParamName, "#5");
680 Assert.AreEqual ("value", ex.ParamName, "#6");
685 [NUnit.Framework.Category ("NotDotNet")]
686 [NUnit.Framework.Category ("MobileNotWorking")]
687 public void TestRedirectedOutputIsAsync ()
689 // Test requires cygwin, so we just bail out for now.
690 if (Path.DirectorySeparatorChar == '\\')
691 Assert.Ignore ("Test requires cygwin.");
693 Process p = new Process ();
694 p.StartInfo = new ProcessStartInfo ("/bin/sh", "-c \"sleep 2; echo hello\"");
695 p.StartInfo.RedirectStandardOutput = true;
696 p.StartInfo.UseShellExecute = false;
699 Stream stdout = p.StandardOutput.BaseStream;
701 byte [] buffer = new byte [200];
703 // start async Read operation
704 DateTime start = DateTime.Now;
705 IAsyncResult ar = stdout.BeginRead (buffer, 0, buffer.Length,
706 new AsyncCallback (Read), stdout);
708 Assert.IsTrue ((DateTime.Now - start).TotalMilliseconds < 1000, "#01 BeginRead was not async");
710 Assert.AreEqual (0, p.ExitCode, "#02 script failure");
713 ar.AsyncWaitHandle.WaitOne (2000, false);
714 if (bytesRead < "hello".Length)
715 Assert.Fail ("#03 got {0} bytes", bytesRead);
716 Assert.AreEqual ("hello", Encoding.Default.GetString (buffer, 0, 5), "#04");
720 void Read (IAsyncResult ar)
722 Stream stm = (Stream) ar.AsyncState;
723 bytesRead = stm.EndRead (ar);
726 static bool RunningOnUnix {
728 int p = (int)Environment.OSVersion.Platform;
729 return ((p == 128) || (p == 4) || (p == 6));
733 public int bytesRead = -1;
736 [NUnit.Framework.Category ("MobileNotWorking")]
737 // This was for bug #459450
738 public void TestEventRaising ()
740 EventWaitHandle errorClosed = new ManualResetEvent(false);
741 EventWaitHandle outClosed = new ManualResetEvent(false);
742 EventWaitHandle exited = new ManualResetEvent(false);
744 Process p = new Process();
746 p.StartInfo = GetCrossPlatformStartInfo ();
747 p.StartInfo.UseShellExecute = false;
748 p.StartInfo.RedirectStandardOutput = true;
749 p.StartInfo.RedirectStandardError = true;
750 p.StartInfo.RedirectStandardInput = false;
751 p.OutputDataReceived += (object sender, DataReceivedEventArgs e) => {
752 if (e.Data == null) {
757 p.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => {
758 if (e.Data == null) {
763 p.Exited += (object sender, EventArgs e) => {
767 p.EnableRaisingEvents = true;
771 p.BeginErrorReadLine();
772 p.BeginOutputReadLine();
774 Console.WriteLine("started, waiting for handles");
775 bool r = WaitHandle.WaitAll(new WaitHandle[] { errorClosed, outClosed, exited }, 10000, false);
777 Assert.AreEqual (true, r, "Null Argument Events Raised");
781 [NUnit.Framework.Category ("MobileNotWorking")]
782 public void TestEnableEventsAfterExitedEvent ()
784 Process p = new Process ();
786 p.StartInfo = GetCrossPlatformStartInfo ();
787 p.StartInfo.UseShellExecute = false;
788 p.StartInfo.RedirectStandardOutput = true;
789 p.StartInfo.RedirectStandardError = true;
791 var exitedCalledCounter = 0;
792 var exited = new ManualResetEventSlim ();
793 p.Exited += (object sender, EventArgs e) => {
794 exitedCalledCounter++;
795 Assert.IsTrue (p.HasExited);
799 p.EnableRaisingEvents = true;
802 p.BeginErrorReadLine ();
803 p.BeginOutputReadLine ();
807 Assert.AreEqual (1, exitedCalledCounter);
809 Assert.AreEqual (1, exitedCalledCounter);
813 [NUnit.Framework.Category ("MobileNotWorking")]
814 public void TestEnableEventsBeforeExitedEvent ()
816 Process p = new Process ();
818 p.StartInfo = GetCrossPlatformStartInfo ();
819 p.StartInfo.UseShellExecute = false;
820 p.StartInfo.RedirectStandardOutput = true;
821 p.StartInfo.RedirectStandardError = true;
823 p.EnableRaisingEvents = true;
825 var exitedCalledCounter = 0;
826 var exited = new ManualResetEventSlim ();
827 p.Exited += (object sender, EventArgs e) => {
828 exitedCalledCounter++;
829 Assert.IsTrue (p.HasExited);
834 p.BeginErrorReadLine ();
835 p.BeginOutputReadLine ();
839 Assert.AreEqual (1, exitedCalledCounter);
841 Assert.AreEqual (1, exitedCalledCounter);
845 ProcessStartInfo GetCrossPlatformStartInfo ()
850 path = "/system/bin/ls";
854 return new ProcessStartInfo (path, "/");
856 return new ProcessStartInfo ("help", "");
860 public void ProcessName_NotStarted ()
862 Process p = new Process ();
865 String.IsNullOrEmpty (p.ProcessName);
866 } catch (Exception ex) {
870 Assert.IsNotNull (e, "ProcessName should raise if process was not started");
872 //msg should be "No process is associated with this object"
873 Assert.AreEqual (e.GetType (), typeof (InvalidOperationException),
874 "exception should be IOE, I got: " + e.GetType ().Name);
876 Assert.IsNull (e.InnerException, "IOE inner exception should be null");
880 [NUnit.Framework.Category ("MobileNotWorking")]
881 public void ProcessName_AfterExit ()
883 Process p = new Process ();
884 p.StartInfo = GetCrossPlatformStartInfo ();
885 p.StartInfo.UseShellExecute = false;
886 p.StartInfo.RedirectStandardOutput = true;
887 p.StartInfo.RedirectStandardError = true;
889 p.BeginErrorReadLine();
890 p.BeginOutputReadLine();
892 String.IsNullOrEmpty (p.ExitCode + "");
896 String.IsNullOrEmpty (p.ProcessName);
897 } catch (Exception ex) {
901 Assert.IsNotNull (e, "ProcessName should raise if process was finished");
903 //msg should be "Process has exited, so the requested information is not available"
904 Assert.AreEqual (e.GetType (), typeof (InvalidOperationException),
905 "exception should be IOE, I got: " + e.GetType ().Name);
907 Assert.IsNull (e.InnerException, "IOE inner exception should be null");
911 public void Handle_ThrowsOnNotStarted ()
913 Process p = new Process ();
916 Assert.Fail ("Handle should throw for unstated procs, but returned " + x);
917 } catch (InvalidOperationException) {
922 public void HasExitedCurrent () {
923 Assert.IsFalse (Process.GetCurrentProcess ().HasExited);
927 [NUnit.Framework.Category ("MobileNotWorking")]
928 public void DisposeWithDisposedStreams ()
930 var psi = GetCrossPlatformStartInfo ();
931 psi.RedirectStandardInput = true;
932 psi.RedirectStandardOutput = true;
933 psi.UseShellExecute = false;
935 var p = Process.Start (psi);
936 p.StandardInput.BaseStream.Dispose ();
937 p.StandardOutput.BaseStream.Dispose ();
942 [NUnit.Framework.Category ("MobileNotWorking")]
943 public void StandardInputWrite ()
945 var psi = GetCrossPlatformStartInfo ();
946 psi.RedirectStandardInput = true;
947 psi.RedirectStandardOutput = true;
948 psi.UseShellExecute = false;
950 using (var p = Process.Start (psi)) {
951 for (int i = 0; i < 1024 * 9; ++i)
952 p.StandardInput.Write ('x');
957 public void Modules () {
958 var modules = Process.GetCurrentProcess ().Modules;
959 foreach (var a in AppDomain.CurrentDomain.GetAssemblies ()) {
961 var name = a.GetName ();
963 StringBuilder sb = new StringBuilder ();
964 sb.AppendFormat ("Could not found: {0} {1}\n", name.Name, name.Version);
965 sb.AppendLine ("Looked in assemblies:");
967 foreach (var o in modules) {
968 var m = (ProcessModule) o;
970 sb.AppendFormat (" {0} {1}.{2}.{3}\n", m.FileName.ToString (),
971 m.FileVersionInfo.FileMajorPart,
972 m.FileVersionInfo.FileMinorPart,
973 m.FileVersionInfo.FileBuildPart);
975 if (!m.FileName.StartsWith ("[In Memory] " + name.Name))
978 var fv = m.FileVersionInfo;
979 if (fv.FileBuildPart != name.Version.Build ||
980 fv.FileMinorPart != name.Version.Minor ||
981 fv.FileMajorPart != name.Version.Major)
987 Assert.IsTrue (found, sb.ToString ());