| 1 | package handoff |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/rastrillo/rastrillo/assertion" |
| 5 | "context" |
| 6 | "database/sql" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestReceivePendingRefusesUnknownNonce(t *testing.T) { |
| 11 | f := fresh(t) |
| 12 | _, nonce := f.pending(t) |
| 13 | f.db.Exec("DELETE FROM oficina_handoff_pending") |
| 14 | if _, err := f.s.ReceivePending(context.Background(), f.token(t, nonce), f.now); err != ErrRefused { |
| 15 | t.Fatalf("unknown nonce: %v", err) |
| 16 | } |
| 17 | if count(t, f.db, "oficina_handoff_inbox") != 0 { |
| 18 | t.Fatal("retained unsolicited receipt") |
| 19 | } |
| 20 | } |
| 21 | func TestFinishAtomicRollsBackAppAndProof(t *testing.T) { |
| 22 | f := fresh(t) |
| 23 | secret, nonce := f.pending(t) |
| 24 | handle, _ := f.received(t, nonce) |
| 25 | f.db.Exec("CREATE TABLE minted (subject TEXT)") |
| 26 | _, _, err := f.s.FinishAtomic(context.Background(), secret, handle, "", f.now, func(tx *sql.Tx, c assertion.Claims, _ string) error { |
| 27 | if _, e := tx.Exec("INSERT INTO minted VALUES(?)", c.Subject); e != nil { |
| 28 | t.Fatal(e) |
| 29 | } |
| 30 | return ErrRefused |
| 31 | }) |
| 32 | if err != ErrRefused || count(t, f.db, "minted") != 0 || count(t, f.db, "oficina_handoff_pending") != 1 { |
| 33 | t.Fatal("partial commit", err) |
| 34 | } |
| 35 | _, _, err = f.s.FinishAtomic(context.Background(), secret, handle, "", f.now, func(tx *sql.Tx, c assertion.Claims, _ string) error { |
| 36 | _, e := tx.Exec("INSERT INTO minted VALUES(?)", c.Subject) |
| 37 | return e |
| 38 | }) |
| 39 | if err != nil || count(t, f.db, "minted") != 1 || count(t, f.db, "oficina_handoff_pending") != 0 { |
| 40 | t.Fatal("atomic completion", err) |
| 41 | } |
| 42 | } |
| 43 | |