-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathreference_counter_test.go
128 lines (109 loc) · 4.57 KB
/
reference_counter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package imapsql
import (
"fmt"
"io/ioutil"
"strings"
"testing"
"time"
"github.com/emersion/go-imap"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
func checkKeysCount(b *Backend, expected int) is.Comparison {
return func() is.Result {
dirList, err := ioutil.ReadDir(b.extStore.(*FSStore).Root)
if err != nil {
return is.ResultFromError(err)
}
if len(dirList) != expected {
names := make([]string, 0, len(dirList))
for _, ent := range dirList {
names = append(names, ent.Name())
}
return is.ResultFailure(fmt.Sprintf("expected %d keys to be stored, got %d: %v", expected, len(dirList), names))
}
return is.ResultSuccess
}
}
func TestKeyIsRemovedWithMsg(t *testing.T) {
b := initTestBackend().(*Backend)
defer cleanBackend(b)
assert.NilError(t, b.CreateUser(t.Name()))
usr, err := b.GetUser(t.Name())
assert.NilError(t, err)
assert.NilError(t, usr.CreateMailbox(t.Name()))
_, mbox, err := usr.GetMailbox(t.Name(), true, &noopConn{})
assert.NilError(t, err)
defer mbox.Close()
// Message is created, there should be a key.
assert.NilError(t, usr.CreateMessage(mbox.Name(), []string{imap.DeletedFlag}, time.Now(), strings.NewReader(testMsg), mbox))
assert.NilError(t, mbox.Poll(true))
assert.Assert(t, checkKeysCount(b, 1), "Wrong amount of external store keys created")
// Message is removed, there should be no key anymore.
assert.NilError(t, mbox.Expunge())
assert.Assert(t, checkKeysCount(b, 0), "Key is not removed after message removal")
}
func TestKeyIsRemovedWithMbox(t *testing.T) {
b := initTestBackend().(*Backend)
defer cleanBackend(b)
assert.NilError(t, b.CreateUser(t.Name()))
usr, err := b.GetUser(t.Name())
assert.NilError(t, err)
assert.NilError(t, usr.CreateMailbox(t.Name()))
_, mbox, err := usr.GetMailbox(t.Name(), true, &noopConn{})
assert.NilError(t, err)
// Message is created, there should be a key.
assert.NilError(t, usr.CreateMessage(mbox.Name(), []string{imap.DeletedFlag}, time.Now(), strings.NewReader(testMsg), mbox))
assert.NilError(t, mbox.Poll(true))
assert.Assert(t, checkKeysCount(b, 1), "Wrong amount of external store keys created")
// The mbox is removed along with all messages, there should be no key anymore.
assert.NilError(t, usr.DeleteMailbox(t.Name()))
assert.Assert(t, checkKeysCount(b, 0), "Key is not removed after mbox removal")
}
func TestKeyIsRemovedWithCopiedMsgs(t *testing.T) {
b := initTestBackend().(*Backend)
defer cleanBackend(b)
assert.NilError(t, b.CreateUser(t.Name()))
usr, err := b.GetUser(t.Name())
assert.NilError(t, err)
assert.NilError(t, usr.CreateMailbox(t.Name()+"-1"))
_, mbox1, err := usr.GetMailbox(t.Name()+"-1", true, &noopConn{})
assert.NilError(t, err)
defer mbox1.Close()
assert.NilError(t, usr.CreateMailbox(t.Name()+"-2"))
_, mbox2, err := usr.GetMailbox(t.Name()+"-2", true, &noopConn{})
assert.NilError(t, err)
defer mbox2.Close()
// The message is created, there should be a key.
assert.NilError(t, usr.CreateMessage(mbox1.Name(), []string{imap.DeletedFlag}, time.Now(), strings.NewReader(testMsg), mbox1))
assert.NilError(t, mbox1.Poll(true))
assert.Assert(t, checkKeysCount(b, 1), "Wrong amount of external store keys created")
// The message is copied, there should be no duplicate key.
seq, _ := imap.ParseSeqSet("1")
assert.NilError(t, mbox1.CopyMessages(false, seq, mbox2.Name()))
assert.NilError(t, mbox2.Poll(true))
assert.Assert(t, checkKeysCount(b, 1), "Wrong amount of external store keys")
// The message copy is removed, key should be still here.
assert.NilError(t, mbox2.Expunge())
assert.Assert(t, checkKeysCount(b, 1), "Wrong amount of external store keys")
// Both messages are deleted, there should be no key anymore.
assert.NilError(t, mbox1.Expunge())
assert.Assert(t, checkKeysCount(b, 0), "Key is not removed after message removal")
}
func TestKeyIsRemovedWithUser(t *testing.T) {
b := initTestBackend().(*Backend)
defer cleanBackend(b)
assert.NilError(t, b.CreateUser(t.Name()))
usr, err := b.GetUser(t.Name())
assert.NilError(t, err)
assert.NilError(t, usr.CreateMailbox(t.Name()))
_, mbox, err := usr.GetMailbox(t.Name(), true, &noopConn{})
assert.NilError(t, err)
defer mbox.Close()
// The message is created, there should be a key.
assert.NilError(t, usr.CreateMessage(mbox.Name(), []string{imap.DeletedFlag}, time.Now(), strings.NewReader(testMsg), mbox))
assert.Assert(t, checkKeysCount(b, 1), "Wrong amount of external store keys created")
// The user account is removed, all keys should be gone.
assert.NilError(t, b.DeleteUser(usr.Username()))
assert.Assert(t, checkKeysCount(b, 0), "Key is not removed after message removal")
}