generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbase64x_test.go
257 lines (226 loc) · 8.06 KB
/
base64x_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package base64x
import (
`encoding/base64`
`strings`
`testing`
)
type TestPair struct {
decoded string
encoded string
}
type EncodingTest struct {
enc Encoding // Encoding to test
conv func(string) string // Reference string converter
}
var pairs = []TestPair{
// RFC 3548 examples
{"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"},
{"\x14\xfb\x9c\x03\xd9", "FPucA9k="},
{"\x14\xfb\x9c\x03", "FPucAw=="},
// RFC 4648 examples
{"", ""},
{"f", "Zg=="},
{"fo", "Zm8="},
{"foo", "Zm9v"},
{"foob", "Zm9vYg=="},
{"fooba", "Zm9vYmE="},
{"foobar", "Zm9vYmFy"},
// Wikipedia examples
{"sure.", "c3VyZS4="},
{"sure", "c3VyZQ=="},
{"sur", "c3Vy"},
{"su", "c3U="},
{"leasure.", "bGVhc3VyZS4="},
{"easure.", "ZWFzdXJlLg=="},
{"asure.", "YXN1cmUu"},
{"sure.", "c3VyZS4="},
// Relatively long strings
{
"Twas brillig, and the slithy toves",
"VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw==",
}, {
"\x9dyH\xd2Y\x9e^e\x9e\xb1\x9a\x9a\x12\xfe\x8a\x07\xc7\x07\xcc\xe8l\x81" +
"\xf2\xd9\xe3\x89\xb5\x98\xee\xbd\x8etQ`2>\\t:_\xd7w\xe6\xb5\x96\xc7\xff\x9c",
"nXlI0lmeXmWesZqaEv6KB8cHzOhsgfLZ44m1mO69jnRRYDI+XHQ6X9d35rWWx/+c",
},
}
var crlf_pairs = []TestPair{
// RFC 3548 examples
{"\x14\xfb\x9c\x03\xd9\x7e", "FPuc\r\nA9l+"},
{"\x14\xfb\x9c\x03\xd9", "FP\r\r\r\rucA9k="},
{"\x14\xfb\x9c\x03", "\r\nFPucAw=\r=\n"},
// RFC 4648 examples
{"", "\r"},
{"f", "Zg\r\n=="},
{"fo", "Zm\r\n8="},
{"fooba", "Zm\r\n9vY\r\nmE="},
// Wikipedia examples
{"su", "c3U\r="},
{"leasure.", "bGVhc3VyZ\nS4="},
{"easure.", "ZW\r\nFzdXJlLg=\r=\r\n"},
{"asure.", "YXN1cmUu"},
{"sure.", "c3VyZ\r\nS4="},
// Relatively long strings
{
"Twas brillig, and the slithy toves",
"VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw\r\n==\r\n",
}, {
"\x9dyH\xd2Y\x9e^e\x9e\xb1\x9a\x9a\x12\xfe\x8a\x07\xc7\x07\xcc\xe8l\x81" +
"\xf2\xd9\xe3\x89\xb5\x98\xee\xbd\x8etQ`2>\\t:_\xd7w\xe6\xb5\x96\xc7\xff\x9c",
"nXlI0lmeXmWesZqaEv6KB8cHzOhsg\r\nfLZ44m1mO69jnRRYDI+XH\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nQ6X9d35rWWx/\r\n+c",
},
}
var json_pairs = []TestPair{
// RFC 3548 examples
{"\x14\xfb\x9c\x03\xd9\x7e", `FPu\rcA9l+\n`},
{"\x14\xfb\x9c\x03\xd9\x7e", `FPuc\u00419l+`},
{"\x14\xfb\x9c\x03\xd9", `FPucA9k\u003d`},
{"\x14\xfb\x9c\x03\xd9", `FPucA\u0039k\u003d`},
{"\x14\xfb\x9c\x03", `FPucAw\u003d\u003d`},
// RFC 4648 examples
{"", ""},
{"f", "Zg=="},
{"fo", "Zm8="},
{"foo", "Zm9v"},
{"foob", "Zm9vYg=="},
{"fooba", "Zm9vYmE="},
{"foobar", "Zm9vYmFy"},
// Wikipedia examples
{"sure.", "c3VyZS4="},
{"sure", "c3VyZQ=="},
{"sur", "c3Vy"},
{"su", "c3U="},
{"leasure.", "bGVhc3VyZS4="},
{"easure.", "ZWFzdXJlLg=="},
{"asure.", "YXN1cmUu"},
{"sure.", "c3VyZS4="},
// Relatively long strings
{
"Twas brillig, and the slithy toves",
"VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw==",
}, {
"\x9dyH\xd2Y\x9e^e\x9e\xb1\x9a\x9a\x12\xfe\x8a\x07\xc7\x07\xcc\xe8l\x81" +
"\xf2\xd9\xe3\x89\xb5\x98\xee\xbd\x8etQ`2>\\t:_\xd7w\xe6\xb5\x96\xc7\xff\x9c",
`nXlI0lmeXmWesZqaEv6KB8cHzOhsgfLZ44m1mO\u0036\u0039jnRRYDI+XHQ6X9d35rWWx\/+c`,
},
}
// Do nothing to a reference base64 string (leave in standard format)
func stdRef(ref string) string {
return ref
}
// Convert a reference string to URL-encoding
func urlRef(ref string) string {
ref = strings.ReplaceAll(ref, "+", "-")
ref = strings.ReplaceAll(ref, "/", "_")
return ref
}
// Convert a reference string to raw, unpadded format
func rawRef(ref string) string {
return strings.ReplaceAll(ref, "=", "")
}
// Both URL and unpadding conversions
func rawURLRef(ref string) string {
return rawRef(urlRef(ref))
}
var encodingTests = []EncodingTest{
{StdEncoding, stdRef},
{URLEncoding, urlRef},
{RawStdEncoding, rawRef},
{RawURLEncoding, rawURLRef},
}
func testEqual(t *testing.T, msg string, args ...interface{}) bool {
t.Helper()
if args[len(args) - 2] != args[len(args) - 1] {
t.Errorf(msg, args...)
return false
}
return true
}
func TestEncoder(t *testing.T) {
for _, p := range pairs {
for _, tt := range encodingTests {
got := tt.enc.EncodeToString([]byte(p.decoded))
testEqual(t, "Encode(%q) = %q, want %q", p.decoded, got, tt.conv(p.encoded))
}
}
}
func TestDecoder(t *testing.T) {
for _, p := range pairs {
for _, tt := range encodingTests {
encoded := tt.conv(p.encoded)
dbuf := make([]byte, tt.enc.DecodedLen(len(encoded)))
count, err := tt.enc.Decode(dbuf, []byte(encoded))
testEqual(t, "Decode(%q) = error %v, want %v", encoded, err, error(nil))
testEqual(t, "Decode(%q) = length %v, want %v", encoded, count, len(p.decoded))
testEqual(t, "Decode(%q) = %q, want %q", encoded, string(dbuf[0:count]), p.decoded)
dbuf, err = tt.enc.DecodeString(encoded)
testEqual(t, "DecodeString(%q) = error %v, want %v", encoded, err, error(nil))
testEqual(t, "DecodeString(%q) = %q, want %q", encoded, string(dbuf), p.decoded)
}
}
}
func TestDecoderCRLF(t *testing.T) {
for _, p := range crlf_pairs {
for _, tt := range encodingTests {
encoded := tt.conv(p.encoded)
dbuf := make([]byte, tt.enc.DecodedLen(len(encoded)))
count, err := tt.enc.Decode(dbuf, []byte(encoded))
testEqual(t, "Decode(%q) = error %v, want %v", encoded, err, error(nil))
testEqual(t, "Decode(%q) = length %v, want %v", encoded, count, len(p.decoded))
testEqual(t, "Decode(%q) = %q, want %q", encoded, string(dbuf[0:count]), p.decoded)
dbuf, err = tt.enc.DecodeString(encoded)
testEqual(t, "DecodeString(%q) = error %v, want %v", encoded, err, error(nil))
testEqual(t, "DecodeString(%q) = %q, want %q", encoded, string(dbuf), p.decoded)
}
}
}
func TestDecoderJSON(t *testing.T) {
for _, p := range json_pairs {
encoded := p.encoded
dbuf := make([]byte, JSONStdEncoding.DecodedLen(len(encoded)))
count, err := JSONStdEncoding.Decode(dbuf, []byte(encoded))
testEqual(t, "Decode(%q) = error %v, want %v", encoded, err, error(nil))
testEqual(t, "Decode(%q) = length %v, want %v", encoded, count, len(p.decoded))
testEqual(t, "Decode(%q) = %q, want %q", encoded, string(dbuf[0:count]), p.decoded)
dbuf, err = JSONStdEncoding.DecodeString(encoded)
testEqual(t, "DecodeString(%q) = error %v, want %v", encoded, err, error(nil))
testEqual(t, "DecodeString(%q) = %q, want %q", encoded, string(dbuf), p.decoded)
}
}
func TestDecoderError(t *testing.T) {
_, err := StdEncoding.DecodeString("!aGVsbG8sIHdvcmxk")
if err != base64.CorruptInputError(0) {
panic(err)
}
_, err = StdEncoding.DecodeString("aGVsbG8!sIHdvcmxk")
if err != base64.CorruptInputError(7) {
panic(err)
}
_, err = StdEncoding.DecodeString("123456")
if err != base64.CorruptInputError(6) {
panic(err)
}
_, err = StdEncoding.DecodeString("1234;6")
if err != base64.CorruptInputError(4) {
panic(err)
}
_, err = StdEncoding.DecodeString("F\xaa\xaa\xaa\xaaDDDDDDDDDDDDD//z")
if err != base64.CorruptInputError(1) {
panic(err)
}
}