[PlatformSpecific(TestPlatforms.Windows)] // Uses P/Invokes to get short path name public static void GetFullPath_Windows_83Paths() { // Create a temporary file name with a name longer than 8.3 such that it'll need to be shortened. string tempFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".txt"); File.Create(tempFilePath).Dispose(); try { // Get its short name var sb = new StringBuilder(260); if (GetShortPathName(tempFilePath, sb, sb.Capacity) > 0) // only proceed if we could successfully create the short name { string shortName = sb.ToString(); // Make sure the shortened name expands back to the original one // Sometimes shortening or GetFullPath is changing the casing of "temp" on some test machines: normalize both sides tempFilePath = Regex.Replace(tempFilePath, @"\\temp\\", @"\TEMP\", RegexOptions.IgnoreCase); shortName = Regex.Replace(Path.GetFullPath(shortName), @"\\temp\\", @"\TEMP\", RegexOptions.IgnoreCase); Assert.Equal(tempFilePath, shortName); // Should work with device paths that aren't well-formed extended syntax if (!PathFeatures.IsUsingLegacyPathNormalization()) { Assert.Equal(@"\\.\" + tempFilePath, Path.GetFullPath(@"\\.\" + shortName)); Assert.Equal(@"\\?\" + tempFilePath, Path.GetFullPath(@"//?/" + shortName)); // Shouldn't mess with well-formed extended syntax Assert.Equal(@"\\?\" + shortName, Path.GetFullPath(@"\\?\" + shortName)); } // Validate case where short name doesn't expand to a real file string invalidShortName = @"S:\DOESNT~1\USERNA~1.RED\LOCALS~1\Temp\bg3ylpzp"; Assert.Equal(invalidShortName, Path.GetFullPath(invalidShortName)); // Same thing, but with a long path that normalizes down to a short enough one const int Iters = 1000; var shortLongName = new StringBuilder(invalidShortName, invalidShortName.Length + (Iters * 2)); for (int i = 0; i < Iters; i++) { shortLongName.Append(Path.DirectorySeparatorChar).Append('.'); } Assert.Equal(invalidShortName, Path.GetFullPath(shortLongName.ToString())); } } finally { File.Delete(tempFilePath); } }
public void PathWithInvalidCharactersAsPath_ThrowsArgumentException(string invalidPath) { if (invalidPath.Equals(@"\\?\") && !PathFeatures.IsUsingLegacyPathNormalization()) { AssertExtensions.ThrowsAny <IOException, UnauthorizedAccessException>(() => Create(invalidPath)); } else if (invalidPath.Contains(@"\\?\") && !PathFeatures.IsUsingLegacyPathNormalization()) { Assert.Throws <DirectoryNotFoundException>(() => Create(invalidPath)); } else { Assert.Throws <ArgumentException>(() => Create(invalidPath)); } }
public IEnumerable <object> RecursiveDepthData() { yield return(10); // Length of the path can be 260 characters on netfx. if (PathFeatures.AreAllLongPathsAvailable()) { yield return(100); // Most Unix distributions have a maximum path length of 1024 characters (1024 UTF-8 bytes). if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { yield return(1000); } } }
public void PathWithIllegalCharacters(string invalidPath) { FileInfo testFile = new FileInfo(GetTestFilePath()); testFile.Create().Dispose(); // Under legacy normalization we kick \\?\ paths back as invalid with ArgumentException // New style we don't prevalidate \\?\ at all if (invalidPath.Contains(@"\\?\") && !PathFeatures.IsUsingLegacyPathNormalization()) { Assert.Throws <IOException>(() => Move(testFile.FullName, invalidPath)); } else { Assert.Throws <ArgumentException>(() => Move(testFile.FullName, invalidPath)); } }
public void PathWithInvalidColons_Throws_Desktop(string invalidPath) { if (PathFeatures.IsUsingLegacyPathNormalization()) { Assert.Throws <ArgumentException>(() => Create(invalidPath)); } else { if (invalidPath.Contains('|')) { Assert.Throws <ArgumentException>(() => Create(invalidPath)); } else { Assert.Throws <NotSupportedException>(() => Create(invalidPath)); } } }
public void GetFullPath_ValidExtendedPaths(string path) { if (PathFeatures.IsUsingLegacyPathNormalization()) { // Legacy Path doesn't support any of these paths. AssertExtensions.ThrowsAny <ArgumentException, NotSupportedException>(() => Path.GetFullPath(path)); return; } // None of these should throw if (path.StartsWith(@"\\?\")) { Assert.Equal(path, Path.GetFullPath(path)); } else { Path.GetFullPath(path); } }
public void WindowsPathWithIllegalColons_Desktop(string invalidPath) { FileInfo testFile = new FileInfo(GetTestFilePath()); testFile.Create().Dispose(); if (PathFeatures.IsUsingLegacyPathNormalization()) { Assert.Throws <ArgumentException>(() => Move(testFile.FullName, invalidPath)); } else { if (invalidPath.Contains('|')) { Assert.Throws <ArgumentException>(() => Move(testFile.FullName, invalidPath)); } else { Assert.Throws <NotSupportedException>(() => Move(testFile.FullName, invalidPath)); } } }
public void GetFullPath_NormalizedLongPathTooLong() { // Try out a long path that normalizes down to more than MaxPath string curDir = Directory.GetCurrentDirectory(); const int Iters = 260; var longPath = new StringBuilder(curDir, curDir.Length + (Iters * 4)); for (int i = 0; i < Iters; i++) { longPath.Append(Path.DirectorySeparatorChar).Append('a').Append(Path.DirectorySeparatorChar).Append('.'); } if (PathFeatures.AreAllLongPathsAvailable()) { // Now no longer throws unless over ~32K Assert.NotNull(Path.GetFullPath(longPath.ToString())); } else { Assert.Throws <PathTooLongException>(() => Path.GetFullPath(longPath.ToString())); } }
public static void GetFullPath_Windows_UNC_Valid_LegacyPathSupport(string path, string normalExpected, string legacyExpected) { string expected = PathFeatures.IsUsingLegacyPathNormalization() ? legacyExpected : normalExpected; Assert.Equal(expected, Path.GetFullPath(path)); }