Files
app/ios/DModemSSH/Tests/DModemSSHTests/SSHShellTests.swift
T
Claude f1eb22c591 SSH, Telnet and rlogin: rlogin on Android, all three on iOS
- shared: TelnetCodec and RloginCodec (sans-I/O), and LoginBridge for Swift
- Android: rlogin directory entries (RloginLink on the shared codec)
- iOS: a Remote screen from the Terminal tab; SSH on SwiftNIO SSH
  (ios/DModemSSH, tested against a real sshd), Telnet and rlogin on
  Network with the shared codecs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRSV65geuGd6zMt2V6r
2026-11-12 12:41:55 +00:00

95 lines
3.5 KiB
Swift

// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Neo-Desktop
import DModemSSH
import Foundation
import XCTest
/// A login to a real sshd, when DMODEM_SSH_TEST names one as
/// host:port:user:password (else skipped).
final class SSHShellTests: XCTestCase {
private func server() throws -> (String, Int, String, String) {
guard let spec = ProcessInfo.processInfo.environment["DMODEM_SSH_TEST"] else {
throw XCTSkip("DMODEM_SSH_TEST is not set")
}
let p = spec.split(separator: ":", maxSplits: 3).map(String.init)
return (p[0], Int(p[1]) ?? 22, p[2], p[3])
}
func testShell() throws {
let (host, port, user, password) = try server()
let shell = SSHShell(host: host, port: port, user: user, term: "vt100", cols: 100, rows: 30)
let lock = NSLock()
var out = Data()
var hostKey = ""
var asks: [String] = []
let closed = expectation(description: "closed")
var why: String?
shell.verifyHostKey = { k, ok in
lock.withLock { hostKey = k }
ok(true)
}
shell.askPassword = { reason, done in
lock.withLock { asks.append(reason) }
// (the first wrong, to see it asked again)
done(asks.count == 1 ? "wrong" : password)
}
shell.onData = { d in
lock.withLock { out.append(d) }
}
shell.onClose = { w in
why = w
closed.fulfill()
}
shell.connect()
// the shell's prompt, then the commands
let deadline = Date().addingTimeInterval(20)
while Date() < deadline && lock.withLock({ out.isEmpty }) { Thread.sleep(forTimeInterval: 0.1) }
shell.resize(cols: 120, rows: 40)
Thread.sleep(forTimeInterval: 0.5)
shell.write(Data("echo TERM=$TERM; stty size; exit 3\r".utf8))
wait(for: [closed], timeout: 20)
let text = String(decoding: lock.withLock { out }, as: UTF8.self)
XCTAssertTrue(hostKey.hasPrefix("ssh-ed25519 ") || hostKey.hasPrefix("ecdsa-"), hostKey)
XCTAssertEqual(asks, ["", "The password was not accepted"])
XCTAssertTrue(text.contains("TERM=vt100"), text)
XCTAssertTrue(text.contains("40 120"), text)
XCTAssertEqual(why, "the shell exited with status 3")
}
func testRefusedHostKey() throws {
let (host, port, user, _) = try server()
let shell = SSHShell(host: host, port: port, user: user)
let closed = expectation(description: "closed")
var why: String?
shell.verifyHostKey = { _, ok in ok(false) }
shell.onClose = { w in
why = w
closed.fulfill()
}
shell.connect()
wait(for: [closed], timeout: 20)
XCTAssertEqual(why, "the host's key was not accepted")
}
func testCloseByUser() throws {
let (host, port, user, password) = try server()
let shell = SSHShell(host: host, port: port, user: user)
let closed = expectation(description: "closed")
let opened = expectation(description: "data")
opened.assertForOverFulfill = false
var why: String? = "unset"
shell.verifyHostKey = { _, ok in ok(true) }
shell.askPassword = { _, done in done(password) }
shell.onData = { _ in opened.fulfill() }
shell.onClose = { w in
why = w
closed.fulfill()
}
shell.connect()
wait(for: [opened], timeout: 20)
shell.close()
wait(for: [closed], timeout: 20)
XCTAssertNil(why)
}
}