Files
mcp-ssh/tests/McpSsh.Tests/DefaultSshKeyResolverTests.cs
2026-05-24 20:45:12 +00:00

60 lines
1.8 KiB
C#

using McpSsh.Server.Ssh;
namespace McpSsh.Tests;
public sealed class DefaultSshKeyResolverTests
{
[Fact]
public void ResolveDefaultKeyPath_ReturnsFirstExistingDefaultKey()
{
var fileSystem = new FakeFileSystem("/home/test/.ssh/id_ecdsa", "/home/test/.ssh/id_rsa");
var resolver = new DefaultSshKeyResolver(fileSystem, "/home/test/.ssh");
var path = resolver.ResolveKeyPath(null);
Assert.Equal("/home/test/.ssh/id_ecdsa", path);
}
[Fact]
public void ResolveDefaultKeyPath_ThrowsWhenNoDefaultKeyExists()
{
var resolver = new DefaultSshKeyResolver(new FakeFileSystem(), "/home/test/.ssh");
var ex = Assert.Throws<SshToolException>(() => resolver.ResolveKeyPath(null));
Assert.Equal("ssh_key_not_found", ex.ErrorCode);
}
[Fact]
public void ResolveKeyPath_ReturnsExplicitKeyWhenItExists()
{
var resolver = new DefaultSshKeyResolver(new FakeFileSystem("/keys/deploy_ed25519"), "/home/test/.ssh");
var path = resolver.ResolveKeyPath("/keys/deploy_ed25519");
Assert.Equal("/keys/deploy_ed25519", path);
}
[Fact]
public void ResolveKeyPath_ThrowsWhenExplicitKeyDoesNotExist()
{
var resolver = new DefaultSshKeyResolver(new FakeFileSystem(), "/home/test/.ssh");
var ex = Assert.Throws<SshToolException>(() => resolver.ResolveKeyPath("/keys/missing"));
Assert.Equal("ssh_key_not_found", ex.ErrorCode);
}
private sealed class FakeFileSystem : IFileSystem
{
private readonly HashSet<string> _paths;
public FakeFileSystem(params string[] paths)
{
_paths = paths.ToHashSet(StringComparer.Ordinal);
}
public bool FileExists(string path) => _paths.Contains(path);
}
}