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; }
|
2019-11-17 04:16:42 +00:00
|
|
|
|
public WorkQueue Queue { get; set; }
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
[TestInitialize]
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
2019-12-04 04:12:08 +00:00
|
|
|
|
Utils.LogMessages.Subscribe(f => TestContext.WriteLine(f.ShortDescription));
|
2019-11-14 22:22:53 +00:00
|
|
|
|
if (Directory.Exists(VFS_TEST_DIR))
|
2019-11-23 17:37:24 +00:00
|
|
|
|
Utils.DeleteDirectory(VFS_TEST_DIR);
|
2019-11-14 22:22:53 +00:00
|
|
|
|
Directory.CreateDirectory(VFS_TEST_DIR);
|
2019-11-17 04:16:42 +00:00
|
|
|
|
Queue = new WorkQueue();
|
|
|
|
|
context = new Context(Queue);
|
2019-11-14 22:22:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2019-12-04 01:26:26 +00:00
|
|
|
|
public async Task FilesAreIndexed()
|
2019-11-14 22:22:53 +00:00
|
|
|
|
{
|
|
|
|
|
AddFile("test.txt", "This is a test");
|
2019-12-04 01:26:26 +00:00
|
|
|
|
await 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-12-04 01:26:26 +00:00
|
|
|
|
private async Task AddTestRoot()
|
2019-11-14 22:22:53 +00:00
|
|
|
|
{
|
2019-12-04 01:26:26 +00:00
|
|
|
|
await context.AddRoot(VFS_TEST_DIR_FULL);
|
|
|
|
|
await context.WriteToFile(Path.Combine(VFS_TEST_DIR_FULL, "vfs_cache.bin"));
|
2019-12-07 02:54:27 +00:00
|
|
|
|
await 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-12-04 01:26:26 +00:00
|
|
|
|
await 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-12-04 01:26:26 +00:00
|
|
|
|
await 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-12-04 01:26:26 +00:00
|
|
|
|
await 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-12-04 01:26:26 +00:00
|
|
|
|
await AddTestRoot();
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
CollectionAssert.DoesNotContain(context.Index.ByFullPath, Path.Combine(VFS_TEST_DIR_FULL, "test.txt"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2019-12-04 01:26:26 +00:00
|
|
|
|
public async Task UnmodifiedFilesAreNotReIndexed()
|
2019-11-14 22:22:53 +00:00
|
|
|
|
{
|
|
|
|
|
AddFile("test.txt", "This is a test");
|
2019-12-04 01:26:26 +00:00
|
|
|
|
await 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-12-04 01:26:26 +00:00
|
|
|
|
await 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-12-04 01:26:26 +00:00
|
|
|
|
public async Task CanStageSimpleArchives()
|
2019-11-14 22:22:53 +00:00
|
|
|
|
{
|
|
|
|
|
AddFile("archive/test.txt", "This is a test");
|
|
|
|
|
ZipUpFolder("archive", "test.zip");
|
2019-12-04 01:26:26 +00:00
|
|
|
|
await 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-12-04 01:26:26 +00:00
|
|
|
|
var cleanup = await 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-12-04 01:26:26 +00:00
|
|
|
|
public async Task 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-12-04 01:26:26 +00:00
|
|
|
|
await AddTestRoot();
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
|
|
|
|
var files = context.Index.ByHash["qX0GZvIaTKM="];
|
|
|
|
|
|
2019-12-04 01:26:26 +00:00
|
|
|
|
var cleanup = await 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-12-04 01:26:26 +00:00
|
|
|
|
public async Task 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-12-04 01:26:26 +00:00
|
|
|
|
await 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);
|
|
|
|
|
|
2019-11-17 04:16:42 +00:00
|
|
|
|
var new_context = new Context(Queue);
|
2019-11-14 22:22:53 +00:00
|
|
|
|
|
2019-12-04 01:26:26 +00:00
|
|
|
|
await 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-12-04 01:26:26 +00:00
|
|
|
|
var close = await 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));
|
2019-11-23 17:37:24 +00:00
|
|
|
|
Utils.DeleteDirectory(path);
|
2019-11-14 22:22:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-23 17:37:24 +00:00
|
|
|
|
}
|