using Febe.Core.Export; using Febe.Core.Library; namespace Febe.Tests.Export; /// /// /proc/self/mountinfo as the export reads it (spec §4.10 «Docker e Windows», decision D14 of tappa E2). The /// content is always given by the test: nothing reads the mounts of the machine that runs the tests. /// [TestClass] public sealed class MountInfoTests { [TestMethod] public void ParsesMountPointRootTypeAndOptionsWithOptionalFields() { const string content = """ 22 1 8:1 / / rw,relatime shared:1 - ext4 /dev/sda1 rw,errors=remount-ro 40 22 8:17 /backup/foto /esporta/usb rw,noatime master:3 propagate_from:2 - vfat /dev/sdb1 rw,fmask=0022 41 22 0:50 / /esporta/nas ro,relatime - cifs //nas/copie rw,vers=3.0 """; var mounts = MountInfoTable.Parse(content); Assert.HasCount(3, mounts); var usb = mounts[1]; Assert.AreEqual((40, 22), (usb.Id, usb.ParentId)); Assert.AreEqual("/backup/foto", usb.Root); Assert.AreEqual("/esporta/usb", usb.MountPoint); CollectionAssert.AreEqual(new[] { "rw", "noatime" }, usb.Options.ToArray()); Assert.AreEqual("vfat", usb.FileSystemType); // after the optional fields and their separator «-» Assert.AreEqual("/dev/sdb1", usb.Source); Assert.IsFalse(usb.ReadOnly); Assert.AreEqual("ext4", mounts[0].FileSystemType); Assert.AreEqual("//nas/copie", mounts[2].Source); Assert.IsTrue(mounts[2].ReadOnly); // the per-mount options say ro, the super options say rw } [TestMethod] public void UnescapesSpacesAndTabsInPaths() { var mounts = MountInfoTable.Parse(@"50 22 8:33 /foto\040di\011Marta /esporta/disco\040usb rw - ext4 /dev/sdc\0401 rw"); Assert.AreEqual("/foto di\tMarta", mounts.Single().Root); Assert.AreEqual("/esporta/disco usb", mounts[0].MountPoint); Assert.AreEqual("/dev/sdc 1", mounts[0].Source); Assert.AreEqual("/esporta/disco usb", MountInfoTable.FindContaining(mounts, "/esporta/disco usb/Febe")!.MountPoint); } [TestMethod] public void SkipsMalformedLines() { const string content = """ riga senza senso 22 1 8:1 / / rw - ext4 /dev/sda1 rw x 1 8:1 / /a rw - ext4 /dev/sda2 rw 23 y 8:1 / /b rw - ext4 /dev/sda3 rw 24 1 8:1 / /c rw shared:1 ext4 /dev/sda4 rw extra 25 1 8:1 / /d rw - 26 1 8:1 relativa /e rw - ext4 /dev/sda6 rw 27 1 8:1 / relativo rw - ext4 /dev/sda7 rw 28 1 8:1 / /f rw """; var mounts = MountInfoTable.Parse(content.Replace("\n", "\r\n", StringComparison.Ordinal)); Assert.AreEqual("/", mounts.Single().MountPoint); Assert.IsEmpty(MountInfoTable.Parse(string.Empty)); Assert.IsEmpty(new ProcMountInfo(() => null).All()); Assert.IsNull(new ProcMountInfo(() => throw new IOException("letto male")).FindContaining("/esporta/usb")); } [TestMethod] public void FindsTheLongestMountContainingAPath() { const string content = """ 22 1 8:1 / / rw - ext4 /dev/sda1 rw 40 22 8:17 / /esporta rw - ext4 /dev/sdb1 rw 41 40 8:33 / /esporta/usb rw - vfat /dev/sdc1 rw 42 40 8:49 / /esporta/usb rw - exfat /dev/sdd1 rw """; var mounts = MountInfoTable.Parse(content); Assert.AreEqual("exfat", MountInfoTable.FindContaining(mounts, "/esporta/usb/Febe/marta.demo")!.FileSystemType); // the last mount over a point wins Assert.AreEqual("exfat", MountInfoTable.FindContaining(mounts, "/esporta/usb/")!.FileSystemType); Assert.AreEqual("/esporta", MountInfoTable.FindContaining(mounts, "/esporta/usbx")!.MountPoint); // not a prefix of a segment Assert.AreEqual("/esporta", MountInfoTable.FindContaining(mounts, "/esporta")!.MountPoint); Assert.AreEqual("/", MountInfoTable.FindContaining(mounts, "/dati")!.MountPoint); Assert.IsNull(MountInfoTable.FindContaining([], "/esporta")); var proc = new ProcMountInfo(() => content); Assert.AreEqual(new ExportMount("/esporta/usb", "/", "exfat", false), proc.FindContaining("/esporta/usb/a")); Assert.HasCount(4, proc.All()); } }