private void File_Delete_NonExistingDriveLetter_ThrowsDirectoryNotFoundException(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var folder = Alphaleonis.Win32.Filesystem.DriveInfo.GetFreeDriveLetter() + @":\NonExistingDriveLetter";

            if (isNetwork)
            {
                folder = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(folder);
            }

            Console.WriteLine("Input File Path: [{0}]", folder);

            UnitTestAssert.ThrowsException <System.IO.DirectoryNotFoundException>(() => System.IO.File.Delete(folder));

            UnitTestAssert.ThrowsException <System.IO.DirectoryNotFoundException>(() => Alphaleonis.Win32.Filesystem.File.Delete(folder));
        }
        public void AlphaFS_Host_DriveConnection_Network_Success()
        {
            UnitTestConstants.PrintUnitTestHeader(true);
            Console.WriteLine();


            var share = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(UnitTestConstants.TempFolder);

            using (var connection = new Alphaleonis.Win32.Network.DriveConnection(share))
            {
                Console.WriteLine("Using DriveConnection(): [{0}] to: [{1}]", connection.LocalName, share);

                UnitTestConstants.Dump(connection, -9);

                Assert.AreEqual(share, connection.Share);
            }
        }
        private void File_Move_CatchDeviceNotReadyException_NonExistingDestinationLogicalDrive(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);
            Console.WriteLine();


            var gotException = false;


            var nonExistingDriveLetter = Alphaleonis.Win32.Filesystem.DriveInfo.GetFreeDriveLetter();

            var srcFile = UnitTestConstants.SysDrive + @"\NonExisting Source File";
            var dstFile = nonExistingDriveLetter + @":\NonExisting Destination File";

            if (isNetwork)
            {
                srcFile = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(srcFile);
                dstFile = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(dstFile);
            }

            Console.WriteLine("Src File Path: [{0}]", srcFile);
            Console.WriteLine("Dst File Path: [{0}]", dstFile);


            try
            {
                Alphaleonis.Win32.Filesystem.File.Move(srcFile, dstFile);
            }
            catch (Exception ex)
            {
                var exType = ex.GetType();

                gotException = exType == typeof(Alphaleonis.Win32.Filesystem.DeviceNotReadyException);

                Console.WriteLine("\n\tCaught {0} Exception: [{1}] {2}", gotException ? "EXPECTED" : "UNEXPECTED", exType.Name, ex.Message);
            }


            Assert.IsTrue(gotException, "The exception is not caught, but is expected to.");

            Assert.IsFalse(System.IO.Directory.Exists(dstFile), "The file exists, but is expected not to.");


            Console.WriteLine();
        }
Пример #4
0
        private void DumpGetDriveFormat(bool isLocal)
        {
            Console.WriteLine("=== TEST {0} ===\n", isLocal ? "LOCAL" : "NETWORK");

            var cnt = 0;

            // Get Logical Drives from Environment.
            foreach (var drv in Directory.GetLogicalDrives(false, true))
            {
                UnitTestConstants.StopWatcher(true);

                try
                {
                    // GetDriveType() can read an empty cdrom drive.
                    // SetCurrentDirectory() wil fail on an empty cdrom drive.
                    System.IO.Directory.SetCurrentDirectory(drv);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\nCaught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
                }

                var drive = isLocal ? drv : Path.LocalToUnc(drv);

                UnitTestConstants.StopWatcher(true);
                var driveInfo = new DriveInfo(drive);

                Console.WriteLine("#{0:000}\tInput Path   : [{1}]", ++cnt, drive);
                Console.WriteLine("\tGetDriveFormat(): [{0}]", driveInfo.DriveFormat);
                Console.WriteLine("\tGetDriveType()  : [{0}]", driveInfo.DriveType);
                Console.WriteLine("\tIsReady()       : [{0}]", driveInfo.IsReady);
                Console.WriteLine("\tIsVolume()      : [{0}]\n", driveInfo.IsVolume);

                if (isLocal)
                {
                    Assert.AreEqual(new System.IO.DriveInfo(drive).IsReady, driveInfo.IsReady, "IsReady AlphaFS != System.IO");
                }
            }
            Console.WriteLine("{0}\n\n", UnitTestConstants.Reporter(true));

            if (cnt == 0)
            {
                Assert.Inconclusive("Nothing was enumerated, but it was expected.");
            }
        }
        private void Path_LocalToUnc_MappedDriveWithSubFolder()
        {
            UnitTestConstants.PrintUnitTestHeader(false);
            Console.WriteLine();


            var backslash       = Alphaleonis.Win32.Filesystem.Path.DirectorySeparator;
            var hostName        = Alphaleonis.Win32.Network.Host.GetUncName() + backslash;
            var localTempFolder = UnitTestConstants.TempFolder;
            var netTempFolder   = localTempFolder.Replace(":", Alphaleonis.Win32.Filesystem.Path.NetworkDriveSeparator);
            var subFolderName   = MethodBase.GetCurrentMethod().Name;
            var subFolderPath   = hostName + netTempFolder + backslash + subFolderName;


            string[] localToUncPaths =
            {
                localTempFolder,
                hostName + netTempFolder,
                subFolderPath,
                Alphaleonis.Win32.Filesystem.Path.GetLongPath(subFolderPath)
            };


            using (var connection = new Alphaleonis.Win32.Network.DriveConnection(localToUncPaths[1]))
            {
                var driveLetter = connection.LocalName;
                var subFolder   = driveLetter + backslash + subFolderName;

                var uncPath     = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(subFolder);
                var uncLongPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(subFolder, Alphaleonis.Win32.Filesystem.GetFullPathOptions.AsLongPath);


                Console.WriteLine("\tMapped letter: [{0}] to: [{1}]", driveLetter, connection.Share);
                Console.WriteLine();

                Console.WriteLine("\tSub Folder Path         : [{0}]", subFolder);
                Console.WriteLine("\tUNC Sub Folder Path     : [{0}]", uncPath);
                Console.WriteLine("\tUNC Sub Folder Long Path: [{0}]", uncLongPath);
                Console.WriteLine();


                Assert.AreEqual(localToUncPaths[2], uncPath);
                Assert.AreEqual(localToUncPaths[3], uncLongPath);
            }
        }
Пример #6
0
        private void File_Move_ExistingFile(bool isNetwork)
        {
            using (var tempRoot = new TemporaryDirectory(isNetwork))
            {
                var srcFile = tempRoot.CreateFile();

                var dstFile = System.IO.Path.Combine(tempRoot.Directory.FullName, srcFile.Name + "-Moved.File");

                var fileLength = srcFile.Length;

                Console.WriteLine("Src File Path: [{0}] [{1}]", Alphaleonis.Utils.UnitSizeToText(srcFile.Length), srcFile.FullName);
                Console.WriteLine("Dst File Path: [{0}]", dstFile);


                var moveResult = Alphaleonis.Win32.Filesystem.File.Move(srcFile.FullName, dstFile, Alphaleonis.Win32.Filesystem.PathFormat.FullPath);

                UnitTestConstants.Dump(moveResult);


                // Test against moveResult results.

                Assert.IsFalse(moveResult.IsCopy);
                Assert.IsTrue(moveResult.IsMove);
                Assert.IsFalse(moveResult.IsDirectory);
                Assert.IsTrue(moveResult.IsFile);

                Assert.AreEqual(fileLength, moveResult.TotalBytes);

                Assert.AreEqual(1, moveResult.TotalFiles);
                Assert.AreEqual(0, moveResult.TotalFolders);


                Assert.IsFalse(System.IO.File.Exists(srcFile.FullName), "The file does exists, but is expected not to.");
                Assert.IsTrue(System.IO.File.Exists(dstFile), "The file does not exists, but is expected to.");

                var fileLen = new System.IO.FileInfo(dstFile).Length;
                Assert.AreEqual(fileLength, fileLen, "The file copy is: {0} bytes, but is expected to be: {1} bytes.", fileLen, fileLength);


                Assert.IsFalse(System.IO.File.Exists(srcFile.FullName), "The original file exists, but is expected not to.");
            }


            Console.WriteLine();
        }
        private void DirectoryInfo_FolderName255Characters(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, MethodBase.GetCurrentMethod().Name))
            {
                var folder = rootDir.Directory.FullName;


                Console.WriteLine("\nInput Directory Path: [{0}]\n", folder);


                // System.IO: 244, anything higher throws System.IO.PathTooLongException: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
                // AlphaFS  : 255, anything higher throws System.IO.PathTooLongException.
                var subFolder = new string('b', 255);


                var local = Alphaleonis.Win32.Filesystem.Path.Combine(folder, subFolder);
                var unc   = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(local);
                Console.WriteLine("SubFolder length: {0}, total path length: {1}", subFolder.Length, isNetwork ? unc.Length : local.Length);

                // Success.
                var di1 = new Alphaleonis.Win32.Filesystem.DirectoryInfo(isNetwork ? unc : local);

                // Fail.
                //var di1 = new System.IO.DirectoryInfo(local);


                di1.Create();
                Assert.IsTrue(di1.Exists);


                UnitTestConstants.Dump(di1, -17);
            }

            Console.WriteLine();
        }
Пример #8
0
        private void FileInfo_Attributes(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, MethodBase.GetCurrentMethod().Name))
            {
                var file     = rootDir.RandomFileFullPath;
                var fileInfo = new Alphaleonis.Win32.Filesystem.FileInfo(file + "-AlphaFS");

                Console.WriteLine("\nAlphaFS Input File Path: [{0}]", fileInfo.FullName);

                using (fileInfo.Create())
                {
                    fileInfo.Attributes |= System.IO.FileAttributes.ReadOnly;
                    Assert.IsTrue((fileInfo.Attributes & System.IO.FileAttributes.ReadOnly) != 0, "The file is not ReadOnly, but is expected to.");

                    fileInfo.Attributes &= ~System.IO.FileAttributes.ReadOnly;
                    Assert.IsTrue((fileInfo.Attributes & System.IO.FileAttributes.ReadOnly) == 0, "The file is ReadOnly, but is expected not to.");


                    fileInfo.Attributes |= System.IO.FileAttributes.Hidden;
                    Assert.IsTrue((fileInfo.Attributes & System.IO.FileAttributes.Hidden) != 0, "The file is not Hidden, but is expected to.");

                    fileInfo.Attributes &= ~System.IO.FileAttributes.Hidden;
                    Assert.IsTrue((fileInfo.Attributes & System.IO.FileAttributes.Hidden) == 0, "The file is Hidden, but is expected not to.");


                    fileInfo.Attributes |= System.IO.FileAttributes.System;
                    Assert.IsTrue((fileInfo.Attributes & System.IO.FileAttributes.System) != 0, "The file is not System, but is expected to.");

                    fileInfo.Attributes &= ~System.IO.FileAttributes.System;
                    Assert.IsTrue((fileInfo.Attributes & System.IO.FileAttributes.System) == 0, "The file is System, but is expected not to.");
                }
            }

            Console.WriteLine();
        }
        public void AlphaFS_Volume_QueryAllDosDevices_Local_Success()
        {
            UnitTestConstants.PrintUnitTestHeader(false);

            var query = Alphaleonis.Win32.Filesystem.Volume.QueryAllDosDevices().ToArray();

            Console.WriteLine("Retrieved {0} MS-DOS Device Names.\n", query.Length);

            Assert.IsTrue(query.Length > 0, "No MS-DOS device names enumerated, but it is expected.");


            var deviceNameCount = 0;

            foreach (var deviceName in query)
            {
                Console.WriteLine("\t#{0:000}\t{1}", ++deviceNameCount, deviceName);
            }
        }
        private void Directory_CopyTimestamps(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, MethodBase.GetCurrentMethod().Name))
            {
                var folder  = rootDir.RandomDirectoryFullPath;
                var folder2 = rootDir.RandomDirectoryFullPath;
                if (isNetwork)
                {
                    folder  = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(folder);
                    folder2 = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(folder2);
                }

                System.IO.Directory.CreateDirectory(folder);
                Thread.Sleep(1500);
                System.IO.Directory.CreateDirectory(folder2);


                Console.WriteLine("\nInput Directory1 Path: [{0}]", folder);
                Console.WriteLine("\nInput Directory2 Path: [{0}]", folder2);


                Assert.AreNotEqual(System.IO.Directory.GetCreationTime(folder), System.IO.Directory.GetCreationTime(folder2));
                Assert.AreNotEqual(System.IO.Directory.GetLastAccessTime(folder), System.IO.Directory.GetLastAccessTime(folder2));
                Assert.AreNotEqual(System.IO.Directory.GetLastWriteTime(folder), System.IO.Directory.GetLastWriteTime(folder2));

                Alphaleonis.Win32.Filesystem.Directory.CopyTimestamps(folder, folder2);

                Assert.AreEqual(System.IO.Directory.GetCreationTime(folder), System.IO.Directory.GetCreationTime(folder2));
                Assert.AreEqual(System.IO.Directory.GetLastAccessTime(folder), System.IO.Directory.GetLastAccessTime(folder2));
                Assert.AreEqual(System.IO.Directory.GetLastWriteTime(folder), System.IO.Directory.GetLastWriteTime(folder2));
            }

            Console.WriteLine();
        }
Пример #11
0
        private void File_Move_CatchDirectoryNotFoundException_NonExistingDriveLetter(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, "File.Move"))
            {
                var fileSource = UnitTestConstants.CreateFile(rootDir.Directory.FullName);
                var fileCopy   = Alphaleonis.Win32.Filesystem.DriveInfo.GetFreeDriveLetter() + @":\NonExistingDriveLetter\" + System.IO.Path.GetRandomFileName();
                if (isNetwork)
                {
                    fileCopy = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(fileCopy);
                }

                Console.WriteLine("\nSrc File Path: [{0}]", fileSource);
                Console.WriteLine("Dst File Path: [{0}]", fileCopy);


                var gotException = false;
                try
                {
                    Alphaleonis.Win32.Filesystem.File.Move(fileSource.FullName, fileCopy);
                }
                catch (Exception ex)
                {
                    // Local: UnauthorizedAccessException.
                    // UNC: IOException.

                    var exName = ex.GetType().Name;
                    gotException = exName.Equals(isNetwork ? "IOException" : "DirectoryNotFoundException", StringComparison.OrdinalIgnoreCase);
                    Console.WriteLine("\n\tCaught Exception: [{0}] Message: [{1}]", exName, ex.Message);
                }
                Assert.IsTrue(gotException, "The exception is not caught, but is expected to.");
            }

            Console.WriteLine();
        }
Пример #12
0
        private void Directory_Move_CatchDirectoryNotFoundException_NonExistingDriveLetter(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, "Directory.Move"))
            {
                var folderSrc = Alphaleonis.Win32.Filesystem.DriveInfo.GetFreeDriveLetter() + @":\NonExistingFolder";
                if (isNetwork)
                {
                    folderSrc = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(folderSrc);
                }

                Console.WriteLine("Dst Directory Path: [{0}]", folderSrc);

                UnitTestConstants.CreateDirectoriesAndFiles(rootDir.Directory.FullName, new Random().Next(5, 15), true);


                var gotException = false;
                try
                {
                    Alphaleonis.Win32.Filesystem.Directory.Move(rootDir.Directory.FullName, folderSrc, Alphaleonis.Win32.Filesystem.MoveOptions.CopyAllowed);
                }
                catch (Exception ex)
                {
                    // Local: DirectoryNotFoundException.
                    // UNC: IOException.

                    var exName = ex.GetType().Name;
                    gotException = exName.Equals(isNetwork ? "IOException" : "DirectoryNotFoundException", StringComparison.OrdinalIgnoreCase);
                    Console.WriteLine("\n\tCaught Exception: [{0}] Message: [{1}]", exName, ex.Message);
                }
                Assert.IsTrue(gotException, "The exception is not caught, but is expected to.");
            }

            Console.WriteLine();
        }
Пример #13
0
        private void Directory_CreateDirectory_Delete(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, "Directory.CreateDirectory"))
            {
                var folder = rootDir.Directory.FullName;

                // Directory depth level.
                var level = new Random().Next(10, 1000);

#if NET35
                // MSDN: .NET 4+ Trailing spaces are removed from the end of the path parameter before deleting the directory.
                //folder += UnitTestConstants.EMspace;
#endif

                Console.WriteLine("\nInput Directory Path: [{0}]\n", folder);


                var root = folder;

                for (var i = 0; i < level; i++)
                {
                    root = System.IO.Path.Combine(root, "Level-" + (i + 1) + "-subFolder");
                }

                Alphaleonis.Win32.Filesystem.Directory.CreateDirectory(root);

                Console.WriteLine("Created directory structure: Depth: [{0}], path length: [{1}] characters.", level, root.Length);
                Console.WriteLine("\n{0}", root);

                Assert.IsTrue(Alphaleonis.Win32.Filesystem.Directory.Exists(root), "The directory does not exists, but is expected to.");
            }

            Console.WriteLine();
        }
Пример #14
0
        private void Directory_Copy(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, "Directory.Copy"))
            {
                var folderSrc = System.IO.Directory.CreateDirectory(System.IO.Path.Combine(rootDir.Directory.FullName, "Source-") + System.IO.Path.GetRandomFileName());
                var folderDst = System.IO.Directory.CreateDirectory(System.IO.Path.Combine(rootDir.Directory.FullName, "Destination-") + System.IO.Path.GetRandomFileName());
                Console.WriteLine("\nSrc Directory Path: [{0}]", folderSrc.FullName);
                Console.WriteLine("Dst Directory Path: [{0}]", folderDst.FullName);

                UnitTestConstants.CreateDirectoriesAndFiles(folderSrc.FullName, new Random().Next(5, 15), true);


                var dirEnumOptions   = Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.FilesAndFolders | Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.Recursive;
                var props            = Alphaleonis.Win32.Filesystem.Directory.GetProperties(folderSrc.FullName, dirEnumOptions);
                var sourceTotal      = props["Total"];
                var sourceTotalFiles = props["File"];
                var sourceTotalSize  = props["Size"];
                Console.WriteLine("\n\tTotal size: [{0}] - Total Folders: [{1}] - Files: [{2}]", Alphaleonis.Utils.UnitSizeToText(sourceTotalSize), sourceTotal - sourceTotalFiles, sourceTotalFiles);


                Alphaleonis.Win32.Filesystem.Directory.Copy(folderSrc.FullName, folderDst.FullName);


                props = Alphaleonis.Win32.Filesystem.Directory.GetProperties(folderDst.FullName, dirEnumOptions);
                Assert.AreEqual(sourceTotal, props["Total"], "The number of total file system objects do not match.");
                Assert.AreEqual(sourceTotalFiles, props["File"], "The number of total files do not match.");
                Assert.AreEqual(sourceTotalSize, props["Size"], "The total file size does not match.");


                Assert.IsTrue(System.IO.Directory.Exists(folderSrc.FullName), "The original directory does not exist, but is expected to.");
            }

            Console.WriteLine();
        }
Пример #15
0
        private void File_TransferTimestamps(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, "File.TransferTimestamps"))
            {
                var file  = rootDir.RandomFileFullPath;
                var file2 = rootDir.RandomFileFullPath;
                if (isNetwork)
                {
                    file  = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(file);
                    file2 = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(file2);
                }

                using (System.IO.File.Create(file)) {}
                Thread.Sleep(1500);
                using (System.IO.File.Create(file2)) { }


                Console.WriteLine("\nInput File1 Path: [{0}]", file);
                Console.WriteLine("\nInput File2 Path: [{0}]", file2);


                Assert.AreNotEqual(System.IO.File.GetCreationTime(file), System.IO.File.GetCreationTime(file2));
                Assert.AreNotEqual(System.IO.File.GetLastAccessTime(file), System.IO.File.GetLastAccessTime(file2));
                Assert.AreNotEqual(System.IO.File.GetLastWriteTime(file), System.IO.File.GetLastWriteTime(file2));

                Alphaleonis.Win32.Filesystem.File.TransferTimestamps(file, file2);

                Assert.AreEqual(System.IO.File.GetCreationTime(file), System.IO.File.GetCreationTime(file2));
                Assert.AreEqual(System.IO.File.GetLastAccessTime(file), System.IO.File.GetLastAccessTime(file2));
                Assert.AreEqual(System.IO.File.GetLastWriteTime(file), System.IO.File.GetLastWriteTime(file2));
            }

            Console.WriteLine();
        }
Пример #16
0
        public void AlphaFS_Volume_EnumerateVolumeMountPoints_Local_Success()
        {
            UnitTestAssert.IsElevatedProcess();
            UnitTestConstants.PrintUnitTestHeader(false);

            var cnt = 0;

            Console.WriteLine("Logical Drives\n");

            // Get Logical Drives from UnitTestConstants.Local Host, .IsReady Drives only.
            foreach (var drive in Alphaleonis.Win32.Filesystem.Directory.GetLogicalDrives(false, true))
            {
                try
                {
                    // Logical Drives --> Volumes --> Volume Mount Points.
                    var uniqueVolName = Alphaleonis.Win32.Filesystem.Volume.GetUniqueVolumeNameForPath(drive);

                    if (!Alphaleonis.Utils.IsNullOrWhiteSpace(uniqueVolName) && !uniqueVolName.Equals(drive, StringComparison.OrdinalIgnoreCase))
                    {
                        foreach (var mountPoint in Alphaleonis.Win32.Filesystem.Volume.EnumerateVolumeMountPoints(uniqueVolName).Where(mp => !Alphaleonis.Utils.IsNullOrWhiteSpace(mp)))
                        {
                            string guid = null;
                            try { guid = Alphaleonis.Win32.Filesystem.Volume.GetVolumeGuid(System.IO.Path.Combine(drive, mountPoint)); }
                            catch (Exception ex)
                            {
                                Console.WriteLine("\n\tCaught (UNEXPECTED #1) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
                            }

                            Console.WriteLine("\t#{0:000}\tLogical Drive: [{1}]\tGUID: [{2}]\n\t\tDestination  : [{3}]\n", ++cnt, drive, guid ?? "null", mountPoint);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\n\tCaught (UNEXPECTED #2) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
                }
            }


            if (cnt == 0)
            {
                UnitTestAssert.InconclusiveBecauseResourcesAreUnavailable();
            }
        }
Пример #17
0
        private void Directory_Delete_CatchUnauthorizedAccessException_FolderWithDenyPermission(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, "Directory.Delete"))
            {
                var folder = rootDir.RandomFileFullPath;
                Console.WriteLine("\nInput Directory Path: [{0}]\n", folder);

                System.IO.Directory.CreateDirectory(folder);

                // Create a temp folder and set DENY permission for current user.
                UnitTestConstants.FolderDenyPermission(true, folder);


                var gotException = false;
                try
                {
                    Alphaleonis.Win32.Filesystem.Directory.Delete(folder);
                }
                catch (Exception ex)
                {
                    var exName = ex.GetType().Name;
                    gotException = exName.Equals("UnauthorizedAccessException", StringComparison.OrdinalIgnoreCase);
                    Console.WriteLine("\n\tCaught Exception: [{0}] Message: [{1}]", exName, ex.Message);
                }
                Assert.IsTrue(gotException, "The exception is not caught, but is expected to.");


                // Remove DENY permission for current user and delete folder.
                UnitTestConstants.FolderDenyPermission(false, folder);
            }

            Console.WriteLine();
        }
Пример #18
0
        private void File_Copy(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);
            Console.WriteLine();


            var tempPath = UnitTestConstants.TempFolder;

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, MethodBase.GetCurrentMethod().Name))
            {
                // Min: 1 bytes, Max: 10485760 = 10 MB.
                var fileLength = new Random().Next(1, 10485760);
                var fileSource = UnitTestConstants.CreateFile(rootDir.Directory.FullName, fileLength);
                var fileCopy   = rootDir.RandomFileFullPath;

                Console.WriteLine("Src File Path: [{0}] [{1}]", Alphaleonis.Utils.UnitSizeToText(fileLength), fileSource);
                Console.WriteLine("Dst File Path: [{0}]", fileCopy);

                Assert.IsTrue(System.IO.File.Exists(fileSource.FullName), "The file does not exists, but is expected to.");


                Alphaleonis.Win32.Filesystem.File.Copy(fileSource.FullName, fileCopy);


                Assert.IsTrue(System.IO.File.Exists(fileCopy), "The file does not exists, but is expected to.");


                var fileLen = new System.IO.FileInfo(fileCopy).Length;

                Assert.AreEqual(fileLength, fileLen, "The file copy is: {0} bytes, but is expected to be: {1} bytes.", fileLen, fileLength);

                Assert.IsTrue(System.IO.File.Exists(fileSource.FullName), "The original file does not exist, but is expected to.");
            }


            Console.WriteLine();
        }
Пример #19
0
        private void DirectoryInfo_InitializeInstance_ExistingDirectory(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

             var tempPath = System.IO.Path.GetTempPath();
             if (isNetwork)
            tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);

             using (var rootDir = new TemporaryDirectory(tempPath, MethodBase.GetCurrentMethod().Name))
             {
            var folder = rootDir.RandomDirectoryFullPath;
            var dirInfo = new System.IO.DirectoryInfo(folder);
            dirInfo.Create();

            CompareDirectoryInfos(dirInfo, new Alphaleonis.Win32.Filesystem.DirectoryInfo(folder), true);
             }

             Console.WriteLine();
        }
        private void AlphaFS_BackupFileStream_InitializeInstance(bool isNetwork)
        {
            using (var tempRoot = new TemporaryDirectory(isNetwork))
            {
                var file = tempRoot.CreateFile();

                Console.WriteLine("Input File Path: [{0}]", file.FullName);


                using (var bfs = new Alphaleonis.Win32.Filesystem.BackupFileStream(file.FullName, System.IO.FileMode.Open))
                {
                    UnitTestConstants.Dump(bfs.ReadStreamInfo());

                    UnitTestConstants.Dump(bfs);
                }
            }

            Console.WriteLine();
        }
Пример #21
0
        public void AlphaFS_Host_EnumerateDrives_Network_Success()
        {
            UnitTestAssert.IsElevatedProcess();

            UnitTestConstants.PrintUnitTestHeader(true);


            var host = Environment.MachineName;

            var drives = Alphaleonis.Win32.Network.Host.EnumerateDrives(host, true).ToArray();

            foreach (var driveInfo in drives)
            {
                Console.WriteLine("Host Local Drive: [{0}]", driveInfo.Name);

                UnitTestConstants.Dump(driveInfo);

                UnitTestConstants.Dump(driveInfo.DiskSpaceInfo, true);

                UnitTestConstants.Dump(driveInfo.VolumeInfo, true);

                Assert.IsNull(driveInfo.DosDeviceName);

                Assert.IsNull(driveInfo.VolumeInfo.Guid);

                Console.WriteLine();
            }


            if (drives.Length == 0)
            {
                UnitTestAssert.InconclusiveBecauseResourcesAreUnavailable();
            }


            // \\localhost\C$

            host = Alphaleonis.Win32.Network.Host.GetUncName() + Alphaleonis.Win32.Filesystem.Path.DirectorySeparator +
                   UnitTestConstants.SysDrive[0] + Alphaleonis.Win32.Filesystem.Path.NetworkDriveSeparator +
                   Alphaleonis.Win32.Filesystem.Path.DirectorySeparator;

            Assert.AreEqual(drives[0].Name, host);
        }
        private void Directory_CreateDirectory_ThrowDirectoryNotFoundException_NonExistingDriveLetter(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var folder = Alphaleonis.Win32.Filesystem.DriveInfo.GetFreeDriveLetter() + @":\NonExistingDriveLetter";

            if (isNetwork)
            {
                folder = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(folder);
            }

            Console.WriteLine("Input Directory Path: [{0}]", folder);

            ExceptionAssert.DirectoryNotFoundException(() => System.IO.Directory.CreateDirectory(folder));

            ExceptionAssert.DirectoryNotFoundException(() => Alphaleonis.Win32.Filesystem.Directory.CreateDirectory(folder));

            Console.WriteLine();
        }
Пример #23
0
        private void File_Move_Overwrite_DestinationFileAlreadyExists(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, MethodBase.GetCurrentMethod().Name))
            {
                var fileSource = UnitTestConstants.CreateFile(rootDir.Directory.FullName);
                var fileCopy   = rootDir.RandomFileFullPath;
                Console.WriteLine("\nSource File Path: [{0}]", fileSource);

                // Copy it.
                System.IO.File.Copy(fileSource.FullName, fileCopy);


                var gotException = false;
                try
                {
                    Alphaleonis.Win32.Filesystem.File.Move(fileSource.FullName, fileCopy);
                }
                catch (Exception ex)
                {
                    Alphaleonis.Win32.Filesystem.File.Move(fileSource.FullName, fileCopy, Alphaleonis.Win32.Filesystem.MoveOptions.ReplaceExisting);

                    var exName = ex.GetType().Name;
                    gotException = exName.Equals("AlreadyExistsException", StringComparison.OrdinalIgnoreCase);
                    Console.WriteLine("\n\tCaught {0} Exception: [{1}] {2}", gotException ? "EXPECTED" : "UNEXPECTED", exName, ex.Message);

                    Assert.IsFalse(System.IO.File.Exists(fileSource.FullName), "The file does exists, but is expected not to.");
                    Assert.IsTrue(System.IO.File.Exists(fileCopy), "The file does not exists, but is expected to.");
                }
                Assert.IsTrue(gotException, "The exception is not caught, but is expected to.");
            }

            Console.WriteLine();
        }
Пример #24
0
        public void AlphaFS_Path_IsLongPath()
        {
            Console.WriteLine("Path.IsLongPath()");

            var pathCnt     = 0;
            var errorCnt    = 0;
            var longPathCnt = 0;

            UnitTestConstants.StopWatcher(true);
            foreach (var path in UnitTestConstants.InputPaths)
            {
                var actual = false;

                Console.WriteLine("\n#{0:000}\tInput Path: [{1}]", ++pathCnt, path);

                // AlphaFS
                try
                {
                    var expected = path.StartsWith(Path.LongPathPrefix, StringComparison.OrdinalIgnoreCase);
                    actual = Path.IsLongPath(path);

                    Assert.AreEqual(expected, actual);

                    if (actual)
                    {
                        longPathCnt++;
                    }
                }
                catch (Exception ex)
                {
                    errorCnt++;

                    Console.WriteLine("\tCaught [AlphaFS] {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
                }
                Console.WriteLine("\tAlphaFS   : [{0}]", actual);
            }
            Console.WriteLine("\n{0}", UnitTestConstants.Reporter(true));

            // Hand counted 33 True's.
            Assert.AreEqual(33, longPathCnt, "Number of local paths do not match.", errorCnt);

            Assert.AreEqual(0, errorCnt, "No errors were expected.");
        }
Пример #25
0
        private void AlphaFS_Volume_GetDriveType(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);


            var logicalDriveCount = 0;

            foreach (var logicalDrive in System.IO.DriveInfo.GetDrives())
            {
                var driveName = isNetwork ? Alphaleonis.Win32.Filesystem.Path.LocalToUnc(logicalDrive.Name) : logicalDrive.Name;

                Console.Write("#{0:000}\tInput Logical Drive Path: [{1}]", ++logicalDriveCount, driveName);

                if (logicalDrive.DriveType == System.IO.DriveType.CDRom)
                {
                    Console.WriteLine();
                    continue;
                }


                var driveType = Alphaleonis.Win32.Filesystem.Volume.GetDriveType(driveName);

                Console.WriteLine("\t\tDrive Type: [{0}]", driveType);


                if (isNetwork)
                {
                    // Some USB drives do not report drive type.
                }

                else
                {
                    Assert.AreEqual(logicalDrive.DriveType, driveType);
                }
            }


            Assert.IsTrue(logicalDriveCount > 0, "No logical drives enumerated, but it is expected.");


            Console.WriteLine();
        }
Пример #26
0
        private void Directory_CreateSymbolicLink_CatchIOException_FileExistsWithSameNameAsDirectory(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, MethodBase.GetCurrentMethod().Name))
            {
                var folderLink = System.IO.Path.Combine(rootDir.Directory.FullName, "FolderLink-ToOriginalFolder");

                var fileInfo = new System.IO.FileInfo(System.IO.Path.Combine(rootDir.Directory.FullName, "OriginalFile.txt"));
                using (fileInfo.Create()) {}

                Console.WriteLine("\nInput File Path     : [{0}]", fileInfo.FullName);
                Console.WriteLine("Input Directory Link: [{0}]", folderLink);


                var gotException = false;

                try
                {
                    Alphaleonis.Win32.Filesystem.Directory.CreateSymbolicLink(folderLink, fileInfo.FullName);
                }
                catch (Exception ex)
                {
                    var exName = ex.GetType().Name;
                    gotException = exName.Equals("IOException", StringComparison.OrdinalIgnoreCase);
                    Console.WriteLine("\n\tCaught {0} Exception: [{1}] {2}", gotException ? "EXPECTED" : "UNEXPECTED", exName, ex.Message);
                }


                Assert.IsTrue(gotException, "The exception is not caught, but is expected to.");
            }

            Console.WriteLine();
        }
Пример #27
0
        private void Open_Append(bool isNetwork)
        {
            using (var tempRoot = new TemporaryDirectory(isNetwork))
            {
                var file = tempRoot.RandomTxtFileFullPath;

                Console.WriteLine("Input File Path: [{0}]", file);


                long fileLength;
                var  tenNumbers = "0123456789";
                var  ten        = tenNumbers.Length;



                // Create.
                using (var fs = Alphaleonis.Win32.Filesystem.File.Open(file, System.IO.FileMode.Create))
                {
                    // According to NotePad++, creates a file type: "ANSI", which is reported as: "Unicode (UTF-8)".
                    fs.Write(UnitTestConstants.StringToByteArray(tenNumbers), 0, ten);

                    fileLength = fs.Length;
                }
                Assert.IsTrue(fileLength == ten, "The file is: {0} bytes, but is expected to be: {1} bytes.", fileLength, ten);



                // Append.
                using (var fs = Alphaleonis.Win32.Filesystem.File.Open(file, System.IO.FileMode.Append))
                {
                    // According to NotePad++, creates a file type: "ANSI", which is reported as: "Unicode (UTF-8)".
                    fs.Write(UnitTestConstants.StringToByteArray(tenNumbers), 0, ten);

                    fileLength = fs.Length;
                }

                Assert.IsTrue(System.IO.File.Exists(file), "The file does not exists, but is expected to.");
                Assert.IsTrue(fileLength == 2 * ten, "The file is: {0} bytes, but is expected to be: {1} bytes.", fileLength, 2 * ten);
            }

            Console.WriteLine();
        }
Пример #28
0
        private void Directory_SetCurrentDirectory_WithLongPath(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = UnitTestConstants.SysRoot;

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            Console.WriteLine("\n\tNo compare with System.IO possible because of: \"System.ArgumentException: Illegal characters in path.\"\n");


            Console.WriteLine("\tAlphaFS Set Current Directory Path: [{0}]", tempPath);
            Alphaleonis.Win32.Filesystem.Directory.SetCurrentDirectory(tempPath);


            // No compare with System.IO possible: System.ArgumentException: Illegal characters in path.
            //var sysIoCurrPath = System.IO.Directory.GetCurrentDirectory();

            var alphaFSCurrPath = Alphaleonis.Win32.Filesystem.Directory.GetCurrentDirectory();


            Console.WriteLine("\tAlphaFS Get Current Directory Path: [{0}]", alphaFSCurrPath);


            var lpPrefix = isNetwork
            ? Alphaleonis.Win32.Filesystem.Path.LongPathUncPrefix
            : Alphaleonis.Win32.Filesystem.Path.LongPathPrefix;

            if (isNetwork)
            {
                tempPath = tempPath.TrimStart('\\');
            }

            Assert.AreEqual(lpPrefix + tempPath, alphaFSCurrPath, "The current directories do not match, but are expected to.");


            Console.WriteLine();
        }
Пример #29
0
        private void File_Copy_CatchUnauthorizedAccessException_DestinationFileIsReadOnly(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, "File.Copy"))
            {
                var fileSource = UnitTestConstants.CreateFile(rootDir.Directory.FullName);
                var fileCopy   = rootDir.RandomFileFullPath + ".txt";
                Console.WriteLine("\nInput File Path: [{0}]", fileSource);


                System.IO.File.Copy(fileSource.FullName, fileCopy);
                System.IO.File.SetAttributes(fileCopy, System.IO.FileAttributes.ReadOnly);


                var gotException = false;
                try
                {
                    Alphaleonis.Win32.Filesystem.File.Copy(fileSource.FullName, fileCopy, true);
                }
                catch (Exception ex)
                {
                    var exName = ex.GetType().Name;
                    gotException = exName.Equals("UnauthorizedAccessException", StringComparison.OrdinalIgnoreCase);
                    Console.WriteLine("\n\tCaught Exception: [{0}] Message: [{1}]", exName, ex.Message);
                }
                Assert.IsTrue(gotException, "The exception is not caught, but is expected to.");


                System.IO.File.SetAttributes(fileCopy, System.IO.FileAttributes.Normal);
            }

            Console.WriteLine();
        }
Пример #30
0
        private void File_Move_DifferentSourceAndDestination(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = System.IO.Path.GetTempPath();


            using (var rootDir = new TemporaryDirectory(tempPath, "File.Move"))
            {
                // Min: 1 bytes, Max: 10485760 = 10 MB.
                var fileLength = new Random().Next(1, 10485760);
                var src        = UnitTestConstants.CreateFile(rootDir.Directory.FullName, fileLength).FullName;
                var dst        = rootDir.RandomFileFullPath + ".txt";


                if (isNetwork)
                {
                    src = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(src);
                }
                else
                {
                    dst = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(dst);
                }


                Console.WriteLine("\nSRC File Path: [{0}] [{1}]", Alphaleonis.Utils.UnitSizeToText(fileLength), src);
                Console.WriteLine("DST File Path: [{0}]", dst);

                Alphaleonis.Win32.Filesystem.File.Move(src, dst);

                Assert.IsFalse(System.IO.File.Exists(src), "The file does exists, but is expected not to.");
                Assert.IsTrue(System.IO.File.Exists(dst), "The file does not exists, but is expected to.");

                var fileLen = new System.IO.FileInfo(dst).Length;
                Assert.AreEqual(fileLength, fileLen, "The file copy is: {0} bytes, but is expected to be: {1} bytes.", fileLen, fileLength);


                Assert.IsFalse(System.IO.File.Exists(src), "The original file exists, but is expected not to.");
            }

            Console.WriteLine();
        }