tgtest

package
v0.90.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: MIT Imports: 31 Imported by: 8

Documentation

Overview

Package tgtest provides test Telegram server for basic end-to-end tests.

Index

Constants

View Source
const (
	// MessageServerResponse is a message type of RPC calls result.
	MessageServerResponse = proto.MessageServerResponse
	// MessageFromServer is a message type of server-side updates.
	MessageFromServer = proto.MessageFromServer
)

Variables

This section is empty.

Functions

func NewPrivateKey added in v0.66.0

func NewPrivateKey(k *rsa.PrivateKey) exchange.PrivateKey

NewPrivateKey creates new private key from RSA private key.

Types

type Dispatcher

type Dispatcher struct {
	// contains filtered or unexported fields
}

Dispatcher is a plain handler to map requests by ID.

func NewDispatcher

func NewDispatcher() *Dispatcher

NewDispatcher creates new Dispatcher.

func (*Dispatcher) Fallback

func (d *Dispatcher) Fallback(h Handler) *Dispatcher

Fallback sets fallback handler.

func (*Dispatcher) Handle

func (d *Dispatcher) Handle(id uint32, h Handler) *Dispatcher

Handle sets handler for given TypeID.

func (*Dispatcher) HandleFunc

func (d *Dispatcher) HandleFunc(id uint32, h func(server *Server, req *Request) error) *Dispatcher

HandleFunc sets handler for given TypeID.

func (*Dispatcher) OnMessage

func (d *Dispatcher) OnMessage(server *Server, req *Request) error

OnMessage implements Handler

func (*Dispatcher) Result

func (d *Dispatcher) Result(id uint32, msg bin.Encoder) *Dispatcher

Result sets constant result for given TypeID. NB: it uses rpc_result to pack given encoder.

func (*Dispatcher) Vector

func (d *Dispatcher) Vector(id uint32, msgs ...bin.Encoder) *Dispatcher

Vector sets constant Vector result for given TypeID. NB: it uses rpc_result to pack generic vector with given encoders.

type Handler

type Handler interface {
	OnMessage(server *Server, req *Request) error
}

Handler is a RPC request handler.

func TestTransport

func TestTransport(t testing.TB, logger *zap.Logger, message string) Handler

TestTransport is a handler for testing MTProto transport.

func UnpackInvoke added in v0.50.0

func UnpackInvoke(next Handler) Handler

UnpackInvoke is a simple Handler middleware to unpack some Invoke*-like requests. Including:

tg.InvokeWithLayerRequest
tg.InitConnectionRequest
tg.InvokeWithoutUpdatesRequest

type HandlerFunc

type HandlerFunc func(server *Server, req *Request) error

HandlerFunc is functional adapter for Handler.OnMessage method.

func (HandlerFunc) OnMessage

func (h HandlerFunc) OnMessage(server *Server, req *Request) error

OnMessage implements Handler.

type Request

type Request struct {
	// DC ID from server structure.
	// Used to make handler less stateful.
	DC int
	// Session is a user session.
	Session Session
	// MsgID is a message ID of RPC request.
	MsgID int64
	// Buf contains RPC request
	Buf *bin.Buffer
	// RequestCtx is a request context.
	RequestCtx context.Context
}

Request represents MTProto RPC request structure.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a MTProto server structure.

func NewServer added in v0.50.0

func NewServer(key exchange.PrivateKey, handler Handler, opts ServerOptions) *Server

NewServer creates new Server.

func (*Server) ForceDisconnect

func (s *Server) ForceDisconnect(k Session)

ForceDisconnect forcibly disconnect user from server. It deletes MTProto session (session_id), but not auth key.

func (*Server) Key

func (s *Server) Key() exchange.PublicKey

Key returns public key of this server.

func (*Server) Send

func (s *Server) Send(ctx context.Context, k Session, t proto.MessageType, message bin.Encoder) error

Send sends given message to user session k. Parameter t denotes MTProto message type. It should be MessageServerResponse or MessageFromServer.

func (*Server) SendAck

func (s *Server) SendAck(ctx context.Context, k Session, ids ...int64) error

SendAck sends acknowledgment for received message.

func (*Server) SendBool

func (s *Server) SendBool(req *Request, r bool) error

SendBool sends RPC answer using given bool as result. Usually used in methods without explicit response.

func (*Server) SendErr

func (s *Server) SendErr(req *Request, e *tgerr.Error) error

SendErr sends RPC answer using given error as result.

func (*Server) SendEternalSalt

func (s *Server) SendEternalSalt(req *Request) error

SendEternalSalt sends response for mt.GetFutureSaltsRequest. It sends an `eternal` salt, which valid until maximum possible date.

func (*Server) SendFutureSalts

func (s *Server) SendFutureSalts(req *Request, salts ...mt.FutureSalt) error

SendFutureSalts sends response for mt.GetFutureSaltsRequest.

func (*Server) SendGZIP added in v0.50.0

func (s *Server) SendGZIP(req *Request, msg bin.Encoder) error

SendGZIP sends RPC answer and packs it into proto.GZIP.

func (*Server) SendPong

func (s *Server) SendPong(req *Request, pingID int64) error

SendPong sends response for mt.PingRequest request.

func (*Server) SendResult

func (s *Server) SendResult(req *Request, msg bin.Encoder) error

SendResult sends RPC answer using msg as result.

func (*Server) SendUpdates

func (s *Server) SendUpdates(ctx context.Context, k Session, updates ...tg.UpdateClass) error

SendUpdates sends given updates to user session k.

func (*Server) SendVector

func (s *Server) SendVector(req *Request, msgs ...bin.Encoder) error

SendVector sends RPC answer using given vector as result.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, l transport.Listener) error

Serve runs server loop using given listener.

type ServerOptions added in v0.50.0

type ServerOptions struct {
	// DC ID of this server. Default to 2.
	DC int
	// Random is random source. Defaults to rand.Reader.
	Random io.Reader
	// Logger is instance of zap.Logger. No logs by default.
	Logger *zap.Logger
	// Codec constructor.
	// Defaults to nil (underlying transport server detects protocol automatically).
	Codec func() transport.Codec
	// Clock to use. Defaults to clock.System.
	Clock clock.Clock
	// MessageID generator. Creates a new proto.MessageIDGen by default.
	// Clock will be used for creation.
	MessageID mtproto.MessageIDSource
	// Types map, used in verbose logging of incoming message.
	Types *tmap.Map
	// ReadTimeout is a connection read timeout.
	ReadTimeout time.Duration
	// ReadTimeout is a connection write timeout.
	WriteTimeout time.Duration
}

ServerOptions of Server.

type Session

type Session struct {
	// ID is a Session ID.
	ID int64
	// AuthKey is an attached key.
	AuthKey crypto.AuthKey
}

Session represents connection session.

func (Session) MarshalLogObject added in v0.50.0

func (s Session) MarshalLogObject(encoder zapcore.ObjectEncoder) error

MarshalLogObject implements zap.ObjectMarshaler.

Directories

Path Synopsis
Package cluster contains Telegram multi-DC setup utilities.
Package cluster contains Telegram multi-DC setup utilities.
Package services contains some Telegram services implemented for testing.
Package services contains some Telegram services implemented for testing.
config
Package config contains config service implementation for tgtest server.
Package config contains config service implementation for tgtest server.
file
Package file contains file service implementation for tgtest server.
Package file contains file service implementation for tgtest server.

Jump to

Keyboard shortcuts

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