feat(dispenser): recover K720 card position during uncertain encoding
This commit is contained in:
@@ -112,6 +112,154 @@ func TestResetPacket(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUncertainRecoveryContinuesPreparationBudget(t *testing.T) {
|
||||
responses := append(positionResponses(0x30, 5), positionResponses(0x35, 5)...)
|
||||
responses = append(responses, cmdResp{status: status(0x35)}, cmdResp{err: errors.New("unusable diagnostic AP")})
|
||||
responses = append(responses, positionResponses(0x30, 4)...)
|
||||
responses = append(responses, cmdResp{status: status(0x35)}, cmdResp{status: status(0x37)})
|
||||
c, d := newSequenceTestClient(t, responses...)
|
||||
preparation, err := c.PrepareCurrentCardForEncoding(context.Background())
|
||||
if err != nil || !preparation.UncertainHandoff || preparation.ShakesUsed != 1 {
|
||||
t.Fatalf("preparation=%+v error=%v, want uncertain handoff after one shake", preparation, err)
|
||||
}
|
||||
before := len(d.commands)
|
||||
if err := c.RecoverCurrentCard(context.Background(), preparation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []cmdType{cmdStatus, cmdReset, cmdStatus, cmdToEncoder, cmdStatus, cmdStatus, cmdStatus, cmdStatus, cmdReset, cmdStatus, cmdToEncoder, cmdStatus}
|
||||
if !reflect.DeepEqual(d.commands[before:], want) {
|
||||
t.Errorf("recovery commands=%v, want %v", d.commands[before:], want)
|
||||
}
|
||||
if commandCount(d.commands, cmdReset) != 3 || commandCount(d.commands, cmdToEncoder) != 4 {
|
||||
t.Errorf("whole issuance commands=%v, want three RS and four FC7", d.commands)
|
||||
}
|
||||
for i := before; i < len(d.commands); i++ {
|
||||
if d.commands[i] == cmdToEncoder && d.commandTimes[i].Sub(d.commandTimes[i-2]) != sequenceResetWait {
|
||||
t.Errorf("recovery FC7 at %d did not follow RS, settle and diagnostic AP: %v", i, d.commands)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUncertainRecoveryProductionTrace(t *testing.T) {
|
||||
responses := []cmdResp{{status: status(0x30)}}
|
||||
for i := 0; i < 5; i++ {
|
||||
responses = append(responses, cmdResp{err: errors.New("AP unavailable")})
|
||||
}
|
||||
responses = append(responses, cmdResp{status: status(0x35)}, cmdResp{status: status(0x35)}, cmdResp{status: status(0x37)})
|
||||
c, d := newSequenceTestClient(t, responses...)
|
||||
preparation, err := c.PrepareCurrentCardForEncoding(context.Background())
|
||||
if err != nil || !preparation.UncertainHandoff || preparation.ShakesUsed != 0 {
|
||||
t.Fatalf("production trace preparation=%+v error=%v, want uncertain handoff with unused budget", preparation, err)
|
||||
}
|
||||
if err := c.RecoverCurrentCard(context.Background(), preparation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if commandCount(d.commands, cmdReset) != 1 || commandCount(d.commands, cmdToEncoder) != 2 {
|
||||
t.Errorf("production trace commands=%v, want one shake after initial FC7", d.commands)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUncertainRecoveryStopsAtPhysicalPosition(t *testing.T) {
|
||||
for _, position := range []byte{0x32, 0x33, 0x37, 0x3F, 0x38} {
|
||||
for _, afterReset := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("position=%X afterReset=%t", position, afterReset), func(t *testing.T) {
|
||||
responses := []cmdResp{{status: status(position)}}
|
||||
if afterReset {
|
||||
responses = append([]cmdResp{{status: status(0x35)}}, responses...)
|
||||
}
|
||||
c, d := newSequenceTestClient(t, responses...)
|
||||
err := c.RecoverCurrentCard(context.Background(), PreparationResult{UncertainHandoff: true})
|
||||
var wantErr error
|
||||
if position == 0x38 {
|
||||
wantErr = ErrCardWellEmpty
|
||||
}
|
||||
wantRS := 0
|
||||
if afterReset {
|
||||
wantRS = 1
|
||||
}
|
||||
if !errors.Is(err, wantErr) || commandCount(d.commands, cmdReset) != wantRS || commandCount(d.commands, cmdToEncoder) != 0 {
|
||||
t.Errorf("position %X recovery error=%v commands=%v, want error=%v RS=%d FC7=0", position, err, d.commands, wantErr, wantRS)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUncertainRecoveryExhaustsOnlyRemainingShakes(t *testing.T) {
|
||||
for used := 0; used <= sequenceMaxShakes; used++ {
|
||||
t.Run(fmt.Sprint(used), func(t *testing.T) {
|
||||
c, d := newSequenceTestClient(t, positionResponses(0x35, 30)...)
|
||||
preparation := PreparationResult{UncertainHandoff: true, ShakesUsed: used, lastFC7: c.now().Add(-sequenceShakeAfter)}
|
||||
if err := c.RecoverCurrentCard(context.Background(), preparation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := sequenceMaxShakes - used
|
||||
if commandCount(d.commands, cmdReset) != want || commandCount(d.commands, cmdToEncoder) != want {
|
||||
t.Errorf("used=%d commands=%v, want %d RS/FC7", used, d.commands, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUncertainRecoveryPreservesFC7ObservationDelay(t *testing.T) {
|
||||
c, d := newSequenceTestClient(t, positionResponses(0x35, 4)...)
|
||||
d.statusResponses = append(d.statusResponses, cmdResp{status: status(0x37)})
|
||||
started := c.now()
|
||||
if err := c.RecoverCurrentCard(context.Background(), PreparationResult{UncertainHandoff: true, lastFC7: started}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if commandCount(d.commands, cmdReset) != 1 || d.commandTimes[4].Sub(started) != sequenceShakeAfter {
|
||||
t.Errorf("commands=%v times=%v, want first RS after three seconds", d.commands, d.commandTimes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUncertainRecoveryCommandFailuresAndCancellation(t *testing.T) {
|
||||
for _, command := range []cmdType{cmdReset, cmdToEncoder} {
|
||||
c, d := newSequenceTestClient(t, positionResponses(0x35, 3)...)
|
||||
failure := errors.New("recovery dispatch failed")
|
||||
d.commandErrors[command] = []error{failure}
|
||||
if err := c.RecoverCurrentCard(context.Background(), PreparationResult{UncertainHandoff: true}); !errors.Is(err, failure) {
|
||||
t.Errorf("command=%v error=%v, want %v", command, err, failure)
|
||||
}
|
||||
if commandCount(d.commands, command) != 1 {
|
||||
t.Errorf("failed command=%v commands=%v, want one attempt", command, d.commands)
|
||||
}
|
||||
}
|
||||
c, d := newSequenceTestClient(t, cmdResp{status: status(0x35)})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
c.sequenceTiming.wait = func(ctx context.Context, duration time.Duration) error {
|
||||
if duration != sequenceResetWait {
|
||||
t.Errorf("wait=%s, want reset settle", duration)
|
||||
}
|
||||
cancel()
|
||||
return ctx.Err()
|
||||
}
|
||||
if err := c.RecoverCurrentCard(ctx, PreparationResult{UncertainHandoff: true}); !errors.Is(err, context.Canceled) {
|
||||
t.Errorf("settle cancellation=%v, want context.Canceled", err)
|
||||
}
|
||||
if !reflect.DeepEqual(d.commands, []cmdType{cmdStatus, cmdReset}) {
|
||||
t.Errorf("cancelled commands=%v, want AP/RS only", d.commands)
|
||||
}
|
||||
if err := c.RecoverCurrentCard(ctx, PreparationResult{UncertainHandoff: true}); !errors.Is(err, context.Canceled) {
|
||||
t.Errorf("already cancelled recovery=%v, want context.Canceled", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfirmedPreparationNeverRecovers(t *testing.T) {
|
||||
c, d := newSequenceTestClient(t, cmdResp{status: status(0x37)})
|
||||
preparation, err := c.PrepareCurrentCardForEncoding(context.Background())
|
||||
if err != nil || preparation.UncertainHandoff {
|
||||
t.Fatalf("confirmed preparation=%+v error=%v", preparation, err)
|
||||
}
|
||||
if err := c.RecoverCurrentCard(context.Background(), preparation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(d.commands, []cmdType{cmdStatus}) {
|
||||
t.Errorf("confirmed commands=%v, want initial AP only", d.commands)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeliverCurrentCardSucceedsAfterCommandAcceptanceWithoutStatusPolling(t *testing.T) {
|
||||
staleStatus := []byte{0x30, 0x32, 0x32, 0x33}
|
||||
client, device := newSequenceTestClient(t, cmdResp{status: staleStatus})
|
||||
|
||||
Reference in New Issue
Block a user