FileStreamTest.cs 43 KB

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