...

Source file src/encoding/ascii85/ascii85_test.go

Documentation: encoding/ascii85

		 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 ascii85
		 6  
		 7  import (
		 8  	"bytes"
		 9  	"io"
		10  	"strings"
		11  	"testing"
		12  )
		13  
		14  type testpair struct {
		15  	decoded, encoded string
		16  }
		17  
		18  var bigtest = testpair{
		19  	"Man is distinguished, not only by his reason, but by this singular passion from " +
		20  		"other animals, which is a lust of the mind, that by a perseverance of delight in " +
		21  		"the continued and indefatigable generation of knowledge, exceeds the short " +
		22  		"vehemence of any carnal pleasure.",
		23  	"9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>[email protected]$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,\n" +
		24  		"O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKY\n" +
		25  		"i(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIa\n" +
		26  		"l(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G\n" +
		27  		">uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c\n",
		28  }
		29  
		30  var pairs = []testpair{
		31  	// Encode returns 0 when len(src) is 0
		32  	{
		33  		"",
		34  		"",
		35  	},
		36  	// Wikipedia example
		37  	bigtest,
		38  	// Special case when shortening !!!!! to z.
		39  	{
		40  		"\000\000\000\000",
		41  		"z",
		42  	},
		43  }
		44  
		45  func testEqual(t *testing.T, msg string, args ...interface{}) bool {
		46  	t.Helper()
		47  	if args[len(args)-2] != args[len(args)-1] {
		48  		t.Errorf(msg, args...)
		49  		return false
		50  	}
		51  	return true
		52  }
		53  
		54  func strip85(s string) string {
		55  	t := make([]byte, len(s))
		56  	w := 0
		57  	for r := 0; r < len(s); r++ {
		58  		c := s[r]
		59  		if c > ' ' {
		60  			t[w] = c
		61  			w++
		62  		}
		63  	}
		64  	return string(t[0:w])
		65  }
		66  
		67  func TestEncode(t *testing.T) {
		68  	for _, p := range pairs {
		69  		buf := make([]byte, MaxEncodedLen(len(p.decoded)))
		70  		n := Encode(buf, []byte(p.decoded))
		71  		buf = buf[0:n]
		72  		testEqual(t, "Encode(%q) = %q, want %q", p.decoded, strip85(string(buf)), strip85(p.encoded))
		73  	}
		74  }
		75  
		76  func TestEncoder(t *testing.T) {
		77  	for _, p := range pairs {
		78  		bb := &bytes.Buffer{}
		79  		encoder := NewEncoder(bb)
		80  		encoder.Write([]byte(p.decoded))
		81  		encoder.Close()
		82  		testEqual(t, "Encode(%q) = %q, want %q", p.decoded, strip85(bb.String()), strip85(p.encoded))
		83  	}
		84  }
		85  
		86  func TestEncoderBuffering(t *testing.T) {
		87  	input := []byte(bigtest.decoded)
		88  	for bs := 1; bs <= 12; bs++ {
		89  		bb := &bytes.Buffer{}
		90  		encoder := NewEncoder(bb)
		91  		for pos := 0; pos < len(input); pos += bs {
		92  			end := pos + bs
		93  			if end > len(input) {
		94  				end = len(input)
		95  			}
		96  			n, err := encoder.Write(input[pos:end])
		97  			testEqual(t, "Write(%q) gave error %v, want %v", input[pos:end], err, error(nil))
		98  			testEqual(t, "Write(%q) gave length %v, want %v", input[pos:end], n, end-pos)
		99  		}
	 100  		err := encoder.Close()
	 101  		testEqual(t, "Close gave error %v, want %v", err, error(nil))
	 102  		testEqual(t, "Encoding/%d of %q = %q, want %q", bs, bigtest.decoded, strip85(bb.String()), strip85(bigtest.encoded))
	 103  	}
	 104  }
	 105  
	 106  func TestDecode(t *testing.T) {
	 107  	for _, p := range pairs {
	 108  		dbuf := make([]byte, 4*len(p.encoded))
	 109  		ndst, nsrc, err := Decode(dbuf, []byte(p.encoded), true)
	 110  		testEqual(t, "Decode(%q) = error %v, want %v", p.encoded, err, error(nil))
	 111  		testEqual(t, "Decode(%q) = nsrc %v, want %v", p.encoded, nsrc, len(p.encoded))
	 112  		testEqual(t, "Decode(%q) = ndst %v, want %v", p.encoded, ndst, len(p.decoded))
	 113  		testEqual(t, "Decode(%q) = %q, want %q", p.encoded, string(dbuf[0:ndst]), p.decoded)
	 114  	}
	 115  }
	 116  
	 117  func TestDecoder(t *testing.T) {
	 118  	for _, p := range pairs {
	 119  		decoder := NewDecoder(strings.NewReader(p.encoded))
	 120  		dbuf, err := io.ReadAll(decoder)
	 121  		if err != nil {
	 122  			t.Fatal("Read failed", err)
	 123  		}
	 124  		testEqual(t, "Read from %q = length %v, want %v", p.encoded, len(dbuf), len(p.decoded))
	 125  		testEqual(t, "Decoding of %q = %q, want %q", p.encoded, string(dbuf), p.decoded)
	 126  		if err != nil {
	 127  			testEqual(t, "Read from %q = %v, want %v", p.encoded, err, io.EOF)
	 128  		}
	 129  	}
	 130  }
	 131  
	 132  func TestDecoderBuffering(t *testing.T) {
	 133  	for bs := 1; bs <= 12; bs++ {
	 134  		decoder := NewDecoder(strings.NewReader(bigtest.encoded))
	 135  		buf := make([]byte, len(bigtest.decoded)+12)
	 136  		var total int
	 137  		var n int
	 138  		var err error
	 139  		for total = 0; total < len(bigtest.decoded) && err == nil; {
	 140  			n, err = decoder.Read(buf[total : total+bs])
	 141  			total += n
	 142  		}
	 143  		if err != nil && err != io.EOF {
	 144  			t.Errorf("Read from %q at pos %d = %d, unexpected error %v", bigtest.encoded, total, n, err)
	 145  		}
	 146  		testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded)
	 147  	}
	 148  }
	 149  
	 150  func TestDecodeCorrupt(t *testing.T) {
	 151  	type corrupt struct {
	 152  		e string
	 153  		p int
	 154  	}
	 155  	examples := []corrupt{
	 156  		{"v", 0},
	 157  		{"!z!!!!!!!!!", 1},
	 158  	}
	 159  
	 160  	for _, e := range examples {
	 161  		dbuf := make([]byte, 4*len(e.e))
	 162  		_, _, err := Decode(dbuf, []byte(e.e), true)
	 163  		switch err := err.(type) {
	 164  		case CorruptInputError:
	 165  			testEqual(t, "Corruption in %q at offset %v, want %v", e.e, int(err), e.p)
	 166  		default:
	 167  			t.Error("Decoder failed to detect corruption in", e)
	 168  		}
	 169  	}
	 170  }
	 171  
	 172  func TestBig(t *testing.T) {
	 173  	n := 3*1000 + 1
	 174  	raw := make([]byte, n)
	 175  	const alpha = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	 176  	for i := 0; i < n; i++ {
	 177  		raw[i] = alpha[i%len(alpha)]
	 178  	}
	 179  	encoded := new(bytes.Buffer)
	 180  	w := NewEncoder(encoded)
	 181  	nn, err := w.Write(raw)
	 182  	if nn != n || err != nil {
	 183  		t.Fatalf("Encoder.Write(raw) = %d, %v want %d, nil", nn, err, n)
	 184  	}
	 185  	err = w.Close()
	 186  	if err != nil {
	 187  		t.Fatalf("Encoder.Close() = %v want nil", err)
	 188  	}
	 189  	decoded, err := io.ReadAll(NewDecoder(encoded))
	 190  	if err != nil {
	 191  		t.Fatalf("io.ReadAll(NewDecoder(...)): %v", err)
	 192  	}
	 193  
	 194  	if !bytes.Equal(raw, decoded) {
	 195  		var i int
	 196  		for i = 0; i < len(decoded) && i < len(raw); i++ {
	 197  			if decoded[i] != raw[i] {
	 198  				break
	 199  			}
	 200  		}
	 201  		t.Errorf("Decode(Encode(%d-byte string)) failed at offset %d", n, i)
	 202  	}
	 203  }
	 204  
	 205  func TestDecoderInternalWhitespace(t *testing.T) {
	 206  	s := strings.Repeat(" ", 2048) + "z"
	 207  	decoded, err := io.ReadAll(NewDecoder(strings.NewReader(s)))
	 208  	if err != nil {
	 209  		t.Errorf("Decode gave error %v", err)
	 210  	}
	 211  	if want := []byte("\000\000\000\000"); !bytes.Equal(want, decoded) {
	 212  		t.Errorf("Decode failed: got %v, want %v", decoded, want)
	 213  	}
	 214  }
	 215  

View as plain text