public void NoAlternateStreamData() { using (var cleaner = new TestFileCleaner()) { NativeMethods.Backup.GetAlternateStreamInformation(cleaner.CreateTestFile("NoAlternateStreamData")).Should().BeEmpty(); } }
public void SetDirectoryTest() { using (var cleaner = new TestFileCleaner()) { NativeMethods.DirectoryManagement.SetCurrentDirectory(cleaner.TempFolder); Directory.GetCurrentDirectory().Should().Be(cleaner.TempFolder); } }
public void SetDirectoryToNonExistant() { using (var cleaner = new TestFileCleaner()) { string directoryPath = Path.Combine(cleaner.TempFolder, Path.GetRandomFileName()); Action action = () => NativeMethods.DirectoryManagement.SetCurrentDirectory(directoryPath); action.ShouldThrow<FileNotFoundException>(); } }
public void DirectoryNotExistsTests() { using (var cleaner = new TestFileCleaner()) { string directoryPath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName()); NativeMethods.FileManagement.FileExists(directoryPath).Should().BeFalse(); NativeMethods.FileManagement.PathExists(directoryPath).Should().BeFalse(); NativeMethods.FileManagement.DirectoryExists(directoryPath).Should().BeFalse(); } }
public void WriteFileBasic(byte[] data) { using (var temp = new TestFileCleaner()) { using (var fileHandle = FileMethods.CreateFile(temp.GetTestPath(), CreationDisposition.CreateNew, DesiredAccess.GenericReadWrite, 0)) { FileMethods.WriteFile(fileHandle, data).Should().Be((uint)data.Length); FileMethods.GetFilePointer(fileHandle).Should().Be(data.Length); } } }
public void CreateLongPathNestedDirectoryTest() { using (var cleaner = new TestFileCleaner(useDotNet: false)) { string longPath = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 300); cleaner.FileService.CreateDirectory(longPath); cleaner.FileService.FileExists(longPath).Should().BeFalse(); cleaner.FileService.DirectoryExists(longPath).Should().BeTrue(); } }
public void CreateDirectoryTest() { using (var cleaner = new TestFileCleaner(useDotNet: false)) { string directoryPath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName()); cleaner.FileService.CreateDirectory(directoryPath); File.Exists(directoryPath).Should().BeFalse(); Directory.Exists(directoryPath).Should().BeTrue(); } }
public void FileExistsTests() { using (var cleaner = new TestFileCleaner()) { string filePath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName()); File.WriteAllText(filePath, "FileExists"); NativeMethods.FileManagement.FileExists(filePath).Should().BeTrue(); NativeMethods.FileManagement.PathExists(filePath).Should().BeTrue(); NativeMethods.FileManagement.DirectoryExists(filePath).Should().BeFalse(); } }
public void OpenFile_LongPath() { // The OpenFile api only supports 128 character paths. using (var cleaner = new TestFileCleaner()) { string path = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 160); FileHelper.WriteAllBytes(path, CompressedFile1); Action action = () => CompressionMethods.LzOpenFile(path); action.ShouldThrow <LzException>().And.Error.Should().Be(LzError.BadInHandle); } }
public void GetDirectoryTest() { using (var cleaner = new TestFileCleaner()) { lock (TestFileCleaner.DirectorySetLock) { Directory.SetCurrentDirectory(cleaner.TempFolder); NativeMethods.DirectoryManagement.GetCurrentDirectory().Should().Be(cleaner.TempFolder); } } }
public void OpenFileDirect_WithSpan() { using var cleaner = new TestFileCleaner(); string path = @"\??\" + cleaner.CreateTestFile(nameof(OpenFileDirect_WithSpan)); string wrongPath = path + "foo"; using (var file = Storage.CreateFileDirect(wrongPath.AsSpan().Slice(0, path.Length), CreateDisposition.Open)) { file.IsInvalid.Should().BeFalse(); } }
public void SetDirectoryTest() { using (var cleaner = new TestFileCleaner()) { lock (TestFileCleaner.DirectorySetLock) { NativeMethods.DirectoryManagement.SetCurrentDirectory(cleaner.TempFolder); Directory.GetCurrentDirectory().Should().Be(cleaner.TempFolder); } } }
public void DirectoryExistsTests() { using (var cleaner = new TestFileCleaner()) { string directoryPath = cleaner.CreateTestDirectory(); NativeMethods.FileManagement.FileExists(directoryPath).Should().BeFalse(); NativeMethods.FileManagement.PathExists(directoryPath).Should().BeTrue(); NativeMethods.FileManagement.DirectoryExists(directoryPath).Should().BeTrue(); } }
public void CreateDirectoryTest() { using (var cleaner = new TestFileCleaner()) { string directoryPath = Path.Combine(cleaner.TempFolder, Path.GetRandomFileName()); NativeMethods.DirectoryManagement.CreateDirectory(directoryPath); File.Exists(directoryPath).Should().BeFalse(); Directory.Exists(directoryPath).Should().BeTrue(); } }
public void CopyFile_NotOverExisting(bool useCreateFile) { using (var cleaner = new TestFileCleaner()) { string source = cleaner.CreateTestFile(CompressedFile2); string destination = cleaner.CreateTestFile($"CopyFile_NotOverExisting({useCreateFile})"); Action action = () => CompressionMethods.LzCopyFile(source, destination, overwrite: false, useCreateFile: useCreateFile); action.ShouldThrow <IOException>().And.HResult.Should().Be((int)ErrorMacros.HRESULT_FROM_WIN32(WindowsError.ERROR_FILE_EXISTS)); } }
public void ReadWriteFileBasic(byte[] data) { using var temp = new TestFileCleaner(); using var fileHandle = Storage.CreateFile(temp.GetTestPath(), CreationDisposition.CreateNew, DesiredAccess.GenericReadWrite, 0); Storage.WriteFile(fileHandle, data).Should().Be((uint)data.Length); Storage.GetFilePointer(fileHandle).Should().Be(data.Length); Storage.SetFilePointer(fileHandle, 0, MoveMethod.Begin); byte[] outBuffer = new byte[data.Length]; Storage.ReadFile(fileHandle, outBuffer).Should().Be((uint)data.Length); outBuffer.Should().BeEquivalentTo(data); }
public void DirectoryNotExistsTests() { using (var cleaner = new TestFileCleaner()) { string directoryPath = cleaner.GetTestPath(); FileMethods.FileExists(directoryPath).Should().BeFalse(); FileMethods.PathExists(directoryPath).Should().BeFalse(); FileMethods.DirectoryExists(directoryPath).Should().BeFalse(); } }
public void DirectoryExistsTests() { using var cleaner = new TestFileCleaner(); string directoryPath = cleaner.GetTestPath(); FileHelper.CreateDirectoryRecursive(directoryPath); Storage.FileExists(directoryPath).Should().BeFalse(); Storage.PathExists(directoryPath).Should().BeTrue(); Storage.DirectoryExists(directoryPath).Should().BeTrue(); }
public void GetFileNameByHandleBasic() { using var cleaner = new TestFileCleaner(); string tempPath = cleaner.TempFolder; string tempFileName = cleaner.GetTestPath(); using var file = Storage.CreateFile(tempFileName, CreationDisposition.CreateNew); string fileName = Storage.GetFileNameLegacy(file); tempFileName.Should().EndWith(fileName); }
public void OpenFileDirect() { using (var cleaner = new TestFileCleaner()) { string path = cleaner.CreateTestFile(nameof(OpenFileDirect)); using (var file = Storage.CreateFileDirect(@"\??\" + path, CreateDisposition.Open)) { file.IsInvalid.Should().BeFalse(); } } }
public void CreateStream() { using (var cleaner = new TestFileCleaner(useDotNet: false)) { string filePath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName()); using (Stream fileStream = cleaner.FileService.CreateFileStream(filePath, FileMode.CreateNew)) { fileStream.Should().NotBeNull(); } } }
public void CopyFile_OverExisting(bool useCreateFile) { using (var cleaner = new TestFileCleaner()) { string source = cleaner.CreateTestFile(CompressedFile2); string destination = cleaner.CreateTestFile($"CopyFile_OverExisting({useCreateFile})"); CompressionMethods.LzCopyFile(source, destination, overwrite: true, useCreateFile: useCreateFile).Should().Be(563); FileHelper.ReadAllText(destination).Should().Be(CompressedContent2); } }
public void FileModeSynchronousFile() { using var cleaner = new TestFileCleaner(); string filePath = cleaner.GetTestPath(); using var file = Storage.CreateFile(filePath, CreationDisposition.CreateNew, DesiredAccess.GenericReadWrite, 0); file.IsInvalid.Should().BeFalse(); var mode = Storage.GetFileMode(file); mode.Should().HaveFlag(FileAccessModes.SynchronousNotAlertable); }
public void FileExistsTests() { using (var cleaner = new TestFileCleaner()) { string filePath = cleaner.GetTestPath(); FileHelper.WriteAllText(filePath, "FileExists"); FileMethods.FileExists(filePath).Should().BeTrue(); FileMethods.PathExists(filePath).Should().BeTrue(); FileMethods.DirectoryExists(filePath).Should().BeFalse(); } }
public void EncryptFile_Basic() { using (var cleaner = new TestFileCleaner()) { string path = cleaner.CreateTestFile(nameof(EncryptFile_Basic)); FileHelper.ReadAllText(path).Should().Be(nameof(EncryptFile_Basic)); Storage.EncryptFile(path); FileHelper.ReadAllText(path).Should().Be(nameof(EncryptFile_Basic)); Storage.DecryptFile(path); FileHelper.ReadAllText(path).Should().Be(nameof(EncryptFile_Basic)); } }
public void CreateDirectoryBasic() { using (var temp = new TestFileCleaner()) { string directoryPath = temp.GetTestPath(); Storage.CreateDirectory(directoryPath); using (var directory = Storage.CreateDirectoryHandle(directoryPath)) { directory.IsInvalid.Should().BeFalse(); } } }
public void CreateFileTests(string fileName) { using (var cleaner = new TestFileCleaner()) { string filePath = Paths.Combine(cleaner.TempFolder, fileName); using (var handle = NativeMethods.FileManagement.CreateFile(filePath, FileAccess.ReadWrite, FileShare.ReadWrite, FileMode.Create, 0)) { handle.IsInvalid.Should().BeFalse(); NativeMethods.FileManagement.FileExists(filePath).Should().BeTrue(); } } }
public void ExpandedNameEx(string compressedName, byte character, string expandedName) { using (var cleaner = new TestFileCleaner()) { byte[] data = new byte[CompressedFile1.Length]; CompressedFile1.CopyTo(data, 0); data[9] = character; string path = Path.Join(cleaner.TempFolder, compressedName); FileHelper.WriteAllBytes(path, data); Path.GetFileName(Compression.GetExpandedNameEx(path)).Should().Be(expandedName); } }
public void CreateFileUnextendedTests(string fileName) { using (var cleaner = new TestFileCleaner()) { string filePath = Paths.Combine(cleaner.TempFolder, fileName); using (var handle = FileMethods.CreateFile(filePath, CreationDisposition.CreateNew)) { handle.IsInvalid.Should().BeFalse(); FileMethods.FileExists(filePath).Should().BeTrue(); } } }
public void CreateStreamsWithDifficultNames(string fileName) { using (var cleaner = new TestFileCleaner(useDotNet: false)) { string filePath = Paths.Combine(cleaner.TempFolder, fileName); using (Stream fileStream = cleaner.FileService.CreateFileStream(filePath, FileMode.CreateNew)) { fileStream.Should().NotBeNull(); cleaner.FileService.FileExists(filePath).Should().BeTrue(); } } }
public void CopyFile(bool useCreateFile) { using (var cleaner = new TestFileCleaner()) { string source = cleaner.CreateTestFile(CompressedFile2); string destination = cleaner.GetTestPath(); Compression.LzCopyFile(source, destination, false, useCreateFile).Should().Be(563); FileHelper.ReadAllText(destination).Should().Be(CompressedContent2); } }
public void LongPathFileNotExistsTests() { using (var cleaner = new TestFileCleaner()) { string longPath = @"\\?\" + PathGenerator.CreatePathOfLength(cleaner.TempFolder, 500); string filePath = cleaner.GetTestPath(); FileMethods.FileExists(filePath).Should().BeFalse(); FileMethods.PathExists(filePath).Should().BeFalse(); FileMethods.DirectoryExists(filePath).Should().BeFalse(); } }
public void FindFileEmptyFolder(string pattern) { using (var temp = new TestFileCleaner()) { string subdir = Paths.Combine(temp.TempFolder, "Subdir"); DirectoryMethods.CreateDirectory(subdir); FileMethods.CreateFindOperation(subdir, nameFilter: pattern, findTransform: FindTransforms.ToFileName.Instance, findFilter: FindFilters.All.Instance) .Should().Contain(new string[] { ".", ".." }); } }
public void ProcessWithActiveHandle() { using (var cleaner = new TestFileCleaner()) { const int MAX_TRIES = 10; // It is tricky to get an open handle conflict- give it a few tries for (int i = 0; i < MAX_TRIES; i++) { string testFile = cleaner.CreateTestFile(nameof(ProcessWithActiveHandle)); // Create an open handle on the test file. Note that this must come first as // the redirect (>>) will refuse to execute if there is already an open handle. Process process = null; ProcessStartInfo startInfo = new ProcessStartInfo { FileName = "cmd", Arguments = $"/K copy con >> {testFile}", WindowStyle = ProcessWindowStyle.Hidden }; try { process = Process.Start(startInfo); // The existing cmd handle won't let us access much using (var handle = Storage.CreateFile(testFile, CreationDisposition.OpenExisting, DesiredAccess.ReadAttributes, ShareModes.ReadWrite | ShareModes.Delete)) { var ids = Storage.GetProcessIds(handle); if (ids.Count() == 0) { continue; } // If this ends up being problematic (i.e. antivirus gets us a count of 2) // we can look to see we have an id that matches the known process and that we // don't contain our own process id. ids.Should().HaveCount(1); ((int)(ulong)ids.First()).Should().Be(process.Id); return; } } finally { process?.CloseMainWindow(); } } false.Should().BeTrue($"Did not get a locked file in {MAX_TRIES} tries."); } }
public void LockedFileDirectoryDeletion() { using (var cleaner = new TestFileCleaner()) { string directory = cleaner.GetTestPath(); DirectoryMethods.CreateDirectory(directory); FileMethods.DirectoryExists(directory).Should().BeTrue(); string file = cleaner.CreateTestFile(nameof(LockedFileDirectoryDeletion), directory); using (var handle = FileMethods.CreateFile(file, CreationDisposition.OpenExisting, DesiredAccess.GenericRead, ShareModes.ReadWrite | ShareModes.Delete)) { handle.IsInvalid.Should().BeFalse(); // Mark the file for deletion FileMethods.DeleteFile(file); // RemoveDirectory API call will throw Action action = () => DirectoryMethods.RemoveDirectory(directory); action.ShouldThrow <WInteropIOException>().And.HResult.Should().Be((int)ErrorMacros.HRESULT_FROM_WIN32(WindowsError.ERROR_DIR_NOT_EMPTY)); // Opening the directory for deletion will succeed, but have no impact using (var directoryHandle = FileMethods.CreateFile( directory, CreationDisposition.OpenExisting, DesiredAccess.ListDirectory | DesiredAccess.Delete, ShareModes.ReadWrite | ShareModes.Delete, FileAttributes.None, FileFlags.BackupSemantics | FileFlags.DeleteOnClose)) { directoryHandle.IsInvalid.Should().BeFalse(); } } // File will be gone now that the handle is closed FileMethods.FileExists(file).Should().BeFalse(); // But the directory will still exist as it doesn't respect DeleteOnClose with an open handle when it is closed FileMethods.DirectoryExists(directory).Should().BeTrue(); // Create a handle to the directory again with DeleteOnClose and it will actually delete the directory using (var directoryHandle = FileMethods.CreateFile( directory, CreationDisposition.OpenExisting, DesiredAccess.ListDirectory | DesiredAccess.Delete, ShareModes.ReadWrite | ShareModes.Delete, FileAttributes.None, FileFlags.BackupSemantics | FileFlags.DeleteOnClose)) { directoryHandle.IsInvalid.Should().BeFalse(); } FileMethods.DirectoryExists(directory).Should().BeFalse(); } }
public void OneAlternateDataStream() { using (var cleaner = new TestFileCleaner()) { string testFile = cleaner.CreateTestFile("OneAlternateDataStream"); string firstStream = testFile + ":First"; cleaner.FileService.WriteAllText(firstStream, "First alternate data stream"); var info = NativeMethods.Backup.GetAlternateStreamInformation(testFile); info.Should().HaveCount(1); info.First().Name.Should().Be(":First:$DATA"); } }
public void CreateFile_LongPath() { // Unlike OpenFile, CreateFile handles > 128 character paths. using (var cleaner = new TestFileCleaner()) { string path = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 160); FileHelper.WriteAllBytes(path, CompressedFile1); using (var handle = Compression.LzCreateFile(path)) { handle.RawHandle.Should().BeGreaterThan(0); } } }
public void CreateFile_OverMaxPathLongPath() { // Unlike OpenFile, CreateFile handles > 128 character paths. Unfortunately it is // constrained by MAX_PATH internal buffers, so it cant go over 260. using (var cleaner = new TestFileCleaner()) { string path = @"\\?\" + PathGenerator.CreatePathOfLength(cleaner.TempFolder, 300); FileHelper.EnsurePathDirectoryExists(path); FileHelper.WriteAllBytes(path, CompressedFile1); Action action = () => Compression.LzCreateFile(path); action.Should().Throw <LzException>().And.Error.Should().Be(LzError.BadValue); } }
public void AddGroup_NoWriteDac() { using (var cleaner = new TestFileCleaner()) { using (var handle = Storage.CreateFile(cleaner.GetTestPath(), CreationDisposition.CreateNew, DesiredAccess.GenericReadWrite)) { handle.IsInvalid.Should().BeFalse(); SID usersGroup = Security.CreateWellKnownSid(WellKnownSID.Users); Action action = () => handle.ChangeAccess(usersGroup, FileAccessRights.GenericRead, AccessMode.Grant); action.Should().Throw <UnauthorizedAccessException>(); } } }
public void LoadStringFromLongPath() { using (var cleaner = new TestFileCleaner(useDotNet: false)) { string longPath = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 500); cleaner.FileService.CreateDirectory(longPath); string longPathLibrary = Paths.Combine(longPath, "LoadStringFromLongPath.dll"); cleaner.FileService.CopyFile(GetNativeTestLibraryLocation(), longPathLibrary); longPathLibrary = Paths.AddExtendedPrefix(longPathLibrary); using (var handle = NativeMethods.LoadLibrary(longPathLibrary, LoadLibraryFlags.LOAD_LIBRARY_AS_IMAGE_RESOURCE | LoadLibraryFlags.LOAD_LIBRARY_AS_DATAFILE)) { string resource = NativeMethods.LoadString(handle, 101); resource.Should().Be("Test"); } } }
public void FileInfoHasCanonicalPaths() { using (var cleaner = new TestFileCleaner(useDotNet: false)) { string filePath = cleaner.GetTestPath() + "UPPER"; cleaner.FileService.WriteAllText(filePath, "FileInfoHasCanonicalPaths"); var info = cleaner.FileService.GetPathInfo(filePath.ToLowerInvariant()); info.Path.Should().Be(filePath); } }
public void OpenNonExistantStream(string appendix) { using (var cleaner = new TestFileCleaner(useDotNet: false)) { string filePath = cleaner.GetTestPath() + appendix; Action action = () => cleaner.FileService.CreateFileStream(filePath, FileMode.Open); action.ShouldThrow<FileNotFoundException>(); } }
public void CreateLongPathStream() { using (var cleaner = new TestFileCleaner(useDotNet: false)) { string longPath = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 300); cleaner.FileService.CreateDirectory(longPath); string filePath = Paths.Combine(longPath, Path.GetRandomFileName()); using (Stream fileStream = cleaner.FileService.CreateFileStream(filePath, FileMode.CreateNew)) { fileStream.Should().NotBeNull(); } } }
public void CreateSymbolicLinkToLongPathFile() { using (var cleaner = new TestFileCleaner()) { string filePath = cleaner.CreateTestFile("CreateSymbolicLinkToLongPathFile"); string symbolicLink = cleaner.GetTestPath(); Action action = () => NativeMethods.FileManagement.CreateSymbolicLink(symbolicLink, filePath); if (cleaner.ExtendedFileService.CanCreateSymbolicLinks()) { action(); var attributes = NativeMethods.FileManagement.GetFileAttributes(symbolicLink); attributes.Should().HaveFlag(FileAttributes.ReparsePoint); } else { action.ShouldThrow<IOException>().And.HResult.Should().Be(NativeErrorHelper.GetHResultForWindowsError(NativeMethods.WinError.ERROR_PRIVILEGE_NOT_HELD)); } } }
public void CreateFileExtendedTests(string fileName) { using (var cleaner = new TestFileCleaner()) { string filePath = Paths.AddExtendedPrefix(Paths.Combine(cleaner.TempFolder, fileName), addIfUnderLegacyMaxPath: true); using (var handle = NativeMethods.FileManagement.CreateFile(filePath, FileAccess.ReadWrite, FileShare.ReadWrite, FileMode.Create, 0)) { handle.IsInvalid.Should().BeFalse(); NativeMethods.FileManagement.FlushFileBuffers(handle); NativeMethods.FileManagement.FileExists(filePath).Should().BeTrue(); } } }
public void WriteAndReadAlternateStreams() { using (var cleaner = new TestFileCleaner(useDotNet: false)) { string directoryPath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName()); Directory.CreateDirectory(directoryPath); string filePath = Paths.Combine(directoryPath, Path.GetRandomFileName()); using (Stream fileStream = cleaner.FileService.CreateFileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite)) { } for (int i = 0; i < 3; i++) { string streamPath = $"{filePath}:Stream{i}:$DATA"; using (Stream fileStream = cleaner.FileService.CreateFileStream(streamPath, FileMode.CreateNew, FileAccess.ReadWrite)) { string testString = $"This is test string {i}."; fileStream.Should().NotBeNull(); StreamWriter writer = new StreamWriter(fileStream); writer.WriteLine(testString); writer.Flush(); fileStream.Position = 0; StreamReader reader = new StreamReader(fileStream); string readLine = reader.ReadLine(); readLine.Should().Be(testString); } } var directoryInfo = cleaner.FileService.GetPathInfo(directoryPath) as IDirectoryInformation; directoryInfo.Should().NotBeNull(); directoryInfo.EnumerateChildren().Should().HaveCount(1); } }
public void FinalPathNameLongPathPrefixRoundTripBehavior() { using (var cleaner = new TestFileCleaner(useDotNet: false)) { string longPath = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 500); string filePath = Paths.Combine(longPath, Path.GetRandomFileName()); cleaner.FileService.CreateDirectory(longPath); cleaner.FileService.WriteAllText(filePath, "FinalPathNameLongPathPrefixRoundTripBehavior"); using (var handle = NativeMethods.FileManagement.CreateFile(filePath, FileAccess.Read, FileShare.ReadWrite, FileMode.Open, 0)) { handle.IsInvalid.Should().BeFalse(); string extendedPath = Paths.AddExtendedPrefix(filePath, addIfUnderLegacyMaxPath: true); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.FILE_NAME_NORMALIZED) .Should().Be(extendedPath); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.FILE_NAME_OPENED) .Should().Be(extendedPath); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.VOLUME_NAME_DOS) .Should().Be(extendedPath); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.VOLUME_NAME_GUID) .Should().StartWith(@"\\?\Volume"); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.VOLUME_NAME_NT) .Should().StartWith(@"\Device\"); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.VOLUME_NAME_NONE) .Should().Be(filePath.Substring(2)); } } }
public void AttributesForNonExistantLongPath() { using (var cleaner = new TestFileCleaner()) { string longPath = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 500); FileAttributes attributes; NativeMethods.FileManagement.TryGetFileAttributes(longPath, out attributes).Should().BeFalse(); attributes.Should().Be(NativeMethods.FileManagement.InvalidFileAttributes); } }
public void FinalPathNameBehavior() { using (var cleaner = new TestFileCleaner()) { string filePath = cleaner.CreateTestFile("FinalPathNameBehavior"); using (var handle = NativeMethods.FileManagement.CreateFile(filePath.ToLower(), FileAccess.Read, FileShare.ReadWrite, FileMode.Open, 0)) { handle.IsInvalid.Should().BeFalse(); string extendedPath = Paths.AddExtendedPrefix(filePath, addIfUnderLegacyMaxPath: true); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.FILE_NAME_NORMALIZED) .Should().Be(extendedPath); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.FILE_NAME_OPENED) .Should().Be(extendedPath); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.VOLUME_NAME_DOS) .Should().Be(extendedPath); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.VOLUME_NAME_GUID) .Should().StartWith(@"\\?\Volume"); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.VOLUME_NAME_NT) .Should().StartWith(@"\Device\"); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.VOLUME_NAME_NONE) .Should().Be(filePath.Substring(2)); } } }
public void FinalPathNameVolumeNameBehavior() { // This test is asserting that the original volume name has nothing to do with the volume GetFinalPathNameByHandle returns using (var cleaner = new TestFileCleaner()) { string filePath = cleaner.CreateTestFile("FinalPathNameVolumeNameBehavior"); string canonicalRoot = cleaner.ExtendedFileService.GetCanonicalRoot(cleaner.FileService, filePath); string replacedPath = Paths.ReplaceRoot(canonicalRoot, filePath); using (var handle = NativeMethods.FileManagement.CreateFile(replacedPath.ToLower(), FileAccess.Read, FileShare.ReadWrite, FileMode.Open, 0)) { handle.IsInvalid.Should().BeFalse(); string extendedPath = Paths.AddExtendedPrefix(filePath, addIfUnderLegacyMaxPath: true); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.FILE_NAME_NORMALIZED) .Should().Be(extendedPath); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.FILE_NAME_OPENED) .Should().Be(extendedPath); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.VOLUME_NAME_DOS) .Should().Be(extendedPath); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.VOLUME_NAME_GUID) .Should().StartWith(@"\\?\Volume"); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.VOLUME_NAME_NT) .Should().StartWith(@"\Device\"); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.VOLUME_NAME_NONE) .Should().Be(filePath.Substring(2)); } } }
public void FinalPathNameLinkBehavior() { var extendedFileService = new ExtendedFileService(); if (!extendedFileService.CanCreateSymbolicLinks()) return; // GetFinalPathName always points to the linked file unless you specifically open the reparse point using (var cleaner = new TestFileCleaner()) { string filePath = Paths.Combine(cleaner.TempFolder, "Target"); string extendedPath = Paths.AddExtendedPrefix(filePath, addIfUnderLegacyMaxPath: true); cleaner.FileService.WriteAllText(filePath, "CreateSymbolicLinkToFile"); string symbolicLink = Paths.Combine(cleaner.TempFolder, "Link"); string extendedLink = Paths.AddExtendedPrefix(symbolicLink, addIfUnderLegacyMaxPath: true); NativeMethods.FileManagement.CreateSymbolicLink(symbolicLink, filePath); NativeMethods.FileManagement.FileExists(symbolicLink).Should().BeTrue("symbolic link should exist"); // GetFinalPathName should normalize the casing, pushing ToUpper to validate using (var handle = NativeMethods.FileManagement.CreateFile(symbolicLink.ToUpperInvariant(), FileAccess.Read, FileShare.ReadWrite, FileMode.Open, 0)) { handle.IsInvalid.Should().BeFalse(); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.FILE_NAME_NORMALIZED) .Should().Be(extendedPath); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.FILE_NAME_OPENED) .Should().Be(extendedPath); } using (var handle = NativeMethods.FileManagement.CreateFile(symbolicLink.ToUpperInvariant(), FileAccess.Read, FileShare.ReadWrite, FileMode.Open, NativeMethods.FileManagement.AllFileAttributeFlags.FILE_FLAG_OPEN_REPARSE_POINT)) { handle.IsInvalid.Should().BeFalse(); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.FILE_NAME_NORMALIZED) .Should().Be(extendedLink); NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.FILE_NAME_OPENED) .Should().Be(extendedLink); } } }
public void WriteAndReadLongPathStream() { using (var cleaner = new TestFileCleaner(useDotNet: false)) { string longPath = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 300); cleaner.FileService.CreateDirectory(longPath); string filePath = Paths.Combine(longPath, Path.GetRandomFileName()); using (Stream fileStream = cleaner.FileService.CreateFileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite)) { fileStream.Should().NotBeNull(); StreamWriter writer = new StreamWriter(fileStream); writer.WriteLine("This is a test string."); writer.Flush(); fileStream.Position = 0; StreamReader reader = new StreamReader(fileStream); string readLine = reader.ReadLine(); readLine.Should().Be("This is a test string."); } } }
public void LongPathFileNotExistsTests() { using (var cleaner = new TestFileCleaner()) { string longPath = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 500); string filePath = cleaner.GetTestPath(); NativeMethods.FileManagement.FileExists(filePath).Should().BeFalse(); NativeMethods.FileManagement.PathExists(filePath).Should().BeFalse(); NativeMethods.FileManagement.DirectoryExists(filePath).Should().BeFalse(); } }
public void WriteAndReadStream(string appendix) { using (var cleaner = new TestFileCleaner(useDotNet: false)) { string filePath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName() + appendix); using (Stream fileStream = cleaner.FileService.CreateFileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite)) { fileStream.Should().NotBeNull(); StreamWriter writer = new StreamWriter(fileStream); writer.WriteLine("This is a test string."); writer.Flush(); fileStream.Position = 0; StreamReader reader = new StreamReader(fileStream); string readLine = reader.ReadLine(); readLine.Should().Be("This is a test string."); } } }
public void FileTypeOfFile() { using (var cleaner = new TestFileCleaner()) { using (var testFile = NativeMethods.FileManagement.CreateFile( cleaner.GetTestPath(), FileAccess.ReadWrite, FileShare.ReadWrite, FileMode.Create, 0)) { NativeMethods.FileManagement.GetFileType(testFile).Should().Be(NativeMethods.FileManagement.FileType.FILE_TYPE_DISK); } } }