sshdtest

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package sshdtest 提供一个只用于测试的进程内 SSH 服务器:支持密码 / 键盘交互 / 公钥认证, 对每个 exec 请求用本机的 /bin/sh -c 真正执行,并把 stdin 转给子进程。 它让 SSH 后端的单元测试与真实进程 e2e 不依赖外部的 sshd,却能走完整的协议与真实的 shell 语义。

绝不要在生产代码里引用本包:它会执行客户端发来的任意命令。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NeedShell added in v2.1.0

func NeedShell(t testing.TB)

NeedShell 在没有 /bin/sh 的环境(Windows 原生)里跳过调用它的测试。

本服务器对每个 exec 请求用 /bin/sh -c 真正执行命令;没有 POSIX shell 时,命令根本起不来(退出码 127), 失败的原因在测试服务器而不在被测代码。需要真实执行命令的用例开头调用它;不执行命令的用例(握手、认证、主机密钥)不必调用。

func NewSigner

func NewSigner(t testing.TB) ssh.Signer

NewSigner 生成一把新的 ed25519 主机 / 用户密钥。

Types

type Options

type Options struct {
	User           string
	Password       string          // 非空则允许密码与键盘交互认证
	AuthorizedKeys []ssh.PublicKey // 非空则允许公钥认证
	Home           string          // 子进程的 HOME;为空则使用测试临时目录
	// KbdNoPrompt 模拟没有启用 PAM 的 OpenSSH:声明支持 keyboard-interactive,
	// 却不发出任何提问就直接拒绝(客户端因此收不到 INFO_REQUEST)。
	KbdNoPrompt   bool
	HostSigner    ssh.Signer   // 为空则生成新的 ed25519 主机密钥
	ExtraHostKeys []ssh.Signer // 额外的主机密钥(用于验证算法协商)
	// Handler 可以接管某条命令(例如模拟非 POSIX 主机);返回 handled=false 则照常用 sh 执行。
	// 已经申请了伪终端的会话里的 exec(交互式启动 shell)不会经过 Handler:它要读输入,不能等到 EOF。
	Handler func(cmdline string, stdin []byte) (stdout, stderr []byte, code int, handled bool)
	// RejectEnv 让服务器对所有 env 请求回复失败(模拟没有配置 AcceptEnv 的 sshd)。
	RejectEnv bool
	// RejectPtyExec 让服务器对「已申请伪终端的会话」里的 exec 请求回复失败(模拟只允许 shell 请求的受限环境)。
	RejectPtyExec bool
}

Options 配置测试服务器。

type Pty

type Pty struct {
	Term       string
	Rows, Cols int
	// Modes 是 pty-req 里编码的终端模式(RFC 4254 §8:操作码 → 取值),例如 ssh.IUTF8。
	Modes map[uint8]uint32
}

Pty 是服务器最近一次收到的伪终端请求 / 窗口调整。

type Server

type Server struct {
	Addr string
	Home string
	// contains filtered or unexported fields
}

Server 是一个在 127.0.0.1 上监听的测试 SSH 服务器。

func Start

func Start(t testing.TB, o Options) *Server

Start 启动服务器,测试结束时自动关闭。

func (*Server) AuthAttempts

func (s *Server) AuthAttempts() int

AuthAttempts 返回累计收到的认证尝试次数(主机密钥不匹配时客户端不应发出任何认证)。

func (*Server) Close

func (s *Server) Close()

Close 关闭监听与所有连接。

func (*Server) Commands

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

Commands 返回收到的所有 exec 命令行(不含 stdin)。

func (*Server) Connections

func (s *Server) Connections() int

Connections 返回累计接受的连接数。

func (*Server) DropConnections

func (s *Server) DropConnections()

DropConnections 关闭所有已建立的连接。

func (*Server) DropNextExec

func (s *Server) DropNextExec(n int)

DropNextExec 让接下来的 n 个 exec 请求在执行前直接断开连接(模拟网络抖动)。

func (*Server) Env

func (s *Server) Env() map[string]string

Env 返回通过 env 请求收到并被接受的环境变量(副本)。

func (*Server) HostKey

func (s *Server) HostKey() ssh.PublicKey

HostKey 返回当前对外使用的主机公钥。

func (*Server) Keepalives

func (s *Server) Keepalives() int

Keepalives 返回累计收到的保活请求数。

func (*Server) MuteKeepalive

func (s *Server) MuteKeepalive(mute bool)

MuteKeepalive 让服务器不再应答 keepalive@openssh.com(模拟 NAT 静默丢包):之后客户端的保活请求会一直等不到回复。

func (*Server) Pty

func (s *Server) Pty() Pty

Pty 返回最近一次收到的伪终端请求(含窗口调整后的大小)。

func (*Server) SetHostSigner

func (s *Server) SetHostSigner(sg ssh.Signer)

SetHostSigner 更换主机密钥:之后的新连接使用新密钥(模拟主机被重装 / 中间人)。

func (*Server) Shells

func (s *Server) Shells() int

Shells 返回累计启动的交互式 shell 数。

Jump to

Keyboard shortcuts

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