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 Assert.AreEqual (2, ex.NativeErrorCode, "#6");
291 [Test] // Start (ProcessStartInfo)
292 public void Start2_FileName_Empty ()
294 ProcessStartInfo startInfo = new ProcessStartInfo (string.Empty);
297 startInfo.UseShellExecute = false;
299 Process.Start (startInfo);
301 } catch (InvalidOperationException ex) {
302 // Cannot start process because a file name has
304 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
305 Assert.IsNull (ex.InnerException, "#A3");
306 Assert.IsNotNull (ex.Message, "#A4");
310 startInfo.UseShellExecute = true;
312 Process.Start (startInfo);
314 } catch (InvalidOperationException ex) {
315 // Cannot start process because a file name has
317 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
318 Assert.IsNull (ex.InnerException, "#B3");
319 Assert.IsNotNull (ex.Message, "#B4");
323 [Test] // Start (ProcessStartInfo)
324 public void Start2_FileName_NotFound ()
326 ProcessStartInfo startInfo = new ProcessStartInfo ();
327 string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
328 : @"c:\shouldnoteverexist.exe";
330 // absolute path, no shell
331 startInfo.FileName = exe;
332 startInfo.UseShellExecute = false;
334 Process.Start (startInfo);
336 } catch (Win32Exception ex) {
337 // The system cannot find the file specified
338 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
339 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
340 Assert.IsNull (ex.InnerException, "#A4");
341 Assert.IsNotNull (ex.Message, "#A5");
342 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
345 // relative path, no shell
346 startInfo.FileName = "shouldnoteverexist.exe";
347 startInfo.UseShellExecute = false;
349 Process.Start (startInfo);
351 } catch (Win32Exception ex) {
352 // The system cannot find the file specified
353 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
354 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
355 Assert.IsNull (ex.InnerException, "#B4");
356 Assert.IsNotNull (ex.Message, "#B5");
357 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
361 Assert.Ignore ("On Unix and Mac OS X, we try " +
362 "to open any file (using xdg-open, ...)" +
363 " and we do not report an exception " +
366 // absolute path, shell
367 startInfo.FileName = exe;
368 startInfo.UseShellExecute = true;
370 Process.Start (startInfo);
372 } catch (Win32Exception ex) {
373 // The system cannot find the file specified
374 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
375 Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
376 Assert.IsNull (ex.InnerException, "#C4");
377 Assert.IsNotNull (ex.Message, "#C5");
378 Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
381 // relative path, shell
382 startInfo.FileName = "shouldnoteverexist.exe";
383 startInfo.UseShellExecute = true;
385 Process.Start (startInfo);
387 } catch (Win32Exception ex) {
388 // The system cannot find the file specified
389 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
390 Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
391 Assert.IsNull (ex.InnerException, "#D4");
392 Assert.IsNotNull (ex.Message, "#D5");
393 Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
397 [Test] // Start (ProcessStartInfo)
398 public void Start2_FileName_Null ()
400 ProcessStartInfo startInfo = new ProcessStartInfo ((string) null);
403 startInfo.UseShellExecute = false;
405 Process.Start (startInfo);
407 } catch (InvalidOperationException ex) {
408 // Cannot start process because a file name has
410 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
411 Assert.IsNull (ex.InnerException, "#A3");
412 Assert.IsNotNull (ex.Message, "#A4");
416 startInfo.UseShellExecute = true;
418 Process.Start (startInfo);
420 } catch (InvalidOperationException ex) {
421 // Cannot start process because a file name has
423 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
424 Assert.IsNull (ex.InnerException, "#B3");
425 Assert.IsNotNull (ex.Message, "#B4");
429 [Test] // Start (ProcessStartInfo)
430 public void Start2_FileName_Whitespace ()
432 ProcessStartInfo startInfo = new ProcessStartInfo (" ");
433 startInfo.UseShellExecute = false;
435 Process.Start (startInfo);
437 } catch (Win32Exception ex) {
438 // The system cannot find the file specified
439 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
440 Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
441 Assert.IsNull (ex.InnerException, "#4");
442 Assert.IsNotNull (ex.Message, "#5");
443 Assert.AreEqual (2, ex.NativeErrorCode, "#6");
447 [Test] // Start (ProcessStartInfo)
448 public void Start2_StartInfo_Null ()
451 Process.Start ((ProcessStartInfo) null);
453 } catch (ArgumentNullException ex) {
454 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
455 Assert.IsNull (ex.InnerException, "#3");
456 Assert.IsNotNull (ex.Message, "#4");
457 Assert.IsNotNull (ex.ParamName, "#5");
458 Assert.AreEqual ("startInfo", ex.ParamName, "#6");
462 [Test] // Start (string)
463 public void Start3_FileName_Empty ()
466 Process.Start (string.Empty);
468 } catch (InvalidOperationException ex) {
469 // Cannot start process because a file name has
471 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
472 Assert.IsNull (ex.InnerException, "#3");
473 Assert.IsNotNull (ex.Message, "#4");
477 [Test] // Start (string)
478 public void Start3_FileName_NotFound ()
481 Assert.Ignore ("On Unix and Mac OS X, we try " +
482 "to open any file (using xdg-open, ...)" +
483 " and we do not report an exception " +
486 string exe = @"c:\shouldnoteverexist.exe";
488 // absolute path, no shell
492 } catch (Win32Exception ex) {
493 // The system cannot find the file specified
494 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
495 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
496 Assert.IsNull (ex.InnerException, "#A4");
497 Assert.IsNotNull (ex.Message, "#A5");
498 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
501 // relative path, no shell
503 Process.Start ("shouldnoteverexist.exe");
505 } catch (Win32Exception ex) {
506 // The system cannot find the file specified
507 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
508 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
509 Assert.IsNull (ex.InnerException, "#B4");
510 Assert.IsNotNull (ex.Message, "#B5");
511 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
515 [Test] // Start (string)
516 public void Start3_FileName_Null ()
519 Process.Start ((string) null);
521 } catch (InvalidOperationException ex) {
522 // Cannot start process because a file name has
524 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
525 Assert.IsNull (ex.InnerException, "#3");
526 Assert.IsNotNull (ex.Message, "#4");
530 [Test] // Start (string, string)
531 public void Start4_Arguments_Null ()
534 Assert.Ignore ("On Unix and Mac OS X, we try " +
535 "to open any file (using xdg-open, ...)" +
536 " and we do not report an exception " +
539 string exe = @"c:\shouldnoteverexist.exe";
542 Process.Start ("whatever.exe", (string) null);
544 } catch (Win32Exception ex) {
545 // The system cannot find the file specified
546 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
547 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
548 Assert.IsNull (ex.InnerException, "#B4");
549 Assert.IsNotNull (ex.Message, "#B5");
550 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
554 [Test] // Start (string, string)
555 public void Start4_FileName_Empty ()
558 Process.Start (string.Empty, string.Empty);
560 } catch (InvalidOperationException ex) {
561 // Cannot start process because a file name has
563 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
564 Assert.IsNull (ex.InnerException, "#3");
565 Assert.IsNotNull (ex.Message, "#4");
569 [Test] // Start (string, string)
570 public void Start4_FileName_NotFound ()
573 Assert.Ignore ("On Unix and Mac OS X, we try " +
574 "to open any file (using xdg-open, ...)" +
575 " and we do not report an exception " +
578 string exe = @"c:\shouldnoteverexist.exe";
580 // absolute path, no shell
582 Process.Start (exe, string.Empty);
584 } catch (Win32Exception ex) {
585 // The system cannot find the file specified
586 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
587 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
588 Assert.IsNull (ex.InnerException, "#A4");
589 Assert.IsNotNull (ex.Message, "#A5");
590 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
593 // relative path, no shell
595 Process.Start ("shouldnoteverexist.exe", string.Empty);
597 } catch (Win32Exception ex) {
598 // The system cannot find the file specified
599 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
600 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
601 Assert.IsNull (ex.InnerException, "#B4");
602 Assert.IsNotNull (ex.Message, "#B5");
603 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
608 public void Start_UseShellExecuteWithEmptyUserName ()
611 Assert.Ignore ("On Unix and Mac OS X, we try " +
612 "to open any file (using xdg-open, ...)" +
613 " and we do not report an exception " +
616 string exe = @"c:\shouldnoteverexist.exe";
619 Process p = new Process ();
620 p.StartInfo.FileName = exe;
621 p.StartInfo.UseShellExecute = true;
622 p.StartInfo.UserName = "";
625 } catch (InvalidOperationException) {
627 } catch (Win32Exception) {
631 Process p = new Process ();
632 p.StartInfo.FileName = exe;
633 p.StartInfo.UseShellExecute = true;
634 p.StartInfo.UserName = null;
637 } catch (InvalidOperationException) {
639 } catch (Win32Exception) {
643 [Test] // Start (string, string)
644 public void Start4_FileName_Null ()
647 Process.Start ((string) null, string.Empty);
649 } catch (InvalidOperationException ex) {
650 // Cannot start process because a file name has
652 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
653 Assert.IsNull (ex.InnerException, "#3");
654 Assert.IsNotNull (ex.Message, "#4");
659 public void StartInfo ()
661 ProcessStartInfo startInfo = new ProcessStartInfo ();
663 Process p = new Process ();
664 Assert.IsNotNull (p.StartInfo, "#A1");
665 p.StartInfo = startInfo;
666 Assert.AreSame (startInfo, p.StartInfo, "#A2");
670 public void StartInfo_Null ()
672 Process p = new Process ();
676 } catch (ArgumentNullException ex) {
677 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
678 Assert.IsNull (ex.InnerException, "#3");
679 Assert.IsNotNull (ex.Message, "#4");
680 Assert.IsNotNull (ex.ParamName, "#5");
681 Assert.AreEqual ("value", ex.ParamName, "#6");
686 [NUnit.Framework.Category ("NotDotNet")]
687 [NUnit.Framework.Category ("MobileNotWorking")]
688 public void TestRedirectedOutputIsAsync ()
690 // Test requires cygwin, so we just bail out for now.
691 if (Path.DirectorySeparatorChar == '\\')
692 Assert.Ignore ("Test requires cygwin.");
694 Process p = new Process ();
695 p.StartInfo = new ProcessStartInfo ("/bin/sh", "-c \"sleep 2; echo hello\"");
696 p.StartInfo.RedirectStandardOutput = true;
697 p.StartInfo.UseShellExecute = false;
700 Stream stdout = p.StandardOutput.BaseStream;
702 byte [] buffer = new byte [200];
704 // start async Read operation
705 DateTime start = DateTime.Now;
706 IAsyncResult ar = stdout.BeginRead (buffer, 0, buffer.Length,
707 new AsyncCallback (Read), stdout);
709 Assert.IsTrue ((DateTime.Now - start).TotalMilliseconds < 1000, "#01 BeginRead was not async");
711 Assert.AreEqual (0, p.ExitCode, "#02 script failure");
714 ar.AsyncWaitHandle.WaitOne (2000, false);
715 if (bytesRead < "hello".Length)
716 Assert.Fail ("#03 got {0} bytes", bytesRead);
717 Assert.AreEqual ("hello", Encoding.Default.GetString (buffer, 0, 5), "#04");
721 void Read (IAsyncResult ar)
723 Stream stm = (Stream) ar.AsyncState;
724 bytesRead = stm.EndRead (ar);
727 static bool RunningOnUnix {
729 int p = (int)Environment.OSVersion.Platform;
730 return ((p == 128) || (p == 4) || (p == 6));
734 public int bytesRead = -1;
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 var exited = new ManualResetEventSlim ();
794 p.Exited += (object sender, EventArgs e) => {
795 exitedCalledCounter++;
796 Assert.IsTrue (p.HasExited);
800 p.EnableRaisingEvents = true;
803 p.BeginErrorReadLine ();
804 p.BeginOutputReadLine ();
808 Assert.AreEqual (1, exitedCalledCounter);
810 Assert.AreEqual (1, exitedCalledCounter);
814 [NUnit.Framework.Category ("MobileNotWorking")]
815 public void TestEnableEventsBeforeExitedEvent ()
817 Process p = new Process ();
819 p.StartInfo = GetCrossPlatformStartInfo ();
820 p.StartInfo.UseShellExecute = false;
821 p.StartInfo.RedirectStandardOutput = true;
822 p.StartInfo.RedirectStandardError = true;
824 p.EnableRaisingEvents = true;
826 var exitedCalledCounter = 0;
827 var exited = new ManualResetEventSlim ();
828 p.Exited += (object sender, EventArgs e) => {
829 exitedCalledCounter++;
830 Assert.IsTrue (p.HasExited);
835 p.BeginErrorReadLine ();
836 p.BeginOutputReadLine ();
839 Assert.IsTrue (exited.Wait (10000));
840 Assert.AreEqual (1, exitedCalledCounter);
842 Assert.AreEqual (1, exitedCalledCounter);
846 [NUnit.Framework.Category ("MobileNotWorking")]
847 public void TestDisableEventsBeforeExitedEvent ()
849 Process p = new Process ();
851 p.StartInfo = GetCrossPlatformStartInfo ();
852 p.StartInfo.UseShellExecute = false;
853 p.StartInfo.RedirectStandardOutput = true;
854 p.StartInfo.RedirectStandardError = true;
856 p.EnableRaisingEvents = false;
858 var exitedCalledCounter = 0;
859 p.Exited += (object sender, EventArgs e) => {
860 exitedCalledCounter++;
864 p.BeginErrorReadLine ();
865 p.BeginOutputReadLine ();
868 Assert.AreEqual (0, exitedCalledCounter);
871 ProcessStartInfo GetCrossPlatformStartInfo ()
876 path = "/system/bin/ls";
880 return new ProcessStartInfo (path, "/");
882 return new ProcessStartInfo ("help", "");
884 #endif // MONO_FEATURE_PROCESS_START
887 public void ProcessName_NotStarted ()
889 Process p = new Process ();
892 String.IsNullOrEmpty (p.ProcessName);
893 } catch (Exception ex) {
897 Assert.IsNotNull (e, "ProcessName should raise if process was not started");
899 //msg should be "No process is associated with this object"
900 Assert.AreEqual (e.GetType (), typeof (InvalidOperationException),
901 "exception should be IOE, I got: " + e.GetType ().Name);
903 Assert.IsNull (e.InnerException, "IOE inner exception should be null");
906 #if MONO_FEATURE_PROCESS_START
908 [NUnit.Framework.Category ("MobileNotWorking")]
909 public void ProcessName_AfterExit ()
911 Process p = new Process ();
912 p.StartInfo = GetCrossPlatformStartInfo ();
913 p.StartInfo.UseShellExecute = false;
914 p.StartInfo.RedirectStandardOutput = true;
915 p.StartInfo.RedirectStandardError = true;
917 p.BeginErrorReadLine();
918 p.BeginOutputReadLine();
920 String.IsNullOrEmpty (p.ExitCode + "");
924 String.IsNullOrEmpty (p.ProcessName);
925 } catch (Exception ex) {
929 Assert.IsNotNull (e, "ProcessName should raise if process was finished");
931 //msg should be "Process has exited, so the requested information is not available"
932 Assert.AreEqual (e.GetType (), typeof (InvalidOperationException),
933 "exception should be IOE, I got: " + e.GetType ().Name);
935 Assert.IsNull (e.InnerException, "IOE inner exception should be null");
937 #endif // MONO_FEATURE_PROCESS_START
940 public void Handle_ThrowsOnNotStarted ()
942 Process p = new Process ();
945 Assert.Fail ("Handle should throw for unstated procs, but returned " + x);
946 } catch (InvalidOperationException) {
951 public void HasExitedCurrent () {
952 Assert.IsFalse (Process.GetCurrentProcess ().HasExited);
955 #if MONO_FEATURE_PROCESS_START
957 [NUnit.Framework.Category ("MobileNotWorking")]
958 public void DisposeWithDisposedStreams ()
960 var psi = GetCrossPlatformStartInfo ();
961 psi.RedirectStandardInput = true;
962 psi.RedirectStandardOutput = true;
963 psi.UseShellExecute = false;
965 var p = Process.Start (psi);
966 p.StandardInput.BaseStream.Dispose ();
967 p.StandardOutput.BaseStream.Dispose ();
972 [NUnit.Framework.Category ("MobileNotWorking")]
973 public void StandardInputWrite ()
975 var psi = GetCrossPlatformStartInfo ();
976 psi.RedirectStandardInput = true;
977 psi.RedirectStandardOutput = true;
978 psi.UseShellExecute = false;
980 using (var p = Process.Start (psi)) {
981 for (int i = 0; i < 1024 * 9; ++i)
982 p.StandardInput.Write ('x');
985 #endif // MONO_FEATURE_PROCESS_START
988 public void Modules () {
989 var modules = Process.GetCurrentProcess ().Modules;
990 foreach (var a in AppDomain.CurrentDomain.GetAssemblies ()) {
992 var name = a.GetName ();
994 StringBuilder sb = new StringBuilder ();
995 sb.AppendFormat ("Could not found: {0} {1}\n", name.Name, name.Version);
996 sb.AppendLine ("Looked in assemblies:");
998 foreach (var o in modules) {
999 var m = (ProcessModule) o;
1001 sb.AppendFormat (" {0} {1}.{2}.{3}\n", m.FileName.ToString (),
1002 m.FileVersionInfo.FileMajorPart,
1003 m.FileVersionInfo.FileMinorPart,
1004 m.FileVersionInfo.FileBuildPart);
1006 if (!m.FileName.StartsWith ("[In Memory] " + name.Name))
1009 var fv = m.FileVersionInfo;
1010 if (fv.FileBuildPart != name.Version.Build ||
1011 fv.FileMinorPart != name.Version.Minor ||
1012 fv.FileMajorPart != name.Version.Major)
1018 Assert.IsTrue (found, sb.ToString ());