Files
DodoSSH/tests/DodoSSH.Client.Ssh.Tests/RemotePathTests.cs
T
jaap-jan 03e902a2d2
ci / build and test (push) Failing after 2s
Colour the host's file rows by what their mode says
The remote pane's NAME column was blue for a directory and plain for everything
else, and the PERMS column was faint whatever it said. Two colours now come off
the mode, split across those two columns on purpose: NAME says what a row is,
so a file with an execute bit is green there, and PERMS says what is notable
about how it is set, so a file anyone may write to is amber over the characters
that actually say so. Because the two never compete for one TextBlock, a
world-writable executable shows both facts instead of one winning an argument.
No new blue is spent, which is what App.axaml asks for: it reserves blue for a
directory, a distinct scope, and calls it deliberately rare.

Both are files only, and each exclusion is a wrong answer avoided rather than a
case not got to. Every symbolic link is lrwxrwxrwx by convention and its mode
governs nothing — what may be written is the target, whose mode an lstat
listing never fetched — so amber there would fire on every link on the host. A
world-writable directory is /tmp, made safe by a sticky bit PosixMode does not
render, and warning about it would be warning about the half of the mode that
is on screen while the half that answers the warning is not. And the execute
bit on a directory means "may be searched", which is true of very nearly every
directory a host has, so green there would paint the whole pane and mark
nothing.

The two questions read back the string PosixMode wrote rather than carrying its
nine booleans through SftpEntry as well. That is the point rather than a
shortcut: two representations of one fact is how a row ends up coloured for a
bit the column beside it does not show. A mode of the wrong length answers
false rather than throwing, since these decide a colour and a listing is not
worth failing over one.

The amber is Warn rather than WarnText, which is the muted amber a warning card
writes its sentences in. At 9.5px against TextFaint that one is a shade rather
than a signal, and a marker nobody notices is the same as no marker.

The local pane is untouched, on the grounds it already gives for having no
PERMS column at all: a POSIX mode is not a fact about a file on Windows, and
colouring one there would invent exactly what the column declines to print.

Twenty cases in RemotePathTests, which needs no container — the execute bit in
any of the three triples rather than only the owner's, the others-write bit
alone, a mode of the wrong length, and the file-only rule for both questions
from all three kinds. dotnet format is clean and the app and layout suites pass
at 109 and 35.
2026-07-31 12:17:56 +02:00

167 lines
7.0 KiB
C#

namespace DodoSSH.Client.Ssh.Tests;
/// <summary>
/// Remote paths, permission bits and byte counts — the three things the file browser renders.
/// </summary>
/// <remarks>
/// Not in the SSH collection, so this class needs no container. Every case here is one where the obvious
/// implementation is wrong on Windows, at a filesystem root, or on a number that has just crossed a
/// threshold — which is to say, one that a listing of somebody's home directory would not reveal.
/// </remarks>
public sealed class RemotePathTests
{
[Theory]
[InlineData("/var/log", "syslog", "/var/log/syslog")]
[InlineData("/", "etc", "/etc")]
[InlineData("/home/dodo/", "notes", "/home/dodo/notes")]
public void Combine_JoinsWithExactlyOneSeparator(string directory, string name, string expected)
{
// Deliberately not Path.Combine, which on Windows would yield "/var/log\syslog" — a path the remote
// cannot resolve, failing as "no such file" somewhere the backslash is invisible.
SftpPath.Combine(directory, name).ShouldBe(expected);
}
[Theory]
[InlineData("/var/log/syslog", "/var/log")]
[InlineData("/var/log", "/var")]
[InlineData("/var", "/")]
[InlineData("/", "/")]
[InlineData("/var/log/", "/var")]
public void Parent_StopsAtTheRoot(string path, string expected)
{
// The root is its own parent rather than null, which is what lets the breadcrumb's "up" be a plain
// navigation with nothing above it to special-case.
SftpPath.Parent(path).ShouldBe(expected);
}
[Theory]
[InlineData("/var/log/syslog", "syslog")]
[InlineData("/var/log/", "log")]
[InlineData("/", "/")]
public void Name_IsTheLastSegment(string path, string expected)
{
SftpPath.Name(path).ShouldBe(expected);
}
[Fact]
public void Trail_NamesEverySegmentWithThePathThatReachesIt()
{
SftpPath.Trail("/var/log/nginx").ShouldBe(
[("var", "/var"), ("log", "/var/log"), ("nginx", "/var/log/nginx")]);
}
[Fact]
public void Trail_IsEmptyAtTheRoot()
{
// The root has no segment to name. The breadcrumb draws it as a leading separator, so a trail with a
// phantom empty crumb in it would render as a button with no label.
SftpPath.Trail("/").ShouldBeEmpty();
}
[Fact]
public void PosixMode_RendersTheKindCharacterAndThreeTriples()
{
PosixMode.Format(
SftpEntryKind.Directory,
ownerRead: true, ownerWrite: true, ownerExecute: true,
groupRead: true, groupWrite: false, groupExecute: true,
othersRead: true, othersWrite: false, othersExecute: true)
.ShouldBe("drwxr-xr-x");
PosixMode.Format(
SftpEntryKind.File,
ownerRead: true, ownerWrite: true, ownerExecute: false,
groupRead: true, groupWrite: false, groupExecute: false,
othersRead: false, othersWrite: false, othersExecute: false)
.ShouldBe("-rw-r-----");
PosixMode.Format(
SftpEntryKind.SymbolicLink,
ownerRead: true, ownerWrite: true, ownerExecute: true,
groupRead: true, groupWrite: true, groupExecute: true,
othersRead: true, othersWrite: true, othersExecute: true)
.ShouldBe("lrwxrwxrwx");
}
[Theory]
[InlineData("-rwxr-xr-x", true)]
[InlineData("-rw-r-xr--", true)]
[InlineData("-rw-r--r-x", true)]
[InlineData("-rw-r--r--", false)]
[InlineData("----------", false)]
public void PosixMode_ReadsAnExecuteBitInAnyTriple(string mode, bool expected)
{
// Any of the three, not the owner's: the account browsing is not necessarily the owner, and a file
// that only the group may run is still a file somebody runs.
PosixMode.HasAnyExecuteBit(mode).ShouldBe(expected);
}
[Theory]
[InlineData("-rw-rw-rw-", true)]
[InlineData("-rw-rw-r--", false)]
[InlineData("-rw-r--r--", false)]
[InlineData("--------w-", true)]
public void PosixMode_ReadsTheOthersWriteBit(string mode, bool expected)
{
// The others triple only. A group-writable file is writable by a named set of people, which is what
// groups are for; this asks about the column that names nobody.
PosixMode.IsWorldWritable(mode).ShouldBe(expected);
}
[Theory]
[InlineData("")]
[InlineData("rwxrwxrwx")]
[InlineData("?")]
public void PosixMode_AnswersFalseForAModeItDidNotWrite(string mode)
{
// These two decide a row's colour and nothing else. A listing is not worth failing over a string of
// the wrong length, and an index off the end of one is how that would happen.
PosixMode.HasAnyExecuteBit(mode).ShouldBeFalse();
PosixMode.IsWorldWritable(mode).ShouldBeFalse();
}
[Theory]
[InlineData(SftpEntryKind.File, "-rwxr-xr-x", true)]
[InlineData(SftpEntryKind.File, "-rw-r--r--", false)]
[InlineData(SftpEntryKind.Directory, "drwxr-xr-x", false)]
[InlineData(SftpEntryKind.SymbolicLink, "lrwxrwxrwx", false)]
public void AnEntry_IsExecutableOnlyWhenItIsAFile(SftpEntryKind kind, string mode, bool expected)
{
// The x on a directory means "may be searched", which is true of very nearly every directory a host
// has. A file browser that marked them all would be marking nothing.
Entry(kind, mode).IsExecutable.ShouldBe(expected);
}
[Theory]
[InlineData(SftpEntryKind.File, "-rw-rw-rw-", true)]
[InlineData(SftpEntryKind.File, "-rw-r--r--", false)]
[InlineData(SftpEntryKind.SymbolicLink, "lrwxrwxrwx", false)]
[InlineData(SftpEntryKind.Directory, "drwxrwxrwx", false)]
public void AnEntry_IsWorldWritableOnlyWhenItIsAFile(SftpEntryKind kind, string mode, bool expected)
{
// The two exclusions are the point. Every symbolic link is lrwxrwxrwx and its mode governs nothing —
// what may be written is the target, whose mode this listing never fetched. And a world-writable
// directory is /tmp, made safe by a sticky bit PosixMode does not render: warning there would be
// warning about the half of the mode that is on screen.
Entry(kind, mode).IsWorldWritable.ShouldBe(expected);
}
[Theory]
[InlineData(0, "0 B")]
[InlineData(1023, "1023 B")]
[InlineData(1024, "1.0 KB")]
[InlineData(10 * 1024, "10 KB")]
[InlineData(1536 * 1024, "1.5 MB")]
[InlineData(5L * 1024 * 1024 * 1024, "5.0 GB")]
public void ByteSize_KeepsOneDecimalOnlyWhileItMeansSomething(long bytes, string expected)
{
// The boundary at ten is the whole rule: "9.4 MB" and "512 MB" carry the same information, and
// "512.3 MB" claims a precision the figure does not have by the time it is that large.
ByteSize.Format(bytes).ShouldBe(expected);
}
/// <summary>An entry that is nothing but a kind and a mode, which is all these two questions read.</summary>
private static SftpEntry Entry(SftpEntryKind kind, string mode) =>
new("thing", "/tmp/thing", kind, 0, DateTimeOffset.UnixEpoch, mode);
}