Ejemplo n.º 1
0
        public void ValidateJob_ThrowsExceptionBlankUser()
        {
            FindDuplicateOptions options = new FindDuplicateOptions()
            {
                Domain            = "",
                HashLimit         = 0,
                IsLocalFileSystem = false,
                Pass = "******",
                User = "",
                Path = @".\"
            };
            FindDuplicatesJob   testJob        = new FindDuplicatesJob(options);
            Mock <IFileHelpers> fileHelperMock = new Moq.Mock <IFileHelpers>();
            DuplicateFinder     sut            = new DuplicateFinder(fileHelperMock.Object);

            try
            {
                sut.ValidateJob(testJob);
                Assert.Fail("Should have thrown an argument exception");
            }
            catch (ArgumentException ex)
            {
                Assert.Pass();
            }
        }
Ejemplo n.º 2
0
        public void ValidateJob_ThrowsExceptionBlankPass()
        {
            string expectedInnerError    = "Remote Filesystem selected but credentials were invalid.";
            FindDuplicateOptions options = new FindDuplicateOptions()
            {
                Domain            = "",
                HashLimit         = 0,
                IsLocalFileSystem = false,
                Pass = "",
                User = "******",
                Path = @".\"
            };
            FindDuplicatesJob   testJob        = new FindDuplicatesJob(options);
            Mock <IFileHelpers> fileHelperMock = new Moq.Mock <IFileHelpers>();
            DuplicateFinder     sut            = new DuplicateFinder(fileHelperMock.Object);

            try
            {
                sut.ValidateJob(testJob);
                Assert.Fail("Should have thrown an argument exception");
            }
            catch (ArgumentException ex)
            {
                Assert.True(ex.Message.Contains(expectedInnerError));
                Assert.Pass();
            }
        }
Ejemplo n.º 3
0
        public void WalkFilePaths_ThrowsOnInvalidPath()
        {
            FindDuplicateOptions options = new FindDuplicateOptions()
            {
                Domain            = "",
                HashLimit         = 0,
                IsLocalFileSystem = true,
                Pass = "",
                User = "",
                Path = @".\"
            };
            FindDuplicatesJob testJob            = new FindDuplicatesJob(options);
            string            expectedInnerError = "unanticipated error";

            var         filesystemMock = new Moq.Mock <IFileSystem>();
            FileHelpers sut            = new FileHelpers(filesystemMock.Object);

            filesystemMock
            .Setup(x => x.Directory.GetFiles(testJob.Options.Path, It.IsAny <string>(),
                                             It.IsAny <System.IO.SearchOption>())).Throws(new Exception(expectedInnerError));

            try
            {
                sut.WalkFilePaths(testJob);
                Assert.Fail("If WalkFilePaths throws, there isn't anything we can do to recover. Should have thrown but did not.");
            }
            catch (Exception ex)
            {
                Assert.True(ex.Message.Contains(expectedInnerError));
                Assert.Pass();
            }
        }
Ejemplo n.º 4
0
        public void WalkFilePaths_ReturnsExpectedArray()
        {
            FindDuplicateOptions options = new FindDuplicateOptions()
            {
                Domain            = "",
                HashLimit         = 0,
                IsLocalFileSystem = true,
                Pass = "",
                User = "",
                Path = @".\"
            };
            FindDuplicatesJob testJob = new FindDuplicatesJob(options);

            string[] testFiles = new[] { @"C:\BOOTNXT",
                                         @"C:\hiberfil.sys",
                                         @"C:\pagefile.sys",
                                         @"C:\windows-version.txt" };

            var         filesystemMock = new Moq.Mock <IFileSystem>();
            FileHelpers sut            = new FileHelpers(filesystemMock.Object);

            filesystemMock.Setup(x => x.Directory.GetFiles(testJob.Options.Path, It.IsAny <string>(), It.IsAny <System.IO.SearchOption>())).Returns(testFiles);

            string[] actualResult = sut.WalkFilePaths(testJob);
            Assert.AreEqual(testFiles, actualResult);
        }
Ejemplo n.º 5
0
 public void ValidateJob(FindDuplicatesJob job)
 {
     if (!job.Options.IsLocalFileSystem && (String.IsNullOrEmpty(job.Options.User) || String.IsNullOrEmpty(job.Options.Pass)))
     {
         throw new ArgumentException("Remote Filesystem selected but credentials were invalid.");
     }
 }
Ejemplo n.º 6
0
 public string[] GetFilePaths(FindDuplicatesJob job)
 {
     //Since we don't yet know how many files will be found, progress reporting is not trivial.
     Console.WriteLine($"Traversing {job.Path}...");
     string[] files = _fileSystemHelper.WalkFilePaths(job);
     Console.WriteLine("...done.");
     return(files);
 }
Ejemplo n.º 7
0
 public void Setup()
 {
     _options = new FindDuplicateOptions()
     {
         Domain            = "",
         HashLimit         = 0,
         IsLocalFileSystem = true,
         Pass = "",
         User = "",
         Path = @".\"
     };
     _testJob = new FindDuplicatesJob(_options);
 }
Ejemplo n.º 8
0
        public void FindDuplicates()
        {
            //Read app config options or //todo console selections
            FindDuplicatesJob job = new FindDuplicatesJob(SafeGetAppConfigs());

            FileHelpers     fileHelpers = new FileHelpers(new FileSystem());
            DuplicateFinder dupeFinder  = new DuplicateFinder(fileHelpers);

            dupeFinder.ValidateJob(job);
            ConcurrentDictionary <string, List <File> > duplicates = dupeFinder.FindDuplicateFiles(job);

            dupeFinder.ReportResults(duplicates);

            //TODO: Persist

            Console.ReadKey();
        }
Ejemplo n.º 9
0
        public void ValidateJob_IgnoresUserPassLocal()
        {
            FindDuplicateOptions options = new FindDuplicateOptions()
            {
                Domain            = "",
                HashLimit         = 0,
                IsLocalFileSystem = true,
                Pass = "",
                User = "",
                Path = @".\"
            };
            FindDuplicatesJob   testJob        = new FindDuplicatesJob(options);
            Mock <IFileHelpers> fileHelperMock = new Moq.Mock <IFileHelpers>();
            DuplicateFinder     sut            = new DuplicateFinder(fileHelperMock.Object);

            sut.ValidateJob(testJob);
        }
Ejemplo n.º 10
0
        public string[] WalkFilePaths(FindDuplicatesJob job)
        {
            //Console.WriteLine("Walking file system paths...");
            string[] fileSystemList = new string[] { };

            try
            {
                fileSystemList = _fileSystem.Directory.GetFiles(job.Path, "*.*", System.IO.SearchOption.AllDirectories);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Exception encountered walking the file tree: {e}");
                throw;
            }
            int filesFound = fileSystemList.Length;

            Console.WriteLine($"Found {filesFound} files.");

            return(fileSystemList);
        }
Ejemplo n.º 11
0
        public ConcurrentDictionary <string, List <File> > FindDuplicateFiles(FindDuplicatesJob job)
        {
            string[] filePaths;
            ConcurrentDictionary <string, List <File> > duplicateDictionary = new ConcurrentDictionary <string, List <File> >();

            hashLimit = job.Options.HashLimit;
            if (job.Options.IsLocalFileSystem)
            {
                filePaths = GetFilePaths(job);
                PopulateFileMetaData(filePaths, duplicateDictionary);
            }
            else
            {
                NetworkCredential networkCred = new NetworkCredential(job.Options.User, job.Options.Pass, job.Options.Domain);
                CredentialCache   netCache    = new CredentialCache();
                netCache.Add(new System.Uri(job.Options.Path), "Basic", networkCred);
                filePaths = GetFilePaths(job);
                PopulateFileMetaData(filePaths, duplicateDictionary);

                //using (UNCAccessWithCredentials.UNCAccessWithCredentials unc =
                //	new UNCAccessWithCredentials.UNCAccessWithCredentials())
                //{
                //	if (unc.NetUseWithCredentials(job.Options.Path, job.Options.User, job.Options.Domain, job.Options.Pass)
                //		|| unc.LastError == 1219) // Already connected
                //	{
                //		filePaths = GetFilePaths(job);
                //		PopulateFileMetaData(filePaths, duplicateDictionary);

                //	}
                //	else
                //	{
                //		Console.WriteLine($"Failed to connect to UNC location. Error: {unc.LastError}.");
                //		switch (unc.LastError)
                //		{
                //			case 1326:
                //				Console.WriteLine("Login failure: The user name or password is incorrect.");
                //				break;
                //			case 86:
                //				Console.WriteLine("Access denied: The specified network password is not correct.");
                //				break;
                //			case 87:
                //				Console.WriteLine("Invalid parameter.");
                //				break;
                //			case 1219:
                //				Console.WriteLine("Multiple connections to server.");
                //				unc.Dispose();
                //				break;
                //			case 53:
                //				Console.WriteLine("Network path not found.");
                //				break;
                //			case 5:
                //				Console.WriteLine("Access denied.");
                //				break;
                //			default:
                //				Console.WriteLine($"Unknown error. {unc.LastError}");
                //				break;
                //		}
                //		Console.ReadKey();
                //	}
                //}
            }

            return(duplicateDictionary);
        }