test

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailedToCastCard  = errors.New("failed to cast card")
	ErrCardNotResettable = errors.New("card not resettable")
)
View Source
var DangerousWipeRealCard = os.Getenv("TEST_DANGEROUS_WIPE_REAL_CARD") != ""

DangerousWipeRealCard is a global flag which should be used to safe guard any real smart cards from being accidentally wiped. nolint: gochecknoglobals

View Source
var ErrMalformedMockfile = fmt.Errorf("invalid mockfile")

Functions

func PCSCCard

func PCSCCard(card *iso.Card) iso.PCSCCard

PCSCCard unwraps a card which has been passed to the callback of WithCard(). It can be useful to assert that a test is currently running with a real card.

func ResetCard

func ResetCard(card *iso.Card) error

ResetCard is a helper to reset a test card

func WithCard

func WithCard(t *testing.T, flt filter.Filter, cb func(t *testing.T, card *iso.Card))

WithCard opens the first card matching the filter flt and wraps it with the TraceCard and MockCard wrappers to trace commands for debugging as well as record a transcript of commands exchanged during the test. Finally the provided test callback is invoked. The user is expected to implement any test code within the provided callback. Thanks to wrapping the card into a mock object, tests can also be executed without the presence of a real card (e.g. in a CI environment). In this case, previously recorded command transcripts are used to emulate & assert the communication with the card.

Types

type MockCard

type MockCard struct {
	mock.Mock
	// contains filtered or unexported fields
}

MockCard is a wrapper around iso.PCSCCard which reads/writes transcripts of the commands (APDUs) from a file to emulate a real smart card with a mock object.

func NewMockCard

func NewMockCard(t *testing.T, realCard iso.PCSCCard) (c *MockCard, err error)

NewMockCard creates a new smart card mock.

If realCard is nil, we attempt to load a transcript of the expected commands from a transcript file at: `mockdata/t.Name()`.

If realCard is not nil, all exchanged commands will be recorded and written the the transcript file above during Close().

func (*MockCard) BeginTransaction

func (c *MockCard) BeginTransaction() error

func (*MockCard) Close

func (c *MockCard) Close() error

Close invokes WriteTranscript() in case a real-card was passed to NewMockCard().

func (*MockCard) EndTransaction

func (c *MockCard) EndTransaction() error

func (*MockCard) LoadTranscript

func (c *MockCard) LoadTranscript() error

LoadTranscript loads the a command transcript from `mockdata/t.Name()` and configures the mock object with the expected calls to Transmit(), BeginTransaction() and EndTransaction().

func (*MockCard) Transmit

func (c *MockCard) Transmit(cmd []byte) (resp []byte, err error)

func (*MockCard) WriteTranscript

func (c *MockCard) WriteTranscript() error

WriteTranscript writes recorded commands exchanged with a real card to the transcript file at `mockdata/t.Name()`.

type TraceCard

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

TraceCard is a wrapper around iso7816.PCSCCard which logs a exchanged commands (APDUs) to a log/slog logger.

func NewTraceCard

func NewTraceCard(next iso.PCSCCard, logger *slog.Logger) *TraceCard

NewTraceCard wraps a iso7816.PCSCCard into a TraceCard which logs a exchanged commands (APDUs) to a log/slog logger.

func (*TraceCard) BeginTransaction

func (c *TraceCard) BeginTransaction() error

func (*TraceCard) Close

func (c *TraceCard) Close() error

func (*TraceCard) EndTransaction

func (c *TraceCard) EndTransaction() error

func (*TraceCard) Transmit

func (c *TraceCard) Transmit(cmd []byte) ([]byte, error)

Jump to

Keyboard shortcuts

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