using Febe.Core.Export; using Febe.Tests.Export.Support; using Febe.Tests.Support; namespace Febe.Tests.Export; /// /// Where destinations may be chosen (spec §4.10, §11; decision D14 and question Q2 of tappa E2): any folder on Windows; /// in Docker only below /esporta, only when it is declared and the folder sits on a mount at /esporta or /// below it. The Docker root is a temporary folder and the mount table is given by the test, so this runs on every /// platform. /// [TestClass] public sealed class ExportRootTests { [TestMethod] public void DesktopHasNoRootAndIsActive() { var o = ExportRootOptions.AnyFolder with { Mounts = new FixedMounts() }; Assert.AreEqual(new ExportRootState(true, null, null), ExportRoots.Evaluate(o)); using var temp = new TempFolder(); Assert.IsNull(ExportRoots.MountProblem(o, temp.Path)); Assert.AreEqual(Path.GetFullPath(temp.Combine("usb")), ExportRoots.Resolve(o, temp.Combine("usb") + Path.DirectorySeparatorChar)); Assert.AreEqual(temp.Path, ExportRoots.DisplayPath(o, temp.Path)); Assert.AreEqual(ExportFolderProblemKind.NotFullyQualified, Kind(() => ExportRoots.Resolve(o, "usb"))); Assert.AreEqual(ExportFolderProblemKind.Empty, Kind(() => ExportRoots.Resolve(o, " "))); } [TestMethod] public void DockerNotDeclaredIsOffAndSaysWhy() { using var temp = new TempFolder(); var root = temp.Combine("esporta"); Directory.CreateDirectory(Path.Combine(root, "usb")); var o = ExportRootOptions.Docker(root, declared: false, new FixedMounts(Mount(root))); var state = ExportRoots.Evaluate(o); Assert.IsFalse(state.Active); Assert.AreEqual(Path.GetFullPath(root), state.Root); Assert.AreEqual(ExportRoots.NotDeclaredText, state.Problem); StringAssert.Contains(state.Problem, "FEBE_ESPORTA=1"); Assert.AreEqual(ExportRoots.NotDeclaredText, ExportRoots.MountProblem(o, Path.Combine(root, "usb"))); } [TestMethod] public void DockerDeclaredButNotMountedIsOff() { using var temp = new TempFolder(); var root = temp.Combine("esporta"); Directory.CreateDirectory(Path.Combine(root, "usb")); var o = ExportRootOptions.Docker(root, declared: true, new FixedMounts(Mount(Path.GetPathRoot(temp.Path)!), Mount(temp.Combine("altro")))); o = o with { ShownAs = "/esporta" }; var state = ExportRoots.Evaluate(o); Assert.IsFalse(state.Active); Assert.AreEqual("/esporta è dichiarata ma non è montata: Febe non scrive nulla finché non monti una cartella del server su /esporta.", state.Problem); Assert.AreEqual(state.Problem, ExportRoots.MountProblem(o, Path.Combine(root, "usb"))); } [TestMethod] public void DockerMountedAtTheRootIsActive() { using var temp = new TempFolder(); var root = temp.Combine("esporta"); Directory.CreateDirectory(Path.Combine(root, "usb", "Febe")); var o = ExportRootOptions.Docker(root, declared: true, new FixedMounts(Mount(Path.GetPathRoot(temp.Path)!), Mount(root))) with { ShownAs = "/esporta" }; Assert.IsTrue(ExportRoots.Evaluate(o).Active); Assert.IsNull(ExportRoots.MountProblem(o, Path.Combine(root, "usb"))); Assert.IsNull(ExportRoots.MountProblem(o, Path.Combine(root, "usb", "Febe"))); Assert.AreEqual(Path.GetFullPath(Path.Combine(root, "usb", "Febe")), ExportRoots.Resolve(o, "usb/Febe")); Assert.AreEqual(Path.GetFullPath(Path.Combine(root, "usb")), ExportRoots.Resolve(o, "usb/")); Assert.AreEqual(Path.GetFullPath(Path.Combine(root, "usb")), ExportRoots.Resolve(o, Path.Combine(root, "usb"))); Assert.AreEqual("/esporta/usb/Febe", ExportRoots.DisplayPath(o, Path.Combine(root, "usb", "Febe"))); Assert.AreEqual(ExportFolderProblemKind.OutsideExportRoot, Kind(() => ExportRoots.Resolve(o, temp.Combine("altro")))); } [TestMethod] public void DockerSubMountOnlyAcceptsFoldersOnTheMount() { using var temp = new TempFolder(); var root = temp.Combine("esporta"); var usb = Path.Combine(root, "usb"); Directory.CreateDirectory(Path.Combine(usb, "Febe")); Directory.CreateDirectory(Path.Combine(root, "immagine")); var o = ExportRootOptions.Docker(root, declared: true, new FixedMounts(Mount(Path.GetPathRoot(temp.Path)!), Mount(usb))) with { ShownAs = "/esporta" }; Assert.IsTrue(ExportRoots.Evaluate(o).Active); // a mount below /esporta is enough (Q2) Assert.IsNull(ExportRoots.MountProblem(o, usb)); Assert.IsNull(ExportRoots.MountProblem(o, Path.Combine(usb, "Febe"))); // A folder of /esporta that is not on the mount is the disk of the image: never written. var problem = ExportRoots.MountProblem(o, Path.Combine(root, "immagine")); Assert.AreEqual("/esporta/immagine non è su una cartella montata in /esporta: Febe non ci scrive nulla. Scegli una cartella dentro un montaggio di /esporta.", problem); Assert.IsNotNull(ExportRoots.MountProblem(o, Path.Combine(root, "usbx"))); } [TestMethod] public void ReadOnlyMountIsRefused() { using var temp = new TempFolder(); var root = temp.Combine("esporta"); var nas = Path.Combine(root, "nas"); Directory.CreateDirectory(nas); var o = ExportRootOptions.Docker(root, declared: true, new FixedMounts(Mount(root), Mount(nas, readOnly: true))) with { ShownAs = "/esporta" }; Assert.IsTrue(ExportRoots.Evaluate(o).Active); Assert.AreEqual("/esporta/nas/Febe è montata in sola lettura: Febe non può copiarci nulla.", ExportRoots.MountProblem(o, Path.Combine(nas, "Febe"))); Assert.IsNull(ExportRoots.MountProblem(o, Path.Combine(root, "usb"))); } [TestMethod] public void ParentSegmentsAreRefusedEvenWhenTheyStayInside() { using var temp = new TempFolder(); var root = temp.Combine("esporta"); Directory.CreateDirectory(Path.Combine(root, "usb")); var o = ExportRootOptions.Docker(root, declared: true, new FixedMounts(Mount(root))) with { ShownAs = "/esporta" }; foreach (var chosen in new[] { "usb/../usb", "usb/..", "../esporta/usb", @"usb\..\usb", "usb/../../etc" }) { var refused = Assert.ThrowsExactly(() => ExportRoots.Resolve(o, chosen), chosen); Assert.AreEqual(ExportFolderProblemKind.ParentSegment, refused.Kind, chosen); Assert.AreEqual("«..» non è ammesso: scegli una sottocartella di /esporta dall'elenco.", refused.Message); } Assert.AreEqual(ExportFolderProblemKind.ParentSegment, Kind(() => ExportRoots.Resolve(ExportRootOptions.AnyFolder, Path.Combine(temp.Path, "..", "x")))); Assert.AreEqual(ExportFolderProblemKind.ParentSegment, Kind(() => ExportRoots.Browse(o, "usb/.."))); Assert.AreEqual("..x", Path.GetFileName(ExportRoots.Resolve(o, "usb/..x"))); // a name that only starts with dots is a name } [TestMethod] public void TheRootItselfIsRefused() { using var temp = new TempFolder(); var root = temp.Combine("esporta"); Directory.CreateDirectory(root); var o = ExportRootOptions.Docker(root, declared: true, new FixedMounts(Mount(root))) with { ShownAs = "/esporta" }; // («/» alone is a full path on Linux, so it is refused there as outside the root: refused all the same.) foreach (var chosen in new[] { ".", "./", "./.", root, root + Path.DirectorySeparatorChar }) { var refused = Assert.ThrowsExactly(() => ExportRoots.Resolve(o, chosen), chosen); Assert.AreEqual(ExportFolderProblemKind.ExportRootItself, refused.Kind, chosen); Assert.AreEqual("Scegli una sottocartella di /esporta: /esporta stessa non è una destinazione.", refused.Message); } Assert.AreEqual(ExportFolderProblemKind.Empty, Kind(() => ExportRoots.Resolve(o, string.Empty))); Assert.IsEmpty(ExportRoots.Browse(o, null)); // the picker may open it, just not choose it } [TestMethod] public void BrowseListsSubfoldersWithoutLinksOrHiddenOnes() { using var temp = new TempFolder(); var root = temp.Combine("esporta"); Directory.CreateDirectory(Path.Combine(root, "usb", "Febe")); Directory.CreateDirectory(Path.Combine(root, "nas")); var hidden = Directory.CreateDirectory(Path.Combine(root, ".nascosta")); hidden.Attributes |= FileAttributes.Hidden; // on Linux the dot is enough File.WriteAllText(Path.Combine(root, "file.txt"), "non è una cartella"); if (!OperatingSystem.IsWindows()) { // A link out of the root (creating one on Windows needs rights a test does not have). Directory.CreateSymbolicLink(Path.Combine(root, "collegamento"), temp.Path); } var o = ExportRootOptions.Docker(root, declared: true, new FixedMounts(Mount(root))) with { ShownAs = "/esporta" }; var top = ExportRoots.Browse(o, null); CollectionAssert.AreEqual(new[] { "nas", "usb" }, top.Select(c => c.Name).ToArray()); Assert.AreEqual("usb", top[1].Path); // relative to the root Assert.IsTrue(top[1].HasSubfolders); Assert.IsFalse(top[0].HasSubfolders); Assert.AreEqual("usb/Febe", ExportRoots.Browse(o, "usb").Single().Path); var missing = Assert.ThrowsExactly(() => ExportRoots.Browse(o, "assente")); Assert.AreEqual(ExportFolderProblemKind.Missing, missing.Kind); Assert.AreEqual("La cartella /esporta/assente non esiste.", missing.Message); // Windows: a full path, and the whole tree is offered (links and hidden folders still skipped). var desktop = ExportRoots.Browse(ExportRootOptions.AnyFolder, root); CollectionAssert.AreEqual(new[] { Path.Combine(Path.GetFullPath(root), "nas"), Path.Combine(Path.GetFullPath(root), "usb") }, desktop.Select(c => c.Path).ToArray()); Assert.AreEqual(ExportFolderProblemKind.NotFullyQualified, Kind(() => ExportRoots.Browse(ExportRootOptions.AnyFolder, "usb"))); } internal static ExportMount Mount(string point, string root = "/", string type = "ext4", bool readOnly = false) => new(Path.GetFullPath(point), root, type, readOnly); private static ExportFolderProblemKind? Kind(Action action) => Assert.ThrowsExactly(action).Kind; }