fake

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package fake provides a scriptable in-memory implementation of firmware.Communication for testing BitBox02 client code without a device or simulator.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("bitbox-testkit/bitbox/fake: communication closed")

ErrClosed is returned from Query after Close has been called.

View Source
var ErrInjected = errors.New("bitbox-testkit/bitbox/fake: injected error")

ErrInjected is the canonical error used by InjectError-style scenarios.

View Source
var ErrUnexpectedQuery = errors.New("bitbox-testkit/bitbox/fake: unexpected query, no handler matched")

ErrUnexpectedQuery is returned when no handler matches an incoming query. Tests should treat this as a failure unless an UnhandledHandler is installed.

Functions

This section is empty.

Types

type Fake

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

Fake implements firmware.Communication using a chain of Handlers.

Handlers are tried in registration order; the first matching, non-exhausted handler responds. Calls are recorded for later assertions.

queryMu serializes the entire Query call so handler state mutations in Respond do not need their own locking. stateMu protects only the metadata fields (handlers, calls, closed, onClose), which lets a Respond implementation safely call back into Close or Add without deadlocking.

func New

func New() *Fake

New returns an empty Fake. Add handlers with Expect, ExpectMatch, etc.

func (*Fake) Add

func (f *Fake) Add(h Handler) *Fake

Add appends a Handler to the dispatch chain.

func (*Fake) Always

func (f *Fake) Always(r ResponderFunc) *Fake

Always adds a handler that responds to every request indefinitely. Useful for unconditional sinks ("any further query returns generic OK").

func (*Fake) AlwaysError

func (f *Fake) AlwaysError(err error) *Fake

AlwaysError installs a fallthrough handler returning err for everything that no prior handler matched.

func (*Fake) Calls

func (f *Fake) Calls() [][]byte

Calls returns a copy of every Query payload received so far.

func (*Fake) Close

func (f *Fake) Close()

Close marks the Fake closed. Subsequent Query calls return ErrClosed. Safe to call from inside a Handler's Respond.

func (*Fake) Closed

func (f *Fake) Closed() bool

Closed reports whether Close has been called.

func (*Fake) Expect

func (f *Fake) Expect(resp []byte) *Fake

Expect adds a handler that matches every request and replies with resp once. Use this to script a fixed sequence: Expect, Expect, Expect.

func (*Fake) ExpectError

func (f *Fake) ExpectError(err error) *Fake

ExpectError adds a handler that matches once and returns err.

func (*Fake) ExpectMatch

func (f *Fake) ExpectMatch(m MatcherFunc, r ResponderFunc) *Fake

ExpectMatch adds a single-use handler with explicit matcher + responder.

func (*Fake) OnClose

func (f *Fake) OnClose(fn func()) *Fake

OnClose registers a callback invoked exactly once when Close is first called. Useful for assertions or coordinating goroutines.

func (*Fake) PanicHandler

func (f *Fake) PanicHandler(m MatcherFunc, v any) *Fake

PanicHandler installs a handler that calls panic(v) when matched. Lets tests verify recoverPanic shields gomobile exports from crashes.

func (*Fake) Query

func (f *Fake) Query(req []byte) ([]byte, error)

Query dispatches req to the first matching live handler.

type Handler

type Handler interface {
	Match(req []byte) bool
	Respond(req []byte) ([]byte, error)
	Exhausted() bool
}

Handler reacts to a single Query call. Match returns true to claim the query; Respond produces the answer (or error). A Handler may consume itself after one match by returning true from Exhausted.

type MatcherFunc

type MatcherFunc func(req []byte) bool

MatcherFunc reports whether a request belongs to this handler.

func MatchAny

func MatchAny() MatcherFunc

MatchAny matches every request.

func MatchEqual

func MatchEqual(want []byte) MatcherFunc

MatchEqual matches requests with identical bytes.

func MatchPrefix

func MatchPrefix(prefix []byte) MatcherFunc

MatchPrefix matches requests starting with prefix.

type ResponderFunc

type ResponderFunc func(req []byte) ([]byte, error)

ResponderFunc produces a response (or error) for a matched request.

Jump to

Keyboard shortcuts

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