-
Notifications
You must be signed in to change notification settings - Fork 0
/
maildir_test.go
144 lines (102 loc) · 3.17 KB
/
maildir_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package maildir
import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/hownowstephen/email"
)
func TestCreate(t *testing.T) {
defer os.RemoveAll("tmp")
if err := os.MkdirAll("tmp/maildir-test", 0755); err != nil {
t.Errorf("Couldn't create testing dir: %v", err)
}
_, err := NewDir("tmp/maildir-test/")
if err != nil {
t.Errorf("Couldn't create a maildir: %v", err)
}
if !exists("tmp/maildir-test/tmp") {
t.Errorf("Populating children of maildir failed.")
}
}
func TestMakeID(t *testing.T) {
defer os.RemoveAll("tmp")
if err := os.MkdirAll("tmp/maildir-test", 0755); err != nil {
t.Errorf("Couldn't create testing dir: %v", err)
}
dir, err := NewDir("tmp/maildir-test/")
if err != nil {
t.Errorf("Couldn't create a maildir: %v", err)
}
id1 := dir.makeID()
c1 := dir.counter
id2 := dir.makeID()
c2 := dir.counter
if id1 == id2 {
t.Errorf("Ids should be uniquely generated, got %v twice", id1)
}
if c2 <= c1 {
t.Errorf("Counter value should be increasing, got: %v after %v", c2, c1)
}
}
func TestWriteMessage(t *testing.T) {
defer os.RemoveAll("tmp")
dir, err := NewDir("tmp/maildir-test/")
if err != nil {
t.Errorf("Couldn't create a maildir: %v", err)
}
rawMessage := `To: sender@example.org
From: recipient@example.net
Content-Type: text/html
This is the email body`
m, err := email.NewMessage([]byte(rawMessage))
if err != nil {
t.Errorf("Example message unparseable: %v", err)
}
filename, err := dir.Write(m)
if err != nil {
t.Errorf("Couldn't write message to maildir: %v", err)
}
info, err := os.Stat(fmt.Sprintf("tmp/maildir-test/new/%v", filename))
if err != nil {
t.Errorf("Couldn't stat '%v': %v", filename, err)
}
if int(info.Size()) != len(rawMessage) {
t.Errorf("Size of file doesn't match size of message? want: %v, got: %v", len(rawMessage), info.Size())
}
}
func TestOpen(t *testing.T) {
defer os.RemoveAll("tmp")
dir, err := NewDir("tmp/maildir-test/")
if err != nil {
t.Errorf("Couldn't create a maildir: %v", err)
}
rawMessage := `To: sender@example.org
From: recipient@example.net
Content-Type: text/html
This is the email body`
m, err := email.NewMessage([]byte(rawMessage))
if err != nil {
t.Errorf("Example message unparseable: %v", err)
}
filename, err := dir.Write(m)
n, err := dir.Open(filename)
if err != nil {
t.Errorf("Couldn't open message: %v", err)
}
if !reflect.DeepEqual(m, n) {
t.Errorf("Reloaded message doesn't match saved message! want: %v, got: %v", m, n)
}
err = os.Rename(filepath.Join(dir.dir, "new", filename), filepath.Join(dir.dir, "cur", filename)+":PRS")
if err != nil {
t.Errorf("Couldn't move file to cur: %v", err)
}
c, err := dir.Open(filename)
if err != nil {
t.Errorf("Couldn't open message from cur: %v", err)
}
if !reflect.DeepEqual(m, c) {
t.Errorf("Message from cur didn't match original! want: %v, got: %v", m, c)
}
}