mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
137 lines
4.4 KiB
Go
137 lines
4.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
"github.com/phishingclub/phishingclub/database"
|
|
"github.com/phishingclub/phishingclub/utils"
|
|
)
|
|
|
|
// busySnapshotError makes a real SQLITE_BUSY_SNAPSHOT: a transaction reads,
|
|
// another connection writes and commits, and the first transaction then
|
|
// tries to write
|
|
func busySnapshotError(t *testing.T) error {
|
|
t.Helper()
|
|
dsn := filepath.Join(t.TempDir(), "busy.db") + "?_journal_mode=WAL&_busy_timeout=5000"
|
|
a, err := sql.Open("sqlite3", dsn)
|
|
if err != nil {
|
|
t.Fatalf("open: %v", err)
|
|
}
|
|
defer a.Close()
|
|
b, err := sql.Open("sqlite3", dsn)
|
|
if err != nil {
|
|
t.Fatalf("open: %v", err)
|
|
}
|
|
defer b.Close()
|
|
if _, err := a.Exec("CREATE TABLE t (v INTEGER)"); err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
ctx := context.Background()
|
|
tx, err := a.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
t.Fatalf("begin: %v", err)
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
var n int
|
|
if err := tx.QueryRow("SELECT COUNT(*) FROM t").Scan(&n); err != nil {
|
|
t.Fatalf("read: %v", err)
|
|
}
|
|
if _, err := b.Exec("INSERT INTO t (v) VALUES (1)"); err != nil {
|
|
t.Fatalf("other write: %v", err)
|
|
}
|
|
_, err = tx.Exec("INSERT INTO t (v) VALUES (2)")
|
|
if err == nil {
|
|
t.Fatal("a write after a stale read must fail")
|
|
}
|
|
return err
|
|
}
|
|
|
|
// TestRetryBusyTriesAgain checks a chunk write that fails with a busy error,
|
|
// as a stale read before the first write gives, is tried again, and any
|
|
// other error or a busy error on every try ends the write
|
|
func TestRetryBusyTriesAgain(t *testing.T) {
|
|
busy := busySnapshotError(t)
|
|
if !isBusy(busy) {
|
|
t.Fatalf("a busy snapshot error must count as busy: %v", busy)
|
|
}
|
|
ctx := context.Background()
|
|
calls := 0
|
|
if err := retryBusy(ctx, func() error {
|
|
calls++
|
|
if calls < 3 {
|
|
return busy
|
|
}
|
|
return nil
|
|
}); err != nil || calls != 3 {
|
|
t.Fatalf("expected success on the third try, got %v after %d", err, calls)
|
|
}
|
|
calls = 0
|
|
if err := retryBusy(ctx, func() error {
|
|
calls++
|
|
return busy
|
|
}); err == nil || calls != closeWriteTries {
|
|
t.Fatalf("expected the busy error after %d tries, got %v after %d", closeWriteTries, err, calls)
|
|
}
|
|
calls = 0
|
|
other := context.Canceled
|
|
if err := retryBusy(ctx, func() error {
|
|
calls++
|
|
return other
|
|
}); err != other || calls != 1 {
|
|
t.Fatalf("another error must not be tried again, got %v after %d", err, calls)
|
|
}
|
|
}
|
|
|
|
// TestCloseKeepsTakenSends checks a send the sender took keeps its row and
|
|
// its events when the program closes, as it may have gone out before sent_at
|
|
// was written, while a send never taken is cancelled and then deleted with
|
|
// its events
|
|
func TestCloseKeepsTakenSends(t *testing.T) {
|
|
db := newProgramTestDB(t)
|
|
ctx := context.Background()
|
|
r := &Program{DB: db}
|
|
programID := createProgram(t, db, "close", nil)
|
|
campaign := ptrUUID()
|
|
createHiddenCampaign(t, db, campaign, programID)
|
|
taken, waiting := ptrUUID(), ptrUUID()
|
|
takenRow := addCampaignRecipient(t, db, campaign, taken, nil, false)
|
|
waitingRow := addCampaignRecipient(t, db, campaign, waiting, nil, false)
|
|
db.Model(&database.CampaignRecipient{}).Where("id = ?", takenRow).
|
|
Update("last_attempt_at", utils.RFC3339UTC(time.Now()))
|
|
eventID := ptrUUID()
|
|
for _, person := range []any{taken, waiting} {
|
|
if err := db.Create(&database.CampaignEvent{
|
|
ID: ptrUUID(), CreatedAt: ptrNow(), UpdatedAt: ptrNow(),
|
|
CampaignID: campaign, RecipientID: person.(*uuid.UUID), EventID: eventID,
|
|
}).Error; err != nil {
|
|
t.Fatalf("add event: %v", err)
|
|
}
|
|
}
|
|
if err := r.CancelAllUnsent(ctx, programID); err != nil {
|
|
t.Fatalf("cancel: %v", err)
|
|
}
|
|
if n, err := r.CancelSends(ctx, []*uuid.UUID{takenRow}); err != nil || n != 0 {
|
|
t.Fatalf("a taken send must not be cancelled: %d %v", n, err)
|
|
}
|
|
if err := r.DeleteUnsentSends(ctx, programID); err != nil {
|
|
t.Fatalf("delete: %v", err)
|
|
}
|
|
var row database.CampaignRecipient
|
|
if err := db.Where("id = ?", takenRow).First(&row).Error; err != nil || row.CancelledAt != nil {
|
|
t.Fatalf("the taken send must stay as it was: %v %+v", err, row)
|
|
}
|
|
if countWhere(t, db, &database.CampaignEvent{}, "recipient_id", taken) != 1 {
|
|
t.Fatal("the events of the taken send must stay")
|
|
}
|
|
if countWhere(t, db, &database.CampaignRecipient{}, "id", waitingRow) != 0 ||
|
|
countWhere(t, db, &database.CampaignEvent{}, "recipient_id", waiting) != 0 {
|
|
t.Fatal("a send never taken is deleted with its events")
|
|
}
|
|
}
|