FileStreamTest.cs 46 KB

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