Documentation ¶
Overview ¶
Package testutils contains functions used as utility code for tests.
Because of that most of these functions do not have return values of type error, but instead call panic() when something goes wrong.
While these functions are stable and tested themselves, they are not intended to be used directly by proxyssh-external code. As such their signatures and edge case behaviour may change without notice.
Index ¶
- func AuthorizedKeysString(key ssh.PublicKey) string
- func GenerateRSATestKeyPair() (ssh.Signer, ssh.PublicKey)
- func GetTestSessionProcess(session *ssh.Session) *os.Process
- func IsProcessAlive(proc *os.Process) (res bool)
- func NewTestListenAddress() string
- func NewTestServerSession(address string, options ssh.ClientConfig) (*ssh.Client, *ssh.Session, error)
- func RunTestServerCommand(address string, options ssh.ClientConfig, command, stdin string) (stdout string, stderr string, code int, err error)
- func SliceContainsString(haystack []string, needle string) bool
- func TCPConstantTestResponse(listener net.Listener, response string)
- func WriteTempFile(pattern, content string) (filepath string, cleanup func())
- type InterceptReader
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AuthorizedKeysString ¶
AuthorizedKeysString turns a public key into a string that can be written to an authorized_keys file. If something goes wrong, calls panic()
func GenerateRSATestKeyPair ¶
GenerateRSATestKeyPair generates a new RSA Keypair for use within testing. A keypair consists of a private key (in the form of a Signer) and a public key.
If something goes wrong during the generation of the keypair, panic() is called.
The bitsize of the keypair is determined internally, but will be the same for subsequent calls of this function.
func GetTestSessionProcess ¶
GetTestSessionProcess returns the process belonging to an ssh session. If the process can not be found panic() is called.
The session is assumed to run on the same host as where this function is called. The session is furthermore assumed to use a bash-compatible shell and that the '$$' variable returns the process id.
This function is itself untested.
func IsProcessAlive ¶
IsProcessAlive checks if the process refered to by the argument is still alive.
This function relies on the fact that the underlying operating system supports sending signals to the process. On Windows and Plan 9 this function may panic, as it can not be used.
This function is itself untested.
func NewTestListenAddress ¶
func NewTestListenAddress() string
NewTestListenAddress returns a new unused address that can be used to start a listener on. The address returned will be off the form 'host:port' and compatible with net.Dial() and friends.
The address is guaranteed to be on a port higher than 1024. Furthermore, it is guaranteed not be used by any other server, and listens only on the loopback interface. It will typically be something like "127.0.0.1:12345", but is not guaranteed to be of this form.
If no address is available, or something unexpected happens, panic() is called.
func NewTestServerSession ¶
func NewTestServerSession(address string, options ssh.ClientConfig) (*ssh.Client, *ssh.Session, error)
NewTestServerSession connects and starts a new ssh session on the sever listening at address.
This function sets reasonable defaults for the options. If options.HostKeyCallback is not set, sets it to a function that accepts every host key. If options.User is the empty string, uses the username "user". This function can thus be called with an empty options struct.
If no error occurs, the function expects the caller the Close() method on the client. If an error occurs during initialization, the client and session will be closed and an error will be returned. A typical invocation of this function should look like:
client, session, err := NewTestServerSession(address, ssh.ClientConfig{}) if err != nil { return err } defer client.Close()
This function is itself untested.
func RunTestServerCommand ¶
func RunTestServerCommand(address string, options ssh.ClientConfig, command, stdin string) (stdout string, stderr string, code int, err error)
RunTestServerCommand runs a command on the ssh server listening at address, and returns its standard output and input. The address and options parameters are passed to NewTestServerSession. The command being run is determined by command. The standard input passed to the command is determined from stdin.
The output of the command (consisting of stdout and stderr), along with it's exit code will be returned. If something goes wrong running the command, an error will be returned.
This function is itself untested.
func SliceContainsString ¶
SliceContainsString returns true if haystack contains needle, and false otherwise.
func TCPConstantTestResponse ¶
TCPConstantTestResponse starts accepting connections on the listener. It then sends a constant response and closes the accepted connection.
This function performs blocking work on the goroutine it was called on. As such, it should typically be called like:
listener, err := net.Listen("tcp", address) go TCPConstantTestResponse(listener, response) defer listener.Close()
func WriteTempFile ¶
WriteTempFile writes content into a temporary file.
Pattern is used to generate the filename of the file, it is passed on to os.CreateTemp. Content is written as a string into the file.
This function returns a pair of (filepath, cleanup) where filepath is the path to the temporary file and cleanup is a function that removes the temporary file.
It is the callers responsibility to call the cleanup function. A typical invocation of this function is something like:
tempfile, cleanup = WriteTempFile(pattern, content) defer cleanup()
If something goes wrong during the creation of the temporary file, panic() is called.
Types ¶
type InterceptReader ¶
InterceptReader wraps an io.Reader. It calls the Intercept function on every received set of bytes.
It is intended for debugging processes and should not be used in production code.
func InterceptAndLog ¶
func InterceptAndLog(reader io.Reader, s string) *InterceptReader
InterceptAndLog returns a new io.Reader that logs every Read() call result. It is intended for debugging processes and should not be used in production code.