using ClawdDotNet.Tools.FileRW;
using Shouldly;
namespace ClawdDotNet.Tools.Tests.FileRW;
///
/// S6 aus der Bestandsaufnahme: Die Pfadprüfung verglich nur Zeichenketten-Präfixe.
/// Ohne abschließenden Verzeichnistrenner erlaubte ein Root "…\Workspace" damit auch
/// "…\Workspace-Backup\…".
///
/// Das Dateisystem wird hier bewusst NICHT abstrahiert — die Tests sollen die echte
/// Windows-Pfadsemantik prüfen (.., UNC, Alternate Data Streams, abschließende Punkte).
/// Eine Abstraktion würde genau die Fehlerklasse verstecken, um die es geht.
///
public sealed class WorkspacePathTests : IDisposable
{
private readonly string _root;
public WorkspacePathTests()
{
_root = Path.Combine(Path.GetTempPath(), "clawd-tests", Guid.NewGuid().ToString("N"), "Workspace");
Directory.CreateDirectory(_root);
}
public void Dispose()
{
try { Directory.Delete(Path.GetDirectoryName(_root)!, recursive: true); }
catch { /* Aufräumen ist Nebensache */ }
}
private string Resolve(string? relative) => WorkspacePath.Resolve(_root, relative, "personal");
// ═══════════════════════════════════════════════════════════
// Ausbruchsversuche
// ═══════════════════════════════════════════════════════════
[Theory]
[InlineData("../../../Windows/System32/drivers/etc/hosts")]
[InlineData(@"..\..\evil.txt")]
[InlineData("unterordner/../../../ausserhalb.txt")]
[InlineData("./../../evil.txt")]
[InlineData("..")]
public void Relative_Ausbrueche_werden_abgelehnt(string path)
{
Should.Throw(() => Resolve(path));
}
[Theory]
[InlineData(@"C:\Windows\System32\config\SAM")]
[InlineData(@"\\server\share\evil.txt")]
[InlineData("//server/share/evil.txt")]
[InlineData(@"C:\temp\datei.txt")]
public void Absolute_Pfade_und_UNC_Freigaben_werden_abgelehnt(string path)
{
Should.Throw(() => Resolve(path));
}
[Theory]
[InlineData("datei.txt:versteckt")]
[InlineData("datei.txt:$DATA")]
public void Alternate_Data_Streams_werden_abgelehnt(string path)
{
// Ein ADS umgeht sonst die Endungsprüfung: "x.txt:evil.exe".
Should.Throw(() => Resolve(path));
}
///
/// Der eigentliche Kern von S6: ein Nachbarverzeichnis mit gleichem Präfix.
///
[Fact]
public void Ein_Nachbarverzeichnis_mit_gleichem_Praefix_gilt_als_ausserhalb()
{
var backup = _root + "-Backup";
WorkspacePath.IsInside(Path.Combine(backup, "geheim.txt"), _root).ShouldBeFalse();
}
[Fact]
public void Auch_bei_Rootangabe_mit_Trenner_bleibt_das_Nachbarverzeichnis_aussen()
{
var rootWithSeparator = _root + Path.DirectorySeparatorChar;
WorkspacePath.IsInside(_root + "-Backup" + Path.DirectorySeparatorChar + "x.txt", rootWithSeparator)
.ShouldBeFalse();
}
// ═══════════════════════════════════════════════════════════
// Gegenproben — normale Nutzung muss funktionieren
// ═══════════════════════════════════════════════════════════
[Theory]
[InlineData("bericht.md")]
[InlineData("unterordner/bericht.md")]
[InlineData("a/b/c/tief.json")]
[InlineData("./bericht.md")]
[InlineData("unterordner/../bericht.md")]
public void Pfade_innerhalb_des_Workspace_werden_aufgeloest(string path)
{
var resolved = Resolve(path);
WorkspacePath.IsInside(resolved, _root).ShouldBeTrue();
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
[InlineData(".")]
public void Leere_Angaben_ergeben_das_Wurzelverzeichnis(string? path)
{
var resolved = Resolve(path);
resolved.TrimEnd(Path.DirectorySeparatorChar)
.ShouldBe(Path.GetFullPath(_root).TrimEnd(Path.DirectorySeparatorChar));
}
[Fact]
public void Das_Wurzelverzeichnis_selbst_gilt_als_innerhalb()
{
WorkspacePath.IsInside(_root, _root).ShouldBeTrue();
}
[Fact]
public void Ein_Unterverzeichnis_mit_aehnlichem_Namen_bleibt_innerhalb()
{
// Gegenprobe zur Präfix-Regel: Innerhalb des Roots ist alles erlaubt.
var inner = Path.Combine(_root, "Workspace-Backup", "x.txt");
WorkspacePath.IsInside(inner, _root).ShouldBeTrue();
}
}