A bug fix for 459450
[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.ComponentModel;
13 using System.Diagnostics;
14 using System.IO;
15 using System.Text;
16
17 using NUnit.Framework;
18
19 namespace MonoTests.System.Diagnostics
20 {
21         [TestFixture]
22         public class ProcessTest
23         {
24                 [Test]
25                 public void GetProcessById_MachineName_Null ()
26                 {
27                         try {
28                                 Process.GetProcessById (1, (string) null);
29                                 Assert.Fail ("#1");
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");
36                         }
37                 }
38
39                 [Test]
40                 public void GetProcesses_MachineName_Null ()
41                 {
42                         try {
43                                 Process.GetProcesses ((string) null);
44                                 Assert.Fail ("#1");
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");
51                         }
52                 }
53
54                 [Test]
55                 public void PriorityClass_NotStarted ()
56                 {
57                         Process process = new Process ();
58                         try {
59                                 process.PriorityClass = ProcessPriorityClass.Normal;
60                                 Assert.Fail ("#A1");
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");
66                         }
67
68                         try {
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");
75                         }
76                 }
77
78                 [Test]
79                 public void PriorityClass_Invalid ()
80                 {
81                         Process process = new Process ();
82                         try {
83                                 process.PriorityClass = (ProcessPriorityClass) 666;
84                                 Assert.Fail ("#1");
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");
93                         }
94                 }
95
96                 [Test] // Start ()
97                 public void Start1_FileName_Empty ()
98                 {
99                         Process process = new Process ();
100                         process.StartInfo = new ProcessStartInfo (string.Empty);
101
102                         // no shell
103                         process.StartInfo.UseShellExecute = false;
104                         try {
105                                 process.Start ();
106                                 Assert.Fail ("#A1");
107                         } catch (InvalidOperationException ex) {
108                                 // Cannot start process because a file name has
109                                 // not been provided
110                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
111                                 Assert.IsNull (ex.InnerException, "#A3");
112                                 Assert.IsNotNull (ex.Message, "#A4");
113                         }
114
115                         // shell
116                         process.StartInfo.UseShellExecute = true;
117                         try {
118                                 process.Start ();
119                                 Assert.Fail ("#B1");
120                         } catch (InvalidOperationException ex) {
121                                 // Cannot start process because a file name has
122                                 // not been provided
123                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
124                                 Assert.IsNull (ex.InnerException, "#B3");
125                                 Assert.IsNotNull (ex.Message, "#B4");
126                         }
127                 }
128
129                 [Test] // Start ()
130                 public void Start1_FileName_InvalidPathCharacters ()
131                 {
132                         if (RunningOnUnix)
133                                 // on unix, all characters are allowed
134                                 return;
135
136                         string systemDir = Environment.GetFolderPath (Environment.SpecialFolder.System);
137                         string exe = "\"" + Path.Combine (systemDir, "calc.exe") + "\"";
138
139                         Process process = new Process ();
140                         process.StartInfo = new ProcessStartInfo (exe);
141
142                         // no shell
143                         process.StartInfo.UseShellExecute = false;
144                         Assert.IsTrue (process.Start ());
145                         process.Kill ();
146
147                         // shell
148                         process.StartInfo.UseShellExecute = true;
149                         Assert.IsTrue (process.Start ());
150                         process.Kill ();
151                 }
152
153                 [Test] // Start ()
154                 public void Start1_FileName_NotFound ()
155                 {
156                         Process process = new Process ();
157                         string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
158                                 : @"c:\shouldnoteverexist.exe";
159
160                         // absolute path, no shell
161                         process.StartInfo = new ProcessStartInfo (exe);
162                         process.StartInfo.UseShellExecute = false;
163                         try {
164                                 process.Start ();
165                                 Assert.Fail ("#A1");
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");
173                         }
174
175                         // relative path, no shell
176                         process.StartInfo.FileName = "shouldnoteverexist.exe";
177                         process.StartInfo.UseShellExecute = false;
178                         try {
179                                 process.Start ();
180                                 Assert.Fail ("#B1");
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");
188                         }
189
190                         if (RunningOnUnix)
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 " +
194                                         "if this fails.");
195
196                         // absolute path, shell
197                         process.StartInfo.FileName = exe;
198                         process.StartInfo.UseShellExecute = true;
199                         try {
200                                 process.Start ();
201                                 Assert.Fail ("#C1");
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");
209                         }
210
211                         // relative path, shell
212                         process.StartInfo.FileName = "shouldnoteverexist.exe";
213                         process.StartInfo.UseShellExecute = true;
214                         try {
215                                 process.Start ();
216                                 Assert.Fail ("#D1");
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");
224                         }
225                 }
226
227                 [Test] // Start ()
228                 public void Start1_FileName_Null ()
229                 {
230                         Process process = new Process ();
231                         process.StartInfo = new ProcessStartInfo ((string) null);
232
233                         // no shell
234                         process.StartInfo.UseShellExecute = false;
235                         try {
236                                 process.Start ();
237                                 Assert.Fail ("#A1");
238                         } catch (InvalidOperationException ex) {
239                                 // Cannot start process because a file name has
240                                 // not been provided
241                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
242                                 Assert.IsNull (ex.InnerException, "#A3");
243                                 Assert.IsNotNull (ex.Message, "#A4");
244                         }
245
246                         // shell
247                         process.StartInfo.UseShellExecute = true;
248                         try {
249                                 process.Start ();
250                                 Assert.Fail ("#B1");
251                         } catch (InvalidOperationException ex) {
252                                 // Cannot start process because a file name has
253                                 // not been provided
254                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
255                                 Assert.IsNull (ex.InnerException, "#B3");
256                                 Assert.IsNotNull (ex.Message, "#B4");
257                         }
258                 }
259
260                 [Test] // Start ()
261                 public void Start1_FileName_Whitespace ()
262                 {
263                         Process process = new Process ();
264                         process.StartInfo = new ProcessStartInfo (" ");
265                         process.StartInfo.UseShellExecute = false;
266                         try {
267                                 process.Start ();
268                                 Assert.Fail ("#1");
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");
276                         }
277                 }
278
279                 [Test] // Start (ProcessStartInfo)
280                 public void Start2_FileName_Empty ()
281                 {
282                         ProcessStartInfo startInfo = new ProcessStartInfo (string.Empty);
283
284                         // no shell
285                         startInfo.UseShellExecute = false;
286                         try {
287                                 Process.Start (startInfo);
288                                 Assert.Fail ("#A1");
289                         } catch (InvalidOperationException ex) {
290                                 // Cannot start process because a file name has
291                                 // not been provided
292                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
293                                 Assert.IsNull (ex.InnerException, "#A3");
294                                 Assert.IsNotNull (ex.Message, "#A4");
295                         }
296
297                         // shell
298                         startInfo.UseShellExecute = true;
299                         try {
300                                 Process.Start (startInfo);
301                                 Assert.Fail ("#B1");
302                         } catch (InvalidOperationException ex) {
303                                 // Cannot start process because a file name has
304                                 // not been provided
305                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
306                                 Assert.IsNull (ex.InnerException, "#B3");
307                                 Assert.IsNotNull (ex.Message, "#B4");
308                         }
309                 }
310
311                 [Test] // Start (ProcessStartInfo)
312                 public void Start2_FileName_NotFound ()
313                 {
314                         ProcessStartInfo startInfo = new ProcessStartInfo ();
315                         string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
316                                 : @"c:\shouldnoteverexist.exe";
317
318                         // absolute path, no shell
319                         startInfo.FileName = exe;
320                         startInfo.UseShellExecute = false;
321                         try {
322                                 Process.Start (startInfo);
323                                 Assert.Fail ("#A1");
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");
331                         }
332
333                         // relative path, no shell
334                         startInfo.FileName = "shouldnoteverexist.exe";
335                         startInfo.UseShellExecute = false;
336                         try {
337                                 Process.Start (startInfo);
338                                 Assert.Fail ("#B1");
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");
346                         }
347
348                         if (RunningOnUnix)
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 " +
352                                         "if this fails.");
353
354                         // absolute path, shell
355                         startInfo.FileName = exe;
356                         startInfo.UseShellExecute = true;
357                         try {
358                                 Process.Start (startInfo);
359                                 Assert.Fail ("#C1");
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");
367                         }
368
369                         // relative path, shell
370                         startInfo.FileName = "shouldnoteverexist.exe";
371                         startInfo.UseShellExecute = true;
372                         try {
373                                 Process.Start (startInfo);
374                                 Assert.Fail ("#D1");
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");
382                         }
383                 }
384
385                 [Test] // Start (ProcessStartInfo)
386                 public void Start2_FileName_Null ()
387                 {
388                         ProcessStartInfo startInfo = new ProcessStartInfo ((string) null);
389
390                         // no shell
391                         startInfo.UseShellExecute = false;
392                         try {
393                                 Process.Start (startInfo);
394                                 Assert.Fail ("#A1");
395                         } catch (InvalidOperationException ex) {
396                                 // Cannot start process because a file name has
397                                 // not been provided
398                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
399                                 Assert.IsNull (ex.InnerException, "#A3");
400                                 Assert.IsNotNull (ex.Message, "#A4");
401                         }
402
403                         // shell
404                         startInfo.UseShellExecute = true;
405                         try {
406                                 Process.Start (startInfo);
407                                 Assert.Fail ("#B1");
408                         } catch (InvalidOperationException ex) {
409                                 // Cannot start process because a file name has
410                                 // not been provided
411                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
412                                 Assert.IsNull (ex.InnerException, "#B3");
413                                 Assert.IsNotNull (ex.Message, "#B4");
414                         }
415                 }
416
417                 [Test] // Start (ProcessStartInfo)
418                 public void Start2_FileName_Whitespace ()
419                 {
420                         ProcessStartInfo startInfo = new ProcessStartInfo (" ");
421                         startInfo.UseShellExecute = false;
422                         try {
423                                 Process.Start (startInfo);
424                                 Assert.Fail ("#1");
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");
432                         }
433                 }
434
435                 [Test] // Start (ProcessStartInfo)
436                 public void Start2_StartInfo_Null ()
437                 {
438                         try {
439                                 Process.Start ((ProcessStartInfo) null);
440                                 Assert.Fail ("#1");
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");
447                         }
448                 }
449
450                 [Test] // Start (string)
451                 public void Start3_FileName_Empty ()
452                 {
453                         try {
454                                 Process.Start (string.Empty);
455                                 Assert.Fail ("#1");
456                         } catch (InvalidOperationException ex) {
457                                 // Cannot start process because a file name has
458                                 // not been provided
459                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
460                                 Assert.IsNull (ex.InnerException, "#3");
461                                 Assert.IsNotNull (ex.Message, "#4");
462                         }
463                 }
464
465                 [Test] // Start (string)
466                 public void Start3_FileName_NotFound ()
467                 {
468                         if (RunningOnUnix)
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 " +
472                                         "if this fails.");
473
474                         string exe = @"c:\shouldnoteverexist.exe";
475
476                         // absolute path, no shell
477                         try {
478                                 Process.Start (exe);
479                                 Assert.Fail ("#A1");
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");
487                         }
488
489                         // relative path, no shell
490                         try {
491                                 Process.Start ("shouldnoteverexist.exe");
492                                 Assert.Fail ("#B1");
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");
500                         }
501                 }
502
503                 [Test] // Start (string)
504                 public void Start3_FileName_Null ()
505                 {
506                         try {
507                                 Process.Start ((string) null);
508                                 Assert.Fail ("#1");
509                         } catch (InvalidOperationException ex) {
510                                 // Cannot start process because a file name has
511                                 // not been provided
512                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
513                                 Assert.IsNull (ex.InnerException, "#3");
514                                 Assert.IsNotNull (ex.Message, "#4");
515                         }
516                 }
517
518                 [Test] // Start (string, string)
519                 public void Start4_Arguments_Null ()
520                 {
521                         if (RunningOnUnix)
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 " +
525                                         "if this fails.");
526
527                         string exe = @"c:\shouldnoteverexist.exe";
528
529                         try {
530                                 Process.Start ("whatever.exe", (string) null);
531                                 Assert.Fail ("#1");
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");
539                         }
540                 }
541
542                 [Test] // Start (string, string)
543                 public void Start4_FileName_Empty ()
544                 {
545                         try {
546                                 Process.Start (string.Empty, string.Empty);
547                                 Assert.Fail ("#1");
548                         } catch (InvalidOperationException ex) {
549                                 // Cannot start process because a file name has
550                                 // not been provided
551                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
552                                 Assert.IsNull (ex.InnerException, "#3");
553                                 Assert.IsNotNull (ex.Message, "#4");
554                         }
555                 }
556
557                 [Test] // Start (string, string)
558                 public void Start4_FileName_NotFound ()
559                 {
560                         if (RunningOnUnix)
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 " +
564                                         "if this fails.");
565
566                         string exe = @"c:\shouldnoteverexist.exe";
567
568                         // absolute path, no shell
569                         try {
570                                 Process.Start (exe, string.Empty);
571                                 Assert.Fail ("#A1");
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");
579                         }
580
581                         // relative path, no shell
582                         try {
583                                 Process.Start ("shouldnoteverexist.exe", string.Empty);
584                                 Assert.Fail ("#B1");
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");
592                         }
593                 }
594
595 #if NET_2_0             
596                 [Test]
597                 public void Start_UseShellExecuteWithEmptyUserName ()
598                 {
599                         if (RunningOnUnix)
600                                 Assert.Ignore ("On Unix and Mac OS X, we try " +
601                                         "to open any file (using xdg-open, ...)" +
602                                         " and we do not report an exception " +
603                                         "if this fails.");
604
605                         string exe = @"c:\shouldnoteverexist.exe";
606
607                         try {
608                                 Process p = new Process ();
609                                 p.StartInfo.FileName = exe;
610                                 p.StartInfo.UseShellExecute = true;
611                                 p.StartInfo.UserName = "";
612                                 p.Start ();
613                                 Assert.Fail ("#1");
614                         } catch (InvalidOperationException) {
615                                 Assert.Fail ("#2");
616                         } catch (Win32Exception) {
617                         }
618
619                         try {
620                                 Process p = new Process ();
621                                 p.StartInfo.FileName = exe;
622                                 p.StartInfo.UseShellExecute = true;
623                                 p.StartInfo.UserName = null;
624                                 p.Start ();
625                                 Assert.Fail ("#3");                             
626                         } catch (InvalidOperationException) {
627                                 Assert.Fail ("#4");
628                         } catch (Win32Exception) {
629                         }
630                 }
631 #endif
632                 
633                 [Test] // Start (string, string)
634                 public void Start4_FileName_Null ()
635                 {
636                         try {
637                                 Process.Start ((string) null, string.Empty);
638                                 Assert.Fail ("#1");
639                         } catch (InvalidOperationException ex) {
640                                 // Cannot start process because a file name has
641                                 // not been provided
642                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
643                                 Assert.IsNull (ex.InnerException, "#3");
644                                 Assert.IsNotNull (ex.Message, "#4");
645                         }
646                 }
647
648                 [Test]
649                 public void StartInfo ()
650                 {
651                         ProcessStartInfo startInfo = new ProcessStartInfo ();
652
653                         Process p = new Process ();
654                         Assert.IsNotNull (p.StartInfo, "#A1");
655                         p.StartInfo = startInfo;
656                         Assert.AreSame (startInfo, p.StartInfo, "#A2");
657                 }
658
659                 [Test]
660                 public void StartInfo_Null ()
661                 {
662                         Process p = new Process ();
663                         try {
664                                 p.StartInfo = null;
665                                 Assert.Fail ("#1");
666                         } catch (ArgumentNullException ex) {
667                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
668                                 Assert.IsNull (ex.InnerException, "#3");
669                                 Assert.IsNotNull (ex.Message, "#4");
670                                 Assert.IsNotNull (ex.ParamName, "#5");
671                                 Assert.AreEqual ("value", ex.ParamName, "#6");
672                         }
673                 }
674
675                 [Test]
676                 [NUnit.Framework.Category ("NotDotNet")]
677                 public void TestRedirectedOutputIsAsync ()
678                 {
679                         // Test requires cygwin, so we just bail out for now.
680                         if (Path.DirectorySeparatorChar == '\\')
681                                 return;
682                         
683                         Process p = new Process ();
684                         p.StartInfo = new ProcessStartInfo ("/bin/sh", "-c \"sleep 2; echo hello\"");
685                         p.StartInfo.RedirectStandardOutput = true;
686                         p.StartInfo.UseShellExecute = false;
687                         p.Start ();
688
689                         Stream stdout = p.StandardOutput.BaseStream;
690
691                         byte [] buffer = new byte [200];
692
693                         // start async Read operation
694                         DateTime start = DateTime.Now;
695                         IAsyncResult ar = stdout.BeginRead (buffer, 0, buffer.Length,
696                                                             new AsyncCallback (Read), stdout);
697
698                         Assert.IsTrue ((DateTime.Now - start).TotalMilliseconds < 1000, "#01 BeginRead was not async");
699                         p.WaitForExit ();
700                         Assert.AreEqual (0, p.ExitCode, "#02 script failure");
701
702                         /*
703                         ar.AsyncWaitHandle.WaitOne (2000, false);
704                         if (bytesRead < "hello".Length)
705                                 Assert.Fail ("#03 got {0} bytes", bytesRead);
706                         Assert.AreEqual ("hello", Encoding.Default.GetString (buffer, 0, 5), "#04");
707                         */
708                 }
709
710                 void Read (IAsyncResult ar)
711                 {
712                         Stream stm = (Stream) ar.AsyncState;
713                         bytesRead = stm.EndRead (ar);
714                 }
715
716                 static bool RunningOnUnix {
717                         get {
718                                 int p = (int)Environment.OSVersion.Platform;
719                                 return ((p == 128) || (p == 4) || (p == 6));
720                         }
721                 }
722
723                 int bytesRead = -1;
724
725 #if NET_2_0
726 // Not technically a 2.0 only test, but I use lambdas, so I need gmcs
727
728                 [Test]
729                 // This was for bug #459450
730                 public void TestEventRaising ()
731                 {
732                         EventWaitHandle errorClosed = new ManualResetEvent(false);
733                         EventWaitHandle outClosed = new ManualResetEvent(false);
734                         EventWaitHandle exited = new ManualResetEvent(false);
735
736                         Process p = new Process();
737                         p.StartInfo.UseShellExecute = false;
738                         p.StartInfo.RedirectStandardOutput = true;
739                         p.StartInfo.RedirectStandardError = true;
740                         p.StartInfo.RedirectStandardInput = false;
741                         p.OutputDataReceived += (object sender, DataReceivedEventArgs e) => {
742                                 if (e.Data == null) {
743                                         outClosed.Set();
744                                 }
745                         };
746                         
747                         p.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => {
748                                 if (e.Data == null) {
749                                         errorClosed.Set();
750                                 }
751                         };
752                         
753                         p.Exited += (object sender, EventArgs e) => {
754                                 exited.Set ();
755                         };
756                         
757                         p.EnableRaisingEvents = true;
758
759                         if (RunningOnUnix){
760                                 p.StartInfo.FileName = "/bin/ls";
761                                 p.StartInfo.Arguments = "/";
762                         } else {
763                                 p.StartInfo.FileName = "help";
764                                 p.StartInfo.Arguments = "";
765                         }
766
767                         p.Start();
768
769                         p.BeginErrorReadLine();
770                         p.BeginOutputReadLine();
771
772                         Console.WriteLine("started, waiting for handles");
773                         bool r = WaitHandle.WaitAll(new WaitHandle[] { errorClosed, outClosed, exited }, 10000, false);
774
775                         Assert.AreEqual (true, r, "Null Argument Events Raised");
776                 }
777 #endif
778         }
779 }