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.Diagnostics;
16 using NUnit.Framework;
18 namespace MonoTests.System.Diagnostics
21 public class ProcessTest
24 public void GetProcessById_MachineName_Null ()
27 Process.GetProcessById (1, (string) null);
29 } catch (ArgumentNullException ex) {
30 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
31 Assert.IsNotNull (ex.Message, "#3");
32 Assert.IsNotNull (ex.ParamName, "#4");
33 Assert.AreEqual ("machineName", ex.ParamName, "#5");
34 Assert.IsNull (ex.InnerException, "#6");
39 public void GetProcesses_MachineName_Null ()
42 Process.GetProcesses ((string) null);
44 } catch (ArgumentNullException ex) {
45 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
46 Assert.IsNotNull (ex.Message, "#3");
47 Assert.IsNotNull (ex.ParamName, "#4");
48 Assert.AreEqual ("machineName", ex.ParamName, "#5");
49 Assert.IsNull (ex.InnerException, "#6");
54 [Category ("NotDotNet")]
55 public void TestRedirectedOutputIsAsync ()
57 // Test requires cygwin, so we just bail out for now.
58 if (Path.DirectorySeparatorChar == '\\')
61 Process p = new Process ();
62 p.StartInfo = new ProcessStartInfo ("/bin/sh", "-c \"sleep 2; echo hello\"");
63 p.StartInfo.RedirectStandardOutput = true;
64 p.StartInfo.UseShellExecute = false;
67 Stream stdout = p.StandardOutput.BaseStream;
69 byte [] buffer = new byte [200];
71 // start async Read operation
72 DateTime start = DateTime.Now;
73 IAsyncResult ar = stdout.BeginRead (buffer, 0, buffer.Length,
74 new AsyncCallback (Read), stdout);
76 Assert.IsTrue ((DateTime.Now - start).TotalMilliseconds < 1000, "#01 BeginRead was not async");
78 Assert.AreEqual (0, p.ExitCode, "#02 script failure");
81 ar.AsyncWaitHandle.WaitOne (2000, false);
82 if (bytesRead < "hello".Length)
83 Assert.Fail ("#03 got {0} bytes", bytesRead);
84 Assert.AreEqual ("hello", Encoding.Default.GetString (buffer, 0, 5), "#04");
88 void Read (IAsyncResult ar)
90 Stream stm = (Stream) ar.AsyncState;
91 bytesRead = stm.EndRead (ar);