From 5dcf5473449e376cdd6f927443278161a2552635 Mon Sep 17 00:00:00 2001 From: Oscar Sanderson Date: Fri, 27 Nov 2026 10:10:56 +0800 Subject: [PATCH 1/3] refactor(sqlstore): name the backchannel look-up and decide operations The two operation strings sqlstore puts in its backchannel errors were repeated as literals; they are now constants, like opCreateBackchannel. The errors are unchanged. Co-Authored-By: Claude Opus 5.5 --- sqlstore/backchannel.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/sqlstore/backchannel.go b/sqlstore/backchannel.go index 069b37ce..1ae85c4e 100644 --- a/sqlstore/backchannel.go +++ b/sqlstore/backchannel.go @@ -76,6 +76,14 @@ var ( // errors. const opCreateBackchannel = "create backchannel authentication" +// opLookUpBackchannel and opDecideBackchannel name +// LookupBackchannelAuthentication and DecideBackchannelAuthentication +// in their errors. +const ( + opLookUpBackchannel = "look up backchannel authentication" + opDecideBackchannel = "decide backchannel authentication" +) + // CreateBackchannelAuthentication implements // storage.BackchannelAuthenticationStore, storing the request pending, // for Sweep to delete BackchannelExpiredRetention after it expires. @@ -131,9 +139,9 @@ func (s *BackchannelAuthenticationStore) LookupBackchannelAuthentication(ctx con handleHash[:]).Scan(&key, &clientID, &stored, &bound.deliveryMode, &bound.pollInterval, &bound.expires) switch { case errors.Is(err, sql.ErrNoRows): - return storage.LookedUpBackchannelAuthentication{}, unknownBackchannelHandle("look up backchannel authentication") + return storage.LookedUpBackchannelAuthentication{}, unknownBackchannelHandle(opLookUpBackchannel) case err != nil: - return storage.LookedUpBackchannelAuthentication{}, unavailable("look up backchannel authentication", err) + return storage.LookedUpBackchannelAuthentication{}, unavailable(opLookUpBackchannel, err) } record, err := s.openRecord(bound.ref(recordColumn, string(key), clientID), stored) if err != nil { @@ -189,7 +197,7 @@ func (s *BackchannelAuthenticationStore) DecideBackchannelAuthentication(ctx con case errors.Is(err, sql.ErrNoRows): return storage.DecidedBackchannelAuthentication{}, s.whyUndecided(ctx, decision.HandleHash) case err != nil: - return storage.DecidedBackchannelAuthentication{}, unavailable("decide backchannel authentication", err) + return storage.DecidedBackchannelAuthentication{}, unavailable(opDecideBackchannel, err) } return out, nil } @@ -207,10 +215,10 @@ func unknownBackchannelHandle(op string) error { func (s *BackchannelAuthenticationStore) whyUndecided(ctx context.Context, handleHash [32]byte) error { var n int if err := s.d.queryRow(ctx, "SELECT COUNT(*) FROM {backchannel_authentications} WHERE handle_hash = ?", handleHash[:]).Scan(&n); err != nil { - return unavailable("decide backchannel authentication", err) + return unavailable(opDecideBackchannel, err) } if n == 0 { - return unknownBackchannelHandle("decide backchannel authentication") + return unknownBackchannelHandle(opDecideBackchannel) } return errBackchannelDecided } From 8512b11535dc9a2ebea1e6b69129c467df956b4d Mon Sep 17 00:00:00 2001 From: Oscar Sanderson Date: Fri, 27 Nov 2026 10:30:58 +0800 Subject: [PATCH 2/3] test(sqlstore): refuse a backchannel look-up and decision on a closed database as an outage Co-Authored-By: Claude Opus 5.5 --- sqlstore/db_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sqlstore/db_test.go b/sqlstore/db_test.go index 9275bb3c..fcc8eec5 100644 --- a/sqlstore/db_test.go +++ b/sqlstore/db_test.go @@ -553,6 +553,14 @@ func TestClosedDatabase(t *testing.T) { if _, err := d.Sweep(ctx, time.Now()); !errors.Is(err, storage.ErrStoreUnavailable) { t.Errorf("Sweep on a closed database = %v, want ErrStoreUnavailable", err) } + handle := sha256.Sum256([]byte("handle-on-a-closed-database")) + if _, err := d.Backchannel().LookupBackchannelAuthentication(ctx, handle); !errors.Is(err, storage.ErrStoreUnavailable) || errors.Is(err, storage.ErrBackchannelAuthenticationNotFound) { + t.Errorf("LookupBackchannelAuthentication on a closed database = %v, want ErrStoreUnavailable, not not-found", err) + } + decision := storage.DecideBackchannelAuthentication{HandleHash: handle, Status: storage.BackchannelAuthenticationDenied} + if _, err := d.Backchannel().DecideBackchannelAuthentication(ctx, decision); !errors.Is(err, storage.ErrStoreUnavailable) || errors.Is(err, storage.ErrBackchannelAuthenticationNotFound) { + t.Errorf("DecideBackchannelAuthentication on a closed database = %v, want ErrStoreUnavailable, not not-found", err) + } } // getOrCreateErr is GetOrCreate's error. From dc7cbf418071c853a83e6106beb460a949ae82b7 Mon Sep 17 00:00:00 2001 From: Oscar Sanderson Date: Fri, 27 Nov 2026 10:33:06 +0800 Subject: [PATCH 3/3] test(sqlstore): report an outage while explaining an undecided CIBA request Co-Authored-By: Claude Opus 5.5 --- sqlstore/backchannel_undecided_outage_test.go | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 sqlstore/backchannel_undecided_outage_test.go diff --git a/sqlstore/backchannel_undecided_outage_test.go b/sqlstore/backchannel_undecided_outage_test.go new file mode 100644 index 00000000..f5c22de7 --- /dev/null +++ b/sqlstore/backchannel_undecided_outage_test.go @@ -0,0 +1,34 @@ +package sqlstore_test + +import ( + "context" + "crypto/sha256" + "errors" + "strings" + "testing" + + "github.com/idfoundry/fapigo/sqlstore" + "github.com/idfoundry/fapigo/storage" +) + +// TestDecideBackchannelReportsAnOutageWhileExplainingARefusal: when a +// decision's update decides nothing, the store asks whether the handle +// is unknown or already decided; if the database can't answer that, +// the decision fails as an outage, not as an unknown handle. +func TestDecideBackchannelReportsAnOutageWhileExplainingARefusal(t *testing.T) { + p := newPlace(t, sqlstore.SQLite) + p.migrated() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + hook := func(query string, after bool) { + if !after && strings.Contains(query, "SELECT COUNT(*)") && strings.Contains(query, "backchannel_authentications") { + cancel() + } + } + b := p.handle(hookedDB(t, p, "", hook)).Backchannel() + decision := storage.DecideBackchannelAuthentication{HandleHash: sha256.Sum256([]byte("handle-never-stored")), Status: storage.BackchannelAuthenticationDenied} + _, err := b.DecideBackchannelAuthentication(ctx, decision) + if !errors.Is(err, storage.ErrStoreUnavailable) || errors.Is(err, storage.ErrBackchannelAuthenticationNotFound) { + t.Fatalf("DecideBackchannelAuthentication with the follow-up query failing = %v, want ErrStoreUnavailable, not not-found", err) + } +}