Le prove della tappa E2 (WP5a) coprono il montaggio di /esporta, la radice, il controllo delle cartelle, le destinazioni, i percorsi, la copia, gli stati «Tolto» e «Superato», lo stesso post in due account, il lavoro in sottofondo (interrompi, riprendi, annulla, riavvio, porta condivisa con importazione e ricostruzione, sessione del motore, interruttore dell'account, Docker senza montaggio) e il filtro «Copie». Si appoggiano a un banco comune con falsi per disco, sessione, montaggi e file system, senza permessi dei file, così girano anche da root. Le prove hanno trovato due difetti, corretti: - MarkCopiesRemovedAsync riusava il comando in cache del primo blocco di 900 id: «Controlla le copie» falliva con più di 900 file mancanti in una pagina. - ReplacedStatesSql senza statistiche leggeva tutte le copie tolte o superate della destinazione: «+state» la tiene sul suo indice. Il controllo delle cartelle accetta una scrittura di prova sostituibile, per provare il guasto della sonda senza permessi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
288 lines
16 KiB
C#
288 lines
16 KiB
C#
using Febe.Core.Export;
|
|
using Febe.Core.Library;
|
|
using Febe.Tests.Export.Support;
|
|
using Febe.Tests.Support;
|
|
|
|
namespace Febe.Tests.Export;
|
|
|
|
/// <summary>
|
|
/// «Cartelle rifiutate» of spec §4.10 (decision D13 of tappa E2): textual checks, then the identity test with a probe
|
|
/// written only in the candidate. Failures of the disk are injected through the probe seam, never provoked with
|
|
/// permissions (the CI runs as root, which writes anywhere).
|
|
/// </summary>
|
|
[TestClass]
|
|
public sealed class ExportFolderCheckTests
|
|
{
|
|
public TestContext TestContext { get; set; } = null!;
|
|
|
|
[TestMethod]
|
|
public async Task AMissingFolderIsRefusedAndNeverCreated()
|
|
{
|
|
using var temp = new TempFolder();
|
|
var missing = temp.Combine("disco", "Febe");
|
|
var verdict = await Check().CheckAsync(missing, Context(temp), TestContext.CancellationToken);
|
|
Assert.AreEqual(ExportFolderProblemKind.Missing, verdict.Problem!.Kind);
|
|
Assert.StartsWith($"La cartella {Path.GetFullPath(missing)} non esiste. Febe non crea mai la cartella di una destinazione", verdict.Problem.Message);
|
|
Assert.IsFalse(Directory.Exists(temp.Combine("disco")));
|
|
Assert.IsFalse(verdict.Accepted);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AFileIsNotAFolder()
|
|
{
|
|
using var temp = new TempFolder();
|
|
var file = temp.Combine("copie.txt");
|
|
await File.WriteAllTextAsync(file, "testo", TestContext.CancellationToken);
|
|
var verdict = await Check().CheckAsync(file, Context(temp), TestContext.CancellationToken);
|
|
Assert.AreEqual(ExportFolderProblemKind.NotAFolder, verdict.Problem!.Kind);
|
|
Assert.AreEqual($"{Path.GetFullPath(file)} è un file: scegli una cartella.", verdict.Problem.Message);
|
|
Assert.AreEqual("testo", await File.ReadAllTextAsync(file, TestContext.CancellationToken));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TheRootOfADiskIsRefused()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
using var temp = new TempFolder();
|
|
var context = Context(temp);
|
|
|
|
// Windows: «C:\», textually; Linux: «/».
|
|
var root = Path.GetPathRoot(temp.Path)!;
|
|
var verdict = await Check().CheckAsync(root, context, ct);
|
|
Assert.AreEqual(ExportFolderProblemKind.RootOfDisk, verdict.Problem!.Kind, verdict.Problem.Message);
|
|
StringAssert.Contains(verdict.Problem.Message, "è la radice di un disco o di una condivisione");
|
|
|
|
// Linux: a mount point that shows the root of its file system (a USB disk mounted there) is a root too; one that
|
|
// shows a folder of its file system is not (Windows decides from the path alone).
|
|
var usb = temp.Combine("usb");
|
|
var nas = temp.Combine("nas");
|
|
Directory.CreateDirectory(usb);
|
|
Directory.CreateDirectory(nas);
|
|
var mounts = new FixedMounts(ExportRootTests.Mount(usb, "/"), ExportRootTests.Mount(nas, "/condivisa/foto"));
|
|
var onMount = await new ExportFolderCheck(mounts).CheckAsync(usb, context, ct);
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
Assert.IsTrue(onMount.Accepted, onMount.Problem?.Message);
|
|
}
|
|
else
|
|
{
|
|
Assert.AreEqual(ExportFolderProblemKind.RootOfDisk, onMount.Problem!.Kind);
|
|
}
|
|
|
|
Assert.IsTrue((await new ExportFolderCheck(mounts).CheckAsync(nas, context, ct)).Accepted);
|
|
Directory.CreateDirectory(Path.Combine(usb, "Febe"));
|
|
Assert.IsTrue((await new ExportFolderCheck(mounts).CheckAsync(Path.Combine(usb, "Febe"), context, ct)).Accepted);
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(ProtectedFolderKind.Data, "la cartella dei dati di Febe")]
|
|
[DataRow(ProtectedFolderKind.Library, "la cartella della libreria")]
|
|
[DataRow(ProtectedFolderKind.Account, "la cartella dell'account @marta.demo")]
|
|
[DataRow(ProtectedFolderKind.Import, "la cartella delle importazioni")]
|
|
public async Task FoldersInsideOrContainingDataLibraryAccountsOrImportAreRefused(ProtectedFolderKind kind, string description)
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
using var temp = new TempFolder();
|
|
var guarded = temp.Combine("dentro", "protetta");
|
|
var inside = Path.Combine(guarded, "sotto");
|
|
var outside = temp.Combine("fuori");
|
|
Directory.CreateDirectory(inside);
|
|
Directory.CreateDirectory(outside);
|
|
var context = new ExportFolderContext([new(Path.GetFullPath(guarded), kind, kind == ProtectedFolderKind.Account ? "marta.demo" : null)], ExportRootOptions.AnyFolder);
|
|
var check = Check();
|
|
|
|
var same = (await check.CheckAsync(guarded, context, ct)).Problem!;
|
|
Assert.AreEqual((ExportFolderProblemKind.SameAsProtected, $"{Path.GetFullPath(guarded)} è {description}: scegli un'altra cartella."), (same.Kind, same.Message));
|
|
var within = (await check.CheckAsync(inside, context, ct)).Problem!;
|
|
Assert.AreEqual((ExportFolderProblemKind.InsideProtected, $"{Path.GetFullPath(inside)} è dentro {description}: scegli una cartella fuori."), (within.Kind, within.Message));
|
|
var containing = (await check.CheckAsync(temp.Combine("dentro"), context, ct)).Problem!;
|
|
Assert.AreEqual(ExportFolderProblemKind.ContainsProtected, containing.Kind);
|
|
Assert.AreEqual($"{Path.GetFullPath(temp.Combine("dentro"))} contiene {description}: scegli una cartella che non la contenga.", containing.Message);
|
|
Assert.IsTrue(same.IsOverlap && within.IsOverlap && containing.IsOverlap);
|
|
|
|
Assert.IsTrue((await check.CheckAsync(outside, context, ct)).Accepted);
|
|
Assert.IsEmpty(Fingerprint.Temporaries(temp.Path));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AnAccountFolderOutsideTheLibraryCountsToo()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
await using var bed = await ExportTestBed.CreateAsync();
|
|
var elsewhere = bed.Temp.Combine("archivio", "luca");
|
|
Directory.CreateDirectory(Path.Combine(elsewhere, "post"));
|
|
await bed.Db.Accounts.UpdateAsync(bed.Luca with { Folder = elsewhere }, ct);
|
|
var import = bed.Temp.Combine("importa");
|
|
Directory.CreateDirectory(import);
|
|
|
|
var context = await ExportFolderCheck.ContextAsync(bed.Db, bed.Library.Layout, bed.Library.Data.Root, import, ExportRootOptions.AnyFolder, null, ct);
|
|
var kinds = context.Protected.Select(p => (p.Kind, p.Username)).ToList();
|
|
CollectionAssert.Contains(kinds, (ProtectedFolderKind.Data, (string?)null));
|
|
CollectionAssert.Contains(kinds, (ProtectedFolderKind.Library, (string?)null));
|
|
CollectionAssert.Contains(kinds, (ProtectedFolderKind.Import, (string?)null));
|
|
Assert.AreEqual(Path.GetFullPath(elsewhere), context.Protected.Single(p => p.Username == "luca.demo").Path);
|
|
|
|
var check = Check();
|
|
var verdict = await check.CheckAsync(Path.Combine(elsewhere, "post"), context, ct);
|
|
Assert.AreEqual(ExportFolderProblemKind.InsideProtected, verdict.Problem!.Kind);
|
|
StringAssert.Contains(verdict.Problem.Message, "la cartella dell'account @luca.demo");
|
|
Assert.AreEqual(ExportFolderProblemKind.ContainsProtected, (await check.CheckAsync(bed.Temp.Combine("archivio"), context, ct)).Problem!.Kind);
|
|
Assert.AreEqual(ExportFolderProblemKind.SameAsProtected, (await check.CheckAsync(import, context, ct)).Problem!.Kind);
|
|
|
|
// A folder about to be saved for an account (WP1.10) is protected before it is stored.
|
|
var future = bed.Temp.Combine("nuova");
|
|
Directory.CreateDirectory(future);
|
|
var extra = await ExportFolderCheck.ContextAsync(bed.Db, bed.Library.Layout, bed.Library.Data.Root, null, ExportRootOptions.AnyFolder, [new(future, ProtectedFolderKind.Account, null)], ct);
|
|
Assert.AreEqual(ExportFolderProblemKind.SameAsProtected, (await check.CheckAsync(future, extra, ct)).Problem!.Kind);
|
|
Assert.IsTrue((await check.CheckAsync(future, context, ct)).Accepted);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AnAliasIsRecognisedByTheProbeFile()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
using var temp = new TempFolder();
|
|
var library = temp.Combine("libreria");
|
|
Directory.CreateDirectory(Path.Combine(library, "marta.demo"));
|
|
|
|
// A second path to the same folder that no text comparison sees: a symbolic link on Linux; on Windows the
|
|
// «\\?\» form of the path (a link needs rights a test does not have there).
|
|
string Alias(string target, string name)
|
|
{
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
return @"\\?\" + Path.GetFullPath(target);
|
|
}
|
|
|
|
var link = temp.Combine(name);
|
|
Directory.CreateSymbolicLink(link, target);
|
|
return link;
|
|
}
|
|
|
|
var check = Check();
|
|
var guarded = new ExportFolderContext([new(Path.GetFullPath(library), ProtectedFolderKind.Library, null)], ExportRootOptions.AnyFolder);
|
|
|
|
// The candidate is the library under another name, or a folder of it.
|
|
var same = Alias(library, "alias-libreria");
|
|
Assert.IsFalse(ExportPaths.IsSameOrInside(same, library) || ExportPaths.IsSameOrInside(library, same), "the text must not tell");
|
|
Assert.AreEqual(ExportFolderProblemKind.SameAsProtected, (await check.CheckAsync(same, guarded, ct)).Problem!.Kind);
|
|
var inside = Alias(Path.Combine(library, "marta.demo"), "alias-account");
|
|
Assert.AreEqual(ExportFolderProblemKind.InsideProtected, (await check.CheckAsync(inside, guarded, ct)).Problem!.Kind);
|
|
|
|
// The other direction: the protected folder is known by its other name, the candidate by the real one.
|
|
var aliased = new ExportFolderContext([new(Alias(library, "alias-protetta"), ProtectedFolderKind.Library, null)], ExportRootOptions.AnyFolder);
|
|
Assert.AreEqual(ExportFolderProblemKind.SameAsProtected, (await check.CheckAsync(library, aliased, ct)).Problem!.Kind);
|
|
Assert.AreEqual(ExportFolderProblemKind.InsideProtected, (await check.CheckAsync(Path.Combine(library, "marta.demo"), aliased, ct)).Problem!.Kind);
|
|
Assert.AreEqual(ExportFolderProblemKind.ContainsProtected, (await check.CheckAsync(temp.Path, aliased, ct)).Problem!.Kind);
|
|
|
|
Assert.IsEmpty(Fingerprint.Temporaries(temp.Path));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TheProbeIsWrittenOnlyInTheCandidate()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
await using var bed = await ExportTestBed.CreateAsync();
|
|
await bed.PostAsync(bed.Marta, "901", 1);
|
|
var import = bed.Temp.Combine("importa");
|
|
Directory.CreateDirectory(import);
|
|
await File.WriteAllTextAsync(Path.Combine(import, "IMG_0001.JPG"), "foto", ct);
|
|
var before = (Data: Fingerprint.Of(bed.Library.Data.Root, DatabaseName(bed)), Library: Fingerprint.Of(bed.Library.Layout.Root), Import: Fingerprint.Of(import));
|
|
Assert.IsNotEmpty(before.Library);
|
|
|
|
var probes = new List<string>();
|
|
var check = new ExportFolderCheck(new FixedMounts(), writeProbe: path =>
|
|
{
|
|
probes.Add(path);
|
|
File.WriteAllText(path, "Febe");
|
|
});
|
|
var context = await ExportFolderCheck.ContextAsync(bed.Db, bed.Library.Layout, bed.Library.Data.Root, import, ExportRootOptions.AnyFolder, null, ct);
|
|
Assert.IsTrue((await check.CheckAsync(bed.Usb, context, ct)).Accepted);
|
|
|
|
var probe = probes.Single();
|
|
Assert.AreEqual(Path.GetFullPath(bed.Usb), Path.GetDirectoryName(probe));
|
|
StringAssert.Matches(Path.GetFileName(probe), new System.Text.RegularExpressions.Regex(@"^\.febe-prova-[0-9a-f]{12}\.part$"));
|
|
Assert.IsFalse(File.Exists(probe));
|
|
Assert.IsEmpty(Directory.GetFileSystemEntries(bed.Usb));
|
|
Fingerprint.AreEqual(before.Data, Fingerprint.Of(bed.Library.Data.Root, DatabaseName(bed)), "Dati");
|
|
Fingerprint.AreEqual(before.Library, Fingerprint.Of(bed.Library.Layout.Root), "Libreria");
|
|
Fingerprint.AreEqual(before.Import, Fingerprint.Of(import), "importa");
|
|
Assert.IsEmpty(Directory.GetFiles(bed.Temp.Path, ExportFolderCheck.ProbePrefix + "*", SearchOption.AllDirectories));
|
|
|
|
// A refused folder is refused by its text: no probe at all.
|
|
Assert.IsFalse((await check.CheckAsync(bed.Library.Layout.Root, context, ct)).Accepted);
|
|
Assert.HasCount(1, probes);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AFolderWhereTheProbeCannotBeWrittenIsRefused()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
using var temp = new TempFolder();
|
|
var usb = temp.Combine("usb");
|
|
Directory.CreateDirectory(usb);
|
|
|
|
var readOnly = await new ExportFolderCheck(new FixedMounts(), writeProbe: _ => throw new IOException("Il supporto è protetto da scrittura."))
|
|
.CheckAsync(usb, Context(temp), ct);
|
|
Assert.AreEqual(ExportFolderProblemKind.ProbeFailed, readOnly.Problem!.Kind);
|
|
Assert.AreEqual(
|
|
$"Febe non riesce a scrivere in {Path.GetFullPath(usb)} (Il supporto è protetto da scrittura.): controlla i permessi o che il disco non sia in sola lettura.",
|
|
readOnly.Problem.Message);
|
|
|
|
var denied = await new ExportFolderCheck(new FixedMounts(), writeProbe: _ => throw new UnauthorizedAccessException("no"))
|
|
.CheckAsync(usb, Context(temp), ct);
|
|
Assert.AreEqual(ExportFolderProblemKind.ProbeFailed, denied.Problem!.Kind);
|
|
StringAssert.Contains(denied.Problem.Message, "(accesso negato)");
|
|
Assert.IsFalse(denied.Problem.IsOverlap);
|
|
}
|
|
|
|
[TestMethod]
|
|
[Timeout(30_000, CooperativeCancellation = true)]
|
|
public async Task ASlowFolderTimesOutWithoutBlocking()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
using var temp = new TempFolder();
|
|
var usb = temp.Combine("usb");
|
|
Directory.CreateDirectory(usb);
|
|
using var release = new ManualResetEventSlim();
|
|
var check = new ExportFolderCheck(new FixedMounts(), TimeSpan.FromMilliseconds(200), _ => release.Wait(TimeSpan.FromSeconds(20)));
|
|
|
|
var started = System.Diagnostics.Stopwatch.StartNew();
|
|
var verdict = await check.CheckAsync(usb, Context(temp), ct);
|
|
started.Stop();
|
|
release.Set();
|
|
Assert.AreEqual(ExportFolderProblemKind.NotResponding, verdict.Problem!.Kind);
|
|
Assert.AreEqual(
|
|
"La cartella non risponde entro 0 secondi: controlla che il disco o la condivisione siano collegati, poi riprova.",
|
|
verdict.Problem.Message);
|
|
Assert.IsLessThan(TimeSpan.FromSeconds(10), started.Elapsed, "the caller did not wait for the disk");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task APathTooLongIsRefused()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
using var temp = new TempFolder();
|
|
var deep = temp.Path;
|
|
while (deep.Length < WindowsFileNames.MaxPathLength + 2)
|
|
{
|
|
deep = Path.Combine(deep, new string('c', 40));
|
|
}
|
|
|
|
Directory.CreateDirectory(deep);
|
|
var verdict = await Check().CheckAsync(deep, Context(temp), ct);
|
|
Assert.AreEqual(ExportFolderProblemKind.PathTooLong, verdict.Problem!.Kind);
|
|
StringAssert.Contains(verdict.Problem.Message, "è troppo lungo: i nomi dei file non ci starebbero nei 240 caratteri");
|
|
Assert.IsEmpty(Directory.GetFileSystemEntries(deep));
|
|
}
|
|
|
|
private static ExportFolderCheck Check() => new(new FixedMounts());
|
|
|
|
private static string DatabaseName(ExportTestBed bed) => Path.GetFileName(bed.Library.Data.Database);
|
|
|
|
/// <summary>A Data folder and a library inside the temporary folder, away from the candidates.</summary>
|
|
private static ExportFolderContext Context(TempFolder temp) => new(
|
|
[new(Path.GetFullPath(temp.Combine("dati")), ProtectedFolderKind.Data, null), new(Path.GetFullPath(temp.Combine("libreria")), ProtectedFolderKind.Library, null)],
|
|
ExportRootOptions.AnyFolder with { Mounts = new FixedMounts() });
|
|
}
|