Exemplo n.º 1
0
        public void RescanRemoteDirectory_FileNotFound_CreatesMessage()
        {
            // ARRANGE
            List <string> messages = new List <string>();

            ScannedFileStore.DirectoryGetFiles = (string path, string searchPatther, System.IO.SearchOption searchOption) =>
            {
                return(new string[] {
                    "foobar"
                });
            };
            ScannedFileStore.FileExists = (string path) =>
            {
                return(false);
            };

            // ACT
            ScannedFileStore.RescanRemoteDirectory(_remotePath, (message) =>
            {
                messages.Add(message);
            });

            // ASSERT
            Assert.IsTrue(messages.Contains("File not found foobar."), "The log message expected was not generated.");
        }
        public async Task ScanLocationAsync_StoresScannedLocation()
        {
            // ARRANGE
            List <ScannedLocation>            scannedLocations       = new List <ScannedLocation>();
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();

            dictionaryMockFileData.Add(@"C:\foobar\file.txt", new MockFileData("This is a test file{0}."));
            MockFileSystem               fileSystem       = new MockFileSystem(dictionaryMockFileData);
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();
            List <string> locations = new List <string>()
            {
                @"C:\foobar"
            };

            service.Setup(t => t.InsertScannedLocationAsync(It.IsAny <ScannedLocation>()))
            .Returns(Task.CompletedTask)
            .Callback((ScannedLocation s) => { scannedLocations.Add(s); });

            // ACT
            await scannedFileStore.ScanLocationsAsync(locations, progress);

            // ASSERT
            Assert.AreEqual(locations[0], scannedLocations[0].Path);
        }
Exemplo n.º 3
0
        public void RescanRemoteDirectory_ExecutesStatusCallback_OnlyOnPercentageChange()
        {
            // ARRANGE
            int           percentageCount = -2; // Offset for Begin and End messages
            DirectoryInfo remotePath      = new DirectoryInfo(GetTemporaryDirectory());
            string        file            = Path.Combine(_remotePath, "One.png");

            for (int i = 0; i < 10; i++)
            {
                File.Copy(file, Path.Combine(remotePath.FullName, Path.GetRandomFileName()));
            }
            ScannedFileStore.ScanRemoteDirectory(remotePath.FullName);
            for (int i = 0; i < 1000; i++)
            {
                File.Copy(file, Path.Combine(remotePath.FullName, Path.GetRandomFileName()));
            }

            // ACT
            ScannedFileStore.RescanRemoteDirectory(remotePath.FullName, t =>
            {
                percentageCount++;
            });

            // ASSERT
            Assert.IsTrue(percentageCount == 100); // 100 percentage status changes

            // Cleanup
            remotePath.GetFiles("*", SearchOption.AllDirectories).ToList().ForEach(t => t.IsReadOnly = false);
            remotePath.Delete(true);
        }
        public async Task RescanLocationAsync_RemovesScannedFilesNotFoundInDiretory()
        {
            // ARRANGE
            MockFileSystem fileSystem = new MockFileSystem();

            fileSystem.AddDirectory(@"C:\foobar");
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();
            List <string> locations = new List <string>()
            {
                @"C:\foobar"
            };

            service.Setup(t => t.ListScannedFilePathsAsync(It.IsAny <List <string> >())).ReturnsAsync((List <string> locs) =>
            {
                return(new List <string>()
                {
                    @"C:\foobar\foo.bar"
                });
            });

            // ACT
            await scannedFileStore.RescanLocationsAsync(locations, progress);

            // ASSERT
            service.Verify(t => t.RemoveScannedFilesByFilePathAsync(It.Is <string>(s => s.Equals(@"C:\foobar\foo.bar"))));
        }
Exemplo n.º 5
0
        public void RescanRemoteDirectory_RemovesPurgedFilesFromRemoteFileTable()
        {
            // ARRANGE
            DirectoryInfo remotePath          = new DirectoryInfo(GetTemporaryDirectory());
            string        fileOne             = Path.Combine(_remotePath, "One.png");
            string        fileTwo             = Path.Combine(_remotePath, "Dir\\Two.png");
            string        fileTwoAtRemotePath = Path.Combine(remotePath.FullName, Path.GetRandomFileName());

            File.Copy(fileOne, Path.Combine(remotePath.FullName, Path.GetRandomFileName()));
            File.Copy(fileTwo, fileTwoAtRemotePath);
            ScannedFileStore.ScanRemoteDirectory(remotePath.FullName);
            File.SetAttributes(fileTwoAtRemotePath, ~FileAttributes.ReadOnly);
            File.Delete(fileTwoAtRemotePath);

            // ACT
            ScannedFileStore.RescanRemoteDirectory(remotePath.FullName);

            // ASSERT
            byte[] hash;
            using (FileStream stream = File.OpenRead(fileTwo))
            {
                var sha = new System.Security.Cryptography.SHA256Managed();
                hash = sha.ComputeHash(stream);
            }


            Assert.IsTrue(ScannedFileStore.SearchRemoteFiles(hash) == 0);

            // Cleanup
            remotePath.GetFiles("*", SearchOption.AllDirectories).ToList().ForEach(t => t.IsReadOnly = false);
            remotePath.Delete(true);
        }
        public async Task RescanLocationAsync_StoresAddedScannedFilesFromDiretory()
        {
            // ARRANGE
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();

            dictionaryMockFileData.Add(@"C:\foobar\file1.txt", new MockFileData("This is a test file."));
            dictionaryMockFileData.Add(@"C:\foobar\file2.txt", new MockFileData("This is another test file."));
            MockFileSystem               fileSystem       = new MockFileSystem(dictionaryMockFileData);
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();
            List <string> locations = new List <string>()
            {
                @"C:\foobar"
            };

            service.Setup(t => t.ListScannedFilePathsAsync(It.IsAny <List <string> >())).ReturnsAsync((List <string> locs) =>
            {
                return(new List <string>()
                {
                    @"C:\foobar\foo.bar"
                });
            });

            // ACT
            await scannedFileStore.RescanLocationsAsync(locations, progress);

            // ASSERT
            service.Verify(t => t.InsertScannedFileAsync(It.IsAny <ScannedFile>()), Times.Exactly(2), "The correct number of files were not inserted.");
        }
        public async Task ScanLocationAsync_ReportsProgressCorrectly()
        {
            // ARRANGE
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();
            Action <int> addFileDelegate = (int id) =>
            {
                string path    = string.Format(@"C:\foobar\file{0}.txt", id);
                string content = string.Format("This is a test file{0}.", id);
                dictionaryMockFileData.Add(path, new MockFileData(content));
            };

            for (int i = 0; i < 100; i++)
            {
                addFileDelegate(i);
            }
            MockFileSystem               fileSystem       = new MockFileSystem(dictionaryMockFileData);
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();
            List <string> locations = new List <string>()
            {
                @"C:\foobar"
            };

            // ACT
            await scannedFileStore.ScanLocationsAsync(locations, progress);

            // ASSERT
            Assert.AreEqual(progress.ReportedValues.Count, 100);
            Assert.AreEqual(progress.ReportedValues[49], 50);
            Assert.AreEqual(progress.ReportedValues[99], 100);
        }
Exemplo n.º 8
0
        public void SearchRemoteFilesByHash_NonExistingFile_ReturnsZero()
        {
            // ARRANGE

            // ACT
            int result = ScannedFileStore.SearchRemoteFiles(_fileHash);

            // ASSERT
            Assert.IsTrue(result == 0);
        }
        public async Task RemoveFile_RemovesScannedFiles()
        {
            // ARRANGE
            MockFileSystem          fileSystem       = new MockFileSystem();
            Mock <IFileHashService> service          = new Mock <IFileHashService>();
            ScannedFileStore        scannedFileStore = new ScannedFileStore(fileSystem, service.Object);

            // ACT
            await scannedFileStore.RemoveFile(@"C:\foo\bar.txt", null, 0, 0);

            // ASSERT
            service.Verify(t => t.RemoveScannedFilesByFilePathAsync(It.Is <string>(s => s.Equals(@"C:\foo\bar.txt"))));
        }
        public async Task ListDuplicateFiles_ReturnsDuplicatesFromService()
        {
            // ARRANGE
            MockFileSystem          fileSystem       = new MockFileSystem();
            Mock <IFileHashService> service          = new Mock <IFileHashService>();
            ScannedFileStore        scannedFileStore = new ScannedFileStore(fileSystem, service.Object);

            // ACT
            List <ScannedFile> results = await scannedFileStore.ListDuplicateFilesAsync();

            // ASSERT
            service.Verify(t => t.ReturnDuplicatesAsync(), Times.Once);
        }
        public async Task ListScannedLocationsAsync_ReturnsListFromService()
        {
            // ARRANGE
            MockFileSystem          fileSystem       = new MockFileSystem();
            Mock <IFileHashService> service          = new Mock <IFileHashService>();
            ScannedFileStore        scannedFileStore = new ScannedFileStore(fileSystem, service.Object);

            // ACT
            List <string> results = await scannedFileStore.ListScannedLocationsAsync();

            // ASSERT
            service.Verify(t => t.ListScannedLocationsAsync(), Times.Once());
        }
        public async Task RescanLocationAsync_ReportsProgressCorrectly()
        {
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();
            MockFileSystem               fileSystem       = new MockFileSystem(dictionaryMockFileData);
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();
            List <ScannedFile>           scannedFiles     = new List <ScannedFile>();
            Action <int> addFileDelegate = (int id) =>
            {
                string path    = string.Format(@"C:\foobar\file{0}.txt", id);
                string content = string.Format("This is a test file{0}.", id);
                fileSystem.AddFile(path, new MockFileData(content));
            };
            Action <int> addFileRemovalDelegate = (int id) =>
            {
                string path = string.Format(@"C:\foobar\oldfile{0}.txt", id);
                string name = string.Format("oldfile{0}.txt", id);
                byte[] hash = new byte[32];
                hash[0] = System.Convert.ToByte(id);
                scannedFiles.Add(new ScannedFile()
                {
                    Path = path,
                    Name = name,
                    Hash = hash
                });
            };

            for (int i = 0; i < 50; i++)
            {
                addFileDelegate(i);
                addFileRemovalDelegate(i);
            }
            service.Setup(t => t.ListScannedFilePathsAsync(It.IsAny <List <string> >()))
            .ReturnsAsync((List <string> locs) =>
            {
                return(scannedFiles.Select(t => t.Path).ToList());
            });
            List <string> locations = new List <string>()
            {
                @"C:\foobar"
            };

            // ACT
            await scannedFileStore.RescanLocationsAsync(locations, progress);

            // ASSERT
            Assert.AreEqual(progress.ReportedValues.Count, 100);
            Assert.AreEqual(progress.ReportedValues[49], 50);
            Assert.AreEqual(progress.ReportedValues[99], 100);
        }
        public async Task LoadScannedFileStoreFromFileAsync_HandlesFileNotFoundException()
        {
            // ARRANGE
            string                  filePath         = "C:\\foobar\\data.json";
            MockFileSystem          fileSystem       = new MockFileSystem();
            Mock <IFileHashService> service          = new Mock <IFileHashService>();
            ScannedFileStore        scannedFileStore = new ScannedFileStore(fileSystem, service.Object);

            // ACT
            await scannedFileStore.LoadScannedFileStoreFromFileAsync(filePath);

            // ASSERT
            Assert.IsFalse(fileSystem.FileExists("C:\\foobar\\data.json"));
        }
Exemplo n.º 14
0
        public void RescanRemoteDirectory_ExecutesStatusCallback()
        {
            // ARRANGE
            bool callbackExecuted = false;

            // ACT
            ScannedFileStore.RescanRemoteDirectory(_remotePath, t =>
            {
                callbackExecuted = true;
            });

            // ASSERT
            Assert.IsTrue(callbackExecuted);
        }
        public async Task RemoveFile_UpdatesProgress()
        {
            // ARRANGE
            MockFileSystem               fileSystem       = new MockFileSystem();
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();

            // ACT
            await scannedFileStore.RemoveFile(@"C:\foo\bar.txt", progress, 1, 1);

            // ASSERT
            Assert.AreEqual(100, progress.ReportedValues[0]);
        }
        public async Task RescanLocationAsync_PreviouslyScannedFilesPersist()
        {
            // ARRANGE
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();

            dictionaryMockFileData.Add(@"C:\foobar\file1.txt", new MockFileData("This is a test file."));
            MockFileSystem     fileSystem   = new MockFileSystem(dictionaryMockFileData);
            List <ScannedFile> scannedFiles = new List <ScannedFile>()
            {
                new ScannedFile()
                {
                    Name = "file1.txt",
                    Path = @"C:\foobar\file1.txt",
                    Hash = new byte[32]
                },
                new ScannedFile()
                {
                    Name = "file2.txt",
                    Path = @"C:\foobar\file2.txt",
                    Hash = new byte[32]
                }
            };
            List <ScannedLocation> scannedLocations = new List <ScannedLocation>()
            {
                new ScannedLocation()
                {
                    Path = @"C:\foobar"
                }
            };
            IDataCache <ScannedFile> scannedFileDataCache = new DataCache <ScannedFile>(scannedFiles);
            IFileHashService         service = new FileHashService(
                scannedFileDataCache,
                new DataCache <ScannedLocation>(scannedLocations));
            ScannedFileStore scannedFileStore = new ScannedFileStore(fileSystem, service);


            // ACT
            await scannedFileStore.RescanLocationsAsync(new List <string>
            {
                @"C:\foobar"
            }, new MockScannedFileStoreProgress());

            // ASSERT
            var result = scannedFileDataCache.ListData().ToList();

            Assert.AreEqual(1, result.Count);
            Assert.IsTrue(result[0].Path.Equals(@"C:\foobar\file1.txt"));
        }
Exemplo n.º 17
0
        public void InsertRemoteFile_InsertsNewEntry()
        {
            // ARRANGE
            RemoteFile file = new RemoteFile()
            {
                Hash = _fileHash,
                Name = "Foo.file",
                Path = "\\\\Some\\kind\\of\\path"
            };

            // ACT
            ScannedFileStore.InsertRemoteFile(file);

            // ASSERT
            Assert.IsTrue(ScannedFileStore.SearchRemoteFiles(_fileHash) > 0);
        }
        public async Task ScanFile_InsertsScannedFile()
        {
            // ARRANGE
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();

            dictionaryMockFileData.Add(@"C:\foo\bar.txt", new MockFileData("This is a test file."));
            MockFileSystem          fileSystem       = new MockFileSystem(dictionaryMockFileData);
            Mock <IFileHashService> service          = new Mock <IFileHashService>();
            ScannedFileStore        scannedFileStore = new ScannedFileStore(fileSystem, service.Object);

            // ACT
            await scannedFileStore.ScanFile(@"C:\foo\bar.txt", null, 0, 0);

            // ASSERT
            service.Verify(t => t.InsertScannedFileAsync(It.IsAny <ScannedFile>()), Times.Once());
        }
        public async Task PurgeLocationsAsync_CallsFileHashServicePurgesLocations()
        {
            // ARRANGE
            MockFileSystem          fileSystem       = new MockFileSystem();
            Mock <IFileHashService> service          = new Mock <IFileHashService>();
            ScannedFileStore        scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            List <string>           purgeLocations   = new List <string>()
            {
                @"C:\foobar"
            };

            // ACT
            await scannedFileStore.PurgeLocationsAsync(purgeLocations);

            // ASSERT
            service.Verify(t => t.PurgeScannedLocationsAsync(purgeLocations), Times.Once());
        }
Exemplo n.º 20
0
        public void Cleanup()
        {
            ScannedFileStore.DirectoryGetFiles = Directory.GetFiles;
            ScannedFileStore.FileExists        = File.Exists;

            ScannedFileStore.RemoveRemoteFilesByHash(_fileHash);
            string[] files = Directory.GetFiles(_remotePath, "*.*", SearchOption.AllDirectories);
            foreach (var file in files)
            {
                byte[] hash;
                using (FileStream stream = File.OpenRead(file))
                {
                    var sha = new System.Security.Cryptography.SHA256Managed();
                    hash = sha.ComputeHash(stream);
                }
                ScannedFileStore.RemoveRemoteFilesByHash(hash);
            }
        }
Exemplo n.º 21
0
        public void ScanRemoteDirectory_RebuildsRemoteFileTable()
        {
            // ARRANGE
            RemoteFile file = new RemoteFile()
            {
                Hash = _fileHash,
                Name = "Foo.file",
                Path = "\\\\Some\\kind\\of\\path"
            };

            ScannedFileStore.InsertRemoteFile(file);

            // ACT
            ScannedFileStore.ScanRemoteDirectory(_remotePath);

            // ASSERT
            Assert.IsTrue(ScannedFileStore.SearchRemoteFiles(_fileHash) == 0);
        }
        public async Task LoadScannedFileStoreFromFileAsync_LoadsScannedFilesToDataCaches()
        {
            // ARRANGE
            string                  filePath         = "C:\\foobar\\data.json";
            MockFileSystem          fileSystem       = new MockFileSystem();
            Mock <IFileHashService> service          = new Mock <IFileHashService>();
            ScannedFileStore        scannedFileStore = new ScannedFileStore(fileSystem, service.Object);

            fileSystem.AddFile(filePath, new MockFileData(
                                   "{\"files\": [{\"Name\": \"Foobar File\"}],\"locations\": [{\"Path\": \"Foobar Location\"}]}"));

            // ACT
            await scannedFileStore.LoadScannedFileStoreFromFileAsync(filePath);

            // ASSERT
            service.Verify(t => t.UpdateDataCaches(
                               It.IsAny <IDataCache <ScannedFile> >(), It.IsAny <IDataCache <ScannedLocation> >()), Times.Once);
        }
Exemplo n.º 23
0
        public void SearchRemoteFilesByHash_ExistingFile_ReturnsGreaterThanZero()
        {
            // ARRANGE
            RemoteFile file = new RemoteFile()
            {
                Hash = _fileHash,
                Name = "Foo.file",
                Path = "\\\\Some\\kind\\of\\path"
            };

            ScannedFileStore.InsertRemoteFile(file);

            // ACT
            int result = ScannedFileStore.SearchRemoteFiles(_fileHash);

            // ASSERT
            Assert.IsTrue(result > 0);
        }
Exemplo n.º 24
0
        public void RemoveRemoteFileByHash_RemovesEntry()
        {
            // ARRANGE
            RemoteFile file = new RemoteFile()
            {
                Hash = _fileHash,
                Name = "Foo.file",
                Path = "\\\\Some\\kind\\of\\path"
            };

            ScannedFileStore.InsertRemoteFile(file);

            // ACT
            ScannedFileStore.RemoveRemoteFilesByHash(_fileHash);

            // ASSERT
            Assert.IsTrue(ScannedFileStore.SearchRemoteFiles(_fileHash) == 0);
        }
Exemplo n.º 25
0
        public void ScanRemoteDirectory_BuildsRemoteFileTable()
        {
            // ARRANGE
            // ACT
            ScannedFileStore.ScanRemoteDirectory(_remotePath);

            // ASSERT
            string[] files = Directory.GetFiles(_remotePath, "*.*", SearchOption.AllDirectories);
            foreach (var file in files)
            {
                byte[] hash;
                using (FileStream stream = File.OpenRead(file))
                {
                    var sha = new System.Security.Cryptography.SHA256Managed();
                    hash = sha.ComputeHash(stream);
                }
                Assert.IsTrue(ScannedFileStore.SearchRemoteFiles(hash) > 0);
            }
        }
        public async Task ScanLocationAsync_ExistingScanedLocationsPurged()
        {
            // ARRANGE
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();

            dictionaryMockFileData.Add(@"C:\foobar\file.txt", new MockFileData("This is a test file{0}."));
            MockFileSystem               fileSystem       = new MockFileSystem(dictionaryMockFileData);
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();
            List <string> locations = new List <string>()
            {
                @"C:\foobar"
            };

            // ACT
            await scannedFileStore.ScanLocationsAsync(locations, progress);

            // ASSERT
            service.Verify(t => t.PurgeScannedLocationsAsync(It.IsAny <List <string> >()));
        }
        public async Task ScanLocationAsync_StoresScannedFilesFromDirectory()
        {
            // ARRANGE
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();

            dictionaryMockFileData.Add(@"C:\foobar\file1.txt", new MockFileData("This is a test file."));
            dictionaryMockFileData.Add(@"C:\foobar\file2.txt", new MockFileData("This is another test file."));
            MockFileSystem          fileSystem       = new MockFileSystem(dictionaryMockFileData);
            Mock <IFileHashService> service          = new Mock <IFileHashService>();
            ScannedFileStore        scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            List <string>           locations        = new List <string>()
            {
                @"C:\foobar"
            };

            // ACT
            await scannedFileStore.ScanLocationsAsync(locations, new MockScannedFileStoreProgress());

            // ASSERT
            service.Verify(t => t.InsertScannedFileAsync(It.Is <ScannedFile>(sf => sf.Path.Equals(@"C:\foobar\file1.txt"))), Times.Once);
            service.Verify(t => t.InsertScannedFileAsync(It.Is <ScannedFile>(sf => sf.Path.Equals(@"C:\foobar\file2.txt"))), Times.Once);
        }
Exemplo n.º 28
0
        public void RescanRemoteDirectory_FileNotFound_PassesOverFile()
        {
            // ARRANGE
            int numberOfFilesScanned = 0;

            ScannedFileStore.DirectoryGetFiles = (string path, string searchPatther, System.IO.SearchOption searchOption) =>
            {
                return(new string[] {
                    "foobar",
                    "foobar2"
                });
            };
            ScannedFileStore.FileExists = (string path) =>
            {
                numberOfFilesScanned++;
                return(false);
            };

            // ACT
            ScannedFileStore.RescanRemoteDirectory(_remotePath);

            // ASSERT
            Assert.AreEqual(numberOfFilesScanned, 2, "The number of files scanned does not match what was expected.");
        }