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;
17 using NUnit.Framework;
19 namespace MonoTests.System.Diagnostics
22 public class ProcessTest
25 public void GetProcessById_MachineName_Null ()
28 Process.GetProcessById (1, (string) null);
30 } catch (ArgumentNullException ex) {
31 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
32 Assert.IsNotNull (ex.Message, "#3");
33 Assert.IsNotNull (ex.ParamName, "#4");
34 Assert.AreEqual ("machineName", ex.ParamName, "#5");
35 Assert.IsNull (ex.InnerException, "#6");
40 public void GetProcesses_MachineName_Null ()
43 Process.GetProcesses ((string) null);
45 } catch (ArgumentNullException ex) {
46 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
47 Assert.IsNotNull (ex.Message, "#3");
48 Assert.IsNotNull (ex.ParamName, "#4");
49 Assert.AreEqual ("machineName", ex.ParamName, "#5");
50 Assert.IsNull (ex.InnerException, "#6");
55 public void PriorityClass_NotStarted ()
57 Process process = new Process ();
59 process.PriorityClass = ProcessPriorityClass.Normal;
61 } catch (InvalidOperationException ex) {
62 // No process is associated with this object
63 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
64 Assert.IsNull (ex.InnerException, "#A3");
65 Assert.IsNotNull (ex.Message, "#A4");
69 Assert.Fail ("#B1:" + process.PriorityClass);
70 } catch (InvalidOperationException ex) {
71 // No process is associated with this object
72 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
73 Assert.IsNull (ex.InnerException, "#B3");
74 Assert.IsNotNull (ex.Message, "#B4");
79 public void PriorityClass_Invalid ()
81 Process process = new Process ();
83 process.PriorityClass = (ProcessPriorityClass) 666;
85 } catch (InvalidEnumArgumentException ex) {
86 Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
87 Assert.IsNull (ex.InnerException, "#3");
88 Assert.IsNotNull (ex.Message, "#4");
89 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#5");
90 Assert.IsTrue (ex.Message.IndexOf (typeof (ProcessPriorityClass).Name) != -1, "#6");
91 Assert.IsNotNull (ex.ParamName, "#7");
92 Assert.AreEqual ("value", ex.ParamName, "#8");
97 public void Start1_FileName_Empty ()
99 Process process = new Process ();
100 process.StartInfo = new ProcessStartInfo (string.Empty);
103 process.StartInfo.UseShellExecute = false;
107 } catch (InvalidOperationException ex) {
108 // Cannot start process because a file name has
110 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
111 Assert.IsNull (ex.InnerException, "#A3");
112 Assert.IsNotNull (ex.Message, "#A4");
116 process.StartInfo.UseShellExecute = true;
120 } catch (InvalidOperationException ex) {
121 // Cannot start process because a file name has
123 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
124 Assert.IsNull (ex.InnerException, "#B3");
125 Assert.IsNotNull (ex.Message, "#B4");
130 public void Start1_FileName_InvalidPathCharacters ()
133 // on unix, all characters are allowed
136 string systemDir = Environment.GetFolderPath (Environment.SpecialFolder.System);
137 string exe = "\"" + Path.Combine (systemDir, "calc.exe") + "\"";
139 Process process = new Process ();
140 process.StartInfo = new ProcessStartInfo (exe);
143 process.StartInfo.UseShellExecute = false;
144 Assert.IsTrue (process.Start ());
148 process.StartInfo.UseShellExecute = true;
149 Assert.IsTrue (process.Start ());
154 public void Start1_FileName_NotFound ()
156 Process process = new Process ();
157 string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
158 : @"c:\shouldnoteverexist.exe";
160 // absolute path, no shell
161 process.StartInfo = new ProcessStartInfo (exe);
162 process.StartInfo.UseShellExecute = false;
166 } catch (Win32Exception ex) {
167 // The system cannot find the file specified
168 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
169 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
170 Assert.IsNull (ex.InnerException, "#A4");
171 Assert.IsNotNull (ex.Message, "#A5");
172 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
175 // relative path, no shell
176 process.StartInfo.FileName = "shouldnoteverexist.exe";
177 process.StartInfo.UseShellExecute = false;
181 } catch (Win32Exception ex) {
182 // The system cannot find the file specified
183 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
184 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
185 Assert.IsNull (ex.InnerException, "#B4");
186 Assert.IsNotNull (ex.Message, "#B5");
187 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
191 Assert.Ignore ("On Unix and Mac OS X, we try " +
192 "to open any file (using xdg-open, ...)" +
193 " and we do not report an exception " +
196 // absolute path, shell
197 process.StartInfo.FileName = exe;
198 process.StartInfo.UseShellExecute = true;
202 } catch (Win32Exception ex) {
203 // The system cannot find the file specified
204 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
205 Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
206 Assert.IsNull (ex.InnerException, "#C4");
207 Assert.IsNotNull (ex.Message, "#C5");
208 Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
211 // relative path, shell
212 process.StartInfo.FileName = "shouldnoteverexist.exe";
213 process.StartInfo.UseShellExecute = true;
217 } catch (Win32Exception ex) {
218 // The system cannot find the file specified
219 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
220 Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
221 Assert.IsNull (ex.InnerException, "#D4");
222 Assert.IsNotNull (ex.Message, "#D5");
223 Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
228 public void Start1_FileName_Null ()
230 Process process = new Process ();
231 process.StartInfo = new ProcessStartInfo ((string) null);
234 process.StartInfo.UseShellExecute = false;
238 } catch (InvalidOperationException ex) {
239 // Cannot start process because a file name has
241 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
242 Assert.IsNull (ex.InnerException, "#A3");
243 Assert.IsNotNull (ex.Message, "#A4");
247 process.StartInfo.UseShellExecute = true;
251 } catch (InvalidOperationException ex) {
252 // Cannot start process because a file name has
254 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
255 Assert.IsNull (ex.InnerException, "#B3");
256 Assert.IsNotNull (ex.Message, "#B4");
261 public void Start1_FileName_Whitespace ()
263 Process process = new Process ();
264 process.StartInfo = new ProcessStartInfo (" ");
265 process.StartInfo.UseShellExecute = false;
269 } catch (Win32Exception ex) {
270 // The system cannot find the file specified
271 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
272 Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
273 Assert.IsNull (ex.InnerException, "#4");
274 Assert.IsNotNull (ex.Message, "#5");
275 Assert.AreEqual (2, ex.NativeErrorCode, "#6");
279 [Test] // Start (ProcessStartInfo)
280 public void Start2_FileName_Empty ()
282 ProcessStartInfo startInfo = new ProcessStartInfo (string.Empty);
285 startInfo.UseShellExecute = false;
287 Process.Start (startInfo);
289 } catch (InvalidOperationException ex) {
290 // Cannot start process because a file name has
292 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
293 Assert.IsNull (ex.InnerException, "#A3");
294 Assert.IsNotNull (ex.Message, "#A4");
298 startInfo.UseShellExecute = true;
300 Process.Start (startInfo);
302 } catch (InvalidOperationException ex) {
303 // Cannot start process because a file name has
305 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
306 Assert.IsNull (ex.InnerException, "#B3");
307 Assert.IsNotNull (ex.Message, "#B4");
311 [Test] // Start (ProcessStartInfo)
312 public void Start2_FileName_NotFound ()
314 ProcessStartInfo startInfo = new ProcessStartInfo ();
315 string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
316 : @"c:\shouldnoteverexist.exe";
318 // absolute path, no shell
319 startInfo.FileName = exe;
320 startInfo.UseShellExecute = false;
322 Process.Start (startInfo);
324 } catch (Win32Exception ex) {
325 // The system cannot find the file specified
326 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
327 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
328 Assert.IsNull (ex.InnerException, "#A4");
329 Assert.IsNotNull (ex.Message, "#A5");
330 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
333 // relative path, no shell
334 startInfo.FileName = "shouldnoteverexist.exe";
335 startInfo.UseShellExecute = false;
337 Process.Start (startInfo);
339 } catch (Win32Exception ex) {
340 // The system cannot find the file specified
341 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
342 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
343 Assert.IsNull (ex.InnerException, "#B4");
344 Assert.IsNotNull (ex.Message, "#B5");
345 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
349 Assert.Ignore ("On Unix and Mac OS X, we try " +
350 "to open any file (using xdg-open, ...)" +
351 " and we do not report an exception " +
354 // absolute path, shell
355 startInfo.FileName = exe;
356 startInfo.UseShellExecute = true;
358 Process.Start (startInfo);
360 } catch (Win32Exception ex) {
361 // The system cannot find the file specified
362 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
363 Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
364 Assert.IsNull (ex.InnerException, "#C4");
365 Assert.IsNotNull (ex.Message, "#C5");
366 Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
369 // relative path, shell
370 startInfo.FileName = "shouldnoteverexist.exe";
371 startInfo.UseShellExecute = true;
373 Process.Start (startInfo);
375 } catch (Win32Exception ex) {
376 // The system cannot find the file specified
377 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
378 Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
379 Assert.IsNull (ex.InnerException, "#D4");
380 Assert.IsNotNull (ex.Message, "#D5");
381 Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
385 [Test] // Start (ProcessStartInfo)
386 public void Start2_FileName_Null ()
388 ProcessStartInfo startInfo = new ProcessStartInfo ((string) null);
391 startInfo.UseShellExecute = false;
393 Process.Start (startInfo);
395 } catch (InvalidOperationException ex) {
396 // Cannot start process because a file name has
398 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
399 Assert.IsNull (ex.InnerException, "#A3");
400 Assert.IsNotNull (ex.Message, "#A4");
404 startInfo.UseShellExecute = true;
406 Process.Start (startInfo);
408 } catch (InvalidOperationException ex) {
409 // Cannot start process because a file name has
411 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
412 Assert.IsNull (ex.InnerException, "#B3");
413 Assert.IsNotNull (ex.Message, "#B4");
417 [Test] // Start (ProcessStartInfo)
418 public void Start2_FileName_Whitespace ()
420 ProcessStartInfo startInfo = new ProcessStartInfo (" ");
421 startInfo.UseShellExecute = false;
423 Process.Start (startInfo);
425 } catch (Win32Exception ex) {
426 // The system cannot find the file specified
427 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
428 Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
429 Assert.IsNull (ex.InnerException, "#4");
430 Assert.IsNotNull (ex.Message, "#5");
431 Assert.AreEqual (2, ex.NativeErrorCode, "#6");
435 [Test] // Start (ProcessStartInfo)
436 public void Start2_StartInfo_Null ()
439 Process.Start ((ProcessStartInfo) null);
441 } catch (ArgumentNullException ex) {
442 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
443 Assert.IsNull (ex.InnerException, "#3");
444 Assert.IsNotNull (ex.Message, "#4");
445 Assert.IsNotNull (ex.ParamName, "#5");
446 Assert.AreEqual ("startInfo", ex.ParamName, "#6");
450 [Test] // Start (string)
451 public void Start3_FileName_Empty ()
454 Process.Start (string.Empty);
456 } catch (InvalidOperationException ex) {
457 // Cannot start process because a file name has
459 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
460 Assert.IsNull (ex.InnerException, "#3");
461 Assert.IsNotNull (ex.Message, "#4");
465 [Test] // Start (string)
466 public void Start3_FileName_NotFound ()
469 Assert.Ignore ("On Unix and Mac OS X, we try " +
470 "to open any file (using xdg-open, ...)" +
471 " and we do not report an exception " +
474 string exe = @"c:\shouldnoteverexist.exe";
476 // absolute path, no shell
480 } catch (Win32Exception ex) {
481 // The system cannot find the file specified
482 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
483 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
484 Assert.IsNull (ex.InnerException, "#A4");
485 Assert.IsNotNull (ex.Message, "#A5");
486 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
489 // relative path, no shell
491 Process.Start ("shouldnoteverexist.exe");
493 } catch (Win32Exception ex) {
494 // The system cannot find the file specified
495 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
496 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
497 Assert.IsNull (ex.InnerException, "#B4");
498 Assert.IsNotNull (ex.Message, "#B5");
499 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
503 [Test] // Start (string)
504 public void Start3_FileName_Null ()
507 Process.Start ((string) null);
509 } catch (InvalidOperationException ex) {
510 // Cannot start process because a file name has
512 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
513 Assert.IsNull (ex.InnerException, "#3");
514 Assert.IsNotNull (ex.Message, "#4");
518 [Test] // Start (string, string)
519 public void Start4_Arguments_Null ()
522 Assert.Ignore ("On Unix and Mac OS X, we try " +
523 "to open any file (using xdg-open, ...)" +
524 " and we do not report an exception " +
527 string exe = @"c:\shouldnoteverexist.exe";
530 Process.Start ("whatever.exe", (string) null);
532 } catch (Win32Exception ex) {
533 // The system cannot find the file specified
534 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
535 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
536 Assert.IsNull (ex.InnerException, "#B4");
537 Assert.IsNotNull (ex.Message, "#B5");
538 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
542 [Test] // Start (string, string)
543 public void Start4_FileName_Empty ()
546 Process.Start (string.Empty, string.Empty);
548 } catch (InvalidOperationException ex) {
549 // Cannot start process because a file name has
551 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
552 Assert.IsNull (ex.InnerException, "#3");
553 Assert.IsNotNull (ex.Message, "#4");
557 [Test] // Start (string, string)
558 public void Start4_FileName_NotFound ()
561 Assert.Ignore ("On Unix and Mac OS X, we try " +
562 "to open any file (using xdg-open, ...)" +
563 " and we do not report an exception " +
566 string exe = @"c:\shouldnoteverexist.exe";
568 // absolute path, no shell
570 Process.Start (exe, string.Empty);
572 } catch (Win32Exception ex) {
573 // The system cannot find the file specified
574 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
575 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
576 Assert.IsNull (ex.InnerException, "#A4");
577 Assert.IsNotNull (ex.Message, "#A5");
578 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
581 // relative path, no shell
583 Process.Start ("shouldnoteverexist.exe", string.Empty);
585 } catch (Win32Exception ex) {
586 // The system cannot find the file specified
587 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
588 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
589 Assert.IsNull (ex.InnerException, "#B4");
590 Assert.IsNotNull (ex.Message, "#B5");
591 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
595 [Test] // Start (string, string)
596 public void Start4_FileName_Null ()
599 Process.Start ((string) null, string.Empty);
601 } catch (InvalidOperationException ex) {
602 // Cannot start process because a file name has
604 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
605 Assert.IsNull (ex.InnerException, "#3");
606 Assert.IsNotNull (ex.Message, "#4");
611 public void StartInfo ()
613 ProcessStartInfo startInfo = new ProcessStartInfo ();
615 Process p = new Process ();
616 Assert.IsNotNull (p.StartInfo, "#A1");
617 p.StartInfo = startInfo;
618 Assert.AreSame (startInfo, p.StartInfo, "#A2");
622 public void StartInfo_Null ()
624 Process p = new Process ();
628 } catch (ArgumentNullException ex) {
629 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
630 Assert.IsNull (ex.InnerException, "#3");
631 Assert.IsNotNull (ex.Message, "#4");
632 Assert.IsNotNull (ex.ParamName, "#5");
633 Assert.AreEqual ("value", ex.ParamName, "#6");
638 [NUnit.Framework.Category ("NotDotNet")]
639 public void TestRedirectedOutputIsAsync ()
641 // Test requires cygwin, so we just bail out for now.
642 if (Path.DirectorySeparatorChar == '\\')
645 Process p = new Process ();
646 p.StartInfo = new ProcessStartInfo ("/bin/sh", "-c \"sleep 2; echo hello\"");
647 p.StartInfo.RedirectStandardOutput = true;
648 p.StartInfo.UseShellExecute = false;
651 Stream stdout = p.StandardOutput.BaseStream;
653 byte [] buffer = new byte [200];
655 // start async Read operation
656 DateTime start = DateTime.Now;
657 IAsyncResult ar = stdout.BeginRead (buffer, 0, buffer.Length,
658 new AsyncCallback (Read), stdout);
660 Assert.IsTrue ((DateTime.Now - start).TotalMilliseconds < 1000, "#01 BeginRead was not async");
662 Assert.AreEqual (0, p.ExitCode, "#02 script failure");
665 ar.AsyncWaitHandle.WaitOne (2000, false);
666 if (bytesRead < "hello".Length)
667 Assert.Fail ("#03 got {0} bytes", bytesRead);
668 Assert.AreEqual ("hello", Encoding.Default.GetString (buffer, 0, 5), "#04");
672 void Read (IAsyncResult ar)
674 Stream stm = (Stream) ar.AsyncState;
675 bytesRead = stm.EndRead (ar);
678 static bool RunningOnUnix {
680 PlatformID platform = Environment.OSVersion.Platform;
682 return platform == PlatformID.Unix;
684 return ((int) platform) == 128;