2
0

ProcessTest.cs 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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 System.Threading;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.Diagnostics
  18. {
  19. [TestFixture]
  20. public class ProcessTest
  21. {
  22. [Test]
  23. public void GetProcessById_MachineName_Null ()
  24. {
  25. try {
  26. Process.GetProcessById (1, (string) null);
  27. Assert.Fail ("#1");
  28. } catch (ArgumentNullException ex) {
  29. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  30. Assert.IsNotNull (ex.Message, "#3");
  31. Assert.IsNotNull (ex.ParamName, "#4");
  32. Assert.AreEqual ("machineName", ex.ParamName, "#5");
  33. Assert.IsNull (ex.InnerException, "#6");
  34. }
  35. }
  36. [Test]
  37. public void GetProcesses_MachineName_Null ()
  38. {
  39. try {
  40. Process.GetProcesses ((string) null);
  41. Assert.Fail ("#1");
  42. } catch (ArgumentNullException ex) {
  43. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  44. Assert.IsNotNull (ex.Message, "#3");
  45. Assert.IsNotNull (ex.ParamName, "#4");
  46. Assert.AreEqual ("machineName", ex.ParamName, "#5");
  47. Assert.IsNull (ex.InnerException, "#6");
  48. }
  49. }
  50. [Test] // Covers #26363
  51. [NUnit.Framework.Category ("MobileNotWorking")]
  52. public void GetProcesses_StartTime ()
  53. {
  54. foreach (var p in Process.GetProcesses ()) {
  55. if (!p.HasExited && p.StartTime.Year < 1800)
  56. Assert.Fail ("Process should not be started since the 18th century.");
  57. }
  58. }
  59. [Test]
  60. public void PriorityClass_NotStarted ()
  61. {
  62. Process process = new Process ();
  63. try {
  64. process.PriorityClass = ProcessPriorityClass.Normal;
  65. Assert.Fail ("#A1");
  66. } catch (InvalidOperationException ex) {
  67. // No process is associated with this object
  68. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  69. Assert.IsNull (ex.InnerException, "#A3");
  70. Assert.IsNotNull (ex.Message, "#A4");
  71. }
  72. try {
  73. Assert.Fail ("#B1:" + process.PriorityClass);
  74. } catch (InvalidOperationException ex) {
  75. // No process is associated with this object
  76. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  77. Assert.IsNull (ex.InnerException, "#B3");
  78. Assert.IsNotNull (ex.Message, "#B4");
  79. }
  80. }
  81. [Test]
  82. public void PriorityClass_Invalid ()
  83. {
  84. Process process = new Process ();
  85. try {
  86. process.PriorityClass = (ProcessPriorityClass) 666;
  87. Assert.Fail ("#1");
  88. } catch (InvalidEnumArgumentException ex) {
  89. Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
  90. Assert.IsNull (ex.InnerException, "#3");
  91. Assert.IsNotNull (ex.Message, "#4");
  92. Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#5");
  93. Assert.IsTrue (ex.Message.IndexOf (typeof (ProcessPriorityClass).Name) != -1, "#6");
  94. Assert.IsNotNull (ex.ParamName, "#7");
  95. Assert.AreEqual ("value", ex.ParamName, "#8");
  96. }
  97. }
  98. #if MONO_FEATURE_PROCESS_START
  99. [Test] // Start ()
  100. public void Start1_FileName_Empty ()
  101. {
  102. Process process = new Process ();
  103. process.StartInfo = new ProcessStartInfo (string.Empty);
  104. // no shell
  105. process.StartInfo.UseShellExecute = false;
  106. try {
  107. process.Start ();
  108. Assert.Fail ("#A1");
  109. } catch (InvalidOperationException ex) {
  110. // Cannot start process because a file name has
  111. // not been provided
  112. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  113. Assert.IsNull (ex.InnerException, "#A3");
  114. Assert.IsNotNull (ex.Message, "#A4");
  115. }
  116. // shell
  117. process.StartInfo.UseShellExecute = true;
  118. try {
  119. process.Start ();
  120. Assert.Fail ("#B1");
  121. } catch (InvalidOperationException ex) {
  122. // Cannot start process because a file name has
  123. // not been provided
  124. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  125. Assert.IsNull (ex.InnerException, "#B3");
  126. Assert.IsNotNull (ex.Message, "#B4");
  127. }
  128. }
  129. [Test] // Start ()
  130. public void Start1_FileName_InvalidPathCharacters ()
  131. {
  132. if (RunningOnUnix)
  133. // on unix, all characters are allowed
  134. Assert.Ignore ("Running on Unix.");
  135. string systemDir = Environment.GetFolderPath (Environment.SpecialFolder.System);
  136. string exe = "\"" + Path.Combine (systemDir, "calc.exe") + "\"";
  137. Process process = new Process ();
  138. process.StartInfo = new ProcessStartInfo (exe);
  139. // no shell
  140. process.StartInfo.UseShellExecute = false;
  141. Assert.IsTrue (process.Start ());
  142. process.Kill ();
  143. // shell
  144. process.StartInfo.UseShellExecute = true;
  145. Assert.IsTrue (process.Start ());
  146. process.Kill ();
  147. }
  148. [Test] // Start ()
  149. public void Start1_FileName_NotFound ()
  150. {
  151. Process process = new Process ();
  152. string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
  153. : @"c:\shouldnoteverexist.exe";
  154. // absolute path, no shell
  155. process.StartInfo = new ProcessStartInfo (exe);
  156. process.StartInfo.UseShellExecute = false;
  157. try {
  158. process.Start ();
  159. Assert.Fail ("#A1");
  160. } catch (Win32Exception ex) {
  161. // The system cannot find the file specified
  162. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
  163. Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
  164. Assert.IsNull (ex.InnerException, "#A4");
  165. Assert.IsNotNull (ex.Message, "#A5");
  166. Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
  167. }
  168. // relative path, no shell
  169. process.StartInfo.FileName = "shouldnoteverexist.exe";
  170. process.StartInfo.UseShellExecute = false;
  171. try {
  172. process.Start ();
  173. Assert.Fail ("#B1");
  174. } catch (Win32Exception ex) {
  175. // The system cannot find the file specified
  176. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
  177. Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
  178. Assert.IsNull (ex.InnerException, "#B4");
  179. Assert.IsNotNull (ex.Message, "#B5");
  180. Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
  181. }
  182. if (RunningOnUnix)
  183. Assert.Ignore ("On Unix and Mac OS X, we try " +
  184. "to open any file (using xdg-open, ...)" +
  185. " and we do not report an exception " +
  186. "if this fails.");
  187. // absolute path, shell
  188. process.StartInfo.FileName = exe;
  189. process.StartInfo.UseShellExecute = true;
  190. try {
  191. process.Start ();
  192. Assert.Fail ("#C1");
  193. } catch (Win32Exception ex) {
  194. // The system cannot find the file specified
  195. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
  196. Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
  197. Assert.IsNull (ex.InnerException, "#C4");
  198. Assert.IsNotNull (ex.Message, "#C5");
  199. Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
  200. }
  201. // relative path, shell
  202. process.StartInfo.FileName = "shouldnoteverexist.exe";
  203. process.StartInfo.UseShellExecute = true;
  204. try {
  205. process.Start ();
  206. Assert.Fail ("#D1");
  207. } catch (Win32Exception ex) {
  208. // The system cannot find the file specified
  209. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
  210. Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
  211. Assert.IsNull (ex.InnerException, "#D4");
  212. Assert.IsNotNull (ex.Message, "#D5");
  213. Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
  214. }
  215. }
  216. [Test] // Start ()
  217. public void Start1_FileName_Null ()
  218. {
  219. Process process = new Process ();
  220. process.StartInfo = new ProcessStartInfo ((string) null);
  221. // no shell
  222. process.StartInfo.UseShellExecute = false;
  223. try {
  224. process.Start ();
  225. Assert.Fail ("#A1");
  226. } catch (InvalidOperationException ex) {
  227. // Cannot start process because a file name has
  228. // not been provided
  229. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  230. Assert.IsNull (ex.InnerException, "#A3");
  231. Assert.IsNotNull (ex.Message, "#A4");
  232. }
  233. // shell
  234. process.StartInfo.UseShellExecute = true;
  235. try {
  236. process.Start ();
  237. Assert.Fail ("#B1");
  238. } catch (InvalidOperationException ex) {
  239. // Cannot start process because a file name has
  240. // not been provided
  241. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  242. Assert.IsNull (ex.InnerException, "#B3");
  243. Assert.IsNotNull (ex.Message, "#B4");
  244. }
  245. }
  246. [Test] // Start ()
  247. public void Start1_FileName_Whitespace ()
  248. {
  249. Process process = new Process ();
  250. process.StartInfo = new ProcessStartInfo (" ");
  251. process.StartInfo.UseShellExecute = false;
  252. try {
  253. process.Start ();
  254. Assert.Fail ("#1");
  255. } catch (Win32Exception ex) {
  256. // The system cannot find the file specified
  257. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
  258. Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
  259. Assert.IsNull (ex.InnerException, "#4");
  260. Assert.IsNotNull (ex.Message, "#5");
  261. Assert.AreEqual (2, ex.NativeErrorCode, "#6");
  262. }
  263. }
  264. [Test] // Start (ProcessStartInfo)
  265. public void Start2_FileName_Empty ()
  266. {
  267. ProcessStartInfo startInfo = new ProcessStartInfo (string.Empty);
  268. // no shell
  269. startInfo.UseShellExecute = false;
  270. try {
  271. Process.Start (startInfo);
  272. Assert.Fail ("#A1");
  273. } catch (InvalidOperationException ex) {
  274. // Cannot start process because a file name has
  275. // not been provided
  276. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  277. Assert.IsNull (ex.InnerException, "#A3");
  278. Assert.IsNotNull (ex.Message, "#A4");
  279. }
  280. // shell
  281. startInfo.UseShellExecute = true;
  282. try {
  283. Process.Start (startInfo);
  284. Assert.Fail ("#B1");
  285. } catch (InvalidOperationException ex) {
  286. // Cannot start process because a file name has
  287. // not been provided
  288. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  289. Assert.IsNull (ex.InnerException, "#B3");
  290. Assert.IsNotNull (ex.Message, "#B4");
  291. }
  292. }
  293. [Test] // Start (ProcessStartInfo)
  294. public void Start2_FileName_NotFound ()
  295. {
  296. ProcessStartInfo startInfo = new ProcessStartInfo ();
  297. string exe = RunningOnUnix ? exe = "/usr/bin/shouldnoteverexist"
  298. : @"c:\shouldnoteverexist.exe";
  299. // absolute path, no shell
  300. startInfo.FileName = exe;
  301. startInfo.UseShellExecute = false;
  302. try {
  303. Process.Start (startInfo);
  304. Assert.Fail ("#A1");
  305. } catch (Win32Exception ex) {
  306. // The system cannot find the file specified
  307. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
  308. Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
  309. Assert.IsNull (ex.InnerException, "#A4");
  310. Assert.IsNotNull (ex.Message, "#A5");
  311. Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
  312. }
  313. // relative path, no shell
  314. startInfo.FileName = "shouldnoteverexist.exe";
  315. startInfo.UseShellExecute = false;
  316. try {
  317. Process.Start (startInfo);
  318. Assert.Fail ("#B1");
  319. } catch (Win32Exception ex) {
  320. // The system cannot find the file specified
  321. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
  322. Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
  323. Assert.IsNull (ex.InnerException, "#B4");
  324. Assert.IsNotNull (ex.Message, "#B5");
  325. Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
  326. }
  327. if (RunningOnUnix)
  328. Assert.Ignore ("On Unix and Mac OS X, we try " +
  329. "to open any file (using xdg-open, ...)" +
  330. " and we do not report an exception " +
  331. "if this fails.");
  332. // absolute path, shell
  333. startInfo.FileName = exe;
  334. startInfo.UseShellExecute = true;
  335. try {
  336. Process.Start (startInfo);
  337. Assert.Fail ("#C1");
  338. } catch (Win32Exception ex) {
  339. // The system cannot find the file specified
  340. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#C2");
  341. Assert.AreEqual (-2147467259, ex.ErrorCode, "#C3");
  342. Assert.IsNull (ex.InnerException, "#C4");
  343. Assert.IsNotNull (ex.Message, "#C5");
  344. Assert.AreEqual (2, ex.NativeErrorCode, "#C6");
  345. }
  346. // relative path, shell
  347. startInfo.FileName = "shouldnoteverexist.exe";
  348. startInfo.UseShellExecute = true;
  349. try {
  350. Process.Start (startInfo);
  351. Assert.Fail ("#D1");
  352. } catch (Win32Exception ex) {
  353. // The system cannot find the file specified
  354. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#D2");
  355. Assert.AreEqual (-2147467259, ex.ErrorCode, "#D3");
  356. Assert.IsNull (ex.InnerException, "#D4");
  357. Assert.IsNotNull (ex.Message, "#D5");
  358. Assert.AreEqual (2, ex.NativeErrorCode, "#D6");
  359. }
  360. }
  361. [Test] // Start (ProcessStartInfo)
  362. public void Start2_FileName_Null ()
  363. {
  364. ProcessStartInfo startInfo = new ProcessStartInfo ((string) null);
  365. // no shell
  366. startInfo.UseShellExecute = false;
  367. try {
  368. Process.Start (startInfo);
  369. Assert.Fail ("#A1");
  370. } catch (InvalidOperationException ex) {
  371. // Cannot start process because a file name has
  372. // not been provided
  373. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  374. Assert.IsNull (ex.InnerException, "#A3");
  375. Assert.IsNotNull (ex.Message, "#A4");
  376. }
  377. // shell
  378. startInfo.UseShellExecute = true;
  379. try {
  380. Process.Start (startInfo);
  381. Assert.Fail ("#B1");
  382. } catch (InvalidOperationException ex) {
  383. // Cannot start process because a file name has
  384. // not been provided
  385. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  386. Assert.IsNull (ex.InnerException, "#B3");
  387. Assert.IsNotNull (ex.Message, "#B4");
  388. }
  389. }
  390. [Test] // Start (ProcessStartInfo)
  391. public void Start2_FileName_Whitespace ()
  392. {
  393. ProcessStartInfo startInfo = new ProcessStartInfo (" ");
  394. startInfo.UseShellExecute = false;
  395. try {
  396. Process.Start (startInfo);
  397. Assert.Fail ("#1");
  398. } catch (Win32Exception ex) {
  399. // The system cannot find the file specified
  400. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#2");
  401. Assert.AreEqual (-2147467259, ex.ErrorCode, "#3");
  402. Assert.IsNull (ex.InnerException, "#4");
  403. Assert.IsNotNull (ex.Message, "#5");
  404. Assert.AreEqual (2, ex.NativeErrorCode, "#6");
  405. }
  406. }
  407. [Test] // Start (ProcessStartInfo)
  408. public void Start2_StartInfo_Null ()
  409. {
  410. try {
  411. Process.Start ((ProcessStartInfo) null);
  412. Assert.Fail ("#1");
  413. } catch (ArgumentNullException ex) {
  414. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  415. Assert.IsNull (ex.InnerException, "#3");
  416. Assert.IsNotNull (ex.Message, "#4");
  417. Assert.IsNotNull (ex.ParamName, "#5");
  418. Assert.AreEqual ("startInfo", ex.ParamName, "#6");
  419. }
  420. }
  421. [Test] // Start (string)
  422. public void Start3_FileName_Empty ()
  423. {
  424. try {
  425. Process.Start (string.Empty);
  426. Assert.Fail ("#1");
  427. } catch (InvalidOperationException ex) {
  428. // Cannot start process because a file name has
  429. // not been provided
  430. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  431. Assert.IsNull (ex.InnerException, "#3");
  432. Assert.IsNotNull (ex.Message, "#4");
  433. }
  434. }
  435. [Test] // Start (string)
  436. public void Start3_FileName_NotFound ()
  437. {
  438. if (RunningOnUnix)
  439. Assert.Ignore ("On Unix and Mac OS X, we try " +
  440. "to open any file (using xdg-open, ...)" +
  441. " and we do not report an exception " +
  442. "if this fails.");
  443. string exe = @"c:\shouldnoteverexist.exe";
  444. // absolute path, no shell
  445. try {
  446. Process.Start (exe);
  447. Assert.Fail ("#A1");
  448. } catch (Win32Exception ex) {
  449. // The system cannot find the file specified
  450. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
  451. Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
  452. Assert.IsNull (ex.InnerException, "#A4");
  453. Assert.IsNotNull (ex.Message, "#A5");
  454. Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
  455. }
  456. // relative path, no shell
  457. try {
  458. Process.Start ("shouldnoteverexist.exe");
  459. Assert.Fail ("#B1");
  460. } catch (Win32Exception ex) {
  461. // The system cannot find the file specified
  462. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
  463. Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
  464. Assert.IsNull (ex.InnerException, "#B4");
  465. Assert.IsNotNull (ex.Message, "#B5");
  466. Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
  467. }
  468. }
  469. [Test] // Start (string)
  470. public void Start3_FileName_Null ()
  471. {
  472. try {
  473. Process.Start ((string) null);
  474. Assert.Fail ("#1");
  475. } catch (InvalidOperationException ex) {
  476. // Cannot start process because a file name has
  477. // not been provided
  478. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  479. Assert.IsNull (ex.InnerException, "#3");
  480. Assert.IsNotNull (ex.Message, "#4");
  481. }
  482. }
  483. [Test] // Start (string, string)
  484. public void Start4_Arguments_Null ()
  485. {
  486. if (RunningOnUnix)
  487. Assert.Ignore ("On Unix and Mac OS X, we try " +
  488. "to open any file (using xdg-open, ...)" +
  489. " and we do not report an exception " +
  490. "if this fails.");
  491. string exe = @"c:\shouldnoteverexist.exe";
  492. try {
  493. Process.Start ("whatever.exe", (string) null);
  494. Assert.Fail ("#1");
  495. } catch (Win32Exception ex) {
  496. // The system cannot find the file specified
  497. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#B2");
  498. Assert.AreEqual (-2147467259, ex.ErrorCode, "#B3");
  499. Assert.IsNull (ex.InnerException, "#B4");
  500. Assert.IsNotNull (ex.Message, "#B5");
  501. Assert.AreEqual (2, ex.NativeErrorCode, "#B6");
  502. }
  503. }
  504. [Test] // Start (string, string)
  505. public void Start4_FileName_Empty ()
  506. {
  507. try {
  508. Process.Start (string.Empty, string.Empty);
  509. Assert.Fail ("#1");
  510. } catch (InvalidOperationException ex) {
  511. // Cannot start process because a file name has
  512. // not been provided
  513. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  514. Assert.IsNull (ex.InnerException, "#3");
  515. Assert.IsNotNull (ex.Message, "#4");
  516. }
  517. }
  518. [Test] // Start (string, string)
  519. public void Start4_FileName_NotFound ()
  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. string exe = @"c:\shouldnoteverexist.exe";
  527. // absolute path, no shell
  528. try {
  529. Process.Start (exe, string.Empty);
  530. Assert.Fail ("#A1");
  531. } catch (Win32Exception ex) {
  532. // The system cannot find the file specified
  533. Assert.AreEqual (typeof (Win32Exception), ex.GetType (), "#A2");
  534. Assert.AreEqual (-2147467259, ex.ErrorCode, "#A3");
  535. Assert.IsNull (ex.InnerException, "#A4");
  536. Assert.IsNotNull (ex.Message, "#A5");
  537. Assert.AreEqual (2, ex.NativeErrorCode, "#A6");
  538. }
  539. // relative path, no shell
  540. try {
  541. Process.Start ("shouldnoteverexist.exe", string.Empty);
  542. Assert.Fail ("#B1");
  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");
  550. }
  551. }
  552. [Test]
  553. public void Start_UseShellExecuteWithEmptyUserName ()
  554. {
  555. if (RunningOnUnix)
  556. Assert.Ignore ("On Unix and Mac OS X, we try " +
  557. "to open any file (using xdg-open, ...)" +
  558. " and we do not report an exception " +
  559. "if this fails.");
  560. string exe = @"c:\shouldnoteverexist.exe";
  561. try {
  562. Process p = new Process ();
  563. p.StartInfo.FileName = exe;
  564. p.StartInfo.UseShellExecute = true;
  565. p.StartInfo.UserName = "";
  566. p.Start ();
  567. Assert.Fail ("#1");
  568. } catch (InvalidOperationException) {
  569. Assert.Fail ("#2");
  570. } catch (Win32Exception) {
  571. }
  572. try {
  573. Process p = new Process ();
  574. p.StartInfo.FileName = exe;
  575. p.StartInfo.UseShellExecute = true;
  576. p.StartInfo.UserName = null;
  577. p.Start ();
  578. Assert.Fail ("#3");
  579. } catch (InvalidOperationException) {
  580. Assert.Fail ("#4");
  581. } catch (Win32Exception) {
  582. }
  583. }
  584. [Test] // Start (string, string)
  585. public void Start4_FileName_Null ()
  586. {
  587. try {
  588. Process.Start ((string) null, string.Empty);
  589. Assert.Fail ("#1");
  590. } catch (InvalidOperationException ex) {
  591. // Cannot start process because a file name has
  592. // not been provided
  593. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  594. Assert.IsNull (ex.InnerException, "#3");
  595. Assert.IsNotNull (ex.Message, "#4");
  596. }
  597. }
  598. [Test]
  599. public void StartInfo ()
  600. {
  601. ProcessStartInfo startInfo = new ProcessStartInfo ();
  602. Process p = new Process ();
  603. Assert.IsNotNull (p.StartInfo, "#A1");
  604. p.StartInfo = startInfo;
  605. Assert.AreSame (startInfo, p.StartInfo, "#A2");
  606. }
  607. [Test]
  608. public void StartInfo_Null ()
  609. {
  610. Process p = new Process ();
  611. try {
  612. p.StartInfo = null;
  613. Assert.Fail ("#1");
  614. } catch (ArgumentNullException ex) {
  615. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  616. Assert.IsNull (ex.InnerException, "#3");
  617. Assert.IsNotNull (ex.Message, "#4");
  618. Assert.IsNotNull (ex.ParamName, "#5");
  619. Assert.AreEqual ("value", ex.ParamName, "#6");
  620. }
  621. }
  622. [Test]
  623. [NUnit.Framework.Category ("NotDotNet")]
  624. [NUnit.Framework.Category ("MobileNotWorking")]
  625. public void TestRedirectedOutputIsAsync ()
  626. {
  627. // Test requires cygwin, so we just bail out for now.
  628. if (Path.DirectorySeparatorChar == '\\')
  629. Assert.Ignore ("Test requires cygwin.");
  630. Process p = new Process ();
  631. p.StartInfo = new ProcessStartInfo ("/bin/sh", "-c \"sleep 2; echo hello\"");
  632. p.StartInfo.RedirectStandardOutput = true;
  633. p.StartInfo.UseShellExecute = false;
  634. p.Start ();
  635. Stream stdout = p.StandardOutput.BaseStream;
  636. byte [] buffer = new byte [200];
  637. // start async Read operation
  638. DateTime start = DateTime.Now;
  639. IAsyncResult ar = stdout.BeginRead (buffer, 0, buffer.Length,
  640. new AsyncCallback (Read), stdout);
  641. Assert.IsTrue ((DateTime.Now - start).TotalMilliseconds < 1000, "#01 BeginRead was not async");
  642. p.WaitForExit ();
  643. Assert.AreEqual (0, p.ExitCode, "#02 script failure");
  644. /*
  645. ar.AsyncWaitHandle.WaitOne (2000, false);
  646. if (bytesRead < "hello".Length)
  647. Assert.Fail ("#03 got {0} bytes", bytesRead);
  648. Assert.AreEqual ("hello", Encoding.Default.GetString (buffer, 0, 5), "#04");
  649. */
  650. }
  651. void Read (IAsyncResult ar)
  652. {
  653. Stream stm = (Stream) ar.AsyncState;
  654. bytesRead = stm.EndRead (ar);
  655. }
  656. static bool RunningOnUnix {
  657. get {
  658. int p = (int)Environment.OSVersion.Platform;
  659. return ((p == 128) || (p == 4) || (p == 6));
  660. }
  661. }
  662. public int bytesRead = -1;
  663. [Test]
  664. [NUnit.Framework.Category ("MobileNotWorking")]
  665. // This was for bug #459450
  666. public void TestEventRaising ()
  667. {
  668. EventWaitHandle errorClosed = new ManualResetEvent(false);
  669. EventWaitHandle outClosed = new ManualResetEvent(false);
  670. EventWaitHandle exited = new ManualResetEvent(false);
  671. Process p = new Process();
  672. p.StartInfo = GetCrossPlatformStartInfo ();
  673. p.StartInfo.UseShellExecute = false;
  674. p.StartInfo.RedirectStandardOutput = true;
  675. p.StartInfo.RedirectStandardError = true;
  676. p.StartInfo.RedirectStandardInput = false;
  677. p.OutputDataReceived += (object sender, DataReceivedEventArgs e) => {
  678. if (e.Data == null) {
  679. outClosed.Set();
  680. }
  681. };
  682. p.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => {
  683. if (e.Data == null) {
  684. errorClosed.Set();
  685. }
  686. };
  687. p.Exited += (object sender, EventArgs e) => {
  688. exited.Set ();
  689. };
  690. p.EnableRaisingEvents = true;
  691. p.Start();
  692. p.BeginErrorReadLine();
  693. p.BeginOutputReadLine();
  694. Console.WriteLine("started, waiting for handles");
  695. bool r = WaitHandle.WaitAll(new WaitHandle[] { errorClosed, outClosed, exited }, 10000, false);
  696. Assert.AreEqual (true, r, "Null Argument Events Raised");
  697. }
  698. [Test]
  699. [NUnit.Framework.Category ("MobileNotWorking")]
  700. public void TestEnableEventsAfterExitedEvent ()
  701. {
  702. Process p = new Process ();
  703. p.StartInfo = GetCrossPlatformStartInfo ();
  704. p.StartInfo.UseShellExecute = false;
  705. p.StartInfo.RedirectStandardOutput = true;
  706. p.StartInfo.RedirectStandardError = true;
  707. var exitedCalledCounter = 0;
  708. var exited = new ManualResetEventSlim ();
  709. p.Exited += (object sender, EventArgs e) => {
  710. exitedCalledCounter++;
  711. Assert.IsTrue (p.HasExited);
  712. exited.Set ();
  713. };
  714. p.EnableRaisingEvents = true;
  715. p.Start ();
  716. p.BeginErrorReadLine ();
  717. p.BeginOutputReadLine ();
  718. p.WaitForExit ();
  719. exited.Wait (10000);
  720. Assert.AreEqual (1, exitedCalledCounter);
  721. Thread.Sleep (50);
  722. Assert.AreEqual (1, exitedCalledCounter);
  723. }
  724. [Test]
  725. [NUnit.Framework.Category ("MobileNotWorking")]
  726. public void TestEnableEventsBeforeExitedEvent ()
  727. {
  728. Process p = new Process ();
  729. p.StartInfo = GetCrossPlatformStartInfo ();
  730. p.StartInfo.UseShellExecute = false;
  731. p.StartInfo.RedirectStandardOutput = true;
  732. p.StartInfo.RedirectStandardError = true;
  733. p.EnableRaisingEvents = true;
  734. var exitedCalledCounter = 0;
  735. var exited = new ManualResetEventSlim ();
  736. p.Exited += (object sender, EventArgs e) => {
  737. exitedCalledCounter++;
  738. Assert.IsTrue (p.HasExited);
  739. exited.Set ();
  740. };
  741. p.Start ();
  742. p.BeginErrorReadLine ();
  743. p.BeginOutputReadLine ();
  744. p.WaitForExit ();
  745. Assert.IsTrue (exited.Wait (10000));
  746. Assert.AreEqual (1, exitedCalledCounter);
  747. Thread.Sleep (50);
  748. Assert.AreEqual (1, exitedCalledCounter);
  749. }
  750. [Test]
  751. [NUnit.Framework.Category ("MobileNotWorking")]
  752. public void TestDisableEventsBeforeExitedEvent ()
  753. {
  754. Process p = new Process ();
  755. p.StartInfo = GetCrossPlatformStartInfo ();
  756. p.StartInfo.UseShellExecute = false;
  757. p.StartInfo.RedirectStandardOutput = true;
  758. p.StartInfo.RedirectStandardError = true;
  759. p.EnableRaisingEvents = false;
  760. ManualResetEvent mre = new ManualResetEvent (false);
  761. p.Exited += (object sender, EventArgs e) => {
  762. mre.Set ();
  763. };
  764. p.Start ();
  765. p.BeginErrorReadLine ();
  766. p.BeginOutputReadLine ();
  767. p.WaitForExit ();
  768. Assert.IsFalse (mre.WaitOne (1000));
  769. }
  770. ProcessStartInfo GetCrossPlatformStartInfo ()
  771. {
  772. if (RunningOnUnix) {
  773. string path;
  774. #if MONODROID
  775. path = "/system/bin/ls";
  776. #else
  777. path = "/bin/ls";
  778. #endif
  779. return new ProcessStartInfo (path, "/");
  780. } else
  781. return new ProcessStartInfo ("help", "");
  782. }
  783. #endif // MONO_FEATURE_PROCESS_START
  784. [Test]
  785. public void ProcessName_NotStarted ()
  786. {
  787. Process p = new Process ();
  788. Exception e = null;
  789. try {
  790. String.IsNullOrEmpty (p.ProcessName);
  791. } catch (Exception ex) {
  792. e = ex;
  793. }
  794. Assert.IsNotNull (e, "ProcessName should raise if process was not started");
  795. //msg should be "No process is associated with this object"
  796. Assert.AreEqual (e.GetType (), typeof (InvalidOperationException),
  797. "exception should be IOE, I got: " + e.GetType ().Name);
  798. Assert.IsNull (e.InnerException, "IOE inner exception should be null");
  799. }
  800. #if MONO_FEATURE_PROCESS_START
  801. [Test]
  802. [NUnit.Framework.Category ("MobileNotWorking")]
  803. public void ProcessName_AfterExit ()
  804. {
  805. Process p = new Process ();
  806. p.StartInfo = GetCrossPlatformStartInfo ();
  807. p.StartInfo.UseShellExecute = false;
  808. p.StartInfo.RedirectStandardOutput = true;
  809. p.StartInfo.RedirectStandardError = true;
  810. p.Start ();
  811. p.BeginErrorReadLine();
  812. p.BeginOutputReadLine();
  813. p.WaitForExit ();
  814. String.IsNullOrEmpty (p.ExitCode + "");
  815. Exception e = null;
  816. try {
  817. String.IsNullOrEmpty (p.ProcessName);
  818. } catch (Exception ex) {
  819. e = ex;
  820. }
  821. Assert.IsNotNull (e, "ProcessName should raise if process was finished");
  822. //msg should be "Process has exited, so the requested information is not available"
  823. Assert.AreEqual (e.GetType (), typeof (InvalidOperationException),
  824. "exception should be IOE, I got: " + e.GetType ().Name);
  825. Assert.IsNull (e.InnerException, "IOE inner exception should be null");
  826. }
  827. #endif // MONO_FEATURE_PROCESS_START
  828. [Test]
  829. public void Handle_ThrowsOnNotStarted ()
  830. {
  831. Process p = new Process ();
  832. try {
  833. var x = p.Handle;
  834. Assert.Fail ("Handle should throw for unstated procs, but returned " + x);
  835. } catch (InvalidOperationException) {
  836. }
  837. }
  838. [Test]
  839. public void HasExitedCurrent () {
  840. Assert.IsFalse (Process.GetCurrentProcess ().HasExited);
  841. }
  842. #if MONO_FEATURE_PROCESS_START
  843. [Test]
  844. [NUnit.Framework.Category ("MobileNotWorking")]
  845. public void DisposeWithDisposedStreams ()
  846. {
  847. var psi = GetCrossPlatformStartInfo ();
  848. psi.RedirectStandardInput = true;
  849. psi.RedirectStandardOutput = true;
  850. psi.UseShellExecute = false;
  851. var p = Process.Start (psi);
  852. p.StandardInput.BaseStream.Dispose ();
  853. p.StandardOutput.BaseStream.Dispose ();
  854. p.Dispose ();
  855. }
  856. [Test]
  857. [NUnit.Framework.Category ("MobileNotWorking")]
  858. public void StandardInputWrite ()
  859. {
  860. var psi = GetCrossPlatformStartInfo ();
  861. psi.RedirectStandardInput = true;
  862. psi.RedirectStandardOutput = true;
  863. psi.UseShellExecute = false;
  864. using (var p = Process.Start (psi)) {
  865. for (int i = 0; i < 1024 * 9; ++i)
  866. p.StandardInput.Write ('x');
  867. }
  868. }
  869. #endif // MONO_FEATURE_PROCESS_START
  870. [Test]
  871. public void Modules () {
  872. var modules = Process.GetCurrentProcess ().Modules;
  873. foreach (var a in AppDomain.CurrentDomain.GetAssemblies ()) {
  874. var found = false;
  875. var name = a.GetName ();
  876. StringBuilder sb = new StringBuilder ();
  877. sb.AppendFormat ("Could not found: {0} {1}\n", name.Name, name.Version);
  878. sb.AppendLine ("Looked in assemblies:");
  879. foreach (var o in modules) {
  880. var m = (ProcessModule) o;
  881. sb.AppendFormat (" {0} {1}.{2}.{3}\n", m.FileName.ToString (),
  882. m.FileVersionInfo.FileMajorPart,
  883. m.FileVersionInfo.FileMinorPart,
  884. m.FileVersionInfo.FileBuildPart);
  885. if (!m.FileName.StartsWith ("[In Memory] " + name.Name))
  886. continue;
  887. var fv = m.FileVersionInfo;
  888. if (fv.FileBuildPart != name.Version.Build ||
  889. fv.FileMinorPart != name.Version.Minor ||
  890. fv.FileMajorPart != name.Version.Major)
  891. continue;
  892. found = true;
  893. }
  894. Assert.IsTrue (found, sb.ToString ());
  895. }
  896. }
  897. #if MONO_FEATURE_PROCESS_START
  898. [Test]
  899. [NUnit.Framework.Category ("MobileNotWorking")]
  900. [ExpectedException (typeof (InvalidOperationException))]
  901. public void TestDoubleBeginOutputReadLine ()
  902. {
  903. using (Process p = new Process ()) {
  904. p.StartInfo = GetCrossPlatformStartInfo ();
  905. p.StartInfo.UseShellExecute = false;
  906. p.StartInfo.RedirectStandardOutput = true;
  907. p.StartInfo.RedirectStandardError = true;
  908. p.Start ();
  909. p.BeginOutputReadLine ();
  910. p.BeginOutputReadLine ();
  911. Assert.Fail ();
  912. }
  913. }
  914. [Test]
  915. [NUnit.Framework.Category ("MobileNotWorking")]
  916. [ExpectedException (typeof (InvalidOperationException))]
  917. public void TestDoubleBeginErrorReadLine ()
  918. {
  919. using (Process p = new Process ()) {
  920. p.StartInfo = GetCrossPlatformStartInfo ();
  921. p.StartInfo.UseShellExecute = false;
  922. p.StartInfo.RedirectStandardOutput = true;
  923. p.StartInfo.RedirectStandardError = true;
  924. p.Start ();
  925. p.BeginErrorReadLine ();
  926. p.BeginErrorReadLine ();
  927. Assert.Fail ();
  928. }
  929. }
  930. #endif // MONO_FEATURE_PROCESS_START
  931. }
  932. }