Documentation
¶
Overview ¶
Package telnettest contains TELNET server for TELNET client testing.
Index ¶
Examples ¶
Constants ¶
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.
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.
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 ¶
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 ¶
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 ¶
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) NewContext ¶
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.