telnettest

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package telnettest contains TELNET server for TELNET client testing.

Index

Examples

Constants

View Source
const AuthSuccessWelcomeMessage = `` /* 354-byte string literal not displayed */

AuthSuccessWelcomeMessage contains welcome TELNET Server message.

Variables

This section is empty.

Functions

func AuthHandler

func AuthHandler(c *Context)

AuthHandler checks authorisation data and sets true if received password is valid.

func EmptyHandler

func EmptyHandler(c *Context)

EmptyHandler responses with empty body.

Types

type Context

type Context struct {
	Auth struct {
		Success bool
		Break   bool
	}
	// contains filtered or unexported fields
}

Context represents the context of the current TELNET request.

func (*Context) Conn

func (c *Context) Conn() net.Conn

Conn returns current TELNET Client connection.

func (*Context) Reader

func (c *Context) Reader() *bufio.Reader

Reader returns current TELNET Client connection Reader.

func (*Context) Request

func (c *Context) Request() string

Request returns current request body string.

func (*Context) Server

func (c *Context) Server() *Server

Server returns the Server instance.

func (*Context) Writer

func (c *Context) Writer() *bufio.Writer

Writer returns current TELNET Client connection Writer.

type HandlerFunc

type HandlerFunc func(c *Context)

HandlerFunc defines a function to serve TELNET requests.

type Option

type Option func(s *Server)

Option allows to inject Settings to Server.

func SetAuthHandler

func SetAuthHandler(handler HandlerFunc) Option

SetAuthHandler injects HandlerFunc with authorisation data checking.

func SetCommandHandler

func SetCommandHandler(handler HandlerFunc) Option

SetCommandHandler injects HandlerFunc with commands processing.

func SetSettings

func SetSettings(settings Settings) Option

SetSettings injects configuration for TELNET Server.

type Server

type Server struct {
	Settings Settings
	Listener net.Listener
	// contains filtered or unexported fields
}

Server is an TELNET server listening on a system-chosen port on the local loopback interface, for use in end-to-end TELNET tests.

Example
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/gorcon/telnet"
	"github.com/gorcon/telnet/telnettest"
)

func main() {
	server := telnettest.NewServer(
		telnettest.SetSettings(telnettest.Settings{Password: "password"}),
		telnettest.SetAuthHandler(func(c *telnettest.Context) {
			switch c.Request() {
			case c.Server().Settings.Password:
				c.Writer().WriteString(telnet.ResponseAuthSuccess + telnet.CRLF + telnet.CRLF + telnet.CRLF + telnet.CRLF)
				c.Writer().WriteString(telnettest.AuthSuccessWelcomeMessage + telnet.CRLF + telnet.CRLF)

				c.Auth.Success = true
				c.Auth.Break = true
			default:
				c.Writer().WriteString(telnet.ResponseAuthIncorrectPassword + telnet.CRLF)
			}
		}),
		telnettest.SetCommandHandler(func(c *telnettest.Context) {
			switch c.Request() {
			case "Hello, server":
				c.Writer().WriteString(fmt.Sprintf(time.Now().Format("2006-01-02T15:04:05 00000.000 ")+telnet.ResponseINFLayout, c.Request(), c.Conn().RemoteAddr()) + telnet.CRLF)
				c.Writer().WriteString("Hello, client" + telnet.CRLF)
			default:
				c.Writer().WriteString(fmt.Sprintf("*** ERROR: unknown command '%s'", c.Request()) + telnet.CRLF)
			}

			c.Writer().Flush()
		}),
	)
	defer server.Close()

	client, err := telnet.Dial(server.Addr(), "password", telnet.SetClearResponse(true))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	response, err := client.Execute("Hello, server")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(response)

	response, err = client.Execute("Hi!")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(response)

}
Output:

Hello, client
*** ERROR: unknown command 'Hi!'

func NewServer

func NewServer(options ...Option) *Server

NewServer returns a running TELNET Server or nil if an error occurred. The caller should call Close when finished, to shut it down.

func NewUnstartedServer

func NewUnstartedServer(options ...Option) *Server

NewUnstartedServer returns a new Server but doesn't start it. After changing its configuration, the caller should call Start. The caller should call Close when finished, to shut it down.

func (*Server) Addr

func (s *Server) Addr() string

Addr returns IPv4 string Server address.

func (*Server) Close

func (s *Server) Close()

Close shuts down the Server.

func (*Server) NewContext

func (s *Server) NewContext(conn net.Conn) *Context

NewContext returns a Context instance.

func (*Server) SetAuthHandler

func (s *Server) SetAuthHandler(handler HandlerFunc)

SetAuthHandler injects HandlerFunc with authorisation data checking.

func (*Server) SetCommandHandler

func (s *Server) SetCommandHandler(handler HandlerFunc)

SetCommandHandler injects HandlerFunc with commands processing.

func (*Server) Start

func (s *Server) Start()

Start starts a server from NewUnstartedServer.

type Settings

type Settings struct {
	Password             string
	AuthResponseDelay    time.Duration
	CommandResponseDelay time.Duration
}

Settings contains configuration for TELNET Server.

Jump to

Keyboard shortcuts

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