FileTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. //
  2. // FileTest.cs: Test cases for System.IO.File
  3. //
  4. // Author: Duncan Mak ([email protected])
  5. //
  6. // (C) 2002 Ximian, Inc. http://www.ximian.com
  7. //
  8. using NUnit.Framework;
  9. using System;
  10. using System.IO;
  11. namespace MonoTests.System.IO
  12. {
  13. public class FileTest : TestCase
  14. {
  15. protected override void SetUp ()
  16. {
  17. File.Delete ("resources" + Path.DirectorySeparatorChar + "baz");
  18. File.Delete ("resources" + Path.DirectorySeparatorChar + "bar");
  19. File.Delete ("resources" + Path.DirectorySeparatorChar + "foo");
  20. }
  21. protected override void TearDown ()
  22. {
  23. File.Delete ("resources" + Path.DirectorySeparatorChar + "baz");
  24. File.Delete ("resources" + Path.DirectorySeparatorChar + "bar");
  25. File.Delete ("resources" + Path.DirectorySeparatorChar + "foo");
  26. }
  27. public void TestExists ()
  28. {
  29. int i = 0;
  30. try {
  31. Assert ("null filename should not exist", !File.Exists (null));
  32. i++;
  33. Assert ("empty filename should not exist", !File.Exists (""));
  34. i++;
  35. Assert ("whitespace filename should not exist", !File.Exists (" \t\t \t \n\t\n \n"));
  36. i++;
  37. Assert ("File resources" + Path.DirectorySeparatorChar + "AFile.txt should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
  38. i++;
  39. Assert ("File resources" + Path.DirectorySeparatorChar + "doesnotexist should not exist", !File.Exists ("resources" + Path.DirectorySeparatorChar + "doesnotexist"));
  40. } catch (Exception e) {
  41. Fail ("Unexpected exception at i = " + i + ". e=" + e);
  42. }
  43. }
  44. public void TestCreate ()
  45. {
  46. FileStream stream;
  47. /* exception test: File.Create(null) */
  48. try {
  49. stream = File.Create (null);
  50. Fail ("File.Create(null) should throw ArgumentNullException");
  51. } catch (ArgumentNullException) {
  52. // do nothing, this is what we expect
  53. } catch (Exception e) {
  54. Fail ("File.Create(null) unexpected exception caught: e=" + e.ToString());
  55. }
  56. /* exception test: File.Create("") */
  57. try {
  58. stream = File.Create ("");
  59. Fail ("File.Create('') should throw ArgumentException");
  60. } catch (ArgumentException) {
  61. // do nothing, this is what we expect
  62. } catch (Exception e) {
  63. Fail ("File.Create('') unexpected exception caught: e=" + e.ToString());
  64. }
  65. /* exception test: File.Create(" ") */
  66. try {
  67. stream = File.Create (" ");
  68. Fail ("File.Create(' ') should throw ArgumentException");
  69. } catch (ArgumentException) {
  70. // do nothing, this is what we expect
  71. } catch (Exception e) {
  72. Fail ("File.Create(' ') unexpected exception caught: e=" + e.ToString());
  73. }
  74. /* exception test: File.Create(directory_not_found) */
  75. try {
  76. stream = File.Create ("directory_does_not_exist" + Path.DirectorySeparatorChar + "foo");
  77. Fail ("File.Create(directory_does_not_exist) should throw DirectoryNotFoundException");
  78. } catch (DirectoryNotFoundException) {
  79. // do nothing, this is what we expect
  80. } catch (Exception e) {
  81. Fail ("File.Create(directory_does_not_exist) unexpected exception caught: e=" + e.ToString());
  82. }
  83. /* positive test: create resources/foo */
  84. try {
  85. stream = File.Create ("resources" + Path.DirectorySeparatorChar + "foo");
  86. Assert ("File should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
  87. stream.Close ();
  88. } catch (Exception e) {
  89. Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
  90. }
  91. /* positive test: repeat test above again to test for overwriting file */
  92. try {
  93. stream = File.Create ("resources" + Path.DirectorySeparatorChar + "foo");
  94. Assert ("File should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
  95. stream.Close ();
  96. } catch (Exception e) {
  97. Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
  98. }
  99. }
  100. public void TestCopy ()
  101. {
  102. /* exception test: File.Copy(null, b) */
  103. try {
  104. File.Copy (null, "b");
  105. Fail ("File.Copy(null, 'b') should throw ArgumentNullException");
  106. } catch (ArgumentNullException) {
  107. // do nothing, this is what we expect
  108. } catch (Exception e) {
  109. Fail ("File.Copy(null, 'b') unexpected exception caught: e=" + e.ToString());
  110. }
  111. /* exception test: File.Copy(a, null) */
  112. try {
  113. File.Copy ("a", null);
  114. Fail ("File.Copy('a', null) should throw ArgumentNullException");
  115. } catch (ArgumentNullException) {
  116. // do nothing, this is what we expect
  117. } catch (Exception e) {
  118. Fail ("File.Copy('a', null) unexpected exception caught: e=" + e.ToString());
  119. }
  120. /* exception test: File.Copy("", b) */
  121. try {
  122. File.Copy ("", "b");
  123. Fail ("File.Copy('', 'b') should throw ArgumentException");
  124. } catch (ArgumentException) {
  125. // do nothing, this is what we expect
  126. } catch (Exception e) {
  127. Fail ("File.Copy('', 'b') unexpected exception caught: e=" + e.ToString());
  128. }
  129. /* exception test: File.Copy(a, "") */
  130. try {
  131. File.Copy ("a", "");
  132. Fail ("File.Copy('a', '') should throw ArgumentException");
  133. } catch (ArgumentException) {
  134. // do nothing, this is what we expect
  135. } catch (Exception e) {
  136. Fail ("File.Copy('a', '') unexpected exception caught: e=" + e.ToString());
  137. }
  138. /* exception test: File.Copy(" ", b) */
  139. try {
  140. File.Copy (" ", "b");
  141. Fail ("File.Copy(' ', 'b') should throw ArgumentException");
  142. } catch (ArgumentException) {
  143. // do nothing, this is what we expect
  144. } catch (Exception e) {
  145. Fail ("File.Copy(' ', 'b') unexpected exception caught: e=" + e.ToString());
  146. }
  147. /* exception test: File.Copy(a, " ") */
  148. try {
  149. File.Copy ("a", " ");
  150. Fail ("File.Copy('a', ' ') should throw ArgumentException");
  151. } catch (ArgumentException) {
  152. // do nothing, this is what we expect
  153. } catch (Exception e) {
  154. Fail ("File.Copy('a', ' ') unexpected exception caught: e=" + e.ToString());
  155. }
  156. /* exception test: File.Copy(doesnotexist, b) */
  157. try {
  158. File.Copy ("doesnotexist", "b");
  159. Fail ("File.Copy('doesnotexist', 'b') should throw FileNotFoundException");
  160. } catch (FileNotFoundException) {
  161. // do nothing, this is what we expect
  162. } catch (Exception e) {
  163. Fail ("File.Copy('doesnotexist', 'b') unexpected exception caught: e=" + e.ToString());
  164. }
  165. /* positive test: copy resources/AFile.txt to resources/bar */
  166. try {
  167. File.Delete ("resources" + Path.DirectorySeparatorChar + "bar");
  168. File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar");
  169. Assert ("File AFile.txt should still exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
  170. Assert ("File bar should exist after File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  171. } catch (Exception e) {
  172. Fail ("#1 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
  173. }
  174. /* exception test: File.Copy(resources/AFile.txt, resources/bar) (default is overwrite == false) */
  175. try {
  176. File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar");
  177. Fail ("File.Copy('resources/AFile.txt', 'resources/bar') should throw IOException");
  178. } catch (IOException) {
  179. // do nothing, this is what we expect
  180. } catch (Exception e) {
  181. Fail ("#2 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
  182. }
  183. /* positive test: copy resources/AFile.txt to resources/bar, overwrite */
  184. try {
  185. Assert ("File bar should exist before File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  186. File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar", true);
  187. Assert ("File AFile.txt should still exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
  188. Assert ("File bar should exist after File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  189. } catch (Exception e) {
  190. Fail ("File.Copy('resources/AFile.txt', 'resources/bar', true) unexpected exception caught: e=" + e.ToString());
  191. }
  192. }
  193. public void TestDelete ()
  194. {
  195. /* exception test: File.Delete(null) */
  196. try {
  197. File.Delete (null);
  198. Fail ("File.Delete(null) should throw ArgumentNullException");
  199. } catch (ArgumentNullException) {
  200. // do nothing, this is what we expect
  201. } catch (Exception e) {
  202. Fail ("File.Delete(null) unexpected exception caught: e=" + e.ToString());
  203. }
  204. /* exception test: File.Delete("") */
  205. try {
  206. File.Delete ("");
  207. Fail ("File.Delete('') should throw ArgumentException");
  208. } catch (ArgumentException) {
  209. // do nothing, this is what we expect
  210. } catch (Exception e) {
  211. Fail ("File.Delete('') unexpected exception caught: e=" + e.ToString());
  212. }
  213. /* exception test: File.Delete(" ") */
  214. try {
  215. File.Delete (" ");
  216. Fail ("File.Delete(' ') should throw ArgumentException");
  217. } catch (ArgumentException) {
  218. // do nothing, this is what we expect
  219. } catch (Exception e) {
  220. Fail ("File.Delete(' ') unexpected exception caught: e=" + e.ToString());
  221. }
  222. /* exception test: File.Delete(directory_not_found) */
  223. try {
  224. File.Delete ("directory_does_not_exist" + Path.DirectorySeparatorChar + "foo");
  225. Fail ("File.Delete(directory_does_not_exist) should throw DirectoryNotFoundException");
  226. } catch (DirectoryNotFoundException) {
  227. // do nothing, this is what we expect
  228. } catch (Exception e) {
  229. Fail ("File.Delete(directory_does_not_exist) unexpected exception caught: e=" + e.ToString());
  230. }
  231. if (!File.Exists ("resources" + Path.DirectorySeparatorChar + "foo")) {
  232. FileStream f = File.Create("resources" + Path.DirectorySeparatorChar + "foo");
  233. f.Close();
  234. }
  235. Assert ("File resources" + Path.DirectorySeparatorChar + "foo should exist for TestDelete to succeed", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
  236. try {
  237. File.Delete ("resources" + Path.DirectorySeparatorChar + "foo");
  238. } catch (Exception e) {
  239. Fail ("Unable to delete resources" + Path.DirectorySeparatorChar + "foo: e=" + e.ToString());
  240. }
  241. Assert ("File resources" + Path.DirectorySeparatorChar + "foo should not exist after File.Delete", !File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
  242. }
  243. public void TestMove ()
  244. {
  245. /* exception test: File.Move(null, b) */
  246. try {
  247. File.Move (null, "b");
  248. Fail ("File.Move(null, 'b') should throw ArgumentNullException");
  249. } catch (ArgumentNullException) {
  250. // do nothing, this is what we expect
  251. } catch (Exception e) {
  252. Fail ("File.Move(null, 'b') unexpected exception caught: e=" + e.ToString());
  253. }
  254. /* exception test: File.Move(a, null) */
  255. try {
  256. File.Move ("a", null);
  257. Fail ("File.Move('a', null) should throw ArgumentNullException");
  258. } catch (ArgumentNullException) {
  259. // do nothing, this is what we expect
  260. } catch (Exception e) {
  261. Fail ("File.Move('a', null) unexpected exception caught: e=" + e.ToString());
  262. }
  263. /* exception test: File.Move("", b) */
  264. try {
  265. File.Move ("", "b");
  266. Fail ("File.Move('', 'b') should throw ArgumentException");
  267. } catch (ArgumentException) {
  268. // do nothing, this is what we expect
  269. } catch (Exception e) {
  270. Fail ("File.Move('', 'b') unexpected exception caught: e=" + e.ToString());
  271. }
  272. /* exception test: File.Move(a, "") */
  273. try {
  274. File.Move ("a", "");
  275. Fail ("File.Move('a', '') should throw ArgumentException");
  276. } catch (ArgumentException) {
  277. // do nothing, this is what we expect
  278. } catch (Exception e) {
  279. Fail ("File.Move('a', '') unexpected exception caught: e=" + e.ToString());
  280. }
  281. /* exception test: File.Move(" ", b) */
  282. try {
  283. File.Move (" ", "b");
  284. Fail ("File.Move(' ', 'b') should throw ArgumentException");
  285. } catch (ArgumentException) {
  286. // do nothing, this is what we expect
  287. } catch (Exception e) {
  288. Fail ("File.Move(' ', 'b') unexpected exception caught: e=" + e.ToString());
  289. }
  290. /* exception test: File.Move(a, " ") */
  291. try {
  292. File.Move ("a", " ");
  293. Fail ("File.Move('a', ' ') should throw ArgumentException");
  294. } catch (ArgumentException) {
  295. // do nothing, this is what we expect
  296. } catch (Exception e) {
  297. Fail ("File.Move('a', ' ') unexpected exception caught: e=" + e.ToString());
  298. }
  299. /* exception test: File.Move(doesnotexist, b) */
  300. try {
  301. File.Move ("doesnotexist", "b");
  302. Fail ("File.Move('doesnotexist', 'b') should throw FileNotFoundException");
  303. } catch (FileNotFoundException) {
  304. // do nothing, this is what we expect
  305. } catch (Exception e) {
  306. Fail ("File.Move('doesnotexist', 'b') unexpected exception caught: e=" + e.ToString());
  307. }
  308. /* exception test: File.Move(resources/foo, doesnotexist/b) */
  309. File.Copy("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "foo", true);
  310. try {
  311. File.Move ("resources" + Path.DirectorySeparatorChar + "foo", "doesnotexist" + Path.DirectorySeparatorChar + "b");
  312. Fail ("File.Move('resources/foo', 'b') should throw DirectoryNotFoundException");
  313. } catch (DirectoryNotFoundException) {
  314. // do nothing, this is what we expect
  315. } catch (FileNotFoundException) {
  316. // LAMESPEC
  317. // do nothing, this is (kind of) what we expect
  318. } catch (Exception e) {
  319. Fail ("File.Move('resources/foo', 'doesnotexist/b') unexpected exception caught: e=" + e.ToString());
  320. }
  321. /* exception test: File.Move(doesnotexist/foo, b) */
  322. try {
  323. File.Move ("doesnotexist" + Path.DirectorySeparatorChar + "foo", "b");
  324. Fail ("File.Move('doesnotexist/foo', 'b') should throw DirectoryNotFoundException");
  325. } catch (DirectoryNotFoundException) {
  326. // do nothing, this is what we expect
  327. } catch (FileNotFoundException) {
  328. // LAMESPEC
  329. // do nothing, this is (kind of) what we expect
  330. } catch (Exception e) {
  331. Fail ("File.Move('doesnotexist/foo', 'b') unexpected exception caught: e=" + e.ToString());
  332. }
  333. /* exception test: File.Move(resources/foo, resources) */
  334. try {
  335. File.Move ("resources" + Path.DirectorySeparatorChar + "foo", "resources");
  336. Fail ("File.Move('resources/foo', 'resources') should throw IOException");
  337. } catch (IOException) {
  338. // do nothing, this is what we expect
  339. } catch (Exception e) {
  340. Fail ("File.Move('resources/foo', 'resources') unexpected exception caught: e=" + e.ToString());
  341. }
  342. /* positive test: File.Move(a, a) shouldn't throw exception */
  343. try {
  344. File.Move ("resources" + Path.DirectorySeparatorChar + "foo", "resources" + Path.DirectorySeparatorChar + "foo");
  345. } catch (Exception e) {
  346. Fail ("File.Move('doesnotexist/foo', 'b') unexpected exception caught: e=" + e.ToString());
  347. }
  348. if (!File.Exists ("resources" + Path.DirectorySeparatorChar + "bar")) {
  349. FileStream f = File.Create("resources" + Path.DirectorySeparatorChar + "bar");
  350. f.Close();
  351. }
  352. Assert ("File resources" + Path.DirectorySeparatorChar + "bar should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  353. File.Move ("resources" + Path.DirectorySeparatorChar + "bar", "resources" + Path.DirectorySeparatorChar + "baz");
  354. Assert ("File resources" + Path.DirectorySeparatorChar + "bar should not exist", !File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  355. Assert ("File resources" + Path.DirectorySeparatorChar + "baz should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "baz"));
  356. }
  357. public void TestOpen ()
  358. {
  359. try {
  360. FileStream stream = File.Open ("resources" + Path.DirectorySeparatorChar + "AFile.txt", FileMode.Open);
  361. stream.Close ();
  362. } catch (Exception e) {
  363. Fail ("Unable to open resources" + Path.DirectorySeparatorChar + "AFile.txt: e=" + e.ToString());
  364. }
  365. /* Exception tests */
  366. try {
  367. FileStream stream = File.Open ("filedoesnotexist", FileMode.Open);
  368. Fail ("File 'filedoesnotexist' should not exist");
  369. } catch (FileNotFoundException) {
  370. // do nothing, this is what we expect
  371. } catch (Exception e) {
  372. Fail ("Unexpect exception caught: e=" + e.ToString());
  373. }
  374. }
  375. [ExpectedException(typeof(IOException))]
  376. public void TestGetCreationTime ()
  377. {
  378. string path = "resources" + Path.DirectorySeparatorChar + "baz";
  379. FileStream stream = File.Create (path);
  380. Assert ("GetCreationTime incorrect", (DateTime.Now - File.GetCreationTime (path)).TotalSeconds < 10);
  381. // Test nonexistent files
  382. string path2 = "resources" + Path.DirectorySeparatorChar + "filedoesnotexist";
  383. // should throw an exception
  384. File.GetCreationTime (path2);
  385. }
  386. }
  387. }