Repository Public
1import Foundation2import Testing3@testable import RemoteCore45@Suite struct SSHConfigParserTests {6 let sample = """7 # cluster8 Host M3U96a9 HostName M3U96a.example.io10 User simon11 Port 2212 Host m4ma m4mb13 HostName %h.example.io14 IdentityFile ~/.ssh/id_ed2551915 Host *.example.io16 ServerAliveInterval 3017 Host gw18 ProxyJump bastion19 HostName=10.0.0.520 Match host gw21 User other22 Host M3U96a23 User duplicate24 """2526 @Test func parsesConcreteHostsOnly() {27 let hosts = SSHConfigParser.parse(sample)28 #expect(hosts.map(\.alias) == ["M3U96a", "m4ma", "m4mb", "gw"])29 #expect(hosts[0].hostName == "M3U96a.example.io")30 #expect(hosts[0].user == "simon")31 #expect(hosts[0].port == 22)32 #expect(hosts[1].identityFile == "~/.ssh/id_ed25519")33 #expect(hosts[2].hostName == "%h.example.io")34 #expect(hosts[3].proxyJump == "bastion")35 #expect(hosts[3].hostName == "10.0.0.5")36 #expect(hosts[3].user == nil, "Match blocks must be ignored")37 }3839 @Test func followsIncludes() {40 let hosts = SSHConfigParser.parse("Include conf.d/*\nHost a\n", includeResolver: { _ in ["Host inc\n HostName inc.example"] })41 #expect(hosts.map(\.alias) == ["inc", "a"])42 }4344 @Test func readsRealUserConfigWithoutCrashing() {45 // Whatever this machine has; must not throw and must not include wildcards.46 let hosts = SSHConfigParser.loadUserConfig()47 #expect(hosts.allSatisfy { !$0.alias.contains("*") })48 }49}5051@Suite struct SSHCommandBuilderTests {52 @Test func neverDisablesHostKeyChecking() {53 let host = SSHHost(alias: "node1")54 let args = SSHCommandBuilder.arguments(for: host)55 #expect(args.contains("StrictHostKeyChecking=ask"))56 #expect(!args.joined(separator: " ").contains("StrictHostKeyChecking=no"))57 #expect(!args.joined(separator: " ").contains("UserKnownHostsFile=/dev/null"))58 #expect(args.last == "node1")59 #expect(args.contains("--"))60 }6162 @Test func tmuxAndDirectoryAreQuoted() {63 let host = SSHHost(alias: "n", tmuxSession: "agent 1", remoteDirectory: "/srv/it's here")64 let args = SSHCommandBuilder.arguments(for: host)65 #expect(args.contains("-t"))66 #expect(args.last == "cd '/srv/it'\\''s here' && exec tmux new-session -A -s 'agent 1'")67 }6869 @Test func controlCharactersAreStripped() {70 #expect(SSHCommandBuilder.shellQuote("a\u{0}b\nc") == "'abc'")71 }7273 @Test func manualHostUsesExplicitEndpoint() {74 let host = SSHHost(alias: "custom", hostName: "10.1.2.3", user: "ubuntu", port: 2222, source: .manual)75 let args = SSHCommandBuilder.arguments(for: host)76 #expect(args.contains("-p")); #expect(args.contains("2222")); #expect(args.contains("ubuntu"))77 #expect(args.last == "10.1.2.3")78 }7980 @Test func aliasStartingWithDashCannotBecomeAnOption() {81 let host = SSHHost(alias: "-oProxyCommand=evil")82 let args = SSHCommandBuilder.arguments(for: host)83 let dash = args.firstIndex(of: "--")!84 #expect(args[dash + 1] == "-oProxyCommand=evil", "alias is placed after -- so ssh treats it as a host name")85 }8687 @Test func tmuxNames() {88 #expect(TmuxCommandBuilder.isValidSessionName("agent-1"))89 #expect(!TmuxCommandBuilder.isValidSessionName("a:b"))90 #expect(!TmuxCommandBuilder.isValidSessionName(""))91 #expect(SSHCommandBuilder.isConnectionFailure(exitCode: 255))92 #expect(!SSHCommandBuilder.isConnectionFailure(exitCode: 0))93 }94}95