ProcessTest.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. //
  2. // ProcessTest.cs - NUnit Test Cases for System.Diagnostics.Process
  3. //
  4. // Authors:
  5. // Gert Driesen ([email protected])
  6. // Robert Jordan <[email protected]>
  7. //
  8. // (C) 2007 Gert Driesen
  9. //
  10. using System;
  11. using System.ComponentModel;
  12. using System.Diagnostics;
  13. using System.IO;
  14. using System.Text;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Diagnostics
  17. {
  18. [TestFixture]
  19. public class ProcessTest
  20. {
  21. [Test]
  22. public void GetProcessById_MachineName_Null ()
  23. {
  24. try {
  25. Process.GetProcessById (1, (string) null);
  26. Assert.Fail ("#1");
  27. } catch (ArgumentNullException ex) {
  28. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  29. Assert.IsNotNull (ex.Message, "#3");
  30. Assert.IsNotNull (ex.ParamName, "#4");
  31. Assert.AreEqual ("machineName", ex.ParamName, "#5");
  32. Assert.IsNull (ex.InnerException, "#6");
  33. }
  34. }
  35. [Test]
  36. public void GetProcesses_MachineName_Null ()
  37. {
  38. try {
  39. Process.GetProcesses ((string) null);
  40. Assert.Fail ("#1");
  41. } catch (ArgumentNullException ex) {
  42. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  43. Assert.IsNotNull (ex.Message, "#3");
  44. Assert.IsNotNull (ex.ParamName, "#4");
  45. Assert.AreEqual ("machineName", ex.ParamName, "#5");
  46. Assert.IsNull (ex.InnerException, "#6");
  47. }
  48. }
  49. [Test]
  50. public void PriorityClass_NotStarted ()
  51. {
  52. Process process = new Process ();
  53. try {
  54. process.PriorityClass = ProcessPriorityClass.Normal;
  55. Assert.Fail ("#A1");
  56. } catch (InvalidOperationException ex) {
  57. // No process is associated with this object
  58. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  59. Assert.IsNull (ex.InnerException, "#A3");
  60. Assert.IsNotNull (ex.Message, "#A4");
  61. }
  62. try {
  63. Assert.Fail ("#B1:" + process.PriorityClass);
  64. } catch (InvalidOperationException ex) {
  65. // No process is associated with this object
  66. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  67. Assert.IsNull (ex.InnerException, "#B3");
  68. Assert.IsNotNull (ex.Message, "#B4");
  69. }
  70. }
  71. [Test]
  72. public void PriorityClass_Invalid ()
  73. {
  74. Process process = new Process ();
  75. try {
  76. process.PriorityClass = (ProcessPriorityClass) 666;
  77. Assert.Fail ("#1");
  78. } catch (InvalidEnumArgumentException ex) {
  79. Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
  80. Assert.IsNull (ex.InnerException, "#3");
  81. Assert.IsNotNull (ex.Message, "#4");
  82. Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#5");
  83. Assert.IsTrue (ex.Message.IndexOf (typeof (ProcessPriorityClass).Name) != -1, "#6");
  84. Assert.IsNotNull (ex.ParamName, "#7");
  85. Assert.AreEqual ("value", ex.ParamName, "#8");
  86. }
  87. }
  88. [Test] // Start ()
  89. public void Start1_FileName_Empty ()
  90. {
  91. Process process = new Process ();
  92. process.StartInfo = new ProcessStartInfo (string.Empty);
  93. // no shell
  94. process.StartInfo.UseShellExecute = false;
  95. try {
  96. process.Start ();
  97. Assert.Fail ("#A1");
  98. } catch (InvalidOperationException ex) {
  99. // Cannot start process because a file name has
  100. // not been provided
  101. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  102. Assert.IsNull (ex.InnerException, "#A3");
  103. Assert.IsNotNull (ex.Message, "#A4");
  104. }
  105. // shell
  106. process.StartInfo.UseShellExecute = true;
  107. try {
  108. process.Start ();
  109. Assert.Fail ("#B1");
  110. } catch (InvalidOperationException ex) {
  111. // Cannot start process because a file name has
  112. // not been provided
  113. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  114. Assert.IsNull (ex.InnerException, "#B3");
  115. Assert.IsNotNull (ex.Message, "#B4");
  116. }
  117. }
  118. [Test] // Start ()
  119. public void Start1_FileName_InvalidPathCharacters ()
  120. {
  121. if (RunningOnUnix)
  122. // on unix, all characters are allowed
  123. return;
  124. string systemDir = Environment.GetFolderPath (Environment.SpecialFolder.System);
  125. string exe = "\"" + Path.Combine (systemDir, "calc.exe") + "\"";
  126. Process process = new Process ();
  127. process.StartInfo = new ProcessStartInfo (exe);
  128. // no shell
  129. process.StartInfo.UseShellExecute = false;
  130. Assert.IsTrue (process.Start ());
  131. process.Kill ();
  132. // shell
  133. process.StartInfo.UseShellExecute = true;
  134. Assert.IsTrue (process.Start ());
  135. process.Kill ();
  136. }
  137. [Test] // Start ()
  138. public void Start1_FileName_NotFound ()
  139. {
  140. Process process = new Process ();
  141. string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
  142. : @"c:\shouldnoteverexist.exe";
  143. // absolute path, no shell
  144. process.StartInfo = new ProcessStartInfo (exe);
  145. process.StartInfo.UseShellExecute = false;
  146. try {
  147. process.Start ();
  148. Assert.Fail ("#A1");
  149. } catch (Win32Exception ex) {
  150. // The system cannot find the file specified
  151. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
  152. Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
  153. Assert.IsNull (ex.InnerException, "#A4");
  154. Assert.IsNotNull (ex.Message, "#A5");
  155. Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
  156. }
  157. // relative path, no shell
  158. process.StartInfo.FileName = "shouldnoteverexist.exe";
  159. process.StartInfo.UseShellExecute = false;
  160. try {
  161. process.Start ();
  162. Assert.Fail ("#B1");
  163. } catch (Win32Exception ex) {
  164. // The system cannot find the file specified
  165. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
  166. Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
  167. Assert.IsNull (ex.InnerException, "#B4");
  168. Assert.IsNotNull (ex.Message, "#B5");
  169. Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
  170. }
  171. if (RunningOnUnix)
  172. Assert.Ignore ("On Unix and Mac OS X, we try " +
  173. "to open any file (using xdg-open, ...)" +
  174. " and we do not report an exception " +
  175. "if this fails.");
  176. // absolute path, shell
  177. process.StartInfo.FileName = exe;
  178. process.StartInfo.UseShellExecute = true;
  179. try {
  180. process.Start ();
  181. Assert.Fail ("#C1");
  182. } catch (Win32Exception ex) {
  183. // The system cannot find the file specified
  184. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
  185. Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
  186. Assert.IsNull (ex.InnerException, "#C4");
  187. Assert.IsNotNull (ex.Message, "#C5");
  188. Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
  189. }
  190. // relative path, shell
  191. process.StartInfo.FileName = "shouldnoteverexist.exe";
  192. process.StartInfo.UseShellExecute = true;
  193. try {
  194. process.Start ();
  195. Assert.Fail ("#D1");
  196. } catch (Win32Exception ex) {
  197. // The system cannot find the file specified
  198. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
  199. Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
  200. Assert.IsNull (ex.InnerException, "#D4");
  201. Assert.IsNotNull (ex.Message, "#D5");
  202. Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
  203. }
  204. }
  205. [Test] // Start ()
  206. public void Start1_FileName_Null ()
  207. {
  208. Process process = new Process ();
  209. process.StartInfo = new ProcessStartInfo ((string) null);
  210. // no shell
  211. process.StartInfo.UseShellExecute = false;
  212. try {
  213. process.Start ();
  214. Assert.Fail ("#A1");
  215. } catch (InvalidOperationException ex) {
  216. // Cannot start process because a file name has
  217. // not been provided
  218. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  219. Assert.IsNull (ex.InnerException, "#A3");
  220. Assert.IsNotNull (ex.Message, "#A4");
  221. }
  222. // shell
  223. process.StartInfo.UseShellExecute = true;
  224. try {
  225. process.Start ();
  226. Assert.Fail ("#B1");
  227. } catch (InvalidOperationException ex) {
  228. // Cannot start process because a file name has
  229. // not been provided
  230. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  231. Assert.IsNull (ex.InnerException, "#B3");
  232. Assert.IsNotNull (ex.Message, "#B4");
  233. }
  234. }
  235. [Test] // Start ()
  236. public void Start1_FileName_Whitespace ()
  237. {
  238. Process process = new Process ();
  239. process.StartInfo = new ProcessStartInfo (" ");
  240. process.StartInfo.UseShellExecute = false;
  241. try {
  242. process.Start ();
  243. Assert.Fail ("#1");
  244. } catch (Win32Exception ex) {
  245. // The system cannot find the file specified
  246. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
  247. Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
  248. Assert.IsNull (ex.InnerException, "#4");
  249. Assert.IsNotNull (ex.Message, "#5");
  250. Assert.AreEqual (2, ex.NativeErrorCode, "#6");
  251. }
  252. }
  253. [Test] // Start (ProcessStartInfo)
  254. public void Start2_FileName_Empty ()
  255. {
  256. ProcessStartInfo startInfo = new ProcessStartInfo (string.Empty);
  257. // no shell
  258. startInfo.UseShellExecute = false;
  259. try {
  260. Process.Start (startInfo);
  261. Assert.Fail ("#A1");
  262. } catch (InvalidOperationException ex) {
  263. // Cannot start process because a file name has
  264. // not been provided
  265. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  266. Assert.IsNull (ex.InnerException, "#A3");
  267. Assert.IsNotNull (ex.Message, "#A4");
  268. }
  269. // shell
  270. startInfo.UseShellExecute = true;
  271. try {
  272. Process.Start (startInfo);
  273. Assert.Fail ("#B1");
  274. } catch (InvalidOperationException ex) {
  275. // Cannot start process because a file name has
  276. // not been provided
  277. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  278. Assert.IsNull (ex.InnerException, "#B3");
  279. Assert.IsNotNull (ex.Message, "#B4");
  280. }
  281. }
  282. [Test] // Start (ProcessStartInfo)
  283. public void Start2_FileName_NotFound ()
  284. {
  285. ProcessStartInfo startInfo = new ProcessStartInfo ();
  286. string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
  287. : @"c:\shouldnoteverexist.exe";
  288. // absolute path, no shell
  289. startInfo.FileName = exe;
  290. startInfo.UseShellExecute = false;
  291. try {
  292. Process.Start (startInfo);
  293. Assert.Fail ("#A1");
  294. } catch (Win32Exception ex) {
  295. // The system cannot find the file specified
  296. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
  297. Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
  298. Assert.IsNull (ex.InnerException, "#A4");
  299. Assert.IsNotNull (ex.Message, "#A5");
  300. Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
  301. }
  302. // relative path, no shell
  303. startInfo.FileName = "shouldnoteverexist.exe";
  304. startInfo.UseShellExecute = false;
  305. try {
  306. Process.Start (startInfo);
  307. Assert.Fail ("#B1");
  308. } catch (Win32Exception ex) {
  309. // The system cannot find the file specified
  310. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
  311. Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
  312. Assert.IsNull (ex.InnerException, "#B4");
  313. Assert.IsNotNull (ex.Message, "#B5");
  314. Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
  315. }
  316. if (RunningOnUnix)
  317. Assert.Ignore ("On Unix and Mac OS X, we try " +
  318. "to open any file (using xdg-open, ...)" +
  319. " and we do not report an exception " +
  320. "if this fails.");
  321. // absolute path, shell
  322. startInfo.FileName = exe;
  323. startInfo.UseShellExecute = true;
  324. try {
  325. Process.Start (startInfo);
  326. Assert.Fail ("#C1");
  327. } catch (Win32Exception ex) {
  328. // The system cannot find the file specified
  329. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
  330. Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
  331. Assert.IsNull (ex.InnerException, "#C4");
  332. Assert.IsNotNull (ex.Message, "#C5");
  333. Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
  334. }
  335. // relative path, shell
  336. startInfo.FileName = "shouldnoteverexist.exe";
  337. startInfo.UseShellExecute = true;
  338. try {
  339. Process.Start (startInfo);
  340. Assert.Fail ("#D1");
  341. } catch (Win32Exception ex) {
  342. // The system cannot find the file specified
  343. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
  344. Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
  345. Assert.IsNull (ex.InnerException, "#D4");
  346. Assert.IsNotNull (ex.Message, "#D5");
  347. Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
  348. }
  349. }
  350. [Test] // Start (ProcessStartInfo)
  351. public void Start2_FileName_Null ()
  352. {
  353. ProcessStartInfo startInfo = new ProcessStartInfo ((string) null);
  354. // no shell
  355. startInfo.UseShellExecute = false;
  356. try {
  357. Process.Start (startInfo);
  358. Assert.Fail ("#A1");
  359. } catch (InvalidOperationException ex) {
  360. // Cannot start process because a file name has
  361. // not been provided
  362. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  363. Assert.IsNull (ex.InnerException, "#A3");
  364. Assert.IsNotNull (ex.Message, "#A4");
  365. }
  366. // shell
  367. startInfo.UseShellExecute = true;
  368. try {
  369. Process.Start (startInfo);
  370. Assert.Fail ("#B1");
  371. } catch (InvalidOperationException ex) {
  372. // Cannot start process because a file name has
  373. // not been provided
  374. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  375. Assert.IsNull (ex.InnerException, "#B3");
  376. Assert.IsNotNull (ex.Message, "#B4");
  377. }
  378. }
  379. [Test] // Start (ProcessStartInfo)
  380. public void Start2_FileName_Whitespace ()
  381. {
  382. ProcessStartInfo startInfo = new ProcessStartInfo (" ");
  383. startInfo.UseShellExecute = false;
  384. try {
  385. Process.Start (startInfo);
  386. Assert.Fail ("#1");
  387. } catch (Win32Exception ex) {
  388. // The system cannot find the file specified
  389. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
  390. Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
  391. Assert.IsNull (ex.InnerException, "#4");
  392. Assert.IsNotNull (ex.Message, "#5");
  393. Assert.AreEqual (2, ex.NativeErrorCode, "#6");
  394. }
  395. }
  396. [Test] // Start (ProcessStartInfo)
  397. public void Start2_StartInfo_Null ()
  398. {
  399. try {
  400. Process.Start ((ProcessStartInfo) null);
  401. Assert.Fail ("#1");
  402. } catch (ArgumentNullException ex) {
  403. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  404. Assert.IsNull (ex.InnerException, "#3");
  405. Assert.IsNotNull (ex.Message, "#4");
  406. Assert.IsNotNull (ex.ParamName, "#5");
  407. Assert.AreEqual ("startInfo", ex.ParamName, "#6");
  408. }
  409. }
  410. [Test] // Start (string)
  411. public void Start3_FileName_Empty ()
  412. {
  413. try {
  414. Process.Start (string.Empty);
  415. Assert.Fail ("#1");
  416. } catch (InvalidOperationException ex) {
  417. // Cannot start process because a file name has
  418. // not been provided
  419. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  420. Assert.IsNull (ex.InnerException, "#3");
  421. Assert.IsNotNull (ex.Message, "#4");
  422. }
  423. }
  424. [Test] // Start (string)
  425. public void Start3_FileName_NotFound ()
  426. {
  427. if (RunningOnUnix)
  428. Assert.Ignore ("On Unix and Mac OS X, we try " +
  429. "to open any file (using xdg-open, ...)" +
  430. " and we do not report an exception " +
  431. "if this fails.");
  432. string exe = @"c:\shouldnoteverexist.exe";
  433. // absolute path, no shell
  434. try {
  435. Process.Start (exe);
  436. Assert.Fail ("#A1");
  437. } catch (Win32Exception ex) {
  438. // The system cannot find the file specified
  439. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
  440. Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
  441. Assert.IsNull (ex.InnerException, "#A4");
  442. Assert.IsNotNull (ex.Message, "#A5");
  443. Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
  444. }
  445. // relative path, no shell
  446. try {
  447. Process.Start ("shouldnoteverexist.exe");
  448. Assert.Fail ("#B1");
  449. } catch (Win32Exception ex) {
  450. // The system cannot find the file specified
  451. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
  452. Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
  453. Assert.IsNull (ex.InnerException, "#B4");
  454. Assert.IsNotNull (ex.Message, "#B5");
  455. Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
  456. }
  457. }
  458. [Test] // Start (string)
  459. public void Start3_FileName_Null ()
  460. {
  461. try {
  462. Process.Start ((string) null);
  463. Assert.Fail ("#1");
  464. } catch (InvalidOperationException ex) {
  465. // Cannot start process because a file name has
  466. // not been provided
  467. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  468. Assert.IsNull (ex.InnerException, "#3");
  469. Assert.IsNotNull (ex.Message, "#4");
  470. }
  471. }
  472. [Test] // Start (string, string)
  473. public void Start4_Arguments_Null ()
  474. {
  475. if (RunningOnUnix)
  476. Assert.Ignore ("On Unix and Mac OS X, we try " +
  477. "to open any file (using xdg-open, ...)" +
  478. " and we do not report an exception " +
  479. "if this fails.");
  480. string exe = @"c:\shouldnoteverexist.exe";
  481. try {
  482. Process.Start ("whatever.exe", (string) null);
  483. Assert.Fail ("#1");
  484. } catch (Win32Exception ex) {
  485. // The system cannot find the file specified
  486. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
  487. Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
  488. Assert.IsNull (ex.InnerException, "#B4");
  489. Assert.IsNotNull (ex.Message, "#B5");
  490. Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
  491. }
  492. }
  493. [Test] // Start (string, string)
  494. public void Start4_FileName_Empty ()
  495. {
  496. try {
  497. Process.Start (string.Empty, string.Empty);
  498. Assert.Fail ("#1");
  499. } catch (InvalidOperationException ex) {
  500. // Cannot start process because a file name has
  501. // not been provided
  502. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  503. Assert.IsNull (ex.InnerException, "#3");
  504. Assert.IsNotNull (ex.Message, "#4");
  505. }
  506. }
  507. [Test] // Start (string, string)
  508. public void Start4_FileName_NotFound ()
  509. {
  510. if (RunningOnUnix)
  511. Assert.Ignore ("On Unix and Mac OS X, we try " +
  512. "to open any file (using xdg-open, ...)" +
  513. " and we do not report an exception " +
  514. "if this fails.");
  515. string exe = @"c:\shouldnoteverexist.exe";
  516. // absolute path, no shell
  517. try {
  518. Process.Start (exe, string.Empty);
  519. Assert.Fail ("#A1");
  520. } catch (Win32Exception ex) {
  521. // The system cannot find the file specified
  522. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
  523. Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
  524. Assert.IsNull (ex.InnerException, "#A4");
  525. Assert.IsNotNull (ex.Message, "#A5");
  526. Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
  527. }
  528. // relative path, no shell
  529. try {
  530. Process.Start ("shouldnoteverexist.exe", string.Empty);
  531. Assert.Fail ("#B1");
  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. #if NET_2_0
  542. [Test]
  543. public void Start_UseShellExecuteWithEmptyUserName ()
  544. {
  545. if (RunningOnUnix)
  546. Assert.Ignore ("On Unix and Mac OS X, we try " +
  547. "to open any file (using xdg-open, ...)" +
  548. " and we do not report an exception " +
  549. "if this fails.");
  550. string exe = @"c:\shouldnoteverexist.exe";
  551. try {
  552. Process p = new Process ();
  553. p.StartInfo.FileName = exe;
  554. p.StartInfo.UseShellExecute = true;
  555. p.StartInfo.UserName = "";
  556. p.Start ();
  557. Assert.Fail ("#1");
  558. } catch (InvalidOperationException) {
  559. Assert.Fail ("#2");
  560. } catch (Win32Exception) {
  561. }
  562. try {
  563. Process p = new Process ();
  564. p.StartInfo.FileName = exe;
  565. p.StartInfo.UseShellExecute = true;
  566. p.StartInfo.UserName = null;
  567. p.Start ();
  568. Assert.Fail ("#3");
  569. } catch (InvalidOperationException) {
  570. Assert.Fail ("#4");
  571. } catch (Win32Exception) {
  572. }
  573. }
  574. #endif
  575. [Test] // Start (string, string)
  576. public void Start4_FileName_Null ()
  577. {
  578. try {
  579. Process.Start ((string) null, string.Empty);
  580. Assert.Fail ("#1");
  581. } catch (InvalidOperationException ex) {
  582. // Cannot start process because a file name has
  583. // not been provided
  584. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  585. Assert.IsNull (ex.InnerException, "#3");
  586. Assert.IsNotNull (ex.Message, "#4");
  587. }
  588. }
  589. [Test]
  590. public void StartInfo ()
  591. {
  592. ProcessStartInfo startInfo = new ProcessStartInfo ();
  593. Process p = new Process ();
  594. Assert.IsNotNull (p.StartInfo, "#A1");
  595. p.StartInfo = startInfo;
  596. Assert.AreSame (startInfo, p.StartInfo, "#A2");
  597. }
  598. [Test]
  599. public void StartInfo_Null ()
  600. {
  601. Process p = new Process ();
  602. try {
  603. p.StartInfo = null;
  604. Assert.Fail ("#1");
  605. } catch (ArgumentNullException ex) {
  606. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  607. Assert.IsNull (ex.InnerException, "#3");
  608. Assert.IsNotNull (ex.Message, "#4");
  609. Assert.IsNotNull (ex.ParamName, "#5");
  610. Assert.AreEqual ("value", ex.ParamName, "#6");
  611. }
  612. }
  613. [Test]
  614. [NUnit.Framework.Category ("NotDotNet")]
  615. public void TestRedirectedOutputIsAsync ()
  616. {
  617. // Test requires cygwin, so we just bail out for now.
  618. if (Path.DirectorySeparatorChar == '\\')
  619. return;
  620. Process p = new Process ();
  621. p.StartInfo = new ProcessStartInfo ("/bin/sh", "-c \"sleep 2; echo hello\"");
  622. p.StartInfo.RedirectStandardOutput = true;
  623. p.StartInfo.UseShellExecute = false;
  624. p.Start ();
  625. Stream stdout = p.StandardOutput.BaseStream;
  626. byte [] buffer = new byte [200];
  627. // start async Read operation
  628. DateTime start = DateTime.Now;
  629. IAsyncResult ar = stdout.BeginRead (buffer, 0, buffer.Length,
  630. new AsyncCallback (Read), stdout);
  631. Assert.IsTrue ((DateTime.Now - start).TotalMilliseconds < 1000, "#01 BeginRead was not async");
  632. p.WaitForExit ();
  633. Assert.AreEqual (0, p.ExitCode, "#02 script failure");
  634. /*
  635. ar.AsyncWaitHandle.WaitOne (2000, false);
  636. if (bytesRead < "hello".Length)
  637. Assert.Fail ("#03 got {0} bytes", bytesRead);
  638. Assert.AreEqual ("hello", Encoding.Default.GetString (buffer, 0, 5), "#04");
  639. */
  640. }
  641. void Read (IAsyncResult ar)
  642. {
  643. Stream stm = (Stream) ar.AsyncState;
  644. bytesRead = stm.EndRead (ar);
  645. }
  646. static bool RunningOnUnix {
  647. get {
  648. int p = (int)Environment.OSVersion.Platform;
  649. return ((p == 128) || (p == 4) || (p == 6));
  650. }
  651. }
  652. int bytesRead = -1;
  653. }
  654. }