2019-11-14 22:22:53 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Alphaleonis.Win32.Filesystem;
|
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.VirtualFileSystem.Test
|
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class VFSTests
|
|
|
|
|
{
|
|
|
|
|
private const string VFS_TEST_DIR = "vfs_test_dir";
|
|
|
|
|
private static readonly string VFS_TEST_DIR_FULL = Path.Combine(Directory.GetCurrentDirectory(), VFS_TEST_DIR);
|
|
|
|
|
private Context context;
|
|
|
|
|
|
|
|
|
|
public TestContext TestContext { get; set; }
|
|
|
|
|
|
|
|
|
|
[TestInitialize]
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
|
|
|
|
Utils.LogMessages.Subscribe(f => TestContext.WriteLine(f));
|
|
|
|
|
if (Directory.Exists(VFS_TEST_DIR))
|
|
|
|
|
Directory.Delete(VFS_TEST_DIR, true);
|
|
|
|
|
Directory.CreateDirectory(VFS_TEST_DIR);
|
|
|
|
|
context = new Context();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2019-11-16 00:01:37 +00:00
|
|
|
|
public void FilesAreIndexed()
|
2019-11-14 22:22:53 +00:00
|
|
|
|
{
|
|
|
|
|
AddFile("test.txt", "This is a test");
|
2019-11-16 00:01:37 +00:00
|
|
|
|
AddTestRoot();
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
var file = context.Index.ByFullPath[Path.Combine(VFS_TEST_DIR_FULL, "test.txt")];
|
|
|
|
|
Assert.IsNotNull(file);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(file.Size, 14);
|
|
|
|
|
Assert.AreEqual(file.Hash, "qX0GZvIaTKM=");
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 00:01:37 +00:00
|
|
|
|
private void AddTestRoot()
|
2019-11-14 22:22:53 +00:00
|
|
|
|
{
|
2019-11-16 00:01:37 +00:00
|
|
|
|
context.AddRoot(VFS_TEST_DIR_FULL);
|
|
|
|
|
context.WriteToFile(Path.Combine(VFS_TEST_DIR_FULL, "vfs_cache.bin"));
|
|
|
|
|
context.IntegrateFromFile(Path.Combine(VFS_TEST_DIR_FULL, "vfs_cache.bin"));
|
2019-11-14 22:22:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public async Task ArchiveContentsAreIndexed()
|
|
|
|
|
{
|
|
|
|
|
AddFile("archive/test.txt", "This is a test");
|
|
|
|
|
ZipUpFolder("archive", "test.zip");
|
2019-11-16 00:01:37 +00:00
|
|
|
|
AddTestRoot();
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
var abs_path = Path.Combine(VFS_TEST_DIR_FULL, "test.zip");
|
|
|
|
|
var file = context.Index.ByFullPath[abs_path];
|
|
|
|
|
Assert.IsNotNull(file);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(128, file.Size);
|
|
|
|
|
Assert.AreEqual(abs_path.FileHash(), file.Hash);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(file.IsArchive);
|
|
|
|
|
var inner_file = file.Children.First();
|
|
|
|
|
Assert.AreEqual(14, inner_file.Size);
|
|
|
|
|
Assert.AreEqual("qX0GZvIaTKM=", inner_file.Hash);
|
|
|
|
|
Assert.AreSame(file, file.Children.First().Parent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public async Task DuplicateFileHashes()
|
|
|
|
|
{
|
|
|
|
|
AddFile("archive/test.txt", "This is a test");
|
|
|
|
|
ZipUpFolder("archive", "test.zip");
|
|
|
|
|
|
|
|
|
|
AddFile("test.txt", "This is a test");
|
2019-11-16 00:01:37 +00:00
|
|
|
|
AddTestRoot();
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var files = context.Index.ByHash["qX0GZvIaTKM="];
|
|
|
|
|
Assert.AreEqual(files.Count(), 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public async Task DeletedFilesAreRemoved()
|
|
|
|
|
{
|
|
|
|
|
AddFile("test.txt", "This is a test");
|
2019-11-16 00:01:37 +00:00
|
|
|
|
AddTestRoot();
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
var file = context.Index.ByFullPath[Path.Combine(VFS_TEST_DIR_FULL, "test.txt")];
|
|
|
|
|
Assert.IsNotNull(file);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(file.Size, 14);
|
|
|
|
|
Assert.AreEqual(file.Hash, "qX0GZvIaTKM=");
|
|
|
|
|
|
|
|
|
|
File.Delete(Path.Combine(VFS_TEST_DIR_FULL, "test.txt"));
|
|
|
|
|
|
2019-11-16 00:01:37 +00:00
|
|
|
|
AddTestRoot();
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
CollectionAssert.DoesNotContain(context.Index.ByFullPath, Path.Combine(VFS_TEST_DIR_FULL, "test.txt"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2019-11-16 00:01:37 +00:00
|
|
|
|
public void UnmodifiedFilesAreNotReIndexed()
|
2019-11-14 22:22:53 +00:00
|
|
|
|
{
|
|
|
|
|
AddFile("test.txt", "This is a test");
|
2019-11-16 00:01:37 +00:00
|
|
|
|
AddTestRoot();
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
var old_file = context.Index.ByFullPath[Path.Combine(VFS_TEST_DIR_FULL, "test.txt")];
|
|
|
|
|
var old_time = old_file.LastAnalyzed;
|
|
|
|
|
|
2019-11-16 00:01:37 +00:00
|
|
|
|
AddTestRoot();
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
var new_file = context.Index.ByFullPath[Path.Combine(VFS_TEST_DIR_FULL, "test.txt")];
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(old_time, new_file.LastAnalyzed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2019-11-16 00:01:37 +00:00
|
|
|
|
public void CanStageSimpleArchives()
|
2019-11-14 22:22:53 +00:00
|
|
|
|
{
|
|
|
|
|
AddFile("archive/test.txt", "This is a test");
|
|
|
|
|
ZipUpFolder("archive", "test.zip");
|
2019-11-16 00:01:37 +00:00
|
|
|
|
AddTestRoot();
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
var abs_path = Path.Combine(VFS_TEST_DIR_FULL, "test.zip");
|
|
|
|
|
var file = context.Index.ByFullPath[abs_path + "|test.txt"];
|
|
|
|
|
|
2019-11-16 00:01:37 +00:00
|
|
|
|
var cleanup = context.Stage(new List<VirtualFile> {file});
|
2019-11-14 22:22:53 +00:00
|
|
|
|
Assert.AreEqual("This is a test", File.ReadAllText(file.StagedPath));
|
|
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2019-11-16 00:01:37 +00:00
|
|
|
|
public void CanStageNestedArchives()
|
2019-11-14 22:22:53 +00:00
|
|
|
|
{
|
|
|
|
|
AddFile("archive/test.txt", "This is a test");
|
|
|
|
|
ZipUpFolder("archive", "test.zip");
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(Path.Combine(VFS_TEST_DIR_FULL, @"archive\other\dir"));
|
|
|
|
|
File.Move(Path.Combine(VFS_TEST_DIR_FULL, "test.zip"),
|
|
|
|
|
Path.Combine(VFS_TEST_DIR_FULL, @"archive\other\dir\nested.zip"));
|
|
|
|
|
ZipUpFolder("archive", "test.zip");
|
|
|
|
|
|
2019-11-16 00:01:37 +00:00
|
|
|
|
AddTestRoot();
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
var files = context.Index.ByHash["qX0GZvIaTKM="];
|
|
|
|
|
|
2019-11-16 00:01:37 +00:00
|
|
|
|
var cleanup = context.Stage(files);
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
Assert.AreEqual("This is a test", File.ReadAllText(file.StagedPath));
|
|
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2019-11-16 00:01:37 +00:00
|
|
|
|
public void CanRequestPortableFileTrees()
|
2019-11-14 22:22:53 +00:00
|
|
|
|
{
|
|
|
|
|
AddFile("archive/test.txt", "This is a test");
|
|
|
|
|
ZipUpFolder("archive", "test.zip");
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(Path.Combine(VFS_TEST_DIR_FULL, @"archive\other\dir"));
|
|
|
|
|
File.Move(Path.Combine(VFS_TEST_DIR_FULL, "test.zip"),
|
|
|
|
|
Path.Combine(VFS_TEST_DIR_FULL, @"archive\other\dir\nested.zip"));
|
|
|
|
|
ZipUpFolder("archive", "test.zip");
|
|
|
|
|
|
2019-11-16 00:01:37 +00:00
|
|
|
|
AddTestRoot();
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
var files = context.Index.ByHash["qX0GZvIaTKM="];
|
|
|
|
|
var archive = context.Index.ByRootPath[Path.Combine(VFS_TEST_DIR_FULL, "test.zip")];
|
|
|
|
|
|
|
|
|
|
var state = context.GetPortableState(files);
|
|
|
|
|
|
|
|
|
|
var new_context = new Context();
|
|
|
|
|
|
2019-11-16 00:01:37 +00:00
|
|
|
|
new_context.IntegrateFromPortable(state,
|
2019-11-14 22:22:53 +00:00
|
|
|
|
new Dictionary<string, string> {{archive.Hash, archive.FullPath}});
|
|
|
|
|
|
|
|
|
|
var new_files = new_context.Index.ByHash["qX0GZvIaTKM="];
|
|
|
|
|
|
2019-11-16 00:01:37 +00:00
|
|
|
|
var close = new_context.Stage(new_files);
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
foreach (var file in new_files)
|
|
|
|
|
Assert.AreEqual("This is a test", File.ReadAllText(file.StagedPath));
|
|
|
|
|
|
|
|
|
|
close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AddFile(string filename, string thisIsATest)
|
|
|
|
|
{
|
|
|
|
|
var fullpath = Path.Combine(VFS_TEST_DIR, filename);
|
|
|
|
|
if (!Directory.Exists(Path.GetDirectoryName(fullpath)))
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(fullpath));
|
|
|
|
|
File.WriteAllText(fullpath, thisIsATest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ZipUpFolder(string folder, string output)
|
|
|
|
|
{
|
|
|
|
|
var path = Path.Combine(VFS_TEST_DIR, folder);
|
|
|
|
|
ZipFile.CreateFromDirectory(path, Path.Combine(VFS_TEST_DIR, output));
|
|
|
|
|
Directory.Delete(path, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|