/MicroFrameworkPK_v4_1/Test/Platform/Tests/CLR/System/IO/File/Create.cs

https://bitbucket.org/pmfsampaio/netmf-lpc · C# · 314 lines · 268 code · 37 blank · 9 comment · 27 complexity · dda7a29b23250ad6f36f0de844c99c1d MD5 · raw file

  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4. using System;
  5. using System.IO;
  6. using Microsoft.SPOT;
  7. using Microsoft.SPOT.IO;
  8. using Microsoft.SPOT.Platform.Test;
  9. namespace Microsoft.SPOT.Platform.Tests
  10. {
  11. public class Create : IMFTestInterface
  12. {
  13. [SetUp]
  14. public InitializeResult Initialize()
  15. {
  16. // These tests rely on underlying file system so we need to make
  17. // sure we can format it before we start the tests. If we can't
  18. // format it, then we assume there is no FS to test on this platform.
  19. // delete the directory DOTNETMF_FS_EMULATION
  20. try
  21. {
  22. IOTests.IntializeVolume();
  23. Directory.CreateDirectory(sourceDir);
  24. Directory.CreateDirectory("Test " + sourceDir);
  25. Directory.SetCurrentDirectory(sourceDir);
  26. }
  27. catch (Exception ex)
  28. {
  29. Log.Comment("Skipping: Unable to initialize file system" + ex.StackTrace);
  30. return InitializeResult.Skip;
  31. }
  32. return InitializeResult.ReadyToGo;
  33. }
  34. [TearDown]
  35. public void CleanUp()
  36. {
  37. }
  38. #region Local vars
  39. private const string file1Name = "file1.tmp";
  40. private const string sourceDir = "source";
  41. #endregion Local vars
  42. #region Helper methods
  43. private bool TestCreate(string file)
  44. {
  45. return TestCreate(file, 1000);
  46. }
  47. private bool TestCreate(string file, int buffer)
  48. {
  49. bool success = true;
  50. Log.Comment("Create " + file + " of size " + buffer);
  51. if (File.Exists(file))
  52. {
  53. Log.Exception("Test space dirty, cleaning up!");
  54. File.Delete(file);
  55. }
  56. string dir = Path.GetDirectoryName(Path.GetFullPath(file));
  57. if ((dir != null) && (dir.Length > 0))
  58. Directory.CreateDirectory(dir);
  59. FileStream fs = null;
  60. try
  61. {
  62. fs = File.Create(file, buffer);
  63. if (!File.Exists(file))
  64. {
  65. Log.Exception("Could not find file after creation!");
  66. success = false;
  67. }
  68. if (fs.Length != 0)
  69. {
  70. Log.Exception("Incorrect file length == " + fs.Length);
  71. success = false;
  72. }
  73. if (fs.Position != 0)
  74. {
  75. Log.Exception("Incorrect file postion == " + fs.Position);
  76. success = false;
  77. }
  78. }
  79. finally
  80. {
  81. if (fs != null)
  82. fs.Close();
  83. File.Delete(file);
  84. }
  85. return success;
  86. }
  87. #endregion Helper methods
  88. #region Test Cases
  89. [TestMethod]
  90. public MFTestResults ArgumentExceptionTests()
  91. {
  92. MFTestResults result = MFTestResults.Pass;
  93. try
  94. {
  95. Log.Comment("Current Directory: " + Directory.GetCurrentDirectory());
  96. try
  97. {
  98. Log.Comment("Null Constructor");
  99. FileStream fs = File.Create(null);
  100. result = MFTestResults.Fail;
  101. Log.Exception("Expected ArgumentException");
  102. fs.Close();
  103. }
  104. catch (ArgumentException) { /* pass case */ }
  105. try
  106. {
  107. Log.Comment("String.Empty Constructor");
  108. FileStream fs = File.Create(string.Empty);
  109. result = MFTestResults.Fail;
  110. Log.Exception("Expected ArgumentException");
  111. fs.Close();
  112. }
  113. catch (ArgumentException) { /* pass case */ }
  114. try
  115. {
  116. Log.Comment("Whitespace Constructor");
  117. FileStream fs = File.Create(" ");
  118. result = MFTestResults.Fail;
  119. Log.Exception("Expected ArgumentException");
  120. fs.Close();
  121. }
  122. catch (ArgumentException) { /* pass case */ }
  123. try
  124. {
  125. Log.Comment("Negative buffer Constructor");
  126. FileStream fs = File.Create(file1Name, -10);
  127. result = MFTestResults.Fail;
  128. Log.Exception("Expected ArgumentException");
  129. fs.Close();
  130. }
  131. catch (ArgumentException) { /* pass case */ }
  132. try
  133. {
  134. Log.Comment("Current dir '.' Constructor");
  135. FileStream fs = File.Create(".");
  136. result = MFTestResults.Fail;
  137. Log.Exception("Expected ArgumentException");
  138. fs.Close();
  139. }
  140. catch (IOException) { /* pass case */ } // UnauthorizedAccess
  141. }
  142. catch (Exception ex)
  143. {
  144. Log.Exception("Unexpected exception: " + ex.Message);
  145. result = MFTestResults.Fail;
  146. }
  147. return result;
  148. }
  149. [TestMethod]
  150. public MFTestResults ValidCases()
  151. {
  152. MFTestResults result = MFTestResults.Pass;
  153. string file1Dir1 = Path.Combine(Path.Combine(IOTests.Volume.RootDirectory, sourceDir), file1Name);
  154. string filedirspace = Path.Combine(Path.Combine(IOTests.Volume.RootDirectory, "Test " + sourceDir), "Test " + file1Name);
  155. try
  156. {
  157. Log.Comment("relative create");
  158. if (!TestCreate(file1Name))
  159. result = MFTestResults.Fail;
  160. Log.Comment("absolute create");
  161. if (!TestCreate(file1Dir1))
  162. result = MFTestResults.Fail;
  163. Log.Comment("elative .. Create");
  164. if (!TestCreate(@"..\" + file1Name))
  165. result = MFTestResults.Fail;
  166. Log.Comment("relative . Create");
  167. if (!TestCreate(@".\" + file1Name))
  168. result = MFTestResults.Fail;
  169. Log.Comment("Create at root");
  170. if (!TestCreate(Path.Combine(IOTests.Volume.RootDirectory, file1Name)))
  171. result = MFTestResults.Fail;
  172. Log.Comment("white space in file name");
  173. if (!TestCreate(@"test " + file1Name))
  174. result = MFTestResults.Fail;
  175. Log.Comment("white space in path & file name");
  176. if (!TestCreate(filedirspace))
  177. result = MFTestResults.Fail;
  178. Log.Comment("max int buffer");
  179. if (!TestCreate(file1Name, int.MaxValue))
  180. result = MFTestResults.Fail;
  181. Log.Comment("1 int buffer");
  182. if (!TestCreate(file1Name, 1))
  183. result = MFTestResults.Fail;
  184. Log.Comment("0 int buffer");
  185. if (!TestCreate(file1Name, 0))
  186. result = MFTestResults.Fail;
  187. }
  188. catch (Exception ex)
  189. {
  190. Log.Exception("Unexpected exception: " + ex.Message);
  191. result = MFTestResults.Fail;
  192. }
  193. return result;
  194. }
  195. [TestMethod]
  196. public MFTestResults CaseInsensitive()
  197. {
  198. MFTestResults result = MFTestResults.Pass;
  199. string file1Dir1 = Path.Combine(Path.Combine(IOTests.Volume.RootDirectory, sourceDir.ToLower()), file1Name.ToLower());
  200. string filedirspace = Path.Combine(Path.Combine(IOTests.Volume.RootDirectory, "Test " + sourceDir.ToUpper()), "Test " + file1Name.ToUpper());
  201. try
  202. {
  203. // to lower
  204. if (!TestCreate(file1Dir1))
  205. result = MFTestResults.Fail;
  206. // to upper
  207. if (!TestCreate(filedirspace))
  208. result = MFTestResults.Fail;
  209. }
  210. catch (Exception ex)
  211. {
  212. Log.Exception("Unexpected exception: " + ex.Message);
  213. result = MFTestResults.Fail;
  214. }
  215. return result;
  216. }
  217. [TestMethod]
  218. public MFTestResults SpecialFileNames()
  219. {
  220. MFTestResults result = MFTestResults.Pass;
  221. char[] special = new char[] { '!', '#', '$', '%', '\'', '(', ')', '+', '-', '.', '@', '[', ']', '_', '`', '{', '}', '~' };
  222. try
  223. {
  224. Log.Comment("Create file each with special char file names");
  225. for (int i = 0; i < special.Length; i++)
  226. {
  227. string file = i + "_" + new string(new char[] { special[i] }) + "_z.file";
  228. if (!TestCreate(file))
  229. result = MFTestResults.Fail;
  230. }
  231. }
  232. catch (Exception ex)
  233. {
  234. Log.Exception("Unexpected exception: " + ex.Message);
  235. result = MFTestResults.Fail;
  236. }
  237. return result;
  238. }
  239. [TestMethod]
  240. public MFTestResults InvalidPathChars()
  241. {
  242. MFTestResults result = MFTestResults.Pass;
  243. try
  244. {
  245. foreach (char invalidChar in Path.GetInvalidPathChars())
  246. {
  247. try
  248. {
  249. Log.Comment("Invalid char ascii val = " + (int)invalidChar);
  250. string file = new string(new char[] { 'b', 'a', 'd', invalidChar, 'f', 'i', 'l', 'e', invalidChar, '.', 't', 'x', 't' });
  251. FileStream fs = File.Create(file);
  252. if (invalidChar == 0)
  253. {
  254. Log.Exception("Known failure for null");
  255. result = MFTestResults.KnownFailure;
  256. }
  257. else
  258. {
  259. result = MFTestResults.Fail;
  260. Log.Exception("Expected Argument exception for '" + file + "' but got: '" + fs.Name + "'");
  261. }
  262. fs.Close();
  263. File.Delete(file);
  264. }
  265. catch (ArgumentException) { /* Pass Case */ }
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. Log.Exception("Unexpected exception: " + ex.Message);
  271. result = MFTestResults.Fail;
  272. }
  273. return result;
  274. }
  275. #endregion Test Cases
  276. }
  277. }