PageRenderTime 3690ms CodeModel.GetById 45ms RepoModel.GetById 1ms app.codeStats 0ms

/src/System.IO.FileSystem/tests/PortedCommon/IOInputs.cs

https://gitlab.com/0072016/0072016-corefx-
C# | 268 lines | 220 code | 29 blank | 19 comment | 1 complexity | 3f8af97531d8a24e32605333b01258ea MD5 | raw file
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. internal static class IOInputs
  10. {
  11. // see: http://msdn.microsoft.com/en-us/library/aa365247.aspx
  12. private static readonly char[] s_invalidFileNameChars = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
  13. new char[] { '\"', '<', '>', '|', '\0', (Char)1, (Char)2, (Char)3, (Char)4, (Char)5, (Char)6, (Char)7, (Char)8, (Char)9, (Char)10, (Char)11, (Char)12, (Char)13, (Char)14, (Char)15, (Char)16, (Char)17, (Char)18, (Char)19, (Char)20, (Char)21, (Char)22, (Char)23, (Char)24, (Char)25, (Char)26, (Char)27, (Char)28, (Char)29, (Char)30, (Char)31, '*', '?' } :
  14. new char[] { '\0' };
  15. public static bool SupportsSettingCreationTime { get { return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); } }
  16. public static bool SupportsGettingCreationTime { get { return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) | RuntimeInformation.IsOSPlatform(OSPlatform.OSX); } }
  17. // Max path length (minus trailing \0). Unix values vary system to system; just using really long values here likely to be more than on the average system.
  18. public static readonly int MaxPath = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 259 : 10000;
  19. // Same as MaxPath on Unix
  20. public static readonly int MaxLongPath = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? MaxExtendedPath : MaxPath;
  21. // Windows specific, this is the maximum length that can be passed to APIs taking directory names, such as Directory.CreateDirectory & Directory.Move.
  22. // Does not include the trailing \0.
  23. // We now do the appropriate wrapping to allow creating longer directories. Like MaxPath, this is a legacy restriction.
  24. public static readonly int MaxDirectory = 247;
  25. // Windows specific, this is the maximum length that can be passed using extended syntax. Does not include the trailing \0.
  26. public static readonly int MaxExtendedPath = short.MaxValue - 1;
  27. public const int MaxComponent = 255;
  28. public const string ExtendedPrefix = @"\\?\";
  29. public const string ExtendedUncPrefix = @"\\?\UNC\";
  30. public static IEnumerable<string> GetValidPathComponentNames()
  31. {
  32. yield return Path.GetRandomFileName();
  33. yield return "!@#$%^&";
  34. yield return "\x65e5\x672c\x8a9e";
  35. yield return "A";
  36. yield return " A";
  37. yield return " A";
  38. yield return "FileName";
  39. yield return "FileName.txt";
  40. yield return " FileName";
  41. yield return " FileName.txt";
  42. yield return " FileName";
  43. yield return " FileName.txt";
  44. yield return "This is a valid component name";
  45. yield return "This is a valid component name.txt";
  46. yield return "V1.0.0.0000";
  47. }
  48. public static IEnumerable<string> GetControlWhiteSpace()
  49. {
  50. yield return "\t";
  51. yield return "\t\t";
  52. yield return "\t\t\t";
  53. yield return "\n";
  54. yield return "\n\n";
  55. yield return "\n\n\n";
  56. yield return "\t\n";
  57. yield return "\t\n\t\n";
  58. yield return "\n\t\n";
  59. yield return "\n\t\n\t";
  60. }
  61. public static IEnumerable<string> GetSimpleWhiteSpace()
  62. {
  63. yield return " ";
  64. yield return " ";
  65. yield return " ";
  66. yield return " ";
  67. yield return " ";
  68. }
  69. public static IEnumerable<string> GetWhiteSpace()
  70. {
  71. return GetControlWhiteSpace().Concat(GetSimpleWhiteSpace());
  72. }
  73. public static IEnumerable<string> GetUncPathsWithoutShareName()
  74. {
  75. foreach (char slash in new[] { Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar })
  76. {
  77. string slashes = new string(slash, 2);
  78. yield return slashes;
  79. yield return slashes + " ";
  80. yield return slashes + new string(slash, 5);
  81. yield return slashes + "S";
  82. yield return slashes + "S ";
  83. yield return slashes + "LOCALHOST";
  84. yield return slashes + "LOCALHOST " + slash;
  85. yield return slashes + "LOCALHOST " + new string(slash, 2);
  86. yield return slashes + "LOCALHOST" + slash + " ";
  87. yield return slashes + "LOCALHOST" + slash + slash + " ";
  88. }
  89. }
  90. public static IEnumerable<string> GetPathsWithReservedDeviceNames()
  91. {
  92. string root = Path.GetPathRoot(Directory.GetCurrentDirectory());
  93. foreach (string deviceName in GetReservedDeviceNames())
  94. {
  95. yield return deviceName;
  96. yield return Path.Combine(root, deviceName);
  97. yield return Path.Combine(root, "Directory", deviceName);
  98. yield return Path.Combine(new string(Path.DirectorySeparatorChar, 2), "LOCALHOST", deviceName);
  99. }
  100. }
  101. public static IEnumerable<string> GetPathsWithAlternativeDataStreams()
  102. {
  103. yield return @"AA:";
  104. yield return @"AAA:";
  105. yield return @"AA:A";
  106. yield return @"AAA:A";
  107. yield return @"AA:AA";
  108. yield return @"AAA:AA";
  109. yield return @"AA:AAA";
  110. yield return @"AAA:AAA";
  111. yield return @"AA:FileName";
  112. yield return @"AAA:FileName";
  113. yield return @"AA:FileName.txt";
  114. yield return @"AAA:FileName.txt";
  115. yield return @"A:FileName.txt:";
  116. yield return @"AA:FileName.txt:AA";
  117. yield return @"AAA:FileName.txt:AAA";
  118. yield return @"C:\:";
  119. yield return @"C:\:FileName";
  120. yield return @"C:\:FileName.txt";
  121. yield return @"C:\fileName:";
  122. yield return @"C:\fileName:FileName.txt";
  123. yield return @"C:\fileName:FileName.txt:";
  124. yield return @"C:\fileName:FileName.txt:AA";
  125. yield return @"C:\fileName:FileName.txt:AAA";
  126. yield return @"ftp://fileName:FileName.txt:AAA";
  127. }
  128. public static IEnumerable<string> GetPathsWithInvalidColons()
  129. {
  130. // Windows specific. We document that these return NotSupportedException.
  131. yield return @":";
  132. yield return @" :";
  133. yield return @" :";
  134. yield return @"C::";
  135. yield return @"C::FileName";
  136. yield return @"C::FileName.txt";
  137. yield return @"C::FileName.txt:";
  138. yield return @"C::FileName.txt::";
  139. yield return @":f";
  140. yield return @":filename";
  141. yield return @"file:";
  142. yield return @"file:file";
  143. yield return @"http:";
  144. yield return @"http:/";
  145. yield return @"http://";
  146. yield return @"http://www";
  147. yield return @"http://www.microsoft.com";
  148. yield return @"http://www.microsoft.com/index.html";
  149. yield return @"http://server";
  150. yield return @"http://server/";
  151. yield return @"http://server/home";
  152. yield return @"file://";
  153. yield return @"file:///C|/My Documents/ALetter.html";
  154. }
  155. public static IEnumerable<string> GetPathsWithInvalidCharacters()
  156. {
  157. // NOTE: That I/O treats "file"/http" specially and throws ArgumentException.
  158. // Otherwise, it treats all other urls as alternative data streams
  159. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // alternate data streams, drive labels, etc.
  160. {
  161. yield return "\0";
  162. yield return "middle\0path";
  163. yield return "trailing\0";
  164. // TODO: #6931 Add these back in some fashion
  165. //yield return @"\\?\";
  166. //yield return @"\\?\UNC\";
  167. //yield return @"\\?\UNC\LOCALHOST";
  168. }
  169. else
  170. {
  171. yield return "\0";
  172. yield return "middle\0path";
  173. yield return "trailing\0";
  174. }
  175. foreach (char c in s_invalidFileNameChars)
  176. {
  177. yield return c.ToString();
  178. }
  179. }
  180. public static IEnumerable<string> GetPathsWithComponentLongerThanMaxComponent()
  181. {
  182. // While paths themselves can be up to and including 32,000 characters, most volumes
  183. // limit each component of the path to a total of 255 characters.
  184. string component = new string('s', MaxComponent + 1);
  185. yield return String.Format(@"C:\{0}", component);
  186. yield return String.Format(@"C:\{0}\Filename.txt", component);
  187. yield return String.Format(@"C:\{0}\Filename.txt\", component);
  188. yield return String.Format(@"\\{0}\Share", component);
  189. yield return String.Format(@"\\LOCALHOST\{0}", component);
  190. yield return String.Format(@"\\LOCALHOST\{0}\FileName.txt", component);
  191. yield return String.Format(@"\\LOCALHOST\Share\{0}", component);
  192. }
  193. public static IEnumerable<string> GetPathsLongerThanMaxDirectory(string rootPath)
  194. {
  195. yield return GetLongPath(rootPath, MaxDirectory + 1);
  196. yield return GetLongPath(rootPath, MaxDirectory + 2);
  197. yield return GetLongPath(rootPath, MaxDirectory + 3);
  198. }
  199. public static IEnumerable<string> GetPathsLongerThanMaxPath(string rootPath, bool useExtendedSyntax = false)
  200. {
  201. yield return GetLongPath(rootPath, MaxPath + 1, useExtendedSyntax);
  202. yield return GetLongPath(rootPath, MaxPath + 2, useExtendedSyntax);
  203. yield return GetLongPath(rootPath, MaxPath + 3, useExtendedSyntax);
  204. }
  205. public static IEnumerable<string> GetPathsLongerThanMaxLongPath(string rootPath, bool useExtendedSyntax = false)
  206. {
  207. yield return GetLongPath(rootPath, MaxExtendedPath + 1 - (useExtendedSyntax ? 0 : ExtendedPrefix.Length), useExtendedSyntax);
  208. yield return GetLongPath(rootPath, MaxExtendedPath + 2 - (useExtendedSyntax ? 0 : ExtendedPrefix.Length), useExtendedSyntax);
  209. }
  210. private static string GetLongPath(string rootPath, int characterCount, bool extended = false)
  211. {
  212. return IOServices.GetPath(rootPath, characterCount, extended).FullPath;
  213. }
  214. public static IEnumerable<string> GetReservedDeviceNames()
  215. { // See: http://msdn.microsoft.com/en-us/library/aa365247.aspx
  216. yield return "CON";
  217. yield return "AUX";
  218. yield return "NUL";
  219. yield return "PRN";
  220. yield return "COM1";
  221. yield return "COM2";
  222. yield return "COM3";
  223. yield return "COM4";
  224. yield return "COM5";
  225. yield return "COM6";
  226. yield return "COM7";
  227. yield return "COM8";
  228. yield return "COM9";
  229. yield return "LPT1";
  230. yield return "LPT2";
  231. yield return "LPT3";
  232. yield return "LPT4";
  233. yield return "LPT5";
  234. yield return "LPT6";
  235. yield return "LPT7";
  236. yield return "LPT8";
  237. yield return "LPT9";
  238. }
  239. }