a729bf5bcbd46baf7b3d1c6164f200266ca080fa
[mono.git] / mcs / class / System / Test / System.Diagnostics / ProcessTest.cs
1 //
2 // ProcessTest.cs - NUnit Test Cases for System.Diagnostics.Process
3 //
4 // Authors:
5 //   Gert Driesen (drieseng@users.sourceforge.net)
6 //   Robert Jordan <robertj@gmx.net>
7 //
8 // (C) 2007 Gert Driesen
9 // 
10
11 using System;
12 using System.Diagnostics;
13 using System.IO;
14 using System.Text;
15
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Diagnostics
19 {
20         [TestFixture]
21         public class ProcessTest
22         {
23                 [Test]
24                 public void GetProcessById_MachineName_Null ()
25                 {
26                         try {
27                                 Process.GetProcessById (1, (string) null);
28                                 Assert.Fail ("#1");
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");
35                         }
36                 }
37
38                 [Test]
39                 public void GetProcesses_MachineName_Null ()
40                 {
41                         try {
42                                 Process.GetProcesses ((string) null);
43                                 Assert.Fail ("#1");
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");
50                         }
51                 }
52
53                 [Test]
54                 [Category ("NotDotNet")]
55                 public void TestRedirectedOutputIsAsync ()
56                 {
57                         // Test requires cygwin, so we just bail out for now.
58                         if (Path.DirectorySeparatorChar == '\\')
59                                 return;
60                         
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;
65                         p.Start ();
66
67                         Stream stdout = p.StandardOutput.BaseStream;
68
69                         byte [] buffer = new byte [200];
70
71                         // start async Read operation
72                         DateTime start = DateTime.Now;
73                         stdout.BeginRead (buffer, 0, buffer.Length,
74                                           new AsyncCallback (Read), stdout);
75
76                         Assert.IsTrue ((DateTime.Now - start).TotalMilliseconds < 1000, "#01 BeginRead was not async");
77                         p.WaitForExit ();
78                         Assert.AreEqual (0, p.ExitCode, "#02 script failure");
79                         if (bytesRead < "hello".Length)
80                                 Assert.Fail ("#03 got {0} bytes", bytesRead);
81                         Assert.AreEqual ("hello", Encoding.Default.GetString (buffer, 0, 5), "#04");
82                 }
83
84                 void Read (IAsyncResult ar)
85                 {
86                         Stream stm = (Stream) ar.AsyncState;
87                         bytesRead = stm.EndRead (ar);
88                 }
89
90                 int bytesRead = Int32.MinValue;
91         }
92 }