sshtest

package
v1.35.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: GPL-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package sshtest generates the SSH key material and credential key that the models and database tests both need. The generators panic instead of taking a *testing.T so package-level vars can call them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeadAddr

func DeadAddr(t testing.TB) string

DeadAddr returns a 127.0.0.1 address that was listening a moment ago and is closed now, so a dial to it is refused rather than left to whatever the host happens to run on a fixed port.

func EncryptionKey

func EncryptionKey() []byte

EncryptionKey returns the deterministic 32-byte key the credential store tests seal with.

func HostKey

func HostKey() []byte

HostKey returns an ed25519 public key in SSH wire format, the form the pin column holds.

func NewRSASigner

func NewRSASigner() ssh.Signer

NewRSASigner returns a fresh RSA host key signer, for the tests that need a host to change key type. 2048 bits keeps key generation off the critical path of the test run.

func NewSigner

func NewSigner() ssh.Signer

NewSigner returns a fresh ed25519 host key signer.

func PrivateKey

func PrivateKey(passphrase string) string

PrivateKey returns a PEM-encoded ed25519 private key, passphrase-protected when passphrase is non-empty. Real key material: the store parses what it is handed, so a placeholder string would only prove the fixtures match.

Types

type ExecMode

type ExecMode int

ExecMode selects what the test server's exec channel does, so a test can stand up the servers the capability probe has to tell apart.

const (
	// ExecGNU answers --version with GNU findutils/coreutils banners, and runs
	// every other command through the local sh.
	ExecGNU ExecMode = iota
	// ExecBSD answers --version without any GNU banner.
	ExecBSD
	// ExecNonPOSIX is a login shell with the GNU tools that quotes like fish:
	// a backslash before the closing quote escapes it, so the quote stays
	// open and the command fails on an unbalanced quote.
	ExecNonPOSIX
	// ExecSplitQuote is a login shell with the GNU tools that quotes like
	// nushell: it fails on the '\” that closes a quote, escapes one and
	// opens another.
	ExecSplitQuote
	// ExecCsh is a login shell with the GNU tools that quotes like tcsh: it
	// fails on a newline in single quotes, which it reads as an unmatched
	// quote whatever its history settings.
	ExecCsh
	// ExecSFTPOnly is a key restricted to sftp: every command is refused with
	// a message and a non-zero exit status, the way a seedbox does it.
	ExecSFTPOnly
	// ExecHang accepts the command and never answers it, the way a wedged or
	// tarpitting host does. Only the client's own deadline ends the session.
	ExecHang
	// ExecStallOpen accepts the first session channel of a connection, the
	// one sftp opens, and never answers the open of any later one.
	ExecStallOpen
	// ExecOneSession runs commands like ExecGNU but refuses a session channel
	// while another is open, the way sshd does with MaxSessions 1.
	ExecOneSession
	// ExecTwoSessions refuses a session channel while two are open, the way
	// sshd does with MaxSessions 2: sftp and one command fill it. The refusal
	// arrives late, as over a real network, so a session can end before it.
	ExecTwoSessions
	// ExecRefuseEveryOtherSession runs commands like ExecGNU but refuses every
	// second session channel, the sftp one never, the way sshd refuses an open
	// that arrives before it counts the close of the session before it.
	ExecRefuseEveryOtherSession
)

type HangingListener added in v1.34.0

type HangingListener struct {
	Addr string
	// Accepted is closed when the listener accepts its first connection: the
	// dial behind it is then in the handshake.
	Accepted <-chan struct{}
}

HangingListener accepts TCP connections and never speaks SSH, so a dial against it blocks in the handshake until the caller's context gives up.

func NewHangingListener

func NewHangingListener(t testing.TB) *HangingListener

NewHangingListener starts a HangingListener on 127.0.0.1 and stops it when the test ends.

type SFTPMode added in v1.31.0

type SFTPMode int

SFTPMode selects what the test server does with an sftp subsystem request.

const (
	// SFTPServe serves the subsystem through pkg/sftp.
	SFTPServe SFTPMode = iota
	// SFTPRefuse refuses the subsystem request.
	SFTPRefuse
	// SFTPStall accepts the subsystem request and never speaks sftp.
	SFTPStall
	// SFTPDropOnNextRequest serves sftp but closes the connection when the
	// next request arrives, so that request is cut off in flight.
	SFTPDropOnNextRequest
	// SFTPCloseChannelOnNextRequest closes only the sftp channel when the
	// next request arrives, the way sshd does when its sftp-server exits,
	// and leaves the connection up.
	SFTPCloseChannelOnNextRequest
	// SFTPStallReadDir serves an empty in-memory tree whose directory
	// listings hang, the way sftp-server does on a hung network mount. Every
	// other request is answered. The first listing to arrive starts a timer
	// that ends the stall on its own, so a client that ignores its deadline
	// fails the test instead of hanging it. ReleaseStall ends it sooner.
	SFTPStallReadDir
	// SFTPStatEOF serves an empty in-memory tree and answers every stat and
	// lstat with SSH_FX_EOF, which pkg/sftp hands its caller as io.EOF.
	SFTPStatEOF
	// SFTPNoHardlink serves sftp from a server that does not advertise
	// hardlink@openssh.com, the way a non-OpenSSH server does. pkg/sftp's
	// extension list is process-global, so the version reply is edited on
	// its way out instead.
	SFTPNoHardlink
	// SFTPHoldNextMutation serves sftp but holds the next mkdir, remove, rmdir
	// or hardlink request before the server sees it, until ReleaseStall or
	// the stall timer. Requests after that one pass. Stalled is closed at
	// the hold, so a test can cancel its caller while the request is held.
	SFTPHoldNextMutation
	// SFTPRefuseRemove serves an empty in-memory tree that refuses every
	// remove with "permission denied" and every rmdir with a bare failure,
	// the way two different errnos reach a client. pkg/sftp's Client.Remove
	// then names the error from a link-following Stat.
	SFTPRefuseRemove
	// SFTPStatVFS serves deterministic filesystem statistics without host statvfs support.
	SFTPStatVFS
	// SFTPRefuseLinkAndLstat injects permission errors without host directory modes.
	SFTPRefuseLinkAndLstat
)

type Server

type Server struct {
	Addr    string
	HostKey ssh.PublicKey
	// contains filtered or unexported fields
}

Server is an in-process SSH server listening on loopback. It serves the sftp subsystem through pkg/sftp and answers exec requests per its ExecMode, and it counts handshakes and session channels so a test can assert that a refused host key opened nothing.

func NewServer

func NewServer(t testing.TB, hostKey ssh.Signer, exec ExecMode) *Server

NewServer starts a server on 127.0.0.1 with hostKey and stops it when the test ends.

func (*Server) Accepts

func (s *Server) Accepts() int

Accepts returns the number of SSH handshakes that passed authentication.

func (*Server) Auths

func (s *Server) Auths() int

Auths returns the number of public-key authentication attempts, which is how a test proves a rejected host key stopped the client before it authenticated.

func (*Server) Channels

func (s *Server) Channels() int

Channels returns the number of session channels the client opened.

func (*Server) Commands added in v1.33.0

func (s *Server) Commands() [][]string

Commands returns the words of each command the server ran through the shell, in order.

func (*Server) Dials added in v1.31.0

func (s *Server) Dials() int

Dials returns the number of TCP connections the server accepted, whether or not a handshake followed: a refused host key still counts here, which is how a test proves a refusal was answered from the memo rather than redialled.

func (*Server) DropConnections added in v1.31.0

func (s *Server) DropConnections()

DropConnections closes every live connection, the way a host that reboots or an idle timeout that fires does, so a test can assert the client redials.

func (*Server) MaxInFlight added in v1.34.0

func (s *Server) MaxInFlight() int

MaxInFlight returns the most sftp requests the server had read and not yet answered at one time, so a test can show how many requests the client overlaps. Without SetLatency the server answers at once, and the count says little.

func (*Server) ReleaseStall added in v1.31.0

func (s *Server) ReleaseStall()

ReleaseStall ends an SFTPStallReadDir or SFTPHoldNextMutation stall. Safe to call more than once.

func (*Server) SessionOpened added in v1.34.0

func (s *Server) SessionOpened() <-chan struct{}

SessionOpened returns a channel that is closed when the server next accepts a session channel, so a test knows that session holds its place in the host's limit.

func (*Server) SetExec added in v1.33.0

func (s *Server) SetExec(mode ExecMode)

SetExec changes what the exec channel does for commands from now on, the way an edit of authorized_keys changes what a key may run.

func (*Server) SetLatency added in v1.31.0

func (s *Server) SetLatency(d time.Duration)

SetLatency delays each sftp answer by d, the way a distant host does, on sftp sessions opened from now on. It applies to the modes that serve the host file system, not to the in-memory ones. Delivery is deferred, not serialised: requests the client pipelines still overlap, and MaxInFlight counts them.

func (*Server) SetSFTP added in v1.31.0

func (s *Server) SetSFTP(mode SFTPMode)

SetSFTP sets what later sftp subsystem requests get.

func (*Server) Stalled added in v1.34.0

func (s *Server) Stalled() <-chan struct{}

Stalled is closed when the first request reaches a stall: a listing in SFTPStallReadDir, the mutation SFTPHoldNextMutation holds, or a command in ExecHang. A test cancels on it, so its cancel lands inside the stall.

Jump to

Keyboard shortcuts

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