...
Run Format

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

View as plain text