Documentation
¶
Overview ¶
Package sshdtest 提供一个只用于测试的进程内 SSH 服务器:支持密码 / 键盘交互 / 公钥认证, 对每个 exec 请求用本机的 /bin/sh -c 真正执行,并把 stdin 转给子进程。 它让 SSH 后端的单元测试与真实进程 e2e 不依赖外部的 sshd,却能走完整的协议与真实的 shell 语义。
绝不要在生产代码里引用本包:它会执行客户端发来的任意命令。
Index ¶
- func NewSigner(t testing.TB) ssh.Signer
- type Options
- type Pty
- type Server
- func (s *Server) AuthAttempts() int
- func (s *Server) Close()
- func (s *Server) Commands() []string
- func (s *Server) Connections() int
- func (s *Server) DropConnections()
- func (s *Server) DropNextExec(n int)
- func (s *Server) Env() map[string]string
- func (s *Server) HostKey() ssh.PublicKey
- func (s *Server) Keepalives() int
- func (s *Server) MuteKeepalive(mute bool)
- func (s *Server) Pty() Pty
- func (s *Server) SetHostSigner(sg ssh.Signer)
- func (s *Server) Shells() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
Server 是一个在 127.0.0.1 上监听的测试 SSH 服务器。
func (*Server) AuthAttempts ¶
AuthAttempts 返回累计收到的认证尝试次数(主机密钥不匹配时客户端不应发出任何认证)。
func (*Server) DropNextExec ¶
DropNextExec 让接下来的 n 个 exec 请求在执行前直接断开连接(模拟网络抖动)。
func (*Server) MuteKeepalive ¶
MuteKeepalive 让服务器不再应答 keepalive@openssh.com(模拟 NAT 静默丢包):之后客户端的保活请求会一直等不到回复。
func (*Server) SetHostSigner ¶
SetHostSigner 更换主机密钥:之后的新连接使用新密钥(模拟主机被重装 / 中间人)。
Click to show internal directories.
Click to hide internal directories.