forked from jhillyerd/enmime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotedprint_test.go
108 lines (94 loc) · 2.1 KB
/
quotedprint_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
package enmime
import (
"bytes"
"errors"
"io"
"strings"
"testing"
)
func TestQPCleaner(t *testing.T) {
ttable := []struct {
input string
want string
}{
{"", ""},
{"abcDEF_", "abcDEF_"},
{"=5bSlack=5d", "=5bSlack=5d"},
{"low: ,high:~", "low: ,high:~"},
{"\r\n\t", "\r\n\t"},
{"pédagogues", "p=C3=A9dagogues"},
{"Stuffs’s", "Stuffs=E2=80=99s"},
{"=", "=3D"},
{"=a", "=3Da"},
}
for _, tc := range ttable {
// Run cleaner
cleaner := newQPCleaner(strings.NewReader(tc.input))
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(cleaner)
if err != nil {
t.Fatal(err)
}
got := buf.String()
if got != tc.want {
t.Errorf("Got: %q, want: %q", got, tc.want)
}
}
}
// TestQPCleanerOverflow attempts to confuse the cleaner by issuing a smaller subsequent read
func TestQPCleanerOverflow(t *testing.T) {
input := bytes.Repeat([]byte("pédagogues =\r\n"), 1000)
want := bytes.Repeat([]byte("p=C3=A9dagogues =\r\n"), 1000)
inbuf := bytes.NewBuffer(input)
qp := newQPCleaner(inbuf)
offset := 0
for len := 1000; len > 0; len -= 100 {
p := make([]byte, len)
n, err := qp.Read(p)
if err != nil {
t.Fatal(err)
}
if n < 1 {
t.Fatalf("Read(p) = %v, wanted >0", n)
}
for i := 0; i < n; i++ {
if p[i] != want[offset] {
t.Errorf("p[%v] = %q, want: %q (want[%v])", i, p[i], want[offset], offset)
}
offset++
}
}
}
var PEEK_ERR = errors.New("DIE BART DIE")
type peekBreakReader string
func (r peekBreakReader) Read(p []byte) (int, error) {
return copy(p, r), PEEK_ERR
}
func TestQPPeekError(t *testing.T) {
qp := newQPCleaner(peekBreakReader("=a"))
buf := make([]byte, 100)
_, err := qp.Read(buf)
if err != PEEK_ERR {
t.Errorf("Got: %q, want: %q", err, PEEK_ERR)
}
}
var result int
func BenchmarkQPCleaner(b *testing.B) {
b.StopTimer()
input := bytes.Repeat([]byte("pédagogues =\r\n"), b.N)
b.SetBytes(int64(len(input)))
inbuf := bytes.NewBuffer(input)
qp := newQPCleaner(inbuf)
p := make([]byte, 1024)
b.StartTimer()
for {
n, err := qp.Read(p)
result += n
if err == io.EOF {
break
}
if err != nil {
b.Fatalf("Read(): %v", err)
}
}
}