maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

 1//go:build cgo && !no_sqlite3
 2// +build cgo,!no_sqlite3
 3
 4package blob
 5
 6import (
 7	"math/rand"
 8	"testing"
 9
10	backendtests "github.com/foxcpp/go-imap-backend-tests"
11	imapsql "github.com/foxcpp/go-imap-sql"
12	"github.com/foxcpp/maddy/framework/module"
13	imapsql2 "github.com/foxcpp/maddy/internal/storage/imapsql"
14	"github.com/foxcpp/maddy/internal/testutils"
15)
16
17type testBack struct {
18	backendtests.Backend
19	ExtStore module.BlobStore
20}
21
22func TestStore(t *testing.T, newStore func() module.BlobStore, cleanStore func(module.BlobStore)) {
23	// We use go-imap-sql backend and run a subset of
24	// go-imap-backend-tests related to loading and saving messages.
25	//
26	// In the future we should probably switch to using a memory
27	// backend for this.
28
29	backendtests.Whitelist = []string{
30		t.Name() + "/Mailbox_CreateMessage",
31		t.Name() + "/Mailbox_ListMessages_Body",
32		t.Name() + "/Mailbox_CopyMessages",
33		t.Name() + "/Mailbox_Expunge",
34		t.Name() + "/Mailbox_MoveMessages",
35	}
36
37	initBackend := func() backendtests.Backend {
38		randSrc := rand.NewSource(0)
39		prng := rand.New(randSrc)
40		store := newStore()
41
42		b, err := imapsql.New("sqlite3", ":memory:",
43			imapsql2.ExtBlobStore{Base: store}, imapsql.Opts{
44				PRNG: prng,
45				Log:  testutils.Logger(t, "imapsql"),
46			},
47		)
48		if err != nil {
49			panic(err)
50		}
51		return testBack{Backend: b, ExtStore: store}
52	}
53	cleanBackend := func(bi backendtests.Backend) {
54		b := bi.(testBack)
55		if err := b.Backend.(*imapsql.Backend).Close(); err != nil {
56			panic(err)
57		}
58		cleanStore(b.ExtStore)
59	}
60
61	backendtests.RunTests(t, initBackend, cleanBackend)
62}