-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding_test.go
194 lines (177 loc) · 10.2 KB
/
encoding_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package email
import (
"bytes"
"crypto/rand"
"encoding/base64"
"testing"
)
type encodingTestCase struct {
src, exp []byte
}
func Test_Base64Encode(t *testing.T) {
for i := 1; i < 1024; i++ {
src := make([]byte, i)
rand.Read(src)
el := base64.StdEncoding.EncodedLen(i)
b64 := make([]byte, el)
base64.StdEncoding.Encode(b64, src)
exp := make([]byte, 0, el+el/76*2)
for len(b64) > 76 {
exp = append(exp, b64[:76]...)
exp = append(exp, '\r', '\n')
b64 = b64[76:]
}
exp = append(exp, b64...)
if act := Base64Encode(src); !bytes.Equal(act, exp) {
t.Errorf("Base64Encode(%d): got (len=%d)\n%s\nwant (len=%d)\n%s", i, len(act), act, len(exp), exp)
}
}
}
func Test_QuotedPrintableEncode(t *testing.T) {
cases := []encodingTestCase{
{[]byte("test "), []byte("test =")},
{[]byte("test\n me"), []byte("test=0A me")},
{[]byte("test\\/me=again…"), []byte("test\\/me=3Dagain=E2=80=A6")},
{[]byte("Lorem ipsum dolor sit amet, no sit enim fugit, solum omittam evertitur qui cu. Usu ad sonet facilisis, cu partem platonem conceptam has. Tincidunt scribentur nec ex, eu hinc quodsi consequat quo, ex est labore fuisset. Vel semper salutatus ne."),
[]byte("Lorem ipsum dolor sit amet, no sit enim fugit, solum omittam evertitur qui =\r\n" +
"cu. Usu ad sonet facilisis, cu partem platonem conceptam has. Tincidunt scr=\r\n" +
"ibentur nec ex, eu hinc quodsi consequat quo, ex est labore fuisset. Vel se=\r\n" +
"mper salutatus ne.")},
{[]byte("Δεσωρε αππελλανθυρ υθ μει, αν ηαβεο ομνες νυμκυαμ μεα. Αδ φιξ αλικυιπ ινφιδυντ, ηις εξ σαπερεθ δετρασθο σαεφολα, αδ δολορ αλικυανδο ηας. Ευ πυρθο ιυδισο εως, φισι σωνσεκυαθ πρι ευ. Ασυμ σοντεντιωνες ιυς ει, ει κυαεκυε ινσωλενς σενσιβυς κυο. Εξ κυωτ αλιενυμ ηις, συ πρω σονσυλατυ μεδιοσριθαθεμ. Τιβικυε ινστρυσθιορ κυι νο, ευμ ιδ κυοδσι τασιμαθες αδωλεσενς."),
[]byte("=CE=94=CE=B5=CF=83=CF=89=CF=81=CE=B5 =CE=B1=CF=80=CF=80=CE=B5=CE=BB=CE=BB=\r\n" +
"=CE=B1=CE=BD=CE=B8=CF=85=CF=81 =CF=85=CE=B8 =CE=BC=CE=B5=CE=B9, =CE=B1=\r\n" +
"=CE=BD =CE=B7=CE=B1=CE=B2=CE=B5=CE=BF =CE=BF=CE=BC=CE=BD=CE=B5=CF=82 =CE=BD=\r\n" +
"=CF=85=CE=BC=CE=BA=CF=85=CE=B1=CE=BC =CE=BC=CE=B5=CE=B1. =CE=91=CE=B4 =\r\n" +
"=CF=86=CE=B9=CE=BE =CE=B1=CE=BB=CE=B9=CE=BA=CF=85=CE=B9=CF=80 =CE=B9=CE=BD=\r\n" +
"=CF=86=CE=B9=CE=B4=CF=85=CE=BD=CF=84, =CE=B7=CE=B9=CF=82 =CE=B5=CE=BE =\r\n" +
"=CF=83=CE=B1=CF=80=CE=B5=CF=81=CE=B5=CE=B8 =CE=B4=CE=B5=CF=84=CF=81=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E=A4=CE=B9=CE=B2=CE=B9=CE=BA=CF=85=CE=B5 =\r\n" +
"=CE=B9=CE=BD=CF=83=CF=84=CF=81=CF=85=CF=83=CE=B8=CE=B9=CE=BF=CF=81 =CE=BA=\r\n" +
"=CF=85=CE=B9 =CE=BD=CE=BF, =CE=B5=CF=85=CE=BC =CE=B9=CE=B4 =CE=BA=CF=85=\r\n" +
"=CE=BF=CE=B4=CF=83=CE=B9 =CF=84=CE=B1=CF=83=CE=B9=CE=BC=CE=B1=CE=B8=CE=B5=\r\n" +
"=CF=82 =CE=B1=CE=B4=CF=89=CE=BB=CE=B5=CF=83=CE=B5=CE=BD=CF=82.")},
}
for _, c := range cases {
if act := QuotedPrintableEncode(c.src); !bytes.Equal(act, c.exp) {
t.Errorf("QuotedPrintableEncode: got (len=%d)\n%s\nwant (len=%d)\n%s", len(act), act, len(c.exp), c.exp)
}
}
}
func Test_QEncode(t *testing.T) {
cases := []encodingTestCase{
{[]byte("test "), []byte("=?utf-8?q?test_?=")},
{[]byte("test\n me"), []byte("=?utf-8?q?test=0A_me?=")},
{[]byte("test\\/me=again…"), []byte("=?utf-8?q?test\\/me=3Dagain=E2=80=A6?=")},
{[]byte("Lorem ipsum dolor sit amet, no sit enim fugit, solum omittam evertitur qui cu. Usu ad sonet facilisis, cu partem platonem conceptam has. Tincidunt scribentur nec ex, eu hinc quodsi consequat quo, ex est labore fuisset. Vel semper salutatus ne."),
[]byte("=?utf-8?q?Lorem_ipsum_dolor_sit_amet,_no_s?=\r\n" +
" =?utf-8?q?it_enim_fugit,_solum_omittam_evertitur_qui_cu._Usu_ad_sonet_fac?=\r\n" +
" =?utf-8?q?ilisis,_cu_partem_platonem_conceptam_has._Tincidunt_scribentur_?=\r\n" +
" =?utf-8?q?nec_ex,_eu_hinc_quodsi_consequat_quo,_ex_est_labore_fuisset._Ve?=\r\n" +
" =?utf-8?q?l_semper_salutatus_ne.?=")},
{[]byte("Δεσωρε αππελλανθυρ υθ μει, αν ηαβεο ομνες νυμκυαμ μεα. Αδ φιξ αλικυιπ ινφιδυντ, ηις εξ σαπερεθ δετρασθο σαεφολα, αδ δολορ αλικυανδο ηας. Ευ πυρθο ιυδισο εως, φισι σωνσεκυαθ πρι ευ. Ασυμ σοντεντιωνες ιυς ει, ει κυαεκυε ινσωλενς σενσιβυς κυο. Εξ κυωτ αλιενυμ ηις, συ πρω σονσυλατυ μεδιοσριθαθεμ. Τιβικυε ινστρυσθιορ κυι νο, ευμ ιδ κυοδσι τασιμαθες αδωλεσενς."),
[]byte("=?utf-8?q?=CE=94=CE=B5=CF=83=CF=89=CF=81?=\r\n" +
" =?utf-8?q?=CE=B5_=CE=B1=CF=80=CF=80=CE=B5=CE=BB=CE=BB=CE=B1=CE=BD=CE=B8?=\r\n" +
" =?utf-8?q?=CF=85=CF=81_=CF=85=CE=B8_=CE=BC=CE=B5=CE=B9,_=CE=B1=CE=BD_?=\r\n" +
" =?utf-8?q?=CE=B7=CE=B1=CE=B2=CE=B5=CE=BF_=CE=BF=CE=BC=CE=BD=CE=B5=CF=82_?=\r\n" +
" =?utf-8?q?=CE=BD=CF=85=CE=BC=CE=BA=CF=85=CE=B1=CE=BC_=CE=BC=CE=B5=CE=B1._?=\r\n" +
" =?utf-8?q?=CE=91=CE=B4_=CF=86=CE=B9=CE=BE_=CE=B1=CE=BB=CE=B9=CE=BA=CF=85?=\r\n" +
" =?utf-8?q?=CE=B9=CF=80_=CE=B9=CE=BD=CF=86=CE=B9=CE=B4=CF=85=CE=BD=CF=84,_?=\r\n" +
" =?utf-8?q?=CE=B7=CE=B9=CF=82_=CE=B5=CE=BE_=CF=83=CE=B1=CF=80=CE=B5=CF=81?=\r\n" +
" =?utf-8?q?=CE=B5=CE=B8_=CE=B4=CE=B5=CF=84=CF=81=CE=B1=CF=83=CE=B8=CE=BF_?=\r\n" +
" =?utf-8?q?=CF=83=CE=B1=CE=B5=CF=86=CE=BF=CE=BB=CE=B1,_=CE=B1=CE=B4_=CE=B4?=\r\n" +
" =?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9=CE=BF=CF=83=CF=81=CE=B9=CE=B8?=\r\n" +
" =?utf-8?q?=CE=B1=CE=B8=CE=B5=CE=BC._=CE=A4=CE=B9=CE=B2=CE=B9=CE=BA=CF=85?=\r\n" +
" =?utf-8?q?=CE=B5_=CE=B9=CE=BD=CF=83=CF=84=CF=81=CF=85=CF=83=CE=B8=CE=B9?=\r\n" +
" =?utf-8?q?=CE=BF=CF=81_=CE=BA=CF=85=CE=B9_=CE=BD=CE=BF,_=CE=B5=CF=85?=\r\n" +
" =?utf-8?q?=CE=BC_=CE=B9=CE=B4_=CE=BA=CF=85=CE=BF=CE=B4=CF=83=CE=B9_=CF=84?=\r\n" +
" =?utf-8?q?=CE=B1=CF=83=CE=B9=CE=BC=CE=B1=CE=B8=CE=B5=CF=82_=CE=B1=CE=B4?=\r\n" +
" =?utf-8?q?=CF=89=CE=BB=CE=B5=CF=83=CE=B5=CE=BD=CF=82.?=")},
}
for _, c := range cases {
expOffset := 32 + len(c.exp)
if pos := bytes.LastIndex(c.exp, []byte("\r\n")); pos > -1 {
expOffset = len(c.exp) - pos - 2
}
act, pos := QEncode(c.src, 32)
if !bytes.Equal(act, c.exp) {
t.Errorf("QEncode: got (len=%d)\n%s\nwant (len=%d)\n%s", len(act), act, len(c.exp), c.exp)
}
if pos != expOffset {
t.Errorf("QEncode: got offset = %d, want %d", pos, expOffset)
}
}
}
var b64bmRes []byte
func benchmarkBase64Encode(srcLen int, b *testing.B) {
src := make([]byte, srcLen)
for i := 0; i < b.N; i++ {
rand.Read(src)
b64bmRes = Base64Encode(src)
}
}
func benchmarkBase64EncodeStdlib(srcLen int, b *testing.B) {
src := make([]byte, srcLen)
for i := 0; i < b.N; i++ {
rand.Read(src)
el := base64.StdEncoding.EncodedLen(srcLen)
b64 := make([]byte, el)
base64.StdEncoding.Encode(b64, src)
exp := make([]byte, 0, el+el/76*2)
for len(b64) > 76 {
exp = append(exp, b64[:76]...)
exp = append(exp, '\r', '\n')
b64 = b64[76:]
}
b64bmRes = append(exp, b64...)
}
}
func Benchmark_Base64Encode_1k(b *testing.B) {
benchmarkBase64Encode(1024, b)
}
func Benchmark_Base64Encode_stdlib_1k(b *testing.B) {
benchmarkBase64EncodeStdlib(1024, b)
}
func Benchmark_Base64Encode_4k(b *testing.B) {
benchmarkBase64Encode(4096, b)
}
func Benchmark_Base64Encode_stdlib_4k(b *testing.B) {
benchmarkBase64EncodeStdlib(4096, b)
}
func Benchmark_Base64Encode_10k(b *testing.B) {
benchmarkBase64Encode(10240, b)
}
func Benchmark_Base64Encode_stdlib_10k(b *testing.B) {
benchmarkBase64EncodeStdlib(10240, b)
}
func Benchmark_Base64Encode_40k(b *testing.B) {
benchmarkBase64Encode(40960, b)
}
func Benchmark_Base64Encode_stdlib_40k(b *testing.B) {
benchmarkBase64EncodeStdlib(40960, b)
}