FileStreamTest.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  1. // FileStreamTests.cs - NUnit2 Test Cases for System.IO.FileStream class
  2. //
  3. // Authors:
  4. // Ville Palo ([email protected])
  5. // Gert Driesen ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) Ville Palo
  9. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  10. // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  11. //
  12. using NUnit.Framework;
  13. using System;
  14. using System.IO;
  15. using System.Runtime.InteropServices;
  16. using System.Text;
  17. namespace MonoTests.System.IO
  18. {
  19. [TestFixture]
  20. public class FileStreamTest
  21. {
  22. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  23. static readonly char DSC = Path.DirectorySeparatorChar;
  24. static bool MacOSX = false;
  25. [DllImport ("libc")]
  26. static extern int uname (IntPtr buf);
  27. [TearDown]
  28. public void TearDown ()
  29. {
  30. if (Directory.Exists (TempFolder))
  31. Directory.Delete (TempFolder, true);
  32. }
  33. [SetUp]
  34. public void SetUp ()
  35. {
  36. if (Directory.Exists (TempFolder))
  37. Directory.Delete (TempFolder, true);
  38. Directory.CreateDirectory (TempFolder);
  39. #if !MOBILE
  40. // from XplatUI.cs
  41. IntPtr buf = Marshal.AllocHGlobal (8192);
  42. if (uname (buf) == 0)
  43. MacOSX = Marshal.PtrToStringAnsi (buf) == "Darwin";
  44. Marshal.FreeHGlobal (buf);
  45. #endif
  46. }
  47. public void TestCtr ()
  48. {
  49. string path = TempFolder + DSC + "testfilestream.tmp.1";
  50. DeleteFile (path);
  51. FileStream stream = null;
  52. try {
  53. stream = new FileStream (path, FileMode.Create);
  54. } finally {
  55. if (stream != null)
  56. stream.Close ();
  57. DeleteFile (path);
  58. }
  59. }
  60. [Test]
  61. [ExpectedException (typeof (ArgumentException))]
  62. public void CtorArgumentException1 ()
  63. {
  64. FileStream stream;
  65. stream = new FileStream ("", FileMode.Create);
  66. stream.Close ();
  67. }
  68. [Test]
  69. [ExpectedException (typeof (ArgumentNullException))]
  70. public void CtorArgumentNullException ()
  71. {
  72. FileStream stream = new FileStream (null, FileMode.Create);
  73. stream.Close ();
  74. }
  75. [Test]
  76. public void CtorFileNotFoundException_Mode_Open ()
  77. {
  78. // absolute path
  79. string path = TempFolder + DSC + "thisfileshouldnotexists.test";
  80. DeleteFile (path);
  81. FileStream stream = null;
  82. try {
  83. stream = new FileStream (TempFolder + DSC + "thisfileshouldnotexists.test", FileMode.Open);
  84. Assert.Fail ("#A1");
  85. } catch (FileNotFoundException ex) {
  86. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
  87. Assert.AreEqual (path, ex.FileName, "#A3");
  88. Assert.IsNull (ex.InnerException, "#A4");
  89. Assert.IsNotNull (ex.Message, "#A5");
  90. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
  91. } finally {
  92. if (stream != null) {
  93. stream.Close ();
  94. stream = null;
  95. }
  96. DeleteFile (path);
  97. }
  98. // relative path
  99. string orignalCurrentDir = Directory.GetCurrentDirectory ();
  100. Directory.SetCurrentDirectory (TempFolder);
  101. try {
  102. stream = new FileStream ("thisfileshouldnotexists.test", FileMode.Open);
  103. Assert.Fail ("#B1");
  104. } catch (FileNotFoundException ex) {
  105. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
  106. // under OSX 'var' is a symlink to 'private/var'
  107. if (MacOSX)
  108. path = "/private" + path;
  109. Assert.AreEqual (path, ex.FileName, "#B3");
  110. Assert.IsNull (ex.InnerException, "#B4");
  111. Assert.IsNotNull (ex.Message, "#B5");
  112. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
  113. } finally {
  114. // restore original current directory
  115. Directory.SetCurrentDirectory (orignalCurrentDir);
  116. if (stream != null)
  117. stream.Close ();
  118. DeleteFile (path);
  119. }
  120. }
  121. [Test]
  122. public void CtorFileNotFoundException_Mode_Truncate ()
  123. {
  124. // absolute path
  125. string path = TempFolder + DSC + "thisfileshouldNOTexists.test";
  126. DeleteFile (path);
  127. FileStream stream = null;
  128. try {
  129. stream = new FileStream (path, FileMode.Truncate);
  130. Assert.Fail ("#A1");
  131. } catch (FileNotFoundException ex) {
  132. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
  133. Assert.AreEqual (path, ex.FileName, "#A3");
  134. Assert.IsNull (ex.InnerException, "#A4");
  135. Assert.IsNotNull (ex.Message, "#A5");
  136. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
  137. } finally {
  138. if (stream != null) {
  139. stream.Close ();
  140. stream = null;
  141. }
  142. DeleteFile (path);
  143. }
  144. // relative path
  145. string orignalCurrentDir = Directory.GetCurrentDirectory ();
  146. Directory.SetCurrentDirectory (TempFolder);
  147. try {
  148. stream = new FileStream ("thisfileshouldNOTexists.test", FileMode.Truncate);
  149. Assert.Fail ("#B1");
  150. } catch (FileNotFoundException ex) {
  151. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
  152. // under OSX 'var' is a symlink to 'private/var'
  153. if (MacOSX)
  154. path = "/private" + path;
  155. Assert.AreEqual (path, ex.FileName, "#B3");
  156. Assert.IsNull (ex.InnerException, "#B4");
  157. Assert.IsNotNull (ex.Message, "#B5");
  158. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
  159. } finally {
  160. // restore original current directory
  161. Directory.SetCurrentDirectory (orignalCurrentDir);
  162. if (stream != null)
  163. stream.Close ();
  164. DeleteFile (path);
  165. }
  166. }
  167. [Test]
  168. public void CtorIOException1 ()
  169. {
  170. // absolute path
  171. string path = TempFolder + DSC + "thisfileshouldexists.test";
  172. FileStream stream = null;
  173. DeleteFile (path);
  174. try {
  175. stream = new FileStream (path, FileMode.CreateNew);
  176. stream.Close ();
  177. stream = null;
  178. stream = new FileStream (path, FileMode.CreateNew);
  179. Assert.Fail ("#A1");
  180. } catch (IOException ex) {
  181. Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
  182. Assert.IsNull (ex.InnerException, "#A3");
  183. Assert.IsNotNull (ex.Message, "#A4");
  184. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A5");
  185. } finally {
  186. if (stream != null) {
  187. stream.Close ();
  188. stream = null;
  189. }
  190. DeleteFile (path);
  191. }
  192. // relative path
  193. string orignalCurrentDir = Directory.GetCurrentDirectory ();
  194. Directory.SetCurrentDirectory (TempFolder);
  195. try {
  196. stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
  197. stream.Close ();
  198. stream = null;
  199. stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
  200. Assert.Fail ("#B1");
  201. } catch (IOException ex) {
  202. Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
  203. Assert.IsNull (ex.InnerException, "#B3");
  204. Assert.IsNotNull (ex.Message, "#B4");
  205. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B5");
  206. } finally {
  207. // restore original current directory
  208. Directory.SetCurrentDirectory (orignalCurrentDir);
  209. if (stream != null)
  210. stream.Close ();
  211. DeleteFile (path);
  212. }
  213. }
  214. [Test]
  215. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  216. public void CtorArgumentOutOfRangeException1 ()
  217. {
  218. FileStream stream = null;
  219. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  220. DeleteFile (path);
  221. try {
  222. stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
  223. } finally {
  224. if (stream != null)
  225. stream.Close ();
  226. DeleteFile (path);
  227. }
  228. }
  229. [Test]
  230. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  231. public void CtorArgumentOutOfRangeException2 ()
  232. {
  233. FileStream stream = null;
  234. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  235. DeleteFile (path);
  236. try {
  237. stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
  238. } finally {
  239. if (stream != null)
  240. stream.Close ();
  241. DeleteFile (path);
  242. }
  243. }
  244. private void CtorDirectoryNotFoundException (FileMode mode)
  245. {
  246. string path = TempFolder + DSC + "thisDirectoryShouldNotExists";
  247. if (Directory.Exists (path))
  248. Directory.Delete (path, true);
  249. FileStream stream = null;
  250. try {
  251. stream = new FileStream (path + DSC + "eitherthisfile.test", mode);
  252. } finally {
  253. if (stream != null)
  254. stream.Close ();
  255. if (Directory.Exists (path))
  256. Directory.Delete (path, true);
  257. }
  258. }
  259. [Test]
  260. [ExpectedException (typeof (DirectoryNotFoundException))]
  261. public void CtorDirectoryNotFoundException_CreateNew ()
  262. {
  263. CtorDirectoryNotFoundException (FileMode.CreateNew);
  264. }
  265. [Test]
  266. [ExpectedException (typeof (DirectoryNotFoundException))]
  267. public void CtorDirectoryNotFoundException_Create ()
  268. {
  269. CtorDirectoryNotFoundException (FileMode.Create);
  270. }
  271. [Test]
  272. [ExpectedException (typeof (DirectoryNotFoundException))]
  273. public void CtorDirectoryNotFoundException_Open ()
  274. {
  275. CtorDirectoryNotFoundException (FileMode.Open);
  276. }
  277. [Test]
  278. [ExpectedException (typeof (DirectoryNotFoundException))]
  279. public void CtorDirectoryNotFoundException_OpenOrCreate ()
  280. {
  281. CtorDirectoryNotFoundException (FileMode.OpenOrCreate);
  282. }
  283. [Test]
  284. [ExpectedException (typeof (DirectoryNotFoundException))]
  285. public void CtorDirectoryNotFoundException_Truncate ()
  286. {
  287. CtorDirectoryNotFoundException (FileMode.Truncate);
  288. }
  289. [Test]
  290. [ExpectedException (typeof (DirectoryNotFoundException))]
  291. public void CtorDirectoryNotFoundException_Append ()
  292. {
  293. CtorDirectoryNotFoundException (FileMode.Append);
  294. }
  295. [Test]
  296. public void CtorDirectoryNotFound_RelativePath ()
  297. {
  298. string orignalCurrentDir = Directory.GetCurrentDirectory ();
  299. Directory.SetCurrentDirectory (TempFolder);
  300. string relativePath = "DirectoryDoesNotExist" + Path.DirectorySeparatorChar + "file.txt";
  301. string fullPath = Path.Combine (TempFolder, relativePath);
  302. try {
  303. new FileStream (relativePath, FileMode.Open);
  304. Assert.Fail ("#A1");
  305. } catch (DirectoryNotFoundException ex) {
  306. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
  307. Assert.IsNull (ex.InnerException, "#A3");
  308. Assert.IsNotNull (ex.Message, "#A4");
  309. Assert.IsTrue (ex.Message.IndexOf (fullPath) != -1, "#A5");
  310. } finally {
  311. // restore original current directory
  312. Directory.SetCurrentDirectory (orignalCurrentDir);
  313. }
  314. }
  315. [Test]
  316. // FileShare.Inheritable is ignored, but file does not exist
  317. [ExpectedException (typeof (FileNotFoundException))]
  318. public void CtorArgumentOutOfRangeException3 ()
  319. {
  320. string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
  321. DeleteFile (path);
  322. FileStream stream = null;
  323. try {
  324. stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable);
  325. } finally {
  326. if (stream != null)
  327. stream.Close ();
  328. DeleteFile (path);
  329. }
  330. }
  331. [Test]
  332. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  333. public void CtorArgumentOutOfRangeException4 ()
  334. {
  335. string path = TempFolder + DSC + "CtorArgumentOutOfRangeException4";
  336. DeleteFile (path);
  337. FileStream stream = null;
  338. try {
  339. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, -1);
  340. } finally {
  341. if (stream != null)
  342. stream.Close ();
  343. DeleteFile (path);
  344. }
  345. }
  346. [Test]
  347. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  348. public void CtorBufferSizeZero ()
  349. {
  350. // Buffer size can't be zero
  351. string path = Path.Combine (TempFolder, "CtorBufferSizeZero");
  352. DeleteFile (path);
  353. FileStream stream = null;
  354. try {
  355. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite, 0);
  356. } finally {
  357. if (stream != null)
  358. stream.Close ();
  359. DeleteFile (path);
  360. }
  361. }
  362. [Test]
  363. [ExpectedException (typeof (ArgumentException))]
  364. public void CtorArgumentException2 ()
  365. {
  366. // FileMode.CreateNew && FileAccess.Read
  367. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  368. FileStream stream = null;
  369. DeleteFile (path);
  370. try {
  371. stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
  372. } finally {
  373. if (stream != null)
  374. stream.Close ();
  375. DeleteFile (path);
  376. }
  377. }
  378. [Test]
  379. // FileShare.Inheritable is ignored, but file does not exist
  380. [ExpectedException (typeof (FileNotFoundException))]
  381. public void CtorArgumentOutOfRangeException5 ()
  382. {
  383. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  384. DeleteFile (path);
  385. FileStream stream = null;
  386. try {
  387. stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
  388. } finally {
  389. if (stream != null)
  390. stream.Close ();
  391. DeleteFile (path);
  392. }
  393. }
  394. [Test]
  395. [ExpectedException (typeof (ArgumentException))]
  396. public void CtorArgumentException3 ()
  397. {
  398. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  399. FileStream stream = null;
  400. DeleteFile (path);
  401. try {
  402. stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
  403. } finally {
  404. if (stream != null)
  405. stream.Close ();
  406. DeleteFile (path);
  407. }
  408. }
  409. [Test]
  410. public void ModeAndAccessCombinations ()
  411. {
  412. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  413. DeleteFile (path);
  414. FileStream stream = null;
  415. // Append / Read
  416. try {
  417. // Append access can be requested only in write-only mode
  418. stream = new FileStream (path, FileMode.Append, FileAccess.Read);
  419. Assert.Fail ("#A1");
  420. } catch (ArgumentException ex) {
  421. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  422. } finally {
  423. if (stream != null)
  424. stream.Close ();
  425. DeleteFile (path);
  426. }
  427. // Append / ReadWrite
  428. try {
  429. // Append access can be requested only in write-only mode
  430. stream = new FileStream (path, FileMode.Append, FileAccess.ReadWrite);
  431. Assert.Fail ("#B1");
  432. } catch (ArgumentException ex) {
  433. // make sure it is exact this exception, and not a derived type
  434. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  435. } finally {
  436. if (stream != null)
  437. stream.Close ();
  438. DeleteFile (path);
  439. }
  440. // Append / Write
  441. try {
  442. stream = new FileStream (path, FileMode.Append, FileAccess.Write);
  443. } finally {
  444. if (stream != null)
  445. stream.Close ();
  446. DeleteFile (path);
  447. }
  448. // Create / Read
  449. try {
  450. stream = new FileStream (path, FileMode.Create, FileAccess.Read);
  451. Assert.Fail ("#C1");
  452. } catch (ArgumentException ex) {
  453. // make sure it is exact this exception, and not a derived type
  454. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  455. } finally {
  456. if (stream != null)
  457. stream.Close ();
  458. DeleteFile (path);
  459. }
  460. // Create / ReadWrite
  461. try {
  462. stream = new FileStream (path, FileMode.Create, FileAccess.ReadWrite);
  463. } finally {
  464. if (stream != null)
  465. stream.Close ();
  466. DeleteFile (path);
  467. }
  468. // Create / Write
  469. try {
  470. stream = new FileStream (path, FileMode.Create, FileAccess.Write);
  471. } finally {
  472. if (stream != null)
  473. stream.Close ();
  474. DeleteFile (path);
  475. }
  476. // CreateNew / Read
  477. try {
  478. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
  479. Assert.Fail ("#D1");
  480. } catch (ArgumentException ex) {
  481. // make sure it is exact this exception, and not a derived type
  482. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  483. } finally {
  484. if (stream != null)
  485. stream.Close ();
  486. DeleteFile (path);
  487. }
  488. // CreateNew / ReadWrite
  489. try {
  490. stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  491. } finally {
  492. if (stream != null)
  493. stream.Close ();
  494. DeleteFile (path);
  495. }
  496. // CreateNew / Write
  497. try {
  498. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
  499. } finally {
  500. if (stream != null)
  501. stream.Close ();
  502. DeleteFile (path);
  503. }
  504. // Open / Read
  505. try {
  506. stream = new FileStream (path, FileMode.Open, FileAccess.Read);
  507. Assert.Fail ("#E1");
  508. } catch (FileNotFoundException ex) {
  509. // make sure it is exact this exception, and not a derived type
  510. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#E2");
  511. Assert.AreEqual (path, ex.FileName, "#E3");
  512. Assert.IsNull (ex.InnerException, "#E4");
  513. Assert.IsNotNull (ex.Message, "#E5");
  514. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#E6");
  515. } finally {
  516. if (stream != null)
  517. stream.Close ();
  518. DeleteFile (path);
  519. }
  520. // Open / ReadWrite
  521. try {
  522. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  523. Assert.Fail ("#F1");
  524. } catch (FileNotFoundException ex) {
  525. // make sure it is exact this exception, and not a derived type
  526. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#F2");
  527. Assert.AreEqual (path, ex.FileName, "#F3");
  528. Assert.IsNull (ex.InnerException, "#F4");
  529. Assert.IsNotNull (ex.Message, "#F5");
  530. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#F6");
  531. } finally {
  532. if (stream != null)
  533. stream.Close ();
  534. DeleteFile (path);
  535. }
  536. // Open / Write
  537. try {
  538. stream = new FileStream (path, FileMode.Open, FileAccess.Write);
  539. Assert.Fail ("#G1");
  540. } catch (FileNotFoundException ex) {
  541. // make sure it is exact this exception, and not a derived type
  542. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#G2");
  543. Assert.AreEqual (path, ex.FileName, "#G3");
  544. Assert.IsNull (ex.InnerException, "#G4");
  545. Assert.IsNotNull (ex.Message, "#G5");
  546. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#G6");
  547. } finally {
  548. if (stream != null)
  549. stream.Close ();
  550. DeleteFile (path);
  551. }
  552. // OpenOrCreate / Read
  553. try {
  554. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  555. } finally {
  556. if (stream != null)
  557. stream.Close ();
  558. DeleteFile (path);
  559. }
  560. // OpenOrCreate / ReadWrite
  561. try {
  562. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  563. } finally {
  564. if (stream != null)
  565. stream.Close ();
  566. DeleteFile (path);
  567. }
  568. // OpenOrCreate / Write
  569. try {
  570. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  571. } finally {
  572. if (stream != null)
  573. stream.Close ();
  574. DeleteFile (path);
  575. }
  576. // Truncate / Read
  577. try {
  578. stream = new FileStream (path, FileMode.Truncate, FileAccess.Read);
  579. Assert.Fail ("#H1");
  580. } catch (ArgumentException ex) {
  581. // make sure it is exact this exception, and not a derived type
  582. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#H2");
  583. } finally {
  584. if (stream != null)
  585. stream.Close ();
  586. DeleteFile (path);
  587. }
  588. // Truncate / ReadWrite
  589. try {
  590. stream = new FileStream (path, FileMode.Truncate, FileAccess.ReadWrite);
  591. Assert.Fail ("#I1");
  592. } catch (FileNotFoundException ex) {
  593. // make sure it is exact this exception, and not a derived type
  594. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#I2");
  595. Assert.AreEqual (path, ex.FileName, "#I3");
  596. Assert.IsNull (ex.InnerException, "#I4");
  597. Assert.IsNotNull (ex.Message, "#I5");
  598. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#I6");
  599. } finally {
  600. if (stream != null)
  601. stream.Close ();
  602. DeleteFile (path);
  603. }
  604. // Truncate / Write
  605. try {
  606. stream = new FileStream (path, FileMode.Truncate, FileAccess.Write);
  607. Assert.Fail ("#J1");
  608. } catch (FileNotFoundException ex) {
  609. // make sure it is exact this exception, and not a derived type
  610. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#J2");
  611. Assert.AreEqual (path, ex.FileName, "#J3");
  612. Assert.IsNull (ex.InnerException, "#J4");
  613. Assert.IsNotNull (ex.Message, "#J5");
  614. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#J6");
  615. } finally {
  616. if (stream != null)
  617. stream.Close ();
  618. DeleteFile (path);
  619. }
  620. }
  621. #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
  622. [Test, ExpectedException (typeof (IOException))]
  623. public void CtorIOException2 ()
  624. {
  625. FileStream stream = null;
  626. try {
  627. stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
  628. } finally {
  629. if (stream != null)
  630. stream.Close ();
  631. }
  632. }
  633. #endif // TARGET_JVM
  634. [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
  635. [Test, ExpectedException (typeof (IOException))]
  636. public void CtorIOException ()
  637. {
  638. string path = TempFolder + DSC + "CTorIOException.Test";
  639. FileStream stream = null;
  640. FileStream stream2 = null;
  641. DeleteFile (path);
  642. try {
  643. stream = new FileStream (path, FileMode.CreateNew);
  644. // used by an another process
  645. stream2 = new FileStream (path, FileMode.OpenOrCreate);
  646. } finally {
  647. if (stream != null)
  648. stream.Close ();
  649. if (stream2 != null)
  650. stream2.Close ();
  651. DeleteFile (path);
  652. }
  653. }
  654. [Test]
  655. public void CtorAccess1Read2Read ()
  656. {
  657. FileStream fs = null;
  658. FileStream fs2 = null;
  659. string fn = Path.Combine (TempFolder, "temp");
  660. try {
  661. if (!File.Exists (fn)) {
  662. TextWriter tw = File.CreateText (fn);
  663. tw.Write ("FOO");
  664. tw.Close ();
  665. }
  666. fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
  667. fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
  668. } finally {
  669. if (fs != null)
  670. fs.Close ();
  671. if (fs2 != null)
  672. fs2.Close ();
  673. if (File.Exists (fn))
  674. File.Delete (fn);
  675. }
  676. }
  677. [Test]
  678. [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
  679. [ExpectedException (typeof (IOException))]
  680. public void CtorAccess1Read2Write ()
  681. {
  682. string fn = Path.Combine (TempFolder, "temp");
  683. FileStream fs1 = null;
  684. FileStream fs2 = null;
  685. try {
  686. if (!File.Exists (fn)) {
  687. using (TextWriter tw = File.CreateText (fn)) {
  688. tw.Write ("FOO");
  689. }
  690. }
  691. fs1 = new FileStream (fn, FileMode.Open, FileAccess.Read);
  692. fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
  693. } finally {
  694. if (fs1 != null)
  695. fs1.Close ();
  696. if (fs2 != null)
  697. fs2.Close ();
  698. if (File.Exists (fn))
  699. File.Delete (fn);
  700. }
  701. }
  702. [Test]
  703. [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
  704. [ExpectedException (typeof (IOException))]
  705. public void CtorAccess1Write2Write ()
  706. {
  707. string fn = Path.Combine (TempFolder, "temp");
  708. FileStream fs1 = null;
  709. FileStream fs2 = null;
  710. try {
  711. if (File.Exists (fn))
  712. File.Delete (fn);
  713. fs1 = new FileStream (fn, FileMode.Create, FileAccess.Write);
  714. fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
  715. } finally {
  716. if (fs1 != null)
  717. fs1.Close ();
  718. if (fs2 != null)
  719. fs2.Close ();
  720. if (File.Exists (fn))
  721. File.Delete (fn);
  722. }
  723. }
  724. [Test]
  725. [ExpectedException (typeof (UnauthorizedAccessException))]
  726. public void CtorReadDirectoryAsFile ()
  727. {
  728. FileStream stream = null;
  729. try {
  730. stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
  731. } finally {
  732. if (stream != null)
  733. stream.Close ();
  734. }
  735. }
  736. [Test] // bug #79250
  737. public void FileShare_Delete ()
  738. {
  739. string fn = Path.Combine (TempFolder, "temp");
  740. using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete)) {
  741. s.Write (new byte [1] { 0x5 }, 0, 1);
  742. File.Delete (fn);
  743. }
  744. using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete)) {
  745. s.Write (new byte [1] { 0x5 }, 0, 1);
  746. File.Delete (fn);
  747. }
  748. using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write | FileShare.Delete)) {
  749. s.Write (new byte [1] { 0x5 }, 0, 1);
  750. File.Delete (fn);
  751. }
  752. using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read | FileShare.Delete)) {
  753. s.Write (new byte [1] { 0x5 }, 0, 1);
  754. File.Delete (fn);
  755. }
  756. using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Inheritable | FileShare.Delete)) {
  757. s.Write (new byte [1] { 0x5 }, 0, 1);
  758. File.Delete (fn);
  759. }
  760. }
  761. [Test]
  762. public void Write ()
  763. {
  764. string path = TempFolder + DSC + "FileStreamTest.Write";
  765. DeleteFile (path);
  766. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
  767. byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  768. byte[] bytes = new byte[15];
  769. // Check that the data is flushed when we overflow the buffer
  770. // with a large amount of data
  771. stream.Write (outbytes, 0, 5);
  772. stream.Write (outbytes, 5, 10);
  773. stream.Seek (0, SeekOrigin.Begin);
  774. stream.Read (bytes, 0, 15);
  775. for (int i = 0; i < 15; ++i)
  776. Assert.AreEqual (i + 1, bytes[i], "#1");
  777. // Check that the data is flushed when we overflow the buffer
  778. // with a small amount of data
  779. stream.Write (outbytes, 0, 7);
  780. stream.Write (outbytes, 7, 7);
  781. stream.Write (outbytes, 14, 1);
  782. stream.Read (bytes, 0, 15);
  783. stream.Seek (15, SeekOrigin.Begin);
  784. for (int i = 0; i < 15; ++i)
  785. Assert.AreEqual (i + 1, bytes[i], "#2");
  786. stream.Close ();
  787. }
  788. [Test]
  789. public void Length ()
  790. {
  791. // Test that the Length property takes into account the data
  792. // in the buffer
  793. string path = TempFolder + DSC + "FileStreamTest.Length";
  794. DeleteFile (path);
  795. FileStream stream = new FileStream (path, FileMode.CreateNew);
  796. byte[] outbytes = new byte[] { 1, 2, 3, 4 };
  797. stream.Write (outbytes, 0, 4);
  798. Assert.AreEqual (4, stream.Length);
  799. stream.Close ();
  800. }
  801. [Test]
  802. public void Flush ()
  803. {
  804. string path = TempFolder + DSC + "FileStreamTest.Flush";
  805. FileStream stream = null;
  806. FileStream stream2 = null;
  807. DeleteFile (path);
  808. try {
  809. stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  810. stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  811. stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
  812. byte[] bytes = new byte[5];
  813. stream2.Read (bytes, 0, 5);
  814. Assert.AreEqual (0, bytes[0], "#A1");
  815. Assert.AreEqual (0, bytes[1], "#A2");
  816. Assert.AreEqual (0, bytes[2], "#A3");
  817. Assert.AreEqual (0, bytes[3], "#A4");
  818. stream.Flush ();
  819. stream2.Read (bytes, 0, 5);
  820. Assert.AreEqual (1, bytes[0], "#B1");
  821. Assert.AreEqual (2, bytes[1], "#B2");
  822. Assert.AreEqual (3, bytes[2], "#B3");
  823. Assert.AreEqual (4, bytes[3], "#B4");
  824. } finally {
  825. if (stream != null)
  826. stream.Close ();
  827. if (stream2 != null)
  828. stream2.Close ();
  829. DeleteFile (path);
  830. }
  831. }
  832. public void TestDefaultProperties ()
  833. {
  834. string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
  835. DeleteFile (path);
  836. FileStream stream = new FileStream (path, FileMode.Create);
  837. Assert.IsTrue (stream.CanRead, "#A1");
  838. Assert.IsTrue (stream.CanSeek, "#A2");
  839. Assert.IsTrue (stream.CanWrite, "#A3");
  840. Assert.IsFalse (stream.IsAsync, "#A4");
  841. Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
  842. Assert.AreEqual (0, stream.Position, "#A6");
  843. Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
  844. stream.Close ();
  845. DeleteFile (path);
  846. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  847. Assert.IsTrue (stream.CanRead, "#B1");
  848. Assert.IsTrue (stream.CanSeek, "#B2");
  849. Assert.IsFalse (stream.CanWrite, "#B3");
  850. Assert.IsFalse (stream.IsAsync, "#B4");
  851. Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
  852. Assert.AreEqual (0, stream.Position, "#B6");
  853. Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
  854. stream.Close ();
  855. stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
  856. Assert.IsFalse (stream.CanRead, "#C1");
  857. Assert.IsTrue (stream.CanSeek, "#C2");
  858. Assert.IsTrue (stream.CanWrite, "#C3");
  859. Assert.IsFalse (stream.IsAsync, "#C4");
  860. Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
  861. Assert.AreEqual (0, stream.Position, "#C6");
  862. Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
  863. stream.Close ();
  864. DeleteFile (path);
  865. }
  866. [Category ("NotWorking")]
  867. // Bug: 71371 -> duplicate and WONTFIX.
  868. public void TestLock_FailsOnMono ()
  869. {
  870. string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
  871. DeleteFile (path);
  872. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  873. stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
  874. stream.Close ();
  875. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  876. stream.Lock (0, 5);
  877. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  878. byte[] bytes = new byte[5];
  879. try {
  880. stream2.Read (bytes, 0, 5);
  881. Assert.Fail ("#1");
  882. } catch (Exception e) {
  883. Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
  884. }
  885. stream.Close ();
  886. stream2.Close ();
  887. DeleteFile (path);
  888. }
  889. [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
  890. public void TestLock ()
  891. {
  892. string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
  893. DeleteFile (path);
  894. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  895. stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
  896. stream.Close ();
  897. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  898. stream.Lock (0, 5);
  899. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  900. byte[] bytes = new byte[5];
  901. try {
  902. stream2.Read (bytes, 0, 5);
  903. Assert.Fail ("#A1");
  904. } catch (Exception) {
  905. // Bug #71371: on MS.NET you get an IOException detailing a lock
  906. // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
  907. }
  908. stream2.Seek (5, SeekOrigin.Begin);
  909. stream2.Read (bytes, 0, 5);
  910. Assert.AreEqual (5, bytes[0], "#B1");
  911. Assert.AreEqual (6, bytes[1], "#B2");
  912. Assert.AreEqual (7, bytes[2], "#B3");
  913. Assert.AreEqual (8, bytes[3], "#B4");
  914. Assert.AreEqual (9, bytes[4], "#B5");
  915. stream.Unlock (0, 5);
  916. stream2.Seek (0, SeekOrigin.Begin);
  917. stream2.Read (bytes, 0, 5);
  918. Assert.AreEqual (0, bytes[0], "#C1");
  919. Assert.AreEqual (1, bytes[1], "#C2");
  920. Assert.AreEqual (2, bytes[2], "#C3");
  921. Assert.AreEqual (3, bytes[3], "#C4");
  922. Assert.AreEqual (4, bytes[4], "#C5");
  923. stream.Close ();
  924. stream2.Close ();
  925. DeleteFile (path);
  926. }
  927. [Test]
  928. public void Seek ()
  929. {
  930. string path = TempFolder + DSC + "FST.Seek.Test";
  931. DeleteFile (path);
  932. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  933. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  934. stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
  935. Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
  936. Assert.AreEqual (-1, stream2.ReadByte (), "#2");
  937. Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
  938. Assert.AreEqual (-1, stream2.ReadByte (), "#4");
  939. Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
  940. Assert.AreEqual (-1, stream.ReadByte (), "#6");
  941. Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
  942. Assert.AreEqual (6, stream.ReadByte (), "#8");
  943. Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
  944. Assert.AreEqual (6, stream2.ReadByte (), "#10");
  945. stream.Close ();
  946. stream2.Close ();
  947. DeleteFile (path);
  948. }
  949. public void TestSeek ()
  950. {
  951. string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
  952. DeleteFile (path);
  953. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  954. stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
  955. stream.Seek (5, SeekOrigin.End);
  956. Assert.AreEqual (-1, stream.ReadByte (), "#1");
  957. stream.Seek (-5, SeekOrigin.End);
  958. Assert.AreEqual (6, stream.ReadByte (), "#2");
  959. try {
  960. stream.Seek (-11, SeekOrigin.End);
  961. Assert.Fail ("#3");
  962. } catch (Exception e) {
  963. Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
  964. }
  965. stream.Seek (19, SeekOrigin.Begin);
  966. Assert.AreEqual (-1, stream.ReadByte (), "#5");
  967. stream.Seek (1, SeekOrigin.Begin);
  968. Assert.AreEqual (2, stream.ReadByte (), "#6");
  969. stream.Seek (3, SeekOrigin.Current);
  970. Assert.AreEqual (6, stream.ReadByte (), "#7");
  971. stream.Seek (-2, SeekOrigin.Current);
  972. Assert.AreEqual (5, stream.ReadByte (), "#8");
  973. stream.Flush ();
  974. // Test that seeks work correctly when seeking inside the buffer
  975. stream.Seek (0, SeekOrigin.Begin);
  976. stream.WriteByte (0);
  977. stream.WriteByte (1);
  978. stream.Seek (0, SeekOrigin.Begin);
  979. byte[] buf = new byte[1];
  980. buf[0] = 2;
  981. stream.Write (buf, 0, 1);
  982. stream.Write (buf, 0, 1);
  983. stream.Flush ();
  984. stream.Seek (0, SeekOrigin.Begin);
  985. Assert.AreEqual (2, stream.ReadByte (), "#9");
  986. Assert.AreEqual (2, stream.ReadByte (), "#10");
  987. stream.Close ();
  988. DeleteFile (path);
  989. }
  990. public void TestClose ()
  991. {
  992. string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
  993. DeleteFile (path);
  994. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  995. stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
  996. stream.ReadByte ();
  997. stream.Close ();
  998. try {
  999. stream.ReadByte ();
  1000. Assert.Fail ("#A1");
  1001. } catch (Exception e) {
  1002. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
  1003. }
  1004. try {
  1005. stream.WriteByte (64);
  1006. Assert.Fail ("#B1");
  1007. } catch (Exception e) {
  1008. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
  1009. }
  1010. try {
  1011. stream.Flush ();
  1012. Assert.Fail ("#C1");
  1013. } catch (Exception e) {
  1014. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
  1015. }
  1016. try {
  1017. long l = stream.Length;
  1018. Assert.Fail ("#D1");
  1019. } catch (Exception e) {
  1020. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
  1021. }
  1022. try {
  1023. long l = stream.Position;
  1024. Assert.Fail ("#E1");
  1025. } catch (Exception e) {
  1026. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
  1027. }
  1028. Assert.IsFalse (stream.CanRead, "#F1");
  1029. Assert.IsFalse (stream.CanSeek, "#F2");
  1030. Assert.IsFalse (stream.CanWrite, "#F3");
  1031. Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
  1032. DeleteFile (path);
  1033. }
  1034. /// <summary>
  1035. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  1036. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  1037. /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
  1038. /// </summary>
  1039. [Test]
  1040. [ExpectedException (typeof (NotSupportedException))]
  1041. public void TestWriteVerifyAccessMode ()
  1042. {
  1043. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1044. DeleteFile (path);
  1045. FileStream stream = null;
  1046. byte[] buffer;
  1047. try {
  1048. buffer = Encoding.ASCII.GetBytes ("test");
  1049. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  1050. stream.Write (buffer, 0, buffer.Length);
  1051. } finally {
  1052. if (stream != null)
  1053. stream.Close ();
  1054. DeleteFile (path);
  1055. }
  1056. }
  1057. /// <summary>
  1058. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  1059. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  1060. /// <see cref="FileStream.WriteByte(byte)" /> method is called.
  1061. /// </summary>
  1062. [Test]
  1063. [ExpectedException (typeof (NotSupportedException))]
  1064. public void TestWriteByteVerifyAccessMode ()
  1065. {
  1066. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1067. DeleteFile (path);
  1068. FileStream stream = null;
  1069. try {
  1070. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  1071. stream.WriteByte (Byte.MinValue);
  1072. } finally {
  1073. if (stream != null)
  1074. stream.Close ();
  1075. DeleteFile (path);
  1076. }
  1077. }
  1078. /// <summary>
  1079. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  1080. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  1081. /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
  1082. /// </summary>
  1083. [Test]
  1084. [ExpectedException (typeof (NotSupportedException))]
  1085. public void TestReadVerifyAccessMode ()
  1086. {
  1087. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1088. DeleteFile (path);
  1089. FileStream stream = null;
  1090. byte[] buffer = new byte[100];
  1091. try {
  1092. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1093. stream.Read (buffer, 0, buffer.Length);
  1094. } finally {
  1095. if (stream != null)
  1096. stream.Close ();
  1097. }
  1098. }
  1099. /// <summary>
  1100. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  1101. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  1102. /// <see cref="FileStream.ReadByte()" /> method is called.
  1103. /// </summary>
  1104. [Test]
  1105. [ExpectedException (typeof (NotSupportedException))]
  1106. public void TestReadByteVerifyAccessMode ()
  1107. {
  1108. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1109. DeleteFile (path);
  1110. FileStream stream = null;
  1111. try {
  1112. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1113. int readByte = stream.ReadByte ();
  1114. } finally {
  1115. if (stream != null)
  1116. stream.Close ();
  1117. DeleteFile (path);
  1118. }
  1119. }
  1120. #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
  1121. // Check that the stream is flushed even when it doesn't own the
  1122. // handle
  1123. [Test]
  1124. public void TestFlushNotOwningHandle ()
  1125. {
  1126. string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
  1127. DeleteFile (path);
  1128. FileStream s = new FileStream (path, FileMode.Create);
  1129. using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
  1130. byte[] buf = new byte[2];
  1131. buf[0] = (int) '1';
  1132. s2.Write (buf, 0, 1);
  1133. }
  1134. s.Position = 0;
  1135. Assert.AreEqual ((int) '1', s.ReadByte ());
  1136. s.Close ();
  1137. }
  1138. #endif // TARGET_JVM
  1139. private void DeleteFile (string path)
  1140. {
  1141. if (File.Exists (path))
  1142. File.Delete (path);
  1143. }
  1144. [Test]
  1145. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1146. public void Read_OffsetNegative ()
  1147. {
  1148. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1149. DeleteFile (path);
  1150. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1151. stream.Read (new byte[0], -1, 1);
  1152. }
  1153. }
  1154. [Test]
  1155. [ExpectedException (typeof (ArgumentException))]
  1156. public void Read_OffsetOverflow ()
  1157. {
  1158. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1159. DeleteFile (path);
  1160. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1161. stream.Read (new byte[0], Int32.MaxValue, 1);
  1162. }
  1163. }
  1164. [Test]
  1165. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1166. public void Read_CountNegative ()
  1167. {
  1168. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1169. DeleteFile (path);
  1170. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1171. stream.Read (new byte[0], 1, -1);
  1172. }
  1173. }
  1174. [Test]
  1175. [ExpectedException (typeof (ArgumentException))]
  1176. public void Read_CountOverflow ()
  1177. {
  1178. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1179. DeleteFile (path);
  1180. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1181. stream.Read (new byte[0], 1, Int32.MaxValue);
  1182. }
  1183. }
  1184. [Test]
  1185. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1186. public void Write_OffsetNegative ()
  1187. {
  1188. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1189. DeleteFile (path);
  1190. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1191. stream.Write (new byte[0], -1, 1);
  1192. }
  1193. }
  1194. [Test]
  1195. [ExpectedException (typeof (ArgumentException))]
  1196. public void Write_OffsetOverflow ()
  1197. {
  1198. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1199. DeleteFile (path);
  1200. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1201. stream.Write (new byte[0], Int32.MaxValue, 1);
  1202. }
  1203. }
  1204. [Test]
  1205. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1206. public void Write_CountNegative ()
  1207. {
  1208. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1209. DeleteFile (path);
  1210. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1211. stream.Write (new byte[0], 1, -1);
  1212. }
  1213. }
  1214. [Test]
  1215. [ExpectedException (typeof (ArgumentException))]
  1216. public void Write_CountOverflow ()
  1217. {
  1218. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1219. DeleteFile (path);
  1220. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1221. stream.Write (new byte[0], 1, Int32.MaxValue);
  1222. }
  1223. }
  1224. [Test]
  1225. [ExpectedException (typeof (ArgumentException))]
  1226. public void Seek_InvalidSeekOrigin ()
  1227. {
  1228. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1229. DeleteFile (path);
  1230. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1231. stream.Seek (0, (SeekOrigin) (-1));
  1232. }
  1233. }
  1234. #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
  1235. [Test]
  1236. [ExpectedException (typeof (ArgumentException))]
  1237. public void Constructor_InvalidFileHandle ()
  1238. {
  1239. new FileStream ((IntPtr) (-1L), FileAccess.Read);
  1240. }
  1241. #endif // TARGET_JVM
  1242. [Test]
  1243. public void PositionAfterSetLength ()
  1244. {
  1245. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1246. DeleteFile (path);
  1247. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1248. stream.SetLength (32);
  1249. stream.Position = 32;
  1250. stream.SetLength (16);
  1251. Assert.AreEqual (16, stream.Position);
  1252. }
  1253. }
  1254. [Test]
  1255. [ExpectedException (typeof (ObjectDisposedException))]
  1256. public void SetLength_Disposed ()
  1257. {
  1258. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1259. DeleteFile (path);
  1260. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1261. stream.Close ();
  1262. stream.SetLength (16);
  1263. }
  1264. [Test]
  1265. [ExpectedException (typeof (ObjectDisposedException))]
  1266. public void Position_Disposed ()
  1267. {
  1268. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1269. DeleteFile (path);
  1270. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  1271. stream.Close ();
  1272. stream.Position = 0;
  1273. }
  1274. [Test]
  1275. [Category("TargetJvmNotSupported")] // Async IO not supported for TARGET_JVM
  1276. [ExpectedException (typeof (ObjectDisposedException))]
  1277. public void BeginRead_Disposed ()
  1278. {
  1279. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1280. DeleteFile (path);
  1281. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  1282. stream.Close ();
  1283. stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
  1284. }
  1285. [Test]
  1286. [Category("TargetJvmNotSupported")] // Async IO not supported for TARGET_JVM
  1287. [ExpectedException (typeof (ObjectDisposedException))]
  1288. public void BeginWrite_Disposed ()
  1289. {
  1290. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1291. DeleteFile (path);
  1292. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1293. stream.Close ();
  1294. stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
  1295. }
  1296. [Test]
  1297. [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
  1298. [ExpectedException (typeof (ObjectDisposedException))]
  1299. public void Lock_Disposed ()
  1300. {
  1301. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1302. DeleteFile (path);
  1303. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1304. stream.Close ();
  1305. stream.Lock (0, 1);
  1306. }
  1307. [Test]
  1308. [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
  1309. [ExpectedException (typeof (ObjectDisposedException))]
  1310. public void Unlock_Disposed ()
  1311. {
  1312. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1313. DeleteFile (path);
  1314. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1315. stream.Close ();
  1316. stream.Unlock (0, 1);
  1317. }
  1318. [Test]
  1319. public void ReadBytePastEndOfStream ()
  1320. {
  1321. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1322. DeleteFile (path);
  1323. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1324. stream.Seek (0, SeekOrigin.End);
  1325. Assert.AreEqual (-1, stream.ReadByte ());
  1326. stream.Close ();
  1327. }
  1328. }
  1329. [Test]
  1330. [ExpectedException (typeof (NotSupportedException))]
  1331. public void SetLengthWithClosedBaseStream ()
  1332. {
  1333. string fn = Path.Combine (TempFolder, "temp");
  1334. try {
  1335. FileStream fs = new FileStream (fn, FileMode.Create);
  1336. BufferedStream bs = new BufferedStream (fs);
  1337. fs.Close ();
  1338. bs.SetLength (1000);
  1339. } finally {
  1340. File.Delete (fn);
  1341. }
  1342. }
  1343. [Test]
  1344. public void LengthAfterWrite ()
  1345. {
  1346. string path = TempFolder + DSC + "oneofthefilescreated.txt";
  1347. FileStream fs = null;
  1348. DeleteFile (path);
  1349. try {
  1350. fs = new FileStream (path, FileMode.CreateNew);
  1351. fs.WriteByte (Convert.ToByte ('A'));
  1352. byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
  1353. fs.Write (buffer, 0, buffer.Length);
  1354. fs.Seek (0, SeekOrigin.Begin);
  1355. char a = Convert.ToChar (fs.ReadByte ());
  1356. Assert.AreEqual ('A', a, "#A1");
  1357. Assert.AreEqual (23, fs.Length, "#A2");
  1358. int nread = fs.Read (buffer, 0, 5);
  1359. Assert.AreEqual (5, nread, "#A3");
  1360. } finally {
  1361. if (fs != null)
  1362. fs.Close ();
  1363. DeleteFile (path);
  1364. }
  1365. }
  1366. [Category("TargetJvmNotSupported")] // FileOptions.DeleteOnClose not supported for TARGET_JVM
  1367. [Test]
  1368. public void DeleteOnClose ()
  1369. {
  1370. string path = TempFolder + DSC + "created.txt";
  1371. DeleteFile (path);
  1372. FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
  1373. FileOptions.DeleteOnClose);
  1374. Assert.AreEqual (true, File.Exists (path), "DOC#1");
  1375. fs.Close ();
  1376. Assert.AreEqual (false, File.Exists (path), "DOC#2");
  1377. }
  1378. #if !MOBILE
  1379. [Test]
  1380. public void WriteWithExposedHandle ()
  1381. {
  1382. string path = TempFolder + DSC + "exposed-handle.txt";
  1383. FileStream fs1 = null;
  1384. FileStream fs2 = null;
  1385. DeleteFile (path);
  1386. try {
  1387. fs1 = new FileStream (path, FileMode.Create);
  1388. fs2 = new FileStream (fs1.SafeFileHandle, FileAccess.ReadWrite);
  1389. fs1.WriteByte (Convert.ToByte ('H'));
  1390. fs1.WriteByte (Convert.ToByte ('E'));
  1391. fs1.WriteByte (Convert.ToByte ('L'));
  1392. fs2.WriteByte (Convert.ToByte ('L'));
  1393. fs2.WriteByte (Convert.ToByte ('O'));
  1394. long fs1Pos = fs1.Position;
  1395. fs1.Flush ();
  1396. fs2.Flush ();
  1397. fs1.Close ();
  1398. fs2.Close ();
  1399. var check = Encoding.ASCII.GetString (File.ReadAllBytes (path));
  1400. Assert.AreEqual ("HELLO", check, "EXPOSED#1");
  1401. Assert.AreEqual (5, fs1Pos, "EXPOSED#2");
  1402. } finally {
  1403. if (fs1 != null)
  1404. fs1.Close ();
  1405. if (fs2 != null)
  1406. fs2.Close ();
  1407. DeleteFile (path);
  1408. }
  1409. }
  1410. #endif
  1411. }
  1412. }