Home | History | Annotate | Download | only in base64
      1 // Copyright 2009 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package base64
      6 
      7 import (
      8 	"bytes"
      9 	"errors"
     10 	"io"
     11 	"io/ioutil"
     12 	"reflect"
     13 	"strings"
     14 	"testing"
     15 	"time"
     16 )
     17 
     18 type testpair struct {
     19 	decoded, encoded string
     20 }
     21 
     22 var pairs = []testpair{
     23 	// RFC 3548 examples
     24 	{"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"},
     25 	{"\x14\xfb\x9c\x03\xd9", "FPucA9k="},
     26 	{"\x14\xfb\x9c\x03", "FPucAw=="},
     27 
     28 	// RFC 4648 examples
     29 	{"", ""},
     30 	{"f", "Zg=="},
     31 	{"fo", "Zm8="},
     32 	{"foo", "Zm9v"},
     33 	{"foob", "Zm9vYg=="},
     34 	{"fooba", "Zm9vYmE="},
     35 	{"foobar", "Zm9vYmFy"},
     36 
     37 	// Wikipedia examples
     38 	{"sure.", "c3VyZS4="},
     39 	{"sure", "c3VyZQ=="},
     40 	{"sur", "c3Vy"},
     41 	{"su", "c3U="},
     42 	{"leasure.", "bGVhc3VyZS4="},
     43 	{"easure.", "ZWFzdXJlLg=="},
     44 	{"asure.", "YXN1cmUu"},
     45 	{"sure.", "c3VyZS4="},
     46 }
     47 
     48 // Do nothing to a reference base64 string (leave in standard format)
     49 func stdRef(ref string) string {
     50 	return ref
     51 }
     52 
     53 // Convert a reference string to URL-encoding
     54 func urlRef(ref string) string {
     55 	ref = strings.Replace(ref, "+", "-", -1)
     56 	ref = strings.Replace(ref, "/", "_", -1)
     57 	return ref
     58 }
     59 
     60 // Convert a reference string to raw, unpadded format
     61 func rawRef(ref string) string {
     62 	return strings.TrimRight(ref, "=")
     63 }
     64 
     65 // Both URL and unpadding conversions
     66 func rawUrlRef(ref string) string {
     67 	return rawRef(urlRef(ref))
     68 }
     69 
     70 // A nonstandard encoding with a funny padding character, for testing
     71 var funnyEncoding = NewEncoding(encodeStd).WithPadding(rune('@'))
     72 
     73 func funnyRef(ref string) string {
     74 	return strings.Replace(ref, "=", "@", -1)
     75 }
     76 
     77 type encodingTest struct {
     78 	enc  *Encoding           // Encoding to test
     79 	conv func(string) string // Reference string converter
     80 }
     81 
     82 var encodingTests = []encodingTest{
     83 	encodingTest{StdEncoding, stdRef},
     84 	encodingTest{URLEncoding, urlRef},
     85 	encodingTest{RawStdEncoding, rawRef},
     86 	encodingTest{RawURLEncoding, rawUrlRef},
     87 	encodingTest{funnyEncoding, funnyRef},
     88 }
     89 
     90 var bigtest = testpair{
     91 	"Twas brillig, and the slithy toves",
     92 	"VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw==",
     93 }
     94 
     95 func testEqual(t *testing.T, msg string, args ...interface{}) bool {
     96 	if args[len(args)-2] != args[len(args)-1] {
     97 		t.Errorf(msg, args...)
     98 		return false
     99 	}
    100 	return true
    101 }
    102 
    103 func TestEncode(t *testing.T) {
    104 	for _, p := range pairs {
    105 		for _, tt := range encodingTests {
    106 			got := tt.enc.EncodeToString([]byte(p.decoded))
    107 			testEqual(t, "Encode(%q) = %q, want %q", p.decoded,
    108 				got, tt.conv(p.encoded))
    109 		}
    110 	}
    111 }
    112 
    113 func TestEncoder(t *testing.T) {
    114 	for _, p := range pairs {
    115 		bb := &bytes.Buffer{}
    116 		encoder := NewEncoder(StdEncoding, bb)
    117 		encoder.Write([]byte(p.decoded))
    118 		encoder.Close()
    119 		testEqual(t, "Encode(%q) = %q, want %q", p.decoded, bb.String(), p.encoded)
    120 	}
    121 }
    122 
    123 func TestEncoderBuffering(t *testing.T) {
    124 	input := []byte(bigtest.decoded)
    125 	for bs := 1; bs <= 12; bs++ {
    126 		bb := &bytes.Buffer{}
    127 		encoder := NewEncoder(StdEncoding, bb)
    128 		for pos := 0; pos < len(input); pos += bs {
    129 			end := pos + bs
    130 			if end > len(input) {
    131 				end = len(input)
    132 			}
    133 			n, err := encoder.Write(input[pos:end])
    134 			testEqual(t, "Write(%q) gave error %v, want %v", input[pos:end], err, error(nil))
    135 			testEqual(t, "Write(%q) gave length %v, want %v", input[pos:end], n, end-pos)
    136 		}
    137 		err := encoder.Close()
    138 		testEqual(t, "Close gave error %v, want %v", err, error(nil))
    139 		testEqual(t, "Encoding/%d of %q = %q, want %q", bs, bigtest.decoded, bb.String(), bigtest.encoded)
    140 	}
    141 }
    142 
    143 func TestDecode(t *testing.T) {
    144 	for _, p := range pairs {
    145 		for _, tt := range encodingTests {
    146 			encoded := tt.conv(p.encoded)
    147 			dbuf := make([]byte, tt.enc.DecodedLen(len(encoded)))
    148 			count, end, err := tt.enc.decode(dbuf, []byte(encoded))
    149 			testEqual(t, "Decode(%q) = error %v, want %v", encoded, err, error(nil))
    150 			testEqual(t, "Decode(%q) = length %v, want %v", encoded, count, len(p.decoded))
    151 			if len(encoded) > 0 {
    152 				testEqual(t, "Decode(%q) = end %v, want %v", encoded, end, len(p.decoded)%3 != 0)
    153 			}
    154 			testEqual(t, "Decode(%q) = %q, want %q", encoded, string(dbuf[0:count]), p.decoded)
    155 
    156 			dbuf, err = tt.enc.DecodeString(encoded)
    157 			testEqual(t, "DecodeString(%q) = error %v, want %v", encoded, err, error(nil))
    158 			testEqual(t, "DecodeString(%q) = %q, want %q", string(dbuf), p.decoded)
    159 		}
    160 	}
    161 }
    162 
    163 func TestDecoder(t *testing.T) {
    164 	for _, p := range pairs {
    165 		decoder := NewDecoder(StdEncoding, strings.NewReader(p.encoded))
    166 		dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded)))
    167 		count, err := decoder.Read(dbuf)
    168 		if err != nil && err != io.EOF {
    169 			t.Fatal("Read failed", err)
    170 		}
    171 		testEqual(t, "Read from %q = length %v, want %v", p.encoded, count, len(p.decoded))
    172 		testEqual(t, "Decoding of %q = %q, want %q", p.encoded, string(dbuf[0:count]), p.decoded)
    173 		if err != io.EOF {
    174 			count, err = decoder.Read(dbuf)
    175 		}
    176 		testEqual(t, "Read from %q = %v, want %v", p.encoded, err, io.EOF)
    177 	}
    178 }
    179 
    180 func TestDecoderBuffering(t *testing.T) {
    181 	for bs := 1; bs <= 12; bs++ {
    182 		decoder := NewDecoder(StdEncoding, strings.NewReader(bigtest.encoded))
    183 		buf := make([]byte, len(bigtest.decoded)+12)
    184 		var total int
    185 		for total = 0; total < len(bigtest.decoded); {
    186 			n, err := decoder.Read(buf[total : total+bs])
    187 			testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, error(nil))
    188 			total += n
    189 		}
    190 		testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded)
    191 	}
    192 }
    193 
    194 func TestDecodeCorrupt(t *testing.T) {
    195 	testCases := []struct {
    196 		input  string
    197 		offset int // -1 means no corruption.
    198 	}{
    199 		{"", -1},
    200 		{"!!!!", 0},
    201 		{"====", 0},
    202 		{"x===", 1},
    203 		{"=AAA", 0},
    204 		{"A=AA", 1},
    205 		{"AA=A", 2},
    206 		{"AA==A", 4},
    207 		{"AAA=AAAA", 4},
    208 		{"AAAAA", 4},
    209 		{"AAAAAA", 4},
    210 		{"A=", 1},
    211 		{"A==", 1},
    212 		{"AA=", 3},
    213 		{"AA==", -1},
    214 		{"AAA=", -1},
    215 		{"AAAA", -1},
    216 		{"AAAAAA=", 7},
    217 		{"YWJjZA=====", 8},
    218 	}
    219 	for _, tc := range testCases {
    220 		dbuf := make([]byte, StdEncoding.DecodedLen(len(tc.input)))
    221 		_, err := StdEncoding.Decode(dbuf, []byte(tc.input))
    222 		if tc.offset == -1 {
    223 			if err != nil {
    224 				t.Error("Decoder wrongly detected coruption in", tc.input)
    225 			}
    226 			continue
    227 		}
    228 		switch err := err.(type) {
    229 		case CorruptInputError:
    230 			testEqual(t, "Corruption in %q at offset %v, want %v", tc.input, int(err), tc.offset)
    231 		default:
    232 			t.Error("Decoder failed to detect corruption in", tc)
    233 		}
    234 	}
    235 }
    236 
    237 func TestBig(t *testing.T) {
    238 	n := 3*1000 + 1
    239 	raw := make([]byte, n)
    240 	const alpha = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    241 	for i := 0; i < n; i++ {
    242 		raw[i] = alpha[i%len(alpha)]
    243 	}
    244 	encoded := new(bytes.Buffer)
    245 	w := NewEncoder(StdEncoding, encoded)
    246 	nn, err := w.Write(raw)
    247 	if nn != n || err != nil {
    248 		t.Fatalf("Encoder.Write(raw) = %d, %v want %d, nil", nn, err, n)
    249 	}
    250 	err = w.Close()
    251 	if err != nil {
    252 		t.Fatalf("Encoder.Close() = %v want nil", err)
    253 	}
    254 	decoded, err := ioutil.ReadAll(NewDecoder(StdEncoding, encoded))
    255 	if err != nil {
    256 		t.Fatalf("ioutil.ReadAll(NewDecoder(...)): %v", err)
    257 	}
    258 
    259 	if !bytes.Equal(raw, decoded) {
    260 		var i int
    261 		for i = 0; i < len(decoded) && i < len(raw); i++ {
    262 			if decoded[i] != raw[i] {
    263 				break
    264 			}
    265 		}
    266 		t.Errorf("Decode(Encode(%d-byte string)) failed at offset %d", n, i)
    267 	}
    268 }
    269 
    270 func TestNewLineCharacters(t *testing.T) {
    271 	// Each of these should decode to the string "sure", without errors.
    272 	const expected = "sure"
    273 	examples := []string{
    274 		"c3VyZQ==",
    275 		"c3VyZQ==\r",
    276 		"c3VyZQ==\n",
    277 		"c3VyZQ==\r\n",
    278 		"c3VyZ\r\nQ==",
    279 		"c3V\ryZ\nQ==",
    280 		"c3V\nyZ\rQ==",
    281 		"c3VyZ\nQ==",
    282 		"c3VyZQ\n==",
    283 		"c3VyZQ=\n=",
    284 		"c3VyZQ=\r\n\r\n=",
    285 	}
    286 	for _, e := range examples {
    287 		buf, err := StdEncoding.DecodeString(e)
    288 		if err != nil {
    289 			t.Errorf("Decode(%q) failed: %v", e, err)
    290 			continue
    291 		}
    292 		if s := string(buf); s != expected {
    293 			t.Errorf("Decode(%q) = %q, want %q", e, s, expected)
    294 		}
    295 	}
    296 }
    297 
    298 type nextRead struct {
    299 	n   int   // bytes to return
    300 	err error // error to return
    301 }
    302 
    303 // faultInjectReader returns data from source, rate-limited
    304 // and with the errors as written to nextc.
    305 type faultInjectReader struct {
    306 	source string
    307 	nextc  <-chan nextRead
    308 }
    309 
    310 func (r *faultInjectReader) Read(p []byte) (int, error) {
    311 	nr := <-r.nextc
    312 	if len(p) > nr.n {
    313 		p = p[:nr.n]
    314 	}
    315 	n := copy(p, r.source)
    316 	r.source = r.source[n:]
    317 	return n, nr.err
    318 }
    319 
    320 // tests that we don't ignore errors from our underlying reader
    321 func TestDecoderIssue3577(t *testing.T) {
    322 	next := make(chan nextRead, 10)
    323 	wantErr := errors.New("my error")
    324 	next <- nextRead{5, nil}
    325 	next <- nextRead{10, wantErr}
    326 	next <- nextRead{0, wantErr}
    327 	d := NewDecoder(StdEncoding, &faultInjectReader{
    328 		source: "VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw==", // twas brillig...
    329 		nextc:  next,
    330 	})
    331 	errc := make(chan error)
    332 	go func() {
    333 		_, err := ioutil.ReadAll(d)
    334 		errc <- err
    335 	}()
    336 	select {
    337 	case err := <-errc:
    338 		if err != wantErr {
    339 			t.Errorf("got error %v; want %v", err, wantErr)
    340 		}
    341 	case <-time.After(5 * time.Second):
    342 		t.Errorf("timeout; Decoder blocked without returning an error")
    343 	}
    344 }
    345 
    346 func TestDecoderIssue4779(t *testing.T) {
    347 	encoded := `CP/EAT8AAAEF
    348 AQEBAQEBAAAAAAAAAAMAAQIEBQYHCAkKCwEAAQUBAQEBAQEAAAAAAAAAAQACAwQFBgcICQoLEAAB
    349 BAEDAgQCBQcGCAUDDDMBAAIRAwQhEjEFQVFhEyJxgTIGFJGhsUIjJBVSwWIzNHKC0UMHJZJT8OHx
    350 Y3M1FqKygyZEk1RkRcKjdDYX0lXiZfKzhMPTdePzRieUpIW0lcTU5PSltcXV5fVWZnaGlqa2xtbm
    351 9jdHV2d3h5ent8fX5/cRAAICAQIEBAMEBQYHBwYFNQEAAhEDITESBEFRYXEiEwUygZEUobFCI8FS
    352 0fAzJGLhcoKSQ1MVY3M08SUGFqKygwcmNcLSRJNUoxdkRVU2dGXi8rOEw9N14/NGlKSFtJXE1OT0
    353 pbXF1eX1VmZ2hpamtsbW5vYnN0dXZ3eHl6e3x//aAAwDAQACEQMRAD8A9VSSSSUpJJJJSkkkJ+Tj
    354 1kiy1jCJJDnAcCTykpKkuQ6p/jN6FgmxlNduXawwAzaGH+V6jn/R/wCt71zdn+N/qL3kVYFNYB4N
    355 ji6PDVjWpKp9TSXnvTf8bFNjg3qOEa2n6VlLpj/rT/pf567DpX1i6L1hs9Py67X8mqdtg/rUWbbf
    356 +gkp0kkkklKSSSSUpJJJJT//0PVUkkklKVLq3WMDpGI7KzrNjADtYNXvI/Mqr/Pd/q9W3vaxjnvM
    357 NaCXE9gNSvGPrf8AWS3qmba5jjsJhoB0DAf0NDf6sevf+/lf8Hj0JJATfWT6/dV6oXU1uOLQeKKn
    358 EQP+Hubtfe/+R7Mf/g7f5xcocp++Z11JMCJPgFBxOg7/AOuqDx8I/ikpkXkmSdU8mJIJA/O8EMAy
    359 j+mSARB/17pKVXYWHXjsj7yIex0PadzXMO1zT5KHoNA3HT8ietoGhgjsfA+CSnvvqh/jJtqsrwOv
    360 2b6NGNzXfTYexzJ+nU7/ALkf4P8Awv6P9KvTQQ4AgyDqCF85Pho3CTB7eHwXoH+LT65uZbX9X+o2
    361 bqbPb06551Y4
    362 `
    363 	encodedShort := strings.Replace(encoded, "\n", "", -1)
    364 
    365 	dec := NewDecoder(StdEncoding, strings.NewReader(encoded))
    366 	res1, err := ioutil.ReadAll(dec)
    367 	if err != nil {
    368 		t.Errorf("ReadAll failed: %v", err)
    369 	}
    370 
    371 	dec = NewDecoder(StdEncoding, strings.NewReader(encodedShort))
    372 	var res2 []byte
    373 	res2, err = ioutil.ReadAll(dec)
    374 	if err != nil {
    375 		t.Errorf("ReadAll failed: %v", err)
    376 	}
    377 
    378 	if !bytes.Equal(res1, res2) {
    379 		t.Error("Decoded results not equal")
    380 	}
    381 }
    382 
    383 func TestDecoderIssue7733(t *testing.T) {
    384 	s, err := StdEncoding.DecodeString("YWJjZA=====")
    385 	want := CorruptInputError(8)
    386 	if !reflect.DeepEqual(want, err) {
    387 		t.Errorf("Error = %v; want CorruptInputError(8)", err)
    388 	}
    389 	if string(s) != "abcd" {
    390 		t.Errorf("DecodeString = %q; want abcd", s)
    391 	}
    392 }
    393 
    394 func BenchmarkEncodeToString(b *testing.B) {
    395 	data := make([]byte, 8192)
    396 	b.SetBytes(int64(len(data)))
    397 	for i := 0; i < b.N; i++ {
    398 		StdEncoding.EncodeToString(data)
    399 	}
    400 }
    401 
    402 func BenchmarkDecodeString(b *testing.B) {
    403 	data := StdEncoding.EncodeToString(make([]byte, 8192))
    404 	b.SetBytes(int64(len(data)))
    405 	for i := 0; i < b.N; i++ {
    406 		StdEncoding.DecodeString(data)
    407 	}
    408 }
    409