sshtest

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package sshtest is an SSH server for tests, in the test's process on 127.0.0.1: it logs in its user by password or by an authorized key, runs commands in /bin/sh (each in a process group of its own, as sshd's sessions run; in a session of its own with a PTY on Linux after a pty-req, a shell too), answers signals and keepalives, serves SFTP from a folder, and forwards: direct-tcpip, direct-streamlocal@openssh.com and tcpip-forward (forwarding.go). NewWindows makes one that answers as Windows's OpenSSH does.

Index

Constants

View Source
const PasswordID = "pw-1"

PasswordID is the id of the server's password's credential.

View Source
const WindowsVersion = "SSH-2.0-OpenSSH_for_Windows_9.5"

WindowsVersion is the version Windows's OpenSSH says.

Variables

This section is empty.

Functions

func FakeBecome added in v0.5.0

func FakeBecome(t testing.TB, sudoPassword, suPassword string)

FakeBecome puts fake sudo, su and script first on the test's PATH, which the server's commands run with: sudo asks for sudoPassword (none with FAKE_NOPASSWD set), su for suPassword, both then running the command as the test's user.

func Silent

func Silent(t testing.TB) uint64

Silent is a TCP listener that takes connections and says nothing: a server that never answers. It returns its port.

func Speaks

func Speaks(t testing.TB, said string) uint64

Speaks is a TCP listener that answers every connection with said and closes it: something that is not an SSH server. It returns its port.

Types

type Exec added in v0.2.0

type Exec func(ctx context.Context, command string, out, errOut io.Writer) int

Exec runs a command as a test's Windows server runs it: its output to out and errOut, its exit status returned. Its ctx ends on a signal, on which Windows's OpenSSH ends all the command started, and when the test ends; a channel closing alone leaves it running, as on Windows.

type Server

type Server struct {
	// User and Password log in; so does a key AuthorizeKey adds.
	User, Password string
	// Dir is where SFTP starts and commands run.
	Dir     string
	HostKey ssh.Signer
	// Stall leaves the channels opened from now on unanswered, neither
	// accepted nor refused: a server gone quiet under an open.
	Stall atomic.Bool

	// Signals counts the signals commands got.
	Signals atomic.Int32
	// NoForward refuses every forward, as AllowTcpForwarding no does;
	// NoPTY every pty-req (the session runs on pipes); NoUnix every unix
	// socket's (direct-streamlocal: a server without it says an unknown
	// channel type).
	NoForward, NoPTY, NoUnix atomic.Bool
	// Size is the last size a session's terminal was given (pty-req,
	// window-change), rows and cols.
	Size atomic.Value
	// contains filtered or unexported fields
}

Server is a running SSH server.

func New

func New(t testing.TB, dir string) *Server

New starts a server for t, its SFTP and commands in dir; it stops when the test ends.

func NewWindows added in v0.2.0

func NewWindows(t testing.TB, dir string, exec Exec) *Server

NewWindows starts a server that answers as Windows's OpenSSH does: its version says so, its SFTP's root lists one drive, C:, which is dir, and SFTP starts in C:\Users\<user> (made in dir); a link to a folder reads as the folder it points to, as a junction does over Windows's SFTP, and a read-only file (no owner's write bit) is not removed. Its commands are exec's: it runs no shell.

func (*Server) AuthorizeKey

func (s *Server) AuthorizeKey(key ssh.PublicKey)

AuthorizeKey lets key log in as the user.

func (*Server) Credentials added in v0.4.0

func (s *Server) Credentials() map[string]protocol.Credential

Credentials are what core gives beside Values: the server's user and password, a PLAINTEXT credential.

func (*Server) Fingerprint

func (s *Server) Fingerprint() string

Fingerprint is the server's host key's, as ssh-keygen -l prints it.

func (*Server) Host

func (s *Server) Host() string

Host and Port are where the server listens.

func (*Server) PasswordCredential added in v0.4.0

func (s *Server) PasswordCredential(id, password string) protocol.Credential

PasswordCredential is a PLAINTEXT credential of the server's user, with password.

func (*Server) Port

func (s *Server) Port() int

func (*Server) Values

func (s *Server) Values() map[string]any

Values are a target's values for the server, logging in by its password: the credential Credentials gives under PasswordID.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址