Exemplo n.º 1
0
        public async Task ChangeArchiveLabel()
        {
            // Arrange
            // Prepare DB & files
            Assert.IsTrue(PrepareDirectories());
            Assert.IsTrue(await ResetDatabase(), "Could not create database");

            var targetDir = Path.Combine(testDirD, "dir1");

            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "KeyMap.txt")), Path.Combine(targetDir, "KeyMap.txt"));

            var oldLabel = "oldLabel";
            var newLabel = "newLabel";

            // Act
            var refFi      = new FileIndex();
            var refArchive = await refFi.CreateArchiveAsync(new DirectoryInfo(targetDir), new CancellationTokenSource().Token, new Progress <int>(), new Progress <string>(), oldLabel);

            await refArchive.UpdateLabel(newLabel);

            var diffFi = new FileIndex();
            await diffFi.LoadDataAsync();

            var diffArchive = diffFi.Archives.FirstOrDefault(x => x.Guid.Equals(refArchive.Guid));

            // Assert
            Assert.AreEqual(newLabel, refArchive.Label, "Label was not updated correctly.");
            Assert.AreEqual(newLabel, diffArchive.Label, "New Label was not correctly written to DB.");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Populates the index with data stored in the database.</summary>
        public async Task LoadDataAsync()
        {
            IsOperationInProgress = true;

            await index.LoadDataAsync();

            IsOperationInProgress = false;
        }
Exemplo n.º 3
0
        public async Task UpdateArchive()
        {
            // Arrange
            // Prepare DB & files
            Assert.IsTrue(PrepareDirectories());
            Assert.IsTrue(await ResetDatabase(), "Could not create database");

            var targetDir1 = Path.Combine(testDirD, "dir1");
            var targetDir2 = Path.Combine(testDirD, "dir2");

            // f1 stays the same
            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "KeyMap.txt")), Path.Combine(targetDir1, "KeyMap.txt"));

            // f2 gets moved from dir1 to dir2 after scanning
            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "0266554465.jpeg")), Path.Combine(targetDir1, "0266554465.jpeg"));

            // f3 is removed after scanning
            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "Nikon-1-V3-sample-photo.jpg")), Path.Combine(targetDir1, "Nikon-1-V3-sample-photo.jpg"));

            var fi1      = new FileIndex();
            var archive1 = await fi1.CreateArchiveAsync(new DirectoryInfo(testDirD), new CancellationTokenSource().Token, new Progress <int>(), new Progress <string>(), "archive1");

            var archive1Guid = archive1.Guid;

            var f1 = archive1.GetFileNodes().FirstOrDefault(x => x.FullSessionName.Equals(Path.Combine(targetDir1, "KeyMap.txt")));
            var f2 = archive1.GetFileNodes().FirstOrDefault(x => x.FullSessionName.Equals(Path.Combine(targetDir1, "0266554465.jpeg")));
            var f3 = archive1.GetFileNodes().FirstOrDefault(x => x.FullSessionName.Equals(Path.Combine(targetDir1, "Nikon-1-V3-sample-photo.jpg")));

            var f1Hash = f1.Checksum;
            var f2Hash = f2.Checksum;
            var f3Hash = f3.Checksum;

            // Act

            // f4 is newly added
            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "umlaut_äü(&テスト.txt")), Path.Combine(targetDir1, "umlaut_äü(&テスト.txt"));

            File.Move(Path.Combine(targetDir1, "0266554465.jpeg"), Path.Combine(targetDir2, "0266554465.jpeg"));
            File.Delete(Path.Combine(targetDir1, "Nikon-1-V3-sample-photo.jpg"));

            await fi1.UpdateArchiveAsync(archive1, new CancellationTokenSource().Token, new Progress <int>(), new Progress <string>());

            archive1 = fi1.Archives.FirstOrDefault(x => x.Guid.Equals(archive1Guid));

            // Also check if the changes are correctly written to the DB
            var fi2 = new FileIndex();
            await fi2.LoadDataAsync();

            var archive2 = fi2.Archives.FirstOrDefault(x => x.Guid.Equals(archive1Guid));

            // Assert
            Assert.IsNotNull(archive1, "Archive Guid was changed");
            Assert.IsNotNull(archive2, "Archive Guid was changed");

            Assert.AreEqual(3, archive1.GetFileNodes().Count(), "FileNode count incorrect.");
            Assert.AreEqual(1, archive1.GetFileNodes().Where(x => x.FullSessionName.Equals(Path.Combine(targetDir1, "KeyMap.txt"))).Count(), "FileNode not found.");
            Assert.AreEqual(1, archive1.GetFileNodes().Where(x => x.FullSessionName.Equals(Path.Combine(targetDir2, "0266554465.jpeg"))).Count(), "FileNode not found.");
            Assert.AreEqual(0, archive1.GetFileNodes().Where(x => x.FullSessionName.Equals(Path.Combine(targetDir1, "Nikon-1-V3-sample-photo.jpg"))).Count(), "FileNode not found.");
            Assert.AreEqual(1, archive1.GetFileNodes().Where(x => x.FullSessionName.Equals(Path.Combine(targetDir1, "umlaut_äü(&テスト.txt"))).Count(), "FileNode not found.");


            Assert.AreEqual(3, archive2.GetFileNodes().Count(), "FileNode count incorrect.");
            Assert.AreEqual(1, archive2.GetFileNodes().Where(x => x.FullSessionName.Equals(Path.Combine(targetDir1, "KeyMap.txt"))).Count(), "FileNode not found.");
            Assert.AreEqual(1, archive2.GetFileNodes().Where(x => x.FullSessionName.Equals(Path.Combine(targetDir2, "0266554465.jpeg"))).Count(), "FileNode not found.");
            Assert.AreEqual(0, archive2.GetFileNodes().Where(x => x.FullSessionName.Equals(Path.Combine(targetDir1, "Nikon-1-V3-sample-photo.jpg"))).Count(), "FileNode not found.");
            Assert.AreEqual(1, archive2.GetFileNodes().Where(x => x.FullSessionName.Equals(Path.Combine(targetDir1, "umlaut_äü(&テスト.txt"))).Count(), "FileNode not found.");


            Assert.AreEqual(3, fi1.Hashes.Count, "FileHash count incorrect.");
            Assert.AreEqual(1, fi1.Hashes.Where(x => x.Checksum.Equals(f1Hash)).Count(), "FileHash not found.");
            Assert.AreEqual(1, fi1.Hashes.Where(x => x.Checksum.Equals(f2Hash)).Count(), "FileHash not found.");
            Assert.AreEqual(0, fi1.Hashes.Where(x => x.Checksum.Equals(f3Hash)).Count(), "FileHash not found.");

            Assert.AreEqual(3, fi2.Hashes.Count, "FileHash count incorrect.");
            Assert.AreEqual(1, fi2.Hashes.Where(x => x.Checksum.Equals(f1Hash)).Count(), "FileHash not found.");
            Assert.AreEqual(1, fi2.Hashes.Where(x => x.Checksum.Equals(f2Hash)).Count(), "FileHash not found.");
            Assert.AreEqual(0, fi2.Hashes.Where(x => x.Checksum.Equals(f3Hash)).Count(), "FileHash not found.");
        }
Exemplo n.º 4
0
        public async Task DeleteArchive()
        {
            // Arrange
            // Prepare DB & files
            Assert.IsTrue(PrepareDirectories());
            Assert.IsTrue(await ResetDatabase(), "Could not create database");

            var targetDir = Path.Combine(testDirD, "dir1");

            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "KeyMap.txt")), Path.Combine(targetDir, "KeyMap.txt"));
            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "0266554465.jpeg")), Path.Combine(targetDir, "0266554465.jpeg"));
            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "Nikon-1-V3-sample-photo.jpg")), Path.Combine(targetDir, "Nikon-1-V3-sample-photo.jpg"));
            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "randomExe.exe")), Path.Combine(targetDir, "randomExe.exe"));
            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "umlaut_äü(&テスト.txt")), Path.Combine(targetDir, "umlaut_äü(&テスト.txt"));

            var targetDir2 = Path.Combine(testDirD, "dir2");

            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "KeyMap.txt")), Path.Combine(targetDir2, "KeyMap.txt"));

            var targetDir3 = Path.Combine(testDirF, "dir1");

            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "0266554465.jpeg")), Path.Combine(targetDir3, "0266554465.jpeg"));


            // Act
            var refFi = new FileIndex();

            var refArchives = new List <Archive>();
            var archive1    = await refFi.CreateArchiveAsync(new DirectoryInfo(targetDir), new CancellationTokenSource().Token, new Progress <int>(), new Progress <string>(), "DeleteArchive1");

            refArchives.Add(archive1);

            // Archive on same volume
            var archive2 = await refFi.CreateArchiveAsync(new DirectoryInfo(targetDir2), new CancellationTokenSource().Token, new Progress <int>(), new Progress <string>(), "DeleteArchive2");

            refArchives.Add(archive2);

            // Archive on different volume
            var archive3 = await refFi.CreateArchiveAsync(new DirectoryInfo(targetDir3), new CancellationTokenSource().Token, new Progress <int>(), new Progress <string>(), "DeleteArchive3");

            refArchives.Add(archive3);

            // Assert
            // Remove each archive from the index and check if all data
            // (and only data from the deleted archive) has been removed
            // from the file index in memory and the db after each deletion

            // Remove first archive
            await refFi.RemoveArchiveAsync(archive1, true);

            refArchives.Remove(archive1);
            archive1 = null;
            var diffFi = refFi;

            DeleteArchive_CheckEquality(refArchives, diffFi);

            diffFi = new FileIndex();
            await diffFi.LoadDataAsync();

            DeleteArchive_CheckEquality(refArchives, diffFi);

            // Remove second archive
            await refFi.RemoveArchiveAsync(archive2, true);

            refArchives.Remove(archive2);
            archive2 = null;
            diffFi   = refFi;
            DeleteArchive_CheckEquality(refArchives, diffFi);

            diffFi = new FileIndex();
            await diffFi.LoadDataAsync();

            DeleteArchive_CheckEquality(refArchives, diffFi);

            // Remove third archive, all data should now be removed
            await refFi.RemoveArchiveAsync(archive3, true);

            refArchives.Remove(archive3);
            archive3 = null;
            diffFi   = refFi;

            Assert.AreEqual(0, diffFi.Archives.Count, "Archive count incorrect.");
            Assert.AreEqual(0, diffFi.LogicalVolumes.Count, "LogicalVolume count incorrect.");
            Assert.AreEqual(0, diffFi.Hashes.Count, "FileHash count incorrect.");

            diffFi = new FileIndex();
            await diffFi.LoadDataAsync();

            Assert.AreEqual(0, diffFi.Archives.Count, "Archive count incorrect.");
            Assert.AreEqual(0, diffFi.LogicalVolumes.Count, "LogicalVolume count incorrect.");
            Assert.AreEqual(0, diffFi.Hashes.Count, "FileHash count incorrect.");


            // Also make sure that all data was removed from the database

            using (var dbConn = new SQLiteConnection(Database.GetConnectionString(), true))
            {
                await dbConn.OpenAsync();

                foreach (var tableName in dbTables)
                {
                    var sqlCmd = new SQLiteCommand("SELECT count(*) AS tableCount FROM " + tableName, dbConn);

                    using (var reader = await sqlCmd.ExecuteReaderAsync())
                    {
                        while (await reader.ReadAsync())
                        {
                            Assert.AreEqual(0, int.Parse(reader["tableCount"].ToString()), "Not all entries were deleted in table " + tableName);
                        }
                    }
                }

                if (dbConn.State == ConnectionState.Open)
                {
                    dbConn.Close();
                }
            }
        }
Exemplo n.º 5
0
        public async Task WriteToDatabase()
        {
            // Arrange
            // Prepare DB & files
            Assert.IsTrue(PrepareDirectories());
            Assert.IsTrue(await ResetDatabase(), "Could not create database");

            var targetDir = Path.Combine(testDirD, "dir1");

            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "KeyMap.txt")), Path.Combine(targetDir, "KeyMap.txt"));
            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "0266554465.jpeg")), Path.Combine(targetDir, "0266554465.jpeg"));
            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "Nikon-1-V3-sample-photo.jpg")), Path.Combine(targetDir, "Nikon-1-V3-sample-photo.jpg"));
            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "randomExe.exe")), Path.Combine(targetDir, "randomExe.exe"));
            File.Copy(Path.GetFullPath(Path.Combine(testFileDir, "umlaut_äü(&テスト.txt")), Path.Combine(targetDir, "umlaut_äü(&テスト.txt"));

            var exclusionString1 = @"\.exe";
            var exclusionString2 = @".*\subdir1";

            // Act
            var refFi = new FileIndex();
            await refFi.CreateArchiveAsync(new DirectoryInfo(targetDir), new CancellationTokenSource().Token, new Progress <int>(), new Progress <string>(), "testArchive");

            await refFi.AddFileExclusionAsync(exclusionString1, true);

            await refFi.AddFileExclusionAsync(exclusionString2, true);

            var diffFi = new FileIndex();
            await diffFi.LoadDataAsync();

            // Assert
            Assert.AreEqual(refFi.Archives.Count, diffFi.Archives.Count, "Archive count incorrect.");

            foreach (var refArchive in refFi.Archives)
            {
                Assert.IsTrue(diffFi.Archives.Contains(refArchive), "Archive not found.");

                var diffArchive = diffFi.Archives.FirstOrDefault(x => x.Equals(refArchive));


                Assert.AreEqual(refArchive.GetFileNodes().Count, diffArchive.GetFileNodes().Count, "FileNodes count incorrect.");

                foreach (var refNode in refArchive.GetFileNodes())
                {
                    Assert.IsTrue(diffArchive.GetFileNodes().Contains(refNode), "FileNode not found.");
                }

                Assert.AreEqual(refArchive.GetFileDirectories().Count, diffArchive.GetFileDirectories().Count, "FileDirectory count incorrect.");

                foreach (var refNode in refArchive.GetFileDirectories())
                {
                    Assert.IsTrue(diffArchive.GetFileDirectories().Contains(refNode), "FileDirectory not found.");
                }
            }

            Assert.AreEqual(refFi.LogicalVolumes.Count, diffFi.LogicalVolumes.Count, "LogicalVolume count incorrect.");

            foreach (var refVolume in refFi.LogicalVolumes)
            {
                Assert.IsTrue(diffFi.LogicalVolumes.Contains(refVolume), "LogicalVolume not found.");
            }

            Assert.AreEqual(refFi.Hashes.Count, diffFi.Hashes.Count, "FileHash count incorrect.");
            foreach (var refHash in refFi.Hashes)
            {
                Assert.IsTrue(diffFi.Hashes.Contains(refHash), "FileHash not found.");
                Assert.AreEqual(refHash, diffFi.Hashes.FirstOrDefault(x => x.Equals(refHash)), "FileHash not equal.");
            }
            Assert.IsTrue(diffFi.Exclusions.Contains(exclusionString1), "Exclusion not found.");
            Assert.IsTrue(diffFi.Exclusions.Contains(exclusionString2), "Exclusion not found.");
        }