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 Start1_FileName_Empty ()
57 Process process = new Process ();
58 process.StartInfo = new ProcessStartInfo (string.Empty);
61 process.StartInfo.UseShellExecute = false;
65 } catch (InvalidOperationException ex) {
66 // Cannot start process because a file name has
68 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
69 Assert.IsNull (ex.InnerException, "#A3");
70 Assert.IsNotNull (ex.Message, "#A4");
74 process.StartInfo.UseShellExecute = true;
78 } catch (InvalidOperationException ex) {
79 // Cannot start process because a file name has
81 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
82 Assert.IsNull (ex.InnerException, "#B3");
83 Assert.IsNotNull (ex.Message, "#B4");
88 public void Start1_FileName_InvalidPathCharacters ()
91 // on unix, all characters are allowed
94 string systemDir = Environment.GetFolderPath (Environment.SpecialFolder.System);
95 string exe = "\"" + Path.Combine (systemDir, "calc.exe") + "\"";
97 Process process = new Process ();
98 process.StartInfo = new ProcessStartInfo (exe);
101 process.StartInfo.UseShellExecute = false;
102 Assert.IsTrue (process.Start ());
106 process.StartInfo.UseShellExecute = true;
107 Assert.IsTrue (process.Start ());
112 public void Start1_FileName_NotFound ()
114 Process process = new Process ();
115 string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
116 : @"c:\shouldnoteverexist.exe";
118 // absolute path, no shell
119 process.StartInfo = new ProcessStartInfo (exe);
120 process.StartInfo.UseShellExecute = false;
124 } catch (Win32Exception ex) {
125 // The system cannot find the file specified
126 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
127 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
128 Assert.IsNull (ex.InnerException, "#A4");
129 Assert.IsNotNull (ex.Message, "#A5");
130 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
133 // relative path, no shell
134 process.StartInfo.FileName = "shouldnoteverexist.exe";
135 process.StartInfo.UseShellExecute = false;
139 } catch (Win32Exception ex) {
140 // The system cannot find the file specified
141 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
142 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
143 Assert.IsNull (ex.InnerException, "#B4");
144 Assert.IsNotNull (ex.Message, "#B5");
145 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
149 Assert.Ignore ("On Unix and Mac OS X, we try " +
150 "to open any file (using xdg-open, ...)" +
151 " and we do not report an exception " +
154 // absolute path, shell
155 process.StartInfo.FileName = exe;
156 process.StartInfo.UseShellExecute = true;
160 } catch (Win32Exception ex) {
161 // The system cannot find the file specified
162 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
163 Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
164 Assert.IsNull (ex.InnerException, "#C4");
165 Assert.IsNotNull (ex.Message, "#C5");
166 Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
169 // relative path, shell
170 process.StartInfo.FileName = "shouldnoteverexist.exe";
171 process.StartInfo.UseShellExecute = true;
175 } catch (Win32Exception ex) {
176 // The system cannot find the file specified
177 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
178 Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
179 Assert.IsNull (ex.InnerException, "#D4");
180 Assert.IsNotNull (ex.Message, "#D5");
181 Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
186 public void Start1_FileName_Null ()
188 Process process = new Process ();
189 process.StartInfo = new ProcessStartInfo ((string) null);
192 process.StartInfo.UseShellExecute = false;
196 } catch (InvalidOperationException ex) {
197 // Cannot start process because a file name has
199 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
200 Assert.IsNull (ex.InnerException, "#A3");
201 Assert.IsNotNull (ex.Message, "#A4");
205 process.StartInfo.UseShellExecute = true;
209 } catch (InvalidOperationException ex) {
210 // Cannot start process because a file name has
212 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
213 Assert.IsNull (ex.InnerException, "#B3");
214 Assert.IsNotNull (ex.Message, "#B4");
219 public void Start1_FileName_Whitespace ()
221 Process process = new Process ();
222 process.StartInfo = new ProcessStartInfo (" ");
223 process.StartInfo.UseShellExecute = false;
227 } catch (Win32Exception ex) {
228 // The system cannot find the file specified
229 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
230 Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
231 Assert.IsNull (ex.InnerException, "#4");
232 Assert.IsNotNull (ex.Message, "#5");
233 Assert.AreEqual (2, ex.NativeErrorCode, "#6");
237 [Test] // Start (ProcessStartInfo)
238 public void Start2_FileName_Empty ()
240 ProcessStartInfo startInfo = new ProcessStartInfo (string.Empty);
243 startInfo.UseShellExecute = false;
245 Process.Start (startInfo);
247 } catch (InvalidOperationException ex) {
248 // Cannot start process because a file name has
250 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
251 Assert.IsNull (ex.InnerException, "#A3");
252 Assert.IsNotNull (ex.Message, "#A4");
256 startInfo.UseShellExecute = true;
258 Process.Start (startInfo);
260 } catch (InvalidOperationException ex) {
261 // Cannot start process because a file name has
263 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
264 Assert.IsNull (ex.InnerException, "#B3");
265 Assert.IsNotNull (ex.Message, "#B4");
269 [Test] // Start (ProcessStartInfo)
270 public void Start2_FileName_NotFound ()
272 ProcessStartInfo startInfo = new ProcessStartInfo ();
273 string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
274 : @"c:\shouldnoteverexist.exe";
276 // absolute path, no shell
277 startInfo.FileName = exe;
278 startInfo.UseShellExecute = false;
280 Process.Start (startInfo);
282 } catch (Win32Exception ex) {
283 // The system cannot find the file specified
284 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
285 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
286 Assert.IsNull (ex.InnerException, "#A4");
287 Assert.IsNotNull (ex.Message, "#A5");
288 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
291 // relative path, no shell
292 startInfo.FileName = "shouldnoteverexist.exe";
293 startInfo.UseShellExecute = false;
295 Process.Start (startInfo);
297 } catch (Win32Exception ex) {
298 // The system cannot find the file specified
299 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
300 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
301 Assert.IsNull (ex.InnerException, "#B4");
302 Assert.IsNotNull (ex.Message, "#B5");
303 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
307 Assert.Ignore ("On Unix and Mac OS X, we try " +
308 "to open any file (using xdg-open, ...)" +
309 " and we do not report an exception " +
312 // absolute path, shell
313 startInfo.FileName = exe;
314 startInfo.UseShellExecute = true;
316 Process.Start (startInfo);
318 } catch (Win32Exception ex) {
319 // The system cannot find the file specified
320 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
321 Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
322 Assert.IsNull (ex.InnerException, "#C4");
323 Assert.IsNotNull (ex.Message, "#C5");
324 Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
327 // relative path, shell
328 startInfo.FileName = "shouldnoteverexist.exe";
329 startInfo.UseShellExecute = true;
331 Process.Start (startInfo);
333 } catch (Win32Exception ex) {
334 // The system cannot find the file specified
335 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
336 Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
337 Assert.IsNull (ex.InnerException, "#D4");
338 Assert.IsNotNull (ex.Message, "#D5");
339 Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
343 [Test] // Start (ProcessStartInfo)
344 public void Start2_FileName_Null ()
346 ProcessStartInfo startInfo = new ProcessStartInfo ((string) null);
349 startInfo.UseShellExecute = false;
351 Process.Start (startInfo);
353 } catch (InvalidOperationException ex) {
354 // Cannot start process because a file name has
356 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
357 Assert.IsNull (ex.InnerException, "#A3");
358 Assert.IsNotNull (ex.Message, "#A4");
362 startInfo.UseShellExecute = true;
364 Process.Start (startInfo);
366 } catch (InvalidOperationException ex) {
367 // Cannot start process because a file name has
369 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
370 Assert.IsNull (ex.InnerException, "#B3");
371 Assert.IsNotNull (ex.Message, "#B4");
375 [Test] // Start (ProcessStartInfo)
376 public void Start2_FileName_Whitespace ()
378 ProcessStartInfo startInfo = new ProcessStartInfo (" ");
379 startInfo.UseShellExecute = false;
381 Process.Start (startInfo);
383 } catch (Win32Exception ex) {
384 // The system cannot find the file specified
385 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
386 Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
387 Assert.IsNull (ex.InnerException, "#4");
388 Assert.IsNotNull (ex.Message, "#5");
389 Assert.AreEqual (2, ex.NativeErrorCode, "#6");
393 [Test] // Start (ProcessStartInfo)
394 public void Start2_StartInfo_Null ()
397 Process.Start ((ProcessStartInfo) null);
399 } catch (ArgumentNullException ex) {
400 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
401 Assert.IsNull (ex.InnerException, "#3");
402 Assert.IsNotNull (ex.Message, "#4");
403 Assert.IsNotNull (ex.ParamName, "#5");
404 Assert.AreEqual ("startInfo", ex.ParamName, "#6");
408 [Test] // Start (string)
409 public void Start3_FileName_Empty ()
412 Process.Start (string.Empty);
414 } catch (InvalidOperationException ex) {
415 // Cannot start process because a file name has
417 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
418 Assert.IsNull (ex.InnerException, "#3");
419 Assert.IsNotNull (ex.Message, "#4");
423 [Test] // Start (string)
424 public void Start3_FileName_NotFound ()
427 Assert.Ignore ("On Unix and Mac OS X, we try " +
428 "to open any file (using xdg-open, ...)" +
429 " and we do not report an exception " +
432 string exe = @"c:\shouldnoteverexist.exe";
434 // absolute path, no shell
438 } catch (Win32Exception ex) {
439 // The system cannot find the file specified
440 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
441 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
442 Assert.IsNull (ex.InnerException, "#A4");
443 Assert.IsNotNull (ex.Message, "#A5");
444 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
447 // relative path, no shell
449 Process.Start ("shouldnoteverexist.exe");
451 } catch (Win32Exception ex) {
452 // The system cannot find the file specified
453 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
454 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
455 Assert.IsNull (ex.InnerException, "#B4");
456 Assert.IsNotNull (ex.Message, "#B5");
457 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
461 [Test] // Start (string)
462 public void Start3_FileName_Null ()
465 Process.Start ((string) null);
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, string)
477 public void Start4_Arguments_Null ()
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";
488 Process.Start ("whatever.exe", (string) null);
490 } catch (Win32Exception ex) {
491 // The system cannot find the file specified
492 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
493 Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
494 Assert.IsNull (ex.InnerException, "#B4");
495 Assert.IsNotNull (ex.Message, "#B5");
496 Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
500 [Test] // Start (string, string)
501 public void Start4_FileName_Empty ()
504 Process.Start (string.Empty, string.Empty);
506 } catch (InvalidOperationException ex) {
507 // Cannot start process because a file name has
509 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
510 Assert.IsNull (ex.InnerException, "#3");
511 Assert.IsNotNull (ex.Message, "#4");
515 [Test] // Start (string, string)
516 public void Start4_FileName_NotFound ()
519 Assert.Ignore ("On Unix and Mac OS X, we try " +
520 "to open any file (using xdg-open, ...)" +
521 " and we do not report an exception " +
524 string exe = @"c:\shouldnoteverexist.exe";
526 // absolute path, no shell
528 Process.Start (exe, string.Empty);
530 } catch (Win32Exception ex) {
531 // The system cannot find the file specified
532 Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
533 Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
534 Assert.IsNull (ex.InnerException, "#A4");
535 Assert.IsNotNull (ex.Message, "#A5");
536 Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
539 // relative path, no shell
541 Process.Start ("shouldnoteverexist.exe", string.Empty);
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_Null ()
557 Process.Start ((string) null, 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");
569 public void StartInfo ()
571 ProcessStartInfo startInfo = new ProcessStartInfo ();
573 Process p = new Process ();
574 Assert.IsNotNull (p.StartInfo, "#A1");
575 p.StartInfo = startInfo;
576 Assert.AreSame (startInfo, p.StartInfo, "#A2");
580 public void StartInfo_Null ()
582 Process p = new Process ();
586 } catch (ArgumentNullException ex) {
587 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
588 Assert.IsNull (ex.InnerException, "#3");
589 Assert.IsNotNull (ex.Message, "#4");
590 Assert.IsNotNull (ex.ParamName, "#5");
591 Assert.AreEqual ("value", ex.ParamName, "#6");
596 [NUnit.Framework.Category ("NotDotNet")]
597 public void TestRedirectedOutputIsAsync ()
599 // Test requires cygwin, so we just bail out for now.
600 if (Path.DirectorySeparatorChar == '\\')
603 Process p = new Process ();
604 p.StartInfo = new ProcessStartInfo ("/bin/sh", "-c \"sleep 2; echo hello\"");
605 p.StartInfo.RedirectStandardOutput = true;
606 p.StartInfo.UseShellExecute = false;
609 Stream stdout = p.StandardOutput.BaseStream;
611 byte [] buffer = new byte [200];
613 // start async Read operation
614 DateTime start = DateTime.Now;
615 IAsyncResult ar = stdout.BeginRead (buffer, 0, buffer.Length,
616 new AsyncCallback (Read), stdout);
618 Assert.IsTrue ((DateTime.Now - start).TotalMilliseconds < 1000, "#01 BeginRead was not async");
620 Assert.AreEqual (0, p.ExitCode, "#02 script failure");
623 ar.AsyncWaitHandle.WaitOne (2000, false);
624 if (bytesRead < "hello".Length)
625 Assert.Fail ("#03 got {0} bytes", bytesRead);
626 Assert.AreEqual ("hello", Encoding.Default.GetString (buffer, 0, 5), "#04");
630 void Read (IAsyncResult ar)
632 Stream stm = (Stream) ar.AsyncState;
633 bytesRead = stm.EndRead (ar);
636 static bool RunningOnUnix {
638 PlatformID platform = Environment.OSVersion.Platform;
640 return platform == PlatformID.Unix;
642 return ((int) platform) == 128;