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 pairs = []testpair{ 20 // Encode returns 0 when len(src) is 0 21 { 22 "", 23 "", 24 }, 25 // Wikipedia example 26 { 27 "Man is distinguished, not only by his reason, but by this singular passion from " + 28 "other animals, which is a lust of the mind, that by a perseverance of delight in " + 29 "the continued and indefatigable generation of knowledge, exceeds the short " + 30 "vehemence of any carnal pleasure.", 31 "9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,\n" + 32 "O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKY\n" + 33 "i(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIa\n" + 34 "l(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G\n" + 35 ">uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c\n", 36 }, 37 // Special case when shortening !!!!! to z. 38 { 39 "\000\000\000\000", 40 "z", 41 }, 42 } 43 44 var bigtest = pairs[len(pairs)-1] 45 46 func testEqual(t *testing.T, msg string, args ...interface{}) bool { 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 := ioutil.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 for total = 0; total < len(bigtest.decoded); { 138 n, err := decoder.Read(buf[total : total+bs]) 139 testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, error(nil)) 140 total += n 141 } 142 testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded) 143 } 144 } 145 146 func TestDecodeCorrupt(t *testing.T) { 147 type corrupt struct { 148 e string 149 p int 150 } 151 examples := []corrupt{ 152 {"v", 0}, 153 {"!z!!!!!!!!!", 1}, 154 } 155 156 for _, e := range examples { 157 dbuf := make([]byte, 4*len(e.e)) 158 _, _, err := Decode(dbuf, []byte(e.e), true) 159 switch err := err.(type) { 160 case CorruptInputError: 161 testEqual(t, "Corruption in %q at offset %v, want %v", e.e, int(err), e.p) 162 default: 163 t.Error("Decoder failed to detect corruption in", e) 164 } 165 } 166 } 167 168 func TestBig(t *testing.T) { 169 n := 3*1000 + 1 170 raw := make([]byte, n) 171 const alpha = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 172 for i := 0; i < n; i++ { 173 raw[i] = alpha[i%len(alpha)] 174 } 175 encoded := new(bytes.Buffer) 176 w := NewEncoder(encoded) 177 nn, err := w.Write(raw) 178 if nn != n || err != nil { 179 t.Fatalf("Encoder.Write(raw) = %d, %v want %d, nil", nn, err, n) 180 } 181 err = w.Close() 182 if err != nil { 183 t.Fatalf("Encoder.Close() = %v want nil", err) 184 } 185 decoded, err := ioutil.ReadAll(NewDecoder(encoded)) 186 if err != nil { 187 t.Fatalf("io.ReadAll(NewDecoder(...)): %v", err) 188 } 189 190 if !bytes.Equal(raw, decoded) { 191 var i int 192 for i = 0; i < len(decoded) && i < len(raw); i++ { 193 if decoded[i] != raw[i] { 194 break 195 } 196 } 197 t.Errorf("Decode(Encode(%d-byte string)) failed at offset %d", n, i) 198 } 199 } 200 201 func TestDecoderInternalWhitespace(t *testing.T) { 202 s := strings.Repeat(" ", 2048) + "z" 203 decoded, err := ioutil.ReadAll(NewDecoder(strings.NewReader(s))) 204 if err != nil { 205 t.Errorf("Decode gave error %v", err) 206 } 207 if want := []byte("\000\000\000\000"); !bytes.Equal(want, decoded) { 208 t.Errorf("Decode failed: got %v, want %v", decoded, want) 209 } 210 } 211