using Febe.Core.Library; using Febe.Tests.Support; namespace Febe.Tests.Library; [TestClass] public sealed class DataFolderCheckTests { private const string Mounts = """ sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0 proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0 /dev/sda1 / ext4 rw,relatime 0 0 overlay /var/lib/docker/overlay2/x/merged overlay rw,relatime 0 0 shfs /mnt/user fuse.shfs rw,nosuid,nodev,noatime 0 0 //nas/foto /mnt/nas cifs rw,relatime,vers=3.1.1 0 0 nas:/export/dati /dati nfs4 rw,relatime,vers=4.2 0 0 alberto@server:/home /mnt/remoto fuse.sshfs rw,nosuid,nodev 0 0 C:\134 /mnt/c 9p rw,noatime,dirsync,aname=drvfs 0 0 /dev/sdb1 /mnt/disco\040esterno ext4 rw,relatime 0 0 tmpfs /tmp tmpfs rw,nosuid,nodev 0 0 /dev/sdc1 /dati/locale xfs rw 0 0 """; [TestMethod] public void ParsesProcMountsWithEscapes() { var entries = MountTable.Parse(Mounts); Assert.HasCount(12, entries); Assert.Contains(new MountEntry("/dev/sdb1", "/mnt/disco esterno", "ext4"), entries); Assert.Contains(new MountEntry("C:\\", "/mnt/c", "9p"), entries); Assert.AreEqual("a\tb\nc\\d", MountTable.Unescape("a\\011b\\012c\\134d")); Assert.AreEqual("\\09x", MountTable.Unescape("\\09x"), "Non è una sequenza ottale: resta com'è."); Assert.IsEmpty(MountTable.Parse("due campi\n\n# commento")); } [TestMethod] [DataRow("/dati", "nfs4")] [DataRow("/dati/febe", "nfs4")] [DataRow("/mnt/nas/Febe/dati", "cifs")] [DataRow("/mnt/remoto/x", "fuse.sshfs")] [DataRow("/mnt/c/Users/Alberto/Febe", "9p")] public void FindsNetworkMounts(string path, string fileSystem) { var mount = MountTable.FindContaining(MountTable.Parse(Mounts), path); Assert.IsNotNull(mount); Assert.AreEqual(fileSystem, mount.FileSystemType); Assert.IsTrue(MountTable.IsNetworkFileSystem(mount.FileSystemType)); if (!OperatingSystem.IsWindows()) { var problem = new DataFolderCheck(() => Mounts).DetectNetwork(path); Assert.IsNotNull(problem); Assert.AreEqual(DataFolderProblemKind.NetworkPath, problem.Kind); Assert.AreEqual(fileSystem, problem.FileSystem); Assert.AreEqual("La cartella dei dati è su un disco di rete", problem.Title); Assert.Contains("il motore non è partito", problem.Explanation); Assert.IsFalse(string.IsNullOrWhiteSpace(problem.Remedy)); } } [TestMethod] [DataRow("/dati/locale/febe", "xfs")] [DataRow("/mnt/user/appdata/febe", "fuse.shfs")] [DataRow("/var/lib/docker/overlay2/x/merged/dati", "overlay")] [DataRow("/home/alberto/febe", "ext4")] [DataRow("/tmp/febe", "tmpfs")] [DataRow("/datiextra", "ext4")] [DataRow("/mnt/disco esterno/febe", "ext4")] public void LocalMountsAreFine(string path, string fileSystem) { var mount = MountTable.FindContaining(MountTable.Parse(Mounts), path); Assert.AreEqual(fileSystem, mount?.FileSystemType); Assert.IsFalse(MountTable.IsNetworkFileSystem(fileSystem)); if (!OperatingSystem.IsWindows()) { Assert.IsNull(new DataFolderCheck(() => Mounts).DetectNetwork(path)); } } [TestMethod] [DataRow(@"\\nas\febe\dati", true)] [DataRow(@"\\?\UNC\nas\febe", true)] [DataRow(@"\\.\UNC\nas\febe", true)] [DataRow(@"\\?\C:\Febe", false)] [DataRow(@"\\.\C:\Febe", false)] [DataRow(@"C:\Users\Alberto\AppData\Local\Febe", false)] [DataRow("/dati", false)] public void RecognizesUncPaths(string path, bool expected) { Assert.AreEqual(expected, DataFolderCheck.IsUncPath(path)); } [TestMethod] public void UncPathsAreAlwaysNetworkProblems() { var problem = new DataFolderCheck(() => null).DetectNetwork(@"\\nas\febe\dati"); Assert.AreEqual(DataFolderProblemKind.NetworkPath, problem?.Kind); } [TestMethod] public void ALocalWritableFolderPassesAndIsCreated() { using var temp = new TempFolder(); var path = temp.Combine("dati", "nuova"); Assert.IsNull(new DataFolderCheck(() => "/dev/sda1 / ext4 rw 0 0").Check(path)); Assert.IsTrue(Directory.Exists(path)); Assert.IsEmpty(Directory.GetFiles(path), "La prova di scrittura non lascia file."); } [TestMethod] public void AFileOrAnImpossibleFolderIsReported() { using var temp = new TempFolder(); var file = temp.Combine("un-file"); File.WriteAllText(file, "x"); var check = new DataFolderCheck(() => null); Assert.AreEqual(DataFolderProblemKind.NotAFolder, check.Check(file)?.Kind); var below = check.Check(Path.Combine(file, "dati")); Assert.AreEqual(DataFolderProblemKind.CannotCreate, below?.Kind); Assert.Contains("Febe non riesce a creare", below!.Title); } [TestMethod] public void AFolderThatCannotBeWrittenIsReported() { if (!OperatingSystem.IsLinux() || !Directory.Exists("/proc/self")) { Assert.Inconclusive("Serve /proc di Linux: una cartella in cui nemmeno root può scrivere."); } var problem = new DataFolderCheck(() => null).Check("/proc/self"); Assert.AreEqual(DataFolderProblemKind.NotWritable, problem?.Kind); Assert.AreEqual("Febe non può scrivere nella cartella dei dati", problem!.Title); Assert.Contains("PUID", problem.Remedy); } }