testutils

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2022 License: MIT Imports: 15 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthorizedKeysString

func AuthorizedKeysString(key ssh.PublicKey) string

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

func GenerateRSATestKeyPair() (ssh.Signer, ssh.PublicKey)

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

func GetTestSessionProcess(session *ssh.Session) *os.Process

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

func IsProcessAlive(proc *os.Process) (res bool)

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

func SliceContainsString(haystack []string, needle string) bool

SliceContainsString returns true if haystack contains needle, and false otherwise.

func TCPConstantTestResponse

func TCPConstantTestResponse(listener net.Listener, response string)

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

func WriteTempFile(pattern, content string) (filepath string, cleanup func())

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

type InterceptReader struct {
	Reader    io.Reader
	Intercept func(p []byte, err error)
}

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.

func (*InterceptReader) Read

func (r *InterceptReader) Read(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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